ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/logging.C
Revision: 1.58
Committed: Sat Nov 26 13:39:04 2011 UTC (14 years, 9 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.57: +36 -0 lines
Log Message:
Change PT_FIND_FILE to search only for fallback locations of
utmp/wtmp/lastlog files and define the *_FILE macros at compile time.

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 sf-exg 1.56 #ifdef HAVE_UTMPX_H
45     # include <utmpx.h>
46     #endif
47     #ifdef HAVE_UTMP_H
48     # include <utmp.h>
49     #endif
50     #ifdef HAVE_LASTLOG_H
51     # include <lastlog.h>
52     #endif
53    
54 sf-exg 1.58 #if !defined(UTMP_FILE)
55     # if defined(_PATH_UTMP)
56     # define UTMP_FILE _PATH_UTMP
57     # elif defined(PT_UTMP_FILE)
58     # define UTMP_FILE PT_UTMP_FILE
59     # endif
60     #endif
61     #if !defined(WTMP_FILE)
62     # if defined(_PATH_WTMP)
63     # define WTMP_FILE _PATH_WTMP
64     # elif defined(PT_WTMP_FILE)
65     # define WTMP_FILE PT_WTMP_FILE
66     # endif
67     #endif
68     #if !defined(WTMPX_FILE)
69     # if defined(_PATH_WTMPX)
70     # define WTMPX_FILE _PATH_WTMPX
71     # elif defined(PT_WTMPX_FILE)
72     # define WTMPX_FILE PT_WTMPX_FILE
73     # endif
74     #endif
75     #if !defined(LASTLOG_FILE)
76     # if defined(_PATH_LASTLOG)
77     # define LASTLOG_FILE _PATH_LASTLOG
78     # elif defined(PT_LASTLOG_FILE)
79     # define LASTLOG_FILE PT_LASTLOG_FILE
80     # endif
81     #endif
82     #if !defined(LASTLOGX_FILE)
83     # if defined(_PATH_LASTLOGX)
84     # define LASTLOGX_FILE _PATH_LASTLOGX
85     # elif defined(PT_LASTLOGX_FILE)
86     # define LASTLOGX_FILE PT_LASTLOGX_FILE
87     # endif
88     #endif
89    
90 sf-exg 1.56 #include <pwd.h>
91    
92 root 1.7 #include <cstdio>
93 root 1.25 #include <cstring>
94 root 1.7
95     #include <sys/types.h>
96     #include <sys/stat.h>
97 ayin 1.23 #include <fcntl.h>
98 root 1.7 #include <unistd.h>
99     #include <time.h>
100 ayin 1.24 #include <errno.h>
101 root 1.7
102 root 1.1 /*
103     * BSD style utmp entry
104     * ut_line, ut_name, ut_host, ut_time
105     * SYSV style utmp (and utmpx) entry
106     * ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time
107     */
108    
109     /* ------------------------------------------------------------------------- */
110 ayin 1.12 /*
111     * Write a BSD style utmp entry
112     */
113     #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
114 sf-exg 1.53 static void
115 sf-exg 1.43 write_bsd_utmp (int utmp_pos, struct utmp *ut)
116 ayin 1.12 {
117     int fd;
118    
119     if (utmp_pos <= 0 || (fd = open (UTMP_FILE, O_WRONLY)) == -1)
120 sf-exg 1.53 return;
121 ayin 1.12
122     if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1)
123 sf-exg 1.43 write (fd, ut, sizeof (struct utmp));
124 ayin 1.12 close (fd);
125     }
126     #endif
127    
128     /* ------------------------------------------------------------------------- */
129     /*
130     * Update a BSD style wtmp entry
131     */
132     #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP)
133     static void
134 sf-exg 1.43 update_wtmp (const char *fname, const struct utmp *ut)
135 ayin 1.12 {
136 sf-exg 1.29 int fd, gotlock, retry;
137 ayin 1.12 struct flock lck; /* fcntl locking scheme */
138     struct stat sbuf;
139    
140     if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
141     return;
142    
143     lck.l_whence = SEEK_END; /* start lock at current eof */
144     lck.l_len = 0; /* end at ``largest possible eof'' */
145     lck.l_start = 0;
146     lck.l_type = F_WRLCK; /* we want a write lock */
147    
148     /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
149 sf-exg 1.29 for (retry = 10, gotlock = 0; retry--;)
150 ayin 1.12 if (fcntl (fd, F_SETLK, &lck) != -1)
151     {
152 sf-exg 1.29 gotlock = 1;
153 ayin 1.12 break;
154     }
155     else if (errno != EAGAIN && errno != EACCES)
156 sf-exg 1.29 break;
157 sf-exg 1.57
158     if (gotlock)
159 sf-exg 1.29 {
160 sf-exg 1.57 if (fstat (fd, &sbuf) == 0)
161     if (write (fd, ut, sizeof (struct utmp)) != sizeof (struct utmp))
162     ftruncate (fd, sbuf.st_size); /* remove bad writes */
163    
164     lck.l_type = F_UNLCK; /* unlocking the file */
165     fcntl (fd, F_SETLK, &lck);
166 sf-exg 1.29 }
167 ayin 1.12
168     close (fd);
169     }
170     #endif
171    
172     /* ------------------------------------------------------------------------- */
173     #ifdef LASTLOG_SUPPORT
174     static void
175 sf-exg 1.31 update_lastlog (const char *pty, const char *host)
176 ayin 1.12 {
177     # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
178     struct lastlogx llx;
179     # endif
180     # ifdef HAVE_STRUCT_LASTLOG
181     int fd;
182     struct lastlog ll;
183     struct passwd *pwent;
184     # endif
185    
186     # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
187     memset (&llx, 0, sizeof (llx));
188     llx.ll_tv.tv_sec = time (NULL);
189     llx.ll_tv.tv_usec = 0;
190     strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
191     strncpy (llx.ll_host, host, sizeof (llx.ll_host));
192     updlastlogx (LASTLOGX_FILE, getuid (), &llx);
193     # endif
194    
195     # ifdef HAVE_STRUCT_LASTLOG
196     pwent = getpwuid (getuid ());
197     if (!pwent)
198     {
199 sf-exg 1.44 PTYTTY_WARN ("no entry in password file, not updating lastlog.\n", 0);
200 ayin 1.12 return;
201     }
202    
203     memset (&ll, 0, sizeof (ll));
204     ll.ll_time = time (NULL);
205     strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
206     strncpy (ll.ll_host, host, sizeof (ll.ll_host));
207 sf-exg 1.48 if ((fd = open (LASTLOG_FILE, O_RDWR)) != -1)
208 ayin 1.13 {
209 sf-exg 1.48 if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
210     SEEK_SET) != -1)
211     write (fd, &ll, sizeof (ll));
212     close (fd);
213 ayin 1.13 }
214 ayin 1.17 # endif /* HAVE_STRUCT_LASTLOG */
215 ayin 1.12 }
216 ayin 1.17 #endif /* LASTLOG_SUPPORT */
217 ayin 1.12
218 sf-exg 1.45 #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
219     static void
220     fill_id (char *id, const char *line, size_t id_size)
221     {
222     size_t len = strlen (line);
223    
224     if (len > id_size)
225     line += len - id_size;
226     strncpy (id, line, id_size);
227     }
228     #endif
229    
230 sf-exg 1.32 #ifdef HAVE_STRUCT_UTMP
231     static void
232 sf-exg 1.45 fill_utmp (struct utmp *ut, bool login, int pid, const char *line, const char *user, const char *host)
233 sf-exg 1.32 {
234     memset (ut, 0, sizeof (struct utmp));
235    
236 sf-exg 1.40 strncpy (ut->ut_line, line, sizeof (ut->ut_line));
237 sf-exg 1.32 # ifdef HAVE_UTMP_PID
238 sf-exg 1.45 fill_id (ut->ut_id, line, sizeof (ut->ut_id));
239 sf-exg 1.32 ut->ut_pid = pid;
240 sf-exg 1.35 ut->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
241 sf-exg 1.32 # endif
242     ut->ut_time = time (NULL);
243    
244     if (login)
245     {
246     # ifdef HAVE_UTMP_PID
247     strncpy (ut->ut_user, user, sizeof (ut->ut_user));
248     # else
249     strncpy (ut->ut_name, user, sizeof (ut->ut_name));
250     # endif
251     # ifdef HAVE_UTMP_HOST
252     strncpy (ut->ut_host, host, sizeof (ut->ut_host));
253     # endif
254     }
255     }
256     #endif
257    
258     #ifdef HAVE_STRUCT_UTMPX
259     static void
260 sf-exg 1.45 fill_utmpx (struct utmpx *utx, bool login, int pid, const char *line, const char *user, const char *host)
261 sf-exg 1.32 {
262     memset (utx, 0, sizeof (struct utmpx));
263    
264 sf-exg 1.40 // posix says that ut_line is not meaningful for DEAD_PROCESS
265 sf-exg 1.41 // records, but most implementations of last use ut_line to
266 sf-exg 1.40 // associate records in wtmp file
267     strncpy (utx->ut_line, line, sizeof (utx->ut_line));
268 sf-exg 1.45 fill_id (utx->ut_id, line, sizeof (utx->ut_id));
269 sf-exg 1.32 utx->ut_pid = pid;
270 sf-exg 1.35 utx->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
271 sf-exg 1.32 utx->ut_tv.tv_sec = time (NULL);
272     utx->ut_tv.tv_usec = 0;
273     # if HAVE_UTMPX_SESSION
274     utx->ut_session = getsid (0);
275     # endif
276    
277 sf-exg 1.54 // posix says that ut_user is not meaningful for DEAD_PROCESS
278     // records, but solaris utmp_update helper requires that the ut_user
279     // field of a DEAD_PROCESS entry matches the one of an existing
280     // USER_PROCESS entry for the same line, if any
281     strncpy (utx->ut_user, user, sizeof (utx->ut_user));
282    
283 sf-exg 1.32 if (login)
284     {
285     # ifdef HAVE_UTMPX_HOST
286     strncpy (utx->ut_host, host, sizeof (utx->ut_host));
287     # endif
288     }
289     }
290     #endif
291    
292 ayin 1.12 /* ------------------------------------------------------------------------- */
293 root 1.1
294     /*
295     * make and write utmp and wtmp entries
296     */
297     void
298 root 1.3 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
299 root 1.1 {
300 sf-exg 1.54 if (!name || !*name)
301 root 1.2 return;
302    
303     this->cmd_pid = cmd_pid;
304     this->login_shell = login_shell;
305    
306 sf-exg 1.54 log_session (true, hostname);
307     }
308    
309     void
310     ptytty_unix::log_session (bool login, const char *hostname)
311     {
312 root 1.1 struct passwd *pwent = getpwuid (getuid ());
313 sf-exg 1.39 const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
314 root 1.1
315 sf-exg 1.54 const char *pty = name;
316    
317 root 1.1 if (!strncmp (pty, "/dev/", 5))
318     pty += 5; /* skip /dev/ prefix */
319    
320     #ifdef HAVE_STRUCT_UTMP
321 sf-exg 1.54 struct utmp *tmput;
322 sf-exg 1.55 struct utmp ut;
323     fill_utmp (&ut, login, cmd_pid, pty, user, hostname);
324 root 1.1 #endif
325    
326     #ifdef HAVE_STRUCT_UTMPX
327 sf-exg 1.54 struct utmpx *tmputx;
328 sf-exg 1.55 struct utmpx utx;
329     fill_utmpx (&utx, login, cmd_pid, pty, user, hostname);
330 root 1.1 #endif
331    
332     #ifdef HAVE_STRUCT_UTMP
333     # ifdef HAVE_UTMP_PID
334 sf-exg 1.37 setutent ();
335 sf-exg 1.55 if (login || ((tmput = getutid (&ut)) && tmput->ut_pid == cmd_pid))
336     pututline (&ut);
337 sf-exg 1.42 endutent ();
338 sf-exg 1.38 # else
339     int fd_stdin = dup (STDIN_FILENO);
340     dup2 (tty, STDIN_FILENO);
341    
342 sf-exg 1.54 int utmp_pos = ttyslot ();
343 sf-exg 1.55 write_bsd_utmp (utmp_pos, &ut);
344 sf-exg 1.38
345     dup2 (fd_stdin, STDIN_FILENO);
346     close (fd_stdin);
347 root 1.1 # endif
348     #endif
349    
350     #ifdef HAVE_STRUCT_UTMPX
351 sf-exg 1.37 setutxent ();
352 sf-exg 1.55 if (login || ((tmputx = getutxid (&utx)) && tmputx->ut_pid == cmd_pid))
353     pututxline (&utx);
354 sf-exg 1.42 endutxent ();
355 root 1.1 #endif
356    
357     #ifdef WTMP_SUPPORT
358 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
359     if (login_shell)
360     #endif
361 root 1.1 {
362     # ifdef HAVE_STRUCT_UTMP
363     # ifdef HAVE_UPDWTMP
364 sf-exg 1.55 updwtmp (WTMP_FILE, &ut);
365 root 1.1 # else
366 sf-exg 1.55 update_wtmp (WTMP_FILE, &ut);
367 root 1.1 # endif
368     # endif
369 root 1.2 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
370 sf-exg 1.55 updwtmpx (WTMPX_FILE, &utx);
371 root 1.1 # endif
372     }
373     #endif
374 sf-exg 1.54
375 sf-exg 1.31 #ifdef LASTLOG_SUPPORT
376 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
377     if (login_shell)
378     #endif
379 sf-exg 1.54 if (login)
380     update_lastlog (pty, hostname);
381 root 1.1 #endif
382     }
383    
384     /* ------------------------------------------------------------------------- */
385     /*
386     * remove utmp and wtmp entries
387     */
388     void
389 root 1.3 ptytty_unix::logout ()
390 root 1.1 {
391 root 1.2 if (!cmd_pid)
392     return;
393    
394 sf-exg 1.54 log_session (false, 0);
395 root 1.2
396     cmd_pid = 0;
397 root 1.1 }
398    
399 root 1.5 #else
400     void
401     ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
402     {
403     }
404     #endif
405