Annotation of libwww/Library/src/HTAABrow.c, revision 2.38

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.38    ! frystyk     6: **     @(#) $Id: HTAABrow.c,v 2.37 1996/07/09 18:05:32 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.38    ! frystyk    39:     BOOL       retry;                      /* Should we ask the user again? */
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");
2.38    ! frystyk    49:     me->retry = YES;                          /* Ask the first time through */
2.36      frystyk    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 : "") +
2.38    ! frystyk   114:            strlen(basic->pw ? basic->pw : "") + 2;
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);
2.32      frystyk   161:        }
                    162:        HTAlert_deleteReply(reply);
2.36      frystyk   163:        return res ? HT_OK : HT_ERROR;
2.1       luotonen  164:     }
2.36      frystyk   165:     return HT_OK;
2.1       luotonen  166: }
                    167: 
2.32      frystyk   168: /*     HTBasic_generate
                    169: **     ----------------
                    170: **     This function generates "basic" credentials for the challenge found in
                    171: **     the authentication information base for this request. The result is
                    172: **     stored as an association list in the request object.
                    173: **     This is a callback function for the AA handler.
                    174: */
2.36      frystyk   175: PUBLIC int HTBasic_generate (HTRequest * request, void * context, int status)
2.32      frystyk   176: { 
2.36      frystyk   177:     HTBasic * basic = (HTBasic *) context;
                    178:     if (request) {
                    179:        const char * realm = HTRequest_realm(request);
                    180: 
                    181:        /* If we don't have a basic context then add a new one to the tree */
                    182:        if (!basic) {
                    183:            char * url = HTAnchor_physical(HTRequest_anchor(request));
                    184:            basic = HTBasic_new();
2.38    ! frystyk   185:            HTAA_updateNode(BASIC_AUTH, realm, url, basic);
2.36      frystyk   186:        }
                    187: 
2.32      frystyk   188:        /*
2.36      frystyk   189:        ** If we have a set of credentials (or the user provides a new set)
                    190:        ** then store it in the request object as the credentials
2.32      frystyk   191:        */
2.38    ! frystyk   192:        if (basic->retry && prompt_user(request, realm, basic) == HT_OK) {
        !           193:            basic->retry = NO;
2.36      frystyk   194:            return basic_credentials(request, basic);
2.38    ! frystyk   195:        } else
2.37      frystyk   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);
2.38    ! frystyk   212:     HTBasic * basic = NULL;
2.36      frystyk   213:     if (request && challenge) {
                    214:        char * p = HTAssocList_findObject(challenge, BASIC_AUTH);
                    215:        char * realm = HTNextField(&p);
                    216:        char * rm = HTNextField(&p);
2.38    ! frystyk   217: 
2.32      frystyk   218:        /*
2.36      frystyk   219:        ** If valid challenge then make a template for the resource and
                    220:        ** store this information in our authentication URL Tree
2.32      frystyk   221:        */
2.36      frystyk   222:        if (realm && !strcasecomp(realm, "realm") && rm) {
                    223:            char * url = HTAnchor_physical(HTRequest_anchor(request));
                    224:            char * tmplate = make_template(url);
                    225:            if (AUTH_TRACE) HTTrace("Basic Parse. Realm `%s\' found\n", rm);
2.38    ! frystyk   226:            basic = (HTBasic *) HTAA_updateNode(BASIC_AUTH, rm, tmplate, NULL);
2.36      frystyk   227:            HTRequest_setRealm(request, rm);
                    228:            HT_FREE(tmplate);
2.1       luotonen  229:        }
2.38    ! frystyk   230: 
        !           231:        /*
        !           232:        ** For some reason the authentication failed so we have to ask the user
        !           233:        ** if we should try again. It may be because the user typed the wrong
        !           234:        ** user name and password
        !           235:        */
        !           236:        if (basic) {
        !           237:            HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
        !           238:            if (prompt) {
        !           239:                if ((*prompt)(request,HT_A_CONFIRM,HT_MSG_RETRY_AUTHENTICATION,
        !           240:                              NULL, NULL, NULL) != YES)
        !           241:                    return HT_ERROR;
        !           242:                basic->retry = YES;
        !           243:            }
        !           244:        }
2.36      frystyk   245:        return HT_OK;
2.1       luotonen  246:     }
2.36      frystyk   247:     if (AUTH_TRACE) HTTrace("Auth........ No challenges found\n");
2.38    ! frystyk   248:     return HT_ERROR;
2.7       luotonen  249: }

Webmaster