plugin_common.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Generic SASL plugin utility functions
  2. * Rob Siemborski
  3. */
  4. /*
  5. * Copyright (c) 1998-2016 Carnegie Mellon University. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The name "Carnegie Mellon University" must not be used to
  20. * endorse or promote products derived from this software without
  21. * prior written permission. For permission or any other legal
  22. * details, please contact
  23. * Carnegie Mellon University
  24. * Center for Technology Transfer and Enterprise Creation
  25. * 4615 Forbes Avenue
  26. * Suite 302
  27. * Pittsburgh, PA 15213
  28. * (412) 268-7393, fax: (412) 268-7395
  29. * innovation@andrew.cmu.edu
  30. *
  31. * 4. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by Computing Services
  34. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  35. *
  36. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  37. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  38. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  39. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  40. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  41. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  42. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  43. */
  44. #ifndef _PLUGIN_COMMON_H_
  45. #define _PLUGIN_COMMON_H_
  46. #include <config.h>
  47. #ifndef macintosh
  48. #ifdef WIN32
  49. # include <winsock2.h>
  50. #else
  51. # include <sys/socket.h>
  52. # include <netinet/in.h>
  53. # include <arpa/inet.h>
  54. # include <netdb.h>
  55. #endif /* WIN32 */
  56. #endif /* macintosh */
  57. #include <sasl.h>
  58. #include <saslutil.h>
  59. #include <saslplug.h>
  60. #ifdef WIN32
  61. #define PLUG_API __declspec(dllexport)
  62. #else
  63. #define PLUG_API extern
  64. #endif
  65. #define SASL_CLIENT_PLUG_INIT( x ) \
  66. extern sasl_client_plug_init_t x##_client_plug_init; \
  67. PLUG_API int sasl_client_plug_init(const sasl_utils_t *utils, \
  68. int maxversion, int *out_version, \
  69. sasl_client_plug_t **pluglist, \
  70. int *plugcount) { \
  71. return x##_client_plug_init(utils, maxversion, out_version, \
  72. pluglist, plugcount); \
  73. }
  74. #define SASL_SERVER_PLUG_INIT( x ) \
  75. extern sasl_server_plug_init_t x##_server_plug_init; \
  76. PLUG_API int sasl_server_plug_init(const sasl_utils_t *utils, \
  77. int maxversion, int *out_version, \
  78. sasl_server_plug_t **pluglist, \
  79. int *plugcount) { \
  80. return x##_server_plug_init(utils, maxversion, out_version, \
  81. pluglist, plugcount); \
  82. }
  83. #define SASL_AUXPROP_PLUG_INIT( x ) \
  84. extern sasl_auxprop_init_t x##_auxprop_plug_init; \
  85. PLUG_API int sasl_auxprop_plug_init(const sasl_utils_t *utils, \
  86. int maxversion, int *out_version, \
  87. sasl_auxprop_plug_t **plug, \
  88. const char *plugname) {\
  89. return x##_auxprop_plug_init(utils, maxversion, out_version, \
  90. plug, plugname); \
  91. }
  92. #define SASL_CANONUSER_PLUG_INIT( x ) \
  93. extern sasl_canonuser_init_t x##_canonuser_plug_init; \
  94. PLUG_API int sasl_canonuser_init(const sasl_utils_t *utils, \
  95. int maxversion, int *out_version, \
  96. sasl_canonuser_plug_t **plug, \
  97. const char *plugname) {\
  98. return x##_canonuser_plug_init(utils, maxversion, out_version, \
  99. plug, plugname); \
  100. }
  101. /* note: msg cannot include additional variables, so if you want to
  102. * do a printf-format string, then you need to call seterror yourself */
  103. #define SETERROR( utils, msg ) (utils)->seterror( (utils)->conn, 0, (msg) )
  104. #ifndef MEMERROR
  105. #define MEMERROR( utils ) \
  106. (utils)->seterror( (utils)->conn, 0, \
  107. "Out of Memory in " __FILE__ " near line %d", __LINE__ )
  108. #endif
  109. #ifndef PARAMERROR
  110. #define PARAMERROR( utils ) \
  111. (utils)->seterror( (utils)->conn, 0, \
  112. "Parameter Error in " __FILE__ " near line %d", __LINE__ )
  113. #endif
  114. #ifndef SASLINT_H
  115. typedef struct buffer_info
  116. {
  117. char *data;
  118. unsigned curlen; /* Current length of data in buffer */
  119. unsigned reallen; /* total length of buffer (>= curlen) */
  120. } buffer_info_t;
  121. #ifndef HAVE_GETHOSTNAME
  122. #ifdef sun
  123. /* gotta define gethostname ourselves on suns */
  124. extern int gethostname(char *, int);
  125. #endif
  126. #endif /* HAVE_GETHOSTNAME */
  127. #endif /* SASLINT_H */
  128. #ifdef __cplusplus
  129. extern "C" {
  130. #endif
  131. int _plug_ipfromstring(const sasl_utils_t *utils, const char *addr,
  132. struct sockaddr *out, socklen_t outlen);
  133. int _plug_iovec_to_buf(const sasl_utils_t *utils, const struct iovec *vec,
  134. unsigned numiov, buffer_info_t **output);
  135. int _plug_buf_alloc(const sasl_utils_t *utils, char **rwbuf,
  136. unsigned *curlen, unsigned newlen);
  137. int _plug_strdup(const sasl_utils_t * utils, const char *in,
  138. char **out, int *outlen);
  139. void _plug_free_string(const sasl_utils_t *utils, char **str);
  140. void _plug_free_secret(const sasl_utils_t *utils, sasl_secret_t **secret);
  141. #define _plug_get_userid(utils, result, prompt_need) \
  142. _plug_get_simple(utils, SASL_CB_USER, 0, result, prompt_need)
  143. #define _plug_get_authid(utils, result, prompt_need) \
  144. _plug_get_simple(utils, SASL_CB_AUTHNAME, 1, result, prompt_need)
  145. int _plug_get_simple(const sasl_utils_t *utils, unsigned int id, int required,
  146. const char **result, sasl_interact_t **prompt_need);
  147. int _plug_get_password(const sasl_utils_t *utils, sasl_secret_t **secret,
  148. unsigned int *iscopy, sasl_interact_t **prompt_need);
  149. int _plug_challenge_prompt(const sasl_utils_t *utils, unsigned int id,
  150. const char *challenge, const char *promptstr,
  151. const char **result, sasl_interact_t **prompt_need);
  152. int _plug_get_realm(const sasl_utils_t *utils, const char **availrealms,
  153. const char **realm, sasl_interact_t **prompt_need);
  154. int _plug_make_prompts(const sasl_utils_t *utils,
  155. sasl_interact_t **prompts_res,
  156. const char *user_prompt, const char *user_def,
  157. const char *auth_prompt, const char *auth_def,
  158. const char *pass_prompt, const char *pass_def,
  159. const char *echo_chal,
  160. const char *echo_prompt, const char *echo_def,
  161. const char *realm_chal,
  162. const char *realm_prompt, const char *realm_def);
  163. typedef struct decode_context {
  164. const sasl_utils_t *utils;
  165. unsigned int needsize; /* How much of the 4-byte size do we need? */
  166. char sizebuf[4]; /* Buffer to accumulate the 4-byte size */
  167. unsigned int size; /* Absolute size of the encoded packet */
  168. char *buffer; /* Buffer to accumulate an encoded packet */
  169. unsigned int cursize; /* Amount of packet data in the buffer */
  170. unsigned int in_maxbuf; /* Maximum allowed size of an incoming encoded packet */
  171. } decode_context_t;
  172. void _plug_decode_init(decode_context_t *text,
  173. const sasl_utils_t *utils, unsigned int in_maxbuf);
  174. int _plug_decode(decode_context_t *text,
  175. const char *input, unsigned inputlen,
  176. char **output, unsigned *outputsize, unsigned *outputlen,
  177. int (*decode_pkt)(void *rock,
  178. const char *input, unsigned inputlen,
  179. char **output, unsigned *outputlen),
  180. void *rock);
  181. void _plug_decode_free(decode_context_t *text);
  182. int _plug_parseuser(const sasl_utils_t *utils,
  183. char **user, char **realm, const char *user_realm,
  184. const char *serverFQDN, const char *input);
  185. int _plug_make_fulluser(const sasl_utils_t *utils,
  186. char **fulluser, const char * useronly, const char *realm);
  187. char * _plug_get_error_message (const sasl_utils_t *utils,
  188. #ifdef WIN32
  189. DWORD error
  190. #else
  191. int error
  192. #endif
  193. );
  194. void _plug_snprintf_os_info (char * osbuf, int osbuf_len);
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. #endif /* _PLUGIN_COMMON_H_ */