]>
git.saurik.com Git - apple/system_cmds.git/blob - utmp_update.tproj/utmp_update.c
733c649293900f98ae750bff37ee37fc10290179
1 /* $NetBSD: utmp_update.c,v 1.6 2003/02/26 18:16:50 christos 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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: utmp_update.c,v 1.6 2003/02/26 18:16:50 christos Exp $");
42 #include <sys/types.h>
43 #include <sys/param.h>
57 int main(int, char *[]);
60 main(int argc
, char *argv
[])
72 if (seteuid(ruid
) == -1)
76 (void)fprintf(stderr
, "Usage: %s <vis-utmpx-entry>\n",
81 len
= strlen(argv
[1]);
83 // arg string can't be greater the 4x the size of struct utmpx
84 // nor less that the size of struct utmpx
85 if (len
> sizeof(*utx
) * 4 || len
< sizeof(*utx
))
86 errx(1, "Bad argument");
88 // need an extra byte because strunvis will null terminate
89 if ((utx
= malloc(len
+ 1)) == NULL
)
92 if (strunvis((char *)utx
, argv
[1]) != sizeof(*utx
))
93 errx(1, "Decoding error");
95 switch (utx
->ut_type
) {
100 errx(1, "Invalid utmpx type %d", (int)utx
->ut_type
);
104 if ((pwd
= getpwuid(ruid
)) == NULL
)
105 errx(1, "User %lu does not exist in password database",
108 if (strcmp(pwd
->pw_name
, utx
->ut_user
) != 0)
109 errx(1, "Current user `%s' does not match "
110 "`%s' in utmpx entry", pwd
->pw_name
, utx
->ut_user
);
113 (void)snprintf(tty
, sizeof(tty
), "%s%s", _PATH_DEV
, utx
->ut_line
);
114 fd
= open(tty
, O_RDONLY
, 0);
116 if (fstat(fd
, &st
) == -1)
117 err(1, "Cannot stat `%s'", tty
);
118 if (ruid
!= 0 && st
.st_uid
!= ruid
)
119 errx(1, "%s: Is not owned by you", tty
);
121 errx(1, "%s: Not a tty device", tty
);
123 if (access(tty
, W_OK
|R_OK
) == -1)
126 struct utmpx utold
, *utoldp
;
128 * A daemon like ftpd that does not use a tty line?
129 * We only allow it to kill its own existing entries
131 if (utx
->ut_type
!= DEAD_PROCESS
)
132 err(1, "Cannot open `%s'", tty
);
134 (void)memcpy(utold
.ut_line
, utx
->ut_line
, sizeof(utx
->ut_line
));
135 if ((utoldp
= getutxline(&utold
)) == NULL
)
136 err(1, "Cannot find existing entry for `%s'",
138 if (utoldp
->ut_pid
!= getppid())
139 err(1, "Cannot modify entry for `%s'", tty
);
143 if (pututxline(utx
) == NULL
)
144 err(1, "Cannot update utmp entry");