Add missing module dependencies to various res_sip* modules
[asterisk/asterisk.git] / res / res_sip_outbound_authenticator_digest.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_sip</depend>
22         <support_level>core</support_level>
23  ***/
24
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28
29 #include "asterisk/res_sip.h"
30 #include "asterisk/logger.h"
31 #include "asterisk/module.h"
32 #include "asterisk/strings.h"
33
34 static int set_outbound_authentication_credentials(pjsip_auth_clt_sess *auth_sess, const char **auth_strs, size_t num_auths)
35 {
36         struct ast_sip_auth **auths = ast_alloca(num_auths * sizeof(*auths));
37         pjsip_cred_info *auth_creds = ast_alloca(num_auths * sizeof(*auth_creds));
38         int res = 0;
39         int i;
40
41         if (ast_sip_retrieve_auths(auth_strs, num_auths, auths)) {
42                 res = -1;
43                 goto cleanup;
44         }
45
46         for (i = 0; i < num_auths; ++i) {
47                 pj_cstr(&auth_creds[i].realm, auths[i]->realm);
48                 pj_cstr(&auth_creds[i].username, auths[i]->auth_user);
49                 pj_cstr(&auth_creds[i].scheme, "digest");
50                 switch (auths[i]->type) {
51                 case AST_SIP_AUTH_TYPE_USER_PASS:
52                         pj_cstr(&auth_creds[i].data, auths[i]->auth_pass);
53                         auth_creds[i].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
54                         break;
55                 case AST_SIP_AUTH_TYPE_MD5:
56                         pj_cstr(&auth_creds[i].data, auths[i]->md5_creds);
57                         auth_creds[i].data_type = PJSIP_CRED_DATA_DIGEST;
58                         break;
59                 }
60         }
61
62         pjsip_auth_clt_set_credentials(auth_sess, num_auths, auth_creds);
63
64 cleanup:
65         ast_sip_cleanup_auths(auths, num_auths);
66         return res;
67 }
68
69 static int digest_create_request_with_auth(const char **auths, size_t num_auths, pjsip_rx_data *challenge,
70                 pjsip_transaction *tsx, pjsip_tx_data **new_request)
71 {
72         pjsip_auth_clt_sess auth_sess;
73
74         if (pjsip_auth_clt_init(&auth_sess, ast_sip_get_pjsip_endpoint(),
75                                 tsx->pool, 0) != PJ_SUCCESS) {
76                 ast_log(LOG_WARNING, "Failed to initialize client authentication session\n");
77                 return -1;
78         }
79
80         if (set_outbound_authentication_credentials(&auth_sess, auths, num_auths)) {
81                 ast_log(LOG_WARNING, "Failed to set authentication credentials\n");
82                 return -1;
83         }
84
85         if (pjsip_auth_clt_reinit_req(&auth_sess, challenge,
86                                 tsx->last_tx, new_request) != PJ_SUCCESS) {
87                 ast_log(LOG_WARNING, "Failed to create new request with authentication credentials\n");
88                 return -1;
89         }
90
91         return 0;
92 }
93
94 static struct ast_sip_outbound_authenticator digest_authenticator = {
95         .create_request_with_auth = digest_create_request_with_auth,
96 };
97
98 static int load_module(void)
99 {
100         if (ast_sip_register_outbound_authenticator(&digest_authenticator)) {
101                 return AST_MODULE_LOAD_DECLINE;
102         }
103         return AST_MODULE_LOAD_SUCCESS;
104 }
105
106 static int unload_module(void)
107 {
108         ast_sip_unregister_outbound_authenticator(&digest_authenticator);
109         return 0;
110 }
111
112 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP authentication resource",
113                 .load = load_module,
114                 .unload = unload_module,
115                 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
116 );