auth_plugin_delayed.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@CFILE auth_plugin_delayed.c
  25. *
  26. * @brief Plugin for delayed authentication.
  27. *
  28. * This authentication plugin provides authentication operation that is
  29. * intentionally delayed. It serves as an example of server-side
  30. * authentication plugins.
  31. *
  32. * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
  33. *
  34. * @date Created: Wed Apr 11 15:14:03 2001 ppessi
  35. */
  36. #include "config.h"
  37. #include <stddef.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <stdio.h>
  41. #include <assert.h>
  42. #define SU_MSG_ARG_T struct auth_splugin_t
  43. #if HAVE_FUNC
  44. #elif HAVE_FUNCTION
  45. #define __func__ __FUNCTION__
  46. #else
  47. static char const __func__[] = "auth_plugin_delayed";
  48. #endif
  49. #include <sofia-sip/su_debug.h>
  50. #include <sofia-sip/su_wait.h>
  51. #include <sofia-sip/su_alloc.h>
  52. #include <sofia-sip/su_tagarg.h>
  53. #include "sofia-sip/auth_module.h"
  54. #include "sofia-sip/auth_plugin.h"
  55. struct auth_plugin_t
  56. {
  57. su_root_t *ap_root;
  58. auth_scheme_t *ap_base;
  59. auth_splugin_t *ap_list;
  60. auth_splugin_t**ap_tail;
  61. };
  62. /* Digest (or Basic) with delay */
  63. static int delayed_auth_init(auth_mod_t *am,
  64. auth_scheme_t *base,
  65. su_root_t *root,
  66. tag_type_t tag, tag_value_t value, ...);
  67. static void delayed_auth_method(auth_mod_t *am,
  68. auth_status_t *as,
  69. msg_auth_t *auth,
  70. auth_challenger_t const *ach);
  71. static void delayed_auth_challenge(auth_mod_t *am,
  72. auth_status_t *as,
  73. auth_challenger_t const *ach);
  74. static void delayed_auth_cancel(auth_mod_t *am, auth_status_t *as);
  75. static void delayed_auth_destroy(auth_mod_t *am);
  76. auth_scheme_t auth_scheme_delayed[1] =
  77. {{
  78. "Delayed",
  79. sizeof (struct { auth_mod_t mod[1]; auth_plugin_t plug[1]; }),
  80. delayed_auth_init,
  81. delayed_auth_method,
  82. delayed_auth_challenge,
  83. delayed_auth_cancel,
  84. delayed_auth_destroy
  85. }};
  86. static int delayed_auth_init(auth_mod_t *am,
  87. auth_scheme_t *base,
  88. su_root_t *root,
  89. tag_type_t tag, tag_value_t value, ...)
  90. {
  91. auth_plugin_t *ap = AUTH_PLUGIN(am);
  92. int retval = -1;
  93. ta_list ta;
  94. ta_start(ta, tag, value);
  95. if (root && base && auth_init_default(am, base, root, ta_tags(ta)) != -1) {
  96. ap->ap_root = root;
  97. ap->ap_base = base;
  98. ap->ap_tail = &ap->ap_list;
  99. retval = 0;
  100. }
  101. ta_end(ta);
  102. return retval;
  103. }
  104. struct auth_splugin_t
  105. {
  106. void const *asp_cookie;
  107. auth_splugin_t *asp_next;
  108. auth_splugin_t **asp_prev;
  109. auth_mod_t *asp_am;
  110. auth_status_t *asp_as;
  111. msg_auth_t *asp_header;
  112. auth_challenger_t const *asp_ach;
  113. int asp_canceled;
  114. };
  115. /* This is unique identifier */
  116. #define delayed_asp_cookie ((void const *)(intptr_t)delayed_auth_cancel)
  117. static void delayed_auth_method_recv(su_root_magic_t *rm,
  118. su_msg_r msg,
  119. auth_splugin_t *u);
  120. static void delayed_auth_method(auth_mod_t *am,
  121. auth_status_t *as,
  122. msg_auth_t *auth,
  123. auth_challenger_t const *ach)
  124. {
  125. auth_plugin_t *ap = AUTH_PLUGIN(am);
  126. su_msg_r mamc = SU_MSG_R_INIT;
  127. auth_splugin_t *asp;
  128. if (su_msg_create(mamc,
  129. su_root_task(ap->ap_root),
  130. su_root_task(ap->ap_root),
  131. delayed_auth_method_recv,
  132. sizeof *asp) == SU_FAILURE) {
  133. as->as_status = 500;
  134. as->as_phrase = "Asynchronous authentication failure";
  135. return;
  136. }
  137. asp = su_msg_data(mamc); assert(asp);
  138. asp->asp_cookie = delayed_asp_cookie;
  139. asp->asp_am = am;
  140. asp->asp_as = as;
  141. asp->asp_header = auth;
  142. asp->asp_ach = ach;
  143. asp->asp_canceled = 0;
  144. if (su_msg_send(mamc) == SU_FAILURE) {
  145. su_msg_destroy(mamc);
  146. as->as_status = 500;
  147. as->as_phrase = "Asynchronous authentication failure";
  148. return;
  149. }
  150. as->as_plugin = asp;
  151. as->as_status = 100;
  152. as->as_phrase = "Trying";
  153. return;
  154. }
  155. static void delayed_auth_method_recv(su_root_magic_t *rm,
  156. su_msg_r msg,
  157. auth_splugin_t *asp)
  158. {
  159. auth_mod_t *am = asp->asp_am;
  160. auth_plugin_t *ap = AUTH_PLUGIN(am);
  161. if (asp->asp_canceled)
  162. return;
  163. ap->ap_base->asch_check(am, asp->asp_as, asp->asp_header, asp->asp_ach);
  164. if (asp->asp_as->as_callback)
  165. asp->asp_as->as_callback(asp->asp_as->as_magic, asp->asp_as);
  166. }
  167. static void delayed_auth_challenge(auth_mod_t *am,
  168. auth_status_t *as,
  169. auth_challenger_t const *ach)
  170. {
  171. auth_plugin_t *ap = AUTH_PLUGIN(am);
  172. /* Invoke member function of base scheme */
  173. ap->ap_base->asch_challenge(am, as, ach);
  174. }
  175. static void delayed_auth_cancel(auth_mod_t *am, auth_status_t *as)
  176. {
  177. auth_plugin_t *ap = AUTH_PLUGIN(am);
  178. (void)ap; /* xyzzy */
  179. if (as->as_plugin && as->as_plugin->asp_cookie == delayed_asp_cookie)
  180. as->as_plugin->asp_canceled = 1;
  181. as->as_status = 500, as->as_phrase = "Authentication canceled";
  182. }
  183. static void delayed_auth_destroy(auth_mod_t *am)
  184. {
  185. auth_plugin_t *ap = AUTH_PLUGIN(am);
  186. /* Invoke member function of base scheme */
  187. ap->ap_base->asch_destroy(am);
  188. }