]>
git.ipfire.org Git - thirdparty/squid.git/blob - helpers/digest_auth/file/text_backend.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.
10 * AUTHOR: Robert Collins.
11 * Based on ncsa_auth.c by Arjan de Vet <Arjan.deVet@adv.iae.nl>
13 * Example digest auth text backend for Squid, based on the original
14 * proxy_auth code from client_side.c, written by
15 * Jon Thackray <jrmt@uk.gdscorp.com>.
17 * - comment lines are possible and should start with a '#';
18 * - empty or blank lines are possible;
19 * - file format is username:plaintext or username:realm:HA1
21 * To build a directory integrated backend, you need to be able to
22 * calculate the HA1 returned to squid. To avoid storing a plaintext
23 * password you can calculate MD5(username:realm:password) when the
24 * user changes their password, and store the tuple username:realm:HA1.
25 * then find the matching username:realm when squid asks for the
28 * This implementation could be improved by using such a triple for
29 * the file format. However storing such a triple does little to
30 * improve security: If compromised the username:realm:HA1 combination
31 * is "plaintext equivalent" - for the purposes of digest authentication
32 * they allow the user access. Password syncronisation is not tackled
33 * by digest - just preventing on the wire compromise.
35 * Copyright (c) 2003 Robert Collins <robertc@squid-cache.org>
39 #include "text_backend.h"
41 static hash_table
*hash
= NULL
;
42 static HASHFREE my_free
;
43 static char *passwdfile
= NULL
;
44 static int ha1mode
= 0;
45 static time_t change_time
= 0;
47 typedef struct _user_data
{
56 user_data
*u
= static_cast<user_data
*>(p
);
63 read_passwd_file(const char *passwordFile
, int isHa1Mode
)
73 hashFreeItems(hash
, my_free
);
76 hash
= hash_create((HASHCMP
*) strcmp
, 7921, hash_string
);
78 fprintf(stderr
, "digest_file_auth: cannot create hash table\n");
81 FILE *f
= fopen(passwordFile
, "r");
83 fprintf(stderr
, "digest_file_auth: cannot open password file: %s\n", xstrerror());
86 unsigned int lineCount
= 0;
87 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
89 if ((buf
[0] == '#') || (buf
[0] == ' ') || (buf
[0] == '\t') ||
92 user
= strtok(buf
, ":\n");
94 fprintf(stderr
, "digest_file_auth: missing user name at line %u in '%s'\n", lineCount
, passwordFile
);
97 realm
= strtok(NULL
, ":\n");
98 passwd
= strtok(NULL
, ":\n");
103 if ((strlen(user
) > 0) && passwd
) {
104 if (strncmp(passwd
, "{HHA1}", 6) == 0) {
107 } else if (isHa1Mode
) {
111 if (ha1
&& strlen(ha1
) != 32) {
112 /* We cannot accept plaintext passwords when using HA1 encoding,
113 * as the passwords may be output to cache.log if debugging is on.
115 fprintf(stderr
, "digest_file_auth: ignoring invalid password for %s\n", user
);
118 u
= static_cast<user_data
*>(xcalloc(1, sizeof(*u
)));
120 int len
= strlen(user
) + strlen(realm
) + 2;
121 u
->hash
.key
= malloc(len
);
122 snprintf(static_cast<char*>(u
->hash
.key
), len
, "%s:%s", user
, realm
);
124 u
->hash
.key
= xstrdup(user
);
127 u
->ha1
= xstrdup(ha1
);
129 u
->passwd
= xstrdup(passwd
);
130 hash_join(hash
, &u
->hash
);
136 /* replace when changing the backend */
138 TextArguments(int argc
, char **argv
)
142 passwdfile
= argv
[1];
143 if ((argc
== 3) && !strcmp("-c", argv
[1])) {
145 passwdfile
= argv
[2];
148 fprintf(stderr
, "Usage: digest_file_auth [OPTIONS] <passwordfile>\n");
149 fprintf(stderr
, " -c accept digest hashed passwords rather than plaintext in passwordfile\n");
152 if (stat(passwdfile
, &sb
) != 0) {
153 fprintf(stderr
, "cannot stat %s\n", passwdfile
);
158 static const user_data
*
159 GetPassword(RequestData
* requestData
)
165 if (stat(passwdfile
, &sb
) == 0) {
166 if (sb
.st_mtime
!= change_time
) {
167 read_passwd_file(passwdfile
, ha1mode
);
168 change_time
= sb
.st_mtime
;
173 len
= snprintf(buf
, sizeof(buf
), "%s:%s", requestData
->user
, requestData
->realm
);
174 if (len
>= static_cast<int>(sizeof(buf
)))
176 u
= (user_data
*)hash_lookup(hash
, buf
);
179 u
= (user_data
*)hash_lookup(hash
, requestData
->user
);
184 TextHHA1(RequestData
* requestData
)
186 const user_data
*u
= GetPassword(requestData
);
188 requestData
->error
= -1;
192 xstrncpy(requestData
->HHA1
, u
->ha1
, sizeof(requestData
->HHA1
));
195 DigestCalcHA1("md5", requestData
->user
, requestData
->realm
, u
->passwd
, NULL
, NULL
, HA1
, requestData
->HHA1
);