ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.53
Committed: Thu Nov 17 11:59:54 2011 UTC (14 years, 10 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.52: +3 -5 lines
Log Message:
Simplify.

Avoiding an open when the utmp file cannot be opened is not worth the
extra conditional.

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