2 Unix SMB/CIFS implementation.
4 Connect GENSEC to an external SASL lib
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/tsocket/tsocket.h"
24 #include "auth/credentials/credentials.h"
25 #include "auth/gensec/gensec.h"
26 #include "auth/gensec/gensec_proto.h"
27 #include <sasl/sasl.h>
29 struct gensec_sasl_state
{
34 static NTSTATUS
sasl_nt_status(int sasl_ret
)
38 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
40 return NT_STATUS_NO_MEMORY
;
43 return NT_STATUS_INVALID_PARAMETER
;
45 return NT_STATUS_ACCESS_DENIED
;
49 return NT_STATUS_UNSUCCESSFUL
;
53 static int gensec_sasl_get_user(void *context
, int id
,
54 const char **result
, unsigned *len
)
56 struct gensec_security
*gensec_security
= talloc_get_type(context
, struct gensec_security
);
57 const char *username
= cli_credentials_get_username(gensec_get_credentials(gensec_security
));
58 if (id
!= SASL_CB_USER
&& id
!= SASL_CB_AUTHNAME
) {
66 static int gensec_sasl_get_realm(void *context
, int id
,
67 const char **availrealms
,
70 struct gensec_security
*gensec_security
= talloc_get_type(context
, struct gensec_security
);
71 const char *realm
= cli_credentials_get_realm(gensec_get_credentials(gensec_security
));
73 if (id
!= SASL_CB_GETREALM
) {
77 for (i
=0; availrealms
&& availrealms
[i
]; i
++) {
78 if (strcasecmp_m(realm
, availrealms
[i
]) == 0) {
79 result
[i
] = availrealms
[i
];
83 /* None of the realms match, so lets not specify one */
88 static int gensec_sasl_get_password(sasl_conn_t
*conn
, void *context
, int id
,
89 sasl_secret_t
**psecret
)
91 struct gensec_security
*gensec_security
= talloc_get_type(context
, struct gensec_security
);
92 const char *password
= cli_credentials_get_password(gensec_get_credentials(gensec_security
));
94 sasl_secret_t
*secret
;
99 secret
= talloc_size(gensec_security
, sizeof(sasl_secret_t
)+strlen(password
));
103 secret
->len
= strlen(password
);
104 safe_strcpy((char*)secret
->data
, password
, secret
->len
+1);
109 static int gensec_sasl_dispose(struct gensec_sasl_state
*gensec_sasl_state
)
111 sasl_dispose(&gensec_sasl_state
->conn
);
115 static NTSTATUS
gensec_sasl_client_start(struct gensec_security
*gensec_security
)
117 struct gensec_sasl_state
*gensec_sasl_state
;
118 const char *service
= gensec_get_target_service(gensec_security
);
119 const char *target_name
= gensec_get_target_hostname(gensec_security
);
120 const struct tsocket_address
*tlocal_addr
= gensec_get_local_address(gensec_security
);
121 const struct tsocket_address
*tremote_addr
= gensec_get_remote_address(gensec_security
);
122 char *local_addr
= NULL
;
123 char *remote_addr
= NULL
;
126 sasl_callback_t
*callbacks
;
128 gensec_sasl_state
= talloc(gensec_security
, struct gensec_sasl_state
);
129 if (!gensec_sasl_state
) {
130 return NT_STATUS_NO_MEMORY
;
133 callbacks
= talloc_array(gensec_sasl_state
, sasl_callback_t
, 5);
134 callbacks
[0].id
= SASL_CB_USER
;
135 callbacks
[0].proc
= gensec_sasl_get_user
;
136 callbacks
[0].context
= gensec_security
;
138 callbacks
[1].id
= SASL_CB_AUTHNAME
;
139 callbacks
[1].proc
= gensec_sasl_get_user
;
140 callbacks
[1].context
= gensec_security
;
142 callbacks
[2].id
= SASL_CB_GETREALM
;
143 callbacks
[2].proc
= gensec_sasl_get_realm
;
144 callbacks
[2].context
= gensec_security
;
146 callbacks
[3].id
= SASL_CB_PASS
;
147 callbacks
[3].proc
= gensec_sasl_get_password
;
148 callbacks
[3].context
= gensec_security
;
150 callbacks
[4].id
= SASL_CB_LIST_END
;
151 callbacks
[4].proc
= NULL
;
152 callbacks
[4].context
= NULL
;
154 gensec_security
->private_data
= gensec_sasl_state
;
157 local_addr
= talloc_asprintf(gensec_sasl_state
,
159 tsocket_address_inet_addr_string(tlocal_addr
, gensec_sasl_state
),
160 tsocket_address_inet_port(tlocal_addr
));
164 remote_addr
= talloc_asprintf(gensec_sasl_state
,
166 tsocket_address_inet_addr_string(tremote_addr
, gensec_sasl_state
),
167 tsocket_address_inet_port(tremote_addr
));
169 gensec_sasl_state
->step
= 0;
171 sasl_ret
= sasl_client_new(service
,
173 local_addr
, remote_addr
, callbacks
, 0,
174 &gensec_sasl_state
->conn
);
176 if (sasl_ret
== SASL_OK
|| sasl_ret
== SASL_CONTINUE
) {
177 sasl_security_properties_t props
;
178 talloc_set_destructor(gensec_sasl_state
, gensec_sasl_dispose
);
181 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
184 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
188 props
.max_ssf
= UINT_MAX
;
189 props
.maxbufsize
= 65536;
190 sasl_ret
= sasl_setprop(gensec_sasl_state
->conn
, SASL_SEC_PROPS
, &props
);
191 if (sasl_ret
!= SASL_OK
) {
192 return sasl_nt_status(sasl_ret
);
196 DEBUG(1, ("GENSEC SASL: client_new failed: %s\n", sasl_errdetail(gensec_sasl_state
->conn
)));
198 return sasl_nt_status(sasl_ret
);
201 static NTSTATUS
gensec_sasl_update(struct gensec_security
*gensec_security
,
202 TALLOC_CTX
*out_mem_ctx
,
203 const DATA_BLOB in
, DATA_BLOB
*out
)
205 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
206 struct gensec_sasl_state
);
208 const char *out_data
;
209 unsigned int out_len
;
211 if (gensec_sasl_state
->step
== 0) {
213 sasl_ret
= sasl_client_start(gensec_sasl_state
->conn
, gensec_security
->ops
->sasl_name
,
214 NULL
, &out_data
, &out_len
, &mech
);
216 sasl_ret
= sasl_client_step(gensec_sasl_state
->conn
,
217 (char*)in
.data
, in
.length
, NULL
,
218 &out_data
, &out_len
);
220 if (sasl_ret
== SASL_OK
|| sasl_ret
== SASL_CONTINUE
) {
221 *out
= data_blob_talloc(out_mem_ctx
, out_data
, out_len
);
223 DEBUG(1, ("GENSEC SASL: step %d update failed: %s\n", gensec_sasl_state
->step
,
224 sasl_errdetail(gensec_sasl_state
->conn
)));
226 gensec_sasl_state
->step
++;
227 return sasl_nt_status(sasl_ret
);
230 static NTSTATUS
gensec_sasl_unwrap_packets(struct gensec_security
*gensec_security
,
231 TALLOC_CTX
*out_mem_ctx
,
234 size_t *len_processed
)
236 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
237 struct gensec_sasl_state
);
238 const char *out_data
;
239 unsigned int out_len
;
241 int sasl_ret
= sasl_decode(gensec_sasl_state
->conn
,
242 (char*)in
->data
, in
->length
, &out_data
,
244 if (sasl_ret
== SASL_OK
) {
245 *out
= data_blob_talloc(out_mem_ctx
, out_data
, out_len
);
246 *len_processed
= in
->length
;
248 DEBUG(1, ("GENSEC SASL: unwrap failed: %s\n", sasl_errdetail(gensec_sasl_state
->conn
)));
250 return sasl_nt_status(sasl_ret
);
254 static NTSTATUS
gensec_sasl_wrap_packets(struct gensec_security
*gensec_security
,
255 TALLOC_CTX
*out_mem_ctx
,
258 size_t *len_processed
)
260 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
261 struct gensec_sasl_state
);
262 const char *out_data
;
263 unsigned int out_len
;
265 int sasl_ret
= sasl_encode(gensec_sasl_state
->conn
,
266 (char*)in
->data
, in
->length
, &out_data
,
268 if (sasl_ret
== SASL_OK
) {
269 *out
= data_blob_talloc(out_mem_ctx
, out_data
, out_len
);
270 *len_processed
= in
->length
;
272 DEBUG(1, ("GENSEC SASL: wrap failed: %s\n", sasl_errdetail(gensec_sasl_state
->conn
)));
274 return sasl_nt_status(sasl_ret
);
277 /* Try to figure out what features we actually got on the connection */
278 static bool gensec_sasl_have_feature(struct gensec_security
*gensec_security
,
281 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
282 struct gensec_sasl_state
);
284 int sasl_ret
= sasl_getprop(gensec_sasl_state
->conn
, SASL_SSF
,
286 if (sasl_ret
!= SASL_OK
) {
289 if (feature
& GENSEC_FEATURE_SIGN
) {
297 if (feature
& GENSEC_FEATURE_SEAL
) {
308 /* This could in theory work with any SASL mech */
309 static const struct gensec_security_ops gensec_sasl_security_ops
= {
310 .name
= "sasl-DIGEST-MD5",
311 .sasl_name
= "DIGEST-MD5",
312 .client_start
= gensec_sasl_client_start
,
313 .update
= gensec_sasl_update
,
314 .wrap_packets
= gensec_sasl_wrap_packets
,
315 .unwrap_packets
= gensec_sasl_unwrap_packets
,
316 .have_feature
= gensec_sasl_have_feature
,
318 .priority
= GENSEC_SASL
321 static int gensec_sasl_log(void *context
,
326 switch (sasl_log_level
) {
357 DEBUG(dl
, ("gensec_sasl: %s\n", message
));
362 NTSTATUS
gensec_sasl_init(void)
368 const char **sasl_mechs
;
371 static const sasl_callback_t callbacks
[] = {
374 .proc
= gensec_sasl_log
,
378 .id
= SASL_CB_LIST_END
,
379 .proc
= gensec_sasl_log
,
383 sasl_ret
= sasl_client_init(callbacks
);
385 if (sasl_ret
== SASL_NOMECH
) {
386 /* Nothing to do here */
390 if (sasl_ret
!= SASL_OK
) {
391 return sasl_nt_status(sasl_ret
);
394 /* For now, we just register DIGEST-MD5 */
396 ret
= gensec_register(&gensec_sasl_security_ops
);
397 if (!NT_STATUS_IS_OK(ret
)) {
398 DEBUG(0,("Failed to register '%s' gensec backend!\n",
399 gensec_sasl_security_ops
.name
));
403 sasl_mechs
= sasl_global_listmech();
404 for (i
= 0; sasl_mechs
&& sasl_mechs
[i
]; i
++) {
405 const struct gensec_security_ops
*oldmech
;
406 struct gensec_security_ops
*newmech
;
407 oldmech
= gensec_security_by_sasl_name(NULL
, sasl_mechs
[i
]);
411 newmech
= talloc(talloc_autofree_context(), struct gensec_security_ops
);
413 return NT_STATUS_NO_MEMORY
;
415 *newmech
= gensec_sasl_security_ops
;
416 newmech
->sasl_name
= talloc_strdup(newmech
, sasl_mechs
[i
]);
417 newmech
->name
= talloc_asprintf(newmech
, "sasl-%s", sasl_mechs
[i
]);
418 if (!newmech
->sasl_name
|| !newmech
->name
) {
419 return NT_STATUS_NO_MEMORY
;
422 ret
= gensec_register(newmech
);
423 if (!NT_STATUS_IS_OK(ret
)) {
424 DEBUG(0,("Failed to register '%s' gensec backend!\n",
425 gensec_sasl_security_ops
.name
));