auth_api.c

Go to the documentation of this file.
00001 /*
00002  * $Id: auth_api.c 2 2006-11-14 22:37:20Z vingarzan $
00003  *
00004  * Digest Authentication Module 
00005  * 
00006  * Just the credential finding routines
00007  *
00008  * Copyright (C) 2001-2003 FhG Fokus
00009  *
00010  * This file is part of ser, a free SIP server.
00011  *
00012  * ser is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version
00016  *
00017  * For a license to use the ser software under conditions
00018  * other than those described here, or to purchase support for this
00019  * software, please contact iptel.org by e-mail at the following addresses:
00020  *    info@iptel.org
00021  *
00022  * ser is distributed in the hope that it will be useful,
00023  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025  * GNU General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License 
00028  * along with this program; if not, write to the Free Software 
00029  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030  */
00031  
00041 #include <string.h>
00042 #include "auth_api.h"
00043 #include "../../dprint.h"
00044 #include "../../parser/digest/digest.h"
00045 #include "../../sr_module.h"
00046 
00047 
00048 
00049 /*
00050  * Find credentials with given realm in a SIP message header
00051  */
00052 inline int find_credentials(struct sip_msg* _m, str* _realm,
00053                                     hdr_types_t _hftype, struct hdr_field** _h)
00054 {
00055     struct hdr_field** hook, *ptr, *prev;
00056     hdr_flags_t hdr_flags;
00057     int res;
00058     str* r;
00059 
00060          /*
00061           * Determine if we should use WWW-Authorization or
00062           * Proxy-Authorization header fields, this parameter
00063           * is set in www_authorize and proxy_authorize
00064           */
00065     switch(_hftype) {
00066     case HDR_AUTHORIZATION_T: 
00067                             hook = &(_m->authorization);
00068                             hdr_flags=HDR_AUTHORIZATION_F;
00069                             break;
00070     case HDR_PROXYAUTH_T:
00071                             hook = &(_m->proxy_auth);
00072                             hdr_flags=HDR_PROXYAUTH_F;
00073                             break;
00074     default:                
00075                             hook = &(_m->authorization);
00076                             hdr_flags=HDR_T2F(_hftype);
00077                             break;
00078     }
00079 
00080          /*
00081           * If the credentials haven't been parsed yet, do it now
00082           */
00083     if (*hook == 0) {
00084              /* No credentials parsed yet */
00085         if (parse_headers(_m, hdr_flags, 0) == -1) {
00086             LOG(L_ERR, "find_credentials(): Error while parsing headers\n");
00087             return -1;
00088         }
00089     }
00090 
00091     ptr = *hook;
00092 
00093          /*
00094           * Iterate through the credentials in the message and
00095           * find credentials with given realm
00096           */
00097     while(ptr) {
00098         res = parse_credentials(ptr);
00099         if (res < 0) {
00100             LOG(L_ERR, "find_credentials(): Error while parsing credentials\n");
00101             return (res == -1) ? -2 : -3;
00102         } else if (res == 0) {
00103             if (_realm->len) {
00104                 r = &(((auth_body_t*)(ptr->parsed))->digest.realm);
00105     
00106                 if (r->len == _realm->len) {
00107                     if (!strncasecmp(_realm->s, r->s, r->len)) {
00108                         *_h = ptr;
00109                         return 0;
00110                     }
00111                 }
00112             }
00113             else {
00114                 *_h = ptr;
00115                 return 0;
00116             }
00117             
00118         }
00119 
00120         prev = ptr;
00121         if (parse_headers(_m, hdr_flags, 1) == -1) {
00122             LOG(L_ERR, "find_credentials(): Error while parsing headers\n");
00123             return -4;
00124         } else {
00125             if (prev != _m->last_header) {
00126                 if (_m->last_header->type == _hftype) ptr = _m->last_header;
00127                 else break;
00128             } else break;
00129         }
00130     }
00131     
00132          /*
00133           * Credentials with given realm not found
00134           */
00135     return 1;
00136 }

Generated on Thu Oct 23 04:14:37 2008 for Open IMS Core CSCFs by  doxygen 1.5.2