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