ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/logging.C
Revision: 1.69
Committed: Fri Oct 31 07:55:37 2014 UTC (11 years, 10 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
CVS Tags: rxvt-unicode-rel-9_26, rxvt-unicode-rel-9_25, rxvt-unicode-rel-9_22, rxvt-unicode-rel-9_21, rel-1_8, rel-1_7
Changes since 1.68: +2 -1 lines
Log Message:
Fix compilation on fbsd/obsd.

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