ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.32
Committed: Mon May 16 09:24:54 2011 UTC (15 years, 4 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.31: +68 -56 lines
Log Message:
Factor code to fill utmp(x) records and consistently set only id, pid and time in utmp(x) logout records.

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