File: common.c

package info (click to toggle)
jail 1:1.5-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 156 kB
  • ctags: 67
  • sloc: ansic: 482; makefile: 74; sh: 25
file content (140 lines) | stat: -rw-r--r-- 3,534 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * $Id: common.c,v 1.4 1998/08/03 14:09:36 ams Exp $
 * Copyright 1998 ams@wiw.org (Abhijit Menon-Sen)
 */

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/types.h>

#include "global.h"

#ifndef NOFILE
#define NOFILE 1024
#endif

/* similar to 4.4BSD's daemon(3) */
void background(void)
{
    int fd, fs;

    if (getppid() > 0) {

        /* ignore console signals */
        signal(SIGTTOU, SIG_IGN);
        signal(SIGTTIN, SIG_IGN);
        signal(SIGTSTP, SIG_IGN);

        if ((fs = fork()) < 0) {
            perror("fork");
            exit(-1);
        }

        if (fs > 0) {
            /* exit from parent */
            exit(0);
        }

        /* dissociate from controlling TTY */
        setsid();
    }
    for (fd = 0; fd < NOFILE; ++fd) { close(fd); }
    errno = 0;
    chdir("/");
    umask(0);
}

/* decide if a host is to be ignored or not */
int excepted(unsigned long int addr, char *name) 
{
    char *ip;
    struct ex *l = &root;
    struct in_addr i;

    i.s_addr = addr;
    ip = inet_ntoa(i);

    while (l->link != NULL) {
        if (strspn(l->host, ".0123456789") == strlen(l->host)) {
            /* numeric fragment */
            if (strstr(ip, l->host) != NULL) { return 1; }
        }
        else {
            /* hostname fragment */
            if (strstr(name, l->host) != NULL) { return 1; }
        }
        l = l->link;
    }
    return (0);
}

/* gethostbyaddr(3) wrapper */
char *gethost(unsigned long int addr)
{
    extern int resolve_names;
    struct hostent *h;
    struct in_addr i;

    i.s_addr = addr;
    /* decide whether to call gethostbyaddr(3) or not */
    if (resolve_names) {
        h = gethostbyaddr((char *)&i, sizeof (i), AF_INET);
        if (h == NULL) {
            return (inet_ntoa(i));
        }
        else {
            return (h->h_name);
        }
    }
    else { return (inet_ntoa(i)); }
}

/* convert keyword to log level */
short int loglevel(char *str)
{
    extern int lvl;

    if (str == NULL) { return (lvl); }

    /* translate valid keywords */
    if (strcasecmp(str, "IGNORE" ) == 0) { return (IGNORE);      }
    if (strcasecmp(str, "ERR"    ) == 0) { return (LOG_ERR);     }
    if (strcasecmp(str, "WARNING") == 0) { return (LOG_WARNING); }
    if (strcasecmp(str, "NOTICE" ) == 0) { return (LOG_NOTICE);  }
    if (strcasecmp(str, "INFO"   ) == 0) { return (LOG_INFO);    }
    if (strcasecmp(str, "DEBUG"  ) == 0) { return (LOG_DEBUG);   }

    return (lvl);
}

/* convert keyword to syslog facility */
short int facility(char *str)
{
    extern int fac;

    if (str == NULL) { return (fac); }

    /* translate valid keywords */
    if (strcasecmp(str, "USER"  ) == 0) { return (LOG_USER);   }
    if (strcasecmp(str, "DAEMON") == 0) { return (LOG_DAEMON); }
    if (strcasecmp(str, "LOCAL0") == 0) { return (LOG_LOCAL0); }
    if (strcasecmp(str, "LOCAL1") == 0) { return (LOG_LOCAL1); }
    if (strcasecmp(str, "LOCAL2") == 0) { return (LOG_LOCAL2); }
    if (strcasecmp(str, "LOCAL3") == 0) { return (LOG_LOCAL3); }
    if (strcasecmp(str, "LOCAL4") == 0) { return (LOG_LOCAL4); }
    if (strcasecmp(str, "LOCAL5") == 0) { return (LOG_LOCAL5); }
    if (strcasecmp(str, "LOCAL6") == 0) { return (LOG_LOCAL6); }
    if (strcasecmp(str, "LOCAL7") == 0) { return (LOG_LOCAL7); }

    return (fac);
}