ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.68
Committed: Mon Aug 25 13:33:46 2014 UTC (12 years, 1 month ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.67: +1 -1 lines
Log Message:
Revert last change.

File Contents

# Content
1 /*----------------------------------------------------------------------*
2 * File: logging.C
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1992 John Bovey <jdb@ukc.ac.uk>
7 * - original version
8 * Copyright (c) 1993 lipka
9 * Copyright (c) 1993 Brian Stempien <stempien@cs.wmich.edu>
10 * Copyright (c) 1995 Raul Garcia Garcia <rgg@tid.es>
11 * Copyright (c) 1995 Piet W. Plomp <piet@idefix.icce.rug.nl>
12 * Copyright (c) 1997 Raul Garcia Garcia <rgg@tid.es>
13 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
14 * - extensive modifications
15 * Copyright (c) 1999 D J Hawkey Jr <hawkeyd@visi.com>
16 * - lastlog support
17 * Copyright (c) 2004-2006 Marc Lehmann <schmorp@schmorp.de>
18 * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it>
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 *----------------------------------------------------------------------*/
34
35 #include "config.h"
36
37 #include "ptytty.h"
38
39 #if UTMP_SUPPORT
40
41 #ifdef HAVE_UTMPX_H
42 # include <utmpx.h>
43 #endif
44 #ifdef HAVE_UTMP_H
45 # include <utmp.h>
46 #endif
47 #ifdef HAVE_LASTLOG_H
48 # include <lastlog.h>
49 #endif
50
51 #if !defined(UTMP_FILE)
52 # if defined(_PATH_UTMP)
53 # define UTMP_FILE _PATH_UTMP
54 # elif defined(PT_UTMP_FILE)
55 # define UTMP_FILE PT_UTMP_FILE
56 # endif
57 #endif
58 #if !defined(WTMP_FILE)
59 # if defined(_PATH_WTMP)
60 # define WTMP_FILE _PATH_WTMP
61 # elif defined(PT_WTMP_FILE)
62 # define WTMP_FILE PT_WTMP_FILE
63 # endif
64 #endif
65 #if !defined(WTMPX_FILE)
66 # if defined(_PATH_WTMPX)
67 # define WTMPX_FILE _PATH_WTMPX
68 # elif defined(PT_WTMPX_FILE)
69 # define WTMPX_FILE PT_WTMPX_FILE
70 # endif
71 #endif
72 #if !defined(LASTLOG_FILE)
73 # if defined(_PATH_LASTLOG)
74 # define LASTLOG_FILE _PATH_LASTLOG
75 # elif defined(PT_LASTLOG_FILE)
76 # define LASTLOG_FILE PT_LASTLOG_FILE
77 # endif
78 #endif
79 #if !defined(LASTLOGX_FILE)
80 # if defined(_PATH_LASTLOGX)
81 # define LASTLOGX_FILE _PATH_LASTLOGX
82 # elif defined(PT_LASTLOGX_FILE)
83 # define LASTLOGX_FILE PT_LASTLOGX_FILE
84 # endif
85 #endif
86
87 #include <pwd.h>
88
89 #include <stdio.h>
90 #include <string.h>
91
92 #include <sys/types.h>
93 #include <sys/stat.h>
94 #include <fcntl.h>
95 #include <unistd.h>
96 #include <time.h>
97 #include <errno.h>
98
99 /*
100 * BSD style utmp entry
101 * ut_line, ut_name, ut_host, ut_time
102 * SYSV style utmp (and utmpx) entry
103 * ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time
104 */
105
106 /* ------------------------------------------------------------------------- */
107 /*
108 * Write a BSD style utmp entry
109 */
110 #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
111 static void
112 write_bsd_utmp (int utmp_pos, struct utmp *ut)
113 {
114 int fd;
115
116 if (utmp_pos <= 0 || (fd = open (UTMP_FILE, O_WRONLY)) == -1)
117 return;
118
119 if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1)
120 write (fd, ut, sizeof (struct utmp));
121 close (fd);
122 }
123 #endif
124
125 /* ------------------------------------------------------------------------- */
126 /*
127 * Update a BSD style wtmp entry
128 */
129 #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP)
130 static void
131 update_wtmp (const char *fname, const struct utmp *ut)
132 {
133 int fd, gotlock, retry;
134 struct flock lck; /* fcntl locking scheme */
135 struct stat sbuf;
136
137 if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
138 return;
139
140 lck.l_whence = SEEK_END; /* start lock at current eof */
141 lck.l_len = 0; /* end at ``largest possible eof'' */
142 lck.l_start = 0;
143 lck.l_type = F_WRLCK; /* we want a write lock */
144
145 /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
146 for (retry = 10, gotlock = 0; retry--;)
147 if (fcntl (fd, F_SETLK, &lck) != -1)
148 {
149 gotlock = 1;
150 break;
151 }
152 else if (errno != EAGAIN && errno != EACCES)
153 break;
154
155 if (gotlock)
156 {
157 if (fstat (fd, &sbuf) == 0)
158 if (write (fd, ut, sizeof (struct utmp)) != sizeof (struct utmp))
159 ftruncate (fd, sbuf.st_size); /* remove bad writes */
160
161 lck.l_type = F_UNLCK; /* unlocking the file */
162 fcntl (fd, F_SETLK, &lck);
163 }
164
165 close (fd);
166 }
167 #endif
168
169 /* ------------------------------------------------------------------------- */
170 #ifdef LASTLOG_SUPPORT
171 static void
172 update_lastlog (const char *pty, const char *host)
173 {
174 # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
175 struct lastlogx llx;
176 # endif
177 # ifdef HAVE_STRUCT_LASTLOG
178 int fd;
179 struct lastlog ll;
180 # endif
181
182 # if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
183 memset (&llx, 0, sizeof (llx));
184 llx.ll_tv.tv_sec = time (NULL);
185 llx.ll_tv.tv_usec = 0;
186 strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
187 strncpy (llx.ll_host, host, sizeof (llx.ll_host));
188 updlastlogx (LASTLOGX_FILE, getuid (), &llx);
189 # endif
190
191 # ifdef HAVE_STRUCT_LASTLOG
192 memset (&ll, 0, sizeof (ll));
193 ll.ll_time = time (NULL);
194 strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
195 strncpy (ll.ll_host, host, sizeof (ll.ll_host));
196 if ((fd = open (LASTLOG_FILE, O_RDWR)) != -1)
197 {
198 if (lseek (fd, (off_t) (getuid () * sizeof (ll)),
199 SEEK_SET) != -1)
200 write (fd, &ll, sizeof (ll));
201 close (fd);
202 }
203 # endif /* HAVE_STRUCT_LASTLOG */
204 }
205 #endif /* LASTLOG_SUPPORT */
206
207 #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
208 static void
209 fill_id (char *id, const char *line, size_t id_size)
210 {
211 size_t len = strlen (line);
212
213 if (len > id_size)
214 line += len - id_size;
215 strncpy (id, line, id_size);
216 }
217 #endif
218
219 #ifdef HAVE_STRUCT_UTMP
220 static void
221 fill_utmp (struct utmp *ut, bool login, int pid, const char *line, const char *user, const char *host)
222 {
223 memset (ut, 0, sizeof (struct utmp));
224
225 strncpy (ut->ut_line, line, sizeof (ut->ut_line));
226 # ifdef HAVE_UTMP_PID
227 fill_id (ut->ut_id, line, sizeof (ut->ut_id));
228 ut->ut_pid = pid;
229 ut->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
230 # endif
231 ut->ut_time = time (NULL);
232
233 if (login)
234 {
235 # ifdef HAVE_UTMP_PID
236 strncpy (ut->ut_user, user, sizeof (ut->ut_user));
237 # else
238 strncpy (ut->ut_name, user, sizeof (ut->ut_name));
239 # endif
240 # ifdef HAVE_UTMP_HOST
241 strncpy (ut->ut_host, host, sizeof (ut->ut_host));
242 # endif
243 }
244 }
245 #endif
246
247 #ifdef HAVE_STRUCT_UTMPX
248 static void
249 fill_utmpx (struct utmpx *utx, bool login, int pid, const char *line, const char *user, const char *host)
250 {
251 memset (utx, 0, sizeof (struct utmpx));
252
253 // posix says that ut_line is not meaningful for DEAD_PROCESS
254 // records, but most implementations of last use ut_line to
255 // associate records in wtmp file
256 strncpy (utx->ut_line, line, sizeof (utx->ut_line));
257 fill_id (utx->ut_id, line, sizeof (utx->ut_id));
258 utx->ut_pid = pid;
259 utx->ut_type = login ? USER_PROCESS : DEAD_PROCESS;
260 utx->ut_tv.tv_sec = time (NULL);
261 utx->ut_tv.tv_usec = 0;
262
263 // posix says that ut_user is not meaningful for DEAD_PROCESS
264 // records, but solaris utmp_update helper requires that the ut_user
265 // field of a DEAD_PROCESS entry matches the one of an existing
266 // USER_PROCESS entry for the same line, if any
267 strncpy (utx->ut_user, user, sizeof (utx->ut_user));
268
269 if (login)
270 {
271 # ifdef HAVE_UTMPX_HOST
272 strncpy (utx->ut_host, host, sizeof (utx->ut_host));
273 # endif
274 }
275 }
276 #endif
277
278 /* ------------------------------------------------------------------------- */
279
280 /*
281 * make and write utmp and wtmp entries
282 */
283 void
284 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
285 {
286 if (!name || !*name)
287 return;
288
289 this->cmd_pid = cmd_pid;
290 this->login_shell = login_shell;
291
292 log_session (true, hostname);
293 }
294
295 void
296 ptytty_unix::log_session (bool login, const char *hostname)
297 {
298 struct passwd *pwent = getpwuid (getuid ());
299 const char *user = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
300
301 const char *pty = name;
302
303 if (!strncmp (pty, "/dev/", 5))
304 pty += 5; /* skip /dev/ prefix */
305
306 #ifdef HAVE_STRUCT_UTMP
307 struct utmp *tmput;
308 struct utmp ut;
309 fill_utmp (&ut, login, cmd_pid, pty, user, hostname);
310 #endif
311
312 #ifdef HAVE_STRUCT_UTMPX
313 struct utmpx *tmputx;
314 struct utmpx utx;
315 fill_utmpx (&utx, login, cmd_pid, pty, user, hostname);
316 #endif
317
318 #ifdef HAVE_STRUCT_UTMP
319 # ifdef HAVE_UTMP_PID
320 setutent ();
321 if (login || ((tmput = getutid (&ut)) && tmput->ut_pid == cmd_pid))
322 pututline (&ut);
323 endutent ();
324 # else
325 write_bsd_utmp (utmp_pos, &ut);
326 # endif
327 #endif
328
329 #ifdef HAVE_STRUCT_UTMPX
330 setutxent ();
331 if (login || ((tmputx = getutxid (&utx)) && tmputx->ut_pid == cmd_pid))
332 pututxline (&utx);
333 endutxent ();
334 #endif
335
336 #ifdef WTMP_SUPPORT
337 if (login_shell)
338 {
339 # ifdef HAVE_STRUCT_UTMP
340 # ifdef HAVE_UPDWTMP
341 updwtmp (WTMP_FILE, &ut);
342 # else
343 update_wtmp (WTMP_FILE, &ut);
344 # endif
345 # endif
346 # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
347 updwtmpx (WTMPX_FILE, &utx);
348 # endif
349 }
350 #endif
351
352 #ifdef LASTLOG_SUPPORT
353 if (login_shell)
354 if (login)
355 {
356 if (pwent)
357 update_lastlog (pty, hostname);
358 else
359 PTYTTY_WARN ("no entry in password file, not updating lastlog.\n");
360 }
361 #endif
362 }
363
364 /* ------------------------------------------------------------------------- */
365 /*
366 * remove utmp and wtmp entries
367 */
368 void
369 ptytty_unix::logout ()
370 {
371 if (!cmd_pid)
372 return;
373
374 log_session (false, 0);
375
376 cmd_pid = 0;
377 }
378
379 #else
380 void
381 ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
382 {
383 }
384 #endif
385