205ce4c91ed5ec35f384c7900b1c9df45d089f8a
[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     void *gss_peer_name;
197     void *gss_local_name;
198     void *spare_ptr4;
199     int (*spare_fptr1)();
200     int (*spare_fptr2)();
201     int spare_int1;
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
224 /******************************
225  * Client Mechanism Functions *
226  ******************************/
227
228 /*
229  * input parameters to client SASL plugin
230  *
231  * created / destroyed by the glue code
232  *
233  */
234 typedef struct sasl_client_params {
235     const char *service;        /* service name */
236     const char *serverFQDN;     /* server fully qualified domain name */
237     const char *clientFQDN;     /* client's fully qualified domain name */
238     const sasl_utils_t *utils;  /* SASL API utility routines --
239                                  * for a particular sasl_conn_t,
240                                  * MUST remain valid until mech_free is
241                                  * called */
242     const sasl_callback_t *prompt_supp; /* client callback list */
243     const char *iplocalport;    /* server IP domain literal & port */
244     const char *ipremoteport;   /* client IP domain literal & port */
245
246     unsigned servicelen;        /* length of service */
247     unsigned slen;              /* length of serverFQDN */
248     unsigned clen;              /* length of clientFQDN */
249     unsigned iploclen;          /* length of iplocalport */
250     unsigned ipremlen;          /* length of ipremoteport */
251
252     /* application's security requirements & info */
253     sasl_security_properties_t props;
254     sasl_ssf_t external_ssf;    /* external SSF active */
255
256     /* for additions which don't require a version upgrade; set to 0 */
257     void *gss_creds;
258     void *chanbindingstype;
259     void *chanbindingsdata;
260     void *spare_ptr4;
261
262     /* Canonicalize a user name from on-wire to internal format
263      *  added rjs3 2001-05-23
264      *  Must be called once user name aquired if canon_user is non-NULL.
265      *  conn        connection context
266      *  in          user name from wire protocol (need not be NUL terminated)
267      *  len         length of user name from wire protocol (0 = strlen(user))
268      *  flags       for SASL_CU_* flags
269      *  oparams     the user, authid, ulen, alen, fields are
270      *              set appropriately after canonicalization/copying and
271      *              authorization of arguments
272      *
273      *  responsible for setting user, ulen, authid, and alen in the oparams
274      *  structure
275      *
276      *  default behavior is to strip leading and trailing whitespace, as
277      *  well as allocating space for and copying the parameters.
278      *
279      * results:
280      *  SASL_OK       -- success
281      *  SASL_NOMEM    -- out of memory
282      *  SASL_BADPARAM -- invalid conn
283      *  SASL_BADPROT  -- invalid user/authid
284      */
285     int (*canon_user)(sasl_conn_t *conn,
286                     const char *in, unsigned len,
287                     unsigned flags,
288                     sasl_out_params_t *oparams);
289
290     int (*spare_fptr1)();
291
292     int chanbindingscrit;
293     int chanbindingslen;
294     int spare_int3;
295
296     /* flags field as passed to sasl_client_new */
297     unsigned flags;
298
299     /* set to 0 initially, this allows a plugin with extended parameters
300      * to work with an older framework by updating version as parameters
301      * are added.
302      */
303     int param_version;
304 } sasl_client_params_t;
305
306 /* features shared between client and server */
307 /* These allow the glue code to handle client-first and server-last issues */
308
309 /* This indicates that the mechanism prefers to do client-send-first
310  * if the protocol allows it. */
311 #define SASL_FEAT_WANT_CLIENT_FIRST 0x0002
312
313 /* This feature is deprecated, instead, plugins should set *serverout to
314  * non-NULL and return SASL_OK intelligently to allow flexible use of
315  * server-last semantics */
316 /* #define SASL_FEAT_WANT_SERVER_LAST 0x0004 */
317
318 /* This feature is deprecated, instead plugins should correctly set
319  * SASL_FEAT_SERVER_FIRST as needed */
320 /* #define SASL_FEAT_INTERNAL_CLIENT_FIRST 0x0008 */
321
322 /* This indicates that the plugin is server-first only. 
323  * Not defining either of SASL_FEAT_SERVER_FIRST or 
324  * SASL_FEAT_WANT_CLIENT_FIRST indicates that the mechanism will take care 
325  * of the client-first situation internally.
326  */
327 #define SASL_FEAT_SERVER_FIRST 0x0010
328
329 /* This plugin allows proxying */
330 #define SASL_FEAT_ALLOWS_PROXY 0x0020
331
332 /* Underlying mechanism uses GSS framing */
333 #define SASL_FEAT_GSS_FRAMING       0x0040
334
335 /* Underlying mechanism supports channel binding */
336 #define SASL_FEAT_CHANNEL_BINDINGS  0x0080
337
338 /* client plug-in features */
339 #define SASL_FEAT_NEEDSERVERFQDN 0x0001
340
341 /* a C object for a client mechanism
342  */
343 typedef struct sasl_client_plug {
344     /* mechanism name */
345     const char *mech_name;
346
347     /* best mech additional security layer strength factor */
348     sasl_ssf_t max_ssf;
349
350     /* best security flags, as defined in sasl_security_properties_t */
351     unsigned security_flags;
352
353     /* features of plugin */
354     unsigned features;
355
356     /* required prompt ids, NULL = user/pass only */
357     const unsigned long *required_prompts;
358     
359     /* global state for mechanism */
360     void *glob_context;
361     
362     /* create context for mechanism, using params supplied
363      *  glob_context   -- from above
364      *  params         -- params from sasl_client_new
365      *  conn_context   -- context for one connection
366      * returns:
367      *  SASL_OK        -- success
368      *  SASL_NOMEM     -- not enough memory
369      *  SASL_WRONGMECH -- mech doesn't support security params
370      */
371     int (*mech_new)(void *glob_context,
372                     sasl_client_params_t *cparams,
373                     void **conn_context);
374     
375     /* perform one step of exchange.  NULL is passed for serverin on
376      * first step.
377      * returns:
378      *  SASL_OK        -- success
379      *  SASL_INTERACT  -- user interaction needed to fill in prompts
380      *  SASL_BADPROT   -- server protocol incorrect/cancelled
381      *  SASL_BADSERV   -- server failed mutual auth
382      */
383     int (*mech_step)(void *conn_context,
384                      sasl_client_params_t *cparams,
385                      const char *serverin,
386                      unsigned serverinlen,
387                      sasl_interact_t **prompt_need,
388                      const char **clientout,
389                      unsigned *clientoutlen,
390                      sasl_out_params_t *oparams);
391     
392     /* dispose of connection context from mech_new
393      */
394     void (*mech_dispose)(void *conn_context, const sasl_utils_t *utils);
395     
396     /* free all global space used by mechanism
397      *  mech_dispose must be called on all mechanisms first
398      */
399     void (*mech_free)(void *glob_context, const sasl_utils_t *utils);
400      
401     /* perform precalculations during a network round-trip
402      *  or idle period.  conn_context may be NULL
403      *  returns 1 if action taken, 0 if no action taken
404      */
405     int (*idle)(void *glob_context,
406                 void *conn_context,
407                 sasl_client_params_t *cparams);
408
409     /* for additions which don't require a version upgrade; set to 0 */
410     int (*spare_fptr1)();
411     int (*spare_fptr2)();
412 } sasl_client_plug_t;
413
414 #define SASL_CLIENT_PLUG_VERSION         4
415
416 /* plug-in entry point:
417  *  utils       -- utility callback functions
418  *  max_version -- highest client plug version supported
419  * returns:
420  *  out_version -- client plug version of result
421  *  pluglist    -- list of mechanism plug-ins
422  *  plugcount   -- number of mechanism plug-ins
423  * results:
424  *  SASL_OK       -- success
425  *  SASL_NOMEM    -- failure
426  *  SASL_BADVERS  -- max_version too small
427  *  SASL_BADPARAM -- bad config string
428  *  ...
429  */
430 typedef int sasl_client_plug_init_t(const sasl_utils_t *utils,
431                                     int max_version,
432                                     int *out_version,
433                                     sasl_client_plug_t **pluglist,
434                                     int *plugcount);
435
436
437 /* add a client plug-in
438  */
439 LIBSASL_API int sasl_client_add_plugin(const char *plugname,
440                                        sasl_client_plug_init_t *cplugfunc);
441
442 typedef struct client_sasl_mechanism
443 {
444     int version;
445
446     char *plugname;
447     const sasl_client_plug_t *plug;
448 } client_sasl_mechanism_t;
449
450 typedef void sasl_client_info_callback_t (client_sasl_mechanism_t *m,
451                                           sasl_info_callback_stage_t stage,
452                                           void *rock);
453
454 /* Dump information about available client plugins */
455 LIBSASL_API int sasl_client_plugin_info (const char *mech_list,
456         sasl_client_info_callback_t *info_cb,
457         void *info_cb_rock);
458
459
460 /********************
461  * Server Functions *
462  ********************/
463
464 /* log message formatting routine */
465 typedef void sasl_logmsg_p(sasl_conn_t *conn, const char *fmt, ...);
466
467 /*
468  * input parameters to server SASL plugin
469  *
470  * created / destroyed by the glue code
471  *
472  */
473 typedef struct sasl_server_params {
474     const char *service;        /* NULL = default service for user_exists
475                                    and setpass */
476     const char *appname;        /* name of calling application */
477     const char *serverFQDN;     /* server default fully qualified domain name
478                                  * (e.g., gethostname) */
479     const char *user_realm;     /* realm for user (NULL = client supplied) */
480     const char *iplocalport;    /* server IP domain literal & port */
481     const char *ipremoteport;   /* client IP domain literal & port */
482
483     unsigned servicelen;        /* length of service */
484     unsigned applen;            /* length of appname */
485     unsigned slen;              /* length of serverFQDN */
486     unsigned urlen;             /* length of user_realm */
487     unsigned iploclen;          /* length of iplocalport */
488     unsigned ipremlen;          /* length of ipremoteport */
489
490     /* This indicates the level of logging desired.  See SASL_LOG_*
491      * in sasl.h
492      *
493      * Plug-ins can ignore this and just pass their desired level to
494      * the log callback.  This is primarily used to eliminate logging which
495      * might be a performance problem (e.g., full protocol trace) and
496      * to select between SASL_LOG_TRACE and SASL_LOG_PASS alternatives
497      */
498     int log_level;
499
500     const sasl_utils_t *utils;  /* SASL API utility routines --
501                                  * for a particular sasl_conn_t,
502                                  * MUST remain valid until mech_free is
503                                  * called */
504     const sasl_callback_t *callbacks;   /* Callbacks from application */
505
506     /* application's security requirements */
507     sasl_security_properties_t props;
508     sasl_ssf_t external_ssf;    /* external SSF active */
509
510     /* Pointer to the function which takes the plaintext passphrase and
511      *  transitions a user to non-plaintext mechanisms via setpass calls.
512      *  (NULL = auto transition not enabled/supported)
513      *
514      *  If passlen is 0, it defaults to strlen(pass).
515      *  returns 0 if no entry added, 1 if entry added
516      */
517     int (*transition)(sasl_conn_t *conn, const char *pass, unsigned passlen);
518
519     /* Canonicalize a user name from on-wire to internal format
520      *  added cjn 1999-09-21
521      *  Must be called once user name acquired if canon_user is non-NULL.
522      *  conn        connection context
523      *  user        user name from wire protocol (need not be NUL terminated)
524      *  ulen        length of user name from wire protocol (0 = strlen(user))
525      *  flags       for SASL_CU_* flags
526      *  oparams     the user, authid, ulen, alen, fields are
527      *              set appropriately after canonicalization/copying and
528      *              authorization of arguments
529      *
530      *  responsible for setting user, ulen, authid, and alen in the oparams
531      *  structure
532      *
533      *  default behavior is to strip leading and trailing whitespace, as
534      *  well as allocating space for and copying the parameters.
535      *
536      * results:
537      *  SASL_OK       -- success
538      *  SASL_NOMEM    -- out of memory
539      *  SASL_BADPARAM -- invalid conn
540      *  SASL_BADPROT  -- invalid user/authid
541      */
542     int (*canon_user)(sasl_conn_t *conn,
543                       const char *user, unsigned ulen,
544                       unsigned flags,
545                       sasl_out_params_t *oparams);
546     
547     /* auxiliary property context (see definitions in prop.h)
548      *  added cjn 2000-01-30
549      *
550      * NOTE: these properties are the ones associated with the
551      * canonicalized "user" (user to login as / authorization id), not
552      * the "authid" (user whose credentials are used / authentication id)
553      * Prefix the property name with a "*" if a property associated with
554      * the "authid" is interesting.
555      */
556     struct propctx *propctx;
557
558     /* for additions which don't require a version upgrade; set to 0 */
559     void *gss_creds;
560     void *chanbindingstype;
561     void *chanbindingsdata;
562     void *spare_ptr4;
563     int (*spare_fptr1)();
564     int (*spare_fptr2)();
565     int chanbindingscrit;
566     int chanbindingslen;
567     int spare_int3;
568
569     /* flags field as passed to sasl_server_new */
570     unsigned flags;
571
572     /* set to 0 initially, this allows a plugin with extended parameters
573      * to work with an older framework by updating version as parameters
574      * are added.
575      */
576     int param_version;
577 } sasl_server_params_t;
578
579 /* logging levels (more levels may be added later, if necessary):
580  */
581 #define SASL_LOG_NONE  0        /* don't log anything */
582 #define SASL_LOG_ERR   1        /* log unusual errors (default) */
583 #define SASL_LOG_FAIL  2        /* log all authentication failures */
584 #define SASL_LOG_WARN  3        /* log non-fatal warnings */
585 #define SASL_LOG_NOTE  4        /* more verbose than LOG_WARN */
586 #define SASL_LOG_DEBUG 5        /* more verbose than LOG_NOTE */
587 #define SASL_LOG_TRACE 6        /* traces of internal protocols */
588 #define SASL_LOG_PASS  7        /* traces of internal protocols, including
589                                  * passwords */
590
591 /* additional flags for setpass() function below:
592  */
593 /*      SASL_SET_CREATE                     create user if pass non-NULL */
594 /*      SASL_SET_DISABLE                    disable user */
595 #define SASL_SET_REMOVE  SASL_SET_CREATE /* remove user if pass is NULL */
596
597 /* features for server plug-in
598  */
599 #define SASL_FEAT_SERVICE    0x0200 /* service-specific passwords supported */
600 #define SASL_FEAT_GETSECRET  0x0400 /* sasl_server_{get,put}secret_t callbacks
601                                      * required by plug-in */
602
603 /* a C object for a server mechanism
604  */
605 typedef struct sasl_server_plug {
606     /* mechanism name */
607     const char *mech_name;
608
609     /* best mech additional security layer strength factor */
610     sasl_ssf_t max_ssf;
611
612     /* best security flags, as defined in sasl_security_properties_t */
613     unsigned security_flags;
614
615     /* features of plugin */
616     unsigned features;
617     
618     /* global state for mechanism */
619     void *glob_context;
620
621     /* create a new mechanism handler
622      *  glob_context  -- global context
623      *  sparams       -- server config params
624      *  challenge     -- server challenge from previous instance or NULL
625      *  challen       -- length of challenge from previous instance or 0
626      * out:
627      *  conn_context  -- connection context
628      *  errinfo       -- error information
629      *
630      * returns:
631      *  SASL_OK       -- successfully created mech instance
632      *  SASL_*        -- any other server error code
633      */
634     int (*mech_new)(void *glob_context,
635                     sasl_server_params_t *sparams,
636                     const char *challenge,
637                     unsigned challen,
638                     void **conn_context);
639     
640     /* perform one step in exchange
641      *
642      * returns:
643      *  SASL_OK       -- success, all done
644      *  SASL_CONTINUE -- success, one more round trip
645      *  SASL_*        -- any other server error code
646      */
647     int (*mech_step)(void *conn_context,
648                      sasl_server_params_t *sparams,
649                      const char *clientin,
650                      unsigned clientinlen,
651                      const char **serverout,
652                      unsigned *serveroutlen,
653                      sasl_out_params_t *oparams);
654     
655     /* dispose of a connection state
656      */
657     void (*mech_dispose)(void *conn_context, const sasl_utils_t *utils);
658     
659     /* free global state for mechanism
660      *  mech_dispose must be called on all mechanisms first
661      */
662     void (*mech_free)(void *glob_context, const sasl_utils_t *utils);
663     
664     /* set a password (optional)
665      *  glob_context  -- global context
666      *  sparams       -- service, middleware utilities, etc. props ignored
667      *  user          -- user name
668      *  pass          -- password/passphrase (NULL = disable/remove/delete)
669      *  passlen       -- length of password/passphrase
670      *  oldpass       -- old password/passphrase (NULL = transition)
671      *  oldpasslen    -- length of password/passphrase
672      *  flags         -- see above
673      *
674      * returns:
675      *  SASL_NOCHANGE -- no change was needed
676      *  SASL_NOUSER   -- no entry for user
677      *  SASL_NOVERIFY -- no mechanism compatible entry for user
678      *  SASL_PWLOCK   -- password locked
679      *  SASL_DIABLED  -- account disabled
680      *  etc.
681      */
682     int (*setpass)(void *glob_context,
683                    sasl_server_params_t *sparams,
684                    const char *user,
685                    const char *pass, unsigned passlen,
686                    const char *oldpass, unsigned oldpasslen,
687                    unsigned flags);
688
689     /* query which mechanisms are available for user
690      *  glob_context  -- context
691      *  sparams       -- service, middleware utilities, etc. props ignored
692      *  user          -- NUL terminated user name
693      *  maxmech       -- max number of strings in mechlist (0 = no output)
694      * output:
695      *  mechlist      -- an array of C string pointers, filled in with
696      *                   mechanism names available to the user
697      *
698      * returns:
699      *  SASL_OK       -- success
700      *  SASL_NOMEM    -- not enough memory
701      *  SASL_FAIL     -- lower level failure
702      *  SASL_DISABLED -- account disabled
703      *  SASL_NOUSER   -- user not found
704      *  SASL_BUFOVER  -- maxmech is too small
705      *  SASL_NOVERIFY -- user found, but no mechanisms available
706      */
707     int (*user_query)(void *glob_context,
708                       sasl_server_params_t *sparams,
709                       const char *user,
710                       int maxmech,
711                       const char **mechlist);
712      
713     /* perform precalculations during a network round-trip
714      *  or idle period.  conn_context may be NULL (optional)
715      *  returns 1 if action taken, 0 if no action taken
716      */
717     int (*idle)(void *glob_context,
718                 void *conn_context,
719                 sasl_server_params_t *sparams);
720
721     /* check if mechanism is available
722      *  optional--if NULL, mechanism is available based on ENABLE= in config
723      *
724      *  If this routine sets conn_context to a non-NULL value, then the call
725      *  to mech_new will be skipped.  This should not be done unless
726      *  there's a significant performance benefit, since it can cause
727      *  additional memory allocation in SASL core code to keep track of
728      *  contexts potentially for multiple mechanisms.
729      *
730      *  This is called by the first call to sasl_listmech() for a
731      *  given connection context, thus for a given protocol it may
732      *  never be called.  Note that if mech_avail returns SASL_NOMECH,
733      *  then that mechanism is considered disabled for the remainder
734      *  of the session.  If mech_avail returns SASL_NOTDONE, then a
735      *  future call to mech_avail may still return either SASL_OK
736      *  or SASL_NOMECH.
737      *
738      *  returns SASL_OK on success,
739      *          SASL_NOTDONE if mech is not available now, but may be later
740      *                       (e.g. EXTERNAL w/o auth_id)
741      *          SASL_NOMECH if mech disabled
742      */
743     int (*mech_avail)(void *glob_context,
744                       sasl_server_params_t *sparams,
745                       void **conn_context);
746
747     /* for additions which don't require a version upgrade; set to 0 */
748     int (*spare_fptr2)();
749 } sasl_server_plug_t;
750
751 #define SASL_SERVER_PLUG_VERSION 4
752
753 /* plug-in entry point:
754  *  utils         -- utility callback functions
755  *  plugname      -- name of plug-in (may be NULL)
756  *  max_version   -- highest server plug version supported
757  * returns:
758  *  out_version   -- server plug-in version of result
759  *  pluglist      -- list of mechanism plug-ins
760  *  plugcount     -- number of mechanism plug-ins
761  * results:
762  *  SASL_OK       -- success
763  *  SASL_NOMEM    -- failure
764  *  SASL_BADVERS  -- max_version too small
765  *  SASL_BADPARAM -- bad config string
766  *  ...
767  */
768 typedef int sasl_server_plug_init_t(const sasl_utils_t *utils,
769                                     int max_version,
770                                     int *out_version,
771                                     sasl_server_plug_t **pluglist,
772                                     int *plugcount);
773
774 /* 
775  * add a server plug-in
776  */
777 LIBSASL_API int sasl_server_add_plugin(const char *plugname,
778                                        sasl_server_plug_init_t *splugfunc);
779
780
781 typedef struct server_sasl_mechanism
782 {
783     int version;
784     int condition; /* set to SASL_NOUSER if no available users;
785                       set to SASL_CONTINUE if delayed plugin loading */
786     char *plugname; /* for AUTHSOURCE tracking */
787     const sasl_server_plug_t *plug;
788     char *f;       /* where should i load the mechanism from? */
789 } server_sasl_mechanism_t;
790
791 typedef void sasl_server_info_callback_t (server_sasl_mechanism_t *m,
792                                           sasl_info_callback_stage_t stage,
793                                           void *rock);
794
795
796 /* Dump information about available server plugins (separate functions are
797    used for canon and auxprop plugins) */
798 LIBSASL_API int sasl_server_plugin_info (const char *mech_list,
799         sasl_server_info_callback_t *info_cb,
800         void *info_cb_rock);
801
802
803 /*********************************************************
804  * user canonicalization plug-in -- added cjn 1999-09-29 *
805  *********************************************************/
806
807 typedef struct sasl_canonuser {
808     /* optional features of plugin (set to 0) */
809     int features;
810
811     /* spare integer (set to 0) */
812     int spare_int1;
813
814     /* global state for plugin */
815     void *glob_context;
816
817     /* name of plugin */
818     char *name;
819
820     /* free global state for plugin */
821     void (*canon_user_free)(void *glob_context, const sasl_utils_t *utils);
822
823     /* canonicalize a username
824      *  glob_context     -- global context from this structure
825      *  sparams          -- server params, note user_realm&propctx elements
826      *  user             -- user to login as (may not be NUL terminated)
827      *  len              -- length of user name (0 = strlen(user))
828      *  flags            -- for SASL_CU_* flags
829      *  out              -- buffer to copy user name
830      *  out_max          -- max length of user name
831      *  out_len          -- set to length of user name
832      *
833      *  note that the output buffers MAY be the same as the input buffers.
834      *
835      * returns
836      *  SASL_OK         on success
837      *  SASL_BADPROT    username contains invalid character
838      */
839     int (*canon_user_server)(void *glob_context,
840                              sasl_server_params_t *sparams,
841                              const char *user, unsigned len,
842                              unsigned flags,
843                              char *out,
844                              unsigned out_umax, unsigned *out_ulen);
845
846     int (*canon_user_client)(void *glob_context,
847                              sasl_client_params_t *cparams,
848                              const char *user, unsigned len,
849                              unsigned flags,
850                              char *out,
851                              unsigned out_max, unsigned *out_len);
852
853     /* for additions which don't require a version upgrade; set to 0 */
854     int (*spare_fptr1)();
855     int (*spare_fptr2)();
856     int (*spare_fptr3)();
857 } sasl_canonuser_plug_t;
858
859 #define SASL_CANONUSER_PLUG_VERSION 5
860
861 /* default name for canonuser plug-in entry point is "sasl_canonuser_init"
862  *  similar to sasl_server_plug_init model, except only returns one
863  *  sasl_canonuser_plug_t structure;
864  */
865 typedef int sasl_canonuser_init_t(const sasl_utils_t *utils,
866                                   int max_version,
867                                   int *out_version,
868                                   sasl_canonuser_plug_t **plug,
869                                   const char *plugname);
870
871 /* add a canonuser plugin
872  */
873 LIBSASL_API int sasl_canonuser_add_plugin(const char *plugname,
874                                   sasl_canonuser_init_t *canonuserfunc);
875
876 /******************************************************
877  * auxiliary property plug-in -- added cjn 1999-09-29 *
878  ******************************************************/
879
880 typedef struct sasl_auxprop_plug {
881     /* optional features of plugin (none defined yet, set to 0) */
882     int features;
883
884     /* spare integer, must be set to 0 */
885     int spare_int1;
886
887     /* global state for plugin */
888     void *glob_context;
889
890     /* free global state for plugin (OPTIONAL) */
891     void (*auxprop_free)(void *glob_context, const sasl_utils_t *utils);
892
893     /* fill in fields of an auxiliary property context
894      *  last element in array has id of SASL_AUX_END
895      *  elements with non-0 len should be ignored.
896      */
897     void (*auxprop_lookup)(void *glob_context,
898                            sasl_server_params_t *sparams,
899                            unsigned flags,
900                            const char *user, unsigned ulen);
901
902     /* name of the auxprop plugin */
903     char *name;
904
905     /* store the fields/values of an auxiliary property context (OPTIONAL)
906      *
907      * if ctx is NULL, just check if storing properties is enabled
908      *
909      * returns
910      *  SASL_OK         on success
911      *  SASL_FAIL       on failure
912      */
913     int (*auxprop_store)(void *glob_context,
914                          sasl_server_params_t *sparams,
915                          struct propctx *ctx,
916                          const char *user, unsigned ulen);
917 } sasl_auxprop_plug_t;
918
919 /* auxprop lookup flags */
920 #define SASL_AUXPROP_OVERRIDE 0x01 /* if clear, ignore auxiliary properties
921                                     * with non-zero len field.  If set,
922                                     * override value of those properties */
923 #define SASL_AUXPROP_AUTHZID  0x02 /* if clear, we are looking up the
924                                     * authid flags (prefixed with *), otherwise
925                                     * we are looking up the authzid flags
926                                     * (no prefix) */
927
928 #define SASL_AUXPROP_PLUG_VERSION 4
929
930 /* default name for auxprop plug-in entry point is "sasl_auxprop_init"
931  *  similar to sasl_server_plug_init model, except only returns one
932  *  sasl_auxprop_plug_t structure;
933  */
934 typedef int sasl_auxprop_init_t(const sasl_utils_t *utils,
935                                 int max_version,
936                                 int *out_version,
937                                 sasl_auxprop_plug_t **plug,
938                                 const char *plugname);
939
940 /* add an auxiliary property plug-in
941  */
942 LIBSASL_API int sasl_auxprop_add_plugin(const char *plugname,
943                                         sasl_auxprop_init_t *auxpropfunc);
944
945 typedef void auxprop_info_callback_t (sasl_auxprop_plug_t *m,
946                                       sasl_info_callback_stage_t stage,
947                                       void *rock);
948
949 /* Dump information about available auxprop plugins (separate functions are
950    used for canon and server authentication plugins) */
951 LIBSASL_API int auxprop_plugin_info (const char *mech_list,
952         auxprop_info_callback_t *info_cb,
953         void *info_cb_rock);
954
955 #ifdef __cplusplus
956 }
957 #endif
958
959 #endif /* SASLPLUG_H */