ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/logging.C
Revision: 1.12
Committed: Sun Nov 4 09:24:50 2007 UTC (18 years, 10 months ago) by ayin
Content type: text/plain
Branch: MAIN
Changes since 1.11: +126 -133 lines
Log Message:
Move static functions before their use and remove their declarations.

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