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

File Contents

# User Rev Content
1 root 1.37 // This file is part of libptytty. Do not make local modifications.
2     // http://software.schmorp.de/pkg/libptytty
3    
4     /*----------------------------------------------------------------------*
5 pcg 1.9 * File: logging.C
6 pcg 1.1 *----------------------------------------------------------------------*
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.30 * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com>
21 pcg 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.20 #include "../config.h"
38 root 1.36
39 root 1.27 #include "ptytty.h"
40 root 1.20
41 root 1.27 #if UTMP_SUPPORT
42 pcg 1.1
43 root 1.16 #if HAVE_STRUCT_UTMP
44 root 1.36 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.16 #endif
47    
48 root 1.36 static void update_lastlog (const char *fname, const char *pty, const char *host);
49 root 1.14
50 pcg 1.1 /*
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.36 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
64 pcg 1.1 {
65 root 1.28 const char *pty = name;
66    
67     if (!pty || !*pty)
68     return;
69    
70 root 1.25 this->cmd_pid = cmd_pid;
71     this->login_shell = login_shell;
72    
73 pcg 1.1 #ifdef HAVE_STRUCT_UTMP
74 root 1.12 struct utmp *ut = &this->ut;
75 pcg 1.1 #endif
76     #ifdef HAVE_STRUCT_UTMPX
77 root 1.12 struct utmpx *utx = &this->utx;
78 pcg 1.1 #endif
79 root 1.12 int i;
80 root 1.17 struct passwd *pwent = getpwuid (getuid ());
81 pcg 1.1
82 root 1.11 if (!strncmp (pty, "/dev/", 5))
83 pcg 1.6 pty += 5; /* skip /dev/ prefix */
84 pcg 1.1
85 root 1.22 #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
86 root 1.11 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
87 root 1.13 strncpy (ut_id, pty + 3, sizeof (ut_id));
88 pcg 1.7 else if (sscanf (pty, "pts/%d", &i) == 1)
89     sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
90 root 1.11 else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3))
91 pcg 1.6 {
92 root 1.36 warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
93 pcg 1.6 return;
94 pcg 1.1 }
95 root 1.13 #endif
96 pcg 1.1
97     #ifdef HAVE_STRUCT_UTMP
98 root 1.11 memset (ut, 0, sizeof (struct utmp));
99 pcg 1.1 # ifdef HAVE_UTMP_PID
100 pcg 1.7 setutent ();
101 root 1.11 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
102 pcg 1.6 ut->ut_type = DEAD_PROCESS;
103 pcg 1.7 getutid (ut); /* position to entry in utmp file */
104 root 1.21 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
105 pcg 1.1 # endif
106     #endif
107    
108     #ifdef HAVE_STRUCT_UTMPX
109 root 1.11 memset (utx, 0, sizeof (struct utmpx));
110 pcg 1.7 setutxent ();
111 root 1.11 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
112 pcg 1.6 utx->ut_type = DEAD_PROCESS;
113 pcg 1.7 getutxid (utx); /* position to entry in utmp file */
114 root 1.21 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
115 pcg 1.1 #endif
116    
117     #ifdef HAVE_STRUCT_UTMP
118 root 1.11 strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
119 pcg 1.7 ut->ut_time = time (NULL);
120 pcg 1.1 # ifdef HAVE_UTMP_PID
121 root 1.11 strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
122 pcg 1.7 sizeof (ut->ut_user));
123 root 1.11 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
124 pcg 1.7 ut->ut_time = time (NULL);
125 pcg 1.6 ut->ut_pid = cmd_pid;
126 pcg 1.1 # ifdef HAVE_UTMP_HOST
127 root 1.11 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
128 pcg 1.1 # endif
129 pcg 1.6 ut->ut_type = USER_PROCESS;
130 pcg 1.7 pututline (ut);
131     endutent (); /* close the file */
132 pcg 1.6 utmp_pos = 0;
133 pcg 1.1 # else
134 root 1.11 strncpy (ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
135 pcg 1.7 sizeof (ut->ut_name));
136 pcg 1.1 # ifdef HAVE_UTMP_HOST
137 root 1.11 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
138 pcg 1.1 # endif
139     # endif
140     #endif
141    
142     #ifdef HAVE_STRUCT_UTMPX
143 root 1.11 strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
144     strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
145 pcg 1.7 sizeof (utx->ut_user));
146 root 1.11 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
147 root 1.15 # if HAVE_UTMPX_SESSION
148 pcg 1.7 utx->ut_session = getsid (0);
149 root 1.15 # endif
150 pcg 1.7 utx->ut_tv.tv_sec = time (NULL);
151 pcg 1.6 utx->ut_tv.tv_usec = 0;
152     utx->ut_pid = cmd_pid;
153 pcg 1.1 # ifdef HAVE_UTMPX_HOST
154 root 1.11 strncpy (utx->ut_host, hostname, sizeof (utx->ut_host));
155 pcg 1.1 # if 0
156 pcg 1.6 {
157     char *colon;
158 pcg 1.1
159 root 1.11 if ((colon = strrchr (ut->ut_host, ':')) != NULL)
160 pcg 1.6 *colon = '\0';
161     }
162 pcg 1.1 # endif
163     # endif
164 pcg 1.6 utx->ut_type = USER_PROCESS;
165 pcg 1.7 pututxline (utx);
166     endutxent (); /* close the file */
167 pcg 1.6 utmp_pos = 0;
168 pcg 1.1 #endif
169    
170     #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
171 pcg 1.6 {
172 pcg 1.1 # ifdef HAVE_TTYSLOT
173 root 1.31 int fdstdin = dup (STDIN_FILENO);
174 root 1.33 dup2 (tty, STDIN_FILENO);
175 root 1.31
176 pcg 1.7 i = ttyslot ();
177 root 1.36 if (write_bsd_utmp (i, ut))
178 pcg 1.6 utmp_pos = i;
179 root 1.31
180     dup2 (fdstdin, STDIN_FILENO);
181     close (fdstdin);
182 pcg 1.1 # else
183 root 1.31 FILE *fd0;
184 pcg 1.1
185 pcg 1.7 if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL)
186 pcg 1.6 {
187 root 1.18 char buf[256], name[256];
188 pcg 1.6
189 pcg 1.7 buf[sizeof (buf) - 1] = '\0';
190     for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);)
191 pcg 1.6 {
192 pcg 1.7 if (*buf == '#' || sscanf (buf, "%s", name) != 1)
193 pcg 1.6 continue;
194 root 1.11 if (!strcmp (ut->ut_line, name))
195 pcg 1.6 {
196 root 1.36 if (!write_bsd_utmp (i, ut))
197 pcg 1.6 i = 0;
198     utmp_pos = i;
199 pcg 1.7 fclose (fd0);
200 pcg 1.6 break;
201     }
202     i++;
203     }
204 pcg 1.7 fclose (fd0);
205 pcg 1.6 }
206     # endif
207     }
208 pcg 1.1 #endif
209    
210     #ifdef WTMP_SUPPORT
211 ayin 1.34 #ifdef LOG_ONLY_ON_LOGIN
212 root 1.25 if (login_shell)
213 ayin 1.34 #endif
214 pcg 1.1 {
215     # ifdef HAVE_STRUCT_UTMP
216     # ifdef HAVE_UPDWTMP
217 root 1.36 updwtmp (WTMP_FILE, ut);
218 pcg 1.1 # else
219 root 1.36 update_wtmp (WTMP_FILE, ut);
220 pcg 1.1 # endif
221     # endif
222 root 1.23 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
223 root 1.36 updwtmpx (WTMPX_FILE, utx);
224 pcg 1.1 # endif
225     }
226     #endif
227 root 1.36 #if defined(LASTLOG_SUPPORT) && defined(LASTLOG_FILE)
228 ayin 1.34 #ifdef LOG_ONLY_ON_LOGIN
229 root 1.25 if (login_shell)
230 ayin 1.34 #endif
231 root 1.36 update_lastlog (LASTLOG_FILE, pty, hostname);
232 pcg 1.1 #endif
233     }
234    
235     /* ------------------------------------------------------------------------- */
236     /*
237     * remove utmp and wtmp entries
238     */
239     void
240 root 1.36 ptytty_unix::logout ()
241 pcg 1.1 {
242 root 1.28 if (!cmd_pid)
243     return;
244    
245 pcg 1.1 #ifdef HAVE_STRUCT_UTMP
246 ayin 1.26 struct utmp *tmput, *ut = &this->ut;
247 pcg 1.1 #endif
248     #ifdef HAVE_STRUCT_UTMPX
249 root 1.12 struct utmpx *tmputx, *utx = &this->utx;
250 pcg 1.1 #endif
251    
252     #ifdef HAVE_STRUCT_UTMP
253     # ifdef HAVE_UTMP_PID
254 root 1.11 memset (ut, 0, sizeof (struct utmp));
255 pcg 1.7 setutent ();
256 root 1.21 strncpy (ut->ut_id, this->ut_id, sizeof (ut->ut_id));
257 pcg 1.6 ut->ut_type = USER_PROCESS;
258 ayin 1.26 if ((tmput = getutid (ut))) /* position to entry in utmp file */
259     ut = tmput;
260 pcg 1.6 ut->ut_type = DEAD_PROCESS;
261     # else
262 root 1.11 memset (ut->ut_name, 0, sizeof (ut->ut_name));
263 pcg 1.1 # ifdef HAVE_UTMP_HOST
264 root 1.11 memset (ut->ut_host, 0, sizeof (ut->ut_host));
265 pcg 1.1 # endif
266     # endif
267 pcg 1.7 ut->ut_time = time (NULL);
268 pcg 1.1 #endif
269    
270     #ifdef HAVE_STRUCT_UTMPX
271 root 1.11 memset (utx, 0, sizeof (struct utmpx));
272 pcg 1.7 setutxent ();
273 root 1.21 strncpy (utx->ut_id, this->ut_id, sizeof (utx->ut_id));
274 pcg 1.6 utx->ut_type = USER_PROCESS;
275 pcg 1.7 if ((tmputx = getutxid (utx))) /* position to entry in utmp file */
276 pcg 1.6 utx = tmputx;
277     utx->ut_type = DEAD_PROCESS;
278 root 1.15 # if HAVE_UTMPX_SESSION
279 pcg 1.7 utx->ut_session = getsid (0);
280 root 1.15 # endif
281 pcg 1.7 utx->ut_tv.tv_sec = time (NULL);
282 pcg 1.6 utx->ut_tv.tv_usec = 0;
283 pcg 1.1 #endif
284    
285 pcg 1.6 /*
286     * Write ending wtmp entry
287     */
288 pcg 1.1 #ifdef WTMP_SUPPORT
289 ayin 1.34 #ifdef LOG_ONLY_ON_LOGIN
290 root 1.25 if (login_shell)
291 ayin 1.34 #endif
292 pcg 1.1 {
293     # ifdef HAVE_STRUCT_UTMP
294     # ifdef HAVE_UPDWTMP
295 root 1.36 updwtmp (WTMP_FILE, ut);
296 pcg 1.1 # else
297 root 1.36 update_wtmp (WTMP_FILE, ut);
298 pcg 1.1 # endif
299     # endif
300 root 1.24 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
301 root 1.36 updwtmpx (WTMPX_FILE, utx);
302 pcg 1.1 # endif
303     }
304     #endif
305    
306 pcg 1.6 /*
307     * Write utmp entry
308     */
309 pcg 1.1 #ifdef HAVE_STRUCT_UTMP
310     # ifdef HAVE_UTMP_PID
311 pcg 1.6 if (ut->ut_pid == cmd_pid)
312 pcg 1.7 pututline (ut);
313     endutent ();
314 pcg 1.1 # else
315 root 1.11 memset (ut, 0, sizeof (struct utmp));
316 root 1.36 write_bsd_utmp (utmp_pos, ut);
317 pcg 1.1 # endif
318     #endif
319     #ifdef HAVE_STRUCT_UTMPX
320 pcg 1.6 if (utx->ut_pid == cmd_pid)
321 pcg 1.7 pututxline (utx);
322     endutxent ();
323 pcg 1.1 #endif
324 root 1.28
325     cmd_pid = 0;
326 pcg 1.1 }
327    
328     /* ------------------------------------------------------------------------- */
329     /*
330     * Write a BSD style utmp entry
331     */
332 root 1.22 #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
333     static int
334 root 1.36 write_bsd_utmp (int utmp_pos, struct utmp *wu)
335 pcg 1.1 {
336 pcg 1.6 int fd;
337 pcg 1.1
338 root 1.36 if (utmp_pos <= 0 || (fd = open (UTMP_FILE, O_WRONLY)) == -1)
339 pcg 1.6 return 0;
340 pcg 1.1
341 pcg 1.7 if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1)
342     write (fd, wu, sizeof (struct utmp));
343     close (fd);
344 pcg 1.6 return 1;
345 pcg 1.1 }
346     #endif
347    
348     /* ------------------------------------------------------------------------- */
349     /*
350     * Update a BSD style wtmp entry
351     */
352 root 1.16 #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP)
353 root 1.22 static void
354 root 1.36 update_wtmp (const char *fname, const struct utmp *putmp)
355 pcg 1.1 {
356 pcg 1.6 int fd, gotlock, retry;
357     struct flock lck; /* fcntl locking scheme */
358     struct stat sbuf;
359    
360 pcg 1.7 if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
361 pcg 1.6 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 pcg 1.7 if (fcntl (fd, F_SETLK, &lck) != -1)
371 pcg 1.6 {
372     gotlock = 1;
373     break;
374     }
375     else if (errno != EAGAIN && errno != EACCES)
376     break;
377     if (!gotlock)
378     { /* give it up */
379 pcg 1.7 close (fd);
380 pcg 1.6 return;
381     }
382 pcg 1.7 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 pcg 1.6
386     lck.l_type = F_UNLCK; /* unlocking the file */
387 pcg 1.7 fcntl (fd, F_SETLK, &lck);
388     close (fd);
389 pcg 1.1 }
390     #endif
391    
392     /* ------------------------------------------------------------------------- */
393     #ifdef LASTLOG_SUPPORT
394 root 1.22 static void
395 root 1.36 update_lastlog (const char *fname, const char *pty, const char *host)
396 pcg 1.1 {
397     # ifdef HAVE_STRUCT_LASTLOGX
398 pcg 1.6 struct lastlogx llx;
399 pcg 1.1 # endif
400     # ifdef HAVE_STRUCT_LASTLOG
401 pcg 1.6 int fd;
402     struct lastlog ll;
403 pcg 1.1 # ifdef LASTLOG_IS_DIR
404 pcg 1.6 char lastlogfile[256];
405 pcg 1.1 # endif
406 pcg 1.6 struct passwd *pwent;
407 pcg 1.1 # endif
408    
409     # ifdef HAVE_STRUCT_LASTLOGX
410 root 1.11 memset (&llx, 0, sizeof (llx));
411 pcg 1.7 llx.ll_tv.tv_sec = time (NULL);
412 pcg 1.6 llx.ll_tv.tv_usec = 0;
413 root 1.11 strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
414     strncpy (llx.ll_host, host, sizeof (llx.ll_host));
415 root 1.36 updlastlogx (LASTLOGX_FILE, getuid (), &llx);
416 pcg 1.1 # endif
417    
418     # ifdef HAVE_STRUCT_LASTLOG
419 pcg 1.7 pwent = getpwuid (getuid ());
420 pcg 1.6 if (!pwent)
421     {
422 root 1.36 warn ("no entry in password file, not updating lastlog.\n");
423 pcg 1.6 return;
424 pcg 1.1 }
425 pcg 1.8
426 root 1.11 memset (&ll, 0, sizeof (ll));
427 pcg 1.7 ll.ll_time = time (NULL);
428 root 1.11 strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
429     strncpy (ll.ll_host, host, sizeof (ll.ll_host));
430 pcg 1.1 # ifdef LASTLOG_IS_DIR
431 pcg 1.7 sprintf (lastlogfile, "%.*s/%.*s",
432     sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2, fname,
433     sizeof (pwent->pw_name),
434 pcg 1.6 (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown"
435     : pwent->pw_name);
436 pcg 1.7 if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0)
437 pcg 1.6 {
438 pcg 1.7 write (fd, &ll, sizeof (ll));
439     close (fd);
440 pcg 1.1 }
441     # else
442 pcg 1.7 if ((fd = open (fname, O_RDWR)) != -1)
443 pcg 1.6 {
444 pcg 1.7 if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
445 pcg 1.6 SEEK_SET) != -1)
446 pcg 1.7 write (fd, &ll, sizeof (ll));
447     close (fd);
448 pcg 1.1 }
449     # endif /* LASTLOG_IS_DIR */
450     # endif /* HAVE_STRUCT_LASTLOG */
451     }
452     #endif /* LASTLOG_SUPPORT */
453    
454 root 1.35 #else
455     void
456 root 1.36 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
457 root 1.35 {
458     }
459 root 1.36 #endif
460