Squid Web Cache master
Loading...
Searching...
No Matches
text_backend.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9/*
10 * AUTHOR: Robert Collins.
11 * Based on ncsa_auth.c by Arjan de Vet <Arjan.deVet@adv.iae.nl>
12 *
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>.
16 *
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
20 *
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
26 * HA1.
27 *
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.
34 *
35 * Copyright (c) 2003 Robert Collins <robertc@squid-cache.org>
36 */
37
38#include "squid.h"
40
41static hash_table *hash = nullptr;
43static char *passwdfile = nullptr;
44static int ha1mode = 0;
45static time_t change_time = 0;
46
47typedef struct _user_data {
49 char *passwd;
50 char *ha1;
52
53static void
54my_free(void *p)
55{
56 user_data *u = static_cast<user_data*>(p);
57 xfree(u->hash.key);
58 xfree(u->passwd);
59 xfree(u->ha1);
60 xfree(u);
61}
62
63static void
64read_passwd_file(const char *passwordFile, int isHa1Mode)
65{
66 char buf[8192];
67 user_data *u;
68 char *user;
69 char *passwd;
70 char *ha1 = nullptr;
71 char *realm;
72
73 if (hash != nullptr) {
76 hash = nullptr;
77 }
78 /* initial setup */
79 hash = hash_create((HASHCMP *) strcmp, 7921, hash_string);
80 if (nullptr == hash) {
81 fprintf(stderr, "digest_file_auth: cannot create hash table\n");
82 exit(EXIT_FAILURE);
83 }
84 FILE *f = fopen(passwordFile, "r");
85 if (!f) {
86 int xerrno = errno;
87 fprintf(stderr, "digest_file_auth: cannot open password file: %s\n", xstrerr(xerrno));
88 exit(EXIT_FAILURE);
89 }
90 unsigned int lineCount = 0;
91 while (fgets(buf, sizeof(buf), f) != nullptr) {
92 ha1 = nullptr;
93 ++lineCount;
94 if ((buf[0] == '#') || (buf[0] == ' ') || (buf[0] == '\t') ||
95 (buf[0] == '\n'))
96 continue;
97 user = strtok(buf, ":\n");
98 if (!user) {
99 fprintf(stderr, "digest_file_auth: missing user name at line %u in '%s'\n", lineCount, passwordFile);
100 continue;
101 }
102 realm = strtok(nullptr, ":\n");
103 passwd = strtok(nullptr, ":\n");
104 if (!passwd) {
105 passwd = realm;
106 realm = nullptr;
107 }
108 if ((strlen(user) > 0) && passwd) {
109 if (strncmp(passwd, "{HHA1}", 6) == 0) {
110 ha1 = passwd + 6;
111 passwd = nullptr;
112 } else if (isHa1Mode) {
113 ha1 = passwd;
114 passwd = nullptr;
115 }
116 if (ha1 && strlen(ha1) != 32) {
117 /* We cannot accept plaintext passwords when using HA1 encoding,
118 * as the passwords may be output to cache.log if debugging is on.
119 */
120 fprintf(stderr, "digest_file_auth: ignoring invalid password for %s\n", user);
121 continue;
122 }
123 u = static_cast<user_data*>(xcalloc(1, sizeof(*u)));
124 if (realm) {
125 int len = strlen(user) + strlen(realm) + 2;
126 u->hash.key = xmalloc(len);
127 snprintf(static_cast<char*>(u->hash.key), len, "%s:%s", user, realm);
128 } else {
129 u->hash.key = xstrdup(user);
130 }
131 if (ha1)
132 u->ha1 = xstrdup(ha1);
133 else
134 u->passwd = xstrdup(passwd);
135 hash_join(hash, &u->hash);
136 }
137 }
138 fclose(f);
139}
140
141/* replace when changing the backend */
142void
143TextArguments(int argc, char **argv)
144{
145 struct stat sb;
146 if (argc == 2)
147 passwdfile = argv[1];
148 if ((argc == 3) && !strcmp("-c", argv[1])) {
149 ha1mode = 1;
150 passwdfile = argv[2];
151 }
152 if (!passwdfile) {
153 fprintf(stderr, "Usage: digest_file_auth [OPTIONS] <passwordfile>\n");
154 fprintf(stderr, " -c accept digest hashed passwords rather than plaintext in passwordfile\n");
155 exit(EXIT_FAILURE);
156 }
157 if (stat(passwdfile, &sb) != 0) {
158 fprintf(stderr, "cannot stat %s\n", passwdfile);
159 exit(EXIT_FAILURE);
160 }
161}
162
163static const user_data *
165{
166 user_data *u;
167 struct stat sb;
168 char buf[256];
169 int len;
170 if (stat(passwdfile, &sb) == 0) {
171 if (sb.st_mtime != change_time) {
173 change_time = sb.st_mtime;
174 }
175 }
176 if (!hash)
177 return nullptr;
178 len = snprintf(buf, sizeof(buf), "%s:%s", requestData->user, requestData->realm);
179 if (len >= static_cast<int>(sizeof(buf)))
180 return nullptr;
181 u = (user_data*)hash_lookup(hash, buf);
182 if (u)
183 return u;
184 u = (user_data*)hash_lookup(hash, requestData->user);
185 return u;
186}
187
188void
189TextHHA1(RequestData * requestData)
190{
191 const user_data *u = GetPassword(requestData);
192 if (!u) {
193 requestData->error = -1;
194 return;
195 }
196 if (u->ha1) {
197 xstrncpy(requestData->HHA1, u->ha1, sizeof(requestData->HHA1));
198 } else {
199 HASH HA1;
200 DigestCalcHA1("md5", requestData->user, requestData->realm, u->passwd, nullptr, nullptr, HA1, requestData->HHA1);
201 }
202}
203
void hashFreeMemory(hash_table *)
Definition hash.cc:268
HASHHASH hash_string
Definition hash.h:45
void hashFreeItems(hash_table *, HASHFREE *)
Definition hash.cc:252
void HASHFREE(void *)
Definition hash.h:12
hash_link * hash_lookup(hash_table *, const void *)
Definition hash.cc:146
hash_table * hash_create(HASHCMP *, int, HASHHASH *)
Definition hash.cc:108
int HASHCMP(const void *, const void *)
Definition hash.h:13
void hash_join(hash_table *, hash_link *)
Definition hash.cc:131
#define xfree
#define xstrdup
#define xmalloc
void DigestCalcHA1(const char *pszAlg, const char *pszUserName, const char *pszRealm, const char *pszPassword, const char *pszNonce, const char *pszCNonce, HASH HA1, HASHHEX SessionKey)
Definition rfc2617.c:88
char HASH[HASHLEN]
Definition rfc2617.h:31
hash_link hash
char * passwd
static hash_table * hash
static HASHFREE my_free
static int ha1mode
void TextArguments(int argc, char **argv)
void TextHHA1(RequestData *requestData)
static void read_passwd_file(const char *passwordFile, int isHa1Mode)
static char * passwdfile
static time_t change_time
static const user_data * GetPassword(RequestData *requestData)
struct _user_data user_data
void * xcalloc(size_t n, size_t sz)
Definition xalloc.cc:71
const char * xstrerr(int error)
Definition xstrerror.cc:83
char * xstrncpy(char *dst, const char *src, size_t n)
Definition xstring.cc:37