ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/logging.C
Revision: 1.62
Committed: Thu Dec 22 10:32:22 2011 UTC (14 years, 8 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_5
Changes since 1.61: +0 -4 lines
Log Message:
Always honour login_shell argument of login rather than only when an
urxvt feature macro is defined.

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