ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.48
Committed: Thu Nov 10 15:48:52 2011 UTC (14 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.47: +5 -21 lines
Log Message:
remove support for writing lastlog when the lastlog file is a directory.

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