1*ebfedea0SLionel Sambuc /* $NetBSD: saslc.c,v 1.4 2011/02/12 23:21:32 christos 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: saslc.c,v 1.4 2011/02/12 23:21:32 christos Exp $");
39*ebfedea0SLionel Sambuc
40*ebfedea0SLionel Sambuc #include <assert.h>
41*ebfedea0SLionel Sambuc #include <ctype.h>
42*ebfedea0SLionel Sambuc #include <saslc.h>
43*ebfedea0SLionel Sambuc #include <stdbool.h>
44*ebfedea0SLionel Sambuc #include <stdbool.h>
45*ebfedea0SLionel Sambuc #include <stdio.h>
46*ebfedea0SLionel Sambuc #include <string.h>
47*ebfedea0SLionel Sambuc
48*ebfedea0SLionel Sambuc #include "crypto.h" /* XXX: for saslc_{de,en}code64() */
49*ebfedea0SLionel Sambuc #include "dict.h"
50*ebfedea0SLionel Sambuc #include "error.h"
51*ebfedea0SLionel Sambuc #include "mech.h"
52*ebfedea0SLionel Sambuc #include "msg.h"
53*ebfedea0SLionel Sambuc #include "parser.h"
54*ebfedea0SLionel Sambuc #include "saslc_private.h"
55*ebfedea0SLionel Sambuc
56*ebfedea0SLionel Sambuc /**
57*ebfedea0SLionel Sambuc * @brief check for a valid application name (no path separator)
58*ebfedea0SLionel Sambuc * @param appname application name
59*ebfedea0SLionel Sambuc * @return true if application name is valid, false otherwise
60*ebfedea0SLionel Sambuc */
61*ebfedea0SLionel Sambuc static bool
saslc__valid_appname(const char * appname)62*ebfedea0SLionel Sambuc saslc__valid_appname(const char *appname)
63*ebfedea0SLionel Sambuc {
64*ebfedea0SLionel Sambuc const char *p;
65*ebfedea0SLionel Sambuc
66*ebfedea0SLionel Sambuc for (p = appname; *p; p++)
67*ebfedea0SLionel Sambuc if (*p == '/')
68*ebfedea0SLionel Sambuc return false;
69*ebfedea0SLionel Sambuc
70*ebfedea0SLionel Sambuc return true;
71*ebfedea0SLionel Sambuc }
72*ebfedea0SLionel Sambuc
73*ebfedea0SLionel Sambuc /**
74*ebfedea0SLionel Sambuc * @brief allocates new saslc context
75*ebfedea0SLionel Sambuc * @return pointer to the saslc context
76*ebfedea0SLionel Sambuc */
77*ebfedea0SLionel Sambuc saslc_t *
saslc_alloc(void)78*ebfedea0SLionel Sambuc saslc_alloc(void)
79*ebfedea0SLionel Sambuc {
80*ebfedea0SLionel Sambuc
81*ebfedea0SLionel Sambuc /* XXX: Check this as early as possible. */
82*ebfedea0SLionel Sambuc saslc_debug = getenv(SASLC_ENV_DEBUG) != NULL;
83*ebfedea0SLionel Sambuc
84*ebfedea0SLionel Sambuc return calloc(1, sizeof(saslc_t));
85*ebfedea0SLionel Sambuc }
86*ebfedea0SLionel Sambuc
87*ebfedea0SLionel Sambuc /**
88*ebfedea0SLionel Sambuc * @brief initializes sasl context, basing on application name function
89*ebfedea0SLionel Sambuc * parses configuration files, sets up default properties and creates
90*ebfedea0SLionel Sambuc * mechanisms list for the context.
91*ebfedea0SLionel Sambuc * @param ctx sasl context
92*ebfedea0SLionel Sambuc * @param appname application name, NULL could be used for generic aplication
93*ebfedea0SLionel Sambuc * @param pathname location of config files. if NULL, use environment or default
94*ebfedea0SLionel Sambuc * @return 0 on success, -1 otherwise.
95*ebfedea0SLionel Sambuc */
96*ebfedea0SLionel Sambuc int
saslc_init(saslc_t * ctx,const char * appname,const char * pathname)97*ebfedea0SLionel Sambuc saslc_init(saslc_t *ctx, const char *appname, const char *pathname)
98*ebfedea0SLionel Sambuc {
99*ebfedea0SLionel Sambuc
100*ebfedea0SLionel Sambuc /* ctx is already zeroed by saslc_alloc(). */
101*ebfedea0SLionel Sambuc ctx->prop = saslc__dict_create();
102*ebfedea0SLionel Sambuc
103*ebfedea0SLionel Sambuc if (appname != NULL) {
104*ebfedea0SLionel Sambuc if (saslc__valid_appname(appname) == false) {
105*ebfedea0SLionel Sambuc saslc__error_set(ERR(ctx), ERROR_BADARG,
106*ebfedea0SLionel Sambuc "application name is not permited");
107*ebfedea0SLionel Sambuc goto error;
108*ebfedea0SLionel Sambuc }
109*ebfedea0SLionel Sambuc if ((ctx->appname = strdup(appname)) == NULL) {
110*ebfedea0SLionel Sambuc saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
111*ebfedea0SLionel Sambuc goto error;
112*ebfedea0SLionel Sambuc }
113*ebfedea0SLionel Sambuc }
114*ebfedea0SLionel Sambuc if (pathname != NULL && *pathname != '\0') {
115*ebfedea0SLionel Sambuc if ((ctx->pathname = strdup(pathname)) == NULL) {
116*ebfedea0SLionel Sambuc saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
117*ebfedea0SLionel Sambuc goto error;
118*ebfedea0SLionel Sambuc }
119*ebfedea0SLionel Sambuc }
120*ebfedea0SLionel Sambuc ctx->mechanisms = saslc__mech_list_create(ctx);
121*ebfedea0SLionel Sambuc if (ctx->mechanisms == NULL)
122*ebfedea0SLionel Sambuc goto error;
123*ebfedea0SLionel Sambuc
124*ebfedea0SLionel Sambuc /* load the global and mechanism dictionaries */
125*ebfedea0SLionel Sambuc if (saslc__parser_config(ctx) == -1) {
126*ebfedea0SLionel Sambuc free(ctx->appname);
127*ebfedea0SLionel Sambuc ctx->appname = NULL;
128*ebfedea0SLionel Sambuc saslc__dict_destroy(ctx->prop);
129*ebfedea0SLionel Sambuc ctx->prop = NULL;
130*ebfedea0SLionel Sambuc saslc__mech_list_destroy(ctx->mechanisms);
131*ebfedea0SLionel Sambuc ctx->mechanisms = NULL;
132*ebfedea0SLionel Sambuc return -1;
133*ebfedea0SLionel Sambuc }
134*ebfedea0SLionel Sambuc return 0;
135*ebfedea0SLionel Sambuc
136*ebfedea0SLionel Sambuc error:
137*ebfedea0SLionel Sambuc if (ctx->pathname != NULL) {
138*ebfedea0SLionel Sambuc free(ctx->pathname);
139*ebfedea0SLionel Sambuc ctx->pathname = NULL;
140*ebfedea0SLionel Sambuc }
141*ebfedea0SLionel Sambuc if (ctx->appname != NULL) {
142*ebfedea0SLionel Sambuc free(ctx->appname);
143*ebfedea0SLionel Sambuc ctx->appname = NULL;
144*ebfedea0SLionel Sambuc }
145*ebfedea0SLionel Sambuc free(ctx->prop);
146*ebfedea0SLionel Sambuc ctx->prop = NULL;
147*ebfedea0SLionel Sambuc return -1;
148*ebfedea0SLionel Sambuc }
149*ebfedea0SLionel Sambuc
150*ebfedea0SLionel Sambuc /**
151*ebfedea0SLionel Sambuc * @brief gets string message of last error.
152*ebfedea0SLionel Sambuc * @param ctx context
153*ebfedea0SLionel Sambuc * @return pointer to the error message.
154*ebfedea0SLionel Sambuc */
155*ebfedea0SLionel Sambuc const char *
saslc_strerror(saslc_t * ctx)156*ebfedea0SLionel Sambuc saslc_strerror(saslc_t *ctx)
157*ebfedea0SLionel Sambuc {
158*ebfedea0SLionel Sambuc
159*ebfedea0SLionel Sambuc return saslc__error_get_strerror(ERR(ctx));
160*ebfedea0SLionel Sambuc }
161*ebfedea0SLionel Sambuc
162*ebfedea0SLionel Sambuc /**
163*ebfedea0SLionel Sambuc * @brief destroys and deallocate resources used by the context.
164*ebfedea0SLionel Sambuc * @param ctx context
165*ebfedea0SLionel Sambuc * the context (if any) should be destroyed
166*ebfedea0SLionel Sambuc * @return 0 on success, -1 on failure
167*ebfedea0SLionel Sambuc */
168*ebfedea0SLionel Sambuc int
saslc_end(saslc_t * ctx)169*ebfedea0SLionel Sambuc saslc_end(saslc_t *ctx)
170*ebfedea0SLionel Sambuc {
171*ebfedea0SLionel Sambuc
172*ebfedea0SLionel Sambuc if (ctx->refcnt > 0) {
173*ebfedea0SLionel Sambuc saslc__error_set(ERR(ctx), ERROR_GENERAL,
174*ebfedea0SLionel Sambuc "context has got assigned active sessions");
175*ebfedea0SLionel Sambuc return -1;
176*ebfedea0SLionel Sambuc }
177*ebfedea0SLionel Sambuc
178*ebfedea0SLionel Sambuc if (ctx->mechanisms != NULL)
179*ebfedea0SLionel Sambuc saslc__mech_list_destroy(ctx->mechanisms);
180*ebfedea0SLionel Sambuc
181*ebfedea0SLionel Sambuc if (ctx->prop != NULL)
182*ebfedea0SLionel Sambuc saslc__dict_destroy(ctx->prop);
183*ebfedea0SLionel Sambuc
184*ebfedea0SLionel Sambuc free(ctx->appname);
185*ebfedea0SLionel Sambuc free(ctx);
186*ebfedea0SLionel Sambuc return 0;
187*ebfedea0SLionel Sambuc }
188