Browse code

- support for digest credentials selects, supported is:

@proxy_authorization["realm"] -- Returns entire digest credentials as string
@proxy_authorization["realm"].username
@proxy_authorization["realm"].username.user
@proxy_authorization["realm"].username.domain
@proxy_authorization["realm"].realm
@proxy_authorization["realm"].nonce
@proxy_authorization["realm"].uri
@proxy_authorization["realm"].cnonce
@proxy_authorization["realm"].nc
@proxy_authorization["realm"].response
@proxy_authorization["realm"].opaque
@proxy_authorization["realm"].algorithm
@proxy_authorization["realm"].qop

Jan Janak authored on 27/06/2006 13:50:29
Showing 2 changed files
... ...
@@ -37,6 +37,7 @@
37 37
 #include "select_buf.h"
38 38
 #include "dprint.h"
39 39
 #include "trim.h"
40
+#include "ut.h"
40 41
 #include "parser/parser_f.h"
41 42
 #include "parser/hf.h"
42 43
 #include "parser/parse_from.h"
... ...
@@ -47,6 +48,7 @@
47 48
 #include "parser/parse_uri.h"
48 49
 #include "parser/parse_event.h"
49 50
 #include "parser/parse_rr.h"
51
+#include "parser/digest/digest.h"
50 52
 #include "mem/mem.h"
51 53
 #include "parser/parse_hname2.h"
52 54
 
... ...
@@ -794,6 +796,101 @@ int select_cseq_method(str* res, select_t* s, struct sip_msg* msg)
794 796
 	return 0;
795 797
 }
796 798
 
799
+static struct hdr_field* get_credentials(struct sip_msg* msg, select_t* s)
800
+{
801
+	int ret;
802
+	struct hdr_field* hdr;
803
+	str realm;
804
+	hdr_types_t hdr_type;
805
+
806
+	     /* Try to find credentials with corresponding realm
807
+	      * in the message, parse them and return pointer to
808
+	      * parsed structure
809
+	      */
810
+	realm = s->params[1].v.s;
811
+
812
+	switch (s->params[0].v.i) {
813
+	case SEL_AUTH_WWW:
814
+		hdr_type = HDR_AUTHORIZATION_T;
815
+		break;
816
+
817
+	case SEL_AUTH_PROXY:
818
+		hdr_type = HDR_PROXYAUTH_T;
819
+		break;
820
+
821
+	default:
822
+		BUG("Unexpected parameter value \"%d\"\n", s->params[0].v.i);
823
+		return 0;
824
+	}
825
+
826
+	ret = find_credentials(msg, &realm, hdr_type, &hdr);
827
+	if (ret < 0) {
828
+		ERR("Error while looking for credentials\n");
829
+		return 0;
830
+	} else if (ret > 0) {
831
+		return 0;
832
+	}
833
+
834
+	return hdr;
835
+}
836
+
837
+
838
+int select_auth(str* res, select_t* s, struct sip_msg* msg)
839
+{
840
+	struct hdr_field* hdr;
841
+
842
+	if (s->n != 2 && s->params[1].type != SEL_PARAM_STR) return -1;
843
+
844
+	if (s->params[0].type != SEL_PARAM_DIV) {
845
+		BUG("Last parameter should have type DIV (converted)\n");
846
+		return -1;
847
+	}
848
+
849
+        hdr = get_credentials(msg, s);
850
+	if (!hdr) return -1;
851
+	RETURN0_res(hdr->body);
852
+}
853
+
854
+int select_auth_param(str* res, select_t* s, struct sip_msg* msg)
855
+{
856
+	struct hdr_field* hdr;
857
+	dig_cred_t* cred;
858
+
859
+	if (s->n != 3 && s->n != 4 || s->params[s->n - 1].type != SEL_PARAM_DIV) return -1;
860
+
861
+	hdr = get_credentials(msg, s);
862
+	if (!hdr) return 1;
863
+	cred = &((auth_body_t*)hdr->parsed)->digest;
864
+
865
+	switch(s->params[s->n - 1].v.i) {
866
+	case SEL_AUTH_USER:     RETURN0_res(cred->username.user);
867
+	case SEL_AUTH_DOMAIN:   RETURN0_res(cred->username.domain);
868
+	case SEL_AUTH_USERNAME: RETURN0_res(cred->username.whole);
869
+	case SEL_AUTH_REALM:    RETURN0_res(cred->realm);
870
+	case SEL_AUTH_NONCE:    RETURN0_res(cred->nonce);
871
+	case SEL_AUTH_URI:      RETURN0_res(cred->uri);
872
+	case SEL_AUTH_CNONCE:   RETURN0_res(cred->cnonce);
873
+	case SEL_AUTH_NC:       RETURN0_res(cred->nc);
874
+	case SEL_AUTH_RESPONSE: RETURN0_res(cred->response);
875
+	case SEL_AUTH_OPAQUE:   RETURN0_res(cred->opaque);
876
+	case SEL_AUTH_ALG:      RETURN0_res(cred->alg.alg_str);
877
+	case SEL_AUTH_QOP:      RETURN0_res(cred->qop.qop_str);
878
+	default:
879
+		BUG("Unsupported digest credentials parameter in select\n");
880
+		return -1;
881
+	}
882
+}
883
+
884
+int select_auth_username(str* res, select_t* s, struct sip_msg* msg)
885
+{
886
+	return select_auth_param(res, s, msg);
887
+}
888
+
889
+int select_auth_username_comp(str* res, select_t* s, struct sip_msg* msg)
890
+{
891
+	return select_auth_param(res, s, msg);
892
+}
893
+
797 894
 ABSTRACT_F(select_any_nameaddr)
798 895
 
799 896
 int select_nameaddr_name(str* res, select_t* s, struct sip_msg* msg)
... ...
@@ -834,4 +931,3 @@ int select_nameaddr_uri(str* res, select_t* s, struct sip_msg* msg)
834 931
 	res->len=p-res->s;
835 932
 	return 0;
836 933
 }
837
-
... ...
@@ -44,6 +44,23 @@ enum {
44 44
 	SEL_PARAM_BRANCH, SEL_PARAM_RPORT, SEL_PARAM_I, SEL_PARAM_ALIAS
45 45
        };
46 46
 
47
+enum {
48
+	SEL_AUTH_PROXY,
49
+	SEL_AUTH_WWW,
50
+	SEL_AUTH_USERNAME,
51
+	SEL_AUTH_USER,
52
+	SEL_AUTH_DOMAIN,
53
+	SEL_AUTH_REALM,
54
+	SEL_AUTH_NONCE,
55
+	SEL_AUTH_URI,
56
+	SEL_AUTH_CNONCE,
57
+	SEL_AUTH_NC,
58
+	SEL_AUTH_RESPONSE,
59
+	SEL_AUTH_OPAQUE,
60
+	SEL_AUTH_ALG,
61
+	SEL_AUTH_QOP
62
+};
63
+
47 64
 SELECT_F(select_ruri)
48 65
 SELECT_F(select_from)
49 66
 SELECT_F(select_from_uri)
... ...
@@ -97,6 +114,11 @@ SELECT_F(select_cseq)
97 114
 SELECT_F(select_cseq_method)
98 115
 SELECT_F(select_cseq_num)
99 116
 
117
+SELECT_F(select_auth)
118
+SELECT_F(select_auth_param)
119
+SELECT_F(select_auth_username)
120
+SELECT_F(select_auth_username_comp)
121
+
100 122
 static select_row_t select_core[] = {
101 123
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("ruri"), select_ruri, 0},
102 124
 	{ select_ruri, SEL_PARAM_STR, STR_NULL, select_any_uri, NESTED},
... ...
@@ -166,6 +188,22 @@ static select_row_t select_core[] = {
166 188
 
167 189
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("msg"), select_msgheader, SEL_PARAM_EXPECTED},
168 190
 	{ select_msgheader, SEL_PARAM_STR, STR_NULL, select_anyheader, OPTIONAL | CONSUME_NEXT_INT | FIXUP_CALL},
191
+
192
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("proxy_authorization"), select_auth, CONSUME_NEXT_STR | DIVERSION | SEL_AUTH_PROXY},
193
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("authorization"), select_auth, CONSUME_NEXT_STR | DIVERSION | SEL_AUTH_WWW}, 
194
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("username"), select_auth_username, DIVERSION | SEL_AUTH_USERNAME},
195
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("realm"), select_auth_param, DIVERSION | SEL_AUTH_REALM},
196
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("nonce"), select_auth_param, DIVERSION | SEL_AUTH_NONCE},
197
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("uri"), select_auth_param, DIVERSION | SEL_AUTH_URI},
198
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("cnonce"), select_auth_param, DIVERSION | SEL_AUTH_CNONCE},
199
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("nc"), select_auth_param, DIVERSION | SEL_AUTH_NC},
200
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("response"), select_auth_param, DIVERSION | SEL_AUTH_RESPONSE},
201
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("opaque"), select_auth_param, DIVERSION | SEL_AUTH_OPAQUE},
202
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("algorithm"), select_auth_param, DIVERSION | SEL_AUTH_ALG},
203
+	{ select_auth, SEL_PARAM_STR, STR_STATIC_INIT("qop"), select_auth_param, DIVERSION | SEL_AUTH_QOP},
204
+	{ select_auth_username, SEL_PARAM_STR, STR_STATIC_INIT("user"), select_auth_username_comp, DIVERSION | SEL_AUTH_USER},
205
+	{ select_auth_username, SEL_PARAM_STR, STR_STATIC_INIT("domain"), select_auth_username_comp, DIVERSION | SEL_AUTH_DOMAIN},
206
+
169 207
 	{ select_anyheader, SEL_PARAM_STR, STR_STATIC_INIT("nameaddr"), select_any_nameaddr, NESTED | CONSUME_NEXT_STR},
170 208
 	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
171 209
 };