File indexing completed on 2024-05-12 05:22:31

0001 
0002 /* Generic SASL plugin utility functions
0003  * Rob Siemborski
0004  */
0005 /*
0006  * Copyright (c) 1998-2016 Carnegie Mellon University.  All rights reserved.
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions
0010  * are met:
0011  *
0012  * 1. Redistributions of source code must retain the above copyright
0013  *    notice, this list of conditions and the following disclaimer.
0014  *
0015  * 2. Redistributions in binary form must reproduce the above copyright
0016  *    notice, this list of conditions and the following disclaimer in
0017  *    the documentation and/or other materials provided with the
0018  *    distribution.
0019  *
0020  * 3. The name "Carnegie Mellon University" must not be used to
0021  *    endorse or promote products derived from this software without
0022  *    prior written permission. For permission or any other legal
0023  *    details, please contact
0024  *      Carnegie Mellon University
0025  *      Center for Technology Transfer and Enterprise Creation
0026  *      4615 Forbes Avenue
0027  *      Suite 302
0028  *      Pittsburgh, PA  15213
0029  *      (412) 268-7393, fax: (412) 268-7395
0030  *      innovation@andrew.cmu.edu
0031  *
0032  * 4. Redistributions of any form whatsoever must retain the following
0033  *    acknowledgment:
0034  *    "This product includes software developed by Computing Services
0035  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
0036  *
0037  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
0038  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
0039  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
0040  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0041  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
0042  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
0043  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0044  */
0045 
0046 #ifndef _PLUGIN_COMMON_H_
0047 #define _PLUGIN_COMMON_H_
0048 
0049 #include <config.h>
0050 
0051 #ifdef WIN32
0052 #include <winsock2.h>
0053 #else
0054 #ifndef macintosh
0055 #include <arpa/inet.h>
0056 #endif /* macintosh */
0057 #include <netdb.h>
0058 #include <netinet/in.h>
0059 #include <sys/socket.h>
0060 #endif /* WIN32 */
0061 
0062 #include <sasl/sasl.h>
0063 #include <sasl/saslplug.h>
0064 #include <sasl/saslutil.h>
0065 
0066 #ifdef WIN32
0067 #define PLUG_API __declspec(dllexport)
0068 #else
0069 #define PLUG_API extern
0070 #endif
0071 
0072 #define SASL_CLIENT_PLUG_INIT(x)                                                                                                                               \
0073     extern sasl_client_plug_init_t x##_client_plug_init;                                                                                                       \
0074     PLUG_API int sasl_client_plug_init(const sasl_utils_t *utils, int maxversion, int *out_version, sasl_client_plug_t **pluglist, int *plugcount)             \
0075     {                                                                                                                                                          \
0076         return x##_client_plug_init(utils, maxversion, out_version, pluglist, plugcount);                                                                      \
0077     }
0078 
0079 #define SASL_SERVER_PLUG_INIT(x)                                                                                                                               \
0080     extern sasl_server_plug_init_t x##_server_plug_init;                                                                                                       \
0081     PLUG_API int sasl_server_plug_init(const sasl_utils_t *utils, int maxversion, int *out_version, sasl_server_plug_t **pluglist, int *plugcount)             \
0082     {                                                                                                                                                          \
0083         return x##_server_plug_init(utils, maxversion, out_version, pluglist, plugcount);                                                                      \
0084     }
0085 
0086 #define SASL_AUXPROP_PLUG_INIT(x)                                                                                                                              \
0087     extern sasl_auxprop_init_t x##_auxprop_plug_init;                                                                                                          \
0088     PLUG_API int sasl_auxprop_plug_init(const sasl_utils_t *utils, int maxversion, int *out_version, sasl_auxprop_plug_t **plug, const char *plugname)         \
0089     {                                                                                                                                                          \
0090         return x##_auxprop_plug_init(utils, maxversion, out_version, plug, plugname);                                                                          \
0091     }
0092 
0093 #define SASL_CANONUSER_PLUG_INIT(x)                                                                                                                            \
0094     extern sasl_canonuser_init_t x##_canonuser_plug_init;                                                                                                      \
0095     PLUG_API int sasl_canonuser_init(const sasl_utils_t *utils, int maxversion, int *out_version, sasl_canonuser_plug_t **plug, const char *plugname)          \
0096     {                                                                                                                                                          \
0097         return x##_canonuser_plug_init(utils, maxversion, out_version, plug, plugname);                                                                        \
0098     }
0099 
0100 /* note: msg cannot include additional variables, so if you want to
0101  * do a printf-format string, then you need to call seterror yourself */
0102 #define SETERROR(utils, msg) (utils)->seterror((utils)->conn, 0, (msg))
0103 
0104 #ifndef MEMERROR
0105 #define MEMERROR(utils) (utils)->seterror((utils)->conn, 0, "Out of Memory in " __FILE__ " near line %d", __LINE__)
0106 #endif
0107 
0108 #ifndef PARAMERROR
0109 #define PARAMERROR(utils) (utils)->seterror((utils)->conn, 0, "Parameter Error in " __FILE__ " near line %d", __LINE__)
0110 #endif
0111 
0112 #ifndef SASLINT_H
0113 typedef struct buffer_info {
0114     char *data;
0115     unsigned curlen; /* Current length of data in buffer */
0116     unsigned reallen; /* total length of buffer (>= curlen) */
0117 } buffer_info_t;
0118 
0119 #ifndef HAVE_GETHOSTNAME
0120 #ifdef sun
0121 /* gotta define gethostname ourselves on suns */
0122 extern int gethostname(char *, int);
0123 #endif
0124 #endif /* HAVE_GETHOSTNAME */
0125 
0126 #endif /* SASLINT_H */
0127 
0128 #ifdef __cplusplus
0129 extern "C" {
0130 #endif
0131 
0132 int _plug_ipfromstring(const sasl_utils_t *utils, const char *addr, struct sockaddr *out, socklen_t outlen);
0133 int _plug_iovec_to_buf(const sasl_utils_t *utils, const struct iovec *vec, unsigned numiov, buffer_info_t **output);
0134 int _plug_buf_alloc(const sasl_utils_t *utils, char **rwbuf, unsigned *curlen, unsigned newlen);
0135 int _plug_strdup(const sasl_utils_t *utils, const char *in, char **out, int *outlen);
0136 void _plug_free_string(const sasl_utils_t *utils, char **str);
0137 void _plug_free_secret(const sasl_utils_t *utils, sasl_secret_t **secret);
0138 
0139 #define _plug_get_userid(utils, result, prompt_need) _plug_get_simple(utils, SASL_CB_USER, 0, result, prompt_need)
0140 #define _plug_get_authid(utils, result, prompt_need) _plug_get_simple(utils, SASL_CB_AUTHNAME, 1, result, prompt_need)
0141 int _plug_get_simple(const sasl_utils_t *utils, unsigned int id, int required, const char **result, sasl_interact_t **prompt_need);
0142 
0143 int _plug_get_password(const sasl_utils_t *utils, sasl_secret_t **secret, unsigned int *iscopy, sasl_interact_t **prompt_need);
0144 
0145 int _plug_challenge_prompt(const sasl_utils_t *utils,
0146                            unsigned int id,
0147                            const char *challenge,
0148                            const char *promptstr,
0149                            const char **result,
0150                            sasl_interact_t **prompt_need);
0151 
0152 int _plug_get_realm(const sasl_utils_t *utils, const char **availrealms, const char **realm, sasl_interact_t **prompt_need);
0153 
0154 int _plug_make_prompts(const sasl_utils_t *utils,
0155                        sasl_interact_t **prompts_res,
0156                        const char *user_prompt,
0157                        const char *user_def,
0158                        const char *auth_prompt,
0159                        const char *auth_def,
0160                        const char *pass_prompt,
0161                        const char *pass_def,
0162                        const char *echo_chal,
0163                        const char *echo_prompt,
0164                        const char *echo_def,
0165                        const char *realm_chal,
0166                        const char *realm_prompt,
0167                        const char *realm_def);
0168 
0169 typedef struct decode_context {
0170     const sasl_utils_t *utils;
0171     unsigned int needsize; /* How much of the 4-byte size do we need? */
0172     char sizebuf[4]; /* Buffer to accumulate the 4-byte size */
0173     unsigned int size; /* Absolute size of the encoded packet */
0174     char *buffer; /* Buffer to accumulate an encoded packet */
0175     unsigned int cursize; /* Amount of packet data in the buffer */
0176     unsigned int in_maxbuf; /* Maximum allowed size of an incoming encoded packet */
0177 } decode_context_t;
0178 
0179 void _plug_decode_init(decode_context_t *text, const sasl_utils_t *utils, unsigned int in_maxbuf);
0180 
0181 int _plug_decode(decode_context_t *text,
0182                  const char *input,
0183                  unsigned inputlen,
0184                  char **output,
0185                  unsigned *outputsize,
0186                  unsigned *outputlen,
0187                  int (*decode_pkt)(void *rock, const char *input, unsigned inputlen, char **output, unsigned *outputlen),
0188                  void *rock);
0189 
0190 void _plug_decode_free(decode_context_t *text);
0191 
0192 int _plug_parseuser(const sasl_utils_t *utils, char **user, char **realm, const char *user_realm, const char *serverFQDN, const char *input);
0193 
0194 int _plug_make_fulluser(const sasl_utils_t *utils, char **fulluser, const char *useronly, const char *realm);
0195 
0196 char *_plug_get_error_message(const sasl_utils_t *utils,
0197 #ifdef WIN32
0198                               DWORD error
0199 #else
0200                               int error
0201 #endif
0202 );
0203 void _plug_snprintf_os_info(char *osbuf, int osbuf_len);
0204 
0205 #ifdef __cplusplus
0206 }
0207 #endif
0208 
0209 #endif /* _PLUGIN_COMMON_H_ */