Path: blob/main/crypto/heimdal/appl/ftp/ftpd/logwtmp.c
34889 views
/*1* Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#ifdef HAVE_CONFIG_H34#include <config.h>35RCSID("$Id$");36#endif3738#include <stdio.h>39#include <string.h>40#ifdef TIME_WITH_SYS_TIME41#include <sys/time.h>42#include <time.h>43#elif defined(HAVE_SYS_TIME_H)44#include <sys/time.h>45#else46#include <time.h>47#endif48#ifdef HAVE_UNISTD_H49#include <unistd.h>50#endif51#ifdef HAVE_FCNTL_H52#include <fcntl.h>53#endif54#ifdef HAVE_UTMP_H55#include <utmp.h>56#endif57#ifdef HAVE_UTMPX_H58#include <utmpx.h>59#endif60#ifdef HAVE_ASL_H61#include <asl.h>62#endif63#include <roken.h>64#include "extern.h"6566#ifndef HAVE_UTMPX_H67#ifndef WTMP_FILE68#ifdef _PATH_WTMP69#define WTMP_FILE _PATH_WTMP70#else71#define WTMP_FILE "/var/adm/wtmp"72#endif73#endif74#endif7576#ifdef HAVE_ASL_H7778#ifndef ASL_KEY_FACILITY79#define ASL_KEY_FACILITY "Facility"80#endif8182static void83ftpd_logwtmp_asl(char *line, char *name, char *host)84{85static aslmsg m = NULL;86static int init = 0;8788if (!init) {89init = 1;90m = asl_new(ASL_TYPE_MSG);91if (m == NULL)92return;93asl_set(m, ASL_KEY_FACILITY, "org.h5l.ftpd");94}95if (m)96asl_log(NULL, m, ASL_LEVEL_NOTICE,97"host %s/%s user %s%sconnected pid %d",98host, line, name, name[0] ? " " : "dis", (int)getpid());99}100101#endif102103#ifndef HAVE_ASL_H104105static void106ftpd_logwtmp_wtmp(char *line, char *name, char *host)107{108static int init = 0;109static int fd;110#ifdef WTMPX_FILE111static int fdx;112#endif113#ifdef HAVE_UTMP_H114struct utmp ut;115#endif116#if defined(WTMPX_FILE) || defined(HAVE_UTMPX_H)117struct utmpx utx;118#endif119120#ifdef HAVE_UTMPX_H121memset(&utx, 0, sizeof(struct utmpx));122#endif123#ifdef HAVE_UTMP_H124memset(&ut, 0, sizeof(struct utmp));125#ifdef HAVE_STRUCT_UTMP_UT_TYPE126if(name[0])127ut.ut_type = USER_PROCESS;128else129ut.ut_type = DEAD_PROCESS;130#endif131strncpy(ut.ut_line, line, sizeof(ut.ut_line));132strncpy(ut.ut_name, name, sizeof(ut.ut_name));133#ifdef HAVE_STRUCT_UTMP_UT_PID134ut.ut_pid = getpid();135#endif136#ifdef HAVE_STRUCT_UTMP_UT_HOST137strncpy(ut.ut_host, host, sizeof(ut.ut_host));138#endif139ut.ut_time = time(NULL);140#endif141142#if defined(WTMPX_FILE) || defined(HAVE_UTMPX_H)143strncpy(utx.ut_line, line, sizeof(utx.ut_line));144strncpy(utx.ut_user, name, sizeof(utx.ut_user));145strncpy(utx.ut_host, host, sizeof(utx.ut_host));146#ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN147utx.ut_syslen = strlen(host) + 1;148if (utx.ut_syslen > sizeof(utx.ut_host))149utx.ut_syslen = sizeof(utx.ut_host);150#endif151{152struct timeval tv;153154gettimeofday (&tv, 0);155utx.ut_tv.tv_sec = tv.tv_sec;156utx.ut_tv.tv_usec = tv.tv_usec;157}158159if(name[0])160utx.ut_type = USER_PROCESS;161else162utx.ut_type = DEAD_PROCESS;163#endif164165#ifdef HAVE_UTMPX_H166pututxline(&utx);167#endif168169if(!init){170#ifdef WTMP_FILE171fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0);172#endif173#ifdef WTMPX_FILE174fdx = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0);175#endif176init = 1;177}178if(fd >= 0) {179#ifdef WTMP_FILE180write(fd, &ut, sizeof(struct utmp)); /* XXX */181#endif182#ifdef WTMPX_FILE183write(fdx, &utx, sizeof(struct utmpx));184#endif185}186}187188#endif /* !HAVE_ASL_H */189190void191ftpd_logwtmp(char *line, char *name, char *host)192{193#ifdef HAVE_ASL_H194ftpd_logwtmp_asl(line, name, host);195#else196ftpd_logwtmp_wtmp(line, name, host);197#endif198}199200201