1b528cefcSMark Murray /*
25e9cd1aeSAssar Westerlund * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #ifdef HAVE_CONFIG_H
35b528cefcSMark Murray #include <config.h>
36c19800e8SDoug Rabson RCSID("$Id$");
37b528cefcSMark Murray #endif
38b528cefcSMark Murray
39b528cefcSMark Murray #include <stdio.h>
40b528cefcSMark Murray #include <string.h>
41b528cefcSMark Murray #ifdef TIME_WITH_SYS_TIME
42b528cefcSMark Murray #include <sys/time.h>
43b528cefcSMark Murray #include <time.h>
44b528cefcSMark Murray #elif defined(HAVE_SYS_TIME_H)
45b528cefcSMark Murray #include <sys/time.h>
46b528cefcSMark Murray #else
47b528cefcSMark Murray #include <time.h>
48b528cefcSMark Murray #endif
49b528cefcSMark Murray #ifdef HAVE_UNISTD_H
50b528cefcSMark Murray #include <unistd.h>
51b528cefcSMark Murray #endif
52b528cefcSMark Murray #ifdef HAVE_FCNTL_H
53b528cefcSMark Murray #include <fcntl.h>
54b528cefcSMark Murray #endif
55b528cefcSMark Murray #ifdef HAVE_UTMP_H
56b528cefcSMark Murray #include <utmp.h>
57b528cefcSMark Murray #endif
58b528cefcSMark Murray #ifdef HAVE_UTMPX_H
59b528cefcSMark Murray #include <utmpx.h>
60b528cefcSMark Murray #endif
615e9cd1aeSAssar Westerlund #ifdef HAVE_ASL_H
62b528cefcSMark Murray #include <asl.h>
63b528cefcSMark Murray #endif
64b528cefcSMark Murray #include <roken.h>
65b528cefcSMark Murray #include "extern.h"
66b528cefcSMark Murray
67b528cefcSMark Murray #ifndef HAVE_UTMPX_H
68b528cefcSMark Murray #ifndef WTMP_FILE
69b528cefcSMark Murray #ifdef _PATH_WTMP
70b528cefcSMark Murray #define WTMP_FILE _PATH_WTMP
71b528cefcSMark Murray #else
72b528cefcSMark Murray #define WTMP_FILE "/var/adm/wtmp"
73b528cefcSMark Murray #endif
74b528cefcSMark Murray #endif
75b528cefcSMark Murray #endif
76b528cefcSMark Murray
77b528cefcSMark Murray #ifdef HAVE_ASL_H
78b528cefcSMark Murray
79b528cefcSMark Murray #ifndef ASL_KEY_FACILITY
80b528cefcSMark Murray #define ASL_KEY_FACILITY "Facility"
81b528cefcSMark Murray #endif
82b528cefcSMark Murray
83b528cefcSMark Murray static void
ftpd_logwtmp_asl(char * line,char * name,char * host)84b528cefcSMark Murray ftpd_logwtmp_asl(char *line, char *name, char *host)
85b528cefcSMark Murray {
86b528cefcSMark Murray static aslmsg m = NULL;
87b528cefcSMark Murray static int init = 0;
88b528cefcSMark Murray
89b528cefcSMark Murray if (!init) {
90b528cefcSMark Murray init = 1;
91b528cefcSMark Murray m = asl_new(ASL_TYPE_MSG);
92b528cefcSMark Murray if (m == NULL)
93b528cefcSMark Murray return;
94b528cefcSMark Murray asl_set(m, ASL_KEY_FACILITY, "org.h5l.ftpd");
95b528cefcSMark Murray }
96b528cefcSMark Murray if (m)
97b528cefcSMark Murray asl_log(NULL, m, ASL_LEVEL_NOTICE,
98b528cefcSMark Murray "host %s/%s user %s%sconnected pid %d",
99b528cefcSMark Murray host, line, name, name[0] ? " " : "dis", (int)getpid());
100b528cefcSMark Murray }
101b528cefcSMark Murray
102b528cefcSMark Murray #endif
103b528cefcSMark Murray
104b528cefcSMark Murray #ifndef HAVE_ASL_H
105b528cefcSMark Murray
106b528cefcSMark Murray static void
ftpd_logwtmp_wtmp(char * line,char * name,char * host)107b528cefcSMark Murray ftpd_logwtmp_wtmp(char *line, char *name, char *host)
108b528cefcSMark Murray {
109b528cefcSMark Murray static int init = 0;
110b528cefcSMark Murray static int fd;
111b528cefcSMark Murray #ifdef WTMPX_FILE
112b528cefcSMark Murray static int fdx;
113b528cefcSMark Murray #endif
114b528cefcSMark Murray #ifdef HAVE_UTMP_H
115b528cefcSMark Murray struct utmp ut;
116b528cefcSMark Murray #endif
117b528cefcSMark Murray #if defined(WTMPX_FILE) || defined(HAVE_UTMPX_H)
118b528cefcSMark Murray struct utmpx utx;
119b528cefcSMark Murray #endif
120b528cefcSMark Murray
121b528cefcSMark Murray #ifdef HAVE_UTMPX_H
122b528cefcSMark Murray memset(&utx, 0, sizeof(struct utmpx));
123b528cefcSMark Murray #endif
124b528cefcSMark Murray #ifdef HAVE_UTMP_H
125b528cefcSMark Murray memset(&ut, 0, sizeof(struct utmp));
126b528cefcSMark Murray #ifdef HAVE_STRUCT_UTMP_UT_TYPE
127b528cefcSMark Murray if(name[0])
128b528cefcSMark Murray ut.ut_type = USER_PROCESS;
129b528cefcSMark Murray else
130b528cefcSMark Murray ut.ut_type = DEAD_PROCESS;
131b528cefcSMark Murray #endif
132b528cefcSMark Murray strncpy(ut.ut_line, line, sizeof(ut.ut_line));
133b528cefcSMark Murray strncpy(ut.ut_name, name, sizeof(ut.ut_name));
134b528cefcSMark Murray #ifdef HAVE_STRUCT_UTMP_UT_PID
135b528cefcSMark Murray ut.ut_pid = getpid();
136b528cefcSMark Murray #endif
137b528cefcSMark Murray #ifdef HAVE_STRUCT_UTMP_UT_HOST
138b528cefcSMark Murray strncpy(ut.ut_host, host, sizeof(ut.ut_host));
139 #endif
140 ut.ut_time = time(NULL);
141 #endif
142
143 #if defined(WTMPX_FILE) || defined(HAVE_UTMPX_H)
144 strncpy(utx.ut_line, line, sizeof(utx.ut_line));
145 strncpy(utx.ut_user, name, sizeof(utx.ut_user));
146 strncpy(utx.ut_host, host, sizeof(utx.ut_host));
147 #ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN
148 utx.ut_syslen = strlen(host) + 1;
149 if (utx.ut_syslen > sizeof(utx.ut_host))
150 utx.ut_syslen = sizeof(utx.ut_host);
151 #endif
152 {
153 struct timeval tv;
154
155 gettimeofday (&tv, 0);
156 utx.ut_tv.tv_sec = tv.tv_sec;
157 utx.ut_tv.tv_usec = tv.tv_usec;
158 }
159
160 if(name[0])
161 utx.ut_type = USER_PROCESS;
162 else
163 utx.ut_type = DEAD_PROCESS;
164 #endif
165
166 #ifdef HAVE_UTMPX_H
167 pututxline(&utx);
168 #endif
169
170 if(!init){
171 #ifdef WTMP_FILE
172 fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0);
173 #endif
174 #ifdef WTMPX_FILE
175 fdx = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0);
176 #endif
177 init = 1;
178 }
179 if(fd >= 0) {
180 #ifdef WTMP_FILE
181 write(fd, &ut, sizeof(struct utmp)); /* XXX */
182 #endif
183 #ifdef WTMPX_FILE
184 write(fdx, &utx, sizeof(struct utmpx));
185 #endif
186 }
187 }
188
189 #endif /* !HAVE_ASL_H */
190
191 void
ftpd_logwtmp(char * line,char * name,char * host)192 ftpd_logwtmp(char *line, char *name, char *host)
193 {
194 #ifdef HAVE_ASL_H
195 ftpd_logwtmp_asl(line, name, host);
196 #else
197 ftpd_logwtmp_wtmp(line, name, host);
198 #endif
199 }
200