ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/logging.C
Revision: 1.56
Committed: Thu Nov 24 13:43:05 2011 UTC (14 years, 9 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.55: +12 -0 lines
Log Message:
Move some includes from ptytty.h to logging.C.

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     if (!gotlock)
122     {
123     /* give it up */
124     close (fd);
125     return;
126     }
127 ayin 1.12 if (fstat (fd, &sbuf) == 0)
128 sf-exg 1.43 if (write (fd, ut, sizeof (struct utmp)) != sizeof (struct utmp))
129 ayin 1.12 ftruncate (fd, sbuf.st_size); /* remove bad writes */
130    
131     lck.l_type = F_UNLCK; /* unlocking the file */
132     fcntl (fd, F_SETLK, &lck);
133     close (fd);
134     }
135     #endif
136    
137     /* ------------------------------------------------------------------------- */
138     #ifdef LASTLOG_SUPPORT
139     static void
140 sf-exg 1.31 update_lastlog (const char *pty, const char *host)
141 ayin 1.12 {
142     # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
143     struct lastlogx llx;
144     # endif
145     # ifdef HAVE_STRUCT_LASTLOG
146     int fd;
147     struct lastlog ll;
148     struct passwd *pwent;
149     # endif
150    
151     # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
152     memset (&llx, 0, sizeof (llx));
153     llx.ll_tv.tv_sec = time (NULL);
154     llx.ll_tv.tv_usec = 0;
155     strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
156     strncpy (llx.ll_host, host, sizeof (llx.ll_host));
157     updlastlogx (LASTLOGX_FILE, getuid (), &llx);
158     # endif
159    
160     # ifdef HAVE_STRUCT_LASTLOG
161     pwent = getpwuid (getuid ());
162     if (!pwent)
163     {
164 sf-exg 1.44 PTYTTY_WARN ("no entry in password file, not updating lastlog.\n", 0);
165 ayin 1.12 return;
166     }
167    
168     memset (&ll, 0, sizeof (ll));
169     ll.ll_time = time (NULL);
170     strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
171     strncpy (ll.ll_host, host, sizeof (ll.ll_host));
172 sf-exg 1.48 if ((fd = open (LASTLOG_FILE, O_RDWR)) != -1)
173 ayin 1.13 {
174 sf-exg 1.48 if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
175     SEEK_SET) != -1)
176     write (fd, &ll, sizeof (ll));
177     close (fd);
178 ayin 1.13 }
179 ayin 1.17 # endif /* HAVE_STRUCT_LASTLOG */
180 ayin 1.12 }
181 ayin 1.17 #endif /* LASTLOG_SUPPORT */
182 ayin 1.12
183 sf-exg 1.45 #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
184     static void
185     fill_id (char *id, const char *line, size_t id_size)
186     {
187     size_t len = strlen (line);
188    
189     if (len > id_size)
190     line += len - id_size;
191     strncpy (id, line, id_size);
192     }
193     #endif
194    
195 sf-exg 1.32 #ifdef HAVE_STRUCT_UTMP
196     static void
197 sf-exg 1.45 fill_utmp (struct utmp *ut, bool login, int pid, const char *line, const char *user, const char *host)
198 sf-exg 1.32 {
199     memset (ut, 0, sizeof (struct utmp));
200    
201 sf-exg 1.40 strncpy (ut->ut_line, line, sizeof (ut->ut_line));
202 sf-exg 1.32 # ifdef HAVE_UTMP_PID
203 sf-exg 1.45 fill_id (ut->ut_id, line, sizeof (ut->ut_id));
204 sf-exg 1.32 ut->ut_pid = pid;
205 sf-exg 1.35 ut->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
206 sf-exg 1.32 # endif
207     ut->ut_time = time (NULL);
208    
209     if (login)
210     {
211     # ifdef HAVE_UTMP_PID
212     strncpy (ut->ut_user, user, sizeof (ut->ut_user));
213     # else
214     strncpy (ut->ut_name, user, sizeof (ut->ut_name));
215     # endif
216     # ifdef HAVE_UTMP_HOST
217     strncpy (ut->ut_host, host, sizeof (ut->ut_host));
218     # endif
219     }
220     }
221     #endif
222    
223     #ifdef HAVE_STRUCT_UTMPX
224     static void
225 sf-exg 1.45 fill_utmpx (struct utmpx *utx, bool login, int pid, const char *line, const char *user, const char *host)
226 sf-exg 1.32 {
227     memset (utx, 0, sizeof (struct utmpx));
228    
229 sf-exg 1.40 // posix says that ut_line is not meaningful for DEAD_PROCESS
230 sf-exg 1.41 // records, but most implementations of last use ut_line to
231 sf-exg 1.40 // associate records in wtmp file
232     strncpy (utx->ut_line, line, sizeof (utx->ut_line));
233 sf-exg 1.45 fill_id (utx->ut_id, line, sizeof (utx->ut_id));
234 sf-exg 1.32 utx->ut_pid = pid;
235 sf-exg 1.35 utx->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
236 sf-exg 1.32 utx->ut_tv.tv_sec = time (NULL);
237     utx->ut_tv.tv_usec = 0;
238     # if HAVE_UTMPX_SESSION
239     utx->ut_session = getsid (0);
240     # endif
241    
242 sf-exg 1.54 // posix says that ut_user is not meaningful for DEAD_PROCESS
243     // records, but solaris utmp_update helper requires that the ut_user
244     // field of a DEAD_PROCESS entry matches the one of an existing
245     // USER_PROCESS entry for the same line, if any
246     strncpy (utx->ut_user, user, sizeof (utx->ut_user));
247    
248 sf-exg 1.32 if (login)
249     {
250     # ifdef HAVE_UTMPX_HOST
251     strncpy (utx->ut_host, host, sizeof (utx->ut_host));
252     # endif
253     }
254     }
255     #endif
256    
257 ayin 1.12 /* ------------------------------------------------------------------------- */
258 root 1.1
259     /*
260     * make and write utmp and wtmp entries
261     */
262     void
263 root 1.3 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
264 root 1.1 {
265 sf-exg 1.54 if (!name || !*name)
266 root 1.2 return;
267    
268     this->cmd_pid = cmd_pid;
269     this->login_shell = login_shell;
270    
271 sf-exg 1.54 log_session (true, hostname);
272     }
273    
274     void
275     ptytty_unix::log_session (bool login, const char *hostname)
276     {
277 root 1.1 struct passwd *pwent = getpwuid (getuid ());
278 sf-exg 1.39 const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
279 root 1.1
280 sf-exg 1.54 const char *pty = name;
281    
282 root 1.1 if (!strncmp (pty, "/dev/", 5))
283     pty += 5; /* skip /dev/ prefix */
284    
285     #ifdef HAVE_STRUCT_UTMP
286 sf-exg 1.54 struct utmp *tmput;
287 sf-exg 1.55 struct utmp ut;
288     fill_utmp (&ut, login, cmd_pid, pty, user, hostname);
289 root 1.1 #endif
290    
291     #ifdef HAVE_STRUCT_UTMPX
292 sf-exg 1.54 struct utmpx *tmputx;
293 sf-exg 1.55 struct utmpx utx;
294     fill_utmpx (&utx, login, cmd_pid, pty, user, hostname);
295 root 1.1 #endif
296    
297     #ifdef HAVE_STRUCT_UTMP
298     # ifdef HAVE_UTMP_PID
299 sf-exg 1.37 setutent ();
300 sf-exg 1.55 if (login || ((tmput = getutid (&ut)) && tmput->ut_pid == cmd_pid))
301     pututline (&ut);
302 sf-exg 1.42 endutent ();
303 sf-exg 1.38 # else
304     int fd_stdin = dup (STDIN_FILENO);
305     dup2 (tty, STDIN_FILENO);
306    
307 sf-exg 1.54 int utmp_pos = ttyslot ();
308 sf-exg 1.55 write_bsd_utmp (utmp_pos, &ut);
309 sf-exg 1.38
310     dup2 (fd_stdin, STDIN_FILENO);
311     close (fd_stdin);
312 root 1.1 # endif
313     #endif
314    
315     #ifdef HAVE_STRUCT_UTMPX
316 sf-exg 1.37 setutxent ();
317 sf-exg 1.55 if (login || ((tmputx = getutxid (&utx)) && tmputx->ut_pid == cmd_pid))
318     pututxline (&utx);
319 sf-exg 1.42 endutxent ();
320 root 1.1 #endif
321    
322     #ifdef WTMP_SUPPORT
323 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
324     if (login_shell)
325     #endif
326 root 1.1 {
327     # ifdef HAVE_STRUCT_UTMP
328     # ifdef HAVE_UPDWTMP
329 sf-exg 1.55 updwtmp (WTMP_FILE, &ut);
330 root 1.1 # else
331 sf-exg 1.55 update_wtmp (WTMP_FILE, &ut);
332 root 1.1 # endif
333     # endif
334 root 1.2 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
335 sf-exg 1.55 updwtmpx (WTMPX_FILE, &utx);
336 root 1.1 # endif
337     }
338     #endif
339 sf-exg 1.54
340 sf-exg 1.31 #ifdef LASTLOG_SUPPORT
341 root 1.2 #ifdef LOG_ONLY_ON_LOGIN
342     if (login_shell)
343     #endif
344 sf-exg 1.54 if (login)
345     update_lastlog (pty, hostname);
346 root 1.1 #endif
347     }
348    
349     /* ------------------------------------------------------------------------- */
350     /*
351     * remove utmp and wtmp entries
352     */
353     void
354 root 1.3 ptytty_unix::logout ()
355 root 1.1 {
356 root 1.2 if (!cmd_pid)
357     return;
358    
359 sf-exg 1.54 log_session (false, 0);
360 root 1.2
361     cmd_pid = 0;
362 root 1.1 }
363    
364 root 1.5 #else
365     void
366     ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
367     {
368     }
369     #endif
370