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
{
35 static NTSTATUS
sasl_nt_status(int sasl_ret
)
39 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
41 return NT_STATUS_NO_MEMORY
;
44 return NT_STATUS_INVALID_PARAMETER
;
46 return NT_STATUS_ACCESS_DENIED
;
50 return NT_STATUS_UNSUCCESSFUL
;
54 static int gensec_sasl_get_user(void *context
, int id
,
55 const char **result
, unsigned *len
)
57 struct gensec_security
*gensec_security
= talloc_get_type(context
, struct gensec_security
);
58 const char *username
= cli_credentials_get_username(gensec_get_credentials(gensec_security
));
59 if (id
!= SASL_CB_USER
&& id
!= SASL_CB_AUTHNAME
) {
67 static int gensec_sasl_get_realm(void *context
, int id
,
68 const char **availrealms
,
71 struct gensec_security
*gensec_security
= talloc_get_type(context
, struct gensec_security
);
72 const char *realm
= cli_credentials_get_realm(gensec_get_credentials(gensec_security
));
74 if (id
!= SASL_CB_GETREALM
) {
78 for (i
=0; availrealms
&& availrealms
[i
]; i
++) {
79 if (strcasecmp_m(realm
, availrealms
[i
]) == 0) {
80 result
[i
] = availrealms
[i
];
84 /* None of the realms match, so lets not specify one */
89 static int gensec_sasl_get_password(sasl_conn_t
*conn
, void *context
, int id
,
90 sasl_secret_t
**psecret
)
92 struct gensec_security
*gensec_security
= talloc_get_type(context
, struct gensec_security
);
93 const char *password
= cli_credentials_get_password(gensec_get_credentials(gensec_security
));
95 sasl_secret_t
*secret
;
100 secret
= talloc_size(gensec_security
, sizeof(sasl_secret_t
)+strlen(password
));
104 secret
->len
= strlen(password
);
105 safe_strcpy((char*)secret
->data
, password
, secret
->len
+1);
110 static int gensec_sasl_dispose(struct gensec_sasl_state
*gensec_sasl_state
)
112 sasl_dispose(&gensec_sasl_state
->conn
);
116 static NTSTATUS
gensec_sasl_client_start(struct gensec_security
*gensec_security
)
118 struct gensec_sasl_state
*gensec_sasl_state
;
119 const char *service
= gensec_get_target_service(gensec_security
);
120 const char *target_name
= gensec_get_target_hostname(gensec_security
);
121 const struct tsocket_address
*tlocal_addr
= gensec_get_local_address(gensec_security
);
122 const struct tsocket_address
*tremote_addr
= gensec_get_remote_address(gensec_security
);
123 char *local_addr
= NULL
;
124 char *remote_addr
= NULL
;
127 sasl_callback_t
*callbacks
;
129 gensec_sasl_state
= talloc_zero(gensec_security
, struct gensec_sasl_state
);
130 if (!gensec_sasl_state
) {
131 return NT_STATUS_NO_MEMORY
;
134 callbacks
= talloc_array(gensec_sasl_state
, sasl_callback_t
, 5);
135 callbacks
[0].id
= SASL_CB_USER
;
136 callbacks
[0].proc
= gensec_sasl_get_user
;
137 callbacks
[0].context
= gensec_security
;
139 callbacks
[1].id
= SASL_CB_AUTHNAME
;
140 callbacks
[1].proc
= gensec_sasl_get_user
;
141 callbacks
[1].context
= gensec_security
;
143 callbacks
[2].id
= SASL_CB_GETREALM
;
144 callbacks
[2].proc
= gensec_sasl_get_realm
;
145 callbacks
[2].context
= gensec_security
;
147 callbacks
[3].id
= SASL_CB_PASS
;
148 callbacks
[3].proc
= gensec_sasl_get_password
;
149 callbacks
[3].context
= gensec_security
;
151 callbacks
[4].id
= SASL_CB_LIST_END
;
152 callbacks
[4].proc
= NULL
;
153 callbacks
[4].context
= NULL
;
155 gensec_security
->private_data
= gensec_sasl_state
;
158 local_addr
= talloc_asprintf(gensec_sasl_state
,
160 tsocket_address_inet_addr_string(tlocal_addr
, gensec_sasl_state
),
161 tsocket_address_inet_port(tlocal_addr
));
165 remote_addr
= talloc_asprintf(gensec_sasl_state
,
167 tsocket_address_inet_addr_string(tremote_addr
, gensec_sasl_state
),
168 tsocket_address_inet_port(tremote_addr
));
170 gensec_sasl_state
->step
= 0;
172 sasl_ret
= sasl_client_new(service
,
174 local_addr
, remote_addr
, callbacks
, 0,
175 &gensec_sasl_state
->conn
);
177 if (sasl_ret
== SASL_OK
) {
178 sasl_security_properties_t props
;
179 talloc_set_destructor(gensec_sasl_state
, gensec_sasl_dispose
);
182 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
185 props
.maxbufsize
= 65536;
186 gensec_sasl_state
->wrap
= true;
188 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
190 props
.max_ssf
= UINT_MAX
;
191 props
.maxbufsize
= 65536;
192 gensec_sasl_state
->wrap
= true;
195 sasl_ret
= sasl_setprop(gensec_sasl_state
->conn
, SASL_SEC_PROPS
, &props
);
197 if (sasl_ret
!= SASL_OK
) {
198 DEBUG(1, ("GENSEC SASL: client_new failed: %s\n", sasl_errdetail(gensec_sasl_state
->conn
)));
200 return sasl_nt_status(sasl_ret
);
203 static NTSTATUS
gensec_sasl_update(struct gensec_security
*gensec_security
,
204 TALLOC_CTX
*out_mem_ctx
,
205 const DATA_BLOB in
, DATA_BLOB
*out
)
207 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
208 struct gensec_sasl_state
);
210 const char *out_data
;
211 unsigned int out_len
;
213 if (gensec_sasl_state
->step
== 0) {
215 sasl_ret
= sasl_client_start(gensec_sasl_state
->conn
, gensec_security
->ops
->sasl_name
,
216 NULL
, &out_data
, &out_len
, &mech
);
218 sasl_ret
= sasl_client_step(gensec_sasl_state
->conn
,
219 (char*)in
.data
, in
.length
, NULL
,
220 &out_data
, &out_len
);
222 if (sasl_ret
== SASL_OK
|| sasl_ret
== SASL_CONTINUE
) {
223 *out
= data_blob_talloc(out_mem_ctx
, out_data
, out_len
);
225 DEBUG(1, ("GENSEC SASL: step %d update failed: %s\n", gensec_sasl_state
->step
,
226 sasl_errdetail(gensec_sasl_state
->conn
)));
228 gensec_sasl_state
->step
++;
229 return sasl_nt_status(sasl_ret
);
232 static NTSTATUS
gensec_sasl_unwrap_packets(struct gensec_security
*gensec_security
,
233 TALLOC_CTX
*out_mem_ctx
,
236 size_t *len_processed
)
238 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
239 struct gensec_sasl_state
);
240 const char *out_data
;
241 unsigned int out_len
;
243 int sasl_ret
= sasl_decode(gensec_sasl_state
->conn
,
244 (char*)in
->data
, in
->length
, &out_data
,
246 if (sasl_ret
== SASL_OK
) {
247 *out
= data_blob_talloc(out_mem_ctx
, out_data
, out_len
);
248 *len_processed
= in
->length
;
250 DEBUG(1, ("GENSEC SASL: unwrap failed: %s\n", sasl_errdetail(gensec_sasl_state
->conn
)));
252 return sasl_nt_status(sasl_ret
);
256 static NTSTATUS
gensec_sasl_wrap_packets(struct gensec_security
*gensec_security
,
257 TALLOC_CTX
*out_mem_ctx
,
260 size_t *len_processed
)
262 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
263 struct gensec_sasl_state
);
264 const char *out_data
;
265 unsigned int out_len
;
266 unsigned len_permitted
;
267 int sasl_ret
= sasl_getprop(gensec_sasl_state
->conn
, SASL_SSF
,
268 (const void**)&len_permitted
);
269 if (sasl_ret
!= SASL_OK
) {
270 return sasl_nt_status(sasl_ret
);
272 len_permitted
= MIN(len_permitted
, in
->length
);
274 sasl_ret
= sasl_encode(gensec_sasl_state
->conn
,
275 (char*)in
->data
, len_permitted
, &out_data
,
277 if (sasl_ret
== SASL_OK
) {
278 *out
= data_blob_talloc(out_mem_ctx
, out_data
, out_len
);
279 *len_processed
= in
->length
;
281 DEBUG(1, ("GENSEC SASL: wrap failed: %s\n", sasl_errdetail(gensec_sasl_state
->conn
)));
283 return sasl_nt_status(sasl_ret
);
286 /* Try to figure out what features we actually got on the connection */
287 static bool gensec_sasl_have_feature(struct gensec_security
*gensec_security
,
290 struct gensec_sasl_state
*gensec_sasl_state
= talloc_get_type(gensec_security
->private_data
,
291 struct gensec_sasl_state
);
295 /* If we did not elect to wrap, then we have neither sign nor seal, no matter what the SSF claims */
296 if (!gensec_sasl_state
->wrap
) {
300 sasl_ret
= sasl_getprop(gensec_sasl_state
->conn
, SASL_SSF
,
302 if (sasl_ret
!= SASL_OK
) {
305 if (feature
& GENSEC_FEATURE_SIGN
) {
313 if (feature
& GENSEC_FEATURE_SEAL
) {
324 /* This could in theory work with any SASL mech */
325 static const struct gensec_security_ops gensec_sasl_security_ops
= {
326 .name
= "sasl-DIGEST-MD5",
327 .sasl_name
= "DIGEST-MD5",
328 .client_start
= gensec_sasl_client_start
,
329 .update
= gensec_sasl_update
,
330 .wrap_packets
= gensec_sasl_wrap_packets
,
331 .unwrap_packets
= gensec_sasl_unwrap_packets
,
332 .have_feature
= gensec_sasl_have_feature
,
334 .priority
= GENSEC_SASL
337 static int gensec_sasl_log(void *context
,
342 switch (sasl_log_level
) {
373 DEBUG(dl
, ("gensec_sasl: %s\n", message
));
378 NTSTATUS
gensec_sasl_init(void)
384 const char **sasl_mechs
;
387 static const sasl_callback_t callbacks
[] = {
390 .proc
= gensec_sasl_log
,
394 .id
= SASL_CB_LIST_END
,
395 .proc
= gensec_sasl_log
,
399 sasl_ret
= sasl_client_init(callbacks
);
401 if (sasl_ret
== SASL_NOMECH
) {
402 /* Nothing to do here */
406 if (sasl_ret
!= SASL_OK
) {
407 return sasl_nt_status(sasl_ret
);
410 /* For now, we just register DIGEST-MD5 */
412 ret
= gensec_register(&gensec_sasl_security_ops
);
413 if (!NT_STATUS_IS_OK(ret
)) {
414 DEBUG(0,("Failed to register '%s' gensec backend!\n",
415 gensec_sasl_security_ops
.name
));
419 sasl_mechs
= sasl_global_listmech();
420 for (i
= 0; sasl_mechs
&& sasl_mechs
[i
]; i
++) {
421 const struct gensec_security_ops
*oldmech
;
422 struct gensec_security_ops
*newmech
;
423 oldmech
= gensec_security_by_sasl_name(NULL
, sasl_mechs
[i
]);
427 newmech
= talloc(talloc_autofree_context(), struct gensec_security_ops
);
429 return NT_STATUS_NO_MEMORY
;
431 *newmech
= gensec_sasl_security_ops
;
432 newmech
->sasl_name
= talloc_strdup(newmech
, sasl_mechs
[i
]);
433 newmech
->name
= talloc_asprintf(newmech
, "sasl-%s", sasl_mechs
[i
]);
434 if (!newmech
->sasl_name
|| !newmech
->name
) {
435 return NT_STATUS_NO_MEMORY
;
438 ret
= gensec_register(newmech
);
439 if (!NT_STATUS_IS_OK(ret
)) {
440 DEBUG(0,("Failed to register '%s' gensec backend!\n",
441 gensec_sasl_security_ops
.name
));