File: utmp.c

package info (click to toggle)
eterm 0.7-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,024 kB
  • ctags: 1,205
  • sloc: ansic: 14,536; sh: 308; makefile: 294
file content (559 lines) | stat: -rw-r--r-- 14,338 bytes parent folder | download
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*--------------------------------*-C-*---------------------------------*
 * File:	utmp.c
 *
 * Public:
 *	extern void cleanutent (void);
 *	extern void makeutent (const char * pty, const char * hostname);
 *
 * Private:
 *	get_tslot ();
 *	write_utmp ();
 *	update_wtmp ();
 *
 * As usual, the author accepts no responsibility for anything, nor does
 * he guarantee anything whatsoever.
 * ---------------------------------------------------------------------*/
/* notes: */
/*----------------------------------------------------------------------*
 * Extensive modifications by mj olesen <olesen@me.QueensU.CA>
 * No additional restrictions are applied.
 *
 * Revision 1.6  07DEC95:16	Piet W. Plomp,
 * wtmp support for (at least linux)
 *
 * Revision 1.55  1995/10/16  16:04:16  rgg
 * now works in Solaris 2.x and Linux 1.2.x
 *
 * Revision 1.5 08/09/1993 stempien
 * Something was wrong with the Linux support!
 * I fixed it using the provided utmp manipulation routines.
 * I also surrounded many varables that were not needed for Linux
 * in the BSD defines to reduce the memory needed to run.
 * I didn't touch the Sun part of the code so it should still work.
 *
 * Revision 1.4  1993/08/09  11:54:15  lipka
 * now works both on Linux and SunOs 4.1.3.
 * Brians clean-ups incorporated
 *
 * Revision 1.3  1993/08/09  07:16:42  lipka
 * nearly correct (running) linux-version.
 * Delete-Window is still a problem
 *
 * Revision 1.1  1993/08/07  18:03:53  lipka
 * Initial revision
 *
 * Clean-ups according to suggestions of Brian Stempien <stempien@cs.wmich.edu>
 *
 *    Bugs:
 *	UTMP should be locked from call to utmp_end() 'til makeutent() (?).
 *	Maybe the child should tell the parent, where the entry is made.
 *	Tested only on Linux.
 *
 *	Gives weird inital idle times. They go away after one uses the
 *	window, but......
 * ------------------------------------------------------------------------
 * This version has SYSV wtmp support for (at least Linux) added by:
 *    Piet W. Plomp,
 *    ICCE - Institute for educational technology,
 *    State University of Groningen, The Netherlands,
 *    <piet@icce.rug.nl> or (faster) <piet@idefix.icce.rug.nl>
 *
 * all WTMP specific code is #ifdef'd WTMP_SUPPORT, which currently depends
 * on UTMP_SUPPORT.
 * This code is valid and tested on linux (libc.so.4.6.27). but is
 * POSIX compliant.
 *
 * My additions are tagged with an entry like "... pwp 95-12-07", where the
 * former are my initials and the latter the date in yy-mm-dd format.
 *----------------------------------------------------------------------*/
/* includes, defines */
#include "config.h"
#include "feature.h"
#include "screen.h"

int my_ruid, my_euid, my_rgid, my_egid;  /* These are needed regardless of utmp support -- mej */

#ifndef UTMP_SUPPORT
/* Dummy routines if utmp support isn't wanted */
void cleanutent (void) {}
void makeutent (const char * pty, const char * hostname) {}
#else /* UTMP_SUPPORT */

#include <stdio.h>
#include <string.h>

/* For some systems (HP-UX in particular), sys/types.h must be included
   before utmp*.h -- mej */
#include <sys/types.h>
#include <sys/stat.h>

#if defined(_HPUX_SOURCE) || defined(_AIX)
#  undef HAVE_UTMPX_H
#endif

#ifdef HAVE_UTMPX_H
# include <utmpx.h>
# define USE_SYSV_UTMP
#else
# include <utmp.h>
# ifdef HAVE_SETUTENT
#  define USE_SYSV_UTMP
# endif
#endif

#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_LASTLOG_H
# include <lastlog.h>
#endif
#include <pwd.h>

/* WTMP_SUPPORT added pwp 95-12-07 */
#define WTMP_SUPPORT

#include <errno.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif

#ifndef UTMP_FILENAME
# ifdef UTMP_FILE
#  define UTMP_FILENAME UTMP_FILE
# else
#  ifdef _PATH_UTMP
#   define UTMP_FILENAME _PATH_UTMP
#  else
#   define UTMP_FILENAME "/etc/utmp"
#  endif
# endif
#endif

#ifndef LASTLOG_FILENAME
# ifdef _PATH_LASTLOG
#  define LASTLOG_FILENAME _PATH_LASTLOG
# else
#  define LASTLOG_FILENAME "/usr/adm/lastlog"  /* only on BSD systems */
# endif
#endif

#ifndef WTMP_FILENAME
# ifdef WTMP_FILE
#  define WTMP_FILENAME WTMP_FILE
# else
#  ifdef _PATH_WTMP
#   define WTMP_FILENAME _PATH_WTMP
#  else
#   ifdef SYSV
#    define WTMP_FILENAME "/etc/wtmp"
#   else
#    define WTMP_FILENAME "/usr/adm/wtmp"
#   endif
#  endif
# endif
#endif

#ifndef TTYTAB_FILENAME
# ifdef TTYTAB
#  define TTYTAB_FILENAME TTYTAB_FILENAME
# else
#  define TTYTAB_FILENAME "/etc/ttytab"
# endif
#endif

#ifndef USER_PROCESS
# define USER_PROCESS 7
#endif
#ifndef DEAD_PROCESS
# define DEAD_PROCESS 8
#endif

/* extern functions referenced */

/* extern variables referenced */

/* extern variables declared here */

/* local variables */
/* don't go off end of ut_id & remember if an entry has been made */
#ifdef UTMP_SUPPORT
# if defined(USE_SYSV_UTMP) || defined(__FreeBSD__)
static char     ut_id[5];	/* remember if entry to utmp made */
# else
static int      utmp_pos;	/* BSD position of utmp-stamp */
# endif
#endif

/* local functions referenced */

/*----------------------------------------------------------------------*/

/*
 * HAVE_SETUTENT corresponds to SYSV-style utmp support.
 * Without it corresponds to using BSD utmp support.
 *
 * SYSV-style utmp support is further divided in normal utmp support
 * and utmpx support (Solaris 2.x) by HAVE_UTMPX_H
 */
#ifdef USE_SYSV_UTMP

/* update_wtmp() */
/*
 *  added by pwp 95 12 07
 * called by makeutent() and cleanutent() below
 */
# ifdef WTMP_SUPPORT
#  ifdef HAVE_UTMPX_H
#   undef WTMP_FILENAME
#   define WTMP_FILENAME WTMPX_FILE
#   define update_wtmp updwtmpx
#  else		/* HAVE_UTMPX_H */
static void
update_wtmp (char * fname, struct utmp * putmp)
{
   int fd, retry = 10;		/* 10 attempts at locking */
   struct flock lck;		/* fcntl locking scheme */

   if ((fd = open (fname, O_WRONLY|O_APPEND, 0)) < 0) {
#   ifdef DEBUG_UTMP
     print_error("Unable to open \"%s\" for writing -- %s\n",
		 fname, strerror(errno));
#   endif
     return;
   }

   lck.l_whence = SEEK_END;	/* start lock at current eof */
   lck.l_len    = 0;		/* end at ``largest possible eof'' */
   lck.l_start  = 0;
   lck.l_type   = F_WRLCK;	/* we want a write lock */

#   ifndef EACCESS
#    ifdef EAGAIN
#     define EACCESS EAGAIN
#    endif
#   endif

   /* attempt lock with F_SETLK - F_SETLKW would cause a deadlock! */
   while (retry--)
     if ((fcntl (fd, F_SETLK, &lck) < 0) && errno != EACCESS) {
#   ifdef DEBUG_UTMP
       print_error("Unable to establish file lock on \"%s\" -- %s\n",
		   fname, strerror(errno));
#   endif
	close (fd);
	return;			/* failed for unknown reason: give up */
     }
#   ifdef DEBUG_UTMP
     else if (errno == EACCESS) {
       print_error("Unable to establish file lock on \"%s\" -- %s\n",
		   fname, strerror(errno));
     }
#   endif

   write (fd, putmp, sizeof(struct utmp));

   /* unlocking the file */
   lck.l_type = F_UNLCK;
   fcntl (fd, F_SETLK, &lck);

   close (fd);
}
#  endif	/* HAVE_UTMPX_H */
# endif	/* WTMP_SUPPORT */

/* makeutent() - make a utmp entry */
void
makeutent (const char * pty, const char * hostname)
{
   struct passwd * pwent = getpwuid (getuid ());
#ifdef HAVE_UTMPX_H
   struct utmpx utmp;
   struct utmp utmp2;
   memset (&utmp, 0, sizeof(struct utmpx));
#else
   struct utmp utmp;
   memset (&utmp, 0, sizeof(struct utmp));
#endif

   if (!strncmp (pty, "/dev/", 5)) pty += 5;	/* skip /dev/ prefix */
   if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
     strncpy (ut_id, (pty+3), sizeof(ut_id));	/* bsd naming */
   else
     {
	int n;
	if (sscanf (pty, "pts/%d", &n) == 1)
	  sprintf (ut_id, "vt%02x", n);		/* sysv naming */
	else
	  {
	     print_error ("can't parse tty name \"%s\"", pty);
	     ut_id [0] = '\0';	/* entry not made */
	     return;
	  }
     }
   strncpy (utmp.ut_id, ut_id, sizeof(utmp.ut_id));
   utmp.ut_type = DEAD_PROCESS;

   privileges(INVOKE);
#ifdef HAVE_UTMPX_H
   getutmp (&utmp, &utmp2);
   getutid (&utmp2);		/* position to entry in utmp file */
#else
   getutid (&utmp);		/* position to entry in utmp file */
#endif

   /* set up the new entry */
   strncpy (utmp.ut_id, ut_id, sizeof(utmp.ut_id));
   strncpy (utmp.ut_line, pty, sizeof(utmp.ut_line));
   strncpy (utmp.ut_name, pwent->pw_name, sizeof(utmp.ut_name));
   strncpy (utmp.ut_user, pwent->pw_name, sizeof(utmp.ut_user));
   strncpy (utmp.ut_host, hostname, sizeof(utmp.ut_host));
#ifndef linux
     {
	char * colon = strrchr (utmp.ut_host, ':');
	if (colon) *colon = '\0';
     }
#endif

   utmp.ut_type = USER_PROCESS;
   utmp.ut_pid = getpid ();

#ifdef HAVE_UTMPX_H
   utmp.ut_session = getsid (0);
   utmp.ut_xtime = time (NULL);
   utmp.ut_tv.tv_usec = 0;
#else
   utmp.ut_time = time (NULL);
#endif

   /*
    * write a utmp entry to the utmp file
    */
   utmpname (UTMP_FILENAME);
#ifdef HAVE_UTMPX_H
   getutmp (&utmp, &utmp2);
   pututline (&utmp2);
   pututxline (&utmp);
#else
   /* if (!utmpInhibit) */
   pututline (&utmp);
#endif
#ifdef WTMP_SUPPORT
   update_wtmp(WTMP_FILENAME, &utmp);
#endif
   endutent ();			/* close the file */
   privileges(REVERT);
}

/* cleanutent() - remove a utmp entry */
/*----------------------------------------------------------------------*
 * there is a nice function "endutent" defined in <utmp.h>;
 * like "setutent" it takes no arguments, so I think it gets all information
 * from library-calls.
 * That's why "setutent" has to be called by the child-process with
 * file-descriptors 0/1 set to the pty. But that child execs to the
 * application-program and therefore can't clean it's own utmp-entry!(?)
 * The master on the other hand doesn't have the correct process-id
 * and io-channels... I'll do it by hand:
 * (what's the position of the utmp-entry, the child wrote? :-)
 *----------------------------------------------------------------------*/
void
cleanutent (void)
{
#ifdef HAVE_UTMPX_H
   struct utmp utmp;
   struct utmpx utmpx;

   if (!ut_id[0]) return;	/* entry not made */

   privileges(INVOKE);
   utmpname (UTMP_FILENAME);
   setutent ();
   if (getutid (&utmp) == NULL) return;
   utmp.ut_type = DEAD_PROCESS;
   utmp.ut_time = time (NULL);
   pututline (&utmp);
   getutmpx (&utmp, &utmpx);
#ifdef WTMP_SUPPORT
   update_wtmp(WTMP_FILENAME, &utmpx);
#endif
   endutent ();
   privileges(REVERT);

#else	/* HAVE_UTMPX_H */
   struct utmp * putmp;
   pid_t pid = getpid ();

   if (!ut_id[0]) return;	/* entry not made */

   privileges(INVOKE);
   utmpname (UTMP_FILENAME);
   setutent ();
   /*
    * The following code waw copied from the poeigl-1.20 login/init package.
    * Special thanks to poe for the code examples.
    */
   while ((putmp = getutent ()) != NULL)
     {
	if (putmp->ut_pid == pid)
	  {
	     putmp->ut_type = DEAD_PROCESS;
	     putmp->ut_pid = 0;
	     putmp->ut_user[0] = '\0';
	     putmp->ut_time = time (NULL);
	     pututline (putmp);
             update_wtmp (WTMP_FILENAME, putmp);
	     break;
	  }
     }
   endutent ();
   privileges(REVERT);
#endif	/* HAVE_UTMPX_H */
}

#else	/* USE_SYSV_UTMP */
/* BSD utmp support */

#ifdef __FreeBSD__
/* used to hold the line we are using */
static char ut_line[32];
#else /* __FreeBSD__ */
static int utmp_pos = 0;		/* position of utmp-stamp */

/*----------------------------------------------------------------------*
 * get_tslot() - grabbed from xvt-1.0 - modified by David Perry
 *
 * find ttyname in /etc/ttytab and return a slot number that can be used to
 * access the utmp file.  Can't use ttyslot() because the tty name is not
 * that of fd 0.
 *----------------------------------------------------------------------*/
static int
get_tslot (const char * ttyname)
{
   char buf [256], name [256];
   FILE *fd;

   if ((fd = fopen (UTMP_FILENAME, "r")) != NULL)
     {
	int i;
	for (i = 1; fgets (buf, sizeof(buf), fd) != NULL; i++)
	  {
	     if (*buf == '#' || sscanf (buf, "%s", name) != 1) continue;
	     if (!strcmp (ttyname, name))
	       {
		  fclose (fd);
		  return i;
	       }
	  }
	fclose (fd);
     }
   return -1;
}

/*
 * write utmp entry to UTMP_FILENAME
 */
static int
write_utmp (struct utmp * putmp)
{
   int rval = 0;
   FILE * fd;
   fprintf(stderr, "Utmp file is %s\n", UTMP_FILENAME);
   privileges(INVOKE);
   if ((fd = fopen (UTMP_FILENAME, "r+")) != NULL)
     {
	utmp_pos = get_tslot (putmp->ut_line) * sizeof(struct utmp);
	if (utmp_pos >= 0)
	  {
	     fseek (fd, utmp_pos, 0);
	     fwrite (putmp, sizeof(struct utmp), 1, fd);
	     rval = 1;
	  }
	fclose (fd);
     }
   privileges(REVERT);
   return rval;
}

#endif /* __FreeBSD__ */

/*
 * make a utmp entry
 */
void
makeutent (const char * pty, const char * hostname)
{
   struct passwd * pwent = getpwuid (getuid ());
   struct utmp utmp;

   memset (&utmp, 0, sizeof(struct utmp));

   if (!strncmp (pty, "/dev/", 5)) pty += 5;	/* skip /dev/ prefix */
   if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
     strncpy (ut_id, (pty+3), sizeof(ut_id));	/* bsd naming */
   else
     {
	print_error ("can't parse tty name \"%s\"", pty);
	ut_id [0] = '\0';	/* entry not made */
	return;
     }

   strncpy (utmp.ut_line, pty, sizeof(utmp.ut_line));
   strncpy (utmp.ut_name, pwent->pw_name, sizeof(utmp.ut_name));
   strncpy (utmp.ut_host, hostname, sizeof(utmp.ut_host));
   utmp.ut_time = time (NULL);


#ifdef __FreeBSD__
  strncpy (ut_line, pty, 31);

  strncpy (utmp.ut_line, pty, UT_LINESIZE);
  strncpy (utmp.ut_name, pwent->pw_name, UT_NAMESIZE);
  strncpy (utmp.ut_host, hostname, UT_HOSTSIZE);
  utmp.ut_time = time (0);

  login(&utmp);
#else  /* __FreeBSD__ */
   if (write_utmp (&utmp) < 0)
     ut_id [0] = '\0';		/* entry not made */
#endif
}

/*
 * remove a utmp entry
 */
void
cleanutent (void)
{
#if defined(__FreeBSD__)
  logout (ut_line);
  logwtmp(ut_line, "", "");
#else  /* __FreeBSD__ */
   FILE * fd;

   privileges(INVOKE);
   if (!ut_id [0] && (fd = fopen (UTMP_FILENAME, "r+")) != NULL)
     {
	struct utmp utmp;
	memset (&utmp, 0, sizeof(struct utmp));
	fseek (fd, utmp_pos, 0);
	fwrite (&utmp, sizeof(struct utmp), 1, fd);
	fclose (fd);
     }
   privileges(REVERT);
#endif  /* __FreeBSD__ */
}
#endif	/* USE_SYSV_UTMP */
#endif	/* UTMP_SUPPORT */
/*----------------------- end-of-file (C source) -----------------------*/