155682Smarkm/*
2233294Sstas * Copyright (c) 1995 - 2000 Kungliga Tekniska H��gskolan
355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
455682Smarkm * All rights reserved.
5233294Sstas *
655682Smarkm * Redistribution and use in source and binary forms, with or without
755682Smarkm * modification, are permitted provided that the following conditions
855682Smarkm * are met:
9233294Sstas *
1055682Smarkm * 1. Redistributions of source code must retain the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer.
12233294Sstas *
1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1455682Smarkm *    notice, this list of conditions and the following disclaimer in the
1555682Smarkm *    documentation and/or other materials provided with the distribution.
16233294Sstas *
1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors
1855682Smarkm *    may be used to endorse or promote products derived from this software
1955682Smarkm *    without specific prior written permission.
20233294Sstas *
2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155682Smarkm * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#ifdef HAVE_CONFIG_H
3555682Smarkm#include <config.h>
36233294SstasRCSID("$Id$");
3755682Smarkm#endif
3855682Smarkm
3955682Smarkm#include <stdio.h>
4055682Smarkm#include <string.h>
4155682Smarkm#ifdef TIME_WITH_SYS_TIME
4255682Smarkm#include <sys/time.h>
4355682Smarkm#include <time.h>
4455682Smarkm#elif defined(HAVE_SYS_TIME_H)
4555682Smarkm#include <sys/time.h>
4655682Smarkm#else
4755682Smarkm#include <time.h>
4855682Smarkm#endif
4955682Smarkm#ifdef HAVE_UNISTD_H
5055682Smarkm#include <unistd.h>
5155682Smarkm#endif
5255682Smarkm#ifdef HAVE_FCNTL_H
5355682Smarkm#include <fcntl.h>
5455682Smarkm#endif
5555682Smarkm#ifdef HAVE_UTMP_H
5655682Smarkm#include <utmp.h>
5755682Smarkm#endif
5855682Smarkm#ifdef HAVE_UTMPX_H
5955682Smarkm#include <utmpx.h>
6055682Smarkm#endif
61233294Sstas#ifdef HAVE_ASL_H
62233294Sstas#include <asl.h>
63233294Sstas#endif
6472445Sassar#include <roken.h>
6555682Smarkm#include "extern.h"
6655682Smarkm
67233294Sstas#ifndef HAVE_UTMPX_H
6855682Smarkm#ifndef WTMP_FILE
6955682Smarkm#ifdef _PATH_WTMP
7055682Smarkm#define WTMP_FILE _PATH_WTMP
7155682Smarkm#else
7255682Smarkm#define WTMP_FILE "/var/adm/wtmp"
7355682Smarkm#endif
7455682Smarkm#endif
75233294Sstas#endif
7655682Smarkm
77233294Sstas#ifdef HAVE_ASL_H
78233294Sstas
79233294Sstas#ifndef ASL_KEY_FACILITY
80233294Sstas#define ASL_KEY_FACILITY "Facility"
81233294Sstas#endif
82233294Sstas
83233294Sstasstatic void
84233294Sstasftpd_logwtmp_asl(char *line, char *name, char *host)
8555682Smarkm{
86233294Sstas    static aslmsg m = NULL;
8755682Smarkm    static int init = 0;
88233294Sstas
89233294Sstas    if (!init) {
90233294Sstas	init = 1;
91233294Sstas	m = asl_new(ASL_TYPE_MSG);
92233294Sstas	if (m == NULL)
93233294Sstas	    return;
94233294Sstas	asl_set(m, ASL_KEY_FACILITY, "org.h5l.ftpd");
95233294Sstas    }
96233294Sstas    if (m)
97233294Sstas	asl_log(NULL, m, ASL_LEVEL_NOTICE,
98233294Sstas		"host %s/%s user %s%sconnected pid %d",
99233294Sstas		host, line, name, name[0] ? " " : "dis", (int)getpid());
100233294Sstas}
101233294Sstas
102233294Sstas#endif
103233294Sstas
104233294Sstas#ifndef HAVE_ASL_H
105233294Sstas
106233294Sstasstatic void
107233294Sstasftpd_logwtmp_wtmp(char *line, char *name, char *host)
108233294Sstas{
109233294Sstas    static int init = 0;
11055682Smarkm    static int fd;
11155682Smarkm#ifdef WTMPX_FILE
11255682Smarkm    static int fdx;
11355682Smarkm#endif
114233294Sstas#ifdef HAVE_UTMP_H
11555682Smarkm    struct utmp ut;
116233294Sstas#endif
117233294Sstas#if defined(WTMPX_FILE) || defined(HAVE_UTMPX_H)
11855682Smarkm    struct utmpx utx;
11955682Smarkm#endif
12055682Smarkm
121233294Sstas#ifdef HAVE_UTMPX_H
122233294Sstas    memset(&utx, 0, sizeof(struct utmpx));
123233294Sstas#endif
124233294Sstas#ifdef HAVE_UTMP_H
12555682Smarkm    memset(&ut, 0, sizeof(struct utmp));
12655682Smarkm#ifdef HAVE_STRUCT_UTMP_UT_TYPE
12755682Smarkm    if(name[0])
12855682Smarkm	ut.ut_type = USER_PROCESS;
12955682Smarkm    else
13055682Smarkm	ut.ut_type = DEAD_PROCESS;
13155682Smarkm#endif
13255682Smarkm    strncpy(ut.ut_line, line, sizeof(ut.ut_line));
13355682Smarkm    strncpy(ut.ut_name, name, sizeof(ut.ut_name));
13455682Smarkm#ifdef HAVE_STRUCT_UTMP_UT_PID
13555682Smarkm    ut.ut_pid = getpid();
13655682Smarkm#endif
13755682Smarkm#ifdef HAVE_STRUCT_UTMP_UT_HOST
13855682Smarkm    strncpy(ut.ut_host, host, sizeof(ut.ut_host));
13955682Smarkm#endif
14055682Smarkm    ut.ut_time = time(NULL);
141233294Sstas#endif
14255682Smarkm
143233294Sstas#if defined(WTMPX_FILE) || defined(HAVE_UTMPX_H)
14455682Smarkm    strncpy(utx.ut_line, line, sizeof(utx.ut_line));
14555682Smarkm    strncpy(utx.ut_user, name, sizeof(utx.ut_user));
14655682Smarkm    strncpy(utx.ut_host, host, sizeof(utx.ut_host));
14755682Smarkm#ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN
14855682Smarkm    utx.ut_syslen = strlen(host) + 1;
14955682Smarkm    if (utx.ut_syslen > sizeof(utx.ut_host))
15055682Smarkm        utx.ut_syslen = sizeof(utx.ut_host);
15155682Smarkm#endif
15255682Smarkm    {
15355682Smarkm	struct timeval tv;
15455682Smarkm
15555682Smarkm	gettimeofday (&tv, 0);
15655682Smarkm	utx.ut_tv.tv_sec = tv.tv_sec;
15755682Smarkm	utx.ut_tv.tv_usec = tv.tv_usec;
15855682Smarkm    }
15955682Smarkm
16055682Smarkm    if(name[0])
16155682Smarkm	utx.ut_type = USER_PROCESS;
16255682Smarkm    else
16355682Smarkm	utx.ut_type = DEAD_PROCESS;
16455682Smarkm#endif
16555682Smarkm
166233294Sstas#ifdef HAVE_UTMPX_H
167233294Sstas    pututxline(&utx);
168233294Sstas#endif
169233294Sstas
17055682Smarkm    if(!init){
171233294Sstas#ifdef WTMP_FILE
17255682Smarkm	fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0);
173233294Sstas#endif
17455682Smarkm#ifdef WTMPX_FILE
17555682Smarkm	fdx = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0);
17655682Smarkm#endif
17755682Smarkm	init = 1;
17855682Smarkm    }
17955682Smarkm    if(fd >= 0) {
180233294Sstas#ifdef WTMP_FILE
18155682Smarkm	write(fd, &ut, sizeof(struct utmp)); /* XXX */
182233294Sstas#endif
18355682Smarkm#ifdef WTMPX_FILE
18455682Smarkm	write(fdx, &utx, sizeof(struct utmpx));
185233294Sstas#endif
18655682Smarkm    }
18755682Smarkm}
188233294Sstas
189233294Sstas#endif /* !HAVE_ASL_H */
190233294Sstas
191233294Sstasvoid
192233294Sstasftpd_logwtmp(char *line, char *name, char *host)
193233294Sstas{
194233294Sstas#ifdef HAVE_ASL_H
195233294Sstas    ftpd_logwtmp_asl(line, name, host);
196233294Sstas#else
197233294Sstas    ftpd_logwtmp_wtmp(line, name, host);
198233294Sstas#endif
199233294Sstas}
200