Browse code

- disable use_did parameter by default - indentation fixed - clean up DB related data structures only of they have really been created - get_ha1 passes DB errors to the caller

Jan Janak authored on 13/07/2007 08:45:46
Showing 2 changed files
... ...
@@ -102,7 +102,7 @@ str flags_column    = STR_STATIC_INIT(FLAGS_COL);
102 102
 str plain_password_column   = STR_STATIC_INIT(PLAIN_PASS_COL);
103 103
 
104 104
 int calc_ha1 = 0;
105
-int use_did = 1;
105
+int use_did = 0;
106 106
 
107 107
 db_ctx_t* auth_db_handle = 0;      /* database connection handle */
108 108
 auth_api_t auth_api;
... ...
@@ -248,7 +248,10 @@ static int child_init(int rank)
248 248
 
249 249
 err:
250 250
 
251
-	if (auth_db_handle) db_ctx_free(auth_db_handle);
251
+	if (auth_db_handle) {
252
+		auth_db_handle = NULL;
253
+		db_ctx_free(auth_db_handle);
254
+	}
252 255
 
253 256
 	ERR("Error while initializing database layer\n");
254 257
 	return -1;
... ...
@@ -83,6 +83,7 @@ static inline int get_ha1(struct username* username, str* did, str* realm,
83 83
 
84 84
 	if (db_exec(res, q) < 0 ) {
85 85
 		ERR("Error while querying database\n");
86
+		return -1;
86 87
 	}
87 88
 
88 89
 	if (*res) *row = db_first(*res);
... ...
@@ -245,70 +246,70 @@ static inline int authenticate(struct sip_msg* msg, str* realm, authdb_table_inf
245 246
     switch(auth_api.pre_auth(msg, realm, hftype, &h)) {
246 247
     case ERROR:
247 248
     case BAD_CREDENTIALS:
248
-	ret = -3;
249
-	goto end;
250
-	
249
+		ret = -3;
250
+		goto end;
251
+		
251 252
     case NOT_AUTHENTICATED: 
252
-	ret = -1;
253
-	goto end;
254
-	
253
+		ret = -1;
254
+		goto end;
255
+		
255 256
     case DO_AUTHENTICATION: 
256
-	break;
257
-	
257
+		break;
258
+		
258 259
     case AUTHENTICATED:
259
-	ret = 1; 
260
-	goto end;
260
+		ret = 1; 
261
+		goto end;
261 262
     }
262 263
     
263 264
     cred = (auth_body_t*)h->parsed;
264
-
265
+	
265 266
     if (use_did) {
266
-	if (msg->REQ_METHOD == METHOD_REGISTER) {
267
-	    ret = get_to_did(&did, msg);
268
-	} else {
269
-	    ret = get_from_did(&did, msg);
270
-	}
271
-	if (ret == 0) {
272
-	    did.s = DEFAULT_DID;
273
-	    did.len = sizeof(DEFAULT_DID) - 1;
274
-	}
267
+		if (msg->REQ_METHOD == METHOD_REGISTER) {
268
+			ret = get_to_did(&did, msg);
269
+		} else {
270
+			ret = get_from_did(&did, msg);
271
+		}
272
+		if (ret == 0) {
273
+			did.s = DEFAULT_DID;
274
+			did.len = sizeof(DEFAULT_DID) - 1;
275
+		}
275 276
     } else {
276
-	did.len = 0;
277
-	did.s = 0;
277
+		did.len = 0;
278
+		did.s = 0;
278 279
     }
279
-
280
+	
280 281
     res = get_ha1(&cred->digest.username, &did, realm, table, ha1, &result, &row);
281 282
     if (res < 0) {
282
-	ret = -2;
283
-	goto end;
283
+		ret = -2;
284
+		goto end;
284 285
     }
285 286
     if (res > 0) {
286
-	     /* Username not found in the database */
287
-	ret = -1;
288
-	goto end;
287
+		/* Username not found in the database */
288
+		ret = -1;
289
+		goto end;
289 290
     }
290 291
     
291
-	 /* Recalculate response, it must be same to authorize successfully */
292
+	/* Recalculate response, it must be same to authorize successfully */
292 293
     if (!check_response(&(cred->digest), &msg->first_line.u.request.method, ha1)) {
293
-	switch(auth_api.post_auth(msg, h)) {
294
-	case ERROR:
295
-	case BAD_CREDENTIALS:
296
-	    ret = -2; 
297
-	    break;
298
-	    
299
-	case NOT_AUTHENTICATED: 
300
-	    ret = -1; 
301
-	    break;
302
-	    
303
-	case AUTHENTICATED:
304
-	    generate_avps(result, row);
305
-	    ret = 1;
306
-	    break;
307
-	    
308
-	default:
309
-	    ret = -1;
310
-	    break;
311
-	}
294
+		switch(auth_api.post_auth(msg, h)) {
295
+		case ERROR:
296
+		case BAD_CREDENTIALS:
297
+			ret = -2; 
298
+			break;
299
+			
300
+		case NOT_AUTHENTICATED: 
301
+			ret = -1; 
302
+			break;
303
+			
304
+		case AUTHENTICATED:
305
+			generate_avps(result, row);
306
+			ret = 1;
307
+			break;
308
+			
309
+		default:
310
+			ret = -1;
311
+			break;
312
+		}
312 313
     } else {
313 314
 		ret = -1;
314 315
 	}
... ...
@@ -316,10 +317,10 @@ static inline int authenticate(struct sip_msg* msg, str* realm, authdb_table_inf
316 317
  end:
317 318
     if (result) db_res_free(result);
318 319
     if (ret < 0) {
319
-	if (auth_api.build_challenge(msg, (cred ? cred->stale : 0), realm, hftype) < 0) {
320
-	    ERR("Error while creating challenge\n");
321
-	    ret = -2;
322
-	}
320
+		if (auth_api.build_challenge(msg, (cred ? cred->stale : 0), realm, hftype) < 0) {
321
+			ERR("Error while creating challenge\n");
322
+			ret = -2;
323
+		}
323 324
     }
324 325
     return ret;
325 326
 }