1/* init.c --- Entry point for libgsasl.
2 * Copyright (C) 2002-2012 Simon Josefsson
3 *
4 * This file is part of GNU SASL Library.
5 *
6 * GNU SASL Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * GNU SASL Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License License along with GNU SASL Library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "internal.h"
24
25/* Get gc_init. */
26#include <gc.h>
27
28/* Get mechanism headers. */
29#include "cram-md5/cram-md5.h"
30#include "external/external.h"
31#include "gssapi/x-gssapi.h"
32#include "gs2/gs2.h"
33#include "anonymous/anonymous.h"
34#include "plain/plain.h"
35#include "securid/securid.h"
36#include "digest-md5/digest-md5.h"
37#include "scram/scram.h"
38#include "saml20/saml20.h"
39#include "openid20/openid20.h"
40
41#include "login/login.h"
42//#include "ntlm/x-ntlm.h"
43//#include "kerberos_v5/kerberos_v5.h"
44
45/**
46 * GSASL_VALID_MECHANISM_CHARACTERS:
47 *
48 * A zero-terminated character array, or string, with all ASCII
49 * characters that may be used within a SASL mechanism name.
50 **/
51const char *GSASL_VALID_MECHANISM_CHARACTERS =
52 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
53
54static int
55register_builtin_mechs (Gsasl * ctx)
56{
57 int rc = GSASL_OK;
58
59#ifdef USE_ANONYMOUS
60 rc = gsasl_register (ctx, &gsasl_anonymous_mechanism);
61 if (rc != GSASL_OK)
62 return rc;
63#endif /* USE_ANONYMOUS */
64
65#ifdef USE_EXTERNAL
66 rc = gsasl_register (ctx, &gsasl_external_mechanism);
67 if (rc != GSASL_OK)
68 return rc;
69#endif /* USE_EXTERNAL */
70
71#ifdef USE_LOGIN
72 rc = gsasl_register (ctx, &gsasl_login_mechanism);
73 if (rc != GSASL_OK)
74 return rc;
75#endif /* USE_LOGIN */
76
77#ifdef USE_PLAIN
78 rc = gsasl_register (ctx, &gsasl_plain_mechanism);
79 if (rc != GSASL_OK)
80 return rc;
81#endif /* USE_PLAIN */
82
83#ifdef USE_SECURID
84 rc = gsasl_register (ctx, &gsasl_securid_mechanism);
85 if (rc != GSASL_OK)
86 return rc;
87#endif /* USE_SECURID */
88
89#ifdef USE_NTLM
90 rc = gsasl_register (ctx, &gsasl_ntlm_mechanism);
91 if (rc != GSASL_OK)
92 return rc;
93#endif /* USE_NTLM */
94
95#ifdef USE_DIGEST_MD5
96 rc = gsasl_register (ctx, &gsasl_digest_md5_mechanism);
97 if (rc != GSASL_OK)
98 return rc;
99#endif /* USE_DIGEST_MD5 */
100
101#ifdef USE_CRAM_MD5
102 rc = gsasl_register (ctx, &gsasl_cram_md5_mechanism);
103 if (rc != GSASL_OK)
104 return rc;
105#endif /* USE_CRAM_MD5 */
106
107#ifdef USE_SCRAM_SHA1
108 rc = gsasl_register (ctx, &gsasl_scram_sha1_mechanism);
109 if (rc != GSASL_OK)
110 return rc;
111
112 rc = gsasl_register (ctx, &gsasl_scram_sha1_plus_mechanism);
113 if (rc != GSASL_OK)
114 return rc;
115#endif /* USE_SCRAM_SHA1 */
116
117#ifdef USE_SAML20
118 rc = gsasl_register (ctx, &gsasl_saml20_mechanism);
119 if (rc != GSASL_OK)
120 return rc;
121#endif /* USE_SAML20 */
122
123#ifdef USE_OPENID20
124 rc = gsasl_register (ctx, &gsasl_openid20_mechanism);
125 if (rc != GSASL_OK)
126 return rc;
127#endif /* USE_OPENID20 */
128
129#ifdef USE_GSSAPI
130 rc = gsasl_register (ctx, &gsasl_gssapi_mechanism);
131 if (rc != GSASL_OK)
132 return rc;
133#endif /* USE_GSSAPI */
134
135#ifdef USE_GS2
136 rc = gsasl_register (ctx, &gsasl_gs2_krb5_mechanism);
137 if (rc != GSASL_OK)
138 return rc;
139#endif /* USE_GSSAPI */
140
141 return GSASL_OK;
142}
143
144/**
145 * gsasl_init:
146 * @ctx: pointer to libgsasl handle.
147 *
148 * This functions initializes libgsasl. The handle pointed to by ctx
149 * is valid for use with other libgsasl functions iff this function is
150 * successful. It also register all builtin SASL mechanisms, using
151 * gsasl_register().
152 *
153 * Return value: GSASL_OK iff successful, otherwise
154 * %GSASL_MALLOC_ERROR.
155 **/
156int
157gsasl_init (Gsasl ** ctx)
158{
159 int rc;
160
161 if (gc_init () != GC_OK)
162 return GSASL_CRYPTO_ERROR;
163
164 *ctx = (Gsasl *) calloc (1, sizeof (**ctx));
165 if (*ctx == NULL)
166 return GSASL_MALLOC_ERROR;
167
168 rc = register_builtin_mechs (*ctx);
169 if (rc != GSASL_OK)
170 {
171 gsasl_done (*ctx);
172 return rc;
173 }
174
175 return GSASL_OK;
176}
177