2
0

api.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * $Id$
  3. *
  4. * Digest Authentication Module
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #ifndef API_H
  30. #define API_H
  31. #include "../../parser/msg_parser.h"
  32. #include "../../parser/digest/digest.h"
  33. #include "../../sr_module.h"
  34. #include "../../usr_avp.h"
  35. #include "../../parser/hf.h"
  36. #include "../../str.h"
  37. #include "challenge.h"
  38. #include "rfc2617.h"
  39. /**
  40. * return codes to config by auth functions
  41. */
  42. typedef enum auth_cfg_result {
  43. AUTH_USER_MISMATCH = -8, /*!< Auth user != From/To user */
  44. AUTH_NONCE_REUSED = -6, /*!< Returned if nonce is used more than once */
  45. AUTH_NO_CREDENTIALS = -5, /*!< Credentials missing */
  46. AUTH_STALE_NONCE = -4, /*!< Stale nonce */
  47. AUTH_USER_UNKNOWN = -3, /*!< User not found */
  48. AUTH_INVALID_PASSWORD = -2, /*!< Invalid password */
  49. AUTH_ERROR = -1, /*!< Error occurred */
  50. AUTH_DROP = 0, /*!< Error, stop config execution */
  51. AUTH_OK = 1 /*!< Success */
  52. } auth_cfg_result_t;
  53. /**
  54. * flags for checks in auth functions
  55. */
  56. #define AUTH_CHECK_ID_F 1<<0
  57. #define AUTH_CHECK_SKIPFWD_F 1<<1
  58. /**
  59. * return codes to auth API functions
  60. */
  61. typedef enum auth_result {
  62. NONCE_REUSED = -5, /* Returned if nonce is used more than once */
  63. NO_CREDENTIALS, /* Credentials missing */
  64. STALE_NONCE, /* Stale nonce */
  65. ERROR, /* Error occurred, a reply has been sent out -> return 0 to the ser core */
  66. NOT_AUTHENTICATED, /* Don't perform authentication, credentials missing */
  67. DO_AUTHENTICATION, /* Perform digest authentication */
  68. AUTHENTICATED, /* Authenticated by default, no digest authentication necessary */
  69. BAD_CREDENTIALS, /* Digest credentials are malformed */
  70. CREATE_CHALLENGE, /* when AKAv1-MD5 is used first request does not contain credentials,
  71. * only usename, realm and algorithm. Server should get Authentication
  72. * Vector from AuC/HSS, create challenge and send it to the UE. */
  73. DO_RESYNCHRONIZATION /* When AUTS is received we need do resynchronization
  74. * of sequnce numbers with mobile station. */
  75. } auth_result_t;
  76. typedef int (*check_auth_hdr_t)(struct sip_msg* msg, auth_body_t* auth_body,
  77. auth_result_t* auth_res);
  78. int check_auth_hdr(struct sip_msg* msg, auth_body_t* auth_body,
  79. auth_result_t* auth_res);
  80. /*
  81. * Purpose of this function is to find credentials with given realm,
  82. * do sanity check, validate credential correctness and determine if
  83. * we should really authenticate (there must be no authentication for
  84. * ACK and CANCEL
  85. */
  86. typedef auth_result_t (*pre_auth_t)(struct sip_msg* msg, str* realm,
  87. hdr_types_t hftype, struct hdr_field** hdr,
  88. check_auth_hdr_t check_auth_hdr);
  89. auth_result_t pre_auth(struct sip_msg* msg, str* realm, hdr_types_t hftype,
  90. struct hdr_field** hdr, check_auth_hdr_t check_auth_hdr);
  91. /*
  92. * Purpose of this function is to do post authentication steps like
  93. * marking authorized credentials and so on.
  94. */
  95. typedef auth_result_t (*post_auth_t)(struct sip_msg* msg,
  96. struct hdr_field* hdr);
  97. auth_result_t post_auth(struct sip_msg* msg, struct hdr_field* hdr);
  98. typedef int (*check_response_t)(dig_cred_t* cred, str* method, char* ha1);
  99. int auth_check_response(dig_cred_t* cred, str* method, char* ha1);
  100. typedef int (*auth_challenge_f)(struct sip_msg *msg, str *realm, int flags,
  101. int hftype);
  102. int auth_challenge(struct sip_msg *msg, str *realm, int flags,
  103. int hftype);
  104. typedef int (*pv_authenticate_f)(struct sip_msg *msg, str *realm, str *passwd,
  105. int flags, int hftype, str *method);
  106. int pv_authenticate(struct sip_msg *msg, str *realm, str *passwd,
  107. int flags, int hftype, str *method);
  108. typedef int (*consume_credentials_f)(struct sip_msg* msg);
  109. int consume_credentials(struct sip_msg* msg);
  110. /*
  111. * Auth module API
  112. */
  113. typedef struct auth_api_s {
  114. pre_auth_t pre_auth; /* The function to be called before authentication */
  115. post_auth_t post_auth; /* The function to be called after authentication */
  116. build_challenge_hf_t build_challenge; /* Function to build digest challenge header */
  117. struct qp* qop; /* qop module parameter */
  118. calc_HA1_t calc_HA1;
  119. calc_response_t calc_response;
  120. check_response_t check_response;
  121. auth_challenge_f auth_challenge;
  122. pv_authenticate_f pv_authenticate;
  123. consume_credentials_f consume_credentials;
  124. } auth_api_s_t;
  125. typedef int (*bind_auth_s_t)(auth_api_s_t* api);
  126. int bind_auth_s(auth_api_s_t* api);
  127. /**
  128. * load AUTH module API
  129. */
  130. static inline int auth_load_api(auth_api_s_t* api)
  131. {
  132. bind_auth_s_t bind_auth;
  133. /* bind to auth module and import the API */
  134. bind_auth = (bind_auth_s_t)find_export("bind_auth_s", 0, 0);
  135. if (!bind_auth) {
  136. LM_ERR("unable to find bind_auth function. Check if you load"
  137. " the auth module.\n");
  138. return -1;
  139. }
  140. if (bind_auth(api) < 0) {
  141. LM_ERR("unable to bind auth module\n");
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. #endif /* API_H */