]>
Commit | Line | Data |
---|---|---|
0ff1980a | 1 | /* |
b8ae064d | 2 | * Copyright (C) 1996-2023 The Squid Software Foundation and contributors |
0ff1980a | 3 | * |
ca02e0ec AJ |
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> | |
0ff1980a | 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; | |
5fcad6f9 | 19 | * - file format is username:plaintext or username:realm:HA1 |
26ac0430 | 20 | * |
0ff1980a | 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 | |
2b61af8e | 32 | * they allow the user access. Password synchronization is not tackled |
0ff1980a | 33 | * by digest - just preventing on the wire compromise. |
34 | * | |
35 | * Copyright (c) 2003 Robert Collins <robertc@squid-cache.org> | |
36 | */ | |
37 | ||
ca02e0ec | 38 | #include "squid.h" |
d4d7f6dd | 39 | #include "auth/digest/file/text_backend.h" |
0ff1980a | 40 | |
aee3523a | 41 | static hash_table *hash = nullptr; |
0ff1980a | 42 | static HASHFREE my_free; |
aee3523a | 43 | static char *passwdfile = nullptr; |
ca6965d0 | 44 | static int ha1mode = 0; |
0ff1980a | 45 | static time_t change_time = 0; |
46 | ||
47 | typedef struct _user_data { | |
48 | hash_link hash; | |
49 | char *passwd; | |
5fcad6f9 | 50 | char *ha1; |
0ff1980a | 51 | } user_data; |
52 | ||
53 | static void | |
54 | my_free(void *p) | |
55 | { | |
54e8823b | 56 | user_data *u = static_cast<user_data*>(p); |
0ff1980a | 57 | xfree(u->hash.key); |
58 | xfree(u->passwd); | |
59 | xfree(u); | |
60 | } | |
61 | ||
62 | static void | |
d5f8d05f | 63 | read_passwd_file(const char *passwordFile, int isHa1Mode) |
0ff1980a | 64 | { |
0ff1980a | 65 | char buf[8192]; |
66 | user_data *u; | |
67 | char *user; | |
68 | char *passwd; | |
aee3523a | 69 | char *ha1 = nullptr; |
5fcad6f9 | 70 | char *realm; |
0ff1980a | 71 | |
aee3523a | 72 | if (hash != nullptr) { |
26ac0430 | 73 | hashFreeItems(hash, my_free); |
0ff1980a | 74 | } |
75 | /* initial setup */ | |
76 | hash = hash_create((HASHCMP *) strcmp, 7921, hash_string); | |
aee3523a | 77 | if (nullptr == hash) { |
54e8823b | 78 | fprintf(stderr, "digest_file_auth: cannot create hash table\n"); |
24885773 | 79 | exit(EXIT_FAILURE); |
0ff1980a | 80 | } |
85b0d7b8 AJ |
81 | FILE *f = fopen(passwordFile, "r"); |
82 | if (!f) { | |
b69e9ffa AJ |
83 | int xerrno = errno; |
84 | fprintf(stderr, "digest_file_auth: cannot open password file: %s\n", xstrerr(xerrno)); | |
24885773 | 85 | exit(EXIT_FAILURE); |
85b0d7b8 AJ |
86 | } |
87 | unsigned int lineCount = 0; | |
aee3523a | 88 | while (fgets(buf, sizeof(buf), f) != nullptr) { |
85b0d7b8 | 89 | ++lineCount; |
26ac0430 AJ |
90 | if ((buf[0] == '#') || (buf[0] == ' ') || (buf[0] == '\t') || |
91 | (buf[0] == '\n')) | |
92 | continue; | |
93 | user = strtok(buf, ":\n"); | |
85b0d7b8 | 94 | if (!user) { |
665e9b5b | 95 | fprintf(stderr, "digest_file_auth: missing user name at line %u in '%s'\n", lineCount, passwordFile); |
85b0d7b8 AJ |
96 | continue; |
97 | } | |
aee3523a AR |
98 | realm = strtok(nullptr, ":\n"); |
99 | passwd = strtok(nullptr, ":\n"); | |
26ac0430 AJ |
100 | if (!passwd) { |
101 | passwd = realm; | |
aee3523a | 102 | realm = nullptr; |
26ac0430 AJ |
103 | } |
104 | if ((strlen(user) > 0) && passwd) { | |
105 | if (strncmp(passwd, "{HHA1}", 6) == 0) { | |
106 | ha1 = passwd + 6; | |
aee3523a | 107 | passwd = nullptr; |
e053c141 | 108 | } else if (isHa1Mode) { |
26ac0430 | 109 | ha1 = passwd; |
aee3523a | 110 | passwd = nullptr; |
26ac0430 AJ |
111 | } |
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. | |
115 | */ | |
54e8823b | 116 | fprintf(stderr, "digest_file_auth: ignoring invalid password for %s\n", user); |
26ac0430 AJ |
117 | continue; |
118 | } | |
54e8823b | 119 | u = static_cast<user_data*>(xcalloc(1, sizeof(*u))); |
26ac0430 AJ |
120 | if (realm) { |
121 | int len = strlen(user) + strlen(realm) + 2; | |
c14fb378 | 122 | u->hash.key = xmalloc(len); |
54e8823b | 123 | snprintf(static_cast<char*>(u->hash.key), len, "%s:%s", user, realm); |
26ac0430 AJ |
124 | } else { |
125 | u->hash.key = xstrdup(user); | |
126 | } | |
127 | if (ha1) | |
128 | u->ha1 = xstrdup(ha1); | |
129 | else | |
130 | u->passwd = xstrdup(passwd); | |
131 | hash_join(hash, &u->hash); | |
132 | } | |
0ff1980a | 133 | } |
134 | fclose(f); | |
135 | } | |
136 | ||
137 | /* replace when changing the backend */ | |
138 | void | |
ca6965d0 | 139 | TextArguments(int argc, char **argv) |
0ff1980a | 140 | { |
141 | struct stat sb; | |
ca6965d0 | 142 | if (argc == 2) |
26ac0430 | 143 | passwdfile = argv[1]; |
ca6965d0 | 144 | if ((argc == 3) && !strcmp("-c", argv[1])) { |
26ac0430 AJ |
145 | ha1mode = 1; |
146 | passwdfile = argv[2]; | |
0ff1980a | 147 | } |
148 | if (!passwdfile) { | |
54e8823b | 149 | fprintf(stderr, "Usage: digest_file_auth [OPTIONS] <passwordfile>\n"); |
26ac0430 | 150 | fprintf(stderr, " -c accept digest hashed passwords rather than plaintext in passwordfile\n"); |
24885773 | 151 | exit(EXIT_FAILURE); |
0ff1980a | 152 | } |
153 | if (stat(passwdfile, &sb) != 0) { | |
26ac0430 | 154 | fprintf(stderr, "cannot stat %s\n", passwdfile); |
24885773 | 155 | exit(EXIT_FAILURE); |
0ff1980a | 156 | } |
157 | } | |
158 | ||
ca6965d0 | 159 | static const user_data * |
160 | GetPassword(RequestData * requestData) | |
0ff1980a | 161 | { |
162 | user_data *u; | |
163 | struct stat sb; | |
5fcad6f9 | 164 | char buf[256]; |
165 | int len; | |
0ff1980a | 166 | if (stat(passwdfile, &sb) == 0) { |
26ac0430 AJ |
167 | if (sb.st_mtime != change_time) { |
168 | read_passwd_file(passwdfile, ha1mode); | |
169 | change_time = sb.st_mtime; | |
170 | } | |
0ff1980a | 171 | } |
0ff1980a | 172 | if (!hash) |
aee3523a | 173 | return nullptr; |
5fcad6f9 | 174 | len = snprintf(buf, sizeof(buf), "%s:%s", requestData->user, requestData->realm); |
54e8823b | 175 | if (len >= static_cast<int>(sizeof(buf))) |
aee3523a | 176 | return nullptr; |
54e8823b | 177 | u = (user_data*)hash_lookup(hash, buf); |
5fcad6f9 | 178 | if (u) |
26ac0430 | 179 | return u; |
54e8823b | 180 | u = (user_data*)hash_lookup(hash, requestData->user); |
5fcad6f9 | 181 | return u; |
0ff1980a | 182 | } |
183 | ||
184 | void | |
ca6965d0 | 185 | TextHHA1(RequestData * requestData) |
0ff1980a | 186 | { |
ca6965d0 | 187 | const user_data *u = GetPassword(requestData); |
5fcad6f9 | 188 | if (!u) { |
26ac0430 AJ |
189 | requestData->error = -1; |
190 | return; | |
0ff1980a | 191 | } |
5fcad6f9 | 192 | if (u->ha1) { |
26ac0430 | 193 | xstrncpy(requestData->HHA1, u->ha1, sizeof(requestData->HHA1)); |
5fcad6f9 | 194 | } else { |
26ac0430 | 195 | HASH HA1; |
aee3523a | 196 | DigestCalcHA1("md5", requestData->user, requestData->realm, u->passwd, nullptr, nullptr, HA1, requestData->HHA1); |
5fcad6f9 | 197 | } |
0ff1980a | 198 | } |
f53969cc | 199 |