1*beea8b97Schristos /* $NetBSD: saslc.c,v 1.4 2011/02/12 23:21:32 christos 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*beea8b97Schristos __RCSID("$NetBSD: saslc.c,v 1.4 2011/02/12 23:21:32 christos Exp $");
39231558cbSagc
40*beea8b97Schristos #include <assert.h>
41231558cbSagc #include <ctype.h>
4219c14409Schristos #include <saslc.h>
4319c14409Schristos #include <stdbool.h>
44*beea8b97Schristos #include <stdbool.h>
45231558cbSagc #include <stdio.h>
46231558cbSagc #include <string.h>
4719c14409Schristos
4819c14409Schristos #include "crypto.h" /* XXX: for saslc_{de,en}code64() */
49231558cbSagc #include "dict.h"
5019c14409Schristos #include "error.h"
5119c14409Schristos #include "mech.h"
5219c14409Schristos #include "msg.h"
53231558cbSagc #include "parser.h"
54231558cbSagc #include "saslc_private.h"
55231558cbSagc
56231558cbSagc /**
5719c14409Schristos * @brief check for a valid application name (no path separator)
58231558cbSagc * @param appname application name
596b638291Sagc * @return true if application name is valid, false otherwise
60231558cbSagc */
61231558cbSagc static bool
saslc__valid_appname(const char * appname)62231558cbSagc saslc__valid_appname(const char *appname)
63231558cbSagc {
64231558cbSagc const char *p;
65231558cbSagc
66231558cbSagc for (p = appname; *p; p++)
6719c14409Schristos if (*p == '/')
68231558cbSagc return false;
69231558cbSagc
70231558cbSagc return true;
71231558cbSagc }
72231558cbSagc
73231558cbSagc /**
74231558cbSagc * @brief allocates new saslc context
7519c14409Schristos * @return pointer to the saslc context
76231558cbSagc */
77231558cbSagc saslc_t *
saslc_alloc(void)78231558cbSagc saslc_alloc(void)
79231558cbSagc {
8019c14409Schristos
8119c14409Schristos /* XXX: Check this as early as possible. */
8219c14409Schristos saslc_debug = getenv(SASLC_ENV_DEBUG) != NULL;
8319c14409Schristos
84231558cbSagc return calloc(1, sizeof(saslc_t));
85231558cbSagc }
86231558cbSagc
87231558cbSagc /**
88231558cbSagc * @brief initializes sasl context, basing on application name function
8919c14409Schristos * parses configuration files, sets up default properties and creates
90231558cbSagc * mechanisms list for the context.
91231558cbSagc * @param ctx sasl context
92231558cbSagc * @param appname application name, NULL could be used for generic aplication
9319c14409Schristos * @param pathname location of config files. if NULL, use environment or default
94231558cbSagc * @return 0 on success, -1 otherwise.
95231558cbSagc */
96231558cbSagc int
saslc_init(saslc_t * ctx,const char * appname,const char * pathname)9719c14409Schristos saslc_init(saslc_t *ctx, const char *appname, const char *pathname)
98231558cbSagc {
99231558cbSagc
100*beea8b97Schristos /* ctx is already zeroed by saslc_alloc(). */
101231558cbSagc ctx->prop = saslc__dict_create();
102231558cbSagc
103231558cbSagc if (appname != NULL) {
104231558cbSagc if (saslc__valid_appname(appname) == false) {
105231558cbSagc saslc__error_set(ERR(ctx), ERROR_BADARG,
106231558cbSagc "application name is not permited");
1076b638291Sagc goto error;
108231558cbSagc }
10919c14409Schristos if ((ctx->appname = strdup(appname)) == NULL) {
110231558cbSagc saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
1116b638291Sagc goto error;
112231558cbSagc }
11319c14409Schristos }
11419c14409Schristos if (pathname != NULL && *pathname != '\0') {
11519c14409Schristos if ((ctx->pathname = strdup(pathname)) == NULL) {
11619c14409Schristos saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
11719c14409Schristos goto error;
11819c14409Schristos }
11919c14409Schristos }
12019c14409Schristos ctx->mechanisms = saslc__mech_list_create(ctx);
12119c14409Schristos if (ctx->mechanisms == NULL)
12219c14409Schristos goto error;
12319c14409Schristos
12419c14409Schristos /* load the global and mechanism dictionaries */
12519c14409Schristos if (saslc__parser_config(ctx) == -1) {
126*beea8b97Schristos free(ctx->appname);
127231558cbSagc ctx->appname = NULL;
12819c14409Schristos saslc__dict_destroy(ctx->prop);
12919c14409Schristos ctx->prop = NULL;
13019c14409Schristos saslc__mech_list_destroy(ctx->mechanisms);
13119c14409Schristos ctx->mechanisms = NULL;
13219c14409Schristos return -1;
13319c14409Schristos }
134231558cbSagc return 0;
1356b638291Sagc
1366b638291Sagc error:
13719c14409Schristos if (ctx->pathname != NULL) {
13819c14409Schristos free(ctx->pathname);
13919c14409Schristos ctx->pathname = NULL;
14019c14409Schristos }
14119c14409Schristos if (ctx->appname != NULL) {
14219c14409Schristos free(ctx->appname);
1436b638291Sagc ctx->appname = NULL;
14419c14409Schristos }
14519c14409Schristos free(ctx->prop);
1466b638291Sagc ctx->prop = NULL;
1476b638291Sagc return -1;
148231558cbSagc }
149231558cbSagc
150231558cbSagc /**
1516b638291Sagc * @brief gets string message of last error.
152231558cbSagc * @param ctx context
15319c14409Schristos * @return pointer to the error message.
154231558cbSagc */
155231558cbSagc const char *
saslc_strerror(saslc_t * ctx)156231558cbSagc saslc_strerror(saslc_t *ctx)
157231558cbSagc {
15819c14409Schristos
159231558cbSagc return saslc__error_get_strerror(ERR(ctx));
160231558cbSagc }
161231558cbSagc
162231558cbSagc /**
163231558cbSagc * @brief destroys and deallocate resources used by the context.
164231558cbSagc * @param ctx context
1656b638291Sagc * the context (if any) should be destroyed
1666b638291Sagc * @return 0 on success, -1 on failure
167231558cbSagc */
168231558cbSagc int
saslc_end(saslc_t * ctx)16919c14409Schristos saslc_end(saslc_t *ctx)
170231558cbSagc {
17119c14409Schristos
17219c14409Schristos if (ctx->refcnt > 0) {
173231558cbSagc saslc__error_set(ERR(ctx), ERROR_GENERAL,
174231558cbSagc "context has got assigned active sessions");
175231558cbSagc return -1;
176231558cbSagc }
177231558cbSagc
17819c14409Schristos if (ctx->mechanisms != NULL)
17919c14409Schristos saslc__mech_list_destroy(ctx->mechanisms);
180231558cbSagc
181231558cbSagc if (ctx->prop != NULL)
182231558cbSagc saslc__dict_destroy(ctx->prop);
183231558cbSagc
184*beea8b97Schristos free(ctx->appname);
185231558cbSagc free(ctx);
186231558cbSagc return 0;
187231558cbSagc }
188