ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/logging.C
Revision: 1.60
Committed: Thu Dec 22 10:03:01 2011 UTC (14 years, 8 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.59: +7 -10 lines
Log Message:
Inhibit also update of lastlogx if there is no entry in the user
database for the real user id.

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     # endif
184    
185     # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
186     memset (&llx, 0, sizeof (llx));
187     llx.ll_tv.tv_sec = time (NULL);
188     llx.ll_tv.tv_usec = 0;
189     strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
190     strncpy (llx.ll_host, host, sizeof (llx.ll_host));
191     updlastlogx (LASTLOGX_FILE, getuid (), &llx);
192     # endif
193    
194     # ifdef HAVE_STRUCT_LASTLOG
195     memset (&ll, 0, sizeof (ll));
196     ll.ll_time = time (NULL);
197     strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
198     strncpy (ll.ll_host, host, sizeof (ll.ll_host));
199 sf-exg 1.48 if ((fd = open (LASTLOG_FILE, O_RDWR)) != -1)
200 ayin 1.13 {
201 sf-exg 1.60 if (lseek (fd, (off_t) (getuid () * sizeof (ll)),
202 sf-exg 1.48 SEEK_SET) != -1)
203     write (fd, &ll, sizeof (ll));
204     close (fd);
205 ayin 1.13 }
206 ayin 1.17 # endif /* HAVE_STRUCT_LASTLOG */
207 ayin 1.12 }
208 ayin 1.17 #endif /* LASTLOG_SUPPORT */
209 ayin 1.12
210 sf-exg 1.45 #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
211     static void
212     fill_id (char *id, const char *line, size_t id_size)
213     {
214     size_t len = strlen (line);
215    
216     if (len > id_size)
217     line += len - id_size;
218     strncpy (id, line, id_size);
219     }
220     #endif
221    
222 sf-exg 1.32 #ifdef HAVE_STRUCT_UTMP
223     static void
224 sf-exg 1.45 fill_utmp (struct utmp *ut, bool login, int pid, const char *line, const char *user, const char *host)
225 sf-exg 1.32 {
226     memset (ut, 0, sizeof (struct utmp));
227    
228 sf-exg 1.40 strncpy (ut->ut_line, line, sizeof (ut->ut_line));
229 sf-exg 1.32 # ifdef HAVE_UTMP_PID
230 sf-exg 1.45 fill_id (ut->ut_id, line, sizeof (ut->ut_id));
231 sf-exg 1.32 ut->ut_pid = pid;
232 sf-exg 1.35 ut->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
233 sf-exg 1.32 # endif
234     ut->ut_time = time (NULL);
235    
236     if (login)
237     {
238     # ifdef HAVE_UTMP_PID
239     strncpy (ut->ut_user, user, sizeof (ut->ut_user));
240     # else
241     strncpy (ut->ut_name, user, sizeof (ut->ut_name));
242     # endif
243     # ifdef HAVE_UTMP_HOST
244     strncpy (ut->ut_host, host, sizeof (ut->ut_host));
245     # endif
246     }
247     }
248     #endif
249    
250     #ifdef HAVE_STRUCT_UTMPX
251     static void
252 sf-exg 1.45 fill_utmpx (struct utmpx *utx, bool login, int pid, const char *line, const char *user, const char *host)
253 sf-exg 1.32 {
254     memset (utx, 0, sizeof (struct utmpx));
255    
256 sf-exg 1.40 // posix says that ut_line is not meaningful for DEAD_PROCESS
257 sf-exg 1.41 // records, but most implementations of last use ut_line to
258 sf-exg 1.40 // associate records in wtmp file
259     strncpy (utx->ut_line, line, sizeof (utx->ut_line));
260 sf-exg 1.45 fill_id (utx->ut_id, line, sizeof (utx->ut_id));
261 sf-exg 1.32 utx->ut_pid = pid;
262 sf-exg 1.35 utx->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
263 sf-exg 1.32 utx->ut_tv.tv_sec = time (NULL);
264     utx->ut_tv.tv_usec = 0;
265     # if HAVE_UTMPX_SESSION
266     utx->ut_session = getsid (0);
267     # endif
268    
269 sf-exg 1.54 // posix says that ut_user is not meaningful for DEAD_PROCESS
270     // records, but solaris utmp_update helper requires that the ut_user
271     // field of a DEAD_PROCESS entry matches the one of an existing
272     // USER_PROCESS entry for the same line, if any
273     strncpy (utx->ut_user, user, sizeof (utx->ut_user));
274    
275 sf-exg 1.32 if (login)
276     {
277     # ifdef HAVE_UTMPX_HOST
278     strncpy (utx->ut_host, host, sizeof (utx->ut_host));
279     # endif
280     }
281     }
282     #endif
283    
284 ayin 1.12 /* ------------------------------------------------------------------------- */
285 root 1.1
286     /*
287     * make and write utmp and wtmp entries
288     */
289     void
290 root 1.3 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
291 root 1.1 {
292 sf-exg 1.54 if (!name || !*name)
293 root 1.2 return;
294    
295     this->cmd_pid = cmd_pid;
296     this->login_shell = login_shell;
297    
298 sf-exg 1.54 log_session (true, hostname);
299     }
300    
301     void
302     ptytty_unix::log_session (bool login, const char *hostname)
303     {
304 root 1.1 struct passwd *pwent = getpwuid (getuid ());
305 sf-exg 1.39 const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
306 root 1.1
307 sf-exg 1.54 const char *pty = name;
308    
309 root 1.1 if (!strncmp (pty, "/dev/", 5))
310     pty += 5; /* skip /dev/ prefix */
311    
312     #ifdef HAVE_STRUCT_UTMP
313 sf-exg 1.54 struct utmp *tmput;
314 sf-exg 1.55 struct utmp ut;
315     fill_utmp (&ut, login, cmd_pid, pty, user, hostname);
316 root 1.1 #endif
317    
318     #ifdef HAVE_STRUCT_UTMPX
319 sf-exg 1.54 struct utmpx *tmputx;
320 sf-exg 1.55 struct utmpx utx;
321     fill_utmpx (&utx, login, cmd_pid, pty, user, hostname);
322 root 1.1 #endif
323    
324     #ifdef HAVE_STRUCT_UTMP
325     # ifdef HAVE_UTMP_PID
326 sf-exg 1.37 setutent ();
327 sf-exg 1.55 if (login || ((tmput = getutid (&ut)) && tmput->ut_pid == cmd_pid))
328     pututline (&ut);
329 sf-exg 1.42 endutent ();
330 sf-exg 1.38 # else
331 sf-exg 1.55 write_bsd_utmp (utmp_pos, &ut);
332 root 1.1 # endif
333     #endif
334    
335     #ifdef HAVE_STRUCT_UTMPX
336 sf-exg 1.37 setutxent ();
337 sf-exg 1.55 if (login || ((tmputx = getutxid (&utx)) && tmputx->ut_pid == cmd_pid))
338     pututxline (&utx);
339 sf-exg 1.42 endutxent ();
340 root 1.1 #endif
341    
342     #ifdef WTMP_SUPPORT
343 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
344     if (login_shell)
345     #endif
346 root 1.1 {
347     # ifdef HAVE_STRUCT_UTMP
348     # ifdef HAVE_UPDWTMP
349 sf-exg 1.55 updwtmp (WTMP_FILE, &ut);
350 root 1.1 # else
351 sf-exg 1.55 update_wtmp (WTMP_FILE, &ut);
352 root 1.1 # endif
353     # endif
354 root 1.2 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
355 sf-exg 1.55 updwtmpx (WTMPX_FILE, &utx);
356 root 1.1 # endif
357     }
358     #endif
359 sf-exg 1.54
360 sf-exg 1.31 #ifdef LASTLOG_SUPPORT
361 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
362     if (login_shell)
363     #endif
364 sf-exg 1.54 if (login)
365 sf-exg 1.60 {
366     if (pwent)
367     update_lastlog (pty, hostname);
368     else
369     PTYTTY_WARN ("no entry in password file, not updating lastlog.\n", 0);
370     }
371 root 1.1 #endif
372     }
373    
374     /* ------------------------------------------------------------------------- */
375     /*
376     * remove utmp and wtmp entries
377     */
378     void
379 root 1.3 ptytty_unix::logout ()
380 root 1.1 {
381 root 1.2 if (!cmd_pid)
382     return;
383    
384 sf-exg 1.54 log_session (false, 0);
385 root 1.2
386     cmd_pid = 0;
387 root 1.1 }
388    
389 root 1.5 #else
390     void
391     ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
392     {
393     }
394     #endif
395