1*ebfedea0SLionel Sambuc /* $NetBSD: mech.c,v 1.7 2011/02/22 05:45:05 joerg Exp $ */
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc /* Copyright (c) 2010 The NetBSD Foundation, Inc.
4*ebfedea0SLionel Sambuc * All rights reserved.
5*ebfedea0SLionel Sambuc *
6*ebfedea0SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
7*ebfedea0SLionel Sambuc * by Mateusz Kocielski.
8*ebfedea0SLionel Sambuc *
9*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11*ebfedea0SLionel Sambuc * are met:
12*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
15*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
16*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
17*ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
18*ebfedea0SLionel Sambuc * must display the following acknowledgement:
19*ebfedea0SLionel Sambuc * This product includes software developed by the NetBSD
20*ebfedea0SLionel Sambuc * Foundation, Inc. and its contributors.
21*ebfedea0SLionel Sambuc * 4. Neither the name of The NetBSD Foundation nor the names of its
22*ebfedea0SLionel Sambuc * contributors may be used to endorse or promote products derived
23*ebfedea0SLionel Sambuc * from this software without specific prior written permission.
24*ebfedea0SLionel Sambuc *
25*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26*ebfedea0SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27*ebfedea0SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28*ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29*ebfedea0SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30*ebfedea0SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31*ebfedea0SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32*ebfedea0SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33*ebfedea0SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34*ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35*ebfedea0SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
36*ebfedea0SLionel Sambuc */
37*ebfedea0SLionel Sambuc #include <sys/cdefs.h>
38*ebfedea0SLionel Sambuc __RCSID("$NetBSD: mech.c,v 1.7 2011/02/22 05:45:05 joerg Exp $");
39*ebfedea0SLionel Sambuc
40*ebfedea0SLionel Sambuc #include <sys/queue.h>
41*ebfedea0SLionel Sambuc
42*ebfedea0SLionel Sambuc #include <saslc.h>
43*ebfedea0SLionel Sambuc #include <stdio.h>
44*ebfedea0SLionel Sambuc #include <stdlib.h>
45*ebfedea0SLionel Sambuc #include <string.h>
46*ebfedea0SLionel Sambuc
47*ebfedea0SLionel Sambuc #include "dict.h"
48*ebfedea0SLionel Sambuc #include "error.h"
49*ebfedea0SLionel Sambuc #include "mech.h"
50*ebfedea0SLionel Sambuc #include "msg.h"
51*ebfedea0SLionel Sambuc #include "saslc_private.h"
52*ebfedea0SLionel Sambuc
53*ebfedea0SLionel Sambuc /* mechanisms */
54*ebfedea0SLionel Sambuc __weakref_visible const saslc__mech_t weak_saslc__mech_anonymous
55*ebfedea0SLionel Sambuc __weak_reference(saslc__mech_anonymous);
56*ebfedea0SLionel Sambuc __weakref_visible const saslc__mech_t weak_saslc__mech_crammd5
57*ebfedea0SLionel Sambuc __weak_reference(saslc__mech_crammd5);
58*ebfedea0SLionel Sambuc __weakref_visible const saslc__mech_t weak_saslc__mech_digestmd5
59*ebfedea0SLionel Sambuc __weak_reference(saslc__mech_digestmd5);
60*ebfedea0SLionel Sambuc __weakref_visible const saslc__mech_t weak_saslc__mech_external
61*ebfedea0SLionel Sambuc __weak_reference(saslc__mech_external);
62*ebfedea0SLionel Sambuc __weakref_visible const saslc__mech_t weak_saslc__mech_gssapi
63*ebfedea0SLionel Sambuc __weak_reference(saslc__mech_gssapi);
64*ebfedea0SLionel Sambuc __weakref_visible const saslc__mech_t weak_saslc__mech_login
65*ebfedea0SLionel Sambuc __weak_reference(saslc__mech_login);
66*ebfedea0SLionel Sambuc __weakref_visible const saslc__mech_t weak_saslc__mech_plain
67*ebfedea0SLionel Sambuc __weak_reference(saslc__mech_plain);
68*ebfedea0SLionel Sambuc
69*ebfedea0SLionel Sambuc static const saslc__mech_t *saslc__mechanisms[] = {
70*ebfedea0SLionel Sambuc &weak_saslc__mech_anonymous,
71*ebfedea0SLionel Sambuc &weak_saslc__mech_crammd5,
72*ebfedea0SLionel Sambuc &weak_saslc__mech_digestmd5,
73*ebfedea0SLionel Sambuc &weak_saslc__mech_external,
74*ebfedea0SLionel Sambuc &weak_saslc__mech_gssapi,
75*ebfedea0SLionel Sambuc &weak_saslc__mech_login,
76*ebfedea0SLionel Sambuc &weak_saslc__mech_plain,
77*ebfedea0SLionel Sambuc };
78*ebfedea0SLionel Sambuc
79*ebfedea0SLionel Sambuc /*
80*ebfedea0SLionel Sambuc * This table is used by the inline functions in mech.h, which are
81*ebfedea0SLionel Sambuc * used in mech_digestmd5.c and mech_gssapi.c.
82*ebfedea0SLionel Sambuc *
83*ebfedea0SLionel Sambuc * NB: This is indexed by saslc__mech_sess_qop_t values and must be
84*ebfedea0SLionel Sambuc * NULL terminated for use in saslc__list_flags().
85*ebfedea0SLionel Sambuc */
86*ebfedea0SLionel Sambuc const named_flag_t saslc__mech_qop_tbl[] = {
87*ebfedea0SLionel Sambuc { "auth", F_QOP_NONE },
88*ebfedea0SLionel Sambuc { "auth-int", F_QOP_INT },
89*ebfedea0SLionel Sambuc { "auth-conf", F_QOP_CONF },
90*ebfedea0SLionel Sambuc { NULL, 0 }
91*ebfedea0SLionel Sambuc };
92*ebfedea0SLionel Sambuc
93*ebfedea0SLionel Sambuc /**
94*ebfedea0SLionel Sambuc * @brief creates a list of supported mechanisms and their resources.
95*ebfedea0SLionel Sambuc * @param ctx context
96*ebfedea0SLionel Sambuc * @return pointer to head of the list, NULL if allocation failed
97*ebfedea0SLionel Sambuc */
98*ebfedea0SLionel Sambuc saslc__mech_list_t *
saslc__mech_list_create(saslc_t * ctx)99*ebfedea0SLionel Sambuc saslc__mech_list_create(saslc_t *ctx)
100*ebfedea0SLionel Sambuc {
101*ebfedea0SLionel Sambuc saslc__mech_list_t *head = NULL;
102*ebfedea0SLionel Sambuc saslc__mech_list_node_t *node = NULL;
103*ebfedea0SLionel Sambuc size_t i;
104*ebfedea0SLionel Sambuc
105*ebfedea0SLionel Sambuc if ((head = calloc(1, sizeof(*head))) == NULL) {
106*ebfedea0SLionel Sambuc saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
107*ebfedea0SLionel Sambuc return NULL;
108*ebfedea0SLionel Sambuc }
109*ebfedea0SLionel Sambuc for (i = 0; i < __arraycount(saslc__mechanisms); i++) {
110*ebfedea0SLionel Sambuc if (saslc__mechanisms[i] == NULL)
111*ebfedea0SLionel Sambuc continue;
112*ebfedea0SLionel Sambuc if ((node = calloc(1, sizeof(*node))) == NULL)
113*ebfedea0SLionel Sambuc goto error;
114*ebfedea0SLionel Sambuc
115*ebfedea0SLionel Sambuc if ((node->prop = saslc__dict_create()) == NULL) {
116*ebfedea0SLionel Sambuc free(node);
117*ebfedea0SLionel Sambuc goto error;
118*ebfedea0SLionel Sambuc }
119*ebfedea0SLionel Sambuc
120*ebfedea0SLionel Sambuc node->mech = saslc__mechanisms[i];
121*ebfedea0SLionel Sambuc LIST_INSERT_HEAD(head, node, nodes);
122*ebfedea0SLionel Sambuc }
123*ebfedea0SLionel Sambuc return head;
124*ebfedea0SLionel Sambuc
125*ebfedea0SLionel Sambuc error:
126*ebfedea0SLionel Sambuc saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
127*ebfedea0SLionel Sambuc saslc__mech_list_destroy(head);
128*ebfedea0SLionel Sambuc return NULL;
129*ebfedea0SLionel Sambuc }
130*ebfedea0SLionel Sambuc
131*ebfedea0SLionel Sambuc /**
132*ebfedea0SLionel Sambuc * @brief gets mechanism from the list using name
133*ebfedea0SLionel Sambuc * @param list mechanisms list
134*ebfedea0SLionel Sambuc * @param mech_name mechanism name
135*ebfedea0SLionel Sambuc * @return pointer to the mechanism, NULL if mechanism was not found
136*ebfedea0SLionel Sambuc */
137*ebfedea0SLionel Sambuc saslc__mech_list_node_t *
saslc__mech_list_get(saslc__mech_list_t * list,const char * mech_name)138*ebfedea0SLionel Sambuc saslc__mech_list_get(saslc__mech_list_t *list, const char *mech_name)
139*ebfedea0SLionel Sambuc {
140*ebfedea0SLionel Sambuc saslc__mech_list_node_t *node;
141*ebfedea0SLionel Sambuc
142*ebfedea0SLionel Sambuc LIST_FOREACH(node, list, nodes) {
143*ebfedea0SLionel Sambuc if (strcasecmp(node->mech->name, mech_name) == 0)
144*ebfedea0SLionel Sambuc return node;
145*ebfedea0SLionel Sambuc }
146*ebfedea0SLionel Sambuc return NULL;
147*ebfedea0SLionel Sambuc }
148*ebfedea0SLionel Sambuc
149*ebfedea0SLionel Sambuc /**
150*ebfedea0SLionel Sambuc * @brief destroys and deallocates mechanism list
151*ebfedea0SLionel Sambuc * @param list mechanisms list
152*ebfedea0SLionel Sambuc */
153*ebfedea0SLionel Sambuc void
saslc__mech_list_destroy(saslc__mech_list_t * list)154*ebfedea0SLionel Sambuc saslc__mech_list_destroy(saslc__mech_list_t *list)
155*ebfedea0SLionel Sambuc {
156*ebfedea0SLionel Sambuc saslc__mech_list_node_t *node;
157*ebfedea0SLionel Sambuc
158*ebfedea0SLionel Sambuc while (!LIST_EMPTY(list)) {
159*ebfedea0SLionel Sambuc node = LIST_FIRST(list);
160*ebfedea0SLionel Sambuc LIST_REMOVE(node, nodes);
161*ebfedea0SLionel Sambuc saslc__dict_destroy(node->prop);
162*ebfedea0SLionel Sambuc free(node);
163*ebfedea0SLionel Sambuc }
164*ebfedea0SLionel Sambuc free(list);
165*ebfedea0SLionel Sambuc }
166*ebfedea0SLionel Sambuc
167*ebfedea0SLionel Sambuc /**
168*ebfedea0SLionel Sambuc * @brief doing copy of the session property, on error sets
169*ebfedea0SLionel Sambuc * error message for the session. Copied data is located in *out and *outlen
170*ebfedea0SLionel Sambuc * @param sess sasl session
171*ebfedea0SLionel Sambuc * @param out out buffer for the session property copy
172*ebfedea0SLionel Sambuc * @param outlen length of the session property copy
173*ebfedea0SLionel Sambuc * @param name name of the property
174*ebfedea0SLionel Sambuc * @param error_msg error messages set on failure
175*ebfedea0SLionel Sambuc * @return MECH_OK - on success
176*ebfedea0SLionel Sambuc * MECH_ERROR - on failure
177*ebfedea0SLionel Sambuc */
178*ebfedea0SLionel Sambuc int
saslc__mech_strdup(saslc_sess_t * sess,char ** out,size_t * outlen,const char * name,const char * error_msg)179*ebfedea0SLionel Sambuc saslc__mech_strdup(saslc_sess_t *sess, char **out, size_t *outlen,
180*ebfedea0SLionel Sambuc const char *name, const char *error_msg)
181*ebfedea0SLionel Sambuc {
182*ebfedea0SLionel Sambuc const char *value; /* property value */
183*ebfedea0SLionel Sambuc
184*ebfedea0SLionel Sambuc /* get value */
185*ebfedea0SLionel Sambuc if ((value = saslc_sess_getprop(sess, name)) == NULL) {
186*ebfedea0SLionel Sambuc saslc__error_set(ERR(sess), ERROR_MECH, error_msg);
187*ebfedea0SLionel Sambuc return MECH_ERROR;
188*ebfedea0SLionel Sambuc }
189*ebfedea0SLionel Sambuc saslc__msg_dbg("saslc__mech_strdup: value='%s'\n", value);
190*ebfedea0SLionel Sambuc
191*ebfedea0SLionel Sambuc /* copy value */
192*ebfedea0SLionel Sambuc if ((*out = strdup(value)) == NULL) {
193*ebfedea0SLionel Sambuc saslc__error_set_errno(ERR(sess), ERROR_NOMEM);
194*ebfedea0SLionel Sambuc return MECH_ERROR;
195*ebfedea0SLionel Sambuc }
196*ebfedea0SLionel Sambuc if (outlen != NULL)
197*ebfedea0SLionel Sambuc *outlen = strlen(value);
198*ebfedea0SLionel Sambuc
199*ebfedea0SLionel Sambuc return MECH_OK;
200*ebfedea0SLionel Sambuc }
201*ebfedea0SLionel Sambuc
202*ebfedea0SLionel Sambuc /**
203*ebfedea0SLionel Sambuc * @brief generic session create function, this
204*ebfedea0SLionel Sambuc * function is suitable for the most mechanisms.
205*ebfedea0SLionel Sambuc * @return 0 on success, -1 on failure
206*ebfedea0SLionel Sambuc */
207*ebfedea0SLionel Sambuc int
saslc__mech_generic_create(saslc_sess_t * sess)208*ebfedea0SLionel Sambuc saslc__mech_generic_create(saslc_sess_t *sess)
209*ebfedea0SLionel Sambuc {
210*ebfedea0SLionel Sambuc saslc__mech_sess_t *ms;
211*ebfedea0SLionel Sambuc
212*ebfedea0SLionel Sambuc if ((ms = calloc(1, sizeof(*ms))) == NULL) {
213*ebfedea0SLionel Sambuc saslc__error_set(ERR(sess), ERROR_NOMEM, NULL);
214*ebfedea0SLionel Sambuc return -1;
215*ebfedea0SLionel Sambuc }
216*ebfedea0SLionel Sambuc sess->mech_sess = ms;
217*ebfedea0SLionel Sambuc return 0;
218*ebfedea0SLionel Sambuc }
219*ebfedea0SLionel Sambuc
220*ebfedea0SLionel Sambuc /**
221*ebfedea0SLionel Sambuc * @brief generic session destroy function, this
222*ebfedea0SLionel Sambuc * function is suitable for the most mechanisms.
223*ebfedea0SLionel Sambuc * @return function always returns 0
224*ebfedea0SLionel Sambuc */
225*ebfedea0SLionel Sambuc int
saslc__mech_generic_destroy(saslc_sess_t * sess)226*ebfedea0SLionel Sambuc saslc__mech_generic_destroy(saslc_sess_t *sess)
227*ebfedea0SLionel Sambuc {
228*ebfedea0SLionel Sambuc
229*ebfedea0SLionel Sambuc free(sess->mech_sess);
230*ebfedea0SLionel Sambuc sess->mech_sess = NULL;
231*ebfedea0SLionel Sambuc return 0;
232*ebfedea0SLionel Sambuc }
233