Make some values unsigned to avoid gcc warnings.
[polipo.git] / auth.c
blob0ba5c2836df06475116cab89ca58afea94516c35
1 /*
2 Copyright (c) 2004-2006 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
23 #include "polipo.h"
25 int
26 buildClientAuthHeaders(AtomPtr url, char *word,
27 AtomPtr *message_return, AtomPtr *headers_return)
29 int code;
30 char *h;
31 AtomPtr message, headers;
32 if(urlIsLocal(url->string, url->length)) {
33 code = 401;
34 message = internAtomF("Server authentication %s", word);
35 h = "WWW-Authenticate";
36 } else {
37 code = 407;
38 message = internAtomF("Proxy authentication %s", word);
39 h = "Proxy-Authenticate";
41 headers = internAtomF("\r\n%s: Basic realm=\"%s\"",
42 h, authRealm->string);
43 if(message_return)
44 *message_return = message;
45 else
46 releaseAtom(message);
47 *headers_return = headers;
48 return code;
51 int
52 checkClientAuth(AtomPtr auth, AtomPtr url,
53 AtomPtr *message_return, AtomPtr *headers_return)
55 int code = 0;
56 AtomPtr message = NULL, headers = NULL;
58 if(authRealm == NULL || authCredentials == NULL)
59 return 0;
61 if(auth == NULL)
62 code = buildClientAuthHeaders(url, "required", &message, &headers);
63 else if(auth->length >= 6 || lwrcmp(auth->string, "basic ", 6) == 0) {
64 if(b64cmp(auth->string + 6, auth->length - 6,
65 authCredentials->string, authCredentials->length) == 0)
66 return 0;
67 code = buildClientAuthHeaders(url, "incorrect", &message, &headers);
68 } else {
69 code = buildClientAuthHeaders(url, NULL, NULL, &headers);
70 message = internAtom("Unexpected authentication scheme");
73 *message_return = message;
74 *headers_return = headers;
75 return code;
78 int
79 buildServerAuthHeaders(char* buf, int n, int size, AtomPtr authCredentials)
81 char authbuf[4 * 128 + 3];
82 int authlen;
84 if(authCredentials->length >= 3 * 128)
85 return -1;
86 authlen = b64cpy(authbuf, parentAuthCredentials->string,
87 parentAuthCredentials->length, 0);
88 n = snnprintf(buf, n, size, "\r\nProxy-Authorization: Basic ");
89 n = snnprint_n(buf, n, size, authbuf, authlen);
90 return n;