ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.75
Committed: Sat Jul 24 09:05:17 2021 UTC (5 years, 1 month ago) by sf-exg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.74: +1 -1 lines
Log Message:
Update my email address

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.75 * Copyright (c) 2006-2021 Emanuele Giaquinta <emanuele.giaquinta@gmail.com>
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 sf-exg 1.73 if (login)
153     {
154     if (pwent)
155     update_lastlog (pty, hostname);
156     }
157 sf-exg 1.71 #endif
158     }
159     }
160    
161     #elif defined(USE_UTMP)
162    
163 sf-exg 1.72 #include <time.h>
164 sf-exg 1.71 #include <utmp.h>
165 sf-exg 1.56 #ifdef HAVE_LASTLOG_H
166     # include <lastlog.h>
167     #endif
168    
169 sf-exg 1.58 #if !defined(UTMP_FILE)
170     # if defined(_PATH_UTMP)
171     # define UTMP_FILE _PATH_UTMP
172     # elif defined(PT_UTMP_FILE)
173     # define UTMP_FILE PT_UTMP_FILE
174     # endif
175     #endif
176 sf-exg 1.71
177 sf-exg 1.58 #if !defined(WTMP_FILE)
178     # if defined(_PATH_WTMP)
179     # define WTMP_FILE _PATH_WTMP
180     # elif defined(PT_WTMP_FILE)
181     # define WTMP_FILE PT_WTMP_FILE
182     # endif
183     #endif
184 sf-exg 1.71
185 sf-exg 1.58 #if !defined(LASTLOG_FILE)
186     # if defined(_PATH_LASTLOG)
187     # define LASTLOG_FILE _PATH_LASTLOG
188     # elif defined(PT_LASTLOG_FILE)
189     # define LASTLOG_FILE PT_LASTLOG_FILE
190     # endif
191     #endif
192 root 1.7
193     #include <sys/stat.h>
194 ayin 1.23 #include <fcntl.h>
195 ayin 1.24 #include <errno.h>
196 root 1.7
197 sf-exg 1.53 static void
198 sf-exg 1.70 write_record (const char *path, off_t pos, const void *record, size_t size)
199 ayin 1.12 {
200 sf-exg 1.70 int fd = open (path, O_WRONLY);
201     if (fd >= 0)
202     {
203     pwrite (fd, record, size, pos * size);
204     close (fd);
205     }
206 ayin 1.12 }
207    
208     /* ------------------------------------------------------------------------- */
209     /*
210     * Update a BSD style wtmp entry
211     */
212 sf-exg 1.71 #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP)
213 ayin 1.12 static void
214 sf-exg 1.71 updwtmp (const char *fname, const struct utmp *ut)
215 ayin 1.12 {
216 sf-exg 1.29 int fd, gotlock, retry;
217 ayin 1.12 struct flock lck; /* fcntl locking scheme */
218     struct stat sbuf;
219    
220     if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
221     return;
222    
223     lck.l_whence = SEEK_END; /* start lock at current eof */
224     lck.l_len = 0; /* end at ``largest possible eof'' */
225     lck.l_start = 0;
226     lck.l_type = F_WRLCK; /* we want a write lock */
227    
228     /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
229 sf-exg 1.29 for (retry = 10, gotlock = 0; retry--;)
230 ayin 1.12 if (fcntl (fd, F_SETLK, &lck) != -1)
231     {
232 sf-exg 1.29 gotlock = 1;
233 ayin 1.12 break;
234     }
235     else if (errno != EAGAIN && errno != EACCES)
236 sf-exg 1.29 break;
237 sf-exg 1.57
238     if (gotlock)
239 sf-exg 1.29 {
240 sf-exg 1.57 if (fstat (fd, &sbuf) == 0)
241     if (write (fd, ut, sizeof (struct utmp)) != sizeof (struct utmp))
242     ftruncate (fd, sbuf.st_size); /* remove bad writes */
243    
244     lck.l_type = F_UNLCK; /* unlocking the file */
245     fcntl (fd, F_SETLK, &lck);
246 sf-exg 1.29 }
247 ayin 1.12
248     close (fd);
249     }
250     #endif
251    
252     /* ------------------------------------------------------------------------- */
253     #ifdef LASTLOG_SUPPORT
254     static void
255 sf-exg 1.31 update_lastlog (const char *pty, const char *host)
256 ayin 1.12 {
257     # ifdef HAVE_STRUCT_LASTLOG
258 sf-exg 1.70 struct lastlog ll;
259 ayin 1.12
260     memset (&ll, 0, sizeof (ll));
261     ll.ll_time = time (NULL);
262     strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
263     strncpy (ll.ll_host, host, sizeof (ll.ll_host));
264 sf-exg 1.70 write_record (LASTLOG_FILE, getuid (), &ll, sizeof (ll));
265 ayin 1.17 # endif /* HAVE_STRUCT_LASTLOG */
266 ayin 1.12 }
267 ayin 1.17 #endif /* LASTLOG_SUPPORT */
268 ayin 1.12
269 sf-exg 1.32 static void
270 sf-exg 1.45 fill_utmp (struct utmp *ut, bool login, int pid, const char *line, const char *user, const char *host)
271 sf-exg 1.32 {
272     memset (ut, 0, sizeof (struct utmp));
273    
274 sf-exg 1.40 strncpy (ut->ut_line, line, sizeof (ut->ut_line));
275 sf-exg 1.71 #ifdef HAVE_UTMP_PID
276 sf-exg 1.45 fill_id (ut->ut_id, line, sizeof (ut->ut_id));
277 sf-exg 1.32 ut->ut_pid = pid;
278 sf-exg 1.35 ut->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
279 sf-exg 1.71 #endif
280 sf-exg 1.32 ut->ut_time = time (NULL);
281    
282     if (login)
283     {
284 sf-exg 1.71 #ifdef HAVE_UTMP_PID
285 sf-exg 1.32 strncpy (ut->ut_user, user, sizeof (ut->ut_user));
286 sf-exg 1.71 #else
287 sf-exg 1.32 strncpy (ut->ut_name, user, sizeof (ut->ut_name));
288 sf-exg 1.71 #endif
289     #ifdef HAVE_UTMP_HOST
290 sf-exg 1.32 strncpy (ut->ut_host, host, sizeof (ut->ut_host));
291     #endif
292     }
293     }
294 sf-exg 1.54
295     void
296     ptytty_unix::log_session (bool login, const char *hostname)
297     {
298 root 1.1 struct passwd *pwent = getpwuid (getuid ());
299 sf-exg 1.39 const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
300 root 1.1
301 sf-exg 1.54 const char *pty = name;
302    
303 root 1.1 if (!strncmp (pty, "/dev/", 5))
304     pty += 5; /* skip /dev/ prefix */
305    
306 sf-exg 1.54 struct utmp *tmput;
307 sf-exg 1.55 struct utmp ut;
308     fill_utmp (&ut, login, cmd_pid, pty, user, hostname);
309 root 1.1
310 sf-exg 1.71 #ifdef HAVE_UTMP_PID
311 sf-exg 1.37 setutent ();
312 sf-exg 1.55 if (login || ((tmput = getutid (&ut)) && tmput->ut_pid == cmd_pid))
313     pututline (&ut);
314 sf-exg 1.42 endutent ();
315 sf-exg 1.71 #else
316 sf-exg 1.70 if (utmp_pos > 0)
317     write_record (UTMP_FILE, utmp_pos, &ut, sizeof (ut));
318 root 1.1 #endif
319    
320 root 1.2 if (login_shell)
321 root 1.1 {
322 sf-exg 1.71 #ifdef WTMP_SUPPORT
323 sf-exg 1.55 updwtmp (WTMP_FILE, &ut);
324 root 1.1 #endif
325 sf-exg 1.54
326 sf-exg 1.31 #ifdef LASTLOG_SUPPORT
327 sf-exg 1.73 if (login)
328     {
329     if (pwent)
330     update_lastlog (pty, hostname);
331     }
332 root 1.1 #endif
333 sf-exg 1.71 }
334 root 1.1 }
335    
336 root 1.5 #endif
337