]>
git.ipfire.org Git - thirdparty/squid.git/blob - helpers/basic_auth/MSNT/usersfile.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
11 * (C) 2000 Antonino Iannella, Stellar-X Pty Ltd
12 * Released under GPL, see COPYING-2.0 for details.
14 * These routines are to allow users attempting to use the proxy which
15 * have been explicitly allowed by the system administrator.
16 * The code originated from denyusers.c.
29 #include <sys/param.h>
32 #include "usersfile.h"
34 #define NAMELEN 50 /* Maximum username length */
37 name_cmp(const void *a
, const void *b
)
39 const char * const *A
= static_cast<const char * const *>(a
);
40 const char * const *B
= static_cast<const char * const *>(b
);
41 return strcasecmp(*A
, *B
);
45 free_names(usersfile
* uf
)
48 for (i
= 0; i
< uf
->Inuse
; ++i
) {
57 * Reads a file of usernames and stuffs them into an array
59 * Returns 0 if the user list was successfully loaded,
60 * and 1 in case of error.
64 Read_usersfile(const char *path
, usersfile
* uf
)
77 uf
->path
= xstrdup(path
);
80 /* Open the users file. Report any errors. */
81 fp
= fopen(path
, "r");
86 syslog(LOG_ERR
, "%s: %s", path
, strerror(errno
));
89 /* Stat the file. If it does not exist, save the size as zero.
90 * Clear the allowed user string. Return. */
91 if (fstat(fileno(fp
), &FileBuf
) < 0) {
92 syslog(LOG_ERR
, "%s: %s", path
, strerror(errno
));
96 /* If it exists, save the modification time and size */
97 uf
->LMT
= FileBuf
.st_mtime
;
99 /* Handle the special case of a zero length file */
100 if (FileBuf
.st_size
== 0) {
106 * Read the file into memory
107 * XXX assumes one username per input line
109 while (fgets(buf
, 1024, fp
) != NULL
) {
110 /* ensure no names longer than our limit */
112 /* skip bad input lines */
113 if (NULL
== strtok(buf
, "\r\n"))
115 /* grow the list if necessary */
116 if (0 == uf
->Alloc
) {
118 uf
->names
= static_cast<char**>(calloc(uf
->Alloc
, sizeof(*uf
->names
)));
119 } else if (uf
->Inuse
== uf
->Alloc
) {
120 uf
->Alloc
= uf
->Alloc
<< 1;
121 uf
->names
= static_cast<char**>(realloc(uf
->names
, uf
->Alloc
* sizeof(*uf
->names
)));
122 /* zero out the newly allocated memory */
123 memset(&uf
->names
[uf
->Alloc
>> 1],
125 (uf
->Alloc
>> 1) * sizeof(*uf
->names
));
127 uf
->names
[uf
->Inuse
] = xstrdup(buf
);
133 /* sort the names for searching */
134 qsort(uf
->names
, uf
->Inuse
, sizeof(*uf
->names
), name_cmp
);
140 * Check to see if the username provided by Squid appears in the
141 * user list. Returns 0 if the user was not found, and 1 if they were.
145 Check_userlist(usersfile
* uf
, char *User
)
149 /* Empty users are always in the list */
153 /* If allowed user list is empty, allow all users.
154 * If no users are supposed to be using the proxy, stop squid instead. */
158 /* Check if username string is found in the allowed user list.
159 * If so, allow. If not, deny. Reconstruct the username
160 * to have whitespace, to avoid finding wrong string subsets. */
174 * Checks if there has been a change in a users file.
175 * If the modification time has changed, then reload the user list.
178 Check_forfilechange(usersfile
* uf
)
180 struct stat ChkBuf
; /* Stat data buffer */
182 /* Stat the allowed users file. If it cannot be accessed, return. */
184 if (uf
->path
== NULL
)
187 if (stat(uf
->path
, &ChkBuf
) < 0) {
188 if (errno
== ENOENT
) {
191 } else { /* Report error when accessing file */
192 syslog(LOG_ERR
, "%s: %s", uf
->path
, strerror(errno
));
196 /* return if no change */
197 if (ChkBuf
.st_mtime
== uf
->LMT
)
201 * The file changed, so re-read it.
203 syslog(LOG_INFO
, "Check_forfilechange: Reloading user list '%s'.", uf
->path
);
204 Read_usersfile(NULL
, uf
);