~squid/squid/sbuf-use

« back to all changes in this revision

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

  • Committer: hno
  • Date: 2001-01-08 06:32:04 UTC
  • Revision ID: cvs-1:hno-20010108063204-w6a8e1zz6eprqnp8
Major rewrite of proxy authentication to support other schemes than
Basic (auth_rewrite branch on SourceForge).
Contributors:
   Andy Doran
   Robert Collins
   Chemolli Francesco
   Henrik Nordstrom

For details about the new API's, see Programmers Guide.

As part of this change everything from auth_modules has been moved to
src/auth/basic/helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * allowusers.c
 
4
 * (C) 2000 Antonino Iannella, Stellar-X Pty Ltd
 
5
 * Released under GPL, see COPYING-2.0 for details.
 
6
 * 
 
7
 * These routines are to allow users attempting to use the proxy which
 
8
 * have been explicitly allowed by the system administrator.
 
9
 * The code originated from denyusers.c.
 
10
 */
 
11
 
 
12
#include <stdio.h>
 
13
#include <stdlib.h>
 
14
#include <string.h>
 
15
#include <syslog.h>
 
16
#include <unistd.h>
 
17
#include <sys/stat.h>
 
18
#include <errno.h>
 
19
#include <time.h>
 
20
#include <ctype.h>
 
21
#include <sys/param.h>
 
22
 
 
23
#define NAMELEN     50          /* Maximum username length */
 
24
 
 
25
/* Global variables */
 
26
 
 
27
char *AllowedUsers;             /* Pointer to string of allowed users */
 
28
off_t AllowUserSize;            /* Size of allowed users file */
 
29
struct stat FileBuf;            /* Stat data buffer */
 
30
time_t LastModTime;             /* Last allowed user file modification time */
 
31
 
 
32
char Allowuserpath[MAXPATHLEN]; /* MAXPATHLEN defined in param.h */
 
33
 
 
34
/* Function declarations */
 
35
 
 
36
int Read_allowusers();
 
37
int Check_ifuserallowed(char *);
 
38
void Checkforchange();
 
39
void Checktimer();
 
40
 
 
41
/*
 
42
 * Reads the allowed users file for all users to be permitted.
 
43
 * Returns 0 if the user list was successfully loaded,
 
44
 * and 1 in case of error.
 
45
 * Logs any messages to the syslog daemon.
 
46
 */
 
47
 
 
48
int
 
49
Read_allowusers()
 
50
{
 
51
    FILE *AFile;                /* Allowed users file pointer */
 
52
    off_t APos = 0;             /* File counter */
 
53
    char AChar;                 /* Character buffer */
 
54
 
 
55
    /* Stat the file. If it does not exist, save the size as zero.
 
56
     * Clear the allowed user string. Return. */
 
57
    if (stat(Allowuserpath, &FileBuf) == -1) {
 
58
        if (errno == ENOENT) {
 
59
            LastModTime = (time_t) 0;
 
60
            AllowUserSize = 0;
 
61
            free(AllowedUsers);
 
62
            AllowedUsers = malloc(sizeof(char));
 
63
            AllowedUsers[0] = '\0';
 
64
            return 0;
 
65
        } else {
 
66
            syslog(LOG_USER | LOG_ERR, strerror(errno));
 
67
            return 1;
 
68
        }
 
69
    }
 
70
    /* If it exists, save the modification time and size */
 
71
    LastModTime = FileBuf.st_mtime;
 
72
    AllowUserSize = FileBuf.st_size;
 
73
 
 
74
    /* Handle the special case of a zero length file */
 
75
    if (AllowUserSize == 0) {
 
76
        free(AllowedUsers);
 
77
        AllowedUsers = malloc(sizeof(char));
 
78
        AllowedUsers[0] = '\0';
 
79
        return 0;
 
80
    }
 
81
    /* Free and allocate space for a string to store the allowed usernames */
 
82
    free(AllowedUsers);
 
83
 
 
84
    if ((AllowedUsers = malloc(sizeof(char) * (AllowUserSize + 3))) == NULL) {
 
85
        syslog(LOG_USER | LOG_ERR, "Read_allowusers: malloc(AllowedUsers) failed.");
 
86
        return 1;
 
87
    }
 
88
    /* Open the allowed users file. Report any errors. */
 
89
 
 
90
    if ((AFile = fopen(Allowuserpath, "r")) == NULL) {
 
91
        syslog(LOG_USER | LOG_ERR, "Read_allowusers: Failed to open allowed user file.");
 
92
        syslog(LOG_USER | LOG_ERR, strerror(errno));
 
93
        return 1;
 
94
    }
 
95
    /* Read user names into the AllowedUsers string.
 
96
     * Make sure each string is delimited by a space. */
 
97
 
 
98
    AllowedUsers[APos++] = ' ';
 
99
 
 
100
    while (!feof(AFile)) {
 
101
        if ((AChar = fgetc(AFile)) == EOF)
 
102
            break;
 
103
        else {
 
104
            if (isspace(AChar))
 
105
                AllowedUsers[APos++] = ' ';
 
106
            else
 
107
                AllowedUsers[APos++] = toupper(AChar);
 
108
        }
 
109
    }
 
110
 
 
111
    AllowedUsers[APos++] = ' ';
 
112
    AllowedUsers[APos] = '\0';
 
113
    fclose(AFile);
 
114
    return 0;
 
115
}
 
116
 
 
117
/*
 
118
 * Check to see if the username provided by Squid appears in the allowed
 
119
 * user list. Returns 0 if the user was not found, and 1 if they were.
 
120
 */
 
121
 
 
122
int
 
123
Check_ifuserallowed(char *ConnectingUser)
 
124
{
 
125
    static char CUBuf[NAMELEN + 1];
 
126
    static int x;
 
127
    static char AllowMsg[256];
 
128
 
 
129
    /* If user string is empty, allow */
 
130
    if (ConnectingUser[0] == '\0')
 
131
        return 1;
 
132
 
 
133
    /* If allowed user list is empty, allow all users.
 
134
     * If no users are supposed to be using the proxy, stop squid instead. */
 
135
    if (AllowUserSize == 0)
 
136
        return 1;
 
137
 
 
138
    /* Check if username string is found in the allowed user list.
 
139
     * If so, allow. If not, deny. Reconstruct the username
 
140
     * to have whitespace, to avoid finding wrong string subsets. */
 
141
 
 
142
    sscanf(ConnectingUser, " %s ", CUBuf);
 
143
    sprintf(CUBuf, " %s ", CUBuf);
 
144
 
 
145
    for (x = 0; x <= strlen(CUBuf); x++)
 
146
        CUBuf[x] = toupper(CUBuf[x]);
 
147
 
 
148
    if (strstr(AllowedUsers, CUBuf) != NULL)
 
149
        return 1;
 
150
    else {                      /* If NULL, they are not allowed to use the proxy */
 
151
        sprintf(AllowMsg, "Denied access to user '%s'.", CUBuf);
 
152
        syslog(LOG_USER | LOG_ERR, AllowMsg);
 
153
        return 0;
 
154
    }
 
155
}
 
156
 
 
157
/*
 
158
 * Checks if there has been a change in the allowed users file.
 
159
 * If the modification time has changed, then reload the allowed user list.
 
160
 * This function is called by the SIGHUP signal handler.
 
161
 */
 
162
 
 
163
void
 
164
Check_forallowchange()
 
165
{
 
166
    struct stat ChkBuf;         /* Stat data buffer */
 
167
 
 
168
    /* Stat the allowed users file. If it cannot be accessed, return. */
 
169
 
 
170
    if (stat(Allowuserpath, &ChkBuf) == -1) {
 
171
        if (errno == ENOENT) {
 
172
            LastModTime = (time_t) 0;
 
173
            AllowUserSize = 0;
 
174
            free(AllowedUsers);
 
175
            AllowedUsers = malloc(sizeof(char));
 
176
            AllowedUsers[0] = '\0';
 
177
            return;
 
178
        } else {                /* Report error when accessing file */
 
179
            syslog(LOG_USER | LOG_ERR, strerror(errno));
 
180
            return;
 
181
        }
 
182
    }
 
183
    /* If found, compare the modification time with the previously-recorded
 
184
     * modification time.
 
185
     * If the modification time has changed, reload the allowed user list.
 
186
     * Log a message of its actions. */
 
187
 
 
188
    if (ChkBuf.st_mtime != LastModTime) {
 
189
        syslog(LOG_USER | LOG_INFO, "Check_forallowchange: Reloading allowed user list.");
 
190
        Read_allowusers();
 
191
    }
 
192
}