ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.57
Committed: Fri Nov 25 16:31:38 2011 UTC (14 years, 10 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.56: +8 -9 lines
Log Message:
Simplify.

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