ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.58
Committed: Sat Nov 26 13:39:04 2011 UTC (14 years, 10 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.57: +36 -0 lines
Log Message:
Change PT_FIND_FILE to search only for fallback locations of
utmp/wtmp/lastlog files and define the *_FILE macros at compile time.

File Contents

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