ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/logging.C
Revision: 1.44
Committed: Tue Dec 11 21:49:12 2007 UTC (16 years, 5 months ago) by ayin
Content type: text/plain
Branch: MAIN
Changes since 1.43: +6 -11 lines
Log Message:
libptytty sync

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 root 1.40 * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it>
22 pcg 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.20 #include "../config.h"
39 root 1.36
40 root 1.41 #include "ptytty.h"
41    
42     #if UTMP_SUPPORT
43    
44 root 1.38 #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 pcg 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.42 /*
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 ayin 1.44 {
110     /* give it up */
111 ayin 1.42 close (fd);
112     return;
113     }
114     if (fstat (fd, &sbuf) == 0)
115     if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp))
116     ftruncate (fd, sbuf.st_size); /* remove bad writes */
117    
118     lck.l_type = F_UNLCK; /* unlocking the file */
119     fcntl (fd, F_SETLK, &lck);
120     close (fd);
121     }
122     #endif
123    
124     /* ------------------------------------------------------------------------- */
125     #ifdef LASTLOG_SUPPORT
126     static void
127     update_lastlog (const char *fname, const char *pty, const char *host)
128     {
129     # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
130     struct lastlogx llx;
131     # endif
132     # ifdef HAVE_STRUCT_LASTLOG
133     int fd;
134     struct lastlog ll;
135     char lastlogfile[256];
136     struct passwd *pwent;
137     struct stat st;
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     if (stat (fname, &st) != 0)
162     return;
163     if (S_ISDIR (st.st_mode))
164     {
165     sprintf (lastlogfile, "%.*s/%.*s",
166 ayin 1.43 (int)(sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2), fname,
167     (int)sizeof (pwent->pw_name),
168 ayin 1.42 (!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     }
176     else if (S_ISREG (st.st_mode))
177     if ((fd = open (fname, O_RDWR)) != -1)
178     {
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.43 # endif /* HAVE_STRUCT_LASTLOG */
185 ayin 1.42 }
186 ayin 1.43 #endif /* LASTLOG_SUPPORT */
187 ayin 1.42
188     /* ------------------------------------------------------------------------- */
189 pcg 1.1
190     /*
191     * make and write utmp and wtmp entries
192     */
193     void
194 root 1.36 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
195 pcg 1.1 {
196 root 1.28 const char *pty = name;
197    
198     if (!pty || !*pty)
199     return;
200    
201 root 1.25 this->cmd_pid = cmd_pid;
202     this->login_shell = login_shell;
203    
204 pcg 1.1 #ifdef HAVE_STRUCT_UTMP
205 root 1.12 struct utmp *ut = &this->ut;
206 pcg 1.1 #endif
207     #ifdef HAVE_STRUCT_UTMPX
208 root 1.12 struct utmpx *utx = &this->utx;
209 pcg 1.1 #endif
210 root 1.12 int i;
211 root 1.17 struct passwd *pwent = getpwuid (getuid ());
212 ayin 1.43 const char *name = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
213 pcg 1.1
214 root 1.11 if (!strncmp (pty, "/dev/", 5))
215 pcg 1.6 pty += 5; /* skip /dev/ prefix */
216 pcg 1.1
217 root 1.22 #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
218 root 1.11 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
219 root 1.13 strncpy (ut_id, pty + 3, sizeof (ut_id));
220 pcg 1.7 else if (sscanf (pty, "pts/%d", &i) == 1)
221     sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
222 ayin 1.44 else
223 pcg 1.6 {
224 root 1.38 ptytty_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
225 pcg 1.6 return;
226 pcg 1.1 }
227 root 1.13 #endif
228 pcg 1.1
229     #ifdef HAVE_STRUCT_UTMP
230 root 1.11 memset (ut, 0, sizeof (struct utmp));
231 pcg 1.1 # ifdef HAVE_UTMP_PID
232 pcg 1.7 setutent ();
233 root 1.11 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
234 pcg 1.6 ut->ut_type = DEAD_PROCESS;
235 pcg 1.7 getutid (ut); /* position to entry in utmp file */
236 pcg 1.1 # endif
237     #endif
238    
239     #ifdef HAVE_STRUCT_UTMPX
240 root 1.11 memset (utx, 0, sizeof (struct utmpx));
241 pcg 1.7 setutxent ();
242 root 1.11 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
243 pcg 1.6 utx->ut_type = DEAD_PROCESS;
244 pcg 1.7 getutxid (utx); /* position to entry in utmp file */
245 pcg 1.1 #endif
246    
247     #ifdef HAVE_STRUCT_UTMP
248 root 1.11 strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
249 ayin 1.44 # ifdef HAVE_UTMP_HOST
250     strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
251     # endif
252 pcg 1.7 ut->ut_time = time (NULL);
253 pcg 1.1 # ifdef HAVE_UTMP_PID
254 ayin 1.43 strncpy (ut->ut_user, name, sizeof (ut->ut_user));
255 root 1.11 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
256 pcg 1.6 ut->ut_pid = cmd_pid;
257     ut->ut_type = USER_PROCESS;
258 pcg 1.7 pututline (ut);
259     endutent (); /* close the file */
260 pcg 1.6 utmp_pos = 0;
261 pcg 1.1 # else
262 ayin 1.43 strncpy (ut->ut_name, name, sizeof (ut->ut_name));
263 pcg 1.1 # endif
264     #endif
265    
266     #ifdef HAVE_STRUCT_UTMPX
267 root 1.11 strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
268 ayin 1.43 strncpy (utx->ut_user, name, sizeof (utx->ut_user));
269 root 1.11 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
270 root 1.15 # if HAVE_UTMPX_SESSION
271 pcg 1.7 utx->ut_session = getsid (0);
272 root 1.15 # endif
273 pcg 1.7 utx->ut_tv.tv_sec = time (NULL);
274 pcg 1.6 utx->ut_tv.tv_usec = 0;
275     utx->ut_pid = cmd_pid;
276 pcg 1.1 # ifdef HAVE_UTMPX_HOST
277 root 1.11 strncpy (utx->ut_host, hostname, sizeof (utx->ut_host));
278 pcg 1.1 # if 0
279 pcg 1.6 {
280     char *colon;
281 pcg 1.1
282 root 1.11 if ((colon = strrchr (ut->ut_host, ':')) != NULL)
283 pcg 1.6 *colon = '\0';
284     }
285 pcg 1.1 # endif
286     # endif
287 pcg 1.6 utx->ut_type = USER_PROCESS;
288 pcg 1.7 pututxline (utx);
289     endutxent (); /* close the file */
290 pcg 1.6 utmp_pos = 0;
291 pcg 1.1 #endif
292    
293     #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
294 pcg 1.6 {
295 ayin 1.42 # if 1
296 root 1.31 int fdstdin = dup (STDIN_FILENO);
297 root 1.33 dup2 (tty, STDIN_FILENO);
298 root 1.31
299 pcg 1.7 i = ttyslot ();
300 root 1.36 if (write_bsd_utmp (i, ut))
301 pcg 1.6 utmp_pos = i;
302 root 1.31
303     dup2 (fdstdin, STDIN_FILENO);
304     close (fdstdin);
305 pcg 1.6 # endif
306     }
307 pcg 1.1 #endif
308    
309     #ifdef WTMP_SUPPORT
310 ayin 1.34 #ifdef LOG_ONLY_ON_LOGIN
311 root 1.25 if (login_shell)
312 ayin 1.34 #endif
313 pcg 1.1 {
314     # ifdef HAVE_STRUCT_UTMP
315     # ifdef HAVE_UPDWTMP
316 root 1.36 updwtmp (WTMP_FILE, ut);
317 pcg 1.1 # else
318 root 1.36 update_wtmp (WTMP_FILE, ut);
319 pcg 1.1 # endif
320     # endif
321 root 1.23 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
322 root 1.36 updwtmpx (WTMPX_FILE, utx);
323 pcg 1.1 # endif
324     }
325     #endif
326 root 1.36 #if defined(LASTLOG_SUPPORT) && defined(LASTLOG_FILE)
327 ayin 1.34 #ifdef LOG_ONLY_ON_LOGIN
328 root 1.25 if (login_shell)
329 ayin 1.34 #endif
330 root 1.36 update_lastlog (LASTLOG_FILE, pty, hostname);
331 pcg 1.1 #endif
332     }
333    
334     /* ------------------------------------------------------------------------- */
335     /*
336     * remove utmp and wtmp entries
337     */
338     void
339 root 1.36 ptytty_unix::logout ()
340 pcg 1.1 {
341 root 1.28 if (!cmd_pid)
342     return;
343    
344 pcg 1.1 #ifdef HAVE_STRUCT_UTMP
345 ayin 1.26 struct utmp *tmput, *ut = &this->ut;
346 pcg 1.1 #endif
347     #ifdef HAVE_STRUCT_UTMPX
348 root 1.12 struct utmpx *tmputx, *utx = &this->utx;
349 pcg 1.1 #endif
350    
351     #ifdef HAVE_STRUCT_UTMP
352     # ifdef HAVE_UTMP_PID
353 root 1.11 memset (ut, 0, sizeof (struct utmp));
354 pcg 1.7 setutent ();
355 root 1.21 strncpy (ut->ut_id, this->ut_id, sizeof (ut->ut_id));
356 pcg 1.6 ut->ut_type = USER_PROCESS;
357 ayin 1.26 if ((tmput = getutid (ut))) /* position to entry in utmp file */
358     ut = tmput;
359 pcg 1.6 ut->ut_type = DEAD_PROCESS;
360     # else
361 root 1.11 memset (ut->ut_name, 0, sizeof (ut->ut_name));
362 pcg 1.1 # ifdef HAVE_UTMP_HOST
363 root 1.11 memset (ut->ut_host, 0, sizeof (ut->ut_host));
364 pcg 1.1 # endif
365     # endif
366 pcg 1.7 ut->ut_time = time (NULL);
367 pcg 1.1 #endif
368    
369     #ifdef HAVE_STRUCT_UTMPX
370 root 1.11 memset (utx, 0, sizeof (struct utmpx));
371 pcg 1.7 setutxent ();
372 root 1.21 strncpy (utx->ut_id, this->ut_id, sizeof (utx->ut_id));
373 pcg 1.6 utx->ut_type = USER_PROCESS;
374 pcg 1.7 if ((tmputx = getutxid (utx))) /* position to entry in utmp file */
375 pcg 1.6 utx = tmputx;
376     utx->ut_type = DEAD_PROCESS;
377 root 1.15 # if HAVE_UTMPX_SESSION
378 pcg 1.7 utx->ut_session = getsid (0);
379 root 1.15 # endif
380 pcg 1.7 utx->ut_tv.tv_sec = time (NULL);
381 pcg 1.6 utx->ut_tv.tv_usec = 0;
382 pcg 1.1 #endif
383    
384 pcg 1.6 /*
385     * Write ending wtmp entry
386     */
387 pcg 1.1 #ifdef WTMP_SUPPORT
388 ayin 1.34 #ifdef LOG_ONLY_ON_LOGIN
389 root 1.25 if (login_shell)
390 ayin 1.34 #endif
391 pcg 1.1 {
392     # ifdef HAVE_STRUCT_UTMP
393     # ifdef HAVE_UPDWTMP
394 root 1.36 updwtmp (WTMP_FILE, ut);
395 pcg 1.1 # else
396 root 1.36 update_wtmp (WTMP_FILE, ut);
397 pcg 1.1 # endif
398     # endif
399 root 1.24 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
400 root 1.36 updwtmpx (WTMPX_FILE, utx);
401 pcg 1.1 # endif
402     }
403     #endif
404    
405 pcg 1.6 /*
406     * Write utmp entry
407     */
408 pcg 1.1 #ifdef HAVE_STRUCT_UTMP
409     # ifdef HAVE_UTMP_PID
410 pcg 1.6 if (ut->ut_pid == cmd_pid)
411 pcg 1.7 pututline (ut);
412     endutent ();
413 pcg 1.1 # else
414 root 1.11 memset (ut, 0, sizeof (struct utmp));
415 root 1.36 write_bsd_utmp (utmp_pos, ut);
416 pcg 1.1 # endif
417     #endif
418     #ifdef HAVE_STRUCT_UTMPX
419 pcg 1.6 if (utx->ut_pid == cmd_pid)
420 pcg 1.7 pututxline (utx);
421     endutxent ();
422 pcg 1.1 #endif
423 root 1.28
424     cmd_pid = 0;
425 pcg 1.1 }
426    
427 root 1.35 #else
428     void
429 root 1.36 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
430 root 1.35 {
431     }
432 root 1.36 #endif
433