utmp_update.c revision d2117003c7d0588abeea5ed1b925b77f025e2c96
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER START
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The contents of this file are subject to the terms of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Common Development and Distribution License, Version 1.0 only
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * (the "License"). You may not use this file except in compliance
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * with the License.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * See the License for the specific language governing permissions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and limitations under the License.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * When distributing Covered Code, include this CDDL HEADER in each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If applicable, add the following below this CDDL HEADER, with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * fields enclosed by brackets "[]" replaced with your own identifying
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * information: Portions Copyright [yyyy] [name of copyright owner]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER END
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Use is subject to license terms.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#pragma ident "%Z%%M% %I% %E% SMI"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * utmp_update - Update the /var/adm/utmpx file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * As of on28, the utmp interface is obsolete,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * so we only handle updating the utmpx file now.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The utmpx routines in libc "simulate" calls
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * to manipulate utmp entries.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * This program runs set uid root on behalf of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * non-privileged user programs. Normal programs cannot
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * write to /var/adm/utmpx. Non-root callers of pututxline
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * will invoke this program to write the utmpx entry.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Header files
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Invocation argument definitions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Return codes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define MAX_SYSLEN 257 /* From utmpx.h host length + nul */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Other defines
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Debugging support
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic void display_args();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else /* DEBUG */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define dprintf(x, y)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define dprintf3(w, x, y, z)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Local functions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic void usage(void);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int bad_hostname(char *, int);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int hex2bin(unsigned char);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int invalid_utmpx(struct utmpx *, struct utmpx *);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int bad_line(char *);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic void check_id(char *, char *);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Uncomment the following for attaching with dbx(1) */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* while (debugger) ; */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* DEBUG */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * We will always be called by pututxline, so simply
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * verify the correct number of args
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * we should never be called by root the code in libc already
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * updates the file for root so no need to do it here. This
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * assumption simpilfies the rest of code since we nolonger
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * have to do special processing for the case when we are called
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Search for matching entry by line name before put operation
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * (scan over the whole file using getutxent(3C) to ensure
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * that the line name is the same. We can not use getutline(3C)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * because that will return LOGIN_PROCESS and USER_PROCESS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * records. Also check that the entry is for either a dead
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * process or a current process that is valid (see
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * invalid_utmpx() for details of validation criteria).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for (rutmpx = getutxent(); rutmpx != (struct utmpx *)NULL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhex2bin(unsigned char c)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (c - '0');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* NOTREACHED */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * load_utmpx_struct - Load up the utmpx structure with information supplied
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * as arguments in argv.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic void
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin char *user, *id, *line, *pid, *type, *term, *time_usec,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin unsigned char *cp;
usage()
int uid;
int hostlen;
char *user;
#ifdef VERIFY_PID
* Per utmpx.h, len includes the nul character.
for (i = 0; i < len; i++)
int i, len;
if (len > 0)
len--;
int fd;
#ifdef DEBUG
int argc;
char **argv;
while (argc--) {