saslint.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* saslint.h - internal SASL library definitions
  2. * Rob Siemborski
  3. * Tim Martin
  4. */
  5. /*
  6. * Copyright (c) 1998-2016 Carnegie Mellon University. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The name "Carnegie Mellon University" must not be used to
  21. * endorse or promote products derived from this software without
  22. * prior written permission. For permission or any other legal
  23. * details, please contact
  24. * Carnegie Mellon University
  25. * Center for Technology Transfer and Enterprise Creation
  26. * 4615 Forbes Avenue
  27. * Suite 302
  28. * Pittsburgh, PA 15213
  29. * (412) 268-7393, fax: (412) 268-7395
  30. * innovation@andrew.cmu.edu
  31. *
  32. * 4. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by Computing Services
  35. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  36. *
  37. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  38. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  39. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  40. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  41. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  42. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  43. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  44. */
  45. #ifndef SASLINT_H
  46. #define SASLINT_H
  47. #include <config.h>
  48. #include "sasl.h"
  49. #include "saslplug.h"
  50. #include "saslutil.h"
  51. #include "prop.h"
  52. #ifndef INLINE
  53. #if defined (WIN32)
  54. /* Visual Studio: "inline" keyword is not available in C, only in C++ */
  55. #define INLINE __inline
  56. #else
  57. #define INLINE inline
  58. #endif
  59. #endif
  60. /* #define'd constants */
  61. #define CANON_BUF_SIZE 1024
  62. /* Error Handling Foo */
  63. /* Helpful Hints:
  64. * -Error strings are set as soon as possible (first function in stack trace
  65. * with a pointer to the sasl_conn_t.
  66. * -Error codes are set as late as possible (only in the sasl api functions),
  67. * though "as often as possible" also comes to mind to ensure correctness
  68. * -Errors from calls to _buf_alloc, _sasl_strdup, etc are assumed to be
  69. * memory errors.
  70. * -Only errors (error codes < SASL_OK) should be remembered
  71. */
  72. #define RETURN(conn, val) { if(conn && (val) < SASL_OK) \
  73. (conn)->error_code = (val); \
  74. return (val); }
  75. #define MEMERROR(conn) {\
  76. if(conn) sasl_seterror( (conn), 0, \
  77. "Out of Memory in " __FILE__ " near line %d", __LINE__ ); \
  78. RETURN(conn, SASL_NOMEM) }
  79. #define PARAMERROR(conn) {\
  80. if(conn) sasl_seterror( (conn), SASL_NOLOG, \
  81. "Parameter error in " __FILE__ " near line %d", __LINE__ ); \
  82. RETURN(conn, SASL_BADPARAM) }
  83. #define INTERROR(conn, val) {\
  84. if(conn) sasl_seterror( (conn), 0, \
  85. "Internal Error %d in " __FILE__ " near line %d", (val),\
  86. __LINE__ ); \
  87. RETURN(conn, (val)) }
  88. #ifndef PATH_MAX
  89. # ifdef WIN32
  90. # define PATH_MAX MAX_PATH
  91. # else
  92. # ifdef _POSIX_PATH_MAX
  93. # define PATH_MAX _POSIX_PATH_MAX
  94. # else
  95. # define PATH_MAX 1024 /* arbitrary; probably big enough.
  96. * will probably only be 256+64 on
  97. * pre-posix machines */
  98. # endif /* _POSIX_PATH_MAX */
  99. # endif /* WIN32 */
  100. #endif
  101. /* : Define directory delimiter in SASL_PATH/SASL_CONF_PATH variables */
  102. #ifdef WIN32
  103. #define PATHS_DELIMITER ';'
  104. #else
  105. #define PATHS_DELIMITER ':'
  106. #endif
  107. /* A FQDN max len is 255 per RFC 1035,
  108. * this means 253 chars max, we add one more for zero terminator */
  109. #define MAXFQDNLEN 254
  110. /* Datatype Definitions */
  111. typedef struct {
  112. const sasl_callback_t *callbacks;
  113. const char *appname;
  114. } sasl_global_callbacks_t;
  115. typedef struct _sasl_external_properties
  116. {
  117. sasl_ssf_t ssf;
  118. char *auth_id;
  119. } _sasl_external_properties_t;
  120. typedef struct sasl_string_list
  121. {
  122. const char *d;
  123. struct sasl_string_list *next;
  124. } sasl_string_list_t;
  125. typedef struct buffer_info
  126. {
  127. char *data;
  128. size_t curlen;
  129. size_t reallen;
  130. } buffer_info_t;
  131. typedef int add_plugin_t(const char *, void *);
  132. typedef struct add_plugin_list
  133. {
  134. const char *entryname;
  135. add_plugin_t *add_plugin;
  136. } add_plugin_list_t;
  137. enum Sasl_conn_type { SASL_CONN_UNKNOWN = 0,
  138. SASL_CONN_SERVER = 1,
  139. SASL_CONN_CLIENT = 2 };
  140. struct sasl_conn {
  141. enum Sasl_conn_type type;
  142. void (*destroy_conn)(sasl_conn_t *); /* destroy function */
  143. char *service;
  144. unsigned int flags; /* flags passed to sasl_*_new */
  145. /* IP information. A buffer of size 52 is adequate for this in its
  146. longest format (see sasl.h) */
  147. int got_ip_local, got_ip_remote;
  148. char iplocalport[NI_MAXHOST + NI_MAXSERV];
  149. char ipremoteport[NI_MAXHOST + NI_MAXSERV];
  150. void *context;
  151. sasl_out_params_t oparams;
  152. sasl_security_properties_t props;
  153. _sasl_external_properties_t external;
  154. sasl_secret_t *secret;
  155. int (*idle_hook)(sasl_conn_t *conn);
  156. const sasl_callback_t *callbacks;
  157. const sasl_global_callbacks_t *global_callbacks; /* global callbacks
  158. * connection */
  159. char *serverFQDN;
  160. /* Pointers to memory that we are responsible for */
  161. buffer_info_t *encode_buf;
  162. int error_code;
  163. char *error_buf, *errdetail_buf;
  164. size_t error_buf_len, errdetail_buf_len;
  165. char *mechlist_buf;
  166. size_t mechlist_buf_len;
  167. char *decode_buf;
  168. char user_buf[CANON_BUF_SIZE+1], authid_buf[CANON_BUF_SIZE+1];
  169. /* Allocated by sasl_encodev if the output contains multiple SASL packet. */
  170. buffer_info_t multipacket_encoded_data;
  171. };
  172. /* Server Conn Type Information */
  173. typedef struct mechanism
  174. {
  175. server_sasl_mechanism_t m;
  176. struct mechanism *next;
  177. } mechanism_t;
  178. typedef struct mech_list {
  179. const sasl_utils_t *utils; /* gotten from plug_init */
  180. void *mutex; /* mutex for this data */
  181. mechanism_t *mech_list; /* list of loaded mechanisms */
  182. int mech_length; /* number of loaded mechanisms */
  183. } mech_list_t;
  184. typedef struct context_list
  185. {
  186. mechanism_t *mech;
  187. void *context; /* if NULL, this mech is disabled for this connection
  188. * otherwise, use this context instead of a call
  189. * to mech_new */
  190. struct context_list *next;
  191. } context_list_t;
  192. typedef struct sasl_server_conn {
  193. sasl_conn_t base; /* parts common to server + client */
  194. char *appname; /* application name buffer (for sparams) */
  195. char *user_realm; /* domain the user authenticating is in */
  196. int sent_last; /* Have we already done the last send? */
  197. int authenticated;
  198. mechanism_t *mech; /* mechanism trying to use */
  199. sasl_server_params_t *sparams;
  200. context_list_t *mech_contexts;
  201. mechanism_t *mech_list; /* list of available mechanisms */
  202. int mech_length; /* number of available mechanisms */
  203. } sasl_server_conn_t;
  204. /* Client Conn Type Information */
  205. typedef struct cmechanism
  206. {
  207. client_sasl_mechanism_t m;
  208. struct cmechanism *next;
  209. } cmechanism_t;
  210. typedef struct cmech_list {
  211. const sasl_utils_t *utils;
  212. void *mutex; /* mutex for this data */
  213. cmechanism_t *mech_list; /* list of mechanisms */
  214. int mech_length; /* number of mechanisms */
  215. } cmech_list_t;
  216. typedef struct sasl_client_conn {
  217. sasl_conn_t base; /* parts common to server + client */
  218. cmechanism_t *mech;
  219. sasl_client_params_t *cparams;
  220. char *clientFQDN;
  221. cmechanism_t *mech_list; /* list of available mechanisms */
  222. int mech_length; /* number of available mechanisms */
  223. } sasl_client_conn_t;
  224. typedef struct sasl_allocation_utils {
  225. sasl_malloc_t *malloc;
  226. sasl_calloc_t *calloc;
  227. sasl_realloc_t *realloc;
  228. sasl_free_t *free;
  229. } sasl_allocation_utils_t;
  230. typedef struct sasl_mutex_utils {
  231. sasl_mutex_alloc_t *alloc;
  232. sasl_mutex_lock_t *lock;
  233. sasl_mutex_unlock_t *unlock;
  234. sasl_mutex_free_t *free;
  235. } sasl_mutex_utils_t;
  236. typedef struct sasl_log_utils_s {
  237. sasl_log_t *log;
  238. } sasl_log_utils_t;
  239. typedef int sasl_plaintext_verifier(sasl_conn_t *conn,
  240. const char *userid,
  241. const char *passwd,
  242. const char *service,
  243. const char *user_realm);
  244. struct sasl_verify_password_s {
  245. char *name;
  246. sasl_plaintext_verifier *verify;
  247. };
  248. /*
  249. * globals & constants
  250. */
  251. /*
  252. * common.c
  253. */
  254. LIBSASL_API const sasl_utils_t *sasl_global_utils;
  255. extern int (*_sasl_client_idle_hook)(sasl_conn_t *conn);
  256. extern int (*_sasl_server_idle_hook)(sasl_conn_t *conn);
  257. /* These return SASL_OK if we've actually finished cleanup,
  258. * SASL_NOTINIT if that part of the library isn't initialized, and
  259. * SASL_CONTINUE if we need to call them again */
  260. extern int (*_sasl_client_cleanup_hook)(void);
  261. extern int (*_sasl_server_cleanup_hook)(void);
  262. extern sasl_allocation_utils_t _sasl_allocation_utils;
  263. extern sasl_mutex_utils_t _sasl_mutex_utils;
  264. extern int _sasl_allocation_locked;
  265. void sasl_common_done(void);
  266. extern int _sasl_is_equal_mech(const char *req_mech,
  267. const char *plug_mech,
  268. size_t req_mech_len,
  269. int *plus);
  270. /*
  271. * checkpw.c
  272. */
  273. extern struct sasl_verify_password_s _sasl_verify_password[];
  274. /*
  275. * server.c
  276. */
  277. /* (this is a function call to ensure this is read-only to the outside) */
  278. extern int _is_sasl_server_active(void);
  279. /*
  280. * Allocation and Mutex utility macros
  281. */
  282. #define sasl_ALLOC(__size__) (_sasl_allocation_utils.malloc((__size__)))
  283. #define sasl_CALLOC(__nelem__, __size__) \
  284. (_sasl_allocation_utils.calloc((__nelem__), (__size__)))
  285. #define sasl_REALLOC(__ptr__, __size__) \
  286. (_sasl_allocation_utils.realloc((__ptr__), (__size__)))
  287. #define sasl_FREE(__ptr__) (_sasl_allocation_utils.free((__ptr__)))
  288. #define sasl_MUTEX_ALLOC() (_sasl_mutex_utils.alloc())
  289. #define sasl_MUTEX_LOCK(__mutex__) (_sasl_mutex_utils.lock((__mutex__)))
  290. #define sasl_MUTEX_UNLOCK(__mutex__) (_sasl_mutex_utils.unlock((__mutex__)))
  291. #define sasl_MUTEX_FREE(__mutex__) \
  292. (_sasl_mutex_utils.free((__mutex__)))
  293. /* function prototypes */
  294. /*
  295. * dlopen.c and staticopen.c
  296. */
  297. /*
  298. * The differences here are:
  299. * _sasl_load_plugins loads all plugins from all files
  300. * _sasl_get_plugin loads the LIBRARY for an individual file
  301. * _sasl_done_with_plugins frees the LIBRARIES loaded by the above 2
  302. * _sasl_locate_entry locates an entrypoint in a given library
  303. */
  304. extern int _sasl_load_plugins(const add_plugin_list_t *entrypoints,
  305. const sasl_callback_t *getpath_callback,
  306. const sasl_callback_t *verifyfile_callback);
  307. extern int _sasl_get_plugin(const char *file,
  308. const sasl_callback_t *verifyfile_cb,
  309. void **libraryptr);
  310. extern int _sasl_locate_entry(void *library, const char *entryname,
  311. void **entry_point);
  312. extern int _sasl_done_with_plugins();
  313. /*
  314. * common.c
  315. */
  316. extern const sasl_callback_t *
  317. _sasl_find_getpath_callback(const sasl_callback_t *callbacks);
  318. extern const sasl_callback_t *
  319. _sasl_find_getconfpath_callback(const sasl_callback_t *callbacks);
  320. extern const sasl_callback_t *
  321. _sasl_find_verifyfile_callback(const sasl_callback_t *callbacks);
  322. extern int _sasl_common_init(sasl_global_callbacks_t *global_callbacks);
  323. extern int _sasl_conn_init(sasl_conn_t *conn,
  324. const char *service,
  325. unsigned int flags,
  326. enum Sasl_conn_type type,
  327. int (*idle_hook)(sasl_conn_t *conn),
  328. const char *serverFQDN,
  329. const char *iplocalport,
  330. const char *ipremoteport,
  331. const sasl_callback_t *callbacks,
  332. const sasl_global_callbacks_t *global_callbacks);
  333. extern void _sasl_conn_dispose(sasl_conn_t *conn);
  334. extern sasl_utils_t *
  335. _sasl_alloc_utils(sasl_conn_t *conn,
  336. sasl_global_callbacks_t *global_callbacks);
  337. extern int _sasl_free_utils(const sasl_utils_t ** utils);
  338. extern int
  339. _sasl_getcallback(sasl_conn_t * conn,
  340. unsigned long callbackid,
  341. sasl_callback_ft * pproc,
  342. void **pcontext);
  343. extern void
  344. _sasl_log(sasl_conn_t *conn,
  345. int level,
  346. const char *fmt,
  347. ...);
  348. void _sasl_get_errorbuf(sasl_conn_t *conn, char ***bufhdl, size_t **lenhdl);
  349. int _sasl_add_string(char **out, size_t *alloclen,
  350. size_t *outlen, const char *add);
  351. /* More Generic Utilities in common.c */
  352. extern int _sasl_strdup(const char *in, char **out, size_t *outlen);
  353. /* Basically a conditional call to realloc(), if we need more */
  354. int _buf_alloc(char **rwbuf, size_t *curlen, size_t newlen);
  355. /* convert an iovec to a single buffer */
  356. int _iovec_to_buf(const struct iovec *vec,
  357. unsigned numiov, buffer_info_t **output);
  358. /* Convert between string formats and sockaddr formats */
  359. int _sasl_iptostring(const struct sockaddr *addr, socklen_t addrlen,
  360. char *out, unsigned outlen);
  361. int _sasl_ipfromstring(const char *addr, struct sockaddr *out,
  362. socklen_t outlen);
  363. /*
  364. * external plugin (external.c)
  365. */
  366. int external_client_plug_init(const sasl_utils_t *utils,
  367. int max_version,
  368. int *out_version,
  369. sasl_client_plug_t **pluglist,
  370. int *plugcount);
  371. int external_server_plug_init(const sasl_utils_t *utils,
  372. int max_version,
  373. int *out_version,
  374. sasl_server_plug_t **pluglist,
  375. int *plugcount);
  376. /* Mech Listing Functions */
  377. int _sasl_build_mechlist(void);
  378. int _sasl_server_listmech(sasl_conn_t *conn,
  379. const char *user,
  380. const char *prefix,
  381. const char *sep,
  382. const char *suffix,
  383. const char **result,
  384. unsigned *plen,
  385. int *pcount);
  386. int _sasl_client_listmech(sasl_conn_t *conn,
  387. const char *prefix,
  388. const char *sep,
  389. const char *suffix,
  390. const char **result,
  391. unsigned *plen,
  392. int *pcount);
  393. /* Just create a straight list of them */
  394. sasl_string_list_t *_sasl_client_mechs(void);
  395. sasl_string_list_t *_sasl_server_mechs(void);
  396. /*
  397. * config file declarations (config.c)
  398. */
  399. extern const char *sasl_config_getstring(const char *key,const char *def);
  400. /* checkpw.c */
  401. #ifdef DO_SASL_CHECKAPOP
  402. extern int _sasl_auxprop_verify_apop(sasl_conn_t *conn,
  403. const char *userstr,
  404. const char *challenge,
  405. const char *response,
  406. const char *user_realm);
  407. #endif /* DO_SASL_CHECKAPOP */
  408. /* Auxprop Plugin (sasldb.c) */
  409. extern int sasldb_auxprop_plug_init(const sasl_utils_t *utils,
  410. int max_version,
  411. int *out_version,
  412. sasl_auxprop_plug_t **plug,
  413. const char *plugname);
  414. /*
  415. * auxprop.c
  416. */
  417. extern int _sasl_auxprop_add_plugin(void *p, void *library);
  418. extern void _sasl_auxprop_free(void);
  419. extern int _sasl_auxprop_lookup(sasl_server_params_t *sparams,
  420. unsigned flags,
  421. const char *user, unsigned ulen);
  422. /*
  423. * canonusr.c
  424. */
  425. void _sasl_canonuser_free();
  426. extern int internal_canonuser_init(const sasl_utils_t *utils,
  427. int max_version,
  428. int *out_version,
  429. sasl_canonuser_plug_t **plug,
  430. const char *plugname);
  431. extern int _sasl_canon_user(sasl_conn_t *conn,
  432. const char *user,
  433. unsigned ulen,
  434. unsigned flags,
  435. sasl_out_params_t *oparams);
  436. int _sasl_canon_user_lookup (sasl_conn_t *conn,
  437. const char *user,
  438. unsigned ulen,
  439. unsigned flags,
  440. sasl_out_params_t *oparams);
  441. /*
  442. * saslutil.c
  443. */
  444. int get_fqhostname(
  445. char *name,
  446. int namelen,
  447. int abort_if_no_fqdn
  448. );
  449. #ifndef HAVE_GETHOSTNAME
  450. #ifdef sun
  451. /* gotta define gethostname ourselves on suns */
  452. extern int gethostname(char *, int);
  453. #endif
  454. #endif /* HAVE_GETHOSTNAME */
  455. #ifdef WIN32
  456. char* _sasl_wchar_to_utf8(WCHAR *str);
  457. WCHAR* _sasl_utf8_to_wchar(const char *str);
  458. #endif
  459. #endif /* SASLINT_H */