ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/src/logging.C
Revision: 1.55
Committed: Wed Nov 23 20:20:00 2011 UTC (14 years, 9 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.54: +12 -12 lines
Log Message:
Remove useless member variables.

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