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