Explorar o código

auth: several functions exported via inter-module API

Daniel-Constantin Mierla %!s(int64=15) %!d(string=hai) anos
pai
achega
dc15caf830
Modificáronse 3 ficheiros con 57 adicións e 8 borrados
  1. 3 0
      modules/auth/api.c
  2. 41 3
      modules/auth/api.h
  3. 13 5
      modules/auth/auth_mod.c

+ 3 - 0
modules/auth/api.c

@@ -232,5 +232,8 @@ int bind_auth_s(auth_api_s_t* api)
 	api->calc_HA1 = calc_HA1;
 	api->calc_response = calc_response;
 	api->check_response = auth_check_response;
+	api->auth_challenge = auth_challenge;
+	api->pv_authenticate = pv_authenticate;
+	api->consume_credentials = consume_credentials;
 	return 0;
 }

+ 41 - 3
modules/auth/api.h

@@ -33,6 +33,7 @@
 
 #include "../../parser/msg_parser.h"
 #include "../../parser/digest/digest.h"
+#include "../../sr_module.h"
 #include "../../usr_avp.h"
 #include "../../parser/hf.h"
 #include "../../str.h"
@@ -96,6 +97,19 @@ auth_result_t post_auth(struct sip_msg* msg, struct hdr_field* hdr);
 typedef int (*check_response_t)(dig_cred_t* cred, str* method, char* ha1);
 int auth_check_response(dig_cred_t* cred, str* method, char* ha1);
 
+typedef int (*auth_challenge_f)(struct sip_msg *msg, str *realm, int flags,
+		int hftype);
+int auth_challenge(struct sip_msg *msg, str *realm, int flags,
+		int hftype);
+
+typedef int (*pv_authenticate_f)(struct sip_msg *msg, str *realm, str *passwd,
+		int flags, int hftype);
+int pv_authenticate(struct sip_msg *msg, str *realm, str *passwd,
+		int flags, int hftype);
+
+typedef int (*consume_credentials_f)(struct sip_msg* msg);
+int consume_credentials(struct sip_msg* msg);
+
 /*
  * Auth module API
  */
@@ -104,13 +118,37 @@ typedef struct auth_api_s {
     post_auth_t post_auth;                /* The function to be called after authentication */
     build_challenge_hf_t build_challenge; /* Function to build digest challenge header */
     struct qp* qop;                       /* qop module parameter */
-	calc_HA1_t      calc_HA1;
-	calc_response_t calc_response;
-	check_response_t check_response;
+	calc_HA1_t         calc_HA1;
+	calc_response_t    calc_response;
+	check_response_t   check_response;
+	auth_challenge_f   auth_challenge;
+	pv_authenticate_f  pv_authenticate;
+	consume_credentials_f consume_credentials;
 } auth_api_s_t;
 
 typedef int (*bind_auth_s_t)(auth_api_s_t* api);
 int bind_auth_s(auth_api_s_t* api);
 
+/**
+ * load AUTH module API
+ */
+static inline int auth_load_api(auth_api_s_t* api)
+{
+	bind_auth_s_t bind_auth;
+
+	/* bind to auth module and import the API */
+	bind_auth = (bind_auth_s_t)find_export("bind_auth_s", 0, 0);
+	if (!bind_auth) {
+		LM_ERR("unable to find bind_auth function. Check if you load"
+				" the auth module.\n");
+		return -1;
+	}
+
+	if (bind_auth(api) < 0) {
+		LM_ERR("unable to bind auth module\n");
+		return -1;
+	}
+	return 0;
+}
 
 #endif /* API_H */

+ 13 - 5
modules/auth/auth_mod.c

@@ -77,7 +77,7 @@ static int mod_init(void);
 /*
  * Remove used credentials from a SIP message header
  */
-int consume_credentials(struct sip_msg* msg, char* s1, char* s2);
+int w_consume_credentials(struct sip_msg* msg, char* s1, char* s2);
 
 static int pv_proxy_authenticate(struct sip_msg* msg, char* realm,
 		char *passwd, char *flags);
@@ -132,7 +132,7 @@ sl_api_t slb;
  * Exported functions
  */
 static cmd_export_t cmds[] = {
-    {"consume_credentials",    consume_credentials,                  0,
+    {"consume_credentials",    w_consume_credentials,                0,
 			0, REQUEST_ROUTE},
     {"www_challenge",          (cmd_function)www_challenge,          2,
 			fixup_auth_challenge, REQUEST_ROUTE},
@@ -355,7 +355,7 @@ static void destroy(void)
 /*
  * Remove used credentials from a SIP message header
  */
-int consume_credentials(struct sip_msg* msg, char* s1, char* s2)
+int consume_credentials(struct sip_msg* msg)
 {
     struct hdr_field* h;
     int len;
@@ -383,10 +383,18 @@ int consume_credentials(struct sip_msg* msg, char* s1, char* s2)
     return 1;
 }
 
+/**
+ *
+ */
+int w_consume_credentials(struct sip_msg* msg, char* s1, char* s2)
+{
+	return consume_credentials(msg);
+}
+
 /**
  * @brief do WWW-Digest authentication with password taken from cfg var
  */
-static int pv_authenticate(struct sip_msg *msg, str *realm, str *passwd,
+int pv_authenticate(struct sip_msg *msg, str *realm, str *passwd,
 		int flags, int hftype)
 {
 	struct hdr_field* h;
@@ -613,7 +621,7 @@ static int auth_send_reply(struct sip_msg *msg, int code, char *reason,
 /**
  *
  */
-static int auth_challenge(struct sip_msg *msg, str *realm, int flags, int hftype)
+int auth_challenge(struct sip_msg *msg, str *realm, int flags, int hftype)
 {
 	int ret;
     str hf = {0, 0};