ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/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
Error occurred while calculating annotation data.
Log Message:
Update my email address

File Contents

# Content
1 /*----------------------------------------------------------------------*
2 * 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 * Copyright (c) 2004-2006 Marc Lehmann <schmorp@schmorp.de>
18 * Copyright (c) 2006-2021 Emanuele Giaquinta <emanuele.giaquinta@gmail.com>
19 *
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 * the Free Software Foundation; either version 2 of the License, or
23 * (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 #include "config.h"
36
37 #include "ptytty.h"
38
39 #include <pwd.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #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 #include <sys/time.h>
65 #include <utmpx.h>
66
67 #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
75 #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 #endif
82
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 gettimeofday (&llx.ll_tv, 0);
92 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 #endif
98
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 gettimeofday (&utx->ut_tv, 0);
112
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 }
157 #endif
158 }
159 }
160
161 #elif defined(USE_UTMP)
162
163 #include <time.h>
164 #include <utmp.h>
165 #ifdef HAVE_LASTLOG_H
166 # include <lastlog.h>
167 #endif
168
169 #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
177 #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
185 #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
193 #include <sys/stat.h>
194 #include <fcntl.h>
195 #include <errno.h>
196
197 static void
198 write_record (const char *path, off_t pos, const void *record, size_t size)
199 {
200 int fd = open (path, O_WRONLY);
201 if (fd >= 0)
202 {
203 pwrite (fd, record, size, pos * size);
204 close (fd);
205 }
206 }
207
208 /* ------------------------------------------------------------------------- */
209 /*
210 * Update a BSD style wtmp entry
211 */
212 #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP)
213 static void
214 updwtmp (const char *fname, const struct utmp *ut)
215 {
216 int fd, gotlock, retry;
217 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 for (retry = 10, gotlock = 0; retry--;)
230 if (fcntl (fd, F_SETLK, &lck) != -1)
231 {
232 gotlock = 1;
233 break;
234 }
235 else if (errno != EAGAIN && errno != EACCES)
236 break;
237
238 if (gotlock)
239 {
240 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 }
247
248 close (fd);
249 }
250 #endif
251
252 /* ------------------------------------------------------------------------- */
253 #ifdef LASTLOG_SUPPORT
254 static void
255 update_lastlog (const char *pty, const char *host)
256 {
257 # ifdef HAVE_STRUCT_LASTLOG
258 struct lastlog ll;
259
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 write_record (LASTLOG_FILE, getuid (), &ll, sizeof (ll));
265 # endif /* HAVE_STRUCT_LASTLOG */
266 }
267 #endif /* LASTLOG_SUPPORT */
268
269 static void
270 fill_utmp (struct utmp *ut, bool login, int pid, const char *line, const char *user, const char *host)
271 {
272 memset (ut, 0, sizeof (struct utmp));
273
274 strncpy (ut->ut_line, line, sizeof (ut->ut_line));
275 #ifdef HAVE_UTMP_PID
276 fill_id (ut->ut_id, line, sizeof (ut->ut_id));
277 ut->ut_pid = pid;
278 ut->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
279 #endif
280 ut->ut_time = time (NULL);
281
282 if (login)
283 {
284 #ifdef HAVE_UTMP_PID
285 strncpy (ut->ut_user, user, sizeof (ut->ut_user));
286 #else
287 strncpy (ut->ut_name, user, sizeof (ut->ut_name));
288 #endif
289 #ifdef HAVE_UTMP_HOST
290 strncpy (ut->ut_host, host, sizeof (ut->ut_host));
291 #endif
292 }
293 }
294
295 void
296 ptytty_unix::log_session (bool login, const char *hostname)
297 {
298 struct passwd *pwent = getpwuid (getuid ());
299 const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
300
301 const char *pty = name;
302
303 if (!strncmp (pty, "/dev/", 5))
304 pty += 5; /* skip /dev/ prefix */
305
306 struct utmp *tmput;
307 struct utmp ut;
308 fill_utmp (&ut, login, cmd_pid, pty, user, hostname);
309
310 #ifdef HAVE_UTMP_PID
311 setutent ();
312 if (login || ((tmput = getutid (&ut)) && tmput->ut_pid == cmd_pid))
313 pututline (&ut);
314 endutent ();
315 #else
316 if (utmp_pos > 0)
317 write_record (UTMP_FILE, utmp_pos, &ut, sizeof (ut));
318 #endif
319
320 if (login_shell)
321 {
322 #ifdef WTMP_SUPPORT
323 updwtmp (WTMP_FILE, &ut);
324 #endif
325
326 #ifdef LASTLOG_SUPPORT
327 if (login)
328 {
329 if (pwent)
330 update_lastlog (pty, hostname);
331 }
332 #endif
333 }
334 }
335
336 #endif
337