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