d9547fe459c398ace9be431a687efa50be421686
[cyrus-sasl.git] / include / saslplug.h
1 /* saslplug.h --  API for SASL plug-ins
2  */
3
4 #ifndef SASLPLUG_H
5 #define SASLPLUG_H 1
6
7 #ifndef MD5GLOBAL_H
8 #include "md5global.h"
9 #endif
10 #ifndef MD5_H
11 #include "md5.h"
12 #endif
13 #ifndef HMAC_MD5_H
14 #include "hmac-md5.h"
15 #endif
16 #ifndef PROP_H
17 #include "prop.h"
18 #endif
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 /* callback to lookup a sasl_callback_t for a connection
25  * input:
26  *  conn        -- the connection to lookup a callback for
27  *  callbacknum -- the number of the callback
28  * output:
29  *  pproc       -- pointer to the callback function (set to NULL on failure)
30  *  pcontext    -- pointer to the callback context (set to NULL on failure)
31  * returns:
32  *  SASL_OK -- no error
33  *  SASL_FAIL -- unable to find a callback of the requested type
34  *  SASL_INTERACT -- caller must use interaction to get data
35  */
36 typedef int sasl_getcallback_t(sasl_conn_t *conn,
37                                unsigned long callbackid,
38                                int (**pproc)(),
39                                void **pcontext);
40
41 /* The sasl_utils structure will remain backwards compatible unless
42  * the SASL_*_PLUG_VERSION is changed incompatibly
43  * higher SASL_UTILS_VERSION numbers indicate more functions are available
44  */
45 #define SASL_UTILS_VERSION 4
46
47 /* utility function set for plug-ins
48  */
49 typedef struct sasl_utils {
50     int version;
51
52     /* contexts */
53     sasl_conn_t *conn;
54     sasl_rand_t *rpool;
55     void *getopt_context;
56
57     /* option function */
58     sasl_getopt_t *getopt;
59     
60     /* allocation functions: */
61     sasl_malloc_t *malloc;
62     sasl_calloc_t *calloc;
63     sasl_realloc_t *realloc;
64     sasl_free_t *free;
65
66     /* mutex functions: */
67     sasl_mutex_alloc_t *mutex_alloc;
68     sasl_mutex_lock_t *mutex_lock;
69     sasl_mutex_unlock_t *mutex_unlock;
70     sasl_mutex_free_t *mutex_free;
71
72     /* MD5 hash and HMAC functions */
73     void (*MD5Init)(MD5_CTX *);
74     void (*MD5Update)(MD5_CTX *, const unsigned char *text, unsigned int len);
75     void (*MD5Final)(unsigned char [16], MD5_CTX *);
76     void (*hmac_md5)(const unsigned char *text, int text_len,
77                      const unsigned char *key, int key_len,
78                      unsigned char [16]);
79     void (*hmac_md5_init)(HMAC_MD5_CTX *, const unsigned char *key, int len);
80     /* hmac_md5_update() is just a call to MD5Update on inner context */
81     void (*hmac_md5_final)(unsigned char [16], HMAC_MD5_CTX *);
82     void (*hmac_md5_precalc)(HMAC_MD5_STATE *,
83                              const unsigned char *key, int len);
84     void (*hmac_md5_import)(HMAC_MD5_CTX *, HMAC_MD5_STATE *);
85
86     /* mechanism utility functions (same as above): */
87     int (*mkchal)(sasl_conn_t *conn, char *buf, unsigned maxlen,
88                   unsigned hostflag);
89     int (*utf8verify)(const char *str, unsigned len);
90     void (*rand)(sasl_rand_t *rpool, char *buf, unsigned len);
91     void (*churn)(sasl_rand_t *rpool, const char *data, unsigned len);
92
93     /* This allows recursive calls to the sasl_checkpass() routine from
94      * within a SASL plug-in.  This MUST NOT be used in the PLAIN mechanism
95      * as sasl_checkpass MAY be a front-end for the PLAIN mechanism.
96      * This is intended for use by the non-standard LOGIN mechanism and
97      * potentially by a future mechanism which uses public-key technology to
98      * set up a lightweight encryption layer just for sending a password.
99      */
100     int (*checkpass)(sasl_conn_t *conn,
101                      const char *user, unsigned userlen,
102                      const char *pass, unsigned passlen);
103     
104     /* Access to base64 encode/decode routines */
105     int (*decode64)(const char *in, unsigned inlen,
106                     char *out, unsigned outmax, unsigned *outlen);
107     int (*encode64)(const char *in, unsigned inlen,
108                     char *out, unsigned outmax, unsigned *outlen);
109
110     /* erase a buffer */
111     void (*erasebuffer)(char *buf, unsigned len);
112
113     /* callback to sasl_getprop() and sasl_setprop() */
114     int (*getprop)(sasl_conn_t *conn, int propnum, const void **pvalue);
115     int (*setprop)(sasl_conn_t *conn, int propnum, const void *value);
116
117     /* callback function */
118     sasl_getcallback_t *getcallback;
119
120     /* format a message and then pass it to the SASL_CB_LOG callback
121      *
122      * use syslog()-style formatting (printf with %m as a human readable text
123      * (strerror()) for the error specified as the parameter).
124      * The implementation may use a fixed size buffer not smaller
125      * than 512 octets if it securely truncates the message.
126      *
127      * level is a SASL_LOG_* level (see sasl.h)
128      */
129     void (*log)(sasl_conn_t *conn, int level, const char *fmt, ...);
130
131     /* callback to sasl_seterror() */
132     void (*seterror)(sasl_conn_t *conn, unsigned flags, const char *fmt, ...);
133
134     /* spare function pointer */
135     int *(*spare_fptr)();
136
137     /* auxiliary property utilities */
138     struct propctx *(*prop_new)(unsigned estimate);
139     int (*prop_dup)(struct propctx *src_ctx, struct propctx **dst_ctx);
140     int (*prop_request)(struct propctx *ctx, const char **names);
141     const struct propval *(*prop_get)(struct propctx *ctx);
142     int (*prop_getnames)(struct propctx *ctx, const char **names,
143                          struct propval *vals);
144     void (*prop_clear)(struct propctx *ctx, int requests);
145     void (*prop_dispose)(struct propctx **ctx);
146     int (*prop_format)(struct propctx *ctx, const char *sep, int seplen,
147                        char *outbuf, unsigned outmax, unsigned *outlen);
148     int (*prop_set)(struct propctx *ctx, const char *name,
149                     const char *value, int vallen);
150     int (*prop_setvals)(struct propctx *ctx, const char *name,
151                         const char **values);
152     void (*prop_erase)(struct propctx *ctx, const char *name);
153     int (*auxprop_store)(sasl_conn_t *conn,
154                          struct propctx *ctx, const char *user);
155
156     /* for additions which don't require a version upgrade; set to 0 */
157     int (*spare_fptr1)();
158     int (*spare_fptr2)();
159 } sasl_utils_t;
160
161 /*
162  * output parameters from SASL API
163  *
164  * created / destroyed by the glue code, though probably filled in
165  * by a combination of the plugin, the glue code, and the canon_user callback.
166  *
167  */
168 typedef struct sasl_out_params {
169     unsigned doneflag;          /* exchange complete */
170
171     const char *user;           /* canonicalized user name */
172     const char *authid;         /* canonicalized authentication id */
173
174     unsigned ulen;              /* length of canonicalized user name */
175     unsigned alen;              /* length of canonicalized authid */
176
177     /* security layer information */
178     unsigned maxoutbuf;         /* Maximum buffer size, which will
179                                    produce buffer no bigger than the
180                                    negotiated SASL maximum buffer size */
181     sasl_ssf_t mech_ssf;   /* Should be set non-zero if negotiation of a
182                             * security layer was *attempted*, even if
183                             * the negotiation failed */
184     void *encode_context;
185     int (*encode)(void *context, const struct iovec *invec, unsigned numiov,
186                   const char **output, unsigned *outputlen);
187     void *decode_context;
188     int (*decode)(void *context, const char *input, unsigned inputlen,
189                   const char **output, unsigned *outputlen);
190     
191     /* Pointer to delegated (client's) credentials, if supported by
192        the SASL mechanism */
193     void *client_creds;
194
195     /* for additions which don't require a version upgrade; set to 0 */
196     const void *gss_peer_name;
197     const void *gss_local_name;
198     const char *cbindingname;   /* channel binding name from packet */
199     int (*spare_fptr1)();
200     int (*spare_fptr2)();
201     unsigned int cbindingdisp;  /* channel binding disposition from client */
202     int spare_int2;
203     int spare_int3;
204     int spare_int4;
205
206     /* set to 0 initially, this allows a plugin with extended parameters
207      * to work with an older framework by updating version as parameters
208      * are added.
209      */
210     int param_version;
211 } sasl_out_params_t;
212
213
214
215 /* Used by both client and server side plugins */
216 typedef enum  {
217     SASL_INFO_LIST_START = 0,
218     SASL_INFO_LIST_MECH,
219     SASL_INFO_LIST_END
220 } sasl_info_callback_stage_t;
221
222 /******************************
223  * Channel binding macros     **
224  ******************************/
225
226 /* TRUE if channel binding is non-NULL */
227 #define SASL_CB_DISP_NONE   0x00    /* client did not support CB */
228 #define SASL_CB_DISP_USED   0x01    /* client supports CB, thinks server does not */
229 #define SASL_CB_DISP_WANT   0x02    /* client supports and used CB */
230
231 #define SASL_CB_PRESENT(params)     ((params)->cbinding != NULL)
232 #define SASL_CB_CRITICAL(params)    (SASL_CB_PRESENT(params) && \
233                                      (params)->cbinding->critical)
234
235 /******************************
236  * Client Mechanism Functions *
237  ******************************/
238
239 /*
240  * input parameters to client SASL plugin
241  *
242  * created / destroyed by the glue code
243  *
244  */
245 typedef struct sasl_client_params {
246     const char *service;        /* service name */
247     const char *serverFQDN;     /* server fully qualified domain name */
248     const char *clientFQDN;     /* client's fully qualified domain name */
249     const sasl_utils_t *utils;  /* SASL API utility routines --
250                                  * for a particular sasl_conn_t,
251                                  * MUST remain valid until mech_free is
252                                  * called */
253     const sasl_callback_t *prompt_supp; /* client callback list */
254     const char *iplocalport;    /* server IP domain literal & port */
255     const char *ipremoteport;   /* client IP domain literal & port */
256
257     unsigned servicelen;        /* length of service */
258     unsigned slen;              /* length of serverFQDN */
259     unsigned clen;              /* length of clientFQDN */
260     unsigned iploclen;          /* length of iplocalport */
261     unsigned ipremlen;          /* length of ipremoteport */
262
263     /* application's security requirements & info */
264     sasl_security_properties_t props;
265     sasl_ssf_t external_ssf;    /* external SSF active */
266
267     /* for additions which don't require a version upgrade; set to 0 */
268     const void *gss_creds;                  /* GSS credential handle */
269     const sasl_channel_binding_t *cbinding; /* client channel binding */
270     void *spare_ptr3;
271     void *spare_ptr4;
272
273     /* Canonicalize a user name from on-wire to internal format
274      *  added rjs3 2001-05-23
275      *  Must be called once user name aquired if canon_user is non-NULL.
276      *  conn        connection context
277      *  in          user name from wire protocol (need not be NUL terminated)
278      *  len         length of user name from wire protocol (0 = strlen(user))
279      *  flags       for SASL_CU_* flags
280      *  oparams     the user, authid, ulen, alen, fields are
281      *              set appropriately after canonicalization/copying and
282      *              authorization of arguments
283      *
284      *  responsible for setting user, ulen, authid, and alen in the oparams
285      *  structure
286      *
287      *  default behavior is to strip leading and trailing whitespace, as
288      *  well as allocating space for and copying the parameters.
289      *
290      * results:
291      *  SASL_OK       -- success
292      *  SASL_NOMEM    -- out of memory
293      *  SASL_BADPARAM -- invalid conn
294      *  SASL_BADPROT  -- invalid user/authid
295      */
296     int (*canon_user)(sasl_conn_t *conn,
297                     const char *in, unsigned len,
298                     unsigned flags,
299                     sasl_out_params_t *oparams);
300
301     int (*spare_fptr1)();
302
303     unsigned int cbindingdisp;
304     unsigned int spare_int2;
305     int spare_int3;
306
307     /* flags field as passed to sasl_client_new */
308     unsigned flags;
309
310     /* set to 0 initially, this allows a plugin with extended parameters
311      * to work with an older framework by updating version as parameters
312      * are added.
313      */
314     int param_version;
315 } sasl_client_params_t;
316
317 /* features shared between client and server */
318 /* These allow the glue code to handle client-first and server-last issues */
319
320 /* This indicates that the mechanism prefers to do client-send-first
321  * if the protocol allows it. */
322 #define SASL_FEAT_WANT_CLIENT_FIRST 0x0002
323
324 /* This feature is deprecated, instead, plugins should set *serverout to
325  * non-NULL and return SASL_OK intelligently to allow flexible use of
326  * server-last semantics */
327 /* #define SASL_FEAT_WANT_SERVER_LAST 0x0004 */
328
329 /* This feature is deprecated, instead plugins should correctly set
330  * SASL_FEAT_SERVER_FIRST as needed */
331 /* #define SASL_FEAT_INTERNAL_CLIENT_FIRST 0x0008 */
332
333 /* This indicates that the plugin is server-first only. 
334  * Not defining either of SASL_FEAT_SERVER_FIRST or 
335  * SASL_FEAT_WANT_CLIENT_FIRST indicates that the mechanism will take care 
336  * of the client-first situation internally.
337  */
338 #define SASL_FEAT_SERVER_FIRST 0x0010
339
340 /* This plugin allows proxying */
341 #define SASL_FEAT_ALLOWS_PROXY 0x0020
342
343 /* Underlying mechanism uses GSS framing */
344 #define SASL_FEAT_GSS_FRAMING       0x0040
345
346 /* Underlying mechanism supports channel binding */
347 #define SASL_FEAT_CHANNEL_BINDING  0x0080
348
349 /* client plug-in features */
350 #define SASL_FEAT_NEEDSERVERFQDN 0x0001
351
352 /* a C object for a client mechanism
353  */
354 typedef struct sasl_client_plug {
355     /* mechanism name */
356     const char *mech_name;
357
358     /* best mech additional security layer strength factor */
359     sasl_ssf_t max_ssf;
360
361     /* best security flags, as defined in sasl_security_properties_t */
362     unsigned security_flags;
363
364     /* features of plugin */
365     unsigned features;
366
367     /* required prompt ids, NULL = user/pass only */
368     const unsigned long *required_prompts;
369     
370     /* global state for mechanism */
371     void *glob_context;
372     
373     /* create context for mechanism, using params supplied
374      *  glob_context   -- from above
375      *  params         -- params from sasl_client_new
376      *  conn_context   -- context for one connection
377      * returns:
378      *  SASL_OK        -- success
379      *  SASL_NOMEM     -- not enough memory
380      *  SASL_WRONGMECH -- mech doesn't support security params
381      */
382     int (*mech_new)(void *glob_context,
383                     sasl_client_params_t *cparams,
384                     void **conn_context);
385     
386     /* perform one step of exchange.  NULL is passed for serverin on
387      * first step.
388      * returns:
389      *  SASL_OK        -- success
390      *  SASL_INTERACT  -- user interaction needed to fill in prompts
391      *  SASL_BADPROT   -- server protocol incorrect/cancelled
392      *  SASL_BADSERV   -- server failed mutual auth
393      */
394     int (*mech_step)(void *conn_context,
395                      sasl_client_params_t *cparams,
396                      const char *serverin,
397                      unsigned serverinlen,
398                      sasl_interact_t **prompt_need,
399                      const char **clientout,
400                      unsigned *clientoutlen,
401                      sasl_out_params_t *oparams);
402     
403     /* dispose of connection context from mech_new
404      */
405     void (*mech_dispose)(void *conn_context, const sasl_utils_t *utils);
406     
407     /* free all global space used by mechanism
408      *  mech_dispose must be called on all mechanisms first
409      */
410     void (*mech_free)(void *glob_context, const sasl_utils_t *utils);
411      
412     /* perform precalculations during a network round-trip
413      *  or idle period.  conn_context may be NULL
414      *  returns 1 if action taken, 0 if no action taken
415      */
416     int (*idle)(void *glob_context,
417                 void *conn_context,
418                 sasl_client_params_t *cparams);
419
420     /* for additions which don't require a version upgrade; set to 0 */
421     int (*spare_fptr1)();
422     int (*spare_fptr2)();
423 } sasl_client_plug_t;
424
425 #define SASL_CLIENT_PLUG_VERSION         4
426
427 /* plug-in entry point:
428  *  utils       -- utility callback functions
429  *  max_version -- highest client plug version supported
430  * returns:
431  *  out_version -- client plug version of result
432  *  pluglist    -- list of mechanism plug-ins
433  *  plugcount   -- number of mechanism plug-ins
434  * results:
435  *  SASL_OK       -- success
436  *  SASL_NOMEM    -- failure
437  *  SASL_BADVERS  -- max_version too small
438  *  SASL_BADPARAM -- bad config string
439  *  ...
440  */
441 typedef int sasl_client_plug_init_t(const sasl_utils_t *utils,
442                                     int max_version,
443                                     int *out_version,
444                                     sasl_client_plug_t **pluglist,
445                                     int *plugcount);
446
447
448 /* add a client plug-in
449  */
450 LIBSASL_API int sasl_client_add_plugin(const char *plugname,
451                                        sasl_client_plug_init_t *cplugfunc);
452
453 typedef struct client_sasl_mechanism
454 {
455     int version;
456
457     char *plugname;
458     const sasl_client_plug_t *plug;
459 } client_sasl_mechanism_t;
460
461 typedef void sasl_client_info_callback_t (client_sasl_mechanism_t *m,
462                                           sasl_info_callback_stage_t stage,
463                                           void *rock);
464
465 /* Dump information about available client plugins */
466 LIBSASL_API int sasl_client_plugin_info (const char *mech_list,
467         sasl_client_info_callback_t *info_cb,
468         void *info_cb_rock);
469
470
471 /********************
472  * Server Functions *
473  ********************/
474
475 /* log message formatting routine */
476 typedef void sasl_logmsg_p(sasl_conn_t *conn, const char *fmt, ...);
477
478 /*
479  * input parameters to server SASL plugin
480  *
481  * created / destroyed by the glue code
482  *
483  */
484 typedef struct sasl_server_params {
485     const char *service;        /* NULL = default service for user_exists
486                                    and setpass */
487     const char *appname;        /* name of calling application */
488     const char *serverFQDN;     /* server default fully qualified domain name
489                                  * (e.g., gethostname) */
490     const char *user_realm;     /* realm for user (NULL = client supplied) */
491     const char *iplocalport;    /* server IP domain literal & port */
492     const char *ipremoteport;   /* client IP domain literal & port */
493
494     unsigned servicelen;        /* length of service */
495     unsigned applen;            /* length of appname */
496     unsigned slen;              /* length of serverFQDN */
497     unsigned urlen;             /* length of user_realm */
498     unsigned iploclen;          /* length of iplocalport */
499     unsigned ipremlen;          /* length of ipremoteport */
500
501     /* This indicates the level of logging desired.  See SASL_LOG_*
502      * in sasl.h
503      *
504      * Plug-ins can ignore this and just pass their desired level to
505      * the log callback.  This is primarily used to eliminate logging which
506      * might be a performance problem (e.g., full protocol trace) and
507      * to select between SASL_LOG_TRACE and SASL_LOG_PASS alternatives
508      */
509     int log_level;
510
511     const sasl_utils_t *utils;  /* SASL API utility routines --
512                                  * for a particular sasl_conn_t,
513                                  * MUST remain valid until mech_free is
514                                  * called */
515     const sasl_callback_t *callbacks;   /* Callbacks from application */
516
517     /* application's security requirements */
518     sasl_security_properties_t props;
519     sasl_ssf_t external_ssf;    /* external SSF active */
520
521     /* Pointer to the function which takes the plaintext passphrase and
522      *  transitions a user to non-plaintext mechanisms via setpass calls.
523      *  (NULL = auto transition not enabled/supported)
524      *
525      *  If passlen is 0, it defaults to strlen(pass).
526      *  returns 0 if no entry added, 1 if entry added
527      */
528     int (*transition)(sasl_conn_t *conn, const char *pass, unsigned passlen);
529
530     /* Canonicalize a user name from on-wire to internal format
531      *  added cjn 1999-09-21
532      *  Must be called once user name acquired if canon_user is non-NULL.
533      *  conn        connection context
534      *  user        user name from wire protocol (need not be NUL terminated)
535      *  ulen        length of user name from wire protocol (0 = strlen(user))
536      *  flags       for SASL_CU_* flags
537      *  oparams     the user, authid, ulen, alen, fields are
538      *              set appropriately after canonicalization/copying and
539      *              authorization of arguments
540      *
541      *  responsible for setting user, ulen, authid, and alen in the oparams
542      *  structure
543      *
544      *  default behavior is to strip leading and trailing whitespace, as
545      *  well as allocating space for and copying the parameters.
546      *
547      * results:
548      *  SASL_OK       -- success
549      *  SASL_NOMEM    -- out of memory
550      *  SASL_BADPARAM -- invalid conn
551      *  SASL_BADPROT  -- invalid user/authid
552      */
553     int (*canon_user)(sasl_conn_t *conn,
554                       const char *user, unsigned ulen,
555                       unsigned flags,
556                       sasl_out_params_t *oparams);
557     
558     /* auxiliary property context (see definitions in prop.h)
559      *  added cjn 2000-01-30
560      *
561      * NOTE: these properties are the ones associated with the
562      * canonicalized "user" (user to login as / authorization id), not
563      * the "authid" (user whose credentials are used / authentication id)
564      * Prefix the property name with a "*" if a property associated with
565      * the "authid" is interesting.
566      */
567     struct propctx *propctx;
568
569     /* for additions which don't require a version upgrade; set to 0 */
570     const void *gss_creds;                  /* GSS credential handle */
571     const sasl_channel_binding_t *cbinding; /* server channel binding */
572     void *spare_ptr3;
573     void *spare_ptr4;
574     int (*spare_fptr1)();
575     int (*spare_fptr2)();
576     int spare_int1;
577     int spare_int2;
578     int spare_int3;
579
580     /* flags field as passed to sasl_server_new */
581     unsigned flags;
582
583     /* set to 0 initially, this allows a plugin with extended parameters
584      * to work with an older framework by updating version as parameters
585      * are added.
586      */
587     int param_version;
588 } sasl_server_params_t;
589
590 /* logging levels (more levels may be added later, if necessary):
591  */
592 #define SASL_LOG_NONE  0        /* don't log anything */
593 #define SASL_LOG_ERR   1        /* log unusual errors (default) */
594 #define SASL_LOG_FAIL  2        /* log all authentication failures */
595 #define SASL_LOG_WARN  3        /* log non-fatal warnings */
596 #define SASL_LOG_NOTE  4        /* more verbose than LOG_WARN */
597 #define SASL_LOG_DEBUG 5        /* more verbose than LOG_NOTE */
598 #define SASL_LOG_TRACE 6        /* traces of internal protocols */
599 #define SASL_LOG_PASS  7        /* traces of internal protocols, including
600                                  * passwords */
601
602 /* additional flags for setpass() function below:
603  */
604 /*      SASL_SET_CREATE                     create user if pass non-NULL */
605 /*      SASL_SET_DISABLE                    disable user */
606 #define SASL_SET_REMOVE  SASL_SET_CREATE /* remove user if pass is NULL */
607
608 /* features for server plug-in
609  */
610 #define SASL_FEAT_SERVICE    0x0200 /* service-specific passwords supported */
611 #define SASL_FEAT_GETSECRET  0x0400 /* sasl_server_{get,put}secret_t callbacks
612                                      * required by plug-in */
613
614 /* a C object for a server mechanism
615  */
616 typedef struct sasl_server_plug {
617     /* mechanism name */
618     const char *mech_name;
619
620     /* best mech additional security layer strength factor */
621     sasl_ssf_t max_ssf;
622
623     /* best security flags, as defined in sasl_security_properties_t */
624     unsigned security_flags;
625
626     /* features of plugin */
627     unsigned features;
628     
629     /* global state for mechanism */
630     void *glob_context;
631
632     /* create a new mechanism handler
633      *  glob_context  -- global context
634      *  sparams       -- server config params
635      *  challenge     -- server challenge from previous instance or NULL
636      *  challen       -- length of challenge from previous instance or 0
637      * out:
638      *  conn_context  -- connection context
639      *  errinfo       -- error information
640      *
641      * returns:
642      *  SASL_OK       -- successfully created mech instance
643      *  SASL_*        -- any other server error code
644      */
645     int (*mech_new)(void *glob_context,
646                     sasl_server_params_t *sparams,
647                     const char *challenge,
648                     unsigned challen,
649                     void **conn_context);
650     
651     /* perform one step in exchange
652      *
653      * returns:
654      *  SASL_OK       -- success, all done
655      *  SASL_CONTINUE -- success, one more round trip
656      *  SASL_*        -- any other server error code
657      */
658     int (*mech_step)(void *conn_context,
659                      sasl_server_params_t *sparams,
660                      const char *clientin,
661                      unsigned clientinlen,
662                      const char **serverout,
663                      unsigned *serveroutlen,
664                      sasl_out_params_t *oparams);
665     
666     /* dispose of a connection state
667      */
668     void (*mech_dispose)(void *conn_context, const sasl_utils_t *utils);
669     
670     /* free global state for mechanism
671      *  mech_dispose must be called on all mechanisms first
672      */
673     void (*mech_free)(void *glob_context, const sasl_utils_t *utils);
674     
675     /* set a password (optional)
676      *  glob_context  -- global context
677      *  sparams       -- service, middleware utilities, etc. props ignored
678      *  user          -- user name
679      *  pass          -- password/passphrase (NULL = disable/remove/delete)
680      *  passlen       -- length of password/passphrase
681      *  oldpass       -- old password/passphrase (NULL = transition)
682      *  oldpasslen    -- length of password/passphrase
683      *  flags         -- see above
684      *
685      * returns:
686      *  SASL_NOCHANGE -- no change was needed
687      *  SASL_NOUSER   -- no entry for user
688      *  SASL_NOVERIFY -- no mechanism compatible entry for user
689      *  SASL_PWLOCK   -- password locked
690      *  SASL_DIABLED  -- account disabled
691      *  etc.
692      */
693     int (*setpass)(void *glob_context,
694                    sasl_server_params_t *sparams,
695                    const char *user,
696                    const char *pass, unsigned passlen,
697                    const char *oldpass, unsigned oldpasslen,
698                    unsigned flags);
699
700     /* query which mechanisms are available for user
701      *  glob_context  -- context
702      *  sparams       -- service, middleware utilities, etc. props ignored
703      *  user          -- NUL terminated user name
704      *  maxmech       -- max number of strings in mechlist (0 = no output)
705      * output:
706      *  mechlist      -- an array of C string pointers, filled in with
707      *                   mechanism names available to the user
708      *
709      * returns:
710      *  SASL_OK       -- success
711      *  SASL_NOMEM    -- not enough memory
712      *  SASL_FAIL     -- lower level failure
713      *  SASL_DISABLED -- account disabled
714      *  SASL_NOUSER   -- user not found
715      *  SASL_BUFOVER  -- maxmech is too small
716      *  SASL_NOVERIFY -- user found, but no mechanisms available
717      */
718     int (*user_query)(void *glob_context,
719                       sasl_server_params_t *sparams,
720                       const char *user,
721                       int maxmech,
722                       const char **mechlist);
723      
724     /* perform precalculations during a network round-trip
725      *  or idle period.  conn_context may be NULL (optional)
726      *  returns 1 if action taken, 0 if no action taken
727      */
728     int (*idle)(void *glob_context,
729                 void *conn_context,
730                 sasl_server_params_t *sparams);
731
732     /* check if mechanism is available
733      *  optional--if NULL, mechanism is available based on ENABLE= in config
734      *
735      *  If this routine sets conn_context to a non-NULL value, then the call
736      *  to mech_new will be skipped.  This should not be done unless
737      *  there's a significant performance benefit, since it can cause
738      *  additional memory allocation in SASL core code to keep track of
739      *  contexts potentially for multiple mechanisms.
740      *
741      *  This is called by the first call to sasl_listmech() for a
742      *  given connection context, thus for a given protocol it may
743      *  never be called.  Note that if mech_avail returns SASL_NOMECH,
744      *  then that mechanism is considered disabled for the remainder
745      *  of the session.  If mech_avail returns SASL_NOTDONE, then a
746      *  future call to mech_avail may still return either SASL_OK
747      *  or SASL_NOMECH.
748      *
749      *  returns SASL_OK on success,
750      *          SASL_NOTDONE if mech is not available now, but may be later
751      *                       (e.g. EXTERNAL w/o auth_id)
752      *          SASL_NOMECH if mech disabled
753      */
754     int (*mech_avail)(void *glob_context,
755                       sasl_server_params_t *sparams,
756                       void **conn_context);
757
758     /* for additions which don't require a version upgrade; set to 0 */
759     int (*spare_fptr2)();
760 } sasl_server_plug_t;
761
762 #define SASL_SERVER_PLUG_VERSION 4
763
764 /* plug-in entry point:
765  *  utils         -- utility callback functions
766  *  plugname      -- name of plug-in (may be NULL)
767  *  max_version   -- highest server plug version supported
768  * returns:
769  *  out_version   -- server plug-in version of result
770  *  pluglist      -- list of mechanism plug-ins
771  *  plugcount     -- number of mechanism plug-ins
772  * results:
773  *  SASL_OK       -- success
774  *  SASL_NOMEM    -- failure
775  *  SASL_BADVERS  -- max_version too small
776  *  SASL_BADPARAM -- bad config string
777  *  ...
778  */
779 typedef int sasl_server_plug_init_t(const sasl_utils_t *utils,
780                                     int max_version,
781                                     int *out_version,
782                                     sasl_server_plug_t **pluglist,
783                                     int *plugcount);
784
785 /* 
786  * add a server plug-in
787  */
788 LIBSASL_API int sasl_server_add_plugin(const char *plugname,
789                                        sasl_server_plug_init_t *splugfunc);
790
791
792 typedef struct server_sasl_mechanism
793 {
794     int version;
795     int condition; /* set to SASL_NOUSER if no available users;
796                       set to SASL_CONTINUE if delayed plugin loading */
797     char *plugname; /* for AUTHSOURCE tracking */
798     const sasl_server_plug_t *plug;
799     char *f;       /* where should i load the mechanism from? */
800 } server_sasl_mechanism_t;
801
802 typedef void sasl_server_info_callback_t (server_sasl_mechanism_t *m,
803                                           sasl_info_callback_stage_t stage,
804                                           void *rock);
805
806
807 /* Dump information about available server plugins (separate functions are
808    used for canon and auxprop plugins) */
809 LIBSASL_API int sasl_server_plugin_info (const char *mech_list,
810         sasl_server_info_callback_t *info_cb,
811         void *info_cb_rock);
812
813
814 /*********************************************************
815  * user canonicalization plug-in -- added cjn 1999-09-29 *
816  *********************************************************/
817
818 typedef struct sasl_canonuser {
819     /* optional features of plugin (set to 0) */
820     int features;
821
822     /* spare integer (set to 0) */
823     int spare_int1;
824
825     /* global state for plugin */
826     void *glob_context;
827
828     /* name of plugin */
829     char *name;
830
831     /* free global state for plugin */
832     void (*canon_user_free)(void *glob_context, const sasl_utils_t *utils);
833
834     /* canonicalize a username
835      *  glob_context     -- global context from this structure
836      *  sparams          -- server params, note user_realm&propctx elements
837      *  user             -- user to login as (may not be NUL terminated)
838      *  len              -- length of user name (0 = strlen(user))
839      *  flags            -- for SASL_CU_* flags
840      *  out              -- buffer to copy user name
841      *  out_max          -- max length of user name
842      *  out_len          -- set to length of user name
843      *
844      *  note that the output buffers MAY be the same as the input buffers.
845      *
846      * returns
847      *  SASL_OK         on success
848      *  SASL_BADPROT    username contains invalid character
849      */
850     int (*canon_user_server)(void *glob_context,
851                              sasl_server_params_t *sparams,
852                              const char *user, unsigned len,
853                              unsigned flags,
854                              char *out,
855                              unsigned out_umax, unsigned *out_ulen);
856
857     int (*canon_user_client)(void *glob_context,
858                              sasl_client_params_t *cparams,
859                              const char *user, unsigned len,
860                              unsigned flags,
861                              char *out,
862                              unsigned out_max, unsigned *out_len);
863
864     /* for additions which don't require a version upgrade; set to 0 */
865     int (*spare_fptr1)();
866     int (*spare_fptr2)();
867     int (*spare_fptr3)();
868 } sasl_canonuser_plug_t;
869
870 #define SASL_CANONUSER_PLUG_VERSION 5
871
872 /* default name for canonuser plug-in entry point is "sasl_canonuser_init"
873  *  similar to sasl_server_plug_init model, except only returns one
874  *  sasl_canonuser_plug_t structure;
875  */
876 typedef int sasl_canonuser_init_t(const sasl_utils_t *utils,
877                                   int max_version,
878                                   int *out_version,
879                                   sasl_canonuser_plug_t **plug,
880                                   const char *plugname);
881
882 /* add a canonuser plugin
883  */
884 LIBSASL_API int sasl_canonuser_add_plugin(const char *plugname,
885                                   sasl_canonuser_init_t *canonuserfunc);
886
887 /******************************************************
888  * auxiliary property plug-in -- added cjn 1999-09-29 *
889  ******************************************************/
890
891 typedef struct sasl_auxprop_plug {
892     /* optional features of plugin (none defined yet, set to 0) */
893     int features;
894
895     /* spare integer, must be set to 0 */
896     int spare_int1;
897
898     /* global state for plugin */
899     void *glob_context;
900
901     /* free global state for plugin (OPTIONAL) */
902     void (*auxprop_free)(void *glob_context, const sasl_utils_t *utils);
903
904     /* fill in fields of an auxiliary property context
905      *  last element in array has id of SASL_AUX_END
906      *  elements with non-0 len should be ignored.
907      */
908     void (*auxprop_lookup)(void *glob_context,
909                            sasl_server_params_t *sparams,
910                            unsigned flags,
911                            const char *user, unsigned ulen);
912
913     /* name of the auxprop plugin */
914     char *name;
915
916     /* store the fields/values of an auxiliary property context (OPTIONAL)
917      *
918      * if ctx is NULL, just check if storing properties is enabled
919      *
920      * returns
921      *  SASL_OK         on success
922      *  SASL_FAIL       on failure
923      */
924     int (*auxprop_store)(void *glob_context,
925                          sasl_server_params_t *sparams,
926                          struct propctx *ctx,
927                          const char *user, unsigned ulen);
928 } sasl_auxprop_plug_t;
929
930 /* auxprop lookup flags */
931 #define SASL_AUXPROP_OVERRIDE 0x01 /* if clear, ignore auxiliary properties
932                                     * with non-zero len field.  If set,
933                                     * override value of those properties */
934 #define SASL_AUXPROP_AUTHZID  0x02 /* if clear, we are looking up the
935                                     * authid flags (prefixed with *), otherwise
936                                     * we are looking up the authzid flags
937                                     * (no prefix) */
938
939 #define SASL_AUXPROP_PLUG_VERSION 4
940
941 /* default name for auxprop plug-in entry point is "sasl_auxprop_init"
942  *  similar to sasl_server_plug_init model, except only returns one
943  *  sasl_auxprop_plug_t structure;
944  */
945 typedef int sasl_auxprop_init_t(const sasl_utils_t *utils,
946                                 int max_version,
947                                 int *out_version,
948                                 sasl_auxprop_plug_t **plug,
949                                 const char *plugname);
950
951 /* add an auxiliary property plug-in
952  */
953 LIBSASL_API int sasl_auxprop_add_plugin(const char *plugname,
954                                         sasl_auxprop_init_t *auxpropfunc);
955
956 typedef void auxprop_info_callback_t (sasl_auxprop_plug_t *m,
957                                       sasl_info_callback_stage_t stage,
958                                       void *rock);
959
960 /* Dump information about available auxprop plugins (separate functions are
961    used for canon and server authentication plugins) */
962 LIBSASL_API int auxprop_plugin_info (const char *mech_list,
963         auxprop_info_callback_t *info_cb,
964         void *info_cb_rock);
965
966 #ifdef __cplusplus
967 }
968 #endif
969
970 #endif /* SASLPLUG_H */