text_backend.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 
41 static hash_table *hash = nullptr;
43 static char *passwdfile = nullptr;
44 static int ha1mode = 0;
45 static time_t change_time = 0;
46 
47 typedef struct _user_data {
49  char *passwd;
50  char *ha1;
51 } user_data;
52 
53 static void
54 my_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);
60 }
61 
62 static void
63 read_passwd_file(const char *passwordFile, int isHa1Mode)
64 {
65  char buf[8192];
66  user_data *u;
67  char *user;
68  char *passwd;
69  char *ha1 = nullptr;
70  char *realm;
71 
72  if (hash != nullptr) {
74  }
75  /* initial setup */
76  hash = hash_create((HASHCMP *) strcmp, 7921, hash_string);
77  if (nullptr == hash) {
78  fprintf(stderr, "digest_file_auth: cannot create hash table\n");
79  exit(EXIT_FAILURE);
80  }
81  FILE *f = fopen(passwordFile, "r");
82  if (!f) {
83  int xerrno = errno;
84  fprintf(stderr, "digest_file_auth: cannot open password file: %s\n", xstrerr(xerrno));
85  exit(EXIT_FAILURE);
86  }
87  unsigned int lineCount = 0;
88  while (fgets(buf, sizeof(buf), f) != nullptr) {
89  ++lineCount;
90  if ((buf[0] == '#') || (buf[0] == ' ') || (buf[0] == '\t') ||
91  (buf[0] == '\n'))
92  continue;
93  user = strtok(buf, ":\n");
94  if (!user) {
95  fprintf(stderr, "digest_file_auth: missing user name at line %u in '%s'\n", lineCount, passwordFile);
96  continue;
97  }
98  realm = strtok(nullptr, ":\n");
99  passwd = strtok(nullptr, ":\n");
100  if (!passwd) {
101  passwd = realm;
102  realm = nullptr;
103  }
104  if ((strlen(user) > 0) && passwd) {
105  if (strncmp(passwd, "{HHA1}", 6) == 0) {
106  ha1 = passwd + 6;
107  passwd = nullptr;
108  } else if (isHa1Mode) {
109  ha1 = passwd;
110  passwd = nullptr;
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  */
116  fprintf(stderr, "digest_file_auth: ignoring invalid password for %s\n", user);
117  continue;
118  }
119  u = static_cast<user_data*>(xcalloc(1, sizeof(*u)));
120  if (realm) {
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);
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  }
133  }
134  fclose(f);
135 }
136 
137 /* replace when changing the backend */
138 void
139 TextArguments(int argc, char **argv)
140 {
141  struct stat sb;
142  if (argc == 2)
143  passwdfile = argv[1];
144  if ((argc == 3) && !strcmp("-c", argv[1])) {
145  ha1mode = 1;
146  passwdfile = argv[2];
147  }
148  if (!passwdfile) {
149  fprintf(stderr, "Usage: digest_file_auth [OPTIONS] <passwordfile>\n");
150  fprintf(stderr, " -c accept digest hashed passwords rather than plaintext in passwordfile\n");
151  exit(EXIT_FAILURE);
152  }
153  if (stat(passwdfile, &sb) != 0) {
154  fprintf(stderr, "cannot stat %s\n", passwdfile);
155  exit(EXIT_FAILURE);
156  }
157 }
158 
159 static const user_data *
160 GetPassword(RequestData * requestData)
161 {
162  user_data *u;
163  struct stat sb;
164  char buf[256];
165  int len;
166  if (stat(passwdfile, &sb) == 0) {
167  if (sb.st_mtime != change_time) {
169  change_time = sb.st_mtime;
170  }
171  }
172  if (!hash)
173  return nullptr;
174  len = snprintf(buf, sizeof(buf), "%s:%s", requestData->user, requestData->realm);
175  if (len >= static_cast<int>(sizeof(buf)))
176  return nullptr;
177  u = (user_data*)hash_lookup(hash, buf);
178  if (u)
179  return u;
180  u = (user_data*)hash_lookup(hash, requestData->user);
181  return u;
182 }
183 
184 void
185 TextHHA1(RequestData * requestData)
186 {
187  const user_data *u = GetPassword(requestData);
188  if (!u) {
189  requestData->error = -1;
190  return;
191  }
192  if (u->ha1) {
193  xstrncpy(requestData->HHA1, u->ha1, sizeof(requestData->HHA1));
194  } else {
195  HASH HA1;
196  DigestCalcHA1("md5", requestData->user, requestData->realm, u->passwd, nullptr, nullptr, HA1, requestData->HHA1);
197  }
198 }
199 
const char * xstrerr(int error)
Definition: xstrerror.cc:83
void TextArguments(int argc, char **argv)
void * xcalloc(size_t n, size_t sz)
Definition: xalloc.cc:71
#define xmalloc
HASHHASH hash_string
Definition: hash.h:45
static int ha1mode
Definition: text_backend.cc:44
#define xstrdup
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
hash_link * hash_lookup(hash_table *, const void *)
Definition: hash.cc:146
int HASHCMP(const void *, const void *)
Definition: hash.h:13
hash_link hash
Definition: text_backend.cc:48
void hashFreeItems(hash_table *, HASHFREE *)
Definition: hash.cc:252
void TextHHA1(RequestData *requestData)
char * ha1
Definition: text_backend.cc:50
static time_t change_time
Definition: text_backend.cc:45
static char * passwdfile
Definition: text_backend.cc:43
static hash_table * hash
Definition: text_backend.cc:41
static void read_passwd_file(const char *passwordFile, int isHa1Mode)
Definition: text_backend.cc:63
#define xfree
static HASHFREE my_free
Definition: text_backend.cc:42
void HASHFREE(void *)
Definition: hash.h:12
hash_table * hash_create(HASHCMP *, int, HASHHASH *)
Definition: hash.cc:108
char HASH[HASHLEN]
Definition: rfc2617.h:31
char * passwd
Definition: text_backend.cc:49
struct _user_data user_data
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
static const user_data * GetPassword(RequestData *requestData)
void hash_join(hash_table *, hash_link *)
Definition: hash.cc:131

 

Introduction

Documentation

Support

Miscellaneous