Browse code

API cleanup

Jan Janak authored on 13/03/2003 13:24:15
Showing 3 changed files
... ...
@@ -114,7 +114,7 @@ static inline int find_credentials(struct sip_msg* _m, str* _realm, int _hftype,
114 114
  * we should really authenticate (there must be no authentication for
115 115
  * ACK and CANCEL
116 116
  */
117
-auth_result_t pre_auth(struct sip_msg* _m, str** _realm, int _hftype, struct hdr_field** _h)
117
+auth_result_t pre_auth(struct sip_msg* _m, str* _realm, int _hftype, struct hdr_field** _h)
118 118
 {
119 119
 	int ret;
120 120
 	auth_body_t* c;
... ...
@@ -128,7 +128,7 @@ auth_result_t pre_auth(struct sip_msg* _m, str** _realm, int _hftype, struct hdr
128 128
 
129 129
 	if ((_m->REQ_METHOD == METHOD_ACK) ||  (_m->REQ_METHOD == METHOD_CANCEL)) return AUTHORIZED;
130 130
 
131
-	if ((*_realm)->len == 0) {
131
+	if (_realm->len == 0) {
132 132
 		if (get_realm(_m, &uri) < 0) {
133 133
 			LOG(L_ERR, "pre_auth(): Error while extracting realm\n");
134 134
 			if (send_resp(_m, 400, MESSAGE_400, 0, 0) == -1) {
... ...
@@ -137,14 +137,14 @@ auth_result_t pre_auth(struct sip_msg* _m, str** _realm, int _hftype, struct hdr
137 137
 			return ERROR;
138 138
 		}
139 139
 		
140
-		*_realm = &uri.host;
140
+		*_realm = uri.host;
141 141
 	}
142 142
 
143 143
 	     /* Try to find credentials with corresponding realm
144 144
 	      * in the message, parse them and return pointer to
145 145
 	      * parsed structure
146 146
 	      */
147
-	ret = find_credentials(_m, *_realm, _hftype, _h);
147
+	ret = find_credentials(_m, _realm, _hftype, _h);
148 148
 	if (ret < 0) {
149 149
 		LOG(L_ERR, "pre_auth(): Error while looking for credentials\n");
150 150
 		if (send_resp(_m, (ret == -2) ? 500 : 400, 
... ...
@@ -50,9 +50,9 @@ typedef enum auth_result {
50 50
  * we should really authenticate (there must be no authentication for
51 51
  * ACK and CANCEL
52 52
  */
53
-typedef auth_result_t (*pre_auth_f)(struct sip_msg* _m, str** _realm, int _hftype, struct hdr_field** _h);
53
+typedef auth_result_t (*pre_auth_f)(struct sip_msg* _m, str* _realm, int _hftype, struct hdr_field** _h);
54 54
 
55
-auth_result_t pre_auth(struct sip_msg* _m, str** _realm, int _hftype, struct hdr_field** _h);
55
+auth_result_t pre_auth(struct sip_msg* _m, str* _realm, int _hftype, struct hdr_field** _h);
56 56
 
57 57
 
58 58
 /*
... ...
@@ -46,7 +46,7 @@
46 46
 #define MESSAGE_500 "Server Internal Error"
47 47
 
48 48
 
49
-static inline int get_ha1(str* _user, str* _domain, str _realm, char* _table, char* _ha1)
49
+static inline int get_ha1(str* _user, str* _domain, int use_ha1b, char* _table, char* _ha1)
50 50
 {
51 51
 	db_key_t keys[2];
52 52
 	db_val_t vals[2];
... ...
@@ -56,7 +56,7 @@ static inline int get_ha1(str* _user, str* _domain, str _realm, char* _table, ch
56 56
 
57 57
 	keys[0] = username_column;
58 58
 	keys[1] = domain_column;
59
-	col[0] = pass_column;	
59
+	col[0] = (use_ha1b && !calc_ha1) ? (pass_column_2) : (pass_column);	
60 60
 
61 61
 	VAL_TYPE(vals) = VAL_TYPE(vals + 1) = DB_STR;
62 62
 	VAL_NULL(vals) = VAL_NULL(vals + 1) = 0;
... ...
@@ -64,18 +64,8 @@ static inline int get_ha1(str* _user, str* _domain, str _realm, char* _table, ch
64 64
 	VAL_STR(vals).s = _user->s;
65 65
 	VAL_STR(vals).len = _user->len;
66 66
 
67
-	printf("bhoj: %.*s\n", _realm.len, _realm.s);	
68
-	VAL_STR(vals + 1).s = _realm.s;
69
-	VAL_STR(vals + 1).len = _realm.len;
70
-
71
-
72
-	     /* If there is domain in username, we must use
73
-	      * another column holding HA1 calculated with the
74
-	      * domain
75
-	      */
76
-	if ((_domain->len) && !calc_ha1) {
77
-		col[0] = pass_column_2;
78
-	}
67
+	VAL_STR(vals + 1).s = _domain->s;
68
+	VAL_STR(vals + 1).len = _domain->len;
79 69
 
80 70
 	db_use_table(db_handle, _table);
81 71
 	if (db_query(db_handle, keys, 0, vals, col, 2, 1, 0, &res) < 0) {
... ...
@@ -85,9 +75,9 @@ static inline int get_ha1(str* _user, str* _domain, str _realm, char* _table, ch
85 75
 
86 76
 	if (RES_ROW_N(res) == 0) {
87 77
 		DBG("get_ha1(): no result for user \'%.*s@%.*s\'\n", 
88
-		    _user->len, _user->s, _realm.len, _realm.s);
78
+		    _user->len, _user->s, _domain->len, _domain->s);
89 79
 		db_free_query(db_handle, res);
90
-		return -1;
80
+		return 1;
91 81
 	}
92 82
 
93 83
         result.s = (char*)ROW_VALUES(RES_ROWS(res))[0].val.string_val;
... ...
@@ -96,7 +86,7 @@ static inline int get_ha1(str* _user, str* _domain, str _realm, char* _table, ch
96 86
 	if (calc_ha1) {
97 87
 		     /* Only plaintext passwords are stored in database,
98 88
 		      * we have to calculate HA1 */
99
-		calc_HA1(HA_MD5, _user, &_realm, &result, 0, 0, _ha1);
89
+		calc_HA1(HA_MD5, _user, _domain, &result, 0, 0, _ha1);
100 90
 		DBG("HA1 string calculated: %s\n", _ha1);
101 91
 	} else {
102 92
 		memcpy(_ha1, result.s, result.len);
... ...
@@ -160,8 +150,11 @@ static inline int authorize(struct sip_msg* _m, str* _realm, char* _table, int _
160 150
 	struct hdr_field* h;
161 151
 	auth_body_t* cred;
162 152
 	auth_result_t ret;
153
+	str domain;
163 154
 
164
-	ret = pre_auth_func(_m, &_realm, _hftype, &h);
155
+	domain = *_realm;
156
+
157
+	ret = pre_auth_func(_m, &domain, _hftype, &h);
165 158
 	
166 159
 	switch(ret) {
167 160
 	case ERROR:            return 0;
... ...
@@ -172,9 +165,7 @@ static inline int authorize(struct sip_msg* _m, str* _realm, char* _table, int _
172 165
 
173 166
 	cred = (auth_body_t*)h->parsed;
174 167
 
175
-	printf("ahoj: %.*s\n", _realm->len, _realm->s);
176
-
177
-	res = get_ha1(&cred->digest.username.user, &cred->digest.username.domain, *_realm, _table, ha1);
168
+	res = get_ha1(&cred->digest.username.user, &domain, cred->digest.username.domain.len, _table, ha1);
178 169
         if (res < 0) {
179 170
 		     /* Error while accessing the database */
180 171
 		if (sl_reply(_m, (char*)500, MESSAGE_500) == -1) {