2 * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
30 #endif /* STDC_HEADERS */
32 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
36 #endif /* HAVE_STRING_H */
39 #endif /* HAVE_STRINGS_H */
42 #endif /* HAVE_UNISTD_H */
43 #if TIME_WITH_SYS_TIME
50 #endif /* HAVE_UTMPX_H */
58 #include "sudo_exec.h"
61 * Simplify handling of utmp vs. utmpx
63 #if !defined(HAVE_GETUTXID) && defined(HAVE_GETUTID)
64 # define getutxline(u) getutline(u)
65 # define pututxline(u) pututline(u)
66 # define setutxent() setutent()
67 # define endutxent() endutent()
68 #endif /* !HAVE_GETUTXID && HAVE_GETUTID */
71 typedef struct utmpx sudo_utmp_t;
73 typedef struct utmp sudo_utmp_t;
74 /* Older systems have ut_name, not us_user */
75 # if !defined(HAVE_STRUCT_UTMP_UT_USER) && !defined(ut_user)
76 # define ut_user ut_name
80 /* HP-UX has __e_termination and __e_exit, others lack the __ */
81 #if defined(HAVE_STRUCT_UTMPX_UT_EXIT_E_TERMINATION) || defined(HAVE_STRUCT_UTMP_UT_EXIT_E_TERMINATION)
82 # undef __e_termination
83 # define __e_termination e_termination
85 # define __e_exit e_exit
88 #if defined(HAVE_GETUTXID) || defined(HAVE_GETUTID)
90 * Create ut_id from the new ut_line and the old ut_id.
93 utmp_setid(sudo_utmp_t *old, sudo_utmp_t *new)
95 const char *line = new->ut_line;
97 debug_decl(utmp_setid, SUDO_DEBUG_UTMP)
99 /* Skip over "tty" in the id if old entry did too. */
101 if (strncmp(line, "tty", 3) == 0) {
102 idlen = MIN(sizeof(old->ut_id), 3);
103 if (strncmp(old->ut_id, "tty", idlen) != 0)
108 /* Store as much as will fit, skipping parts of the beginning as needed. */
109 idlen = strlen(line);
110 if (idlen > sizeof(new->ut_id)) {
111 line += idlen - sizeof(new->ut_id);
112 idlen = sizeof(new->ut_id);
114 strncpy(new->ut_id, line, idlen);
118 #endif /* HAVE_GETUTXID || HAVE_GETUTID */
121 * Store time in utmp structure.
124 utmp_settime(sudo_utmp_t *ut)
127 debug_decl(utmp_settime, SUDO_DEBUG_UTMP)
129 gettimeofday(&tv, NULL);
131 #if defined(HAVE_STRUCT_UTMP_UT_TV) || defined(HAVE_STRUCT_UTMPX_UT_TV)
132 ut->ut_tv.tv_sec = tv.tv_sec;
133 ut->ut_tv.tv_usec = tv.tv_usec;
135 ut->ut_time = tv.tv_sec;
142 * Fill in a utmp entry, using an old entry as a template if there is one.
145 utmp_fill(const char *line, const char *user, sudo_utmp_t *ut_old,
148 debug_decl(utmp_file, SUDO_DEBUG_UTMP)
150 if (ut_old == NULL) {
151 memset(ut_new, 0, sizeof(*ut_new));
153 strncpy(ut_new->ut_user, user_details.username,
154 sizeof(ut_new->ut_user));
156 } else if (ut_old != ut_new) {
157 memcpy(ut_new, ut_old, sizeof(*ut_new));
160 strncpy(ut_new->ut_user, user, sizeof(ut_new->ut_user));
161 strncpy(ut_new->ut_line, line, sizeof(ut_new->ut_line));
162 #if defined(HAVE_STRUCT_UTMPX_UT_ID) || defined(HAVE_STRUCT_UTMP_UT_ID)
163 utmp_setid(ut_old, ut_new);
165 #if defined(HAVE_STRUCT_UTMPX_UT_PID) || defined(HAVE_STRUCT_UTMP_UT_PID)
166 ut_new->ut_pid = getpid();
168 utmp_settime(ut_new);
169 #if defined(HAVE_STRUCT_UTMPX_UT_TYPE) || defined(HAVE_STRUCT_UTMP_UT_TYPE)
170 ut_new->ut_type = USER_PROCESS;
176 * There are two basic utmp file types:
178 * POSIX: sequential access with new entries appended to the end.
179 * Manipulated via {get,put}utent()/{get,put}getutxent().
181 * Legacy: sparse file indexed by ttyslot() * sizeof(struct utmp)
183 #if defined(HAVE_GETUTXID) || defined(HAVE_GETUTID)
185 utmp_login(const char *from_line, const char *to_line, int ttyfd,
188 sudo_utmp_t utbuf, *ut_old = NULL;
190 debug_decl(utmp_login, SUDO_DEBUG_UTMP)
192 /* Strip off /dev/ prefix from line as needed. */
193 if (strncmp(to_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
194 to_line += sizeof(_PATH_DEV) - 1;
196 if (from_line != NULL) {
197 if (strncmp(from_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
198 from_line += sizeof(_PATH_DEV) - 1;
200 /* Lookup old line. */
201 memset(&utbuf, 0, sizeof(utbuf));
202 strncpy(utbuf.ut_line, from_line, sizeof(utbuf.ut_line));
203 ut_old = getutxline(&utbuf);
205 utmp_fill(to_line, user, ut_old, &utbuf);
206 if (pututxline(&utbuf) != NULL)
210 debug_return_bool(rval);
214 utmp_logout(const char *line, int status)
217 sudo_utmp_t *ut, utbuf;
218 debug_decl(utmp_logout, SUDO_DEBUG_UTMP)
220 /* Strip off /dev/ prefix from line as needed. */
221 if (strncmp(line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
222 line += sizeof(_PATH_DEV) - 1;
224 memset(&utbuf, 0, sizeof(utbuf));
225 strncpy(utbuf.ut_line, line, sizeof(utbuf.ut_line));
226 if ((ut = getutxline(&utbuf)) != NULL) {
227 memset(ut->ut_user, 0, sizeof(ut->ut_user));
228 # if defined(HAVE_STRUCT_UTMPX_UT_TYPE) || defined(HAVE_STRUCT_UTMP_UT_TYPE)
229 ut->ut_type = DEAD_PROCESS;
231 # if defined(HAVE_STRUCT_UTMPX_UT_EXIT) || defined(HAVE_STRUCT_UTMP_UT_EXIT)
232 ut->ut_exit.__e_exit = WEXITSTATUS(status);
233 ut->ut_exit.__e_termination = WIFEXITED(status) ? WEXITSTATUS(status) : 0;
236 if (pututxline(ut) != NULL)
239 debug_return_bool(rval);
242 #else /* !HAVE_GETUTXID && !HAVE_GETUTID */
245 * Find the slot for the specified line (tty name and file descriptor).
246 * Returns a slot suitable for seeking into utmp on success or <= 0 on error.
247 * If getttyent() is available we can use that to compute the slot.
249 # ifdef HAVE_GETTTYENT
251 utmp_slot(const char *line, int ttyfd)
255 debug_decl(utmp_slot, SUDO_DEBUG_UTMP)
258 while ((tty = getttyent()) != NULL) {
259 if (strcmp(line, tty->ty_name) == 0)
264 debug_return_int(tty ? slot : 0);
268 utmp_slot(const char *line, int ttyfd)
271 debug_decl(utmp_slot, SUDO_DEBUG_UTMP)
274 * Temporarily point stdin to the tty since ttyslot()
275 * doesn't take an argument.
277 if ((sfd = dup(STDIN_FILENO)) == -1)
278 fatal(_("unable to save stdin"));
279 if (dup2(ttyfd, STDIN_FILENO) == -1)
280 fatal(_("unable to dup2 stdin"));
282 if (dup2(sfd, STDIN_FILENO) == -1)
283 fatal(_("unable to restore stdin"));
286 debug_return_int(slot);
288 # endif /* HAVE_GETTTYENT */
291 utmp_login(const char *from_line, const char *to_line, int ttyfd,
294 sudo_utmp_t utbuf, *ut_old = NULL;
298 debug_decl(utmp_login, SUDO_DEBUG_UTMP)
300 /* Strip off /dev/ prefix from line as needed. */
301 if (strncmp(to_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
302 to_line += sizeof(_PATH_DEV) - 1;
304 /* Find slot for new entry. */
305 slot = utmp_slot(to_line, ttyfd);
309 if ((fp = fopen(_PATH_UTMP, "r+")) == NULL)
312 if (from_line != NULL) {
313 if (strncmp(from_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
314 from_line += sizeof(_PATH_DEV) - 1;
316 /* Lookup old line. */
317 while (fread(&utbuf, sizeof(utbuf), 1, fp) == 1) {
318 # ifdef HAVE_STRUCT_UTMP_UT_ID
319 if (utbuf.ut_type != LOGIN_PROCESS && utbuf.ut_type != USER_PROCESS)
322 if (utbuf.ut_user[0] &&
323 !strncmp(utbuf.ut_line, from_line, sizeof(utbuf.ut_line))) {
329 utmp_fill(to_line, user, ut_old, &utbuf);
331 if (fseeko(fp, slot * (off_t)sizeof(utbuf), SEEK_SET) == 0) {
333 if (fseek(fp, slot * (long)sizeof(utbuf), SEEK_SET) == 0) {
335 if (fwrite(&utbuf, sizeof(utbuf), 1, fp) == 1)
341 debug_return_bool(rval);
345 utmp_logout(const char *line, int status)
350 debug_decl(utmp_logout, SUDO_DEBUG_UTMP)
352 if ((fp = fopen(_PATH_UTMP, "r+")) == NULL)
353 debug_return_int(rval);
355 /* Strip off /dev/ prefix from line as needed. */
356 if (strncmp(line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
357 line += sizeof(_PATH_DEV) - 1;
359 while (fread(&utbuf, sizeof(utbuf), 1, fp) == 1) {
360 if (!strncmp(utbuf.ut_line, line, sizeof(utbuf.ut_line))) {
361 memset(utbuf.ut_user, 0, sizeof(utbuf.ut_user));
362 # if defined(HAVE_STRUCT_UTMP_UT_TYPE)
363 utbuf.ut_type = DEAD_PROCESS;
365 utmp_settime(&utbuf);
366 /* Back up and overwrite record. */
368 if (fseeko(fp, (off_t)0 - (off_t)sizeof(utbuf), SEEK_CUR) == 0) {
370 if (fseek(fp, 0L - (long)sizeof(utbuf), SEEK_CUR) == 0) {
372 if (fwrite(&utbuf, sizeof(utbuf), 1, fp) == 1)
380 debug_return_bool(rval);
382 #endif /* HAVE_GETUTXID || HAVE_GETUTID */