~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to helpers/basic_auth/MSNT/confload.c

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2006-11-11 10:32:06 UTC
  • Revision ID: james.westby@ubuntu.com-20061111103206-f3p0r9g0vq44rp3r
Tags: upstream-3.0.PRE5
ImportĀ upstreamĀ versionĀ 3.0.PRE5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * confload.c
 
4
 * (C) 2000 Antonino Iannella, Stellar-X Pty Ltd
 
5
 * Released under GPL, see COPYING-2.0 for details.
 
6
 * 
 
7
 * These routines load the msntauth configuration file.
 
8
 * It stores the servers to query, sets the denied and
 
9
 * allowed user files, and provides the 
 
10
 * authenticating function.
 
11
 */
 
12
 
 
13
#include <stdio.h>
 
14
#include <stdlib.h>
 
15
#include <string.h>
 
16
#include <syslog.h>
 
17
#include <errno.h>
 
18
#include <sys/param.h>
 
19
#include <netdb.h>
 
20
#include <assert.h>
 
21
 
 
22
#include "msntauth.h"
 
23
#include "valid.h"
 
24
 
 
25
/* Path to configuration file */
 
26
#ifndef SYSCONFDIR
 
27
#define SYSCONFDIR "/usr/local/squid/etc"
 
28
#endif
 
29
#define CONFIGFILE   SYSCONFDIR "/msntauth.conf"
 
30
 
 
31
/* Maximum number of servers to query. This number can be increased. */
 
32
#define MAXSERVERS 5
 
33
#define NTHOSTLEN 65
 
34
 
 
35
extern char Denyuserpath[MAXPATHLEN];   /* MAXPATHLEN defined in param.h */
 
36
extern char Allowuserpath[MAXPATHLEN];
 
37
 
 
38
typedef struct _ServerTuple {
 
39
    char pdc[NTHOSTLEN];
 
40
    char bdc[NTHOSTLEN];
 
41
    char domain[NTHOSTLEN];
 
42
} ServerTuple;
 
43
 
 
44
ServerTuple ServerArray[MAXSERVERS];    /* Array of servers to query */
 
45
int Serversqueried = 0;         /* Number of servers queried */
 
46
 
 
47
/* Declarations */
 
48
 
 
49
static void ProcessLine(char *);
 
50
static void AddServer(char *, char *, char *);
 
51
static int QueryServerForUser(int, char *, char *);
 
52
 
 
53
/*
 
54
 * Opens and reads the configuration file.
 
55
 * Returns 0 on success, or 1 for error.
 
56
 */
 
57
 
 
58
int
 
59
OpenConfigFile(void)
 
60
{
 
61
    FILE *ConfigFile;
 
62
    char Confbuf[2049];         /* Line reading buffer */
 
63
 
 
64
    /* Initialise defaults */
 
65
 
 
66
    Serversqueried = 0;
 
67
    memset(ServerArray, '\0', sizeof(ServerArray));
 
68
    memset(Denyuserpath, '\0', MAXPATHLEN);
 
69
    memset(Allowuserpath, '\0', MAXPATHLEN);
 
70
 
 
71
    /* Open file */
 
72
    if ((ConfigFile = fopen(CONFIGFILE, "r")) == NULL) {
 
73
        syslog(LOG_ERR, "OpenConfigFile: Failed to open %s.", CONFIGFILE);
 
74
        syslog(LOG_ERR, "%s", strerror(errno));
 
75
        return 1;
 
76
    }
 
77
    /* Read in, one line at a time */
 
78
    while (!feof(ConfigFile)) {
 
79
        Confbuf[0] = '\0';
 
80
        if (NULL == fgets(Confbuf, 2048, ConfigFile))
 
81
            break;
 
82
        Confbuf[2048] = '\0';
 
83
        ProcessLine(Confbuf);
 
84
    }
 
85
 
 
86
    /*
 
87
     * Check that at least one server is being queried. Report error if not.
 
88
     * Denied and allowed user files are hardcoded, so it's fine if they're
 
89
     * not set in the confugration file.
 
90
     */
 
91
    if (Serversqueried == 0) {
 
92
        syslog(LOG_ERR, "OpenConfigFile: No servers set in %s. At least one is needed.", CONFIGFILE);
 
93
        return 1;
 
94
    }
 
95
    fclose(ConfigFile);
 
96
    return 0;
 
97
}
 
98
 
 
99
/* Parses a configuration file line. */
 
100
 
 
101
static void
 
102
ProcessLine(char *Linebuf)
 
103
{
 
104
    char *Directive;
 
105
    char *Param1;
 
106
    char *Param2;
 
107
    char *Param3;
 
108
 
 
109
    /* Ignore empty lines */
 
110
    if (strlen(Linebuf) == 0)
 
111
        return;
 
112
 
 
113
    /* Break up on whitespaces */
 
114
    if ((Directive = strtok(Linebuf, " \t\n")) == NULL)
 
115
        return;
 
116
 
 
117
    /* Check for a comment line. If found, stop . */
 
118
    if (Directive[0] == '#')
 
119
        return;
 
120
 
 
121
    /* Check for server line. Check for 3 parameters. */
 
122
    if (strcasecmp(Directive, "server") == 0) {
 
123
        Param1 = strtok(NULL, " \t\n");
 
124
        if (NULL == Param1) {
 
125
            syslog(LOG_ERR, "ProcessLine: 'server' missing PDC parameter.");
 
126
            return;
 
127
        }
 
128
        Param2 = strtok(NULL, " \t\n");
 
129
        if (NULL == Param2) {
 
130
            syslog(LOG_ERR, "ProcessLine: 'server' missing BDC parameter.");
 
131
            return;
 
132
        }
 
133
        Param3 = strtok(NULL, " \t\n");
 
134
        if (NULL == Param3) {
 
135
            syslog(LOG_ERR, "ProcessLine: 'server' missing domain parameter.");
 
136
            return;
 
137
        }
 
138
        AddServer(Param1, Param2, Param3);
 
139
        return;
 
140
    }
 
141
    /* Check for denyusers line */
 
142
    if (strcasecmp(Directive, "denyusers") == 0) {
 
143
        Param1 = strtok(NULL, " \t\n");
 
144
 
 
145
        if (NULL == Param1) {
 
146
            syslog(LOG_ERR, "ProcessLine: A 'denyusers' line needs a filename parameter.");
 
147
            return;
 
148
        }
 
149
        memset(Denyuserpath, '\0', MAXPATHLEN);
 
150
        strncpy(Denyuserpath, Param1, MAXPATHLEN - 1);
 
151
        return;
 
152
    }
 
153
    /* Check for allowusers line */
 
154
    if (strcasecmp(Directive, "allowusers") == 0) {
 
155
        Param1 = strtok(NULL, " \t\n");
 
156
 
 
157
        if (NULL == Param1) {
 
158
            syslog(LOG_ERR, "ProcessLine: An 'allowusers' line needs a filename parameter.");
 
159
            return;
 
160
        }
 
161
        memset(Allowuserpath, '\0', MAXPATHLEN);
 
162
        strncpy(Allowuserpath, Param1, MAXPATHLEN - 1);
 
163
        return;
 
164
    }
 
165
    /* Reports error for unknown line */
 
166
    syslog(LOG_ERR, "ProcessLine: Ignoring '%s' line.", Directive);
 
167
}
 
168
 
 
169
/*
 
170
 * Adds a server to query to the server array.
 
171
 * Checks if the server IP is resolvable.
 
172
 * Checks if the number of servers to query is not exceeded.
 
173
 * Does not allow parameters longer than NTHOSTLEN.
 
174
 */
 
175
 
 
176
void
 
177
AddServer(char *ParamPDC, char *ParamBDC, char *ParamDomain)
 
178
{
 
179
    if (Serversqueried == MAXSERVERS) {
 
180
        syslog(LOG_ERR, "AddServer: Ignoring '%s' server line; "
 
181
            "too many servers.", ParamPDC);
 
182
        return;
 
183
    }
 
184
    if (gethostbyname(ParamPDC) == NULL) {
 
185
        syslog(LOG_ERR, "AddServer: Ignoring host '%s'. "
 
186
            "Cannot resolve its address.", ParamPDC);
 
187
        return;
 
188
    }
 
189
    if (gethostbyname(ParamBDC) == NULL) {
 
190
        syslog(LOG_USER | LOG_ERR, "AddServer: Ignoring host '%s'. "
 
191
            "Cannot resolve its address.", ParamBDC);
 
192
        return;
 
193
    }
 
194
    /* NOTE: ServerArray is zeroed in OpenConfigFile() */
 
195
    assert(Serversqueried < MAXSERVERS);
 
196
    strncpy(ServerArray[Serversqueried].pdc, ParamPDC, NTHOSTLEN - 1);
 
197
    strncpy(ServerArray[Serversqueried].bdc, ParamBDC, NTHOSTLEN - 1);
 
198
    strncpy(ServerArray[Serversqueried].domain, ParamDomain, NTHOSTLEN - 1);
 
199
    Serversqueried++;
 
200
}
 
201
 
 
202
/*
 
203
 * Cycles through all servers to query.
 
204
 * Returns 0 if one server could authenticate the user.
 
205
 * Returns 1 if no server authenticated the user.
 
206
 */
 
207
 
 
208
int
 
209
QueryServers(char *username, char *password)
 
210
{
 
211
    int i;
 
212
    for (i = 0; i < Serversqueried; i++) {
 
213
        if (0 == QueryServerForUser(i, username, password))
 
214
            return 0;
 
215
    }
 
216
    return 1;
 
217
}
 
218
 
 
219
/*
 
220
 * Attempts to authenticate the user with one server.
 
221
 * Logs syslog messages for different errors.
 
222
 * Returns 0 on success, non-zero on failure.
 
223
 */
 
224
 
 
225
/* Define for systems which don't support it, like Solaris */
 
226
#ifndef LOG_AUTHPRIV
 
227
#define LOG_AUTHPRIV LOG_AUTH
 
228
#endif
 
229
 
 
230
static int
 
231
QueryServerForUser(int x, char *username, char *password)
 
232
{
 
233
    int result = 1;
 
234
 
 
235
    result = Valid_User(username, password, ServerArray[x].pdc,
 
236
        ServerArray[x].bdc, ServerArray[x].domain);
 
237
 
 
238
    switch (result) {           /* Write any helpful syslog messages */
 
239
    case 0:
 
240
        break;
 
241
    case 1:
 
242
        syslog(LOG_AUTHPRIV | LOG_INFO, "Server error when checking %s.",
 
243
            username);
 
244
        break;
 
245
    case 2:
 
246
        syslog(LOG_AUTHPRIV | LOG_INFO, "Protocol error when checking %s.",
 
247
            username);
 
248
        break;
 
249
    case 3:
 
250
        syslog(LOG_AUTHPRIV | LOG_INFO, "Authentication failed for %s.",
 
251
            username);
 
252
        break;
 
253
    }
 
254
 
 
255
    return result;
 
256
}
 
257
 
 
258
/* Valid_User return codes -
 
259
 * 
 
260
 * 0 - User authenticated successfully.
 
261
 * 1 - Server error.
 
262
 * 2 - Protocol error.
 
263
 * 3 - Logon error; Incorrect password or username given.
 
264
 */