auth_radius/authorize.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <string.h>
00032 #include <stdlib.h>
00033 #include "../../mem/mem.h"
00034 #include "../../str.h"
00035 #include "../../parser/hf.h"
00036 #include "../../parser/digest/digest.h"
00037 #include "../../parser/parse_uri.h"
00038 #include "../../parser/parse_from.h"
00039 #include "../../parser/parse_to.h"
00040 #include "../../dprint.h"
00041 #include "../../ut.h"
00042 #include "../../pvar.h"
00043 #include "../auth/api.h"
00044 #include "authorize.h"
00045 #include "sterman.h"
00046 #include "authrad_mod.h"
00047
00048
00049
00050
00051
00052 static inline int get_uri_user(struct sip_msg* _m, str** _uri_user)
00053 {
00054 struct sip_uri *puri;
00055
00056 if ((REQ_LINE(_m).method.len == 8) &&
00057 (memcmp(REQ_LINE(_m).method.s, "REGISTER", 8) == 0)) {
00058 if ((puri=parse_to_uri(_m))==NULL) {
00059 LM_ERR("failed to parse To header\n");
00060 return -1;
00061 }
00062 } else {
00063 if ((puri=parse_from_uri(_m))==NULL) {
00064 LM_ERR("parsing From header\n");
00065 return -1;
00066 }
00067 }
00068
00069 *_uri_user = &(puri->user);
00070
00071 return 0;
00072 }
00073
00074
00075
00076
00077
00078 static inline int authorize(struct sip_msg* _msg, pv_elem_t* _realm,
00079 pv_spec_t * _uri_user, int _hftype)
00080 {
00081 int res;
00082 auth_result_t ret;
00083 struct hdr_field* h;
00084 auth_body_t* cred;
00085 str *uri_user;
00086 str user, domain;
00087 pv_value_t pv_val;
00088
00089
00090 if (_realm) {
00091 if (pv_printf_s(_msg, _realm, &domain)!=0) {
00092 LM_ERR("pv_printf_s failed\n");
00093 return AUTH_ERROR;
00094 }
00095 } else {
00096
00097 domain.len = 0;
00098 domain.s = 0;
00099 }
00100
00101 ret = auth_api.pre_auth(_msg, &domain, _hftype, &h);
00102
00103 if (ret != DO_AUTHORIZATION)
00104 return ret;
00105
00106 cred = (auth_body_t*)h->parsed;
00107
00108
00109
00110 if (_uri_user) {
00111 if (pv_get_spec_value(_msg, _uri_user, &pv_val) == 0) {
00112 if (pv_val.flags & PV_VAL_STR) {
00113 res = radius_authorize_sterman(_msg, &cred->digest,
00114 &_msg->first_line.u.request.method,
00115 &pv_val.rs);
00116 } else {
00117 LM_ERR("uri_user pvar value is not string\n");
00118 return AUTH_ERROR;
00119 }
00120 } else {
00121 LM_ERR("cannot get uri_user pvar value\n");
00122 return AUTH_ERROR;
00123 }
00124 } else {
00125 if (get_uri_user(_msg, &uri_user) < 0) {
00126 LM_ERR("To/From URI not found\n");
00127 return AUTH_ERROR;
00128 }
00129 user.s = (char *)pkg_malloc(uri_user->len);
00130 if (user.s == NULL) {
00131 LM_ERR("no pkg memory left for user\n");
00132 return AUTH_ERROR;
00133 }
00134 un_escape(uri_user, &user);
00135 res = radius_authorize_sterman(_msg, &cred->digest,
00136 &_msg->first_line.u.request.method,
00137 &user);
00138 pkg_free(user.s);
00139 }
00140
00141 if (res == 1) {
00142 ret = auth_api.post_auth(_msg, h);
00143 return ret;
00144 }
00145
00146 return AUTH_ERROR;
00147 }
00148
00149
00150
00151
00152
00153 int radius_proxy_authorize_1(struct sip_msg* _msg, char* _realm, char* _s2)
00154 {
00155
00156 return authorize(_msg, (pv_elem_t*)_realm, (pv_spec_t *)0,
00157 HDR_PROXYAUTH_T);
00158 }
00159
00160
00161
00162
00163
00164 int radius_proxy_authorize_2(struct sip_msg* _msg, char* _realm,
00165 char* _uri_user)
00166 {
00167 return authorize(_msg, (pv_elem_t*)_realm, (pv_spec_t *)_uri_user,
00168 HDR_PROXYAUTH_T);
00169 }
00170
00171
00172
00173
00174
00175 int radius_www_authorize(struct sip_msg* _msg, char* _realm, char* _s2)
00176 {
00177 return authorize(_msg, (pv_elem_t*)_realm, (pv_spec_t *)0,
00178 HDR_AUTHORIZATION_T);
00179 }