new libunwind, updated to netbsd b1f513eedd
[minix3.git] / libexec / ftpd / logutmp.c
blobeacd9eb8e0e39e04e6e7f41fa4774bc113fc6a40
1 /* $NetBSD: logutmp.c,v 1.12 2011/09/16 16:13:17 plunky Exp $ */
3 /*
4 * Portions Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
33 * Portions Copyright (c) 1996, Jason Downs. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
45 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
48 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
57 #include <sys/cdefs.h>
58 #ifndef lint
59 __RCSID("$NetBSD: logutmp.c,v 1.12 2011/09/16 16:13:17 plunky Exp $");
60 #endif /* not lint */
62 #include <sys/types.h>
63 #include <sys/param.h>
65 #include <fcntl.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <ttyent.h>
70 #include <unistd.h>
71 #include <utmp.h>
72 #ifdef SUPPORT_UTMPX
73 #include <utmpx.h>
74 #endif
75 #include <util.h>
77 #include "extern.h"
79 #ifdef SUPPORT_UTMP
80 typedef struct utmp UTMP;
82 static int fd = -1;
83 static int topslot = -1;
86 * Special versions of login()/logout() which hold the utmp file open,
87 * for use with ftpd.
90 void
91 ftpd_login(const struct utmp *ut)
93 UTMP ubuf;
96 * First, loop through /etc/ttys, if needed, to initialize the
97 * top of the tty slots, since ftpd has no tty.
99 if (topslot < 0) {
100 topslot = 0;
101 while (getttyent() != NULL)
102 topslot++;
104 if ((topslot < 0) || ((fd < 0)
105 && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) < 0))
106 return;
109 * Now find a slot that's not in use...
111 (void)lseek(fd, (off_t)(topslot * sizeof(UTMP)), SEEK_SET);
113 while (1) {
114 if (read(fd, &ubuf, sizeof(UTMP)) == sizeof(UTMP)) {
115 if (!ubuf.ut_name[0]) {
116 (void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
117 break;
119 topslot++;
120 } else {
121 (void)lseek(fd, (off_t)(topslot * sizeof(UTMP)),
122 SEEK_SET);
123 break;
127 (void)write(fd, ut, sizeof(UTMP));
131 ftpd_logout(const char *line)
133 UTMP ut;
134 int rval;
136 rval = 0;
137 if (fd < 0)
138 return(rval);
140 (void)lseek(fd, 0, SEEK_SET);
142 while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) {
143 if (!ut.ut_name[0]
144 || strncmp(ut.ut_line, line, UT_LINESIZE))
145 continue;
146 memset(ut.ut_name, 0, UT_NAMESIZE);
147 memset(ut.ut_host, 0, UT_HOSTSIZE);
148 (void)time(&ut.ut_time);
149 (void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
150 (void)write(fd, &ut, sizeof(UTMP));
151 rval = 1;
153 return(rval);
155 #endif /* SUPPORT_UTMP */
157 #ifdef SUPPORT_UTMPX
159 * special version of loginx which updates utmpx only.
161 void
162 ftpd_loginx(const struct utmpx *ut)
164 (void)pututxline(ut);
168 ftpd_logoutx(const char *line, int status, int mode)
170 return logoutx(line, status, mode);
172 #endif