ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.53
Committed: Thu Nov 17 11:59:54 2011 UTC (14 years, 10 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.52: +3 -5 lines
Log Message:
Simplify.

Avoiding an open when the utmp file cannot be opened is not worth the
extra conditional.

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