Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
--- utmpx.c.orig	2006-02-06 00:43:57.000000000 -0800
+++ utmpx.c	2006-02-06 00:51:52.000000000 -0800
@@ -49,34 +49,27 @@
 #include <sys/time.h>
 #include <sys/wait.h>
 
-#include <assert.h>
-#include <db.h>
-#include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <utmp.h>
-/* don't define earlier, has side effects in fcntl.h */
-#define __LIBC12_SOURCE__
 #include <utmpx.h>
+#include <utmpx-darwin.h>
+#include <errno.h>
 #include <vis.h>
-
-__warn_references(getlastlogx,
-    "warning: reference to compatibility getlastlogx(); include <utmpx.h> for correct reference")
-__warn_references(lastlogxname,
-    "warning: reference to deprecated lastlogxname()")
+#include <notify.h>
 
 static FILE *fp;
 static int readonly = 0;
 static struct utmpx ut;
 static char utfile[MAXPATHLEN] = _PATH_UTMPX;
-static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
+__private_extern__ int utfile_system = 1; /* are we using _PATH_UTMPX? */
 
-static struct utmpx *utmp_update(const struct utmpx *);
+static struct utmpx *_getutxid(const struct utmpx *);
 
-static const char vers[] = "utmpx-1.00";
+__private_extern__ const char _utmpx_vers[] = "utmpx-1.00";
 
 void
 setutxent()
@@ -85,7 +78,11 @@
 	(void)memset(&ut, 0, sizeof(ut));
 	if (fp == NULL)
 		return;
+#ifdef __LP64__
+	(void)fseeko(fp, (off_t)sizeof(struct utmpx32), SEEK_SET);
+#else /* __LP64__ */
 	(void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
+#endif /* __LP64__ */
 }
 
 
@@ -105,6 +102,9 @@
 struct utmpx *
 getutxent()
 {
+#ifdef __LP64__
+	struct utmpx32 ut32;
+#endif /* __LP64__ */
 
 	if (fp == NULL) {
 		struct stat st;
@@ -124,42 +124,80 @@
 
 		if (st.st_size == 0) {
 			/* new file, add signature record */
+#ifdef __LP64__
+			(void)memset(&ut32, 0, sizeof(ut32));
+			ut32.ut_type = SIGNATURE;
+			(void)memcpy(ut32.ut_user, _utmpx_vers, sizeof(_utmpx_vers));
+			if (fwrite(&ut32, sizeof(ut32), 1, fp) != 1)
+#else /* __LP64__ */
 			(void)memset(&ut, 0, sizeof(ut));
 			ut.ut_type = SIGNATURE;
-			(void)memcpy(ut.ut_user, vers, sizeof(vers));
+			(void)memcpy(ut.ut_user, _utmpx_vers, sizeof(_utmpx_vers));
 			if (fwrite(&ut, sizeof(ut), 1, fp) != 1)
+#endif /* __LP64__ */
 				goto failclose;
 		} else {
 			/* old file, read signature record */
+#ifdef __LP64__
+			if (fread(&ut32, sizeof(ut32), 1, fp) != 1)
+#else /* __LP64__ */
 			if (fread(&ut, sizeof(ut), 1, fp) != 1)
+#endif /* __LP64__ */
 				goto failclose;
-			if (memcmp(ut.ut_user, vers, sizeof(vers)) != 0 ||
+#ifdef __LP64__
+			if (memcmp(ut32.ut_user, _utmpx_vers, sizeof(_utmpx_vers)) != 0 ||
+			    ut32.ut_type != SIGNATURE)
+#else /* __LP64__ */
+			if (memcmp(ut.ut_user, _utmpx_vers, sizeof(_utmpx_vers)) != 0 ||
 			    ut.ut_type != SIGNATURE)
+#endif /* __LP64__ */
 				goto failclose;
 		}
 	}
 
+#ifdef __LP64__
+	if (fread(&ut32, sizeof(ut32), 1, fp) != 1)
+#else /* __LP64__ */
 	if (fread(&ut, sizeof(ut), 1, fp) != 1)
+#endif /* __LP64__ */
 		goto fail;
 
+#ifdef __LP64__
+	_utmpx32_64(&ut32, &ut);
+#endif /* __LP64__ */
 	return &ut;
 failclose:
 	(void)fclose(fp);
+	fp = NULL;
 fail:
 	(void)memset(&ut, 0, sizeof(ut));
 	return NULL;
 }
 
-
 struct utmpx *
 getutxid(const struct utmpx *utx)
 {
+	struct utmpx temp;
+	const struct utmpx *ux;
 
 	_DIAGASSERT(utx != NULL);
 
 	if (utx->ut_type == EMPTY)
 		return NULL;
 
+	/* make a copy as needed, and auto-fill if requested */
+	ux = _utmpx_working_copy(utx, &temp, 1);
+	if (!ux)
+		return NULL;
+
+	return _getutxid(ux);
+}
+
+
+static struct utmpx *
+_getutxid(const struct utmpx *utx)
+{
+
 	do {
 		if (ut.ut_type == EMPTY)
 			continue;
@@ -225,30 +263,68 @@
 struct utmpx *
 pututxline(const struct utmpx *utx)
 {
-	struct utmpx temp, *u = NULL;
-	int gotlock = 0;
+	struct utmpx *ux;
 
 	_DIAGASSERT(utx != NULL);
 
-	if (utx == NULL)
+	if (utx == NULL) {
+		errno = EINVAL;
 		return NULL;
+	}
 
-	if (strcmp(_PATH_UTMPX, utfile) == 0)
-		if ((fp != NULL && readonly) || (fp == NULL && geteuid() != 0))
-			return utmp_update(utx);
+	if ((ux = _pututxline(utx)) != NULL && utfile_system) {
+		_utmpx_asl(ux);	/* the equivalent of wtmpx and lastlogx */
+#ifdef UTMP_COMPAT
+		_write_utmp_compat(ux);
+#endif /* UTMP_COMPAT */
+	}
+	return ux;
+}
 
+__private_extern__ struct utmpx *
+_pututxline(const struct utmpx *utx)
+{
+	struct utmpx temp, *u = NULL, *x;
+	const struct utmpx *ux;
+#ifdef __LP64__
+	struct utmpx32 ut32;
+#endif /* __LP64__ */
+	int gotlock = 0;
 
-	(void)memcpy(&temp, utx, sizeof(temp));
+	if (utfile_system)
+		if ((fp != NULL && readonly) || (fp == NULL && geteuid() != 0)) {
+			errno = EPERM;
+			return NULL;
+		}
 
 	if (fp == NULL) {
 		(void)getutxent();
-		if (fp == NULL || readonly)
+		if (fp == NULL || readonly) {
+			errno = EPERM;
 			return NULL;
+		}
 	}
 
-	if (getutxid(&temp) == NULL) {
+	/* make a copy as needed, and auto-fill if requested */
+	ux = _utmpx_working_copy(utx, &temp, 0);
+	if (!ux)
+		return NULL;
+
+	if ((x = _getutxid(ux)) == NULL) {
 		setutxent();
-		if (getutxid(&temp) == NULL) {
+		if ((x = _getutxid(ux)) == NULL) {
+			/*
+			 * utx->ut_type has any original mask bits, while
+			 * ux->ut_type has those mask bits removed.  If we
+			 * are trying to record a dead process, and
+			 * UTMPX_DEAD_IF_CORRESPONDING_MASK is set, then since
+			 * there is no matching entry, we return NULL.
+			 */
+			if (ux->ut_type == DEAD_PROCESS &&
+			    (utx->ut_type & UTMPX_DEAD_IF_CORRESPONDING_MASK)) {
+				errno = EINVAL;
+				return NULL;
+			}
 			if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
 				return NULL;
 			gotlock++;
@@ -258,99 +334,66 @@
 	}
 
 	if (!gotlock) {
+		/*
+		 * utx->ut_type has any original mask bits, while
+		 * ux->ut_type has those mask bits removed.  If we
+		 * are trying to record a dead process, if
+		 * UTMPX_DEAD_IF_CORRESPONDING_MASK is set, but the found
+		 * entry is not a (matching) USER_PROCESS, then return NULL.
+		 */
+		if (ux->ut_type == DEAD_PROCESS &&
+		    (utx->ut_type & UTMPX_DEAD_IF_CORRESPONDING_MASK) &&
+		    x->ut_type != USER_PROCESS) {
+			errno = EINVAL;
+			return NULL;
+		}
 		/* we are not appending */
+#ifdef __LP64__
+		if (fseeko(fp, -(off_t)sizeof(ut32), SEEK_CUR) == -1)
+#else /* __LP64__ */
 		if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
+#endif /* __LP64__ */
 			return NULL;
 	}
 
-	if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
+#ifdef __LP64__
+	_utmpx64_32(ux, &ut32);
+	if (fwrite(&ut32, sizeof (ut32), 1, fp) != 1)
+#else /* __LP64__ */
+	if (fwrite(ux, sizeof (*ux), 1, fp) != 1)
+#endif /* __LP64__ */
 		goto fail;
 
 	if (fflush(fp) == -1)
 		goto fail;
 
-	u = memcpy(&ut, &temp, sizeof(ut));
+	u = memcpy(&ut, ux, sizeof(ut));
+	notify_post(UTMPX_CHANGE_NOTIFICATION);
 fail:
 	if (gotlock) {
+		int save = errno;
 		if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
 			return NULL;
+		errno = save;
 	}
 	return u;
 }
 
 
-static struct utmpx *
-utmp_update(const struct utmpx *utx)
-{
-	char buf[sizeof(*utx) * 4 + 1];
-	pid_t pid;
-	int status;
-
-	_DIAGASSERT(utx != NULL);
-
-	(void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
-	    VIS_WHITE);
-	switch (pid = fork()) {
-	case 0:
-		(void)execl(_PATH_UTMP_UPDATE,
-		    strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf, NULL);
-		exit(1);
-		/*NOTREACHED*/
-	case -1:
-		return NULL;
-	default:
-		if (waitpid(pid, &status, 0) == -1)
-			return NULL;
-		if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
-			return memcpy(&ut, utx, sizeof(ut));
-		return NULL;
-	}
-
-}
-
 /*
  * The following are extensions and not part of the X/Open spec.
  */
 int
-updwtmpx(const char *file, const struct utmpx *utx)
-{
-	int fd;
-	int saved_errno;
-
-	_DIAGASSERT(file != NULL);
-	_DIAGASSERT(utx != NULL);
-
-	fd = open(file, O_WRONLY|O_APPEND|O_SHLOCK);
-
-	if (fd == -1) {
-		if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1)
-			return -1;
-		(void)memset(&ut, 0, sizeof(ut));
-		ut.ut_type = SIGNATURE;
-		(void)memcpy(ut.ut_user, vers, sizeof(vers));
-		if (write(fd, &ut, sizeof(ut)) == -1)
-			goto failed;
-	}
-	if (write(fd, utx, sizeof(*utx)) == -1)
-		goto failed;
-	if (close(fd) == -1)
-		return -1;
-	return 0;
-
-  failed:
-	saved_errno = errno;
-	(void) close(fd);
-	errno = saved_errno;
-	return -1;
-}
-
-
-int
 utmpxname(const char *fname)
 {
 	size_t len;
 
-	_DIAGASSERT(fname != NULL);
+	if (fname == NULL) {
+		strcpy(utfile, _PATH_UTMPX);
+		utfile_system = 1;
+		endutxent();
+		return 1;
+	}
 
 	len = strlen(fname);
 
@@ -363,6 +406,7 @@
 
 	(void)strlcpy(utfile, fname, sizeof(utfile));
 	endutxent();
+	utfile_system = 0;
 	return 1;
 }
 
@@ -371,10 +415,8 @@
 getutmp(const struct utmpx *ux, struct utmp *u)
 {
 
-	_DIAGASSERT(ux != NULL);
-	_DIAGASSERT(u != NULL);
-
-	(void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name));
+	bzero(u, sizeof(*u));
+	(void)memcpy(u->ut_name, ux->ut_user, sizeof(u->ut_name));
 	(void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line));
 	(void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host));
 	u->ut_time = ux->ut_tv.tv_sec;
@@ -384,109 +426,15 @@
 getutmpx(const struct utmp *u, struct utmpx *ux)
 {
 
-	_DIAGASSERT(ux != NULL);
-	_DIAGASSERT(u != NULL);
-
-	(void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name));
+	bzero(ux, sizeof(*ux));
+	(void)memcpy(ux->ut_user, u->ut_name, sizeof(u->ut_name));
+	ux->ut_user[sizeof(u->ut_name)] = 0;
 	(void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line));
+	ux->ut_line[sizeof(u->ut_line)] = 0;
 	(void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host));
+	ux->ut_host[sizeof(u->ut_host)] = 0;
 	ux->ut_tv.tv_sec = u->ut_time;
 	ux->ut_tv.tv_usec = 0;
-	(void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss));
-	ux->ut_pid = 0;
+	ux->ut_pid = getpid();
 	ux->ut_type = USER_PROCESS;
-	ux->ut_session = 0;
-	ux->ut_exit.e_termination = 0;
-	ux->ut_exit.e_exit = 0;
-}
-
-int
-lastlogxname(const char *fname)
-{
-	size_t len;
-
-	_DIAGASSERT(fname != NULL);
-
-	len = strlen(fname);
-
-	if (len >= sizeof(llfile))
-		return 0;
-
-	/* must end in x! */
-	if (fname[len - 1] != 'x')
-		return 0;
-
-	(void)strlcpy(llfile, fname, sizeof(llfile));
-	return 1;
-}
-
-struct lastlogx *
-getlastlogx(uid_t uid, struct lastlogx *ll)
-{
-
-	return __getlastlogx13(_PATH_LASTLOGX, uid, ll);
-}
-
-struct lastlogx *
-__getlastlogx13(const char *fname, uid_t uid, struct lastlogx *ll)
-{
-	DBT key, data;
-	DB *db;
-
-	_DIAGASSERT(fname != NULL);
-	_DIAGASSERT(ll != NULL);
-
-	db = dbopen(fname, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
-
-	if (db == NULL)
-		return NULL;
-
-	key.data = &uid;
-	key.size = sizeof(uid);
-
-	if ((db->get)(db, &key, &data, 0) != 0)
-		goto error;
-
-	if (data.size != sizeof(*ll)) {
-		errno = EFTYPE;
-		goto error;
-	}
-
-	if (ll == NULL)
-		if ((ll = malloc(sizeof(*ll))) == NULL)
-			goto done;
-
-	(void)memcpy(ll, data.data, sizeof(*ll));
-	goto done;
-error:
-	ll = NULL;
-done:
-	(db->close)(db);
-	return ll;
-}
-
-int
-updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
-{
-	DBT key, data;
-	int error = 0;
-	DB *db;
-
-	_DIAGASSERT(fname != NULL);
-	_DIAGASSERT(ll != NULL);
-
-	db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0, DB_HASH, NULL);
-
-	if (db == NULL)
-		return -1;
-
-	key.data = &uid;
-	key.size = sizeof(uid);
-	data.data = ll;
-	data.size = sizeof(*ll);
-	if ((db->put)(db, &key, &data, 0) != 0)
-		error = -1;
-
-	(db->close)(db);
-	return error;
 }