1*934a69b8Schristos /* $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 */
370598bb6fSchristos #include <sys/cdefs.h>
38*934a69b8Schristos __RCSID("$NetBSD: saslc.c,v 1.4 2011/02/12 23:21:32 christos Exp $");
39231558cbSagc
40*934a69b8Schristos #include <assert.h>
41231558cbSagc #include <ctype.h>
420598bb6fSchristos #include <saslc.h>
430598bb6fSchristos #include <stdbool.h>
44*934a69b8Schristos #include <stdbool.h>
45231558cbSagc #include <stdio.h>
46231558cbSagc #include <string.h>
470598bb6fSchristos
480598bb6fSchristos #include "crypto.h" /* XXX: for saslc_{de,en}code64() */
49231558cbSagc #include "dict.h"
500598bb6fSchristos #include "error.h"
510598bb6fSchristos #include "mech.h"
520598bb6fSchristos #include "msg.h"
53231558cbSagc #include "parser.h"
54231558cbSagc #include "saslc_private.h"
55231558cbSagc
56231558cbSagc /**
570598bb6fSchristos * @brief check for a valid application name (no path separator)
58231558cbSagc * @param appname application name
59fedeb094Sagc * @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++)
670598bb6fSchristos if (*p == '/')
68231558cbSagc return false;
69231558cbSagc
70231558cbSagc return true;
71231558cbSagc }
72231558cbSagc
73231558cbSagc /**
74231558cbSagc * @brief allocates new saslc context
750598bb6fSchristos * @return pointer to the saslc context
76231558cbSagc */
77231558cbSagc saslc_t *
saslc_alloc(void)78231558cbSagc saslc_alloc(void)
79231558cbSagc {
800598bb6fSchristos
810598bb6fSchristos /* XXX: Check this as early as possible. */
820598bb6fSchristos saslc_debug = getenv(SASLC_ENV_DEBUG) != NULL;
830598bb6fSchristos
84231558cbSagc return calloc(1, sizeof(saslc_t));
85231558cbSagc }
86231558cbSagc
87231558cbSagc /**
88231558cbSagc * @brief initializes sasl context, basing on application name function
890598bb6fSchristos * 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
930598bb6fSchristos * @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)970598bb6fSchristos saslc_init(saslc_t *ctx, const char *appname, const char *pathname)
98231558cbSagc {
99231558cbSagc
100*934a69b8Schristos /* 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");
107fedeb094Sagc goto error;
108231558cbSagc }
1090598bb6fSchristos if ((ctx->appname = strdup(appname)) == NULL) {
110231558cbSagc saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
111fedeb094Sagc goto error;
112231558cbSagc }
1130598bb6fSchristos }
1140598bb6fSchristos if (pathname != NULL && *pathname != '\0') {
1150598bb6fSchristos if ((ctx->pathname = strdup(pathname)) == NULL) {
1160598bb6fSchristos saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
1170598bb6fSchristos goto error;
1180598bb6fSchristos }
1190598bb6fSchristos }
1200598bb6fSchristos ctx->mechanisms = saslc__mech_list_create(ctx);
1210598bb6fSchristos if (ctx->mechanisms == NULL)
1220598bb6fSchristos goto error;
1230598bb6fSchristos
1240598bb6fSchristos /* load the global and mechanism dictionaries */
1250598bb6fSchristos if (saslc__parser_config(ctx) == -1) {
126*934a69b8Schristos free(ctx->appname);
127231558cbSagc ctx->appname = NULL;
1280598bb6fSchristos saslc__dict_destroy(ctx->prop);
1290598bb6fSchristos ctx->prop = NULL;
1300598bb6fSchristos saslc__mech_list_destroy(ctx->mechanisms);
1310598bb6fSchristos ctx->mechanisms = NULL;
1320598bb6fSchristos return -1;
1330598bb6fSchristos }
134231558cbSagc return 0;
135fedeb094Sagc
136fedeb094Sagc error:
1370598bb6fSchristos if (ctx->pathname != NULL) {
1380598bb6fSchristos free(ctx->pathname);
1390598bb6fSchristos ctx->pathname = NULL;
1400598bb6fSchristos }
1410598bb6fSchristos if (ctx->appname != NULL) {
1420598bb6fSchristos free(ctx->appname);
143fedeb094Sagc ctx->appname = NULL;
1440598bb6fSchristos }
1450598bb6fSchristos free(ctx->prop);
146fedeb094Sagc ctx->prop = NULL;
147fedeb094Sagc return -1;
148231558cbSagc }
149231558cbSagc
150231558cbSagc /**
151fedeb094Sagc * @brief gets string message of last error.
152231558cbSagc * @param ctx context
1530598bb6fSchristos * @return pointer to the error message.
154231558cbSagc */
155231558cbSagc const char *
saslc_strerror(saslc_t * ctx)156231558cbSagc saslc_strerror(saslc_t *ctx)
157231558cbSagc {
1580598bb6fSchristos
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
165fedeb094Sagc * the context (if any) should be destroyed
166fedeb094Sagc * @return 0 on success, -1 on failure
167231558cbSagc */
168231558cbSagc int
saslc_end(saslc_t * ctx)1690598bb6fSchristos saslc_end(saslc_t *ctx)
170231558cbSagc {
1710598bb6fSchristos
1720598bb6fSchristos if (ctx->refcnt > 0) {
173231558cbSagc saslc__error_set(ERR(ctx), ERROR_GENERAL,
174231558cbSagc "context has got assigned active sessions");
175231558cbSagc return -1;
176231558cbSagc }
177231558cbSagc
1780598bb6fSchristos if (ctx->mechanisms != NULL)
1790598bb6fSchristos saslc__mech_list_destroy(ctx->mechanisms);
180231558cbSagc
181231558cbSagc if (ctx->prop != NULL)
182231558cbSagc saslc__dict_destroy(ctx->prop);
183231558cbSagc
184*934a69b8Schristos free(ctx->appname);
185231558cbSagc free(ctx);
186231558cbSagc return 0;
187231558cbSagc }
188