1*9674b81eSjoerg /* $NetBSD: mech.c,v 1.7 2011/02/22 05:45:05 joerg Exp $ */
2231558cbSagc
3231558cbSagc /* Copyright (c) 2010 The NetBSD Foundation, Inc.
4231558cbSagc * All rights reserved.
5231558cbSagc *
6231558cbSagc * This code is derived from software contributed to The NetBSD Foundation
7231558cbSagc * by Mateusz Kocielski.
8231558cbSagc *
9231558cbSagc * Redistribution and use in source and binary forms, with or without
10231558cbSagc * modification, are permitted provided that the following conditions
11231558cbSagc * are met:
12231558cbSagc * 1. Redistributions of source code must retain the above copyright
13231558cbSagc * notice, this list of conditions and the following disclaimer.
14231558cbSagc * 2. Redistributions in binary form must reproduce the above copyright
15231558cbSagc * notice, this list of conditions and the following disclaimer in the
16231558cbSagc * documentation and/or other materials provided with the distribution.
17231558cbSagc * 3. All advertising materials mentioning features or use of this software
18231558cbSagc * must display the following acknowledgement:
19231558cbSagc * This product includes software developed by the NetBSD
20231558cbSagc * Foundation, Inc. and its contributors.
21231558cbSagc * 4. Neither the name of The NetBSD Foundation nor the names of its
22231558cbSagc * contributors may be used to endorse or promote products derived
23231558cbSagc * from this software without specific prior written permission.
24231558cbSagc *
25231558cbSagc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26231558cbSagc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27231558cbSagc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28231558cbSagc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29231558cbSagc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30231558cbSagc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31231558cbSagc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32231558cbSagc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33231558cbSagc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34231558cbSagc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35231558cbSagc * POSSIBILITY OF SUCH DAMAGE.
36231558cbSagc */
3719c14409Schristos #include <sys/cdefs.h>
38*9674b81eSjoerg __RCSID("$NetBSD: mech.c,v 1.7 2011/02/22 05:45:05 joerg Exp $");
39231558cbSagc
4019c14409Schristos #include <sys/queue.h>
41beea8b97Schristos
4219c14409Schristos #include <saslc.h>
43231558cbSagc #include <stdio.h>
44231558cbSagc #include <stdlib.h>
45231558cbSagc #include <string.h>
4619c14409Schristos
47231558cbSagc #include "dict.h"
48231558cbSagc #include "error.h"
4919c14409Schristos #include "mech.h"
5019c14409Schristos #include "msg.h"
5119c14409Schristos #include "saslc_private.h"
52231558cbSagc
53231558cbSagc /* mechanisms */
54*9674b81eSjoerg __weakref_visible const saslc__mech_t weak_saslc__mech_anonymous
55561e85baSchristos __weak_reference(saslc__mech_anonymous);
56*9674b81eSjoerg __weakref_visible const saslc__mech_t weak_saslc__mech_crammd5
57561e85baSchristos __weak_reference(saslc__mech_crammd5);
58*9674b81eSjoerg __weakref_visible const saslc__mech_t weak_saslc__mech_digestmd5
59561e85baSchristos __weak_reference(saslc__mech_digestmd5);
60*9674b81eSjoerg __weakref_visible const saslc__mech_t weak_saslc__mech_external
61561e85baSchristos __weak_reference(saslc__mech_external);
62*9674b81eSjoerg __weakref_visible const saslc__mech_t weak_saslc__mech_gssapi
63561e85baSchristos __weak_reference(saslc__mech_gssapi);
64*9674b81eSjoerg __weakref_visible const saslc__mech_t weak_saslc__mech_login
65561e85baSchristos __weak_reference(saslc__mech_login);
66*9674b81eSjoerg __weakref_visible const saslc__mech_t weak_saslc__mech_plain
67561e85baSchristos __weak_reference(saslc__mech_plain);
68231558cbSagc
6919c14409Schristos static const saslc__mech_t *saslc__mechanisms[] = {
70*9674b81eSjoerg &weak_saslc__mech_anonymous,
71*9674b81eSjoerg &weak_saslc__mech_crammd5,
72*9674b81eSjoerg &weak_saslc__mech_digestmd5,
73*9674b81eSjoerg &weak_saslc__mech_external,
74*9674b81eSjoerg &weak_saslc__mech_gssapi,
75*9674b81eSjoerg &weak_saslc__mech_login,
76*9674b81eSjoerg &weak_saslc__mech_plain,
77231558cbSagc };
78231558cbSagc
7919c14409Schristos /*
8019c14409Schristos * This table is used by the inline functions in mech.h, which are
8119c14409Schristos * used in mech_digestmd5.c and mech_gssapi.c.
8219c14409Schristos *
8319c14409Schristos * NB: This is indexed by saslc__mech_sess_qop_t values and must be
8419c14409Schristos * NULL terminated for use in saslc__list_flags().
85231558cbSagc */
8619c14409Schristos const named_flag_t saslc__mech_qop_tbl[] = {
8719c14409Schristos { "auth", F_QOP_NONE },
8819c14409Schristos { "auth-int", F_QOP_INT },
8919c14409Schristos { "auth-conf", F_QOP_CONF },
9019c14409Schristos { NULL, 0 }
9119c14409Schristos };
92231558cbSagc
9319c14409Schristos /**
9419c14409Schristos * @brief creates a list of supported mechanisms and their resources.
9519c14409Schristos * @param ctx context
9619c14409Schristos * @return pointer to head of the list, NULL if allocation failed
9719c14409Schristos */
9819c14409Schristos saslc__mech_list_t *
saslc__mech_list_create(saslc_t * ctx)99231558cbSagc saslc__mech_list_create(saslc_t *ctx)
100231558cbSagc {
10119c14409Schristos saslc__mech_list_t *head = NULL;
102231558cbSagc saslc__mech_list_node_t *node = NULL;
103231558cbSagc size_t i;
104231558cbSagc
10519c14409Schristos if ((head = calloc(1, sizeof(*head))) == NULL) {
10619c14409Schristos saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
10719c14409Schristos return NULL;
10819c14409Schristos }
109d02347a6Schristos for (i = 0; i < __arraycount(saslc__mechanisms); i++) {
110561e85baSchristos if (saslc__mechanisms[i] == NULL)
111561e85baSchristos continue;
11219c14409Schristos if ((node = calloc(1, sizeof(*node))) == NULL)
113231558cbSagc goto error;
114231558cbSagc
11519c14409Schristos if ((node->prop = saslc__dict_create()) == NULL) {
11619c14409Schristos free(node);
117231558cbSagc goto error;
118231558cbSagc }
119231558cbSagc
12019c14409Schristos node->mech = saslc__mechanisms[i];
12119c14409Schristos LIST_INSERT_HEAD(head, node, nodes);
12219c14409Schristos }
12319c14409Schristos return head;
124231558cbSagc
125231558cbSagc error:
126231558cbSagc saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
12719c14409Schristos saslc__mech_list_destroy(head);
12819c14409Schristos return NULL;
129231558cbSagc }
130231558cbSagc
131231558cbSagc /**
132231558cbSagc * @brief gets mechanism from the list using name
133231558cbSagc * @param list mechanisms list
134231558cbSagc * @param mech_name mechanism name
135231558cbSagc * @return pointer to the mechanism, NULL if mechanism was not found
136231558cbSagc */
137231558cbSagc saslc__mech_list_node_t *
saslc__mech_list_get(saslc__mech_list_t * list,const char * mech_name)138231558cbSagc saslc__mech_list_get(saslc__mech_list_t *list, const char *mech_name)
139231558cbSagc {
140231558cbSagc saslc__mech_list_node_t *node;
141231558cbSagc
1426b638291Sagc LIST_FOREACH(node, list, nodes) {
143231558cbSagc if (strcasecmp(node->mech->name, mech_name) == 0)
144231558cbSagc return node;
1456b638291Sagc }
146231558cbSagc return NULL;
147231558cbSagc }
148231558cbSagc
149231558cbSagc /**
150231558cbSagc * @brief destroys and deallocates mechanism list
151231558cbSagc * @param list mechanisms list
152231558cbSagc */
153231558cbSagc void
saslc__mech_list_destroy(saslc__mech_list_t * list)154231558cbSagc saslc__mech_list_destroy(saslc__mech_list_t *list)
155231558cbSagc {
156231558cbSagc saslc__mech_list_node_t *node;
157231558cbSagc
1586b638291Sagc while (!LIST_EMPTY(list)) {
1596b638291Sagc node = LIST_FIRST(list);
160231558cbSagc LIST_REMOVE(node, nodes);
1616b638291Sagc saslc__dict_destroy(node->prop);
162231558cbSagc free(node);
163231558cbSagc }
16419c14409Schristos free(list);
165231558cbSagc }
166231558cbSagc
167231558cbSagc /**
168231558cbSagc * @brief doing copy of the session property, on error sets
169231558cbSagc * error message for the session. Copied data is located in *out and *outlen
170231558cbSagc * @param sess sasl session
171231558cbSagc * @param out out buffer for the session property copy
172231558cbSagc * @param outlen length of the session property copy
173231558cbSagc * @param name name of the property
174231558cbSagc * @param error_msg error messages set on failure
175231558cbSagc * @return MECH_OK - on success
176231558cbSagc * MECH_ERROR - on failure
177231558cbSagc */
178231558cbSagc int
saslc__mech_strdup(saslc_sess_t * sess,char ** out,size_t * outlen,const char * name,const char * error_msg)179231558cbSagc saslc__mech_strdup(saslc_sess_t *sess, char **out, size_t *outlen,
180231558cbSagc const char *name, const char *error_msg)
181231558cbSagc {
182231558cbSagc const char *value; /* property value */
183231558cbSagc
184231558cbSagc /* get value */
18519c14409Schristos if ((value = saslc_sess_getprop(sess, name)) == NULL) {
186231558cbSagc saslc__error_set(ERR(sess), ERROR_MECH, error_msg);
187231558cbSagc return MECH_ERROR;
188231558cbSagc }
18919c14409Schristos saslc__msg_dbg("saslc__mech_strdup: value='%s'\n", value);
190231558cbSagc
191231558cbSagc /* copy value */
19219c14409Schristos if ((*out = strdup(value)) == NULL) {
193231558cbSagc saslc__error_set_errno(ERR(sess), ERROR_NOMEM);
194231558cbSagc return MECH_ERROR;
195231558cbSagc }
196231558cbSagc if (outlen != NULL)
19719c14409Schristos *outlen = strlen(value);
198231558cbSagc
199231558cbSagc return MECH_OK;
200231558cbSagc }
201231558cbSagc
202231558cbSagc /**
203231558cbSagc * @brief generic session create function, this
204231558cbSagc * function is suitable for the most mechanisms.
205231558cbSagc * @return 0 on success, -1 on failure
206231558cbSagc */
207231558cbSagc int
saslc__mech_generic_create(saslc_sess_t * sess)208231558cbSagc saslc__mech_generic_create(saslc_sess_t *sess)
209231558cbSagc {
21019c14409Schristos saslc__mech_sess_t *ms;
2116b638291Sagc
21219c14409Schristos if ((ms = calloc(1, sizeof(*ms))) == NULL) {
213231558cbSagc saslc__error_set(ERR(sess), ERROR_NOMEM, NULL);
214231558cbSagc return -1;
215231558cbSagc }
21619c14409Schristos sess->mech_sess = ms;
217231558cbSagc return 0;
218231558cbSagc }
219231558cbSagc
220231558cbSagc /**
221231558cbSagc * @brief generic session destroy function, this
222231558cbSagc * function is suitable for the most mechanisms.
223231558cbSagc * @return function always returns 0
224231558cbSagc */
225231558cbSagc int
saslc__mech_generic_destroy(saslc_sess_t * sess)226231558cbSagc saslc__mech_generic_destroy(saslc_sess_t *sess)
227231558cbSagc {
228beea8b97Schristos
229231558cbSagc free(sess->mech_sess);
230231558cbSagc sess->mech_sess = NULL;
231231558cbSagc return 0;
232231558cbSagc }
233