auth/api.h

00001 /*
00002  * $Id$
00003  *
00004  * Digest Authentication Module
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License 
00026  * along with this program; if not, write to the Free Software 
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  */
00029 
00030 #ifndef API_H
00031 #define API_H
00032 
00033 
00034 #include "../../parser/msg_parser.h"
00035 #include "../../parser/digest/digest.h"
00036 #include "../../sr_module.h"
00037 #include "../../usr_avp.h"
00038 #include "../../parser/hf.h"
00039 #include "../../str.h"
00040 #include "challenge.h"
00041 #include "rfc2617.h"
00042 
00046 typedef enum auth_cfg_result {
00047         AUTH_USER_MISMATCH = -8,    
00048         AUTH_NONCE_REUSED = -6,     
00049         AUTH_NO_CREDENTIALS = -5,   
00050         AUTH_STALE_NONCE = -4,      
00051         AUTH_USER_UNKNOWN = -3,     
00052         AUTH_INVALID_PASSWORD = -2, 
00053         AUTH_ERROR = -1,            
00054         AUTH_DROP = 0,              
00055         AUTH_OK = 1                 
00056 } auth_cfg_result_t;
00057 
00058 
00062 typedef enum auth_result {
00063         NONCE_REUSED = -5,  /* Returned if nonce is used more than once */
00064         NO_CREDENTIALS,     /* Credentials missing */
00065         STALE_NONCE,        /* Stale nonce */
00066         ERROR,              /* Error occurred, a reply has been sent out -> return 0 to the ser core */
00067         NOT_AUTHENTICATED,  /* Don't perform authentication, credentials missing */
00068         DO_AUTHENTICATION,  /* Perform digest authentication */
00069         AUTHENTICATED,      /* Authenticated by default, no digest authentication necessary */
00070         BAD_CREDENTIALS,    /* Digest credentials are malformed */
00071         CREATE_CHALLENGE,   /* when AKAv1-MD5 is used first request does not contain credentials,
00072                              * only usename, realm and algorithm. Server should get Authentication
00073                              * Vector from AuC/HSS, create challenge and send it to the UE. */
00074         DO_RESYNCHRONIZATION   /* When AUTS is received we need do resynchronization
00075                                 * of sequnce numbers with mobile station. */
00076 } auth_result_t;
00077 
00078 
00079 typedef int (*check_auth_hdr_t)(struct sip_msg* msg, auth_body_t* auth_body,
00080                 auth_result_t* auth_res);
00081 int check_auth_hdr(struct sip_msg* msg, auth_body_t* auth_body,
00082                 auth_result_t* auth_res);
00083 
00084 /*
00085  * Purpose of this function is to find credentials with given realm,
00086  * do sanity check, validate credential correctness and determine if
00087  * we should really authenticate (there must be no authentication for
00088  * ACK and CANCEL
00089  */
00090 typedef auth_result_t (*pre_auth_t)(struct sip_msg* msg, str* realm,
00091                                     hdr_types_t hftype, struct hdr_field** hdr,
00092                                         check_auth_hdr_t check_auth_hdr);
00093 auth_result_t pre_auth(struct sip_msg* msg, str* realm, hdr_types_t hftype,
00094                        struct hdr_field** hdr, check_auth_hdr_t check_auth_hdr);
00095 
00096 
00097 /*
00098  * Purpose of this function is to do post authentication steps like
00099  * marking authorized credentials and so on.
00100  */
00101 typedef auth_result_t (*post_auth_t)(struct sip_msg* msg,
00102                 struct hdr_field* hdr);
00103 auth_result_t post_auth(struct sip_msg* msg, struct hdr_field* hdr);
00104 
00105 typedef int (*check_response_t)(dig_cred_t* cred, str* method, char* ha1);
00106 int auth_check_response(dig_cred_t* cred, str* method, char* ha1);
00107 
00108 typedef int (*auth_challenge_f)(struct sip_msg *msg, str *realm, int flags,
00109                 int hftype);
00110 int auth_challenge(struct sip_msg *msg, str *realm, int flags,
00111                 int hftype);
00112 
00113 typedef int (*pv_authenticate_f)(struct sip_msg *msg, str *realm, str *passwd,
00114                 int flags, int hftype);
00115 int pv_authenticate(struct sip_msg *msg, str *realm, str *passwd,
00116                 int flags, int hftype);
00117 
00118 typedef int (*consume_credentials_f)(struct sip_msg* msg);
00119 int consume_credentials(struct sip_msg* msg);
00120 
00121 /*
00122  * Auth module API
00123  */
00124 typedef struct auth_api_s {
00125     pre_auth_t pre_auth;                  /* The function to be called before authentication */
00126     post_auth_t post_auth;                /* The function to be called after authentication */
00127     build_challenge_hf_t build_challenge; /* Function to build digest challenge header */
00128     struct qp* qop;                       /* qop module parameter */
00129         calc_HA1_t         calc_HA1;
00130         calc_response_t    calc_response;
00131         check_response_t   check_response;
00132         auth_challenge_f   auth_challenge;
00133         pv_authenticate_f  pv_authenticate;
00134         consume_credentials_f consume_credentials;
00135 } auth_api_s_t;
00136 
00137 typedef int (*bind_auth_s_t)(auth_api_s_t* api);
00138 int bind_auth_s(auth_api_s_t* api);
00139 
00143 static inline int auth_load_api(auth_api_s_t* api)
00144 {
00145         bind_auth_s_t bind_auth;
00146 
00147         /* bind to auth module and import the API */
00148         bind_auth = (bind_auth_s_t)find_export("bind_auth_s", 0, 0);
00149         if (!bind_auth) {
00150                 LM_ERR("unable to find bind_auth function. Check if you load"
00151                                 " the auth module.\n");
00152                 return -1;
00153         }
00154 
00155         if (bind_auth(api) < 0) {
00156                 LM_ERR("unable to bind auth module\n");
00157                 return -1;
00158         }
00159         return 0;
00160 }
00161 
00162 #endif /* API_H */