ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.56
Committed: Thu Nov 24 13:43:05 2011 UTC (14 years, 10 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.55: +12 -0 lines
Log Message:
Move some includes from ptytty.h to logging.C.

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