ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/logging.C
Revision: 1.6
Committed: Sun Jan 22 00:58:18 2006 UTC (20 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.5: +4 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.6 // This file is part of libptytty. Do not make local modifications.
2     // http://software.schmorp.de/pkg/libptytty
3    
4     /*----------------------------------------------------------------------*
5 root 1.1 * File: logging.C
6     *----------------------------------------------------------------------*
7     *
8     * All portions of code are copyright by their respective author/s.
9     * Copyright (c) 1992 John Bovey <jdb@ukc.ac.uk>
10     * - original version
11     * Copyright (c) 1993 lipka
12     * Copyright (c) 1993 Brian Stempien <stempien@cs.wmich.edu>
13     * Copyright (c) 1995 Raul Garcia Garcia <rgg@tid.es>
14     * Copyright (c) 1995 Piet W. Plomp <piet@idefix.icce.rug.nl>
15     * Copyright (c) 1997 Raul Garcia Garcia <rgg@tid.es>
16     * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
17     * - extensive modifications
18     * Copyright (c) 1999 D J Hawkey Jr <hawkeyd@visi.com>
19     * - lastlog support
20 root 1.2 * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com>
21 root 1.1 *
22     * This program is free software; you can redistribute it and/or modify
23     * it under the terms of the GNU General Public License as published by
24     * the Free Software Foundation; either version 2 of the License, or
25     * (at your option) any later version.
26     *
27     * This program is distributed in the hope that it will be useful,
28     * but WITHOUT ANY WARRANTY; without even the implied warranty of
29     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30     * GNU General Public License for more details.
31     *
32     * You should have received a copy of the GNU General Public License
33     * along with this program; if not, write to the Free Software
34     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35     *----------------------------------------------------------------------*/
36    
37 root 1.2 #include "../config.h"
38 root 1.4
39 root 1.2 #include "ptytty.h"
40    
41     #if UTMP_SUPPORT
42 root 1.1
43     #if HAVE_STRUCT_UTMP
44 root 1.3 static int write_bsd_utmp (int utmp_pos, struct utmp *wu);
45     static void update_wtmp (const char *fname, const struct utmp *putmp);
46 root 1.1 #endif
47    
48 root 1.3 static void update_lastlog (const char *fname, const char *pty, const char *host);
49 root 1.1
50     /*
51     * BSD style utmp entry
52     * ut_line, ut_name, ut_host, ut_time
53     * SYSV style utmp (and utmpx) entry
54     * ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time
55     */
56    
57     /* ------------------------------------------------------------------------- */
58    
59     /*
60     * make and write utmp and wtmp entries
61     */
62     void
63 root 1.3 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
64 root 1.1 {
65 root 1.2 const char *pty = name;
66    
67     if (!pty || !*pty)
68     return;
69    
70     this->cmd_pid = cmd_pid;
71     this->login_shell = login_shell;
72    
73 root 1.1 #ifdef HAVE_STRUCT_UTMP
74     struct utmp *ut = &this->ut;
75     #endif
76     #ifdef HAVE_STRUCT_UTMPX
77     struct utmpx *utx = &this->utx;
78     #endif
79     int i;
80     struct passwd *pwent = getpwuid (getuid ());
81    
82     if (!strncmp (pty, "/dev/", 5))
83     pty += 5; /* skip /dev/ prefix */
84    
85 root 1.2 #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
86 root 1.1 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
87     strncpy (ut_id, pty + 3, sizeof (ut_id));
88     else if (sscanf (pty, "pts/%d", &i) == 1)
89     sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
90     else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3))
91     {
92 root 1.3 warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
93 root 1.1 return;
94     }
95     #endif
96    
97     #ifdef HAVE_STRUCT_UTMP
98     memset (ut, 0, sizeof (struct utmp));
99     # ifdef HAVE_UTMP_PID
100     setutent ();
101     strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
102     ut->ut_type = DEAD_PROCESS;
103     getutid (ut); /* position to entry in utmp file */
104 root 1.2 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
105 root 1.1 # endif
106     #endif
107    
108     #ifdef HAVE_STRUCT_UTMPX
109     memset (utx, 0, sizeof (struct utmpx));
110     setutxent ();
111     strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
112     utx->ut_type = DEAD_PROCESS;
113     getutxid (utx); /* position to entry in utmp file */
114 root 1.2 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
115 root 1.1 #endif
116    
117     #ifdef HAVE_STRUCT_UTMP
118     strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
119     ut->ut_time = time (NULL);
120     # ifdef HAVE_UTMP_PID
121     strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
122     sizeof (ut->ut_user));
123     strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
124     ut->ut_time = time (NULL);
125     ut->ut_pid = cmd_pid;
126     # ifdef HAVE_UTMP_HOST
127     strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
128     # endif
129     ut->ut_type = USER_PROCESS;
130     pututline (ut);
131     endutent (); /* close the file */
132     utmp_pos = 0;
133     # else
134     strncpy (ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
135     sizeof (ut->ut_name));
136     # ifdef HAVE_UTMP_HOST
137     strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
138     # endif
139     # endif
140     #endif
141    
142     #ifdef HAVE_STRUCT_UTMPX
143     strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
144     strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
145     sizeof (utx->ut_user));
146     strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
147     # if HAVE_UTMPX_SESSION
148     utx->ut_session = getsid (0);
149     # endif
150     utx->ut_tv.tv_sec = time (NULL);
151     utx->ut_tv.tv_usec = 0;
152     utx->ut_pid = cmd_pid;
153     # ifdef HAVE_UTMPX_HOST
154     strncpy (utx->ut_host, hostname, sizeof (utx->ut_host));
155     # if 0
156     {
157     char *colon;
158    
159     if ((colon = strrchr (ut->ut_host, ':')) != NULL)
160     *colon = '\0';
161     }
162     # endif
163     # endif
164     utx->ut_type = USER_PROCESS;
165     pututxline (utx);
166     endutxent (); /* close the file */
167     utmp_pos = 0;
168     #endif
169    
170     #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
171     {
172     # ifdef HAVE_TTYSLOT
173 root 1.2 int fdstdin = dup (STDIN_FILENO);
174     dup2 (tty, STDIN_FILENO);
175    
176 root 1.1 i = ttyslot ();
177 root 1.3 if (write_bsd_utmp (i, ut))
178 root 1.1 utmp_pos = i;
179 root 1.2
180     dup2 (fdstdin, STDIN_FILENO);
181     close (fdstdin);
182 root 1.1 # else
183 root 1.2 FILE *fd0;
184 root 1.1
185     if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL)
186     {
187     char buf[256], name[256];
188    
189     buf[sizeof (buf) - 1] = '\0';
190     for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);)
191     {
192     if (*buf == '#' || sscanf (buf, "%s", name) != 1)
193     continue;
194     if (!strcmp (ut->ut_line, name))
195     {
196 root 1.3 if (!write_bsd_utmp (i, ut))
197 root 1.1 i = 0;
198     utmp_pos = i;
199     fclose (fd0);
200     break;
201     }
202     i++;
203     }
204     fclose (fd0);
205     }
206     # endif
207     }
208     #endif
209    
210     #ifdef WTMP_SUPPORT
211 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
212     if (login_shell)
213     #endif
214 root 1.1 {
215     # ifdef HAVE_STRUCT_UTMP
216     # ifdef HAVE_UPDWTMP
217 root 1.3 updwtmp (WTMP_FILE, ut);
218 root 1.1 # else
219 root 1.3 update_wtmp (WTMP_FILE, ut);
220 root 1.1 # endif
221     # endif
222 root 1.2 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
223 root 1.3 updwtmpx (WTMPX_FILE, utx);
224 root 1.1 # endif
225     }
226     #endif
227 root 1.3 #if defined(LASTLOG_SUPPORT) && defined(LASTLOG_FILE)
228 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
229     if (login_shell)
230     #endif
231 root 1.3 update_lastlog (LASTLOG_FILE, pty, hostname);
232 root 1.1 #endif
233     }
234    
235     /* ------------------------------------------------------------------------- */
236     /*
237     * remove utmp and wtmp entries
238     */
239     void
240 root 1.3 ptytty_unix::logout ()
241 root 1.1 {
242 root 1.2 if (!cmd_pid)
243     return;
244    
245 root 1.1 #ifdef HAVE_STRUCT_UTMP
246 root 1.2 struct utmp *tmput, *ut = &this->ut;
247 root 1.1 #endif
248     #ifdef HAVE_STRUCT_UTMPX
249     struct utmpx *tmputx, *utx = &this->utx;
250     #endif
251    
252     #ifdef HAVE_STRUCT_UTMP
253     # ifdef HAVE_UTMP_PID
254     memset (ut, 0, sizeof (struct utmp));
255     setutent ();
256 root 1.2 strncpy (ut->ut_id, this->ut_id, sizeof (ut->ut_id));
257 root 1.1 ut->ut_type = USER_PROCESS;
258 root 1.2 if ((tmput = getutid (ut))) /* position to entry in utmp file */
259     ut = tmput;
260 root 1.1 ut->ut_type = DEAD_PROCESS;
261     # else
262     memset (ut->ut_name, 0, sizeof (ut->ut_name));
263     # ifdef HAVE_UTMP_HOST
264     memset (ut->ut_host, 0, sizeof (ut->ut_host));
265     # endif
266     # endif
267     ut->ut_time = time (NULL);
268     #endif
269    
270     #ifdef HAVE_STRUCT_UTMPX
271     memset (utx, 0, sizeof (struct utmpx));
272     setutxent ();
273 root 1.2 strncpy (utx->ut_id, this->ut_id, sizeof (utx->ut_id));
274 root 1.1 utx->ut_type = USER_PROCESS;
275     if ((tmputx = getutxid (utx))) /* position to entry in utmp file */
276     utx = tmputx;
277     utx->ut_type = DEAD_PROCESS;
278     # if HAVE_UTMPX_SESSION
279     utx->ut_session = getsid (0);
280     # endif
281     utx->ut_tv.tv_sec = time (NULL);
282     utx->ut_tv.tv_usec = 0;
283     #endif
284    
285     /*
286     * Write ending wtmp entry
287     */
288     #ifdef WTMP_SUPPORT
289 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
290     if (login_shell)
291     #endif
292 root 1.1 {
293     # ifdef HAVE_STRUCT_UTMP
294     # ifdef HAVE_UPDWTMP
295 root 1.3 updwtmp (WTMP_FILE, ut);
296 root 1.1 # else
297 root 1.3 update_wtmp (WTMP_FILE, ut);
298 root 1.1 # endif
299     # endif
300 root 1.2 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
301 root 1.3 updwtmpx (WTMPX_FILE, utx);
302 root 1.1 # endif
303     }
304     #endif
305    
306     /*
307     * Write utmp entry
308     */
309     #ifdef HAVE_STRUCT_UTMP
310     # ifdef HAVE_UTMP_PID
311     if (ut->ut_pid == cmd_pid)
312     pututline (ut);
313     endutent ();
314     # else
315     memset (ut, 0, sizeof (struct utmp));
316 root 1.3 write_bsd_utmp (utmp_pos, ut);
317 root 1.1 # endif
318     #endif
319     #ifdef HAVE_STRUCT_UTMPX
320     if (utx->ut_pid == cmd_pid)
321     pututxline (utx);
322     endutxent ();
323     #endif
324 root 1.2
325     cmd_pid = 0;
326 root 1.1 }
327    
328     /* ------------------------------------------------------------------------- */
329     /*
330     * Write a BSD style utmp entry
331     */
332 root 1.2 #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
333     static int
334 root 1.3 write_bsd_utmp (int utmp_pos, struct utmp *wu)
335 root 1.1 {
336     int fd;
337    
338 root 1.3 if (utmp_pos <= 0 || (fd = open (UTMP_FILE, O_WRONLY)) == -1)
339 root 1.1 return 0;
340    
341     if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1)
342     write (fd, wu, sizeof (struct utmp));
343     close (fd);
344     return 1;
345     }
346     #endif
347    
348     /* ------------------------------------------------------------------------- */
349     /*
350     * Update a BSD style wtmp entry
351     */
352     #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP)
353 root 1.2 static void
354 root 1.3 update_wtmp (const char *fname, const struct utmp *putmp)
355 root 1.1 {
356     int fd, gotlock, retry;
357     struct flock lck; /* fcntl locking scheme */
358     struct stat sbuf;
359    
360     if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
361     return;
362    
363     lck.l_whence = SEEK_END; /* start lock at current eof */
364     lck.l_len = 0; /* end at ``largest possible eof'' */
365     lck.l_start = 0;
366     lck.l_type = F_WRLCK; /* we want a write lock */
367    
368     /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
369     for (retry = 10, gotlock = 0; retry--;)
370     if (fcntl (fd, F_SETLK, &lck) != -1)
371     {
372     gotlock = 1;
373     break;
374     }
375     else if (errno != EAGAIN && errno != EACCES)
376     break;
377     if (!gotlock)
378     { /* give it up */
379     close (fd);
380     return;
381     }
382     if (fstat (fd, &sbuf) == 0)
383     if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp))
384     ftruncate (fd, sbuf.st_size); /* remove bad writes */
385    
386     lck.l_type = F_UNLCK; /* unlocking the file */
387     fcntl (fd, F_SETLK, &lck);
388     close (fd);
389     }
390     #endif
391    
392     /* ------------------------------------------------------------------------- */
393     #ifdef LASTLOG_SUPPORT
394 root 1.2 static void
395 root 1.3 update_lastlog (const char *fname, const char *pty, const char *host)
396 root 1.1 {
397     # ifdef HAVE_STRUCT_LASTLOGX
398     struct lastlogx llx;
399     # endif
400     # ifdef HAVE_STRUCT_LASTLOG
401     int fd;
402     struct lastlog ll;
403     # ifdef LASTLOG_IS_DIR
404     char lastlogfile[256];
405     # endif
406     struct passwd *pwent;
407     # endif
408    
409     # ifdef HAVE_STRUCT_LASTLOGX
410     memset (&llx, 0, sizeof (llx));
411     llx.ll_tv.tv_sec = time (NULL);
412     llx.ll_tv.tv_usec = 0;
413     strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
414     strncpy (llx.ll_host, host, sizeof (llx.ll_host));
415 root 1.3 updlastlogx (LASTLOGX_FILE, getuid (), &llx);
416 root 1.1 # endif
417    
418     # ifdef HAVE_STRUCT_LASTLOG
419     pwent = getpwuid (getuid ());
420     if (!pwent)
421     {
422 root 1.3 warn ("no entry in password file, not updating lastlog.\n");
423 root 1.1 return;
424     }
425    
426     memset (&ll, 0, sizeof (ll));
427     ll.ll_time = time (NULL);
428     strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
429     strncpy (ll.ll_host, host, sizeof (ll.ll_host));
430     # ifdef LASTLOG_IS_DIR
431     sprintf (lastlogfile, "%.*s/%.*s",
432     sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2, fname,
433     sizeof (pwent->pw_name),
434     (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown"
435     : pwent->pw_name);
436     if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0)
437     {
438     write (fd, &ll, sizeof (ll));
439     close (fd);
440     }
441     # else
442     if ((fd = open (fname, O_RDWR)) != -1)
443     {
444     if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
445     SEEK_SET) != -1)
446     write (fd, &ll, sizeof (ll));
447     close (fd);
448     }
449     # endif /* LASTLOG_IS_DIR */
450     # endif /* HAVE_STRUCT_LASTLOG */
451     }
452     #endif /* LASTLOG_SUPPORT */
453    
454 root 1.5 #else
455     void
456     ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
457     {
458     }
459     #endif
460