]>
Commit | Line | Data |
---|---|---|
5b95b903 | 1 | /* |
f70aedc4 | 2 | * Copyright (C) 1996-2021 The Squid Software Foundation and contributors |
5b95b903 AJ |
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 | ||
94439e4e | 9 | /* |
94439e4e | 10 | * AUTHOR: Arjan de Vet <Arjan.deVet@adv.iae.nl> |
11 | * | |
12 | * Example authentication program for Squid, based on the original | |
13 | * proxy_auth code from client_side.c, written by | |
14 | * Jon Thackray <jrmt@uk.gdscorp.com>. | |
15 | * | |
16 | * Uses a NCSA httpd style password file for authentication with the | |
17 | * following improvements suggested by various people: | |
18 | * | |
19 | * - comment lines are possible and should start with a '#'; | |
20 | * - empty or blank lines are possible; | |
21 | * - extra fields in the password file are ignored; this makes it | |
22 | * possible to use a Unix password file but I do not recommend that. | |
23 | * | |
304fb0a1 | 24 | * MD5 without salt and magic strings - Added by Ramon de Carvalho and Rodrigo Rubira Branco |
94439e4e | 25 | */ |
26 | ||
f7f3304a | 27 | #include "squid.h" |
03901cf8 | 28 | #include "auth/basic/NCSA/crypt_md5.h" |
079b1d0f | 29 | #include "helper/protocol_defines.h" |
1fa9b1a7 | 30 | #include "rfc1738.h" |
81951d76 | 31 | |
4dd9db7d | 32 | #include <string> |
d75ae82c | 33 | #include <unordered_map> |
94439e4e | 34 | #if HAVE_SYS_STAT_H |
35 | #include <sys/stat.h> | |
36 | #endif | |
37 | #if HAVE_CRYPT_H | |
38 | #include <crypt.h> | |
39 | #endif | |
40 | ||
d75ae82c FC |
41 | typedef std::unordered_map<std::string, std::string> usermap_t; |
42 | usermap_t usermap; | |
94439e4e | 43 | |
44 | static void | |
45 | read_passwd_file(const char *passwdfile) | |
46 | { | |
47 | FILE *f; | |
3c18a677 | 48 | char buf[HELPER_INPUT_BUFFER]; |
94439e4e | 49 | char *user; |
50 | char *passwd; | |
d75ae82c FC |
51 | |
52 | usermap.clear(); | |
53 | //TODO: change to c++ streams | |
94439e4e | 54 | f = fopen(passwdfile, "r"); |
b69e9ffa AJ |
55 | if (!f) { |
56 | int xerrno = errno; | |
57 | fprintf(stderr, "FATAL: %s: %s\n", passwdfile, xstrerr(xerrno)); | |
24885773 | 58 | exit(EXIT_FAILURE); |
a2897c13 | 59 | } |
3c18a677 | 60 | unsigned int lineCount = 0; |
2ea57237 | 61 | buf[HELPER_INPUT_BUFFER-1] = '\0'; |
3c18a677 AJ |
62 | while (fgets(buf, sizeof(buf)-1, f) != NULL) { |
63 | ++lineCount; | |
26ac0430 AJ |
64 | if ((buf[0] == '#') || (buf[0] == ' ') || (buf[0] == '\t') || |
65 | (buf[0] == '\n')) | |
66 | continue; | |
67 | user = strtok(buf, ":\n\r"); | |
3c18a677 AJ |
68 | if (user == NULL) { |
69 | fprintf(stderr, "ERROR: Missing user name at %s line %d\n", passwdfile, lineCount); | |
70 | continue; | |
71 | } | |
26ac0430 AJ |
72 | passwd = strtok(NULL, ":\n\r"); |
73 | if ((strlen(user) > 0) && passwd) { | |
d75ae82c | 74 | usermap[user] = passwd; |
26ac0430 | 75 | } |
94439e4e | 76 | } |
77 | fclose(f); | |
78 | } | |
79 | ||
80 | int | |
81 | main(int argc, char **argv) | |
82 | { | |
83 | struct stat sb; | |
407e1c93 | 84 | time_t change_time = -1; |
43fed740 | 85 | char buf[HELPER_INPUT_BUFFER]; |
94439e4e | 86 | char *user, *passwd, *p; |
94439e4e | 87 | setbuf(stdout, NULL); |
88 | if (argc != 2) { | |
26ac0430 | 89 | fprintf(stderr, "Usage: ncsa_auth <passwordfile>\n"); |
24885773 | 90 | exit(EXIT_FAILURE); |
94439e4e | 91 | } |
92 | if (stat(argv[1], &sb) != 0) { | |
43fed740 | 93 | fprintf(stderr, "FATAL: cannot stat %s\n", argv[1]); |
24885773 | 94 | exit(EXIT_FAILURE); |
94439e4e | 95 | } |
43fed740 | 96 | while (fgets(buf, HELPER_INPUT_BUFFER, stdin) != NULL) { |
26ac0430 | 97 | if ((p = strchr(buf, '\n')) != NULL) |
f53969cc | 98 | *p = '\0'; /* strip \n */ |
26ac0430 AJ |
99 | if (stat(argv[1], &sb) == 0) { |
100 | if (sb.st_mtime != change_time) { | |
101 | read_passwd_file(argv[1]); | |
102 | change_time = sb.st_mtime; | |
103 | } | |
104 | } | |
105 | if ((user = strtok(buf, " ")) == NULL) { | |
43fed740 | 106 | SEND_ERR(""); |
26ac0430 AJ |
107 | continue; |
108 | } | |
109 | if ((passwd = strtok(NULL, "")) == NULL) { | |
43fed740 | 110 | SEND_ERR(""); |
26ac0430 AJ |
111 | continue; |
112 | } | |
113 | rfc1738_unescape(user); | |
114 | rfc1738_unescape(passwd); | |
d75ae82c FC |
115 | const auto userpassIterator = usermap.find(user); |
116 | if (userpassIterator == usermap.end()) { | |
43fed740 | 117 | SEND_ERR("No such user"); |
2df24144 AJ |
118 | continue; |
119 | } | |
d75ae82c | 120 | std::string stored_pass = userpassIterator->second; |
a4ae022c | 121 | const char *salted = stored_pass.c_str(); // locally stored version contains salt etc. |
d75ae82c | 122 | |
2df24144 | 123 | char *crypted = NULL; |
8655ee19 | 124 | #if HAVE_CRYPT |
f04d5d43 | 125 | size_t passwordLength = strlen(passwd); |
2df24144 AJ |
126 | // Bug 3831: given algorithms more secure than DES crypt() does not truncate, so we can ignore the bug 3107 length checks below |
127 | // '$1$' = MD5, '$2a$' = Blowfish, '$5$' = SHA256 (Linux), '$6$' = SHA256 (BSD) and SHA512 | |
a4ae022c AJ |
128 | if (passwordLength > 1 && salted[0] == '$' && |
129 | (crypted = crypt(passwd, salted)) && stored_pass == crypted) { | |
2df24144 AJ |
130 | SEND_OK(""); |
131 | continue; | |
132 | } | |
133 | // 'other' prefixes indicate DES algorithm. | |
a4ae022c | 134 | if (passwordLength <= 8 && (crypted = crypt(passwd, salted)) && stored_pass == crypted) { |
43fed740 | 135 | SEND_OK(""); |
2df24144 AJ |
136 | continue; |
137 | } | |
a4ae022c | 138 | if (passwordLength > 8 && (crypted = crypt(passwd, salted)) && stored_pass == crypted) { |
2d2772b0 AJ |
139 | // Bug 3107: crypt() DES functionality silently truncates long passwords. |
140 | SEND_ERR("Password too long. Only 8 characters accepted."); | |
2df24144 AJ |
141 | continue; |
142 | } | |
143 | ||
8655ee19 | 144 | #endif |
a4ae022c | 145 | if ( (crypted = crypt_md5(passwd, salted)) && stored_pass == crypted) { |
43fed740 | 146 | SEND_OK(""); |
2df24144 AJ |
147 | continue; |
148 | } | |
d75ae82c | 149 | if ( (crypted = md5sum(passwd)) && stored_pass == crypted) { |
43fed740 | 150 | SEND_OK(""); |
2df24144 | 151 | continue; |
26ac0430 | 152 | } |
2df24144 | 153 | SEND_ERR("Wrong password"); |
94439e4e | 154 | } |
24885773 | 155 | return EXIT_SUCCESS; |
94439e4e | 156 | } |
f53969cc | 157 |