xref: /onnv-gate/usr/src/lib/libsasl/lib/client.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
6*0Sstevel@tonic-gate 
7*0Sstevel@tonic-gate /* SASL server API implementation
8*0Sstevel@tonic-gate  * Rob Siemborski
9*0Sstevel@tonic-gate  * Tim Martin
10*0Sstevel@tonic-gate  * $Id: client.c,v 1.61 2003/04/16 19:36:00 rjs3 Exp $
11*0Sstevel@tonic-gate  */
12*0Sstevel@tonic-gate /*
13*0Sstevel@tonic-gate  * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
16*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
17*0Sstevel@tonic-gate  * are met:
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
20*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
23*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
24*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
25*0Sstevel@tonic-gate  *    distribution.
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  * 3. The name "Carnegie Mellon University" must not be used to
28*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
29*0Sstevel@tonic-gate  *    prior written permission. For permission or any other legal
30*0Sstevel@tonic-gate  *    details, please contact
31*0Sstevel@tonic-gate  *      Office of Technology Transfer
32*0Sstevel@tonic-gate  *      Carnegie Mellon University
33*0Sstevel@tonic-gate  *      5000 Forbes Avenue
34*0Sstevel@tonic-gate  *      Pittsburgh, PA  15213-3890
35*0Sstevel@tonic-gate  *      (412) 268-4387, fax: (412) 268-7395
36*0Sstevel@tonic-gate  *      tech-transfer@andrew.cmu.edu
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  * 4. Redistributions of any form whatsoever must retain the following
39*0Sstevel@tonic-gate  *    acknowledgment:
40*0Sstevel@tonic-gate  *    "This product includes software developed by Computing Services
41*0Sstevel@tonic-gate  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
44*0Sstevel@tonic-gate  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
45*0Sstevel@tonic-gate  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
46*0Sstevel@tonic-gate  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
47*0Sstevel@tonic-gate  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
48*0Sstevel@tonic-gate  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
49*0Sstevel@tonic-gate  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #include <config.h>
53*0Sstevel@tonic-gate #include <stdio.h>
54*0Sstevel@tonic-gate #include <stdlib.h>
55*0Sstevel@tonic-gate #include <limits.h>
56*0Sstevel@tonic-gate #include <ctype.h>
57*0Sstevel@tonic-gate #include <string.h>
58*0Sstevel@tonic-gate #ifdef HAVE_UNISTD_H
59*0Sstevel@tonic-gate #include <unistd.h>
60*0Sstevel@tonic-gate #endif
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /* SASL Headers */
63*0Sstevel@tonic-gate #include "sasl.h"
64*0Sstevel@tonic-gate #include "saslplug.h"
65*0Sstevel@tonic-gate #include "saslutil.h"
66*0Sstevel@tonic-gate #include "saslint.h"
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate #ifdef _SUN_SDK_
69*0Sstevel@tonic-gate DEFINE_STATIC_MUTEX(init_client_mutex);
70*0Sstevel@tonic-gate DEFINE_STATIC_MUTEX(client_active_mutex);
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * client_plug_mutex ensures only one client plugin is init'ed at a time
73*0Sstevel@tonic-gate  * If a plugin is loaded more than once, the glob_context may be overwritten
74*0Sstevel@tonic-gate  * which may lead to a memory leak. We keep glob_context with each mech
75*0Sstevel@tonic-gate  * to avoid this problem.
76*0Sstevel@tonic-gate  */
77*0Sstevel@tonic-gate DEFINE_STATIC_MUTEX(client_plug_mutex);
78*0Sstevel@tonic-gate #else
79*0Sstevel@tonic-gate static cmech_list_t *cmechlist; /* global var which holds the list */
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static sasl_global_callbacks_t global_callbacks;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate static int _sasl_client_active = 0;
84*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate #ifdef _SUN_SDK_
init_mechlist(_sasl_global_context_t * gctx)87*0Sstevel@tonic-gate static int init_mechlist(_sasl_global_context_t *gctx)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate   cmech_list_t *cmechlist = gctx->cmechlist;
90*0Sstevel@tonic-gate #else
91*0Sstevel@tonic-gate static int init_mechlist()
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate   cmechlist->mutex = sasl_MUTEX_ALLOC();
96*0Sstevel@tonic-gate   if(!cmechlist->mutex) return SASL_FAIL;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate #ifdef _SUN_SDK_
99*0Sstevel@tonic-gate   cmechlist->utils=
100*0Sstevel@tonic-gate 	_sasl_alloc_utils(gctx, NULL, &gctx->client_global_callbacks);
101*0Sstevel@tonic-gate #else
102*0Sstevel@tonic-gate   cmechlist->utils=_sasl_alloc_utils(NULL, &global_callbacks);
103*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
104*0Sstevel@tonic-gate   if (cmechlist->utils==NULL)
105*0Sstevel@tonic-gate     return SASL_NOMEM;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate   cmechlist->mech_list=NULL;
108*0Sstevel@tonic-gate   cmechlist->mech_length=0;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate   return SASL_OK;
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate #ifdef _SUN_SDK_
114*0Sstevel@tonic-gate static int client_done(_sasl_global_context_t *gctx) {
115*0Sstevel@tonic-gate   cmech_list_t *cmechlist = gctx->cmechlist;
116*0Sstevel@tonic-gate   _sasl_path_info_t *path_info, *p;
117*0Sstevel@tonic-gate #else
118*0Sstevel@tonic-gate static int client_done(void) {
119*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
120*0Sstevel@tonic-gate   cmechanism_t *cm;
121*0Sstevel@tonic-gate   cmechanism_t *cprevm;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate #ifdef _SUN_SDK_
124*0Sstevel@tonic-gate   if(!gctx->sasl_client_active)
125*0Sstevel@tonic-gate       return SASL_NOTINIT;
126*0Sstevel@tonic-gate   if (LOCK_MUTEX(&client_active_mutex) < 0) {
127*0Sstevel@tonic-gate 	return (SASL_FAIL);
128*0Sstevel@tonic-gate   }
129*0Sstevel@tonic-gate   gctx->sasl_client_active--;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate   if(gctx->sasl_client_active) {
132*0Sstevel@tonic-gate       /* Don't de-init yet! Our refcount is nonzero. */
133*0Sstevel@tonic-gate       UNLOCK_MUTEX(&client_active_mutex);
134*0Sstevel@tonic-gate       return SASL_CONTINUE;
135*0Sstevel@tonic-gate   }
136*0Sstevel@tonic-gate #else
137*0Sstevel@tonic-gate   if(!_sasl_client_active)
138*0Sstevel@tonic-gate       return SASL_NOTINIT;
139*0Sstevel@tonic-gate   else
140*0Sstevel@tonic-gate       _sasl_client_active--;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate   if(_sasl_client_active) {
143*0Sstevel@tonic-gate       /* Don't de-init yet! Our refcount is nonzero. */
144*0Sstevel@tonic-gate       return SASL_CONTINUE;
145*0Sstevel@tonic-gate   }
146*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate   cm=cmechlist->mech_list; /* m point to begging of the list */
149*0Sstevel@tonic-gate   while (cm!=NULL)
150*0Sstevel@tonic-gate   {
151*0Sstevel@tonic-gate     cprevm=cm;
152*0Sstevel@tonic-gate     cm=cm->next;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate     if (cprevm->plug->mech_free) {
155*0Sstevel@tonic-gate #ifdef _SUN_SDK_
156*0Sstevel@tonic-gate 	cprevm->plug->mech_free(cprevm->glob_context, cmechlist->utils);
157*0Sstevel@tonic-gate #else
158*0Sstevel@tonic-gate 	cprevm->plug->mech_free(cprevm->plug->glob_context,
159*0Sstevel@tonic-gate 				cmechlist->utils);
160*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
161*0Sstevel@tonic-gate     }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate     sasl_FREE(cprevm->plugname);
164*0Sstevel@tonic-gate     sasl_FREE(cprevm);
165*0Sstevel@tonic-gate   }
166*0Sstevel@tonic-gate   sasl_MUTEX_FREE(cmechlist->mutex);
167*0Sstevel@tonic-gate   _sasl_free_utils(&cmechlist->utils);
168*0Sstevel@tonic-gate   sasl_FREE(cmechlist);
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate #ifdef _SUN_SDK_
171*0Sstevel@tonic-gate   gctx->cmechlist = NULL;
172*0Sstevel@tonic-gate   p = gctx->cplug_path_info;
173*0Sstevel@tonic-gate   while((path_info = p) != NULL) {
174*0Sstevel@tonic-gate     sasl_FREE(path_info->path);
175*0Sstevel@tonic-gate     p = path_info->next;
176*0Sstevel@tonic-gate     sasl_FREE(path_info);
177*0Sstevel@tonic-gate   }
178*0Sstevel@tonic-gate   gctx->cplug_path_info = NULL;
179*0Sstevel@tonic-gate   UNLOCK_MUTEX(&client_active_mutex);
180*0Sstevel@tonic-gate #else
181*0Sstevel@tonic-gate   cmechlist = NULL;
182*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate   return SASL_OK;
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate int sasl_client_add_plugin(const char *plugname,
188*0Sstevel@tonic-gate 			   sasl_client_plug_init_t *entry_point)
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate #ifdef _SUN_SDK_
191*0Sstevel@tonic-gate     return (_sasl_client_add_plugin(_sasl_gbl_ctx(), plugname, entry_point));
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate int _sasl_client_add_plugin(void *ctx,
195*0Sstevel@tonic-gate                             const char *plugname,
196*0Sstevel@tonic-gate                             sasl_client_plug_init_t *entry_point)
197*0Sstevel@tonic-gate {
198*0Sstevel@tonic-gate   cmech_list_t *cmechlist;
199*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
200*0Sstevel@tonic-gate   _sasl_global_context_t *gctx = ctx == NULL ? _sasl_gbl_ctx() : ctx;
201*0Sstevel@tonic-gate   /* EXPORT DELETE START */
202*0Sstevel@tonic-gate   /* CRYPT DELETE START */
203*0Sstevel@tonic-gate   int sun_reg;
204*0Sstevel@tonic-gate   /* CRYPT DELETE END */
205*0Sstevel@tonic-gate   /* EXPORT DELETE END */
206*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
207*0Sstevel@tonic-gate   int i;
208*0Sstevel@tonic-gate   cmechanism_t *m;
209*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
210*0Sstevel@tonic-gate   int plugcount;
211*0Sstevel@tonic-gate   sasl_client_plug_t *pluglist;
212*0Sstevel@tonic-gate   cmechanism_t *mech;
213*0Sstevel@tonic-gate   int result;
214*0Sstevel@tonic-gate   int version;
215*0Sstevel@tonic-gate   int lupe;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate   if(!plugname || !entry_point) return SASL_BADPARAM;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate #ifdef _SUN_SDK_
220*0Sstevel@tonic-gate   cmechlist = gctx->cmechlist;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate   if (cmechlist == NULL) return SASL_BADPARAM;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate   /* Check to see if this plugin has already been registered */
225*0Sstevel@tonic-gate   m = cmechlist->mech_list;
226*0Sstevel@tonic-gate   for (i = 0; i < cmechlist->mech_length; i++) {
227*0Sstevel@tonic-gate     if (strcmp(plugname, m->plugname) == 0) {
228*0Sstevel@tonic-gate 	return SASL_OK;
229*0Sstevel@tonic-gate     }
230*0Sstevel@tonic-gate     m = m->next;
231*0Sstevel@tonic-gate   }
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate   result = LOCK_MUTEX(&client_plug_mutex);
234*0Sstevel@tonic-gate   if (result != SASL_OK)
235*0Sstevel@tonic-gate 	return result;
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate   result = entry_point(cmechlist->utils, SASL_CLIENT_PLUG_VERSION, &version,
240*0Sstevel@tonic-gate 		       &pluglist, &plugcount);
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate   /* EXPORT DELETE START */
243*0Sstevel@tonic-gate   /* CRYPT DELETE START */
244*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
245*0Sstevel@tonic-gate   sun_reg = _is_sun_reg(pluglist);
246*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
247*0Sstevel@tonic-gate   /* CRYPT DELETE END */
248*0Sstevel@tonic-gate   /* EXPORT DELETE END */
249*0Sstevel@tonic-gate   if (result != SASL_OK)
250*0Sstevel@tonic-gate   {
251*0Sstevel@tonic-gate #ifdef _SUN_SDK_
252*0Sstevel@tonic-gate     UNLOCK_MUTEX(&client_plug_mutex);
253*0Sstevel@tonic-gate     __sasl_log(gctx, gctx->client_global_callbacks.callbacks, SASL_LOG_WARN,
254*0Sstevel@tonic-gate 	      "entry_point failed in sasl_client_add_plugin for %s",
255*0Sstevel@tonic-gate 	      plugname);
256*0Sstevel@tonic-gate #else
257*0Sstevel@tonic-gate     _sasl_log(NULL, SASL_LOG_WARN,
258*0Sstevel@tonic-gate 	      "entry_point failed in sasl_client_add_plugin for %s",
259*0Sstevel@tonic-gate 	      plugname);
260*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
261*0Sstevel@tonic-gate     return result;
262*0Sstevel@tonic-gate   }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate   if (version != SASL_CLIENT_PLUG_VERSION)
265*0Sstevel@tonic-gate   {
266*0Sstevel@tonic-gate #ifdef _SUN_SDK_
267*0Sstevel@tonic-gate     UNLOCK_MUTEX(&client_plug_mutex);
268*0Sstevel@tonic-gate     __sasl_log(gctx, gctx->client_global_callbacks.callbacks, SASL_LOG_WARN,
269*0Sstevel@tonic-gate 	      "version conflict in sasl_client_add_plugin for %s", plugname);
270*0Sstevel@tonic-gate #else
271*0Sstevel@tonic-gate     _sasl_log(NULL, SASL_LOG_WARN,
272*0Sstevel@tonic-gate 	      "version conflict in sasl_client_add_plugin for %s", plugname);
273*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
274*0Sstevel@tonic-gate     return SASL_BADVERS;
275*0Sstevel@tonic-gate   }
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate #ifdef _SUN_SDK_
278*0Sstevel@tonic-gate     /* Check plugins to make sure mech_name is non-NULL */
279*0Sstevel@tonic-gate     for (lupe=0;lupe < plugcount ;lupe++) {
280*0Sstevel@tonic-gate 	if (pluglist[lupe].mech_name == NULL)
281*0Sstevel@tonic-gate 	     break;
282*0Sstevel@tonic-gate     }
283*0Sstevel@tonic-gate     if (lupe < plugcount) {
284*0Sstevel@tonic-gate 	UNLOCK_MUTEX(&client_plug_mutex);
285*0Sstevel@tonic-gate 	__sasl_log(gctx, gctx->client_global_callbacks.callbacks,
286*0Sstevel@tonic-gate 		SASL_LOG_ERR, "invalid client plugin %s", plugname);
287*0Sstevel@tonic-gate 	return SASL_BADPROT;
288*0Sstevel@tonic-gate     }
289*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate   for (lupe=0;lupe< plugcount ;lupe++)
292*0Sstevel@tonic-gate     {
293*0Sstevel@tonic-gate       mech = sasl_ALLOC(sizeof(cmechanism_t));
294*0Sstevel@tonic-gate #ifdef _SUN_SDK_
295*0Sstevel@tonic-gate       if (! mech) {
296*0Sstevel@tonic-gate 	UNLOCK_MUTEX(&client_plug_mutex);
297*0Sstevel@tonic-gate 	return SASL_NOMEM;
298*0Sstevel@tonic-gate       }
299*0Sstevel@tonic-gate       mech->glob_context = pluglist->glob_context;
300*0Sstevel@tonic-gate #else
301*0Sstevel@tonic-gate       if (! mech) return SASL_NOMEM;
302*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate       mech->plug=pluglist++;
305*0Sstevel@tonic-gate       if(_sasl_strdup(plugname, &mech->plugname, NULL) != SASL_OK) {
306*0Sstevel@tonic-gate #ifdef _SUN_SDK_
307*0Sstevel@tonic-gate 	UNLOCK_MUTEX(&client_plug_mutex);
308*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
309*0Sstevel@tonic-gate 	sasl_FREE(mech);
310*0Sstevel@tonic-gate 	return SASL_NOMEM;
311*0Sstevel@tonic-gate       }
312*0Sstevel@tonic-gate       /* EXPORT DELETE START */
313*0Sstevel@tonic-gate       /* CRYPT DELETE START */
314*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
315*0Sstevel@tonic-gate       mech->sun_reg = sun_reg;
316*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
317*0Sstevel@tonic-gate      /* CRYPT DELETE END */
318*0Sstevel@tonic-gate      /* EXPORT DELETE END */
319*0Sstevel@tonic-gate       mech->version = version;
320*0Sstevel@tonic-gate       mech->next = cmechlist->mech_list;
321*0Sstevel@tonic-gate       cmechlist->mech_list = mech;
322*0Sstevel@tonic-gate       cmechlist->mech_length++;
323*0Sstevel@tonic-gate     }
324*0Sstevel@tonic-gate #ifdef _SUN_SDK_
325*0Sstevel@tonic-gate     UNLOCK_MUTEX(&client_plug_mutex);
326*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate   return SASL_OK;
329*0Sstevel@tonic-gate }
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate static int
332*0Sstevel@tonic-gate client_idle(sasl_conn_t *conn)
333*0Sstevel@tonic-gate {
334*0Sstevel@tonic-gate   cmechanism_t *m;
335*0Sstevel@tonic-gate #ifdef _SUN_SDK_
336*0Sstevel@tonic-gate   _sasl_global_context_t *gctx = conn == NULL ? _sasl_gbl_ctx() : conn->gctx;
337*0Sstevel@tonic-gate    cmech_list_t *cmechlist = gctx->cmechlist;
338*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate   if (! cmechlist)
341*0Sstevel@tonic-gate     return 0;
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate   for (m = cmechlist->mech_list;
344*0Sstevel@tonic-gate        m;
345*0Sstevel@tonic-gate        m = m->next)
346*0Sstevel@tonic-gate     if (m->plug->idle
347*0Sstevel@tonic-gate #ifdef _SUN_SDK_
348*0Sstevel@tonic-gate 	&&  m->plug->idle(m->glob_context,
349*0Sstevel@tonic-gate #else
350*0Sstevel@tonic-gate 	&&  m->plug->idle(m->plug->glob_context,
351*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
352*0Sstevel@tonic-gate 			  conn,
353*0Sstevel@tonic-gate 			  conn ? ((sasl_client_conn_t *)conn)->cparams : NULL))
354*0Sstevel@tonic-gate       return 1;
355*0Sstevel@tonic-gate   return 0;
356*0Sstevel@tonic-gate }
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate #ifdef _SUN_SDK_
359*0Sstevel@tonic-gate static int _load_client_plugins(_sasl_global_context_t *gctx)
360*0Sstevel@tonic-gate {
361*0Sstevel@tonic-gate     int ret;
362*0Sstevel@tonic-gate     const add_plugin_list_t _ep_list[] = {
363*0Sstevel@tonic-gate       { "sasl_client_plug_init", (add_plugin_t *)_sasl_client_add_plugin },
364*0Sstevel@tonic-gate       { "sasl_canonuser_init", (add_plugin_t *)_sasl_canonuser_add_plugin },
365*0Sstevel@tonic-gate       { NULL, NULL }
366*0Sstevel@tonic-gate     };
367*0Sstevel@tonic-gate     const sasl_callback_t *callbacks = gctx->client_global_callbacks.callbacks;
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate     ret = _sasl_load_plugins(gctx, 0, _ep_list,
370*0Sstevel@tonic-gate 			     _sasl_find_getpath_callback(callbacks),
371*0Sstevel@tonic-gate 			     _sasl_find_verifyfile_callback(callbacks));
372*0Sstevel@tonic-gate     return (ret);
373*0Sstevel@tonic-gate }
374*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate /* initialize the SASL client drivers
377*0Sstevel@tonic-gate  *  callbacks      -- base callbacks for all client connections
378*0Sstevel@tonic-gate  * returns:
379*0Sstevel@tonic-gate  *  SASL_OK        -- Success
380*0Sstevel@tonic-gate  *  SASL_NOMEM     -- Not enough memory
381*0Sstevel@tonic-gate  *  SASL_BADVERS   -- Mechanism version mismatch
382*0Sstevel@tonic-gate  *  SASL_BADPARAM  -- error in config file
383*0Sstevel@tonic-gate  *  SASL_NOMECH    -- No mechanisms available
384*0Sstevel@tonic-gate  *  ...
385*0Sstevel@tonic-gate  */
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate int sasl_client_init(const sasl_callback_t *callbacks)
388*0Sstevel@tonic-gate {
389*0Sstevel@tonic-gate #ifdef _SUN_SDK_
390*0Sstevel@tonic-gate 	return _sasl_client_init(NULL, callbacks);
391*0Sstevel@tonic-gate }
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate int _sasl_client_init(void *ctx,
394*0Sstevel@tonic-gate 		      const sasl_callback_t *callbacks)
395*0Sstevel@tonic-gate {
396*0Sstevel@tonic-gate   int ret;
397*0Sstevel@tonic-gate   _sasl_global_context_t *gctx = ctx == NULL ? _sasl_gbl_ctx() : ctx;
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate   if (gctx == NULL)
400*0Sstevel@tonic-gate 	gctx = _sasl_gbl_ctx();
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate   ret = LOCK_MUTEX(&init_client_mutex);
403*0Sstevel@tonic-gate   if (ret < 0) {
404*0Sstevel@tonic-gate 	return (SASL_FAIL);
405*0Sstevel@tonic-gate   }
406*0Sstevel@tonic-gate   ret = LOCK_MUTEX(&client_active_mutex);
407*0Sstevel@tonic-gate   if (ret < 0) {
408*0Sstevel@tonic-gate 	UNLOCK_MUTEX(&init_client_mutex);
409*0Sstevel@tonic-gate 	return (SASL_FAIL);
410*0Sstevel@tonic-gate   }
411*0Sstevel@tonic-gate   if(gctx->sasl_client_active) {
412*0Sstevel@tonic-gate       /* We're already active, just increase our refcount */
413*0Sstevel@tonic-gate       /* xxx do something with the callback structure? */
414*0Sstevel@tonic-gate       gctx->sasl_client_active++;
415*0Sstevel@tonic-gate       UNLOCK_MUTEX(&client_active_mutex);
416*0Sstevel@tonic-gate       UNLOCK_MUTEX(&init_client_mutex);
417*0Sstevel@tonic-gate       return SASL_OK;
418*0Sstevel@tonic-gate   }
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate   gctx->client_global_callbacks.callbacks = callbacks;
421*0Sstevel@tonic-gate   gctx->client_global_callbacks.appname = NULL;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate   gctx->cmechlist=sasl_ALLOC(sizeof(cmech_list_t));
424*0Sstevel@tonic-gate   if (gctx->cmechlist==NULL) {
425*0Sstevel@tonic-gate       UNLOCK_MUTEX(&init_client_mutex);
426*0Sstevel@tonic-gate       UNLOCK_MUTEX(&client_active_mutex);
427*0Sstevel@tonic-gate       return SASL_NOMEM;
428*0Sstevel@tonic-gate   }
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate   gctx->sasl_client_active = 1;
431*0Sstevel@tonic-gate   UNLOCK_MUTEX(&client_active_mutex);
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate   /* load plugins */
434*0Sstevel@tonic-gate   ret=init_mechlist(gctx);
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate   if (ret!=SASL_OK) {
437*0Sstevel@tonic-gate     client_done(gctx);
438*0Sstevel@tonic-gate     UNLOCK_MUTEX(&init_client_mutex);
439*0Sstevel@tonic-gate     return ret;
440*0Sstevel@tonic-gate   }
441*0Sstevel@tonic-gate   _sasl_client_add_plugin(gctx, "EXTERNAL", &external_client_plug_init);
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate   ret = _sasl_common_init(gctx, &gctx->client_global_callbacks, 0);
444*0Sstevel@tonic-gate #else
445*0Sstevel@tonic-gate int sasl_client_init(const sasl_callback_t *callbacks)
446*0Sstevel@tonic-gate {
447*0Sstevel@tonic-gate   int ret;
448*0Sstevel@tonic-gate   const add_plugin_list_t ep_list[] = {
449*0Sstevel@tonic-gate       { "sasl_client_plug_init", (add_plugin_t *)sasl_client_add_plugin },
450*0Sstevel@tonic-gate       { "sasl_canonuser_init", (add_plugin_t *)sasl_canonuser_add_plugin },
451*0Sstevel@tonic-gate       { NULL, NULL }
452*0Sstevel@tonic-gate   };
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate   if(_sasl_client_active) {
455*0Sstevel@tonic-gate       /* We're already active, just increase our refcount */
456*0Sstevel@tonic-gate       /* xxx do something with the callback structure? */
457*0Sstevel@tonic-gate       _sasl_client_active++;
458*0Sstevel@tonic-gate       return SASL_OK;
459*0Sstevel@tonic-gate   }
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate   global_callbacks.callbacks = callbacks;
462*0Sstevel@tonic-gate   global_callbacks.appname = NULL;
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate   cmechlist=sasl_ALLOC(sizeof(cmech_list_t));
465*0Sstevel@tonic-gate   if (cmechlist==NULL) return SASL_NOMEM;
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate   /* We need to call client_done if we fail now */
468*0Sstevel@tonic-gate   _sasl_client_active = 1;
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate   /* load plugins */
471*0Sstevel@tonic-gate   ret=init_mechlist();
472*0Sstevel@tonic-gate   if (ret!=SASL_OK) {
473*0Sstevel@tonic-gate       client_done();
474*0Sstevel@tonic-gate       return ret;
475*0Sstevel@tonic-gate   }
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate   sasl_client_add_plugin("EXTERNAL", &external_client_plug_init);
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate   ret = _sasl_common_init(&global_callbacks);
480*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate   if (ret == SASL_OK)
483*0Sstevel@tonic-gate #ifdef _SUN_SDK_
484*0Sstevel@tonic-gate       ret = _load_client_plugins(gctx);
485*0Sstevel@tonic-gate #else
486*0Sstevel@tonic-gate       ret = _sasl_load_plugins(ep_list,
487*0Sstevel@tonic-gate 			       _sasl_find_getpath_callback(callbacks),
488*0Sstevel@tonic-gate 			       _sasl_find_verifyfile_callback(callbacks));
489*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate #ifdef _SUN_SDK_
492*0Sstevel@tonic-gate   if (ret == SASL_OK)
493*0Sstevel@tonic-gate 	/* If sasl_client_init returns error, sasl_done() need not be called */
494*0Sstevel@tonic-gate       ret = _sasl_build_mechlist(gctx);
495*0Sstevel@tonic-gate   if (ret == SASL_OK) {
496*0Sstevel@tonic-gate       gctx->sasl_client_cleanup_hook = &client_done;
497*0Sstevel@tonic-gate       gctx->sasl_client_idle_hook = &client_idle;
498*0Sstevel@tonic-gate   } else {
499*0Sstevel@tonic-gate       client_done(gctx);
500*0Sstevel@tonic-gate   }
501*0Sstevel@tonic-gate   UNLOCK_MUTEX(&init_client_mutex);
502*0Sstevel@tonic-gate #else
503*0Sstevel@tonic-gate   if (ret == SASL_OK) {
504*0Sstevel@tonic-gate       _sasl_client_cleanup_hook = &client_done;
505*0Sstevel@tonic-gate       _sasl_client_idle_hook = &client_idle;
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate       ret = _sasl_build_mechlist();
508*0Sstevel@tonic-gate   } else {
509*0Sstevel@tonic-gate       client_done();
510*0Sstevel@tonic-gate   }
511*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate   return ret;
514*0Sstevel@tonic-gate }
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate static void client_dispose(sasl_conn_t *pconn)
517*0Sstevel@tonic-gate {
518*0Sstevel@tonic-gate   sasl_client_conn_t *c_conn=(sasl_client_conn_t *) pconn;
519*0Sstevel@tonic-gate #ifdef _SUN_SDK_
520*0Sstevel@tonic-gate   sasl_free_t *free_func = c_conn->cparams->utils->free;
521*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate   if (c_conn->mech && c_conn->mech->plug->mech_dispose) {
524*0Sstevel@tonic-gate     c_conn->mech->plug->mech_dispose(pconn->context,
525*0Sstevel@tonic-gate 				     c_conn->cparams->utils);
526*0Sstevel@tonic-gate   }
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate   pconn->context = NULL;
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate   if (c_conn->clientFQDN)
531*0Sstevel@tonic-gate #ifdef _SUN_SDK_
532*0Sstevel@tonic-gate       free_func(c_conn->clientFQDN);
533*0Sstevel@tonic-gate #else
534*0Sstevel@tonic-gate       sasl_FREE(c_conn->clientFQDN);
535*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate   if (c_conn->cparams) {
538*0Sstevel@tonic-gate       _sasl_free_utils(&(c_conn->cparams->utils));
539*0Sstevel@tonic-gate #ifdef _SUN_SDK_
540*0Sstevel@tonic-gate       free_func(c_conn->cparams);
541*0Sstevel@tonic-gate #else
542*0Sstevel@tonic-gate       sasl_FREE(c_conn->cparams);
543*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
544*0Sstevel@tonic-gate   }
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate   _sasl_conn_dispose(pconn);
547*0Sstevel@tonic-gate }
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate /* initialize a client exchange based on the specified mechanism
550*0Sstevel@tonic-gate  *  service       -- registered name of the service using SASL (e.g. "imap")
551*0Sstevel@tonic-gate  *  serverFQDN    -- the fully qualified domain name of the server
552*0Sstevel@tonic-gate  *  iplocalport   -- client IPv4/IPv6 domain literal string with port
553*0Sstevel@tonic-gate  *                    (if NULL, then mechanisms requiring IPaddr are disabled)
554*0Sstevel@tonic-gate  *  ipremoteport  -- server IPv4/IPv6 domain literal string with port
555*0Sstevel@tonic-gate  *                    (if NULL, then mechanisms requiring IPaddr are disabled)
556*0Sstevel@tonic-gate  *  prompt_supp   -- list of client interactions supported
557*0Sstevel@tonic-gate  *                   may also include sasl_getopt_t context & call
558*0Sstevel@tonic-gate  *                   NULL prompt_supp = user/pass via SASL_INTERACT only
559*0Sstevel@tonic-gate  *                   NULL proc = interaction supported via SASL_INTERACT
560*0Sstevel@tonic-gate  *  secflags      -- security flags (see above)
561*0Sstevel@tonic-gate  * in/out:
562*0Sstevel@tonic-gate  *  pconn         -- connection negotiation structure
563*0Sstevel@tonic-gate  *                   pointer to NULL => allocate new
564*0Sstevel@tonic-gate  *                   non-NULL => recycle storage and go for next available mech
565*0Sstevel@tonic-gate  *
566*0Sstevel@tonic-gate  * Returns:
567*0Sstevel@tonic-gate  *  SASL_OK       -- success
568*0Sstevel@tonic-gate  *  SASL_NOMECH   -- no mechanism meets requested properties
569*0Sstevel@tonic-gate  *  SASL_NOMEM    -- not enough memory
570*0Sstevel@tonic-gate  */
571*0Sstevel@tonic-gate int sasl_client_new(const char *service,
572*0Sstevel@tonic-gate 		    const char *serverFQDN,
573*0Sstevel@tonic-gate 		    const char *iplocalport,
574*0Sstevel@tonic-gate 		    const char *ipremoteport,
575*0Sstevel@tonic-gate 		    const sasl_callback_t *prompt_supp,
576*0Sstevel@tonic-gate 		    unsigned flags,
577*0Sstevel@tonic-gate 		    sasl_conn_t **pconn)
578*0Sstevel@tonic-gate {
579*0Sstevel@tonic-gate #ifdef _SUN_SDK_
580*0Sstevel@tonic-gate     return _sasl_client_new(NULL, service, serverFQDN, iplocalport,
581*0Sstevel@tonic-gate 			    ipremoteport, prompt_supp, flags, pconn);
582*0Sstevel@tonic-gate }
583*0Sstevel@tonic-gate int _sasl_client_new(void *ctx,
584*0Sstevel@tonic-gate 		     const char *service,
585*0Sstevel@tonic-gate 		     const char *serverFQDN,
586*0Sstevel@tonic-gate 		     const char *iplocalport,
587*0Sstevel@tonic-gate 		     const char *ipremoteport,
588*0Sstevel@tonic-gate 		     const sasl_callback_t *prompt_supp,
589*0Sstevel@tonic-gate 		     unsigned flags,
590*0Sstevel@tonic-gate 		     sasl_conn_t **pconn)
591*0Sstevel@tonic-gate {
592*0Sstevel@tonic-gate   _sasl_global_context_t *gctx = ctx == NULL ? _sasl_gbl_ctx() : ctx;
593*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
594*0Sstevel@tonic-gate   int result;
595*0Sstevel@tonic-gate   char name[MAXHOSTNAMELEN];
596*0Sstevel@tonic-gate   sasl_client_conn_t *conn;
597*0Sstevel@tonic-gate   sasl_utils_t *utils;
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate #ifdef _SUN_SDK_
600*0Sstevel@tonic-gate   if (gctx == NULL)
601*0Sstevel@tonic-gate 	gctx = _sasl_gbl_ctx();
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate   if(gctx->sasl_client_active==0) return SASL_NOTINIT;
604*0Sstevel@tonic-gate #else
605*0Sstevel@tonic-gate   if(_sasl_client_active==0) return SASL_NOTINIT;
606*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate   /* Remember, iplocalport and ipremoteport can be NULL and be valid! */
609*0Sstevel@tonic-gate   if (!pconn || !service || !serverFQDN)
610*0Sstevel@tonic-gate     return SASL_BADPARAM;
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate   *pconn=sasl_ALLOC(sizeof(sasl_client_conn_t));
613*0Sstevel@tonic-gate   if (*pconn==NULL) {
614*0Sstevel@tonic-gate #ifdef _SUN_SDK_
615*0Sstevel@tonic-gate       __sasl_log(gctx, gctx->client_global_callbacks.callbacks, SASL_LOG_ERR,
616*0Sstevel@tonic-gate 		"Out of memory allocating connection context");
617*0Sstevel@tonic-gate #else
618*0Sstevel@tonic-gate       _sasl_log(NULL, SASL_LOG_ERR,
619*0Sstevel@tonic-gate 		"Out of memory allocating connection context");
620*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
621*0Sstevel@tonic-gate       return SASL_NOMEM;
622*0Sstevel@tonic-gate   }
623*0Sstevel@tonic-gate   memset(*pconn, 0, sizeof(sasl_client_conn_t));
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate #ifdef _SUN_SDK_
626*0Sstevel@tonic-gate   (*pconn)->gctx = gctx;
627*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate   (*pconn)->destroy_conn = &client_dispose;
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate   conn = (sasl_client_conn_t *)*pconn;
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate   conn->mech = NULL;
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate   conn->cparams=sasl_ALLOC(sizeof(sasl_client_params_t));
636*0Sstevel@tonic-gate   if (conn->cparams==NULL)
637*0Sstevel@tonic-gate       MEMERROR(*pconn);
638*0Sstevel@tonic-gate   memset(conn->cparams,0,sizeof(sasl_client_params_t));
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate   result = _sasl_conn_init(*pconn, service, flags, SASL_CONN_CLIENT,
641*0Sstevel@tonic-gate 			   &client_idle, serverFQDN,
642*0Sstevel@tonic-gate 			   iplocalport, ipremoteport,
643*0Sstevel@tonic-gate #ifdef _SUN_SDK_
644*0Sstevel@tonic-gate 			   prompt_supp, &gctx->client_global_callbacks);
645*0Sstevel@tonic-gate #else
646*0Sstevel@tonic-gate 			   prompt_supp, &global_callbacks);
647*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate   if (result != SASL_OK) RETURN(*pconn, result);
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate #ifdef _SUN_SDK_
652*0Sstevel@tonic-gate   utils=_sasl_alloc_utils(gctx, *pconn, &gctx->client_global_callbacks);
653*0Sstevel@tonic-gate #else
654*0Sstevel@tonic-gate   utils=_sasl_alloc_utils(*pconn, &global_callbacks);
655*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
656*0Sstevel@tonic-gate   if (utils==NULL)
657*0Sstevel@tonic-gate       MEMERROR(*pconn);
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate   utils->conn= *pconn;
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate   /* Setup the non-lazy parts of cparams, the rest is done in
662*0Sstevel@tonic-gate    * sasl_client_start */
663*0Sstevel@tonic-gate   conn->cparams->utils = utils;
664*0Sstevel@tonic-gate   conn->cparams->canon_user = &_sasl_canon_user;
665*0Sstevel@tonic-gate   conn->cparams->flags = flags;
666*0Sstevel@tonic-gate   conn->cparams->prompt_supp = (*pconn)->callbacks;
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate   /* get the clientFQDN (serverFQDN was set in _sasl_conn_init) */
669*0Sstevel@tonic-gate   memset(name, 0, sizeof(name));
670*0Sstevel@tonic-gate   gethostname(name, MAXHOSTNAMELEN);
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate   result = _sasl_strdup(name, &conn->clientFQDN, NULL);
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate   if(result == SASL_OK) return SASL_OK;
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate #ifdef _SUN_SDK_
677*0Sstevel@tonic-gate   conn->cparams->iplocalport = (*pconn)->iplocalport;
678*0Sstevel@tonic-gate   conn->cparams->iploclen = strlen((*pconn)->iplocalport);
679*0Sstevel@tonic-gate   conn->cparams->ipremoteport = (*pconn)->ipremoteport;
680*0Sstevel@tonic-gate   conn->cparams->ipremlen = strlen((*pconn)->ipremoteport);
681*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate   /* result isn't SASL_OK */
684*0Sstevel@tonic-gate   _sasl_conn_dispose(*pconn);
685*0Sstevel@tonic-gate   sasl_FREE(*pconn);
686*0Sstevel@tonic-gate   *pconn = NULL;
687*0Sstevel@tonic-gate #ifdef _SUN_SDK_
688*0Sstevel@tonic-gate   __sasl_log(gctx, gctx->client_global_callbacks.callbacks, SASL_LOG_ERR,
689*0Sstevel@tonic-gate 	"Out of memory in sasl_client_new");
690*0Sstevel@tonic-gate #else
691*0Sstevel@tonic-gate   _sasl_log(NULL, SASL_LOG_ERR, "Out of memory in sasl_client_new");
692*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
693*0Sstevel@tonic-gate   return result;
694*0Sstevel@tonic-gate }
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate static int have_prompts(sasl_conn_t *conn,
697*0Sstevel@tonic-gate 			const sasl_client_plug_t *mech)
698*0Sstevel@tonic-gate {
699*0Sstevel@tonic-gate   static const unsigned long default_prompts[] = {
700*0Sstevel@tonic-gate     SASL_CB_AUTHNAME,
701*0Sstevel@tonic-gate     SASL_CB_PASS,
702*0Sstevel@tonic-gate     SASL_CB_LIST_END
703*0Sstevel@tonic-gate   };
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate   const unsigned long *prompt;
706*0Sstevel@tonic-gate   int (*pproc)();
707*0Sstevel@tonic-gate   void *pcontext;
708*0Sstevel@tonic-gate   int result;
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate   for (prompt = (mech->required_prompts
711*0Sstevel@tonic-gate 		 ? mech->required_prompts :
712*0Sstevel@tonic-gate 		 default_prompts);
713*0Sstevel@tonic-gate        *prompt != SASL_CB_LIST_END;
714*0Sstevel@tonic-gate        prompt++) {
715*0Sstevel@tonic-gate     result = _sasl_getcallback(conn, *prompt, &pproc, &pcontext);
716*0Sstevel@tonic-gate     if (result != SASL_OK && result != SASL_INTERACT)
717*0Sstevel@tonic-gate       return 0;			/* we don't have this required prompt */
718*0Sstevel@tonic-gate   }
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate   return 1; /* we have all the prompts */
721*0Sstevel@tonic-gate }
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate /* select a mechanism for a connection
724*0Sstevel@tonic-gate  *  mechlist      -- mechanisms server has available (punctuation ignored)
725*0Sstevel@tonic-gate  *  secret        -- optional secret from previous session
726*0Sstevel@tonic-gate  * output:
727*0Sstevel@tonic-gate  *  prompt_need   -- on SASL_INTERACT, list of prompts needed to continue
728*0Sstevel@tonic-gate  *  clientout     -- the initial client response to send to the server
729*0Sstevel@tonic-gate  *  mech          -- set to mechanism name
730*0Sstevel@tonic-gate  *
731*0Sstevel@tonic-gate  * Returns:
732*0Sstevel@tonic-gate  *  SASL_OK       -- success
733*0Sstevel@tonic-gate  *  SASL_NOMEM    -- not enough memory
734*0Sstevel@tonic-gate  *  SASL_NOMECH   -- no mechanism meets requested properties
735*0Sstevel@tonic-gate  *  SASL_INTERACT -- user interaction needed to fill in prompt_need list
736*0Sstevel@tonic-gate  */
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate /* xxx confirm this with rfc 2222
739*0Sstevel@tonic-gate  * SASL mechanism allowable characters are "AZaz-_"
740*0Sstevel@tonic-gate  * seperators can be any other characters and of any length
741*0Sstevel@tonic-gate  * even variable lengths between
742*0Sstevel@tonic-gate  *
743*0Sstevel@tonic-gate  * Apps should be encouraged to simply use space or comma space
744*0Sstevel@tonic-gate  * though
745*0Sstevel@tonic-gate  */
746*0Sstevel@tonic-gate int sasl_client_start(sasl_conn_t *conn,
747*0Sstevel@tonic-gate 		      const char *mechlist,
748*0Sstevel@tonic-gate 		      sasl_interact_t **prompt_need,
749*0Sstevel@tonic-gate 		      const char **clientout,
750*0Sstevel@tonic-gate 		      unsigned *clientoutlen,
751*0Sstevel@tonic-gate 		      const char **mech)
752*0Sstevel@tonic-gate {
753*0Sstevel@tonic-gate     sasl_client_conn_t *c_conn= (sasl_client_conn_t *) conn;
754*0Sstevel@tonic-gate     char name[SASL_MECHNAMEMAX + 1];
755*0Sstevel@tonic-gate     cmechanism_t *m=NULL,*bestm=NULL;
756*0Sstevel@tonic-gate     size_t pos=0,place;
757*0Sstevel@tonic-gate     size_t list_len;
758*0Sstevel@tonic-gate     sasl_ssf_t bestssf = 0, minssf = 0;
759*0Sstevel@tonic-gate     int result;
760*0Sstevel@tonic-gate #ifdef _SUN_SDK_
761*0Sstevel@tonic-gate     _sasl_global_context_t *gctx = (conn == NULL) ?
762*0Sstevel@tonic-gate 		_sasl_gbl_ctx() : conn->gctx;
763*0Sstevel@tonic-gate     cmech_list_t *cmechlist;
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate     if(gctx->sasl_client_active==0) return SASL_NOTINIT;
766*0Sstevel@tonic-gate     cmechlist = gctx->cmechlist;
767*0Sstevel@tonic-gate #else
768*0Sstevel@tonic-gate     if(_sasl_client_active==0) return SASL_NOTINIT;
769*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate     if (!conn) return SASL_BADPARAM;
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate     /* verify parameters */
774*0Sstevel@tonic-gate     if (mechlist == NULL)
775*0Sstevel@tonic-gate 	PARAMERROR(conn);
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate     /* if prompt_need != NULL we've already been here
778*0Sstevel@tonic-gate        and just need to do the continue step again */
779*0Sstevel@tonic-gate 
780*0Sstevel@tonic-gate     /* do a step */
781*0Sstevel@tonic-gate     /* FIXME: Hopefully they only give us our own prompt_need back */
782*0Sstevel@tonic-gate     if (prompt_need && *prompt_need != NULL) {
783*0Sstevel@tonic-gate 	goto dostep;
784*0Sstevel@tonic-gate     }
785*0Sstevel@tonic-gate 
786*0Sstevel@tonic-gate #ifdef _SUN_SDK_
787*0Sstevel@tonic-gate     if (c_conn->mech != NULL) {
788*0Sstevel@tonic-gate 	if (c_conn->mech->plug->mech_dispose != NULL) {
789*0Sstevel@tonic-gate 	    c_conn->mech->plug->mech_dispose(conn->context,
790*0Sstevel@tonic-gate 		c_conn->cparams->utils);
791*0Sstevel@tonic-gate 	    c_conn->mech = NULL;
792*0Sstevel@tonic-gate 	}
793*0Sstevel@tonic-gate     }
794*0Sstevel@tonic-gate     memset(&conn->oparams, 0, sizeof(sasl_out_params_t));
795*0Sstevel@tonic-gate 
796*0Sstevel@tonic-gate     (void) _load_client_plugins(gctx);
797*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate     if(conn->props.min_ssf < conn->external.ssf) {
800*0Sstevel@tonic-gate 	minssf = 0;
801*0Sstevel@tonic-gate     } else {
802*0Sstevel@tonic-gate 	minssf = conn->props.min_ssf - conn->external.ssf;
803*0Sstevel@tonic-gate     }
804*0Sstevel@tonic-gate 
805*0Sstevel@tonic-gate     /* parse mechlist */
806*0Sstevel@tonic-gate     list_len = strlen(mechlist);
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate     while (pos<list_len)
809*0Sstevel@tonic-gate     {
810*0Sstevel@tonic-gate 	place=0;
811*0Sstevel@tonic-gate 	while ((pos<list_len) && (isalnum((unsigned char)mechlist[pos])
812*0Sstevel@tonic-gate 				  || mechlist[pos] == '_'
813*0Sstevel@tonic-gate 				  || mechlist[pos] == '-')) {
814*0Sstevel@tonic-gate 	    name[place]=mechlist[pos];
815*0Sstevel@tonic-gate 	    pos++;
816*0Sstevel@tonic-gate 	    place++;
817*0Sstevel@tonic-gate 	    if (SASL_MECHNAMEMAX < place) {
818*0Sstevel@tonic-gate 		place--;
819*0Sstevel@tonic-gate 		while(pos<list_len && (isalnum((unsigned char)mechlist[pos])
820*0Sstevel@tonic-gate 				       || mechlist[pos] == '_'
821*0Sstevel@tonic-gate 				       || mechlist[pos] == '-'))
822*0Sstevel@tonic-gate 		    pos++;
823*0Sstevel@tonic-gate 	    }
824*0Sstevel@tonic-gate 	}
825*0Sstevel@tonic-gate 	pos++;
826*0Sstevel@tonic-gate 	name[place]=0;
827*0Sstevel@tonic-gate 
828*0Sstevel@tonic-gate 	if (! place) continue;
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 	/* foreach in server list */
831*0Sstevel@tonic-gate 	for (m = cmechlist->mech_list; m != NULL; m = m->next) {
832*0Sstevel@tonic-gate 	    int myflags;
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 	    /* Is this the mechanism the server is suggesting? */
835*0Sstevel@tonic-gate 	    if (strcasecmp(m->plug->mech_name, name))
836*0Sstevel@tonic-gate 		continue; /* no */
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 	    /* Do we have the prompts for it? */
839*0Sstevel@tonic-gate 	    if (!have_prompts(conn, m->plug))
840*0Sstevel@tonic-gate 		break;
841*0Sstevel@tonic-gate 
842*0Sstevel@tonic-gate 	    /* Is it strong enough? */
843*0Sstevel@tonic-gate 	    if (minssf > m->plug->max_ssf)
844*0Sstevel@tonic-gate 		break;
845*0Sstevel@tonic-gate 
846*0Sstevel@tonic-gate 	    /* EXPORT DELETE START */
847*0Sstevel@tonic-gate 	    /* CRYPT DELETE START */
848*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
849*0Sstevel@tonic-gate 	    /* If not SUN supplied mech, it has no strength */
850*0Sstevel@tonic-gate 	    if (minssf > 0 && !m->sun_reg)
851*0Sstevel@tonic-gate 		break;
852*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
853*0Sstevel@tonic-gate 	    /* CRYPT DELETE END */
854*0Sstevel@tonic-gate 	    /* EXPORT DELETE END */
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 	    /* Does it meet our security properties? */
857*0Sstevel@tonic-gate 	    myflags = conn->props.security_flags;
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 	    /* if there's an external layer this is no longer plaintext */
860*0Sstevel@tonic-gate 	    if ((conn->props.min_ssf <= conn->external.ssf) &&
861*0Sstevel@tonic-gate 		(conn->external.ssf > 1)) {
862*0Sstevel@tonic-gate 		myflags &= ~SASL_SEC_NOPLAINTEXT;
863*0Sstevel@tonic-gate 	    }
864*0Sstevel@tonic-gate 
865*0Sstevel@tonic-gate 	    if (((myflags ^ m->plug->security_flags) & myflags) != 0) {
866*0Sstevel@tonic-gate 		break;
867*0Sstevel@tonic-gate 	    }
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 	    /* Can we meet it's features? */
870*0Sstevel@tonic-gate 	    if ((m->plug->features & SASL_FEAT_NEEDSERVERFQDN)
871*0Sstevel@tonic-gate 		&& !conn->serverFQDN) {
872*0Sstevel@tonic-gate 		break;
873*0Sstevel@tonic-gate 	    }
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 	    /* Can it meet our features? */
876*0Sstevel@tonic-gate 	    if ((conn->flags & SASL_NEED_PROXY) &&
877*0Sstevel@tonic-gate 		!(m->plug->features & SASL_FEAT_ALLOWS_PROXY)) {
878*0Sstevel@tonic-gate 		break;
879*0Sstevel@tonic-gate 	    }
880*0Sstevel@tonic-gate 
881*0Sstevel@tonic-gate #ifdef PREFER_MECH
882*0Sstevel@tonic-gate 	    /* EXPORT DELETE START */
883*0Sstevel@tonic-gate 	    /* CRYPT DELETE START */
884*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
885*0Sstevel@tonic-gate 	    if (strcasecmp(m->plug->mech_name, PREFER_MECH) &&
886*0Sstevel@tonic-gate 		bestm && (m->sun_reg && m->plug->max_ssf <= bestssf) ||
887*0Sstevel@tonic-gate 		(m->plug->max_ssf == 0)) {
888*0Sstevel@tonic-gate #else
889*0Sstevel@tonic-gate 	    /* CRYPT DELETE END */
890*0Sstevel@tonic-gate 	    /* EXPORT DELETE END */
891*0Sstevel@tonic-gate 	    if (strcasecmp(m->plug->mech_name, PREFER_MECH) &&
892*0Sstevel@tonic-gate 		bestm && m->plug->max_ssf <= bestssf) {
893*0Sstevel@tonic-gate 
894*0Sstevel@tonic-gate 		/* EXPORT DELETE START */
895*0Sstevel@tonic-gate 		/* CRYPT DELETE START */
896*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
897*0Sstevel@tonic-gate 		/* CRYPT DELETE END */
898*0Sstevel@tonic-gate 		/* EXPORT DELETE END */
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 		/* this mechanism isn't our favorite, and it's no better
901*0Sstevel@tonic-gate 		   than what we already have! */
902*0Sstevel@tonic-gate 		break;
903*0Sstevel@tonic-gate 	    }
904*0Sstevel@tonic-gate #else
905*0Sstevel@tonic-gate 	    /* EXPORT DELETE START */
906*0Sstevel@tonic-gate 	    /* CRYPT DELETE START */
907*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
908*0Sstevel@tonic-gate 	    if (bestm && m->sun_reg && m->plug->max_ssf <= bestssf) {
909*0Sstevel@tonic-gate #else
910*0Sstevel@tonic-gate 	    /* CRYPT DELETE END */
911*0Sstevel@tonic-gate 	    /* EXPORT DELETE END */
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate 	    if (bestm && m->plug->max_ssf <= bestssf) {
914*0Sstevel@tonic-gate 	    /* EXPORT DELETE START */
915*0Sstevel@tonic-gate 	    /* CRYPT DELETE START */
916*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
917*0Sstevel@tonic-gate 	    /* CRYPT DELETE END */
918*0Sstevel@tonic-gate 	    /* EXPORT DELETE END */
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 		/* this mechanism is no better than what we already have! */
921*0Sstevel@tonic-gate 		break;
922*0Sstevel@tonic-gate 	    }
923*0Sstevel@tonic-gate #endif
924*0Sstevel@tonic-gate 
925*0Sstevel@tonic-gate 	    /* compare security flags, only take new mechanism if it has
926*0Sstevel@tonic-gate 	     * all the security flags of the previous one.
927*0Sstevel@tonic-gate 	     *
928*0Sstevel@tonic-gate 	     * From the mechanisms we ship with, this yields the order:
929*0Sstevel@tonic-gate 	     *
930*0Sstevel@tonic-gate 	     * SRP
931*0Sstevel@tonic-gate 	     * GSSAPI + KERBEROS_V4
932*0Sstevel@tonic-gate 	     * DIGEST + OTP
933*0Sstevel@tonic-gate 	     * CRAM + EXTERNAL
934*0Sstevel@tonic-gate 	     * PLAIN + LOGIN + ANONYMOUS
935*0Sstevel@tonic-gate 	     *
936*0Sstevel@tonic-gate 	     * This might be improved on by comparing the numeric value of
937*0Sstevel@tonic-gate 	     * the bitwise-or'd security flags, which splits DIGEST/OTP,
938*0Sstevel@tonic-gate 	     * CRAM/EXTERNAL, and PLAIN/LOGIN from ANONYMOUS, but then we
939*0Sstevel@tonic-gate 	     * are depending on the numeric values of the flags (which may
940*0Sstevel@tonic-gate 	     * change, and their ordering could be considered dumb luck.
941*0Sstevel@tonic-gate 	     */
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 	    if (bestm &&
944*0Sstevel@tonic-gate 		((m->plug->security_flags ^ bestm->plug->security_flags) &
945*0Sstevel@tonic-gate 		 bestm->plug->security_flags)) {
946*0Sstevel@tonic-gate 		break;
947*0Sstevel@tonic-gate 	    }
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 	    if (mech) {
950*0Sstevel@tonic-gate 		*mech = m->plug->mech_name;
951*0Sstevel@tonic-gate 	    }
952*0Sstevel@tonic-gate 	    /* EXPORT DELETE START */
953*0Sstevel@tonic-gate 	    /* CRYPT DELETE START */
954*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
955*0Sstevel@tonic-gate 	    bestssf = m->sun_reg ? m->plug->max_ssf : 0;
956*0Sstevel@tonic-gate #else
957*0Sstevel@tonic-gate 	    /* CRYPT DELETE END */
958*0Sstevel@tonic-gate 	    /* EXPORT DELETE END */
959*0Sstevel@tonic-gate 	    bestssf = m->plug->max_ssf;
960*0Sstevel@tonic-gate 	    /* EXPORT DELETE START */
961*0Sstevel@tonic-gate 	    /* CRYPT DELETE START */
962*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
963*0Sstevel@tonic-gate 	    /* CRYPT DELETE END */
964*0Sstevel@tonic-gate 	    /* EXPORT DELETE END */
965*0Sstevel@tonic-gate 	    bestm = m;
966*0Sstevel@tonic-gate 	    break;
967*0Sstevel@tonic-gate 	}
968*0Sstevel@tonic-gate     }
969*0Sstevel@tonic-gate 
970*0Sstevel@tonic-gate     if (bestm == NULL) {
971*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
972*0Sstevel@tonic-gate 	sasl_seterror(conn, 0, gettext("No worthy mechs found"));
973*0Sstevel@tonic-gate #else
974*0Sstevel@tonic-gate 	sasl_seterror(conn, 0, "No worthy mechs found");
975*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
976*0Sstevel@tonic-gate 	result = SASL_NOMECH;
977*0Sstevel@tonic-gate 	goto done;
978*0Sstevel@tonic-gate     }
979*0Sstevel@tonic-gate 
980*0Sstevel@tonic-gate     /* make (the rest of) cparams */
981*0Sstevel@tonic-gate     c_conn->cparams->service = conn->service;
982*0Sstevel@tonic-gate     c_conn->cparams->servicelen = strlen(conn->service);
983*0Sstevel@tonic-gate 
984*0Sstevel@tonic-gate     c_conn->cparams->serverFQDN = conn->serverFQDN;
985*0Sstevel@tonic-gate     c_conn->cparams->slen = strlen(conn->serverFQDN);
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate     c_conn->cparams->clientFQDN = c_conn->clientFQDN;
988*0Sstevel@tonic-gate     c_conn->cparams->clen = strlen(c_conn->clientFQDN);
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate     c_conn->cparams->external_ssf = conn->external.ssf;
991*0Sstevel@tonic-gate     c_conn->cparams->props = conn->props;
992*0Sstevel@tonic-gate     /* EXPORT DELETE START */
993*0Sstevel@tonic-gate     /* CRYPT DELETE START */
994*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
995*0Sstevel@tonic-gate     if (!bestm->sun_reg) {
996*0Sstevel@tonic-gate 	c_conn->cparams->props.min_ssf = 0;
997*0Sstevel@tonic-gate 	c_conn->cparams->props.max_ssf = 0;
998*0Sstevel@tonic-gate     }
999*0Sstevel@tonic-gate     c_conn->base.sun_reg = bestm->sun_reg;
1000*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
1001*0Sstevel@tonic-gate     /* CRYPT DELETE END */
1002*0Sstevel@tonic-gate     /* EXPORT DELETE END */
1003*0Sstevel@tonic-gate     c_conn->mech = bestm;
1004*0Sstevel@tonic-gate 
1005*0Sstevel@tonic-gate     /* init that plugin */
1006*0Sstevel@tonic-gate #ifdef _SUN_SDK_
1007*0Sstevel@tonic-gate     result = c_conn->mech->plug->mech_new(c_conn->mech->glob_context,
1008*0Sstevel@tonic-gate #else
1009*0Sstevel@tonic-gate     result = c_conn->mech->plug->mech_new(c_conn->mech->plug->glob_context,
1010*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1011*0Sstevel@tonic-gate 					  c_conn->cparams,
1012*0Sstevel@tonic-gate 					  &(conn->context));
1013*0Sstevel@tonic-gate     if(result != SASL_OK) goto done;
1014*0Sstevel@tonic-gate 
1015*0Sstevel@tonic-gate     /* do a step -- but only if we can do a client-send-first */
1016*0Sstevel@tonic-gate  dostep:
1017*0Sstevel@tonic-gate     if(clientout) {
1018*0Sstevel@tonic-gate         if(c_conn->mech->plug->features & SASL_FEAT_SERVER_FIRST) {
1019*0Sstevel@tonic-gate             *clientout = NULL;
1020*0Sstevel@tonic-gate             *clientoutlen = 0;
1021*0Sstevel@tonic-gate             result = SASL_CONTINUE;
1022*0Sstevel@tonic-gate         } else {
1023*0Sstevel@tonic-gate             result = sasl_client_step(conn, NULL, 0, prompt_need,
1024*0Sstevel@tonic-gate                                       clientout, clientoutlen);
1025*0Sstevel@tonic-gate         }
1026*0Sstevel@tonic-gate     }
1027*0Sstevel@tonic-gate     else
1028*0Sstevel@tonic-gate 	result = SASL_CONTINUE;
1029*0Sstevel@tonic-gate 
1030*0Sstevel@tonic-gate  done:
1031*0Sstevel@tonic-gate     RETURN(conn, result);
1032*0Sstevel@tonic-gate }
1033*0Sstevel@tonic-gate 
1034*0Sstevel@tonic-gate /* do a single authentication step.
1035*0Sstevel@tonic-gate  *  serverin    -- the server message received by the client, MUST have a NUL
1036*0Sstevel@tonic-gate  *                 sentinel, not counted by serverinlen
1037*0Sstevel@tonic-gate  * output:
1038*0Sstevel@tonic-gate  *  prompt_need -- on SASL_INTERACT, list of prompts needed to continue
1039*0Sstevel@tonic-gate  *  clientout   -- the client response to send to the server
1040*0Sstevel@tonic-gate  *
1041*0Sstevel@tonic-gate  * returns:
1042*0Sstevel@tonic-gate  *  SASL_OK        -- success
1043*0Sstevel@tonic-gate  *  SASL_INTERACT  -- user interaction needed to fill in prompt_need list
1044*0Sstevel@tonic-gate  *  SASL_BADPROT   -- server protocol incorrect/cancelled
1045*0Sstevel@tonic-gate  *  SASL_BADSERV   -- server failed mutual auth
1046*0Sstevel@tonic-gate  */
1047*0Sstevel@tonic-gate 
1048*0Sstevel@tonic-gate int sasl_client_step(sasl_conn_t *conn,
1049*0Sstevel@tonic-gate 		     const char *serverin,
1050*0Sstevel@tonic-gate 		     unsigned serverinlen,
1051*0Sstevel@tonic-gate 		     sasl_interact_t **prompt_need,
1052*0Sstevel@tonic-gate 		     const char **clientout,
1053*0Sstevel@tonic-gate 		     unsigned *clientoutlen)
1054*0Sstevel@tonic-gate {
1055*0Sstevel@tonic-gate   sasl_client_conn_t *c_conn= (sasl_client_conn_t *) conn;
1056*0Sstevel@tonic-gate   int result;
1057*0Sstevel@tonic-gate 
1058*0Sstevel@tonic-gate #ifdef _SUN_SDK_
1059*0Sstevel@tonic-gate   _sasl_global_context_t *gctx = (conn == NULL) ?
1060*0Sstevel@tonic-gate 		_sasl_gbl_ctx() : conn->gctx;
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate   if(gctx->sasl_client_active==0) return SASL_NOTINIT;
1063*0Sstevel@tonic-gate #else
1064*0Sstevel@tonic-gate   if(_sasl_client_active==0) return SASL_NOTINIT;
1065*0Sstevel@tonic-gate #endif	/* _SUN_SDK_ */
1066*0Sstevel@tonic-gate   if(!conn) return SASL_BADPARAM;
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate   /* check parameters */
1069*0Sstevel@tonic-gate   if ((serverin==NULL) && (serverinlen>0))
1070*0Sstevel@tonic-gate       PARAMERROR(conn);
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate   /* Don't do another step if the plugin told us that we're done */
1073*0Sstevel@tonic-gate   if (conn->oparams.doneflag) {
1074*0Sstevel@tonic-gate       _sasl_log(conn, SASL_LOG_ERR, "attempting client step after doneflag");
1075*0Sstevel@tonic-gate       return SASL_FAIL;
1076*0Sstevel@tonic-gate   }
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate   if(clientout) *clientout = NULL;
1079*0Sstevel@tonic-gate   if(clientoutlen) *clientoutlen = 0;
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate   /* do a step */
1082*0Sstevel@tonic-gate   result = c_conn->mech->plug->mech_step(conn->context,
1083*0Sstevel@tonic-gate 					 c_conn->cparams,
1084*0Sstevel@tonic-gate 					 serverin,
1085*0Sstevel@tonic-gate 					 serverinlen,
1086*0Sstevel@tonic-gate 					 prompt_need,
1087*0Sstevel@tonic-gate 					 clientout, clientoutlen,
1088*0Sstevel@tonic-gate 					 &conn->oparams);
1089*0Sstevel@tonic-gate 
1090*0Sstevel@tonic-gate   if (result == SASL_OK) {
1091*0Sstevel@tonic-gate       /* So we're done on this end, but if both
1092*0Sstevel@tonic-gate        * 1. the mech does server-send-last
1093*0Sstevel@tonic-gate        * 2. the protocol does not
1094*0Sstevel@tonic-gate        * we need to return no data */
1095*0Sstevel@tonic-gate       if(!*clientout && !(conn->flags & SASL_SUCCESS_DATA)) {
1096*0Sstevel@tonic-gate 	  *clientout = "";
1097*0Sstevel@tonic-gate 	  *clientoutlen = 0;
1098*0Sstevel@tonic-gate       }
1099*0Sstevel@tonic-gate 
1100*0Sstevel@tonic-gate       if(!conn->oparams.maxoutbuf) {
1101*0Sstevel@tonic-gate 	  conn->oparams.maxoutbuf = conn->props.maxbufsize;
1102*0Sstevel@tonic-gate       }
1103*0Sstevel@tonic-gate 
1104*0Sstevel@tonic-gate       if(conn->oparams.user == NULL || conn->oparams.authid == NULL) {
1105*0Sstevel@tonic-gate #ifdef _SUN_SDK_
1106*0Sstevel@tonic-gate 	_sasl_log(conn, SASL_LOG_ERR,
1107*0Sstevel@tonic-gate 		  "mech did not call canon_user for both authzid and authid");
1108*0Sstevel@tonic-gate #else
1109*0Sstevel@tonic-gate 	  sasl_seterror(conn, 0,
1110*0Sstevel@tonic-gate 			"mech did not call canon_user for both authzid and authid");
1111*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1112*0Sstevel@tonic-gate 	  result = SASL_BADPROT;
1113*0Sstevel@tonic-gate       }
1114*0Sstevel@tonic-gate   }
1115*0Sstevel@tonic-gate 
1116*0Sstevel@tonic-gate   RETURN(conn,result);
1117*0Sstevel@tonic-gate }
1118*0Sstevel@tonic-gate 
1119*0Sstevel@tonic-gate /* returns the length of all the mechanisms
1120*0Sstevel@tonic-gate  * added up
1121*0Sstevel@tonic-gate  */
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate #ifdef _SUN_SDK_
1124*0Sstevel@tonic-gate static unsigned mech_names_len(_sasl_global_context_t *gctx)
1125*0Sstevel@tonic-gate {
1126*0Sstevel@tonic-gate   cmech_list_t *cmechlist = gctx->cmechlist;
1127*0Sstevel@tonic-gate #else
1128*0Sstevel@tonic-gate static unsigned mech_names_len()
1129*0Sstevel@tonic-gate {
1130*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1131*0Sstevel@tonic-gate   cmechanism_t *listptr;
1132*0Sstevel@tonic-gate   unsigned result = 0;
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate   for (listptr = cmechlist->mech_list;
1135*0Sstevel@tonic-gate        listptr;
1136*0Sstevel@tonic-gate        listptr = listptr->next)
1137*0Sstevel@tonic-gate     result += strlen(listptr->plug->mech_name);
1138*0Sstevel@tonic-gate 
1139*0Sstevel@tonic-gate   return result;
1140*0Sstevel@tonic-gate }
1141*0Sstevel@tonic-gate 
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate int _sasl_client_listmech(sasl_conn_t *conn,
1144*0Sstevel@tonic-gate 			  const char *prefix,
1145*0Sstevel@tonic-gate 			  const char *sep,
1146*0Sstevel@tonic-gate 			  const char *suffix,
1147*0Sstevel@tonic-gate 			  const char **result,
1148*0Sstevel@tonic-gate 			  unsigned *plen,
1149*0Sstevel@tonic-gate 			  int *pcount)
1150*0Sstevel@tonic-gate {
1151*0Sstevel@tonic-gate     cmechanism_t *m=NULL;
1152*0Sstevel@tonic-gate     sasl_ssf_t minssf = 0;
1153*0Sstevel@tonic-gate     int ret;
1154*0Sstevel@tonic-gate     unsigned int resultlen;
1155*0Sstevel@tonic-gate     int flag;
1156*0Sstevel@tonic-gate     const char *mysep;
1157*0Sstevel@tonic-gate #ifdef _SUN_SDK_
1158*0Sstevel@tonic-gate     _sasl_global_context_t *gctx = conn == NULL ? _sasl_gbl_ctx() : conn->gctx;
1159*0Sstevel@tonic-gate     cmech_list_t *cmechlist;
1160*0Sstevel@tonic-gate 
1161*0Sstevel@tonic-gate     if(gctx->sasl_client_active==0) return SASL_NOTINIT;
1162*0Sstevel@tonic-gate     cmechlist = gctx->cmechlist;
1163*0Sstevel@tonic-gate #else
1164*0Sstevel@tonic-gate     if(_sasl_client_active == 0) return SASL_NOTINIT;
1165*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1166*0Sstevel@tonic-gate     if (!conn) return SASL_BADPARAM;
1167*0Sstevel@tonic-gate     if(conn->type != SASL_CONN_CLIENT) PARAMERROR(conn);
1168*0Sstevel@tonic-gate 
1169*0Sstevel@tonic-gate     if (! result)
1170*0Sstevel@tonic-gate 	PARAMERROR(conn);
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate #ifdef _SUN_SDK_
1173*0Sstevel@tonic-gate      (void) _load_client_plugins(gctx);
1174*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1175*0Sstevel@tonic-gate 
1176*0Sstevel@tonic-gate     if (plen != NULL)
1177*0Sstevel@tonic-gate 	*plen = 0;
1178*0Sstevel@tonic-gate     if (pcount != NULL)
1179*0Sstevel@tonic-gate 	*pcount = 0;
1180*0Sstevel@tonic-gate 
1181*0Sstevel@tonic-gate     if (sep) {
1182*0Sstevel@tonic-gate 	mysep = sep;
1183*0Sstevel@tonic-gate     } else {
1184*0Sstevel@tonic-gate 	mysep = " ";
1185*0Sstevel@tonic-gate     }
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate     if(conn->props.min_ssf < conn->external.ssf) {
1188*0Sstevel@tonic-gate 	minssf = 0;
1189*0Sstevel@tonic-gate     } else {
1190*0Sstevel@tonic-gate 	minssf = conn->props.min_ssf - conn->external.ssf;
1191*0Sstevel@tonic-gate     }
1192*0Sstevel@tonic-gate 
1193*0Sstevel@tonic-gate     if (! cmechlist || cmechlist->mech_length <= 0)
1194*0Sstevel@tonic-gate 	INTERROR(conn, SASL_NOMECH);
1195*0Sstevel@tonic-gate 
1196*0Sstevel@tonic-gate     resultlen = (prefix ? strlen(prefix) : 0)
1197*0Sstevel@tonic-gate 	+ (strlen(mysep) * (cmechlist->mech_length - 1))
1198*0Sstevel@tonic-gate #ifdef _SUN_SDK_
1199*0Sstevel@tonic-gate 	+ mech_names_len(gctx)
1200*0Sstevel@tonic-gate #else
1201*0Sstevel@tonic-gate 	+ mech_names_len()
1202*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1203*0Sstevel@tonic-gate 	+ (suffix ? strlen(suffix) : 0)
1204*0Sstevel@tonic-gate 	+ 1;
1205*0Sstevel@tonic-gate     ret = _buf_alloc(&conn->mechlist_buf,
1206*0Sstevel@tonic-gate 		     &conn->mechlist_buf_len, resultlen);
1207*0Sstevel@tonic-gate     if(ret != SASL_OK) MEMERROR(conn);
1208*0Sstevel@tonic-gate 
1209*0Sstevel@tonic-gate     if (prefix)
1210*0Sstevel@tonic-gate 	strcpy (conn->mechlist_buf,prefix);
1211*0Sstevel@tonic-gate     else
1212*0Sstevel@tonic-gate 	*(conn->mechlist_buf) = '\0';
1213*0Sstevel@tonic-gate 
1214*0Sstevel@tonic-gate     flag = 0;
1215*0Sstevel@tonic-gate     for (m = cmechlist->mech_list; m != NULL; m = m->next) {
1216*0Sstevel@tonic-gate 	    /* do we have the prompts for it? */
1217*0Sstevel@tonic-gate 	    if (!have_prompts(conn, m->plug))
1218*0Sstevel@tonic-gate 		continue;
1219*0Sstevel@tonic-gate 
1220*0Sstevel@tonic-gate 	    /* is it strong enough? */
1221*0Sstevel@tonic-gate 	    if (minssf > m->plug->max_ssf)
1222*0Sstevel@tonic-gate 		continue;
1223*0Sstevel@tonic-gate 
1224*0Sstevel@tonic-gate 	    /* EXPORT DELETE START */
1225*0Sstevel@tonic-gate 	    /* CRYPT DELETE START */
1226*0Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
1227*0Sstevel@tonic-gate 	    /* If not SUN supplied mech, it has no strength */
1228*0Sstevel@tonic-gate 	    if (minssf > 0 && !m->sun_reg)
1229*0Sstevel@tonic-gate 		continue;
1230*0Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
1231*0Sstevel@tonic-gate 	    /* CRYPT DELETE END */
1232*0Sstevel@tonic-gate 	    /* EXPORT DELETE END */
1233*0Sstevel@tonic-gate 
1234*0Sstevel@tonic-gate 	    /* does it meet our security properties? */
1235*0Sstevel@tonic-gate 	    if (((conn->props.security_flags ^ m->plug->security_flags)
1236*0Sstevel@tonic-gate 		 & conn->props.security_flags) != 0) {
1237*0Sstevel@tonic-gate 		continue;
1238*0Sstevel@tonic-gate 	    }
1239*0Sstevel@tonic-gate 
1240*0Sstevel@tonic-gate 	    /* Can we meet it's features? */
1241*0Sstevel@tonic-gate 	    if ((m->plug->features & SASL_FEAT_NEEDSERVERFQDN)
1242*0Sstevel@tonic-gate 		&& !conn->serverFQDN) {
1243*0Sstevel@tonic-gate 		continue;
1244*0Sstevel@tonic-gate 	    }
1245*0Sstevel@tonic-gate 
1246*0Sstevel@tonic-gate 	    /* Can it meet our features? */
1247*0Sstevel@tonic-gate 	    if ((conn->flags & SASL_NEED_PROXY) &&
1248*0Sstevel@tonic-gate 		!(m->plug->features & SASL_FEAT_ALLOWS_PROXY)) {
1249*0Sstevel@tonic-gate 		break;
1250*0Sstevel@tonic-gate 	    }
1251*0Sstevel@tonic-gate 
1252*0Sstevel@tonic-gate 	    /* Okay, we like it, add it to the list! */
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 	    if (pcount != NULL)
1255*0Sstevel@tonic-gate 		(*pcount)++;
1256*0Sstevel@tonic-gate 
1257*0Sstevel@tonic-gate 	    /* print seperator */
1258*0Sstevel@tonic-gate 	    if (flag) {
1259*0Sstevel@tonic-gate 		strcat(conn->mechlist_buf, mysep);
1260*0Sstevel@tonic-gate 	    } else {
1261*0Sstevel@tonic-gate 		flag = 1;
1262*0Sstevel@tonic-gate 	    }
1263*0Sstevel@tonic-gate 
1264*0Sstevel@tonic-gate 	    /* now print the mechanism name */
1265*0Sstevel@tonic-gate 	    strcat(conn->mechlist_buf, m->plug->mech_name);
1266*0Sstevel@tonic-gate     }
1267*0Sstevel@tonic-gate 
1268*0Sstevel@tonic-gate   if (suffix)
1269*0Sstevel@tonic-gate       strcat(conn->mechlist_buf,suffix);
1270*0Sstevel@tonic-gate 
1271*0Sstevel@tonic-gate   if (plen!=NULL)
1272*0Sstevel@tonic-gate       *plen=strlen(conn->mechlist_buf);
1273*0Sstevel@tonic-gate 
1274*0Sstevel@tonic-gate   *result = conn->mechlist_buf;
1275*0Sstevel@tonic-gate 
1276*0Sstevel@tonic-gate   return SASL_OK;
1277*0Sstevel@tonic-gate }
1278*0Sstevel@tonic-gate 
1279*0Sstevel@tonic-gate #ifdef _SUN_SDK_
1280*0Sstevel@tonic-gate sasl_string_list_t *_sasl_client_mechs(_sasl_global_context_t *gctx)
1281*0Sstevel@tonic-gate {
1282*0Sstevel@tonic-gate   cmech_list_t *cmechlist = gctx->cmechlist;
1283*0Sstevel@tonic-gate #else
1284*0Sstevel@tonic-gate sasl_string_list_t *_sasl_client_mechs(void)
1285*0Sstevel@tonic-gate {
1286*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1287*0Sstevel@tonic-gate   cmechanism_t *listptr;
1288*0Sstevel@tonic-gate   sasl_string_list_t *retval = NULL, *next=NULL;
1289*0Sstevel@tonic-gate 
1290*0Sstevel@tonic-gate #ifdef _SUN_SDK_
1291*0Sstevel@tonic-gate   if(!gctx->sasl_client_active) return NULL;
1292*0Sstevel@tonic-gate #else
1293*0Sstevel@tonic-gate   if(!_sasl_client_active) return NULL;
1294*0Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1295*0Sstevel@tonic-gate 
1296*0Sstevel@tonic-gate   /* make list */
1297*0Sstevel@tonic-gate   for (listptr = cmechlist->mech_list; listptr; listptr = listptr->next) {
1298*0Sstevel@tonic-gate       next = sasl_ALLOC(sizeof(sasl_string_list_t));
1299*0Sstevel@tonic-gate 
1300*0Sstevel@tonic-gate       if(!next && !retval) return NULL;
1301*0Sstevel@tonic-gate       else if(!next) {
1302*0Sstevel@tonic-gate 	  next = retval->next;
1303*0Sstevel@tonic-gate 	  do {
1304*0Sstevel@tonic-gate 	      sasl_FREE(retval);
1305*0Sstevel@tonic-gate 	      retval = next;
1306*0Sstevel@tonic-gate 	      next = retval->next;
1307*0Sstevel@tonic-gate 	  } while(next);
1308*0Sstevel@tonic-gate 	  return NULL;
1309*0Sstevel@tonic-gate       }
1310*0Sstevel@tonic-gate 
1311*0Sstevel@tonic-gate       next->d = listptr->plug->mech_name;
1312*0Sstevel@tonic-gate 
1313*0Sstevel@tonic-gate       if(!retval) {
1314*0Sstevel@tonic-gate 	  next->next = NULL;
1315*0Sstevel@tonic-gate 	  retval = next;
1316*0Sstevel@tonic-gate       } else {
1317*0Sstevel@tonic-gate 	  next->next = retval;
1318*0Sstevel@tonic-gate 	  retval = next;
1319*0Sstevel@tonic-gate       }
1320*0Sstevel@tonic-gate   }
1321*0Sstevel@tonic-gate 
1322*0Sstevel@tonic-gate   return retval;
1323*0Sstevel@tonic-gate }
1324