Annotation of libwww/Library/src/HTAABrow.c, revision 2.37
2.15 frystyk 1: /* HTAABrow.c
2.32 frystyk 2: ** BROWSER SIDE ACCESS AUTHORIZATION MODULE
2.15 frystyk 3: **
2.19 frystyk 4: ** (c) COPYRIGHT MIT 1995.
2.15 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
2.37 ! frystyk 6: ** @(#) $Id: HTAABrow.c,v 2.36 1996/07/08 19:10:35 frystyk Exp $
2.1 luotonen 7: **
2.32 frystyk 8: ** Contains code for parsing challenges and creating credentials for
2.36 frystyk 9: ** basic authentication schemes. See also the HTAAUtil module
2.32 frystyk 10: ** for how to handle other authentication schemes. You don't have to use
11: ** this code at all.
2.1 luotonen 12: **
13: ** AUTHORS:
14: ** AL Ari Luotonen luotonen@dxcern.cern.ch
2.32 frystyk 15: ** HFN Henrik Frystyk
2.1 luotonen 16: **
17: ** HISTORY:
2.5 luotonen 18: ** Oct 17 AL Made corrections suggested by marca:
19: ** Added if (!realm->username) return NULL;
20: ** Changed some ""s to NULLs.
2.33 frystyk 21: ** Now doing HT_CALLOC() to init uuencode source;
2.5 luotonen 22: ** otherwise HTUU_encode() reads uninitialized memory
23: ** every now and then (not a real bug but not pretty).
24: ** Corrected the formula for uuencode destination size.
2.32 frystyk 25: ** Feb 96 HFN Rewritten to make it scheme independent and based on
26: ** callback functions and an info structure
2.1 luotonen 27: */
28:
2.17 frystyk 29: /* Library include files */
2.27 frystyk 30: #include "WWWLib.h"
31: #include "HTAAUtil.h"
32: #include "HTAABrow.h" /* Implemented here */
2.1 luotonen 33:
2.36 frystyk 34: #define BASIC_AUTH "basic"
35:
2.32 frystyk 36: typedef struct _HTBasic { /* Basic challenge and credentials */
37: char * uid;
38: char * pw;
2.36 frystyk 39: BOOL first;
2.32 frystyk 40: } HTBasic;
41:
2.36 frystyk 42: /* ------------------------------------------------------------------------- */
43:
44: PRIVATE HTBasic * HTBasic_new()
45: {
46: HTBasic * me = NULL;
47: if ((me = (HTBasic *) HT_CALLOC(1, sizeof(HTBasic))) == NULL)
48: HT_OUTOFMEM("HTBasic_new");
49: me->first = YES;
50: return me;
51: }
2.7 luotonen 52:
2.36 frystyk 53: /* HTBasic_delete
54: ** --------------
55: ** Deletes a "basic" information object
56: */
57: PUBLIC int HTBasic_delete (void * context)
58: {
59: HTBasic * basic = (HTBasic *) context;
60: if (basic) {
61: HT_FREE(basic->uid);
62: HT_FREE(basic->pw);
63: HT_FREE(basic);
64: return YES;
65: }
66: return NO;
67: }
2.1 luotonen 68:
2.32 frystyk 69: /*
70: ** Create a protection template for the files
71: ** in the same directory as the given file
72: ** Returns a template matching docname, and other files in that directory.
73: **
74: ** E.g. /foo/bar/x.html => /foo/bar/ *
75: ** ^
76: ** Space only to prevent it from
77: ** being a comment marker here,
78: ** there really isn't any space.
2.1 luotonen 79: */
2.33 frystyk 80: PRIVATE char * make_template (const char * docname)
2.1 luotonen 81: {
2.32 frystyk 82: char *tmplate = NULL;
83: char *slash = NULL;
84: if (docname) {
85: StrAllocCopy(tmplate, docname);
86: slash = strrchr(tmplate, '/');
87: if (slash) slash++;
88: else slash = tmplate;
89: *slash = '\0';
90: StrAllocCat(tmplate, "*");
91: }
92: else StrAllocCopy(tmplate, "*");
93: if (AUTH_TRACE)
94: HTTrace("Template.... Made template `%s' for file `%s'\n",
95: tmplate, docname);
96: return tmplate;
2.1 luotonen 97: }
98:
2.32 frystyk 99: /*
100: ** Make basic authentication scheme credentials and register this
101: ** information in the request object as credentials. They will then
102: ** be included in the request header. An example is
103: **
104: ** "Basic AkRDIhEF8sdEgs72F73bfaS=="
105: **
2.36 frystyk 106: ** Returns HT_OK or HT_ERROR
2.32 frystyk 107: */
2.36 frystyk 108: PRIVATE BOOL basic_credentials (HTRequest * request, HTBasic * basic)
2.32 frystyk 109: {
110: if (request && basic) {
111: char * cleartext = NULL;
112: char * cipher = NULL;
113: int cl_len = strlen(basic->uid ? basic->uid : "") +
114: strlen(basic->pw ? basic->pw : "") + 3;
2.37 ! frystyk 115: int ci_len = 4 * (((cl_len+2)/3) + 1);
! 116: if ((cleartext = (char *) HT_CALLOC(1, cl_len)) == NULL)
2.32 frystyk 117: HT_OUTOFMEM("basic_credentials");
118: *cleartext = '\0';
119: if (basic->uid) strcpy(cleartext, basic->uid);
120: strcat(cleartext, ":");
121: if (basic->pw) strcat(cleartext, basic->pw);
2.37 ! frystyk 122: if ((cipher = (char *) HT_CALLOC(1, ci_len + 3)) == NULL)
2.32 frystyk 123: HT_OUTOFMEM("basic_credentials");
2.37 ! frystyk 124: HTUU_encode((unsigned char *) cleartext, strlen(cleartext), cipher);
2.1 luotonen 125:
2.32 frystyk 126: /* Create the credentials and assign them to the request object */
127: {
2.37 ! frystyk 128: int cr_len = strlen("basic") + ci_len + 3;
2.32 frystyk 129: char * cookie = (char *) HT_MALLOC(cr_len+1);
130: if (!cookie) HT_OUTOFMEM("basic_credentials");
131: strcpy(cookie, "Basic ");
132: strcat(cookie, cipher);
2.37 ! frystyk 133: if (AUTH_TRACE) HTTrace("Basic Cookie `%s\'\n", cookie);
2.36 frystyk 134: HTRequest_addCredentials(request, "Authorization", cookie);
2.32 frystyk 135: HT_FREE(cookie);
2.1 luotonen 136: }
2.32 frystyk 137: HT_FREE(cleartext);
138: HT_FREE(cipher);
2.36 frystyk 139: return HT_OK;
2.32 frystyk 140: }
2.36 frystyk 141: return HT_ERROR;
2.1 luotonen 142: }
143:
2.32 frystyk 144: /*
145: ** Prompt the user for username and password.
146: ** Returns YES if user name was typed in, else NO
2.1 luotonen 147: */
2.36 frystyk 148: PRIVATE int prompt_user (HTRequest * request, const char * realm,
149: HTBasic * basic)
2.1 luotonen 150: {
2.32 frystyk 151: HTAlertCallback * cbf = HTAlert_find(HT_A_USER_PW);
152: if (request && cbf) {
153: HTAlertPar * reply = HTAlert_newReply();
2.36 frystyk 154: BOOL res = (*cbf)(request, HT_A_USER_PW,HT_MSG_NULL,
155: basic->uid, (char *) realm, reply);
2.32 frystyk 156: if (res) {
2.36 frystyk 157: HT_FREE(basic->uid);
158: HT_FREE(basic->pw);
159: basic->uid = HTAlert_replyMessage(reply);
160: basic->pw = HTAlert_replySecret(reply);
161: basic->first = NO;
2.32 frystyk 162: }
163: HTAlert_deleteReply(reply);
2.36 frystyk 164: return res ? HT_OK : HT_ERROR;
2.1 luotonen 165: }
2.36 frystyk 166: return HT_OK;
2.1 luotonen 167: }
168:
2.32 frystyk 169: /* HTBasic_generate
170: ** ----------------
171: ** This function generates "basic" credentials for the challenge found in
172: ** the authentication information base for this request. The result is
173: ** stored as an association list in the request object.
174: ** This is a callback function for the AA handler.
175: */
2.36 frystyk 176: PUBLIC int HTBasic_generate (HTRequest * request, void * context, int status)
2.32 frystyk 177: {
2.36 frystyk 178: HTBasic * basic = (HTBasic *) context;
179: if (request) {
180: const char * realm = HTRequest_realm(request);
181:
182: /* If we don't have a basic context then add a new one to the tree */
183: if (!basic) {
184: char * url = HTAnchor_physical(HTRequest_anchor(request));
185: basic = HTBasic_new();
186: HTAA_addNode(BASIC_AUTH, realm, url, basic);
187: }
188:
2.32 frystyk 189: /*
2.36 frystyk 190: ** If we have a set of credentials (or the user provides a new set)
191: ** then store it in the request object as the credentials
2.32 frystyk 192: */
2.36 frystyk 193: if (basic->uid || prompt_user(request, realm, basic) == HT_OK)
194: return basic_credentials(request, basic);
2.37 ! frystyk 195: else
! 196: return HT_ERROR;
2.1 luotonen 197: }
2.36 frystyk 198: return HT_OK;
2.1 luotonen 199: }
200:
2.32 frystyk 201: /* HTBasic_parse
202: ** -------------
203: ** This function parses the contents of a "basic" challenge
204: ** and stores the challenge in our authentication information datebase.
205: ** We also store the realm in the request object which will help finding
206: ** the right set of credentials to generate.
207: ** The function is a callback function for the AA handler.
208: */
2.36 frystyk 209: PUBLIC int HTBasic_parse (HTRequest * request, void * context, int status)
2.32 frystyk 210: {
2.36 frystyk 211: HTAssocList * challenge = HTRequest_challenge(request);
212: if (request && challenge) {
213: char * p = HTAssocList_findObject(challenge, BASIC_AUTH);
214: char * realm = HTNextField(&p);
215: char * rm = HTNextField(&p);
2.32 frystyk 216: /*
2.36 frystyk 217: ** If valid challenge then make a template for the resource and
218: ** store this information in our authentication URL Tree
2.32 frystyk 219: */
2.36 frystyk 220: if (realm && !strcasecomp(realm, "realm") && rm) {
221: char * url = HTAnchor_physical(HTRequest_anchor(request));
222: char * tmplate = make_template(url);
223: HTBasic * basic = HTBasic_new();
224: if (AUTH_TRACE) HTTrace("Basic Parse. Realm `%s\' found\n", rm);
225: HTAA_addNode(BASIC_AUTH, rm, tmplate, basic);
226: HTRequest_setRealm(request, rm);
227: HT_FREE(tmplate);
2.1 luotonen 228: }
2.36 frystyk 229: return HT_OK;
2.1 luotonen 230: }
2.36 frystyk 231: if (AUTH_TRACE) HTTrace("Auth........ No challenges found\n");
232: return HT_OK;
2.7 luotonen 233: }
2.1 luotonen 234:
235:
Webmaster