ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/logging.C
Revision: 1.61
Committed: Thu Dec 22 10:16:49 2011 UTC (14 years, 8 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.60: +0 -3 lines
Log Message:
Remove support for utmpx ut_session extension, it serves no purpose.

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    
266 sf-exg 1.54 // posix says that ut_user is not meaningful for DEAD_PROCESS
267     // records, but solaris utmp_update helper requires that the ut_user
268     // field of a DEAD_PROCESS entry matches the one of an existing
269     // USER_PROCESS entry for the same line, if any
270     strncpy (utx->ut_user, user, sizeof (utx->ut_user));
271    
272 sf-exg 1.32 if (login)
273     {
274     # ifdef HAVE_UTMPX_HOST
275     strncpy (utx->ut_host, host, sizeof (utx->ut_host));
276     # endif
277     }
278     }
279     #endif
280    
281 ayin 1.12 /* ------------------------------------------------------------------------- */
282 root 1.1
283     /*
284     * make and write utmp and wtmp entries
285     */
286     void
287 root 1.3 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
288 root 1.1 {
289 sf-exg 1.54 if (!name || !*name)
290 root 1.2 return;
291    
292     this->cmd_pid = cmd_pid;
293     this->login_shell = login_shell;
294    
295 sf-exg 1.54 log_session (true, hostname);
296     }
297    
298     void
299     ptytty_unix::log_session (bool login, const char *hostname)
300     {
301 root 1.1 struct passwd *pwent = getpwuid (getuid ());
302 sf-exg 1.39 const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
303 root 1.1
304 sf-exg 1.54 const char *pty = name;
305    
306 root 1.1 if (!strncmp (pty, "/dev/", 5))
307     pty += 5; /* skip /dev/ prefix */
308    
309     #ifdef HAVE_STRUCT_UTMP
310 sf-exg 1.54 struct utmp *tmput;
311 sf-exg 1.55 struct utmp ut;
312     fill_utmp (&ut, login, cmd_pid, pty, user, hostname);
313 root 1.1 #endif
314    
315     #ifdef HAVE_STRUCT_UTMPX
316 sf-exg 1.54 struct utmpx *tmputx;
317 sf-exg 1.55 struct utmpx utx;
318     fill_utmpx (&utx, login, cmd_pid, pty, user, hostname);
319 root 1.1 #endif
320    
321     #ifdef HAVE_STRUCT_UTMP
322     # ifdef HAVE_UTMP_PID
323 sf-exg 1.37 setutent ();
324 sf-exg 1.55 if (login || ((tmput = getutid (&ut)) && tmput->ut_pid == cmd_pid))
325     pututline (&ut);
326 sf-exg 1.42 endutent ();
327 sf-exg 1.38 # else
328 sf-exg 1.55 write_bsd_utmp (utmp_pos, &ut);
329 root 1.1 # endif
330     #endif
331    
332     #ifdef HAVE_STRUCT_UTMPX
333 sf-exg 1.37 setutxent ();
334 sf-exg 1.55 if (login || ((tmputx = getutxid (&utx)) && tmputx->ut_pid == cmd_pid))
335     pututxline (&utx);
336 sf-exg 1.42 endutxent ();
337 root 1.1 #endif
338    
339     #ifdef WTMP_SUPPORT
340 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
341     if (login_shell)
342     #endif
343 root 1.1 {
344     # ifdef HAVE_STRUCT_UTMP
345     # ifdef HAVE_UPDWTMP
346 sf-exg 1.55 updwtmp (WTMP_FILE, &ut);
347 root 1.1 # else
348 sf-exg 1.55 update_wtmp (WTMP_FILE, &ut);
349 root 1.1 # endif
350     # endif
351 root 1.2 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
352 sf-exg 1.55 updwtmpx (WTMPX_FILE, &utx);
353 root 1.1 # endif
354     }
355     #endif
356 sf-exg 1.54
357 sf-exg 1.31 #ifdef LASTLOG_SUPPORT
358 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
359     if (login_shell)
360     #endif
361 sf-exg 1.54 if (login)
362 sf-exg 1.60 {
363     if (pwent)
364     update_lastlog (pty, hostname);
365     else
366     PTYTTY_WARN ("no entry in password file, not updating lastlog.\n", 0);
367     }
368 root 1.1 #endif
369     }
370    
371     /* ------------------------------------------------------------------------- */
372     /*
373     * remove utmp and wtmp entries
374     */
375     void
376 root 1.3 ptytty_unix::logout ()
377 root 1.1 {
378 root 1.2 if (!cmd_pid)
379     return;
380    
381 sf-exg 1.54 log_session (false, 0);
382 root 1.2
383     cmd_pid = 0;
384 root 1.1 }
385    
386 root 1.5 #else
387     void
388     ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
389     {
390     }
391     #endif
392