ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/logging.C
Revision: 1.72
Committed: Tue May 25 13:51:22 2021 UTC (5 years, 3 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.71: +4 -5 lines
Log Message:
Include microseconds in utmpx and lastlogx time

File Contents

# User Rev Content
1 root 1.6 /*----------------------------------------------------------------------*
2 root 1.1 * File: logging.C
3     *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6     * Copyright (c) 1992 John Bovey <jdb@ukc.ac.uk>
7     * - original version
8     * Copyright (c) 1993 lipka
9     * Copyright (c) 1993 Brian Stempien <stempien@cs.wmich.edu>
10     * Copyright (c) 1995 Raul Garcia Garcia <rgg@tid.es>
11     * Copyright (c) 1995 Piet W. Plomp <piet@idefix.icce.rug.nl>
12     * Copyright (c) 1997 Raul Garcia Garcia <rgg@tid.es>
13     * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
14     * - extensive modifications
15     * Copyright (c) 1999 D J Hawkey Jr <hawkeyd@visi.com>
16     * - lastlog support
17 sf-exg 1.30 * Copyright (c) 2004-2006 Marc Lehmann <schmorp@schmorp.de>
18 sf-exg 1.71 * Copyright (c) 2006-2021 Emanuele Giaquinta <e.giaquinta@glauco.it>
19 root 1.1 *
20     * This program is free software; you can redistribute it and/or modify
21     * it under the terms of the GNU General Public License as published by
22 sf-exg 1.68 * the Free Software Foundation; either version 2 of the License, or
23 root 1.1 * (at your option) any later version.
24     *
25     * This program is distributed in the hope that it will be useful,
26     * but WITHOUT ANY WARRANTY; without even the implied warranty of
27     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28     * GNU General Public License for more details.
29     *
30     * You should have received a copy of the GNU General Public License
31     * along with this program; if not, write to the Free Software
32     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33     *----------------------------------------------------------------------*/
34    
35 sf-exg 1.26 #include "config.h"
36 root 1.4
37 root 1.10 #include "ptytty.h"
38    
39 sf-exg 1.71 #include <pwd.h>
40     #include <stdio.h>
41     #include <string.h>
42 sf-exg 1.69 #include <sys/types.h>
43 sf-exg 1.71 #include <unistd.h>
44    
45     static void
46     fill_id (char *id, const char *line, size_t id_size)
47     {
48     size_t len = strlen (line);
49    
50     if (len > id_size)
51     line += len - id_size;
52     strncpy (id, line, id_size);
53     }
54    
55     /*
56     * BSD style utmp entry
57     * ut_line, ut_name, ut_host, ut_time
58     * SYSV style utmp (and utmpx) entry
59     * ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time
60     */
61    
62     #if defined(USE_UTMPX)
63    
64 sf-exg 1.72 #include <sys/time.h>
65 sf-exg 1.71 #include <utmpx.h>
66 sf-exg 1.69
67 sf-exg 1.71 #if !defined(WTMPX_FILE)
68     # if defined(_PATH_WTMPX)
69     # define WTMPX_FILE _PATH_WTMPX
70     # elif defined(PT_WTMPX_FILE)
71     # define WTMPX_FILE PT_WTMPX_FILE
72     # endif
73     #endif
74 root 1.10
75 sf-exg 1.71 #if !defined(LASTLOGX_FILE)
76     # if defined(_PATH_LASTLOGX)
77     # define LASTLOGX_FILE _PATH_LASTLOGX
78     # elif defined(PT_LASTLOGX_FILE)
79     # define LASTLOGX_FILE PT_LASTLOGX_FILE
80     # endif
81 sf-exg 1.56 #endif
82 sf-exg 1.71
83     #ifdef LASTLOG_SUPPORT
84     static void
85     update_lastlog (const char *pty, const char *host)
86     {
87     # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
88     struct lastlogx llx;
89    
90     memset (&llx, 0, sizeof (llx));
91 sf-exg 1.72 gettimeofday (&llx.ll_tv, 0);
92 sf-exg 1.71 strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
93     strncpy (llx.ll_host, host, sizeof (llx.ll_host));
94     updlastlogx (LASTLOGX_FILE, getuid (), &llx);
95     # endif
96     }
97 sf-exg 1.56 #endif
98 sf-exg 1.71
99     static void
100     fill_utmpx (struct utmpx *utx, bool login, int pid, const char *line, const char *user, const char *host)
101     {
102     memset (utx, 0, sizeof (struct utmpx));
103    
104     // posix says that ut_line is not meaningful for DEAD_PROCESS
105     // records, but most implementations of last use ut_line to
106     // associate records in wtmp file
107     strncpy (utx->ut_line, line, sizeof (utx->ut_line));
108     fill_id (utx->ut_id, line, sizeof (utx->ut_id));
109     utx->ut_pid = pid;
110     utx->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
111 sf-exg 1.72 gettimeofday (&utx->ut_tv, 0);
112 sf-exg 1.71
113     // posix says that ut_user is not meaningful for DEAD_PROCESS
114     // records, but solaris utmp_update helper requires that the ut_user
115     // field of a DEAD_PROCESS entry matches the one of an existing
116     // USER_PROCESS entry for the same line, if any
117     strncpy (utx->ut_user, user, sizeof (utx->ut_user));
118    
119     #ifdef HAVE_UTMPX_HOST
120     if (login)
121     strncpy (utx->ut_host, host, sizeof (utx->ut_host));
122     #endif
123     }
124    
125     void
126     ptytty_unix::log_session (bool login, const char *hostname)
127     {
128     struct passwd *pwent = getpwuid (getuid ());
129     const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
130    
131     const char *pty = name;
132    
133     if (!strncmp (pty, "/dev/", 5))
134     pty += 5; /* skip /dev/ prefix */
135    
136     struct utmpx *tmputx;
137     struct utmpx utx;
138     fill_utmpx (&utx, login, cmd_pid, pty, user, hostname);
139    
140     setutxent ();
141     if (login || ((tmputx = getutxid (&utx)) && tmputx->ut_pid == cmd_pid))
142     pututxline (&utx);
143     endutxent ();
144    
145     if (login_shell)
146     {
147     #if defined(WTMP_SUPPORT) && defined(HAVE_UPDWTMPX)
148     updwtmpx (WTMPX_FILE, &utx);
149     #endif
150    
151     #ifdef LASTLOG_SUPPORT
152     if (login)
153     {
154     if (pwent)
155     update_lastlog (pty, hostname);
156     else
157     PTYTTY_WARN ("no entry in password file, not updating lastlog.\n");
158     }
159     #endif
160     }
161     }
162    
163     #elif defined(USE_UTMP)
164    
165 sf-exg 1.72 #include <time.h>
166 sf-exg 1.71 #include <utmp.h>
167 sf-exg 1.56 #ifdef HAVE_LASTLOG_H
168     # include <lastlog.h>
169     #endif
170    
171 sf-exg 1.58 #if !defined(UTMP_FILE)
172     # if defined(_PATH_UTMP)
173     # define UTMP_FILE _PATH_UTMP
174     # elif defined(PT_UTMP_FILE)
175     # define UTMP_FILE PT_UTMP_FILE
176     # endif
177     #endif
178 sf-exg 1.71
179 sf-exg 1.58 #if !defined(WTMP_FILE)
180     # if defined(_PATH_WTMP)
181     # define WTMP_FILE _PATH_WTMP
182     # elif defined(PT_WTMP_FILE)
183     # define WTMP_FILE PT_WTMP_FILE
184     # endif
185     #endif
186 sf-exg 1.71
187 sf-exg 1.58 #if !defined(LASTLOG_FILE)
188     # if defined(_PATH_LASTLOG)
189     # define LASTLOG_FILE _PATH_LASTLOG
190     # elif defined(PT_LASTLOG_FILE)
191     # define LASTLOG_FILE PT_LASTLOG_FILE
192     # endif
193     #endif
194 root 1.7
195     #include <sys/stat.h>
196 ayin 1.23 #include <fcntl.h>
197 ayin 1.24 #include <errno.h>
198 root 1.7
199 sf-exg 1.53 static void
200 sf-exg 1.70 write_record (const char *path, off_t pos, const void *record, size_t size)
201 ayin 1.12 {
202 sf-exg 1.70 int fd = open (path, O_WRONLY);
203     if (fd >= 0)
204     {
205     pwrite (fd, record, size, pos * size);
206     close (fd);
207     }
208 ayin 1.12 }
209    
210     /* ------------------------------------------------------------------------- */
211     /*
212     * Update a BSD style wtmp entry
213     */
214 sf-exg 1.71 #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP)
215 ayin 1.12 static void
216 sf-exg 1.71 updwtmp (const char *fname, const struct utmp *ut)
217 ayin 1.12 {
218 sf-exg 1.29 int fd, gotlock, retry;
219 ayin 1.12 struct flock lck; /* fcntl locking scheme */
220     struct stat sbuf;
221    
222     if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
223     return;
224    
225     lck.l_whence = SEEK_END; /* start lock at current eof */
226     lck.l_len = 0; /* end at ``largest possible eof'' */
227     lck.l_start = 0;
228     lck.l_type = F_WRLCK; /* we want a write lock */
229    
230     /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
231 sf-exg 1.29 for (retry = 10, gotlock = 0; retry--;)
232 ayin 1.12 if (fcntl (fd, F_SETLK, &lck) != -1)
233     {
234 sf-exg 1.29 gotlock = 1;
235 ayin 1.12 break;
236     }
237     else if (errno != EAGAIN && errno != EACCES)
238 sf-exg 1.29 break;
239 sf-exg 1.57
240     if (gotlock)
241 sf-exg 1.29 {
242 sf-exg 1.57 if (fstat (fd, &sbuf) == 0)
243     if (write (fd, ut, sizeof (struct utmp)) != sizeof (struct utmp))
244     ftruncate (fd, sbuf.st_size); /* remove bad writes */
245    
246     lck.l_type = F_UNLCK; /* unlocking the file */
247     fcntl (fd, F_SETLK, &lck);
248 sf-exg 1.29 }
249 ayin 1.12
250     close (fd);
251     }
252     #endif
253    
254     /* ------------------------------------------------------------------------- */
255     #ifdef LASTLOG_SUPPORT
256     static void
257 sf-exg 1.31 update_lastlog (const char *pty, const char *host)
258 ayin 1.12 {
259     # ifdef HAVE_STRUCT_LASTLOG
260 sf-exg 1.70 struct lastlog ll;
261 ayin 1.12
262     memset (&ll, 0, sizeof (ll));
263     ll.ll_time = time (NULL);
264     strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
265     strncpy (ll.ll_host, host, sizeof (ll.ll_host));
266 sf-exg 1.70 write_record (LASTLOG_FILE, getuid (), &ll, sizeof (ll));
267 ayin 1.17 # endif /* HAVE_STRUCT_LASTLOG */
268 ayin 1.12 }
269 ayin 1.17 #endif /* LASTLOG_SUPPORT */
270 ayin 1.12
271 sf-exg 1.32 static void
272 sf-exg 1.45 fill_utmp (struct utmp *ut, bool login, int pid, const char *line, const char *user, const char *host)
273 sf-exg 1.32 {
274     memset (ut, 0, sizeof (struct utmp));
275    
276 sf-exg 1.40 strncpy (ut->ut_line, line, sizeof (ut->ut_line));
277 sf-exg 1.71 #ifdef HAVE_UTMP_PID
278 sf-exg 1.45 fill_id (ut->ut_id, line, sizeof (ut->ut_id));
279 sf-exg 1.32 ut->ut_pid = pid;
280 sf-exg 1.35 ut->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
281 sf-exg 1.71 #endif
282 sf-exg 1.32 ut->ut_time = time (NULL);
283    
284     if (login)
285     {
286 sf-exg 1.71 #ifdef HAVE_UTMP_PID
287 sf-exg 1.32 strncpy (ut->ut_user, user, sizeof (ut->ut_user));
288 sf-exg 1.71 #else
289 sf-exg 1.32 strncpy (ut->ut_name, user, sizeof (ut->ut_name));
290 sf-exg 1.71 #endif
291     #ifdef HAVE_UTMP_HOST
292 sf-exg 1.32 strncpy (ut->ut_host, host, sizeof (ut->ut_host));
293     #endif
294     }
295     }
296 sf-exg 1.54
297     void
298     ptytty_unix::log_session (bool login, const char *hostname)
299     {
300 root 1.1 struct passwd *pwent = getpwuid (getuid ());
301 sf-exg 1.39 const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
302 root 1.1
303 sf-exg 1.54 const char *pty = name;
304    
305 root 1.1 if (!strncmp (pty, "/dev/", 5))
306     pty += 5; /* skip /dev/ prefix */
307    
308 sf-exg 1.54 struct utmp *tmput;
309 sf-exg 1.55 struct utmp ut;
310     fill_utmp (&ut, login, cmd_pid, pty, user, hostname);
311 root 1.1
312 sf-exg 1.71 #ifdef HAVE_UTMP_PID
313 sf-exg 1.37 setutent ();
314 sf-exg 1.55 if (login || ((tmput = getutid (&ut)) && tmput->ut_pid == cmd_pid))
315     pututline (&ut);
316 sf-exg 1.42 endutent ();
317 sf-exg 1.71 #else
318 sf-exg 1.70 if (utmp_pos > 0)
319     write_record (UTMP_FILE, utmp_pos, &ut, sizeof (ut));
320 root 1.1 #endif
321    
322 root 1.2 if (login_shell)
323 root 1.1 {
324 sf-exg 1.71 #ifdef WTMP_SUPPORT
325 sf-exg 1.55 updwtmp (WTMP_FILE, &ut);
326 root 1.1 #endif
327 sf-exg 1.54
328 sf-exg 1.31 #ifdef LASTLOG_SUPPORT
329 sf-exg 1.54 if (login)
330 sf-exg 1.60 {
331     if (pwent)
332     update_lastlog (pty, hostname);
333     else
334 sf-exg 1.66 PTYTTY_WARN ("no entry in password file, not updating lastlog.\n");
335 sf-exg 1.60 }
336 root 1.1 #endif
337 sf-exg 1.71 }
338 root 1.1 }
339    
340 root 1.5 #endif
341