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