| 1 |
/* |
| 2 |
* $LynxId: HTAABrow.c,v 1.45 2025/01/07 22:55:07 tom Exp $ |
| 3 |
* |
| 4 |
* MODULE HTAABrow.c |
| 5 |
* BROWSER SIDE ACCESS AUTHORIZATION MODULE |
| 6 |
* |
| 7 |
* Contains the code for keeping track on server hostnames, |
| 8 |
* port numbers, scheme names, usernames, passwords |
| 9 |
* (and servers' public keys). |
| 10 |
* |
| 11 |
* IMPORTANT: |
| 12 |
* Routines in this module use dynamic allocation, but free |
| 13 |
* automatically all the memory reserved by them. |
| 14 |
* |
| 15 |
* Therefore the caller never has to (and never should) |
| 16 |
* free() any object returned by these functions. |
| 17 |
* |
| 18 |
* Therefore also all the strings returned by this package |
| 19 |
* are only valid until the next call to the same function |
| 20 |
* is made. This approach is selected, because of the nature |
| 21 |
* of access authorization: no string returned by the package |
| 22 |
* needs to be valid longer than until the next call. |
| 23 |
* |
| 24 |
* This also makes it easy to plug the AA package in: |
| 25 |
* you don't have to ponder whether to free() something |
| 26 |
* here or is it done somewhere else (because it is always |
| 27 |
* done somewhere else). |
| 28 |
* |
| 29 |
* The strings that the package needs to store are copied |
| 30 |
* so the original strings given as parameters to AA |
| 31 |
* functions may be freed or modified with no side effects. |
| 32 |
* |
| 33 |
* The AA package does not free() anything else than what |
| 34 |
* it has itself allocated. |
| 35 |
* |
| 36 |
* AUTHORS: |
| 37 |
* AL Ari Luotonen [email protected] |
| 38 |
* |
| 39 |
* HISTORY: |
| 40 |
* Oct 17 AL Made corrections suggested by marca: |
| 41 |
* Added if (!realm->username) return NULL; |
| 42 |
* Changed some ""s to NULLs. |
| 43 |
* Now doing calloc() to init uuencode source; |
| 44 |
* otherwise HTUU_encode() reads uninitialized memory |
| 45 |
* every now and then (not a real bug but not pretty). |
| 46 |
* Corrected the formula for uuencode destination size. |
| 47 |
* |
| 48 |
* 28 Apr 1997 AJL Do Proxy Authorisation. |
| 49 |
* |
| 50 |
* BUGS: |
| 51 |
* |
| 52 |
* |
| 53 |
*/ |
| 54 |
|
| 55 |
#include <HTUtils.h> |
| 56 |
#include <HTString.h> |
| 57 |
#include <HTParse.h> /* URL parsing function */ |
| 58 |
#include <HTList.h> /* HTList object */ |
| 59 |
#include <HTAlert.h> /* HTConfirm(), HTPrompt() */ |
| 60 |
#include <HTAAUtil.h> /* AA common to both sides */ |
| 61 |
#include <HTAssoc.h> /* Assoc list */ |
| 62 |
#include <HTAccess.h> /* Are we using an HTTP gateway? */ |
| 63 |
#include <HTAABrow.h> /* Implemented here */ |
| 64 |
#include <HTUU.h> /* Uuencoding and uudecoding */ |
| 65 |
|
| 66 |
#include <LYLeaks.h> |
| 67 |
|
| 68 |
/* |
| 69 |
* Local datatype definitions |
| 70 |
* |
| 71 |
* HTAAServer contains all the information about one server. |
| 72 |
*/ |
| 73 |
typedef struct { |
| 74 |
|
| 75 |
char *hostname; /* Host's name */ |
| 76 |
int portnumber; /* Port number */ |
| 77 |
BOOL IsProxy; /* Is it a proxy? */ |
| 78 |
HTList *setups; /* List of protection setups |
| 79 |
on this server; i.e., valid |
| 80 |
authentication schemes and |
| 81 |
templates when to use them. |
| 82 |
This is actually a list of |
| 83 |
HTAASetup objects. */ |
| 84 |
HTList *realms; /* Information about passwords */ |
| 85 |
} HTAAServer; |
| 86 |
|
| 87 |
/* |
| 88 |
* HTAASetup contains information about one server's one |
| 89 |
* protected tree of documents. |
| 90 |
*/ |
| 91 |
typedef struct { |
| 92 |
HTAAServer *server; /* Which server serves this tree */ |
| 93 |
char *ctemplate; /* Template for this tree */ |
| 94 |
HTList *valid_schemes; /* Valid authentic.schemes */ |
| 95 |
HTAssocList **scheme_specifics; /* Scheme specific params */ |
| 96 |
BOOL retry; /* Failed last time -- reprompt (or whatever) */ |
| 97 |
} HTAASetup; |
| 98 |
|
| 99 |
/* |
| 100 |
* Information about usernames and passwords in |
| 101 |
* Basic and Pubkey authentication schemes; |
| 102 |
*/ |
| 103 |
typedef struct { |
| 104 |
char *realmname; /* Password domain name */ |
| 105 |
char *username; /* Username in that domain */ |
| 106 |
char *password; /* Corresponding password */ |
| 107 |
} HTAARealm; |
| 108 |
|
| 109 |
/* |
| 110 |
* To free off all globals. - FM |
| 111 |
*/ |
| 112 |
static void free_HTAAGlobals(void); |
| 113 |
static BOOL free_HTAAGlobalsSet = FALSE; |
| 114 |
static char *HTAA_composeAuthResult = NULL; |
| 115 |
static char *compose_auth_stringResult = NULL; /* Uuencoded presentation */ |
| 116 |
|
| 117 |
/* |
| 118 |
* Module-wide global variables |
| 119 |
*/ |
| 120 |
static HTList *server_table = NULL; /* Browser's info about servers */ |
| 121 |
static char *secret_key = NULL; /* Browser's latest secret key */ |
| 122 |
static HTAASetup *current_setup = NULL; /* The server setup we are currently */ |
| 123 |
|
| 124 |
/* talking to */ |
| 125 |
static char *current_hostname = NULL; /* The server's name and portnumber */ |
| 126 |
static int current_portnumber = 80; /* where we are currently trying to */ |
| 127 |
|
| 128 |
/* connect. */ |
| 129 |
static char *current_docname = NULL; /* The document's name we are */ |
| 130 |
|
| 131 |
/* trying to access. */ |
| 132 |
static char *HTAAForwardAuth = NULL; /* Authorization: line to forward */ |
| 133 |
|
| 134 |
/* (used by gateway httpds) */ |
| 135 |
static HTAASetup *proxy_setup = NULL; /* Same as above, but for Proxy -AJL */ |
| 136 |
static char *proxy_hostname = NULL; |
| 137 |
static char *proxy_docname = NULL; |
| 138 |
static int proxy_portnumber = 80; |
| 139 |
|
| 140 |
/*** HTAAForwardAuth for enabling gateway-httpds to forward Authorization ***/ |
| 141 |
|
| 142 |
void HTAAForwardAuth_set(const char *scheme_name, |
| 143 |
const char *scheme_specifics) |
| 144 |
{ |
| 145 |
size_t len = (20 |
| 146 |
+ (scheme_name ? strlen(scheme_name) : 0) |
| 147 |
+ (scheme_specifics ? strlen(scheme_specifics) : 0)); |
| 148 |
|
| 149 |
FREE(HTAAForwardAuth); |
| 150 |
if ((HTAAForwardAuth = typecallocn(char, len)) == NULL) |
| 151 |
outofmem(__FILE__, "HTAAForwardAuth_set"); |
| 152 |
|
| 153 |
strcpy(HTAAForwardAuth, "Authorization: "); |
| 154 |
if (scheme_name) { |
| 155 |
strcat(HTAAForwardAuth, scheme_name); |
| 156 |
strcat(HTAAForwardAuth, " "); |
| 157 |
if (scheme_specifics) { |
| 158 |
strcat(HTAAForwardAuth, scheme_specifics); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void HTAAForwardAuth_reset(void) |
| 164 |
{ |
| 165 |
FREE(HTAAForwardAuth); |
| 166 |
} |
| 167 |
|
| 168 |
/**************************** HTAAServer ***********************************/ |
| 169 |
|
| 170 |
static void HTAASetup_delete(HTAASetup * killme); /* Forward */ |
| 171 |
|
| 172 |
/* static HTAAServer_new() |
| 173 |
* ALLOCATE A NEW NODE TO HOLD SERVER INFO |
| 174 |
* AND ADD IT TO THE LIST OF SERVERS |
| 175 |
* ON ENTRY: |
| 176 |
* hostname is the name of the host that the server |
| 177 |
* is running in. |
| 178 |
* portnumber is the portnumber which the server listens. |
| 179 |
* IsProxy should be TRUE if this is a proxy. |
| 180 |
* |
| 181 |
* ON EXIT: |
| 182 |
* returns the newly-allocated node with all the strings |
| 183 |
* duplicated. |
| 184 |
* Strings will be automatically freed by |
| 185 |
* the function HTAAServer_delete(), which also |
| 186 |
* frees the node itself. |
| 187 |
*/ |
| 188 |
static HTAAServer *HTAAServer_new(const char *hostname, |
| 189 |
int portnumber, |
| 190 |
int IsProxy) |
| 191 |
{ |
| 192 |
HTAAServer *server; |
| 193 |
|
| 194 |
if ((server = typecalloc(HTAAServer)) == NULL) |
| 195 |
outofmem(__FILE__, "HTAAServer_new"); |
| 196 |
|
| 197 |
server->hostname = NULL; |
| 198 |
server->portnumber = (portnumber > 0 ? portnumber : 80); |
| 199 |
server->IsProxy = (BOOLEAN) IsProxy; |
| 200 |
server->setups = HTList_new(); |
| 201 |
server->realms = HTList_new(); |
| 202 |
|
| 203 |
if (hostname) |
| 204 |
StrAllocCopy(server->hostname, hostname); |
| 205 |
|
| 206 |
if (!server_table) |
| 207 |
server_table = HTList_new(); |
| 208 |
|
| 209 |
HTList_addObject(server_table, (void *) server); |
| 210 |
|
| 211 |
return server; |
| 212 |
} |
| 213 |
|
| 214 |
/* static HTAAServer_delete() |
| 215 |
* |
| 216 |
* DELETE THE ENTRY FOR THE SERVER FROM THE HOST TABLE, |
| 217 |
* AND FREE THE MEMORY USED BY IT. |
| 218 |
* |
| 219 |
* ON ENTRY: |
| 220 |
* killme points to the HTAAServer to be freed. |
| 221 |
* |
| 222 |
* ON EXIT: |
| 223 |
* returns nothing. |
| 224 |
*/ |
| 225 |
static void HTAAServer_delete(HTAAServer *killme) |
| 226 |
{ |
| 227 |
int n, i; |
| 228 |
HTAASetup *setup; |
| 229 |
HTAARealm *realm; |
| 230 |
HTList *cur; |
| 231 |
|
| 232 |
if (killme) { |
| 233 |
if (killme->setups != NULL) { |
| 234 |
n = HTList_count(killme->setups); |
| 235 |
for (i = (n - 1); i >= 0; i--) { |
| 236 |
if ((setup = (HTAASetup *) HTList_objectAt(killme->setups, |
| 237 |
i)) != NULL) { |
| 238 |
HTAASetup_delete(setup); |
| 239 |
setup = NULL; |
| 240 |
} |
| 241 |
} |
| 242 |
HTList_delete(killme->setups); |
| 243 |
killme->setups = NULL; |
| 244 |
} |
| 245 |
|
| 246 |
cur = killme->realms; |
| 247 |
while (NULL != (realm = (HTAARealm *) HTList_nextObject(cur))) { |
| 248 |
FREE(realm->realmname); |
| 249 |
FREE(realm->username); |
| 250 |
FREE(realm->password); |
| 251 |
FREE(realm); |
| 252 |
} |
| 253 |
HTList_delete(killme->realms); |
| 254 |
killme->realms = NULL; |
| 255 |
|
| 256 |
FREE(killme->hostname); |
| 257 |
|
| 258 |
HTList_removeObject(server_table, (void *) killme); |
| 259 |
FREE(killme); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/* static HTAAServer_lookup() |
| 264 |
* LOOK UP SERVER BY HOSTNAME AND PORTNUMBER |
| 265 |
* ON ENTRY: |
| 266 |
* hostname obvious. |
| 267 |
* portnumber if non-positive defaults to 80. |
| 268 |
* IsProxy should be TRUE if this is a proxy. |
| 269 |
* |
| 270 |
* Looks up the server in the module-global server_table. |
| 271 |
* |
| 272 |
* ON EXIT: |
| 273 |
* returns pointer to a HTAAServer structure |
| 274 |
* representing the looked-up server. |
| 275 |
* NULL, if not found. |
| 276 |
*/ |
| 277 |
static HTAAServer *HTAAServer_lookup(const char *hostname, |
| 278 |
int portnumber, |
| 279 |
int IsProxy) |
| 280 |
{ |
| 281 |
if (hostname) { |
| 282 |
HTList *cur = server_table; |
| 283 |
HTAAServer *server; |
| 284 |
|
| 285 |
if (portnumber <= 0) |
| 286 |
portnumber = 80; |
| 287 |
|
| 288 |
while (NULL != (server = (HTAAServer *) HTList_nextObject(cur))) { |
| 289 |
if (server->portnumber == portnumber && |
| 290 |
0 == strcmp(server->hostname, hostname) && |
| 291 |
server->IsProxy == IsProxy) |
| 292 |
return server; |
| 293 |
} |
| 294 |
} |
| 295 |
return NULL; /* NULL parameter, or not found */ |
| 296 |
} |
| 297 |
|
| 298 |
/*************************** HTAASetup *******************************/ |
| 299 |
|
| 300 |
/* static HTAASetup_lookup() |
| 301 |
* FIGURE OUT WHICH AUTHENTICATION SETUP THE SERVER |
| 302 |
* IS USING FOR A GIVEN FILE ON A GIVEN HOST AND PORT |
| 303 |
* |
| 304 |
* ON ENTRY: |
| 305 |
* hostname is the name of the server host machine. |
| 306 |
* portnumber is the port that the server is running in. |
| 307 |
* docname is the (URL-)pathname of the document we |
| 308 |
* are trying to access. |
| 309 |
* IsProxy should be TRUE if this is a proxy. |
| 310 |
* |
| 311 |
* This function goes through the information known about |
| 312 |
* all the setups of the server, and finds out if the given |
| 313 |
* filename resides in one of the protected directories. |
| 314 |
* |
| 315 |
* ON EXIT: |
| 316 |
* returns NULL if no match. |
| 317 |
* Otherwise, a HTAASetup structure representing |
| 318 |
* the protected server setup on the corresponding |
| 319 |
* document tree. |
| 320 |
* |
| 321 |
*/ |
| 322 |
static HTAASetup *HTAASetup_lookup(const char *hostname, |
| 323 |
int portnumber, |
| 324 |
const char *docname, |
| 325 |
int IsProxy) |
| 326 |
{ |
| 327 |
HTAAServer *server; |
| 328 |
HTAASetup *setup; |
| 329 |
|
| 330 |
if (portnumber <= 0) |
| 331 |
portnumber = 80; |
| 332 |
|
| 333 |
if (hostname && docname && *hostname && *docname && |
| 334 |
NULL != (server = HTAAServer_lookup(hostname, |
| 335 |
portnumber, |
| 336 |
IsProxy))) { |
| 337 |
|
| 338 |
HTList *cur = server->setups; |
| 339 |
|
| 340 |
CTRACE((tfp, "%s %s (%s:%d:%s)\n", |
| 341 |
"HTAASetup_lookup: resolving setup for", |
| 342 |
(IsProxy ? "proxy" : "server"), |
| 343 |
hostname, portnumber, docname)); |
| 344 |
|
| 345 |
while (NULL != (setup = (HTAASetup *) HTList_nextObject(cur))) { |
| 346 |
if (HTAA_templateMatch(setup->ctemplate, docname)) { |
| 347 |
CTRACE((tfp, "%s `%s' %s `%s'\n", |
| 348 |
"HTAASetup_lookup:", docname, |
| 349 |
"matched template", setup->ctemplate)); |
| 350 |
return setup; |
| 351 |
} else { |
| 352 |
CTRACE((tfp, "%s `%s' %s `%s'\n", |
| 353 |
"HTAASetup_lookup:", docname, |
| 354 |
"did NOT match template", setup->ctemplate)); |
| 355 |
} |
| 356 |
} /* while setups remain */ |
| 357 |
} |
| 358 |
/* if valid parameters and server found */ |
| 359 |
CTRACE((tfp, "%s `%s' %s\n", |
| 360 |
"HTAASetup_lookup: No template matched", |
| 361 |
NONNULL(docname), |
| 362 |
"(so probably not protected)")); |
| 363 |
|
| 364 |
return NULL; /* NULL in parameters, or not found */ |
| 365 |
} |
| 366 |
|
| 367 |
/* static HTAASetup_new() |
| 368 |
* CREATE A NEW SETUP NODE |
| 369 |
* ON ENTRY: |
| 370 |
* server is a pointer to a HTAAServer structure |
| 371 |
* to which this setup belongs. |
| 372 |
* ctemplate documents matching this template |
| 373 |
* are protected according to this setup. |
| 374 |
* valid_schemes a list containing all valid authentication |
| 375 |
* schemes for this setup. |
| 376 |
* If NULL, all schemes are disallowed. |
| 377 |
* scheme_specifics is an array of assoc lists, which |
| 378 |
* contain scheme specific parameters given |
| 379 |
* by server in Authenticate: fields. |
| 380 |
* If NULL, all scheme specifics are |
| 381 |
* set to NULL. |
| 382 |
* ON EXIT: |
| 383 |
* returns a new HTAASetup node, and also adds it as |
| 384 |
* part of the HTAAServer given as parameter. |
| 385 |
*/ |
| 386 |
static HTAASetup *HTAASetup_new(HTAAServer *server, char *ctemplate, |
| 387 |
HTList *valid_schemes, |
| 388 |
HTAssocList **scheme_specifics) |
| 389 |
{ |
| 390 |
HTAASetup *setup; |
| 391 |
|
| 392 |
if (!server || isEmpty(ctemplate)) |
| 393 |
return NULL; |
| 394 |
|
| 395 |
if ((setup = typecalloc(HTAASetup)) == NULL) |
| 396 |
outofmem(__FILE__, "HTAASetup_new"); |
| 397 |
|
| 398 |
setup->retry = NO; |
| 399 |
setup->server = server; |
| 400 |
setup->ctemplate = NULL; |
| 401 |
if (ctemplate) |
| 402 |
StrAllocCopy(setup->ctemplate, ctemplate); |
| 403 |
setup->valid_schemes = valid_schemes; |
| 404 |
setup->scheme_specifics = scheme_specifics; |
| 405 |
|
| 406 |
HTList_addObject(server->setups, (void *) setup); |
| 407 |
|
| 408 |
return setup; |
| 409 |
} |
| 410 |
|
| 411 |
/* static HTAASetup_delete() |
| 412 |
* FREE A HTAASetup STRUCTURE |
| 413 |
* ON ENTRY: |
| 414 |
* killme is a pointer to the structure to free(). |
| 415 |
* |
| 416 |
* ON EXIT: |
| 417 |
* returns nothing. |
| 418 |
*/ |
| 419 |
static void HTAASetup_delete(HTAASetup * killme) |
| 420 |
{ |
| 421 |
int scheme; |
| 422 |
|
| 423 |
if (killme) { |
| 424 |
FREE(killme->ctemplate); |
| 425 |
if (killme->valid_schemes) { |
| 426 |
HTList_delete(killme->valid_schemes); |
| 427 |
killme->valid_schemes = NULL; |
| 428 |
} |
| 429 |
for (scheme = 0; scheme < HTAA_MAX_SCHEMES; scheme++) |
| 430 |
if (killme->scheme_specifics[scheme]) |
| 431 |
HTAssocList_delete(killme->scheme_specifics[scheme]); |
| 432 |
FREE(killme->scheme_specifics); |
| 433 |
FREE(killme); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
/* static HTAASetup_updateSpecifics() |
| 438 |
* COPY SCHEME SPECIFIC PARAMETERS |
| 439 |
* TO HTAASetup STRUCTURE |
| 440 |
* ON ENTRY: |
| 441 |
* setup destination setup structure. |
| 442 |
* specifics string array containing scheme |
| 443 |
* specific parameters for each scheme. |
| 444 |
* If NULL, all the scheme specific |
| 445 |
* parameters are set to NULL. |
| 446 |
* |
| 447 |
* ON EXIT: |
| 448 |
* returns nothing. |
| 449 |
*/ |
| 450 |
static void HTAASetup_updateSpecifics(HTAASetup * setup, HTAssocList **specifics) |
| 451 |
{ |
| 452 |
int scheme; |
| 453 |
|
| 454 |
if (setup) { |
| 455 |
if (setup->scheme_specifics) { |
| 456 |
for (scheme = 0; scheme < HTAA_MAX_SCHEMES; scheme++) { |
| 457 |
if (setup->scheme_specifics[scheme]) |
| 458 |
HTAssocList_delete(setup->scheme_specifics[scheme]); |
| 459 |
} |
| 460 |
FREE(setup->scheme_specifics); |
| 461 |
} |
| 462 |
setup->scheme_specifics = specifics; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/*************************** HTAARealm **********************************/ |
| 467 |
|
| 468 |
/* static HTAARealm_lookup() |
| 469 |
* LOOKUP HTAARealm STRUCTURE BY REALM NAME |
| 470 |
* ON ENTRY: |
| 471 |
* realm_table a list of realm objects. |
| 472 |
* realmname is the name of realm to look for. |
| 473 |
* |
| 474 |
* ON EXIT: |
| 475 |
* returns the realm. NULL, if not found. |
| 476 |
*/ |
| 477 |
static HTAARealm *HTAARealm_lookup(HTList *realm_table, |
| 478 |
const char *realmname) |
| 479 |
{ |
| 480 |
if (realm_table && realmname) { |
| 481 |
HTList *cur = realm_table; |
| 482 |
HTAARealm *realm; |
| 483 |
|
| 484 |
while (NULL != (realm = (HTAARealm *) HTList_nextObject(cur))) { |
| 485 |
if (0 == strcmp(realm->realmname, realmname)) |
| 486 |
return realm; |
| 487 |
} |
| 488 |
} |
| 489 |
return NULL; /* No table, NULL param, or not found */ |
| 490 |
} |
| 491 |
|
| 492 |
/* static HTAARealm_new() |
| 493 |
* CREATE A NODE CONTAINING USERNAME AND |
| 494 |
* PASSWORD USED FOR THE GIVEN REALM. |
| 495 |
* IF REALM ALREADY EXISTS, CHANGE |
| 496 |
* USERNAME/PASSWORD. |
| 497 |
* ON ENTRY: |
| 498 |
* realm_table a list of realms to where to add |
| 499 |
* the new one, too. |
| 500 |
* realmname is the name of the password domain. |
| 501 |
* username and |
| 502 |
* password are what you can expect them to be. |
| 503 |
* |
| 504 |
* ON EXIT: |
| 505 |
* returns the created realm. |
| 506 |
*/ |
| 507 |
static HTAARealm *HTAARealm_new(HTList *realm_table, |
| 508 |
const char *realmname, |
| 509 |
const char *username, |
| 510 |
const char *password) |
| 511 |
{ |
| 512 |
HTAARealm *realm; |
| 513 |
|
| 514 |
realm = HTAARealm_lookup(realm_table, realmname); |
| 515 |
|
| 516 |
if (!realm) { |
| 517 |
if ((realm = typecalloc(HTAARealm)) == NULL) |
| 518 |
outofmem(__FILE__, "HTAARealm_new"); |
| 519 |
|
| 520 |
realm->realmname = NULL; |
| 521 |
realm->username = NULL; |
| 522 |
realm->password = NULL; |
| 523 |
StrAllocCopy(realm->realmname, realmname); |
| 524 |
if (realm_table) |
| 525 |
HTList_addObject(realm_table, (void *) realm); |
| 526 |
} |
| 527 |
if (username) |
| 528 |
StrAllocCopy(realm->username, username); |
| 529 |
if (password) |
| 530 |
StrAllocCopy(realm->password, password); |
| 531 |
|
| 532 |
return realm; |
| 533 |
} |
| 534 |
|
| 535 |
BOOL HTAA_HaveUserinfo(const char *hostname) |
| 536 |
{ |
| 537 |
int gen_delims = 0; |
| 538 |
BOOL result = FALSE; |
| 539 |
char *my_info = NULL; |
| 540 |
|
| 541 |
if (StrAllocCopy(my_info, hostname) != NULL) { |
| 542 |
char *at_sign = HTSkipToAt(my_info, &gen_delims); |
| 543 |
|
| 544 |
free(my_info); |
| 545 |
if (at_sign != NULL && gen_delims == 0) |
| 546 |
result = TRUE; |
| 547 |
} |
| 548 |
return result; |
| 549 |
} |
| 550 |
|
| 551 |
/* |
| 552 |
* If there is userinfo in the hostname string, update the realm to use that |
| 553 |
* information. The command-line "-auth" option will override this. |
| 554 |
*/ |
| 555 |
static void fill_in_userinfo(HTAARealm *realm, const char *hostname) |
| 556 |
{ |
| 557 |
int gen_delims = 0; |
| 558 |
char *my_info = NULL; |
| 559 |
char *at_sign = HTSkipToAt(StrAllocCopy(my_info, hostname), &gen_delims); |
| 560 |
|
| 561 |
if (at_sign != NULL && gen_delims == 0) { |
| 562 |
char *colon; |
| 563 |
|
| 564 |
*at_sign = '\0'; |
| 565 |
if ((colon = StrChr(my_info, ':')) != NULL) { |
| 566 |
*colon++ = '\0'; |
| 567 |
} |
| 568 |
if (non_empty(my_info)) { |
| 569 |
char *msg; |
| 570 |
BOOL prior = non_empty(realm->username); |
| 571 |
|
| 572 |
if (prior && strcmp(realm->username, my_info)) { |
| 573 |
msg = NULL; |
| 574 |
HTSprintf0(&msg, |
| 575 |
LY_MSG("username for realm %s changed from %s to %s"), |
| 576 |
realm->realmname, |
| 577 |
realm->username, |
| 578 |
my_info); |
| 579 |
HTAlert(msg); |
| 580 |
free(msg); |
| 581 |
FREE(realm->username); |
| 582 |
StrAllocCopy(realm->username, my_info); |
| 583 |
} else if (!prior) { |
| 584 |
StrAllocCopy(realm->username, my_info); |
| 585 |
} |
| 586 |
if (non_empty(colon)) { |
| 587 |
prior = non_empty(realm->password); |
| 588 |
if (prior && strcmp(realm->password, colon)) { |
| 589 |
msg = NULL; |
| 590 |
HTSprintf0(&msg, |
| 591 |
LY_MSG("password for realm %s user %s changed"), |
| 592 |
realm->realmname, |
| 593 |
realm->username); |
| 594 |
HTAlert(msg); |
| 595 |
free(msg); |
| 596 |
FREE(realm->password); |
| 597 |
StrAllocCopy(realm->password, colon); |
| 598 |
} else if (!prior) { |
| 599 |
StrAllocCopy(realm->password, colon); |
| 600 |
} |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
free(my_info); |
| 605 |
} |
| 606 |
|
| 607 |
/***************** Basic and Pubkey Authentication ************************/ |
| 608 |
|
| 609 |
/* static compose_auth_string() |
| 610 |
* |
| 611 |
* COMPOSE Basic OR Pubkey AUTHENTICATION STRING; |
| 612 |
* PROMPTS FOR USERNAME AND PASSWORD IF NEEDED |
| 613 |
* |
| 614 |
* ON ENTRY: |
| 615 |
* hostname may include user- and password information |
| 616 |
* scheme is either HTAA_BASIC or HTAA_PUBKEY. |
| 617 |
* setup is the current server setup. |
| 618 |
* IsProxy should be TRUE if this is a proxy. |
| 619 |
* |
| 620 |
* ON EXIT: |
| 621 |
* returns a newly composed authorization string, |
| 622 |
* (with, of course, a newly generated secret |
| 623 |
* key and fresh timestamp, if Pubkey-scheme |
| 624 |
* is being used). |
| 625 |
* NULL, if something fails. |
| 626 |
* NOTE: |
| 627 |
* Like throughout the entire AA package, no string or structure |
| 628 |
* returned by AA package needs to (or should) be freed. |
| 629 |
* |
| 630 |
*/ |
| 631 |
static char *compose_auth_string(const char *hostname, |
| 632 |
HTAAScheme scheme, |
| 633 |
HTAASetup * setup, |
| 634 |
int IsProxy) |
| 635 |
{ |
| 636 |
char *cleartext = NULL; /* Cleartext presentation */ |
| 637 |
char *ciphertext = NULL; /* Encrypted presentation */ |
| 638 |
size_t len; |
| 639 |
char *msg = NULL; |
| 640 |
char *username = NULL; |
| 641 |
char *password = NULL; |
| 642 |
char *realmname = NULL; |
| 643 |
char *theHost = NULL; |
| 644 |
char *proxiedHost = NULL; |
| 645 |
char *thePort = NULL; |
| 646 |
HTAARealm *realm; |
| 647 |
const char *i_net_addr = "0.0.0.0"; /* Change... @@@@ */ |
| 648 |
const char *timestamp = "42"; /* ... these @@@@ */ |
| 649 |
|
| 650 |
FREE(compose_auth_stringResult); /* From previous call */ |
| 651 |
|
| 652 |
if ((scheme != HTAA_BASIC && scheme != HTAA_PUBKEY) || |
| 653 |
!(setup && |
| 654 |
setup->scheme_specifics && |
| 655 |
setup->scheme_specifics[scheme] && |
| 656 |
setup->server && |
| 657 |
setup->server->realms)) |
| 658 |
return NULL; |
| 659 |
|
| 660 |
realmname = HTAssocList_lookup(setup->scheme_specifics[scheme], "realm"); |
| 661 |
if (!realmname) |
| 662 |
return NULL; |
| 663 |
|
| 664 |
realm = HTAARealm_lookup(setup->server->realms, realmname); |
| 665 |
setup->retry |= HTAA_HaveUserinfo(hostname); |
| 666 |
|
| 667 |
if (!(realm && |
| 668 |
non_empty(realm->username) && |
| 669 |
non_empty(realm->password)) || setup->retry) { |
| 670 |
if (!realm) { |
| 671 |
CTRACE((tfp, "%s `%s' %s\n", |
| 672 |
"compose_auth_string: realm:", realmname, |
| 673 |
"not found -- creating")); |
| 674 |
realm = HTAARealm_new(setup->server->realms, |
| 675 |
realmname, NULL, NULL); |
| 676 |
} |
| 677 |
fill_in_userinfo(realm, hostname); |
| 678 |
/* |
| 679 |
* The template should be either the '*' global for everything on the |
| 680 |
* server (always true for proxy authorization setups), or a path for |
| 681 |
* the start of a protected limb, with no host field, but we'll check |
| 682 |
* for a host anyway in case a WWW-Protection-Template header set an |
| 683 |
* absolute URL instead of a path. If we do get a host from this, it |
| 684 |
* will include the port. - FM |
| 685 |
*/ |
| 686 |
if ((!IsProxy) && using_proxy && setup->ctemplate) { |
| 687 |
proxiedHost = HTParse(setup->ctemplate, "", PARSE_HOST); |
| 688 |
if (proxiedHost && *proxiedHost != '\0') { |
| 689 |
theHost = proxiedHost; |
| 690 |
} |
| 691 |
} |
| 692 |
/* |
| 693 |
* If we didn't get a host field from the template, set up the host |
| 694 |
* name and port from the setup->server elements. - FM |
| 695 |
*/ |
| 696 |
if (!theHost) |
| 697 |
theHost = setup->server->hostname; |
| 698 |
if (setup->server->portnumber > 0 && |
| 699 |
setup->server->portnumber != 80) { |
| 700 |
HTSprintf0(&thePort, ":%d", setup->server->portnumber); |
| 701 |
} |
| 702 |
|
| 703 |
HTSprintf0(&msg, LY_MSG("Username for '%s' at %s '%s%s':"), |
| 704 |
realm->realmname, |
| 705 |
(IsProxy ? "proxy" : "server"), |
| 706 |
(theHost ? theHost : "??"), |
| 707 |
NonNull(thePort)); |
| 708 |
FREE(proxiedHost); |
| 709 |
FREE(thePort); |
| 710 |
if (non_empty(realm->username)) { |
| 711 |
StrAllocCopy(username, realm->username); |
| 712 |
} |
| 713 |
if (non_empty(realm->password)) { |
| 714 |
StrAllocCopy(password, realm->password); |
| 715 |
} |
| 716 |
HTPromptUsernameAndPassword(msg, &username, &password, IsProxy); |
| 717 |
|
| 718 |
FREE(msg); |
| 719 |
FREE(realm->username); |
| 720 |
FREE(realm->password); |
| 721 |
|
| 722 |
realm->username = username; |
| 723 |
realm->password = password; |
| 724 |
|
| 725 |
if (!realm->username || !realm->password) { |
| 726 |
/* |
| 727 |
* Signals to retry. - FM |
| 728 |
*/ |
| 729 |
return NULL; |
| 730 |
} else if (*realm->username == '\0') { |
| 731 |
/* |
| 732 |
* Signals to abort. - FM |
| 733 |
*/ |
| 734 |
StrAllocCopy(compose_auth_stringResult, ""); |
| 735 |
return compose_auth_stringResult; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
len = (strlen(NonNull(realm->username)) + |
| 740 |
strlen(NonNull(realm->password)) + 3); |
| 741 |
|
| 742 |
if (scheme == HTAA_PUBKEY) { |
| 743 |
#ifdef PUBKEY |
| 744 |
/* Generate new secret key */ |
| 745 |
StrAllocCopy(secret_key, HTAA_generateRandomKey()); |
| 746 |
#endif /* PUBKEY */ |
| 747 |
/* Room for secret key, timestamp and inet address */ |
| 748 |
len += strlen(NonNull(secret_key)) + 30; |
| 749 |
} else { |
| 750 |
FREE(secret_key); |
| 751 |
} |
| 752 |
|
| 753 |
if ((cleartext = typecallocn(char, len)) == NULL) |
| 754 |
outofmem(__FILE__, "compose_auth_string"); |
| 755 |
|
| 756 |
if (realm->username) |
| 757 |
strcpy(cleartext, realm->username); |
| 758 |
else |
| 759 |
*cleartext = '\0'; |
| 760 |
|
| 761 |
strcat(cleartext, ":"); |
| 762 |
|
| 763 |
if (realm->password) |
| 764 |
strcat(cleartext, realm->password); |
| 765 |
|
| 766 |
if (scheme == HTAA_PUBKEY) { |
| 767 |
strcat(cleartext, ":"); |
| 768 |
strcat(cleartext, i_net_addr); |
| 769 |
strcat(cleartext, ":"); |
| 770 |
strcat(cleartext, timestamp); |
| 771 |
strcat(cleartext, ":"); |
| 772 |
if (secret_key) |
| 773 |
strcat(cleartext, secret_key); |
| 774 |
|
| 775 |
if (!((ciphertext = typecallocn(char, 2 * len)) && |
| 776 |
(compose_auth_stringResult = typecallocn(char, 3 * len)))) |
| 777 |
outofmem(__FILE__, "compose_auth_string"); |
| 778 |
|
| 779 |
#ifdef PUBKEY |
| 780 |
HTPK_encrypt(cleartext, ciphertext, server->public_key); |
| 781 |
HTUU_encode((unsigned char *) ciphertext, strlen(ciphertext), |
| 782 |
compose_auth_stringResult); |
| 783 |
#endif /* PUBKEY */ |
| 784 |
FREE(cleartext); |
| 785 |
FREE(ciphertext); |
| 786 |
} else { /* scheme == HTAA_BASIC */ |
| 787 |
if (!(compose_auth_stringResult = |
| 788 |
typecallocn(char, (4 * ((len + 2) / 3)) + 1))) |
| 789 |
outofmem(__FILE__, "compose_auth_string"); |
| 790 |
|
| 791 |
HTUU_encode((unsigned char *) cleartext, strlen(cleartext), |
| 792 |
compose_auth_stringResult); |
| 793 |
FREE(cleartext); |
| 794 |
} |
| 795 |
return compose_auth_stringResult; |
| 796 |
} |
| 797 |
|
| 798 |
/* BROWSER static HTAA_selectScheme() |
| 799 |
* SELECT THE AUTHENTICATION SCHEME TO USE |
| 800 |
* ON ENTRY: |
| 801 |
* setup is the server setup structure which can |
| 802 |
* be used to make the decision about the |
| 803 |
* used scheme. |
| 804 |
* |
| 805 |
* When new authentication methods are added to library |
| 806 |
* this function makes the decision about which one to |
| 807 |
* use at a given time. This can be done by inspecting |
| 808 |
* environment variables etc. |
| 809 |
* |
| 810 |
* Currently only searches for the first valid scheme, |
| 811 |
* and if nothing found suggests Basic scheme; |
| 812 |
* |
| 813 |
* ON EXIT: |
| 814 |
* returns the authentication scheme to use. |
| 815 |
*/ |
| 816 |
static HTAAScheme HTAA_selectScheme(HTAASetup * setup) |
| 817 |
{ |
| 818 |
int scheme; |
| 819 |
|
| 820 |
if (setup && setup->valid_schemes) { |
| 821 |
for (scheme = HTAA_BASIC; scheme < HTAA_MAX_SCHEMES; scheme++) { |
| 822 |
void *object = (void *) (intptr_t) scheme; |
| 823 |
|
| 824 |
if (-1 < HTList_indexOf(setup->valid_schemes, object)) |
| 825 |
return (HTAAScheme) scheme; |
| 826 |
} |
| 827 |
} |
| 828 |
return HTAA_BASIC; |
| 829 |
} |
| 830 |
|
| 831 |
/* |
| 832 |
* Purpose: Free off all module globals. |
| 833 |
* Arguments: void |
| 834 |
* Return Value: void |
| 835 |
* Remarks/Portability/Dependencies/Restrictions: |
| 836 |
* To be used at program exit. |
| 837 |
* Revision History: |
| 838 |
* 06-19-96 created - FM |
| 839 |
*/ |
| 840 |
static void free_HTAAGlobals(void) |
| 841 |
{ |
| 842 |
HTAAServer *server; |
| 843 |
int n, i; |
| 844 |
|
| 845 |
if (server_table != NULL) { |
| 846 |
n = HTList_count(server_table); |
| 847 |
for (i = (n - 1); i >= 0; i--) { |
| 848 |
if ((server = (HTAAServer *) HTList_objectAt(server_table, |
| 849 |
i)) != NULL) { |
| 850 |
HTAAServer_delete(server); |
| 851 |
server = NULL; |
| 852 |
} |
| 853 |
} |
| 854 |
HTList_delete(server_table); |
| 855 |
server_table = NULL; |
| 856 |
} |
| 857 |
|
| 858 |
HTAAForwardAuth_reset(); |
| 859 |
FREE(HTAA_composeAuthResult); |
| 860 |
FREE(current_hostname); |
| 861 |
FREE(current_docname); |
| 862 |
FREE(proxy_hostname); |
| 863 |
FREE(proxy_docname); |
| 864 |
FREE(compose_auth_stringResult); |
| 865 |
FREE(secret_key); |
| 866 |
} |
| 867 |
|
| 868 |
/* BROWSER PUBLIC HTAA_composeAuth() |
| 869 |
* |
| 870 |
* SELECT THE AUTHENTICATION SCHEME AND |
| 871 |
* COMPOSE THE ENTIRE AUTHORIZATION HEADER LINE |
| 872 |
* IF WE ALREADY KNOW THAT THE HOST REQUIRES AUTHENTICATION |
| 873 |
* |
| 874 |
* ON ENTRY: |
| 875 |
* hostname is the hostname of the server. |
| 876 |
* portnumber is the portnumber in which the server runs. |
| 877 |
* docname is the pathname of the document (as in URL) |
| 878 |
* IsProxy should be TRUE if this is a proxy. |
| 879 |
* |
| 880 |
* ON EXIT: |
| 881 |
* returns NULL, if no authorization seems to be needed, or |
| 882 |
* if it is the entire Authorization: line, e.g. |
| 883 |
* |
| 884 |
* "Authorization: Basic username:password" |
| 885 |
* |
| 886 |
* As usual, this string is automatically freed. |
| 887 |
*/ |
| 888 |
char *HTAA_composeAuth(const char *hostname, |
| 889 |
const int portnumber, |
| 890 |
const char *docname, |
| 891 |
int IsProxy) |
| 892 |
{ |
| 893 |
char *auth_string; |
| 894 |
BOOL retry; |
| 895 |
HTAAScheme scheme; |
| 896 |
size_t len; |
| 897 |
|
| 898 |
/* |
| 899 |
* Setup atexit() freeing if not done already. - FM |
| 900 |
*/ |
| 901 |
if (!free_HTAAGlobalsSet) { |
| 902 |
#ifdef LY_FIND_LEAKS |
| 903 |
atexit(free_HTAAGlobals); |
| 904 |
#endif |
| 905 |
free_HTAAGlobalsSet = TRUE; |
| 906 |
} |
| 907 |
|
| 908 |
/* |
| 909 |
* Make gateway httpds pass authorization field as it was received. (This |
| 910 |
* still doesn't really work because Authenticate: headers from remote |
| 911 |
* server are not forwarded to client yet so it cannot really know that it |
| 912 |
* should send authorization; I will not implement it yet because I feel we |
| 913 |
* will soon change radically the way requests are represented to allow |
| 914 |
* multithreading on server-side. Life is hard.) |
| 915 |
*/ |
| 916 |
if (HTAAForwardAuth) { |
| 917 |
CTRACE((tfp, "HTAA_composeAuth: %s\n", |
| 918 |
"Forwarding received authorization")); |
| 919 |
StrAllocCopy(HTAA_composeAuthResult, HTAAForwardAuth); |
| 920 |
HTAAForwardAuth_reset(); /* Just a precaution */ |
| 921 |
return HTAA_composeAuthResult; |
| 922 |
} |
| 923 |
|
| 924 |
FREE(HTAA_composeAuthResult); /* From previous call */ |
| 925 |
|
| 926 |
if (IsProxy) { |
| 927 |
/* |
| 928 |
* Proxy Authorization required. - AJL |
| 929 |
*/ |
| 930 |
|
| 931 |
CTRACE((tfp, "Composing Proxy Authorization for %s:%d/%s\n", |
| 932 |
hostname, portnumber, docname)); |
| 933 |
|
| 934 |
if (proxy_portnumber != portnumber || |
| 935 |
!proxy_hostname || !proxy_docname || |
| 936 |
!hostname || !docname || |
| 937 |
0 != strcmp(proxy_hostname, hostname) || |
| 938 |
0 != strcmp(proxy_docname, docname)) { |
| 939 |
|
| 940 |
retry = NO; |
| 941 |
|
| 942 |
proxy_portnumber = portnumber; |
| 943 |
|
| 944 |
if (hostname) |
| 945 |
StrAllocCopy(proxy_hostname, hostname); |
| 946 |
else |
| 947 |
FREE(proxy_hostname); |
| 948 |
|
| 949 |
if (docname) |
| 950 |
StrAllocCopy(proxy_docname, docname); |
| 951 |
else |
| 952 |
FREE(proxy_docname); |
| 953 |
} else { |
| 954 |
retry = YES; |
| 955 |
} |
| 956 |
|
| 957 |
if (!proxy_setup || !retry) |
| 958 |
proxy_setup = HTAASetup_lookup(hostname, portnumber, |
| 959 |
docname, IsProxy); |
| 960 |
|
| 961 |
if (!proxy_setup) |
| 962 |
return NULL; |
| 963 |
|
| 964 |
switch (scheme = HTAA_selectScheme(proxy_setup)) { |
| 965 |
case HTAA_BASIC: |
| 966 |
case HTAA_PUBKEY: |
| 967 |
auth_string = compose_auth_string(hostname, scheme, proxy_setup, IsProxy); |
| 968 |
break; |
| 969 |
case HTAA_KERBEROS_V4: |
| 970 |
/* OTHER AUTHENTICATION ROUTINES ARE CALLED HERE */ |
| 971 |
default: |
| 972 |
{ |
| 973 |
char *msg = NULL; |
| 974 |
|
| 975 |
HTSprintf0(&msg, "%s `%s'", |
| 976 |
gettext("This client doesn't know how to compose proxy authorization information for scheme"), |
| 977 |
HTAAScheme_name(scheme)); |
| 978 |
HTAlert(msg); |
| 979 |
FREE(msg); |
| 980 |
auth_string = NULL; |
| 981 |
} |
| 982 |
} /* switch scheme */ |
| 983 |
|
| 984 |
proxy_setup->retry = NO; |
| 985 |
|
| 986 |
if (!auth_string) |
| 987 |
/* |
| 988 |
* Signal a failure. - FM |
| 989 |
*/ |
| 990 |
return NULL; /* Added by marca. */ |
| 991 |
if (*auth_string == '\0') { |
| 992 |
/* |
| 993 |
* Signal an abort. - FM |
| 994 |
*/ |
| 995 |
StrAllocCopy(HTAA_composeAuthResult, ""); |
| 996 |
return (HTAA_composeAuthResult); |
| 997 |
} |
| 998 |
len = strlen(auth_string) + strlen(HTAAScheme_name(scheme)) + 26; |
| 999 |
if ((HTAA_composeAuthResult = typecallocn(char, len)) == NULL) |
| 1000 |
outofmem(__FILE__, "HTAA_composeAuth"); |
| 1001 |
|
| 1002 |
strcpy(HTAA_composeAuthResult, "Proxy-Authorization: "); |
| 1003 |
|
| 1004 |
} else { |
| 1005 |
/* |
| 1006 |
* Normal WWW authorization. |
| 1007 |
*/ |
| 1008 |
CTRACE((tfp, "Composing Authorization for %s:%d/%s\n", |
| 1009 |
hostname, portnumber, docname)); |
| 1010 |
|
| 1011 |
if (current_portnumber != portnumber || |
| 1012 |
!current_hostname || !current_docname || |
| 1013 |
!hostname || !docname || |
| 1014 |
0 != strcmp(current_hostname, hostname) || |
| 1015 |
0 != strcmp(current_docname, docname)) { |
| 1016 |
|
| 1017 |
retry = NO; |
| 1018 |
|
| 1019 |
current_portnumber = portnumber; |
| 1020 |
|
| 1021 |
if (hostname) |
| 1022 |
StrAllocCopy(current_hostname, hostname); |
| 1023 |
else |
| 1024 |
FREE(current_hostname); |
| 1025 |
|
| 1026 |
if (docname) |
| 1027 |
StrAllocCopy(current_docname, docname); |
| 1028 |
else |
| 1029 |
FREE(current_docname); |
| 1030 |
} else { |
| 1031 |
retry = YES; |
| 1032 |
} |
| 1033 |
|
| 1034 |
if (!current_setup || !retry) |
| 1035 |
current_setup = HTAASetup_lookup(hostname, portnumber, |
| 1036 |
docname, IsProxy); |
| 1037 |
|
| 1038 |
if (!current_setup) |
| 1039 |
return NULL; |
| 1040 |
|
| 1041 |
switch (scheme = HTAA_selectScheme(current_setup)) { |
| 1042 |
case HTAA_BASIC: |
| 1043 |
case HTAA_PUBKEY: |
| 1044 |
auth_string = compose_auth_string(hostname, scheme, current_setup, IsProxy); |
| 1045 |
break; |
| 1046 |
case HTAA_KERBEROS_V4: |
| 1047 |
/* OTHER AUTHENTICATION ROUTINES ARE CALLED HERE */ |
| 1048 |
default: |
| 1049 |
{ |
| 1050 |
char *msg = NULL; |
| 1051 |
|
| 1052 |
HTSprintf0(&msg, "%s `%s'", |
| 1053 |
gettext("This client doesn't know how to compose authorization information for scheme"), |
| 1054 |
HTAAScheme_name(scheme)); |
| 1055 |
HTAlert(msg); |
| 1056 |
FREE(msg); |
| 1057 |
auth_string = NULL; |
| 1058 |
} |
| 1059 |
} /* switch scheme */ |
| 1060 |
|
| 1061 |
current_setup->retry = NO; |
| 1062 |
|
| 1063 |
if (!auth_string) |
| 1064 |
/* |
| 1065 |
* Signal a failure. - FM |
| 1066 |
*/ |
| 1067 |
return NULL; /* Added by marca. */ |
| 1068 |
if (*auth_string == '\0') { |
| 1069 |
/* |
| 1070 |
* Signal an abort. - FM |
| 1071 |
*/ |
| 1072 |
StrAllocCopy(HTAA_composeAuthResult, ""); |
| 1073 |
return (HTAA_composeAuthResult); |
| 1074 |
} |
| 1075 |
|
| 1076 |
len = strlen(auth_string) + strlen(HTAAScheme_name(scheme)) + 20; |
| 1077 |
if ((HTAA_composeAuthResult = typecallocn(char, len)) == NULL) |
| 1078 |
outofmem(__FILE__, "HTAA_composeAuth"); |
| 1079 |
|
| 1080 |
strcpy(HTAA_composeAuthResult, "Authorization: "); |
| 1081 |
} |
| 1082 |
|
| 1083 |
strcat(HTAA_composeAuthResult, HTAAScheme_name(scheme)); |
| 1084 |
strcat(HTAA_composeAuthResult, " "); |
| 1085 |
strcat(HTAA_composeAuthResult, auth_string); |
| 1086 |
return HTAA_composeAuthResult; |
| 1087 |
} |
| 1088 |
|
| 1089 |
/* BROWSER PUBLIC HTAA_shouldRetryWithAuth() |
| 1090 |
* |
| 1091 |
* DETERMINES IF WE SHOULD RETRY THE SERVER |
| 1092 |
* WITH AUTHORIZATION |
| 1093 |
* (OR IF ALREADY RETRIED, WITH A DIFFERENT |
| 1094 |
* USERNAME AND/OR PASSWORD (IF MISSPELLED)) |
| 1095 |
* ON ENTRY: |
| 1096 |
* start_of_headers is the first block already read from socket, |
| 1097 |
* but status line skipped; i.e., points to the |
| 1098 |
* start of the header section. |
| 1099 |
* length is the remaining length of the first block. |
| 1100 |
* soc is the socket to read the rest of server reply. |
| 1101 |
* IsProxy should be TRUE if this is a proxy. |
| 1102 |
* |
| 1103 |
* This function should only be called when |
| 1104 |
* server has replied with a 401 (Unauthorized) |
| 1105 |
* status code. |
| 1106 |
* ON EXIT: |
| 1107 |
* returns YES, if connection should be retried. |
| 1108 |
* The node containing all the necessary |
| 1109 |
* information is |
| 1110 |
* * either constructed if it does not exist |
| 1111 |
* * or password is reset to NULL to indicate |
| 1112 |
* that username and password should be |
| 1113 |
* reprompted when composing Authorization: |
| 1114 |
* field (in function HTAA_composeAuth()). |
| 1115 |
* NO, otherwise. |
| 1116 |
*/ |
| 1117 |
BOOL HTAA_shouldRetryWithAuth(char *start_of_headers, |
| 1118 |
size_t length, |
| 1119 |
int soc, |
| 1120 |
int IsProxy) |
| 1121 |
{ |
| 1122 |
HTAAScheme scheme; |
| 1123 |
char *line = NULL; |
| 1124 |
int num_schemes = 0; |
| 1125 |
HTList *valid_schemes = HTList_new(); |
| 1126 |
HTAssocList **scheme_specifics = NULL; |
| 1127 |
char *ctemplate = NULL; |
| 1128 |
char *temp = NULL; |
| 1129 |
BOOL result = NO; |
| 1130 |
|
| 1131 |
/* |
| 1132 |
* Setup atexit() freeing if not done already. - FM |
| 1133 |
*/ |
| 1134 |
if (!free_HTAAGlobalsSet) { |
| 1135 |
#ifdef LY_FIND_LEAKS |
| 1136 |
atexit(free_HTAAGlobals); |
| 1137 |
#endif |
| 1138 |
free_HTAAGlobalsSet = TRUE; |
| 1139 |
} |
| 1140 |
|
| 1141 |
/* |
| 1142 |
* Read server reply header lines |
| 1143 |
*/ |
| 1144 |
CTRACE((tfp, "Server reply header lines:\n")); |
| 1145 |
|
| 1146 |
HTAA_setupReader(start_of_headers, length, soc); |
| 1147 |
while (NULL != (line = HTAA_getUnfoldedLine()) && *line != '\0') { |
| 1148 |
CTRACE((tfp, "%s\n", line)); |
| 1149 |
|
| 1150 |
if (StrChr(line, ':')) { /* Valid header line */ |
| 1151 |
|
| 1152 |
char *p = line; |
| 1153 |
char *fieldname = HTNextField(&p); |
| 1154 |
char *arg1 = HTNextField(&p); |
| 1155 |
char *args = p; |
| 1156 |
|
| 1157 |
if ((IsProxy && |
| 1158 |
0 == strcasecomp(fieldname, "Proxy-Authenticate:")) || |
| 1159 |
(!IsProxy && |
| 1160 |
0 == strcasecomp(fieldname, "WWW-Authenticate:"))) { |
| 1161 |
if (isEmpty(arg1) || isEmpty(args)) { |
| 1162 |
HTSprintf0(&temp, LY_MSG("Invalid header '%s%s%s%s%s'"), line, |
| 1163 |
(non_empty(arg1) ? " " : ""), |
| 1164 |
NonNull(arg1), |
| 1165 |
(non_empty(args) ? " " : ""), |
| 1166 |
NonNull(args)); |
| 1167 |
HTAlert(temp); |
| 1168 |
FREE(temp); |
| 1169 |
} else if (HTAA_UNKNOWN != (scheme = HTAAScheme_enum(arg1))) { |
| 1170 |
HTList_addObject(valid_schemes, (void *) scheme); |
| 1171 |
if (!scheme_specifics) { |
| 1172 |
int i; |
| 1173 |
|
| 1174 |
scheme_specifics = |
| 1175 |
typecallocn(HTAssocList *, HTAA_MAX_SCHEMES); |
| 1176 |
|
| 1177 |
if (!scheme_specifics) |
| 1178 |
outofmem(__FILE__, "HTAA_shouldRetryWithAuth"); |
| 1179 |
|
| 1180 |
for (i = 0; i < HTAA_MAX_SCHEMES; i++) |
| 1181 |
scheme_specifics[i] = NULL; |
| 1182 |
} |
| 1183 |
scheme_specifics[scheme] = HTAA_parseArgList(args); |
| 1184 |
num_schemes++; |
| 1185 |
} else { |
| 1186 |
CTRACE((tfp, "Unknown scheme `%s' %s\n", |
| 1187 |
NONNULL(arg1), |
| 1188 |
(IsProxy ? |
| 1189 |
"in Proxy-Authenticate: field" : |
| 1190 |
"in WWW-Authenticate: field"))); |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
else if (!IsProxy && |
| 1195 |
0 == strcasecomp(fieldname, "WWW-Protection-Template:")) { |
| 1196 |
CTRACE((tfp, "Protection template set to `%s'\n", arg1)); |
| 1197 |
StrAllocCopy(ctemplate, arg1); |
| 1198 |
} |
| 1199 |
|
| 1200 |
} else { |
| 1201 |
CTRACE((tfp, "Invalid header line `%s' ignored\n", line)); |
| 1202 |
} |
| 1203 |
|
| 1204 |
FREE(line); |
| 1205 |
} /* while header lines remain */ |
| 1206 |
FREE(line); |
| 1207 |
|
| 1208 |
/* |
| 1209 |
* So should we retry with authorization? |
| 1210 |
*/ |
| 1211 |
if (IsProxy) { |
| 1212 |
if (num_schemes == 0) { |
| 1213 |
/* |
| 1214 |
* No proxy authorization valid |
| 1215 |
*/ |
| 1216 |
proxy_setup = NULL; |
| 1217 |
result = NO; |
| 1218 |
} |
| 1219 |
/* |
| 1220 |
* Doing it for proxy. -AJL |
| 1221 |
*/ |
| 1222 |
else if (proxy_setup && proxy_setup->server) { |
| 1223 |
/* |
| 1224 |
* We have already tried with proxy authorization. Either we don't |
| 1225 |
* have access or username or password was misspelled. |
| 1226 |
* |
| 1227 |
* Update scheme-specific parameters (in case they have expired by |
| 1228 |
* chance). |
| 1229 |
*/ |
| 1230 |
HTAASetup_updateSpecifics(proxy_setup, scheme_specifics); |
| 1231 |
|
| 1232 |
if (NO == HTConfirm(AUTH_FAILED_PROMPT)) { |
| 1233 |
proxy_setup = NULL; |
| 1234 |
result = NO; |
| 1235 |
} else { |
| 1236 |
/* |
| 1237 |
* Re-ask username+password (if misspelled). |
| 1238 |
*/ |
| 1239 |
HTList_delete(valid_schemes); |
| 1240 |
proxy_setup->retry = YES; |
| 1241 |
result = YES; |
| 1242 |
} |
| 1243 |
} else { |
| 1244 |
/* |
| 1245 |
* proxy_setup == NULL, i.e., we have a first connection to a |
| 1246 |
* protected server or the server serves a wider set of documents |
| 1247 |
* than we expected so far. |
| 1248 |
*/ |
| 1249 |
HTAAServer *server = HTAAServer_lookup(proxy_hostname, |
| 1250 |
proxy_portnumber, |
| 1251 |
IsProxy); |
| 1252 |
|
| 1253 |
if (!server) { |
| 1254 |
server = HTAAServer_new(proxy_hostname, |
| 1255 |
proxy_portnumber, |
| 1256 |
IsProxy); |
| 1257 |
} |
| 1258 |
if (!ctemplate) /* Proxy matches everything -AJL */ |
| 1259 |
StrAllocCopy(ctemplate, "*"); |
| 1260 |
proxy_setup = HTAASetup_new(server, |
| 1261 |
ctemplate, |
| 1262 |
valid_schemes, |
| 1263 |
scheme_specifics); |
| 1264 |
FREE(ctemplate); |
| 1265 |
|
| 1266 |
HTAlert(gettext("Proxy authorization required -- retrying")); |
| 1267 |
result = YES; |
| 1268 |
} |
| 1269 |
} |
| 1270 |
/* |
| 1271 |
* Normal WWW authorization. |
| 1272 |
*/ |
| 1273 |
else if (num_schemes == 0) { |
| 1274 |
/* |
| 1275 |
* No authorization valid. |
| 1276 |
*/ |
| 1277 |
current_setup = NULL; |
| 1278 |
result = NO; |
| 1279 |
} else if (current_setup && current_setup->server) { |
| 1280 |
/* |
| 1281 |
* So we have already tried with WWW authorization. Either we don't |
| 1282 |
* have access or username or password was misspelled. |
| 1283 |
* |
| 1284 |
* Update scheme-specific parameters (in case they have expired by |
| 1285 |
* chance). |
| 1286 |
*/ |
| 1287 |
HTAASetup_updateSpecifics(current_setup, scheme_specifics); |
| 1288 |
|
| 1289 |
if (NO == HTConfirm(AUTH_FAILED_PROMPT)) { |
| 1290 |
current_setup = NULL; |
| 1291 |
result = NO; |
| 1292 |
} else { |
| 1293 |
/* |
| 1294 |
* Re-ask username+password (if misspelled). |
| 1295 |
*/ |
| 1296 |
current_setup->retry = YES; |
| 1297 |
result = YES; |
| 1298 |
} |
| 1299 |
} else { |
| 1300 |
/* |
| 1301 |
* current_setup == NULL, i.e., we have a first connection to a |
| 1302 |
* protected server or the server serves a wider set of documents than |
| 1303 |
* we expected so far. |
| 1304 |
*/ |
| 1305 |
HTAAServer *server = HTAAServer_lookup(current_hostname, |
| 1306 |
current_portnumber, |
| 1307 |
IsProxy); |
| 1308 |
|
| 1309 |
if (!server) { |
| 1310 |
server = HTAAServer_new(current_hostname, |
| 1311 |
current_portnumber, |
| 1312 |
IsProxy); |
| 1313 |
} |
| 1314 |
if (!ctemplate) |
| 1315 |
ctemplate = HTAA_makeProtectionTemplate(current_docname); |
| 1316 |
current_setup = HTAASetup_new(server, |
| 1317 |
ctemplate, |
| 1318 |
valid_schemes, |
| 1319 |
scheme_specifics); |
| 1320 |
FREE(ctemplate); |
| 1321 |
|
| 1322 |
HTAlert(gettext("Access without authorization denied -- retrying")); |
| 1323 |
result = YES; |
| 1324 |
} |
| 1325 |
|
| 1326 |
if (result == NO) { |
| 1327 |
HTList_delete(valid_schemes); |
| 1328 |
} |
| 1329 |
return result; |
| 1330 |
} |
| 1331 |
|
| 1332 |
/* |
| 1333 |
* This function clears all authorization information by |
| 1334 |
* invoking the free_HTAAGlobals() function, which normally |
| 1335 |
* is invoked at exit. It allows a browser command to do |
| 1336 |
* this at any time, for example, if the user is leaving |
| 1337 |
* the terminal for a period of time, but does not want |
| 1338 |
* to end the current session. - FM |
| 1339 |
*/ |
| 1340 |
void HTClearHTTPAuthInfo(void) |
| 1341 |
{ |
| 1342 |
/* |
| 1343 |
* Need code to check cached documents against the protection templates, |
| 1344 |
* and do something to ensure that any protected documents no longer can be |
| 1345 |
* accessed without a new retrieval. - FM |
| 1346 |
*/ |
| 1347 |
|
| 1348 |
/* |
| 1349 |
* Now free all of the authorization info, and reset the |
| 1350 |
* free_HTAAGlobalsSet flag. - FM |
| 1351 |
*/ |
| 1352 |
free_HTAAGlobals(); |
| 1353 |
free_HTAAGlobalsSet = FALSE; |
| 1354 |
} |