ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.28
Committed: Mon Mar 29 11:00:21 2010 UTC (16 years, 5 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.27: +8 -10 lines
Log Message:
Simplify.

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 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.28 int fd, 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.28 retry = 10;
103     while (retry--)
104 ayin 1.12 if (fcntl (fd, F_SETLK, &lck) != -1)
105     {
106     break;
107     }
108     else if (errno != EAGAIN && errno != EACCES)
109 sf-exg 1.28 {
110     /* give it up */
111     close (fd);
112     return;
113     }
114 ayin 1.12 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 ayin 1.13 struct stat st;
138 ayin 1.12 # 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 ayin 1.13 if (stat (fname, &st) != 0)
162     return;
163     if (S_ISDIR (st.st_mode))
164     {
165 sf-exg 1.27 snprintf (lastlogfile, sizeof (lastlogfile), "%s/%s", fname,
166 ayin 1.14 (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown"
167     : pwent->pw_name);
168     if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0)
169     {
170     write (fd, &ll, sizeof (ll));
171     close (fd);
172     }
173 ayin 1.13 }
174     else if (S_ISREG (st.st_mode))
175 ayin 1.14 if ((fd = open (fname, O_RDWR)) != -1)
176     {
177     if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
178     SEEK_SET) != -1)
179     write (fd, &ll, sizeof (ll));
180     close (fd);
181     }
182 ayin 1.17 # endif /* HAVE_STRUCT_LASTLOG */
183 ayin 1.12 }
184 ayin 1.17 #endif /* LASTLOG_SUPPORT */
185 ayin 1.12
186     /* ------------------------------------------------------------------------- */
187 root 1.1
188     /*
189     * make and write utmp and wtmp entries
190     */
191     void
192 root 1.3 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
193 root 1.1 {
194 root 1.2 const char *pty = name;
195    
196     if (!pty || !*pty)
197     return;
198    
199     this->cmd_pid = cmd_pid;
200     this->login_shell = login_shell;
201    
202 root 1.1 #ifdef HAVE_STRUCT_UTMP
203     struct utmp *ut = &this->ut;
204     #endif
205     #ifdef HAVE_STRUCT_UTMPX
206     struct utmpx *utx = &this->utx;
207     #endif
208     int i;
209     struct passwd *pwent = getpwuid (getuid ());
210 ayin 1.16 const char *name = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
211 root 1.1
212     if (!strncmp (pty, "/dev/", 5))
213     pty += 5; /* skip /dev/ prefix */
214    
215 root 1.2 #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
216 root 1.1 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
217     strncpy (ut_id, pty + 3, sizeof (ut_id));
218     else if (sscanf (pty, "pts/%d", &i) == 1)
219     sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
220 ayin 1.20 else
221 root 1.1 {
222 root 1.7 ptytty_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
223 root 1.1 return;
224     }
225     #endif
226    
227     #ifdef HAVE_STRUCT_UTMP
228     memset (ut, 0, sizeof (struct utmp));
229     # ifdef HAVE_UTMP_PID
230     setutent ();
231     strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
232     ut->ut_type = DEAD_PROCESS;
233     getutid (ut); /* position to entry in utmp file */
234     # endif
235     #endif
236    
237     #ifdef HAVE_STRUCT_UTMPX
238     memset (utx, 0, sizeof (struct utmpx));
239     setutxent ();
240     strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
241     utx->ut_type = DEAD_PROCESS;
242     getutxid (utx); /* position to entry in utmp file */
243     #endif
244    
245     #ifdef HAVE_STRUCT_UTMP
246     strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
247 ayin 1.21 # ifdef HAVE_UTMP_HOST
248     strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
249     # endif
250 root 1.1 ut->ut_time = time (NULL);
251     # ifdef HAVE_UTMP_PID
252 ayin 1.16 strncpy (ut->ut_user, name, sizeof (ut->ut_user));
253 root 1.1 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
254     ut->ut_pid = cmd_pid;
255     ut->ut_type = USER_PROCESS;
256     pututline (ut);
257     endutent (); /* close the file */
258     utmp_pos = 0;
259     # else
260 ayin 1.16 strncpy (ut->ut_name, name, sizeof (ut->ut_name));
261 root 1.1 # endif
262     #endif
263    
264     #ifdef HAVE_STRUCT_UTMPX
265     strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
266 ayin 1.16 strncpy (utx->ut_user, name, sizeof (utx->ut_user));
267 root 1.1 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
268     # if HAVE_UTMPX_SESSION
269     utx->ut_session = getsid (0);
270     # endif
271     utx->ut_tv.tv_sec = time (NULL);
272     utx->ut_tv.tv_usec = 0;
273     utx->ut_pid = cmd_pid;
274     # ifdef HAVE_UTMPX_HOST
275     strncpy (utx->ut_host, hostname, sizeof (utx->ut_host));
276     # if 0
277     {
278     char *colon;
279    
280     if ((colon = strrchr (ut->ut_host, ':')) != NULL)
281     *colon = '\0';
282     }
283     # endif
284     # endif
285     utx->ut_type = USER_PROCESS;
286     pututxline (utx);
287     endutxent (); /* close the file */
288     utmp_pos = 0;
289     #endif
290    
291     #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
292     {
293 ayin 1.15 # if 1
294 root 1.2 int fdstdin = dup (STDIN_FILENO);
295     dup2 (tty, STDIN_FILENO);
296    
297 root 1.1 i = ttyslot ();
298 root 1.3 if (write_bsd_utmp (i, ut))
299 root 1.1 utmp_pos = i;
300 root 1.2
301     dup2 (fdstdin, STDIN_FILENO);
302     close (fdstdin);
303 root 1.1 # endif
304     }
305     #endif
306    
307     #ifdef WTMP_SUPPORT
308 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
309     if (login_shell)
310     #endif
311 root 1.1 {
312     # ifdef HAVE_STRUCT_UTMP
313     # ifdef HAVE_UPDWTMP
314 root 1.3 updwtmp (WTMP_FILE, ut);
315 root 1.1 # else
316 root 1.3 update_wtmp (WTMP_FILE, ut);
317 root 1.1 # endif
318     # endif
319 root 1.2 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
320 root 1.3 updwtmpx (WTMPX_FILE, utx);
321 root 1.1 # endif
322     }
323     #endif
324 root 1.3 #if defined(LASTLOG_SUPPORT) && defined(LASTLOG_FILE)
325 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
326     if (login_shell)
327     #endif
328 root 1.3 update_lastlog (LASTLOG_FILE, pty, hostname);
329 root 1.1 #endif
330     }
331    
332     /* ------------------------------------------------------------------------- */
333     /*
334     * remove utmp and wtmp entries
335     */
336     void
337 root 1.3 ptytty_unix::logout ()
338 root 1.1 {
339 root 1.2 if (!cmd_pid)
340     return;
341    
342 root 1.1 #ifdef HAVE_STRUCT_UTMP
343 root 1.2 struct utmp *tmput, *ut = &this->ut;
344 root 1.1 #endif
345     #ifdef HAVE_STRUCT_UTMPX
346     struct utmpx *tmputx, *utx = &this->utx;
347     #endif
348    
349     #ifdef HAVE_STRUCT_UTMP
350     # ifdef HAVE_UTMP_PID
351     memset (ut, 0, sizeof (struct utmp));
352     setutent ();
353 root 1.2 strncpy (ut->ut_id, this->ut_id, sizeof (ut->ut_id));
354 root 1.1 ut->ut_type = USER_PROCESS;
355 root 1.2 if ((tmput = getutid (ut))) /* position to entry in utmp file */
356     ut = tmput;
357 root 1.1 ut->ut_type = DEAD_PROCESS;
358     # else
359     memset (ut->ut_name, 0, sizeof (ut->ut_name));
360     # ifdef HAVE_UTMP_HOST
361     memset (ut->ut_host, 0, sizeof (ut->ut_host));
362     # endif
363     # endif
364     ut->ut_time = time (NULL);
365     #endif
366    
367     #ifdef HAVE_STRUCT_UTMPX
368     memset (utx, 0, sizeof (struct utmpx));
369     setutxent ();
370 root 1.2 strncpy (utx->ut_id, this->ut_id, sizeof (utx->ut_id));
371 root 1.1 utx->ut_type = USER_PROCESS;
372     if ((tmputx = getutxid (utx))) /* position to entry in utmp file */
373     utx = tmputx;
374     utx->ut_type = DEAD_PROCESS;
375     # if HAVE_UTMPX_SESSION
376     utx->ut_session = getsid (0);
377     # endif
378     utx->ut_tv.tv_sec = time (NULL);
379     utx->ut_tv.tv_usec = 0;
380     #endif
381    
382     /*
383     * Write ending wtmp entry
384     */
385     #ifdef WTMP_SUPPORT
386 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
387     if (login_shell)
388     #endif
389 root 1.1 {
390     # ifdef HAVE_STRUCT_UTMP
391     # ifdef HAVE_UPDWTMP
392 root 1.3 updwtmp (WTMP_FILE, ut);
393 root 1.1 # else
394 root 1.3 update_wtmp (WTMP_FILE, ut);
395 root 1.1 # endif
396     # endif
397 root 1.2 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
398 root 1.3 updwtmpx (WTMPX_FILE, utx);
399 root 1.1 # endif
400     }
401     #endif
402    
403     /*
404     * Write utmp entry
405     */
406     #ifdef HAVE_STRUCT_UTMP
407     # ifdef HAVE_UTMP_PID
408     if (ut->ut_pid == cmd_pid)
409     pututline (ut);
410     endutent ();
411     # else
412     memset (ut, 0, sizeof (struct utmp));
413 root 1.3 write_bsd_utmp (utmp_pos, ut);
414 root 1.1 # endif
415     #endif
416     #ifdef HAVE_STRUCT_UTMPX
417     if (utx->ut_pid == cmd_pid)
418     pututxline (utx);
419     endutxent ();
420     #endif
421 root 1.2
422     cmd_pid = 0;
423 root 1.1 }
424    
425 root 1.5 #else
426     void
427     ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
428     {
429     }
430     #endif
431