]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/digest/file/text_backend.cc
2 * Copyright (C) 1996-2023 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 synchronization is not tackled
33 * by digest - just preventing on the wire compromise.
35 * Copyright (c) 2003 Robert Collins <robertc@squid-cache.org>
39 #include "auth/digest/file/text_backend.h"
41 static hash_table
*hash
= nullptr;
42 static HASHFREE my_free
;
43 static char *passwdfile
= nullptr;
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
)
72 if (hash
!= nullptr) {
73 hashFreeItems(hash
, my_free
);
76 hash
= hash_create((HASHCMP
*) strcmp
, 7921, hash_string
);
77 if (nullptr == hash
) {
78 fprintf(stderr
, "digest_file_auth: cannot create hash table\n");
81 FILE *f
= fopen(passwordFile
, "r");
84 fprintf(stderr
, "digest_file_auth: cannot open password file: %s\n", xstrerr(xerrno
));
87 unsigned int lineCount
= 0;
88 while (fgets(buf
, sizeof(buf
), f
) != nullptr) {
90 if ((buf
[0] == '#') || (buf
[0] == ' ') || (buf
[0] == '\t') ||
93 user
= strtok(buf
, ":\n");
95 fprintf(stderr
, "digest_file_auth: missing user name at line %u in '%s'\n", lineCount
, passwordFile
);
98 realm
= strtok(nullptr, ":\n");
99 passwd
= strtok(nullptr, ":\n");
104 if ((strlen(user
) > 0) && passwd
) {
105 if (strncmp(passwd
, "{HHA1}", 6) == 0) {
108 } else if (isHa1Mode
) {
112 if (ha1
&& strlen(ha1
) != 32) {
113 /* We cannot accept plaintext passwords when using HA1 encoding,
114 * as the passwords may be output to cache.log if debugging is on.
116 fprintf(stderr
, "digest_file_auth: ignoring invalid password for %s\n", user
);
119 u
= static_cast<user_data
*>(xcalloc(1, sizeof(*u
)));
121 int len
= strlen(user
) + strlen(realm
) + 2;
122 u
->hash
.key
= xmalloc(len
);
123 snprintf(static_cast<char*>(u
->hash
.key
), len
, "%s:%s", user
, realm
);
125 u
->hash
.key
= xstrdup(user
);
128 u
->ha1
= xstrdup(ha1
);
130 u
->passwd
= xstrdup(passwd
);
131 hash_join(hash
, &u
->hash
);
137 /* replace when changing the backend */
139 TextArguments(int argc
, char **argv
)
143 passwdfile
= argv
[1];
144 if ((argc
== 3) && !strcmp("-c", argv
[1])) {
146 passwdfile
= argv
[2];
149 fprintf(stderr
, "Usage: digest_file_auth [OPTIONS] <passwordfile>\n");
150 fprintf(stderr
, " -c accept digest hashed passwords rather than plaintext in passwordfile\n");
153 if (stat(passwdfile
, &sb
) != 0) {
154 fprintf(stderr
, "cannot stat %s\n", passwdfile
);
159 static const user_data
*
160 GetPassword(RequestData
* requestData
)
166 if (stat(passwdfile
, &sb
) == 0) {
167 if (sb
.st_mtime
!= change_time
) {
168 read_passwd_file(passwdfile
, ha1mode
);
169 change_time
= sb
.st_mtime
;
174 len
= snprintf(buf
, sizeof(buf
), "%s:%s", requestData
->user
, requestData
->realm
);
175 if (len
>= static_cast<int>(sizeof(buf
)))
177 u
= (user_data
*)hash_lookup(hash
, buf
);
180 u
= (user_data
*)hash_lookup(hash
, requestData
->user
);
185 TextHHA1(RequestData
* requestData
)
187 const user_data
*u
= GetPassword(requestData
);
189 requestData
->error
= -1;
193 xstrncpy(requestData
->HHA1
, u
->ha1
, sizeof(requestData
->HHA1
));
196 DigestCalcHA1("md5", requestData
->user
, requestData
->realm
, u
->passwd
, nullptr, nullptr, HA1
, requestData
->HHA1
);