auth.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
  3. * All rights reserved
  4. *
  5. * "THE BEER-WARE LICENSE" (Revision 42):
  6. * Sergey Lyubka wrote this file. As long as you retain this notice you
  7. * can do whatever you want with this stuff. If we meet some day, and you think
  8. * this stuff is worth it, you can buy me a beer in return.
  9. */
  10. #include "defs.h"
  11. #if !defined(NO_AUTH)
  12. /*
  13. * Stringify binary data. Output buffer must be twice as big as input,
  14. * because each byte takes 2 bytes in string representation
  15. */
  16. static void
  17. bin2str(char *to, const unsigned char *p, size_t len)
  18. {
  19. const char *hex = "0123456789abcdef";
  20. for (;len--; p++) {
  21. *to++ = hex[p[0] >> 4];
  22. *to++ = hex[p[0] & 0x0f];
  23. }
  24. }
  25. /*
  26. * Return stringified MD5 hash for list of vectors.
  27. * buf must point to at least 32-bytes long buffer
  28. */
  29. static void
  30. md5(char *buf, ...)
  31. {
  32. unsigned char hash[16];
  33. const struct vec *v;
  34. va_list ap;
  35. MD5_CTX ctx;
  36. int i;
  37. MD5Init(&ctx);
  38. va_start(ap, buf);
  39. for (i = 0; (v = va_arg(ap, const struct vec *)) != NULL; i++) {
  40. assert(v->len >= 0);
  41. if (v->len == 0)
  42. continue;
  43. if (i > 0)
  44. MD5Update(&ctx, (unsigned char *) ":", 1);
  45. MD5Update(&ctx,(unsigned char *)v->ptr,(unsigned int)v->len);
  46. }
  47. va_end(ap);
  48. MD5Final(hash, &ctx);
  49. bin2str(buf, hash, sizeof(hash));
  50. }
  51. /*
  52. * Compare to vectors. Return 1 if they are equal
  53. */
  54. static int
  55. vcmp(const struct vec *v1, const struct vec *v2)
  56. {
  57. return (v1->len == v2->len && !memcmp(v1->ptr, v2->ptr, v1->len));
  58. }
  59. struct digest {
  60. struct vec user;
  61. struct vec uri;
  62. struct vec nonce;
  63. struct vec cnonce;
  64. struct vec resp;
  65. struct vec qop;
  66. struct vec nc;
  67. };
  68. static const struct auth_keyword {
  69. size_t offset;
  70. struct vec vec;
  71. } known_auth_keywords[] = {
  72. {offsetof(struct digest, user), {"username=", 9}},
  73. {offsetof(struct digest, cnonce), {"cnonce=", 7}},
  74. {offsetof(struct digest, resp), {"response=", 9}},
  75. {offsetof(struct digest, uri), {"uri=", 4}},
  76. {offsetof(struct digest, qop), {"qop=", 4}},
  77. {offsetof(struct digest, nc), {"nc=", 3}},
  78. {offsetof(struct digest, nonce), {"nonce=", 6}},
  79. {0, {NULL, 0}}
  80. };
  81. static void
  82. parse_authorization_header(const struct vec *h, struct digest *dig)
  83. {
  84. const unsigned char *p, *e, *s;
  85. struct vec *v, vec;
  86. const struct auth_keyword *kw;
  87. (void) memset(dig, 0, sizeof(*dig));
  88. p = (unsigned char *) h->ptr + 7;
  89. e = (unsigned char *) h->ptr + h->len;
  90. while (p < e) {
  91. /* Skip spaces */
  92. while (p < e && (*p == ' ' || *p == ','))
  93. p++;
  94. /* Skip to "=" */
  95. for (s = p; s < e && *s != '='; )
  96. s++;
  97. s++;
  98. /* Is it known keyword ? */
  99. for (kw = known_auth_keywords; kw->vec.len > 0; kw++)
  100. if (kw->vec.len <= s - p &&
  101. !memcmp(p, kw->vec.ptr, kw->vec.len))
  102. break;
  103. if (kw->vec.len == 0)
  104. v = &vec; /* Dummy placeholder */
  105. else
  106. v = (struct vec *) ((char *) dig + kw->offset);
  107. if (*s == '"') {
  108. p = ++s;
  109. while (p < e && *p != '"')
  110. p++;
  111. } else {
  112. p = s;
  113. while (p < e && *p != ' ' && *p != ',')
  114. p++;
  115. }
  116. v->ptr = (char *) s;
  117. v->len = p - s;
  118. if (*p == '"')
  119. p++;
  120. DBG(("auth field [%.*s]", v->len, v->ptr));
  121. }
  122. }
  123. /*
  124. * Check the user's password, return 1 if OK
  125. */
  126. static int
  127. check_password(int method, const struct vec *ha1, const struct digest *digest)
  128. {
  129. char a2[32], resp[32];
  130. struct vec vec_a2;
  131. /* XXX Due to a bug in MSIE, we do not compare the URI */
  132. /* Also, we do not check for authentication timeout */
  133. if (/*strcmp(dig->uri, c->ouri) != 0 || */
  134. digest->resp.len != 32 /*||
  135. now - strtoul(dig->nonce, NULL, 10) > 3600 */)
  136. return (0);
  137. md5(a2, &_shttpd_known_http_methods[method], &digest->uri, NULL);
  138. vec_a2.ptr = a2;
  139. vec_a2.len = sizeof(a2);
  140. md5(resp, ha1, &digest->nonce, &digest->nc,
  141. &digest->cnonce, &digest->qop, &vec_a2, NULL);
  142. DBG(("%s: uri [%.*s] expected_resp [%.*s] resp [%.*s]",
  143. "check_password", digest->uri.len, digest->uri.ptr,
  144. 32, resp, digest->resp.len, digest->resp.ptr));
  145. return (!memcmp(resp, digest->resp.ptr, 32));
  146. }
  147. static FILE *
  148. open_auth_file(struct shttpd_ctx *ctx, const char *path)
  149. {
  150. char name[FILENAME_MAX];
  151. const char *p, *e;
  152. FILE *fp = NULL;
  153. int fd;
  154. if (ctx->options[OPT_AUTH_GPASSWD] != NULL) {
  155. /* Use global passwords file */
  156. _shttpd_snprintf(name, sizeof(name), "%s",
  157. ctx->options[OPT_AUTH_GPASSWD]);
  158. } else {
  159. /*
  160. * Try to find .htpasswd in requested directory.
  161. * Given the path, create the path to .htpasswd file
  162. * in the same directory. Find the right-most
  163. * directory separator character first. That would be the
  164. * directory name. If directory separator character is not
  165. * found, 'e' will point to 'p'.
  166. */
  167. for (p = path, e = p + strlen(p) - 1; e > p; e--)
  168. if (IS_DIRSEP_CHAR(*e))
  169. break;
  170. /*
  171. * Make up the path by concatenating directory name and
  172. * .htpasswd file name.
  173. */
  174. (void) _shttpd_snprintf(name, sizeof(name), "%.*s/%s",
  175. (int) (e - p), p, HTPASSWD);
  176. }
  177. if ((fd = _shttpd_open(name, O_RDONLY, 0)) == -1) {
  178. DBG(("open_auth_file: open(%s)", name));
  179. } else if ((fp = fdopen(fd, "r")) == NULL) {
  180. DBG(("open_auth_file: fdopen(%s)", name));
  181. (void) close(fd);
  182. }
  183. return (fp);
  184. }
  185. /*
  186. * Parse the line from htpasswd file. Line should be in form of
  187. * "user:domain:ha1". Fill in the vector values. Return 1 if successful.
  188. */
  189. static int
  190. parse_htpasswd_line(const char *s, struct vec *user,
  191. struct vec *domain, struct vec *ha1)
  192. {
  193. user->len = domain->len = ha1->len = 0;
  194. for (user->ptr = s; *s != '\0' && *s != ':'; s++, user->len++);
  195. if (*s++ != ':')
  196. return (0);
  197. for (domain->ptr = s; *s != '\0' && *s != ':'; s++, domain->len++);
  198. if (*s++ != ':')
  199. return (0);
  200. for (ha1->ptr = s; *s != '\0' && !isspace(* (unsigned char *) s);
  201. s++, ha1->len++);
  202. DBG(("parse_htpasswd_line: [%.*s] [%.*s] [%.*s]", user->len, user->ptr,
  203. domain->len, domain->ptr, ha1->len, ha1->ptr));
  204. return (user->len > 0 && domain->len > 0 && ha1->len > 0);
  205. }
  206. /*
  207. * Authorize against the opened passwords file. Return 1 if authorized.
  208. */
  209. static int
  210. authorize(struct conn *c, FILE *fp)
  211. {
  212. struct vec *auth_vec = &c->ch.auth.v_vec;
  213. struct vec *user_vec = &c->ch.user.v_vec;
  214. struct vec user, domain, ha1;
  215. struct digest digest;
  216. int ok = 0;
  217. char line[256];
  218. if (auth_vec->len > 20 &&
  219. !_shttpd_strncasecmp(auth_vec->ptr, "Digest ", 7)) {
  220. parse_authorization_header(auth_vec, &digest);
  221. *user_vec = digest.user;
  222. while (fgets(line, sizeof(line), fp) != NULL) {
  223. if (!parse_htpasswd_line(line, &user, &domain, &ha1))
  224. continue;
  225. DBG(("[%.*s] [%.*s] [%.*s]", user.len, user.ptr,
  226. domain.len, domain.ptr, ha1.len, ha1.ptr));
  227. if (vcmp(user_vec, &user) &&
  228. !memcmp(c->ctx->options[OPT_AUTH_REALM],
  229. domain.ptr, domain.len)) {
  230. ok = check_password(c->method, &ha1, &digest);
  231. break;
  232. }
  233. }
  234. }
  235. return (ok);
  236. }
  237. int
  238. _shttpd_check_authorization(struct conn *c, const char *path)
  239. {
  240. FILE *fp = NULL;
  241. int len, n, authorized = 1;
  242. const char *p, *s = c->ctx->options[OPT_PROTECT];
  243. char protected_path[FILENAME_MAX];
  244. FOR_EACH_WORD_IN_LIST(s, len) {
  245. if ((p = memchr(s, '=', len)) == NULL || p >= s + len || p == s)
  246. continue;
  247. if (!memcmp(c->uri, s, p - s)) {
  248. n = s + len - p;
  249. if (n > (int) sizeof(protected_path) - 1)
  250. n = sizeof(protected_path) - 1;
  251. _shttpd_strlcpy(protected_path, p + 1, n);
  252. if ((fp = fopen(protected_path, "r")) == NULL)
  253. _shttpd_elog(E_LOG, c,
  254. "check_auth: cannot open %s: %s",
  255. protected_path, strerror(errno));
  256. break;
  257. }
  258. }
  259. if (fp == NULL)
  260. fp = open_auth_file(c->ctx, path);
  261. if (fp != NULL) {
  262. authorized = authorize(c, fp);
  263. (void) fclose(fp);
  264. }
  265. return (authorized);
  266. }
  267. int
  268. _shttpd_is_authorized_for_put(struct conn *c)
  269. {
  270. FILE *fp;
  271. int ret = 0;
  272. if ((fp = fopen(c->ctx->options[OPT_AUTH_PUT], "r")) != NULL) {
  273. ret = authorize(c, fp);
  274. (void) fclose(fp);
  275. }
  276. return (ret);
  277. }
  278. void
  279. _shttpd_send_authorization_request(struct conn *c)
  280. {
  281. char buf[512];
  282. (void) _shttpd_snprintf(buf, sizeof(buf), "Unauthorized\r\n"
  283. "WWW-Authenticate: Digest qop=\"auth\", realm=\"%s\", "
  284. "nonce=\"%lu\"", c->ctx->options[OPT_AUTH_REALM],
  285. (unsigned long) _shttpd_current_time);
  286. _shttpd_send_server_error(c, 401, buf);
  287. }
  288. /*
  289. * Edit the passwords file.
  290. */
  291. int
  292. _shttpd_edit_passwords(const char *fname, const char *domain,
  293. const char *user, const char *pass)
  294. {
  295. int ret = EXIT_SUCCESS, found = 0;
  296. struct vec u, d, p;
  297. char line[512], tmp[FILENAME_MAX], ha1[32];
  298. FILE *fp = NULL, *fp2 = NULL;
  299. (void) _shttpd_snprintf(tmp, sizeof(tmp), "%s.tmp", fname);
  300. /* Create the file if does not exist */
  301. if ((fp = fopen(fname, "a+")))
  302. (void) fclose(fp);
  303. /* Open the given file and temporary file */
  304. if ((fp = fopen(fname, "r")) == NULL)
  305. _shttpd_elog(E_FATAL, NULL,
  306. "Cannot open %s: %s", fname, strerror(errno));
  307. else if ((fp2 = fopen(tmp, "w+")) == NULL)
  308. _shttpd_elog(E_FATAL, NULL,
  309. "Cannot open %s: %s", tmp, strerror(errno));
  310. p.ptr = pass;
  311. p.len = strlen(pass);
  312. /* Copy the stuff to temporary file */
  313. while (fgets(line, sizeof(line), fp) != NULL) {
  314. u.ptr = line;
  315. if ((d.ptr = strchr(line, ':')) == NULL)
  316. continue;
  317. u.len = d.ptr - u.ptr;
  318. d.ptr++;
  319. if (strchr(d.ptr, ':') == NULL)
  320. continue;
  321. d.len = strchr(d.ptr, ':') - d.ptr;
  322. if ((int) strlen(user) == u.len &&
  323. !memcmp(user, u.ptr, u.len) &&
  324. (int) strlen(domain) == d.len &&
  325. !memcmp(domain, d.ptr, d.len)) {
  326. found++;
  327. md5(ha1, &u, &d, &p, NULL);
  328. (void) fprintf(fp2, "%s:%s:%.32s\n", user, domain, ha1);
  329. } else {
  330. (void) fprintf(fp2, "%s", line);
  331. }
  332. }
  333. /* If new user, just add it */
  334. if (found == 0) {
  335. u.ptr = user; u.len = strlen(user);
  336. d.ptr = domain; d.len = strlen(domain);
  337. md5(ha1, &u, &d, &p, NULL);
  338. (void) fprintf(fp2, "%s:%s:%.32s\n", user, domain, ha1);
  339. }
  340. /* Close files */
  341. (void) fclose(fp);
  342. (void) fclose(fp2);
  343. /* Put the temp file in place of real file */
  344. (void) _shttpd_remove(fname);
  345. (void) _shttpd_rename(tmp, fname);
  346. return (ret);
  347. }
  348. #endif /* NO_AUTH */