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 "auth/gensec/gensec_toplevel_proto.h"
28 #include <sasl/sasl.h>
30 NTSTATUS
gensec_sasl_init(void);
32 struct gensec_sasl_state
{
38 static NTSTATUS
sasl_nt_status(int sasl_ret
)
42 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
44 return NT_STATUS_NO_MEMORY
;
47 return NT_STATUS_INVALID_PARAMETER
;
49 return NT_STATUS_ACCESS_DENIED
;
53 return NT_STATUS_UNSUCCESSFUL
;
57 static int gensec_sasl_get_user(void *context
, int id
,
58 const char **result
, unsigned *len
)
60 struct gensec_security
*gensec_security
= talloc_get_type(context
, struct gensec_security
);
61 const char *username
= cli_credentials_get_username(gensec_get_credentials(gensec_security
));
62 if (id
!= SASL_CB_USER
&& id
!= SASL_CB_AUTHNAME
) {
70 static int gensec_sasl_get_realm(void *context
, int id
,
71 const char **availrealms
,
74 struct gensec_security
*gensec_security
= talloc_get_type(context
, struct gensec_security
);
75 const char *realm
= cli_credentials_get_realm(gensec_get_credentials(gensec_security
));
77 if (id
!= SASL_CB_GETREALM
) {
81 for (i
=0; availrealms
&& availrealms
[i
]; i
++) {
82 if (strcasecmp_m(realm
, availrealms
[i
]) == 0) {
83 result
[i
] = availrealms
[i
];
87 /* None of the realms match, so lets not specify one */
92 static int gensec_sasl_get_password(sasl_conn_t
*conn
, void *context
, int id
,
93 sasl_secret_t
**psecret
)
95 struct gensec_security
*gensec_security
= talloc_get_type(context
, struct gensec_security
);
96 const char *password
= cli_credentials_get_password(gensec_get_credentials(gensec_security
));
98 sasl_secret_t
*secret
;
103 secret
= talloc_size(gensec_security
, sizeof(sasl_secret_t
)+strlen(password
)+1);
107 secret
->len
= strlen(password
);
108 strlcpy((char*)secret
->data
, password
, secret
->len
+1);
113 static int gensec_sasl_dispose(struct gensec_sasl_state
*gensec_sasl_state
)
115 sasl_dispose(&gensec_sasl_state
->conn
);
119 static NTSTATUS
gensec_sasl_client_start(struct gensec_security
*gensec_security
)
121 struct gensec_sasl_state
*gensec_sasl_state
;
122 const char *service
= gensec_get_target_service(gensec_security
);
123 const char *target_name
= gensec_get_target_hostname(gensec_security
);
124 const struct tsocket_address
*tlocal_addr
= gensec_get_local_address(gensec_security
);
125 const struct tsocket_address
*tremote_addr
= gensec_get_remote_address(gensec_security
);
126 char *local_addr
= NULL
;
127 char *remote_addr
= NULL
;
130 sasl_callback_t
*callbacks
;
132 gensec_sasl_state
= talloc_zero(gensec_security
, struct gensec_sasl_state
);
133 if (!gensec_sasl_state
) {
134 return NT_STATUS_NO_MEMORY
;
137 callbacks
= talloc_array(gensec_sasl_state
, sasl_callback_t
, 5);
138 callbacks
[0].id
= SASL_CB_USER
;
139 callbacks
[0].proc
= gensec_sasl_get_user
;
140 callbacks
[0].context
= gensec_security
;
142 callbacks
[1].id
= SASL_CB_AUTHNAME
;
143 callbacks
[1].proc
= gensec_sasl_get_user
;
144 callbacks
[1].context
= gensec_security
;
146 callbacks
[2].id
= SASL_CB_GETREALM
;
147 callbacks
[2].proc
= gensec_sasl_get_realm
;
148 callbacks
[2].context
= gensec_security
;
150 callbacks
[3].id
= SASL_CB_PASS
;
151 callbacks
[3].proc
= gensec_sasl_get_password
;
152 callbacks
[3].context
= gensec_security
;
154 callbacks
[4].id
= SASL_CB_LIST_END
;
155 callbacks
[4].proc
= NULL
;
156 callbacks
[4].context
= NULL
;
158 gensec_security
->private_data
= gensec_sasl_state
;
161 local_addr
= talloc_asprintf(gensec_sasl_state
,
163 tsocket_address_inet_addr_string(tlocal_addr
, gensec_sasl_state
),
164 tsocket_address_inet_port(tlocal_addr
));
168 remote_addr
= talloc_asprintf(gensec_sasl_state
,
170 tsocket_address_inet_addr_string(tremote_addr
, gensec_sasl_state
),
171 tsocket_address_inet_port(tremote_addr
));
173 gensec_sasl_state
->step
= 0;
175 sasl_ret
= sasl_client_new(service
,
177 local_addr
, remote_addr
, callbacks
, 0,
178 &gensec_sasl_state
->conn
);
180 if (sasl_ret
== SASL_OK
) {
181 sasl_security_properties_t props
;
182 talloc_set_destructor(gensec_sasl_state
, gensec_sasl_dispose
);
185 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
188 props
.maxbufsize
= 65536;
189 gensec_sasl_state
->wrap
= true;
191 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
193 props
.max_ssf
= UINT_MAX
;
194 props
.maxbufsize
= 65536;
195 gensec_sasl_state
->wrap
= true;
198 sasl_ret
= sasl_setprop(gensec_sasl_state
->conn
, SASL_SEC_PROPS
, &props
);
200 if (sasl_ret
!= SASL_OK
) {
201 DEBUG(1, ("GENSEC SASL: client_new failed: %s\n", sasl_errdetail(gensec_sasl_state
->conn
)));
203 return sasl_nt_status(sasl_ret
);
206 static NTSTATUS
gensec_sasl_update(struct gensec_security
*gensec_security
,
207 TALLOC_CTX
*out_mem_ctx
,
208 const DATA_BLOB in
, DATA_BLOB
*out
)
210 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
211 struct gensec_sasl_state
);
213 const char *out_data
;
214 unsigned int out_len
;
216 if (gensec_sasl_state
->step
== 0) {
218 sasl_ret
= sasl_client_start(gensec_sasl_state
->conn
, gensec_security
->ops
->sasl_name
,
219 NULL
, &out_data
, &out_len
, &mech
);
221 sasl_ret
= sasl_client_step(gensec_sasl_state
->conn
,
222 (char*)in
.data
, in
.length
, NULL
,
223 &out_data
, &out_len
);
225 if (sasl_ret
== SASL_OK
|| sasl_ret
== SASL_CONTINUE
) {
226 *out
= data_blob_talloc(out_mem_ctx
, out_data
, out_len
);
228 DEBUG(1, ("GENSEC SASL: step %d update failed: %s\n", gensec_sasl_state
->step
,
229 sasl_errdetail(gensec_sasl_state
->conn
)));
231 gensec_sasl_state
->step
++;
232 return sasl_nt_status(sasl_ret
);
235 static NTSTATUS
gensec_sasl_unwrap_packets(struct gensec_security
*gensec_security
,
236 TALLOC_CTX
*out_mem_ctx
,
239 size_t *len_processed
)
241 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
242 struct gensec_sasl_state
);
243 const char *out_data
;
244 unsigned int out_len
;
246 int sasl_ret
= sasl_decode(gensec_sasl_state
->conn
,
247 (char*)in
->data
, in
->length
, &out_data
,
249 if (sasl_ret
== SASL_OK
) {
250 *out
= data_blob_talloc(out_mem_ctx
, out_data
, out_len
);
251 *len_processed
= in
->length
;
253 DEBUG(1, ("GENSEC SASL: unwrap failed: %s\n", sasl_errdetail(gensec_sasl_state
->conn
)));
255 return sasl_nt_status(sasl_ret
);
259 static NTSTATUS
gensec_sasl_wrap_packets(struct gensec_security
*gensec_security
,
260 TALLOC_CTX
*out_mem_ctx
,
263 size_t *len_processed
)
265 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
266 struct gensec_sasl_state
);
267 const char *out_data
;
268 unsigned int out_len
;
269 unsigned len_permitted
;
270 int sasl_ret
= sasl_getprop(gensec_sasl_state
->conn
, SASL_SSF
,
271 (const void**)&len_permitted
);
272 if (sasl_ret
!= SASL_OK
) {
273 return sasl_nt_status(sasl_ret
);
275 len_permitted
= MIN(len_permitted
, in
->length
);
277 sasl_ret
= sasl_encode(gensec_sasl_state
->conn
,
278 (char*)in
->data
, len_permitted
, &out_data
,
280 if (sasl_ret
== SASL_OK
) {
281 *out
= data_blob_talloc(out_mem_ctx
, out_data
, out_len
);
282 *len_processed
= in
->length
;
284 DEBUG(1, ("GENSEC SASL: wrap failed: %s\n", sasl_errdetail(gensec_sasl_state
->conn
)));
286 return sasl_nt_status(sasl_ret
);
289 /* Try to figure out what features we actually got on the connection */
290 static bool gensec_sasl_have_feature(struct gensec_security
*gensec_security
,
293 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
294 struct gensec_sasl_state
);
298 /* If we did not elect to wrap, then we have neither sign nor seal, no matter what the SSF claims */
299 if (!gensec_sasl_state
->wrap
) {
303 sasl_ret
= sasl_getprop(gensec_sasl_state
->conn
, SASL_SSF
,
305 if (sasl_ret
!= SASL_OK
) {
308 if (feature
& GENSEC_FEATURE_SIGN
) {
316 if (feature
& GENSEC_FEATURE_SEAL
) {
327 /* This could in theory work with any SASL mech */
328 static const struct gensec_security_ops gensec_sasl_security_ops
= {
329 .name
= "sasl-DIGEST-MD5",
330 .sasl_name
= "DIGEST-MD5",
331 .client_start
= gensec_sasl_client_start
,
332 .update
= gensec_sasl_update
,
333 .wrap_packets
= gensec_sasl_wrap_packets
,
334 .unwrap_packets
= gensec_sasl_unwrap_packets
,
335 .have_feature
= gensec_sasl_have_feature
,
337 .priority
= GENSEC_SASL
340 static int gensec_sasl_log(void *context
,
345 switch (sasl_log_level
) {
376 DEBUG(dl
, ("gensec_sasl: %s\n", message
));
381 NTSTATUS
gensec_sasl_init(void)
387 const char **sasl_mechs
;
390 static const sasl_callback_t callbacks
[] = {
393 .proc
= gensec_sasl_log
,
397 .id
= SASL_CB_LIST_END
,
398 .proc
= gensec_sasl_log
,
402 sasl_ret
= sasl_client_init(callbacks
);
404 if (sasl_ret
== SASL_NOMECH
) {
405 /* Nothing to do here */
409 if (sasl_ret
!= SASL_OK
) {
410 return sasl_nt_status(sasl_ret
);
413 /* For now, we just register DIGEST-MD5 */
415 ret
= gensec_register(&gensec_sasl_security_ops
);
416 if (!NT_STATUS_IS_OK(ret
)) {
417 DEBUG(0,("Failed to register '%s' gensec backend!\n",
418 gensec_sasl_security_ops
.name
));
422 sasl_mechs
= sasl_global_listmech();
423 for (i
= 0; sasl_mechs
&& sasl_mechs
[i
]; i
++) {
424 const struct gensec_security_ops
*oldmech
;
425 struct gensec_security_ops
*newmech
;
426 oldmech
= gensec_security_by_sasl_name(NULL
, sasl_mechs
[i
]);
430 newmech
= talloc(talloc_autofree_context(), struct gensec_security_ops
);
432 return NT_STATUS_NO_MEMORY
;
434 *newmech
= gensec_sasl_security_ops
;
435 newmech
->sasl_name
= talloc_strdup(newmech
, sasl_mechs
[i
]);
436 newmech
->name
= talloc_asprintf(newmech
, "sasl-%s", sasl_mechs
[i
]);
437 if (!newmech
->sasl_name
|| !newmech
->name
) {
438 return NT_STATUS_NO_MEMORY
;
441 ret
= gensec_register(newmech
);
442 if (!NT_STATUS_IS_OK(ret
)) {
443 DEBUG(0,("Failed to register '%s' gensec backend!\n",
444 gensec_sasl_security_ops
.name
));