1 /* $NetBSD: utmp_update.c,v 1.8 2008/04/28 20:23:04 martin Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: utmp_update.c,v 1.8 2008/04/28 20:23:04 martin Exp $");
35 #include <sys/types.h>
36 #include <sys/param.h>
50 int main(int, char *[]);
53 main(int argc
, char *argv
[])
66 if (seteuid(ruid
) == -1)
70 (void)fprintf(stderr
, "Usage: %s <vis-utmpx-entry>\n",
75 len
= strlen(argv
[1]);
77 if (len
> sizeof(*utx
) * 4 + 1 || len
< sizeof(*utx
))
78 errx(1, "Bad argument");
80 if ((utx
= malloc(len
)) == NULL
)
83 res
= strunvis((char *)utx
, argv
[1]);
84 if (res
!= (int)sizeof(*utx
))
85 errx(1, "Decoding error %s %d != %zu", argv
[1], res
, sizeof(*utx
));
87 switch (utx
->ut_type
) {
92 errx(1, "Invalid utmpx type %d", (int)utx
->ut_type
);
96 if ((pwd
= getpwuid(ruid
)) == NULL
)
97 errx(1, "User %lu does not exist in password database",
100 if (strcmp(pwd
->pw_name
, utx
->ut_name
) != 0)
101 errx(1, "Current user `%s' does not match "
102 "`%s' in utmpx entry", pwd
->pw_name
, utx
->ut_name
);
105 (void)snprintf(tty
, sizeof(tty
), "%s%s", _PATH_DEV
, utx
->ut_line
);
106 fd
= open(tty
, O_RDONLY
|O_NONBLOCK
, 0);
108 if (fstat(fd
, &st
) == -1)
109 err(1, "Cannot stat `%s'", tty
);
110 if (ruid
!= 0 && st
.st_uid
!= ruid
)
111 errx(1, "%s: Is not owned by you", tty
);
113 errx(1, "%s: Not a tty device", tty
);
115 if (access(tty
, W_OK
|R_OK
) == -1)
118 struct utmpx utold
, *utoldp
;
120 * A daemon like ftpd that does not use a tty line?
121 * We only allow it to kill its own existing entries
123 if (utx
->ut_type
!= DEAD_PROCESS
)
124 err(1, "Cannot open `%s'", tty
);
126 (void)memcpy(utold
.ut_line
, utx
->ut_line
, sizeof(utx
->ut_line
));
127 if ((utoldp
= getutxline(&utold
)) == NULL
)
128 err(1, "Cannot find existing entry for `%s'",
130 if (utoldp
->ut_pid
!= getppid())
131 err(1, "Cannot modify entry for `%s'", tty
);
135 if (pututxline(utx
) == NULL
)
136 err(1, "Cannot update utmp entry");