ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.70
Committed: Mon May 24 13:54:45 2021 UTC (5 years, 4 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.69: +11 -25 lines
Log Message:
Factor code to write utmp and lastlog records

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 Emanuele Giaquinta <e.giaquinta@glauco.it>
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 <sys/types.h>
40
41 #if UTMP_SUPPORT
42
43 #ifdef HAVE_UTMPX_H
44 # include <utmpx.h>
45 #endif
46 #ifdef HAVE_UTMP_H
47 # include <utmp.h>
48 #endif
49 #ifdef HAVE_LASTLOG_H
50 # include <lastlog.h>
51 #endif
52
53 #if !defined(UTMP_FILE)
54 # if defined(_PATH_UTMP)
55 # define UTMP_FILE _PATH_UTMP
56 # elif defined(PT_UTMP_FILE)
57 # define UTMP_FILE PT_UTMP_FILE
58 # endif
59 #endif
60 #if !defined(WTMP_FILE)
61 # if defined(_PATH_WTMP)
62 # define WTMP_FILE _PATH_WTMP
63 # elif defined(PT_WTMP_FILE)
64 # define WTMP_FILE PT_WTMP_FILE
65 # endif
66 #endif
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 #if !defined(LASTLOG_FILE)
75 # if defined(_PATH_LASTLOG)
76 # define LASTLOG_FILE _PATH_LASTLOG
77 # elif defined(PT_LASTLOG_FILE)
78 # define LASTLOG_FILE PT_LASTLOG_FILE
79 # endif
80 #endif
81 #if !defined(LASTLOGX_FILE)
82 # if defined(_PATH_LASTLOGX)
83 # define LASTLOGX_FILE _PATH_LASTLOGX
84 # elif defined(PT_LASTLOGX_FILE)
85 # define LASTLOGX_FILE PT_LASTLOGX_FILE
86 # endif
87 #endif
88
89 #include <pwd.h>
90
91 #include <stdio.h>
92 #include <string.h>
93
94 #include <sys/stat.h>
95 #include <fcntl.h>
96 #include <unistd.h>
97 #include <time.h>
98 #include <errno.h>
99
100 /*
101 * BSD style utmp entry
102 * ut_line, ut_name, ut_host, ut_time
103 * SYSV style utmp (and utmpx) entry
104 * ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time
105 */
106
107 static void
108 write_record (const char *path, off_t pos, const void *record, size_t size)
109 {
110 int fd = open (path, O_WRONLY);
111 if (fd >= 0)
112 {
113 pwrite (fd, record, size, pos * size);
114 close (fd);
115 }
116 }
117
118 /* ------------------------------------------------------------------------- */
119 /*
120 * Update a BSD style wtmp entry
121 */
122 #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP)
123 static void
124 update_wtmp (const char *fname, const struct utmp *ut)
125 {
126 int fd, gotlock, retry;
127 struct flock lck; /* fcntl locking scheme */
128 struct stat sbuf;
129
130 if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
131 return;
132
133 lck.l_whence = SEEK_END; /* start lock at current eof */
134 lck.l_len = 0; /* end at ``largest possible eof'' */
135 lck.l_start = 0;
136 lck.l_type = F_WRLCK; /* we want a write lock */
137
138 /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
139 for (retry = 10, gotlock = 0; retry--;)
140 if (fcntl (fd, F_SETLK, &lck) != -1)
141 {
142 gotlock = 1;
143 break;
144 }
145 else if (errno != EAGAIN && errno != EACCES)
146 break;
147
148 if (gotlock)
149 {
150 if (fstat (fd, &sbuf) == 0)
151 if (write (fd, ut, sizeof (struct utmp)) != sizeof (struct utmp))
152 ftruncate (fd, sbuf.st_size); /* remove bad writes */
153
154 lck.l_type = F_UNLCK; /* unlocking the file */
155 fcntl (fd, F_SETLK, &lck);
156 }
157
158 close (fd);
159 }
160 #endif
161
162 /* ------------------------------------------------------------------------- */
163 #ifdef LASTLOG_SUPPORT
164 static void
165 update_lastlog (const char *pty, const char *host)
166 {
167 # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
168 struct lastlogx llx;
169 # endif
170 # ifdef HAVE_STRUCT_LASTLOG
171 struct lastlog ll;
172 # endif
173
174 # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
175 memset (&llx, 0, sizeof (llx));
176 llx.ll_tv.tv_sec = time (NULL);
177 llx.ll_tv.tv_usec = 0;
178 strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
179 strncpy (llx.ll_host, host, sizeof (llx.ll_host));
180 updlastlogx (LASTLOGX_FILE, getuid (), &llx);
181 # endif
182
183 # ifdef HAVE_STRUCT_LASTLOG
184 memset (&ll, 0, sizeof (ll));
185 ll.ll_time = time (NULL);
186 strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
187 strncpy (ll.ll_host, host, sizeof (ll.ll_host));
188 write_record (LASTLOG_FILE, getuid (), &ll, sizeof (ll));
189 # endif /* HAVE_STRUCT_LASTLOG */
190 }
191 #endif /* LASTLOG_SUPPORT */
192
193 #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
194 static void
195 fill_id (char *id, const char *line, size_t id_size)
196 {
197 size_t len = strlen (line);
198
199 if (len > id_size)
200 line += len - id_size;
201 strncpy (id, line, id_size);
202 }
203 #endif
204
205 #ifdef HAVE_STRUCT_UTMP
206 static void
207 fill_utmp (struct utmp *ut, bool login, int pid, const char *line, const char *user, const char *host)
208 {
209 memset (ut, 0, sizeof (struct utmp));
210
211 strncpy (ut->ut_line, line, sizeof (ut->ut_line));
212 # ifdef HAVE_UTMP_PID
213 fill_id (ut->ut_id, line, sizeof (ut->ut_id));
214 ut->ut_pid = pid;
215 ut->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
216 # endif
217 ut->ut_time = time (NULL);
218
219 if (login)
220 {
221 # ifdef HAVE_UTMP_PID
222 strncpy (ut->ut_user, user, sizeof (ut->ut_user));
223 # else
224 strncpy (ut->ut_name, user, sizeof (ut->ut_name));
225 # endif
226 # ifdef HAVE_UTMP_HOST
227 strncpy (ut->ut_host, host, sizeof (ut->ut_host));
228 # endif
229 }
230 }
231 #endif
232
233 #ifdef HAVE_STRUCT_UTMPX
234 static void
235 fill_utmpx (struct utmpx *utx, bool login, int pid, const char *line, const char *user, const char *host)
236 {
237 memset (utx, 0, sizeof (struct utmpx));
238
239 // posix says that ut_line is not meaningful for DEAD_PROCESS
240 // records, but most implementations of last use ut_line to
241 // associate records in wtmp file
242 strncpy (utx->ut_line, line, sizeof (utx->ut_line));
243 fill_id (utx->ut_id, line, sizeof (utx->ut_id));
244 utx->ut_pid = pid;
245 utx->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
246 utx->ut_tv.tv_sec = time (NULL);
247 utx->ut_tv.tv_usec = 0;
248
249 // posix says that ut_user is not meaningful for DEAD_PROCESS
250 // records, but solaris utmp_update helper requires that the ut_user
251 // field of a DEAD_PROCESS entry matches the one of an existing
252 // USER_PROCESS entry for the same line, if any
253 strncpy (utx->ut_user, user, sizeof (utx->ut_user));
254
255 if (login)
256 {
257 # ifdef HAVE_UTMPX_HOST
258 strncpy (utx->ut_host, host, sizeof (utx->ut_host));
259 # endif
260 }
261 }
262 #endif
263
264 /* ------------------------------------------------------------------------- */
265
266 /*
267 * make and write utmp and wtmp entries
268 */
269 void
270 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
271 {
272 if (!name || !*name)
273 return;
274
275 this->cmd_pid = cmd_pid;
276 this->login_shell = login_shell;
277
278 log_session (true, hostname);
279 }
280
281 void
282 ptytty_unix::log_session (bool login, const char *hostname)
283 {
284 struct passwd *pwent = getpwuid (getuid ());
285 const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
286
287 const char *pty = name;
288
289 if (!strncmp (pty, "/dev/", 5))
290 pty += 5; /* skip /dev/ prefix */
291
292 #ifdef HAVE_STRUCT_UTMP
293 struct utmp *tmput;
294 struct utmp ut;
295 fill_utmp (&ut, login, cmd_pid, pty, user, hostname);
296 #endif
297
298 #ifdef HAVE_STRUCT_UTMPX
299 struct utmpx *tmputx;
300 struct utmpx utx;
301 fill_utmpx (&utx, login, cmd_pid, pty, user, hostname);
302 #endif
303
304 #ifdef HAVE_STRUCT_UTMP
305 # ifdef HAVE_UTMP_PID
306 setutent ();
307 if (login || ((tmput = getutid (&ut)) && tmput->ut_pid == cmd_pid))
308 pututline (&ut);
309 endutent ();
310 # else
311 if (utmp_pos > 0)
312 write_record (UTMP_FILE, utmp_pos, &ut, sizeof (ut));
313 # endif
314 #endif
315
316 #ifdef HAVE_STRUCT_UTMPX
317 setutxent ();
318 if (login || ((tmputx = getutxid (&utx)) && tmputx->ut_pid == cmd_pid))
319 pututxline (&utx);
320 endutxent ();
321 #endif
322
323 #ifdef WTMP_SUPPORT
324 if (login_shell)
325 {
326 # ifdef HAVE_STRUCT_UTMP
327 # ifdef HAVE_UPDWTMP
328 updwtmp (WTMP_FILE, &ut);
329 # else
330 update_wtmp (WTMP_FILE, &ut);
331 # endif
332 # endif
333 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
334 updwtmpx (WTMPX_FILE, &utx);
335 # endif
336 }
337 #endif
338
339 #ifdef LASTLOG_SUPPORT
340 if (login_shell)
341 if (login)
342 {
343 if (pwent)
344 update_lastlog (pty, hostname);
345 else
346 PTYTTY_WARN ("no entry in password file, not updating lastlog.\n");
347 }
348 #endif
349 }
350
351 /* ------------------------------------------------------------------------- */
352 /*
353 * remove utmp and wtmp entries
354 */
355 void
356 ptytty_unix::logout ()
357 {
358 if (!cmd_pid)
359 return;
360
361 log_session (false, 0);
362
363 cmd_pid = 0;
364 }
365
366 #else
367 void
368 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
369 {
370 }
371 #endif
372