2 Unix SMB/CIFS implementation.
4 Generic Authentication Interface
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/network.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "lib/tsocket/tsocket.h"
28 #include "librpc/rpc/dcerpc.h"
29 #include "auth/credentials/credentials.h"
30 #include "auth/gensec/gensec.h"
31 #include "param/param.h"
33 /* the list of currently registered GENSEC backends */
34 static struct gensec_security_ops **generic_security_ops;
35 static int gensec_num_backends;
37 /* Return all the registered mechs. Don't modify the return pointer,
38 * but you may talloc_reference it if convient */
39 _PUBLIC_ struct gensec_security_ops **gensec_security_all(void)
41 return generic_security_ops;
44 bool gensec_security_ops_enabled(struct gensec_security_ops *ops, struct gensec_security *security)
46 return lp_parm_bool(security->settings->lp_ctx, NULL, "gensec", ops->name, ops->enabled);
49 /* Sometimes we want to force only kerberos, sometimes we want to
50 * force it's avoidance. The old list could be either
51 * gensec_security_all(), or from cli_credentials_gensec_list() (ie,
52 * an existing list we have trimmed down) */
54 _PUBLIC_ struct gensec_security_ops **gensec_use_kerberos_mechs(TALLOC_CTX *mem_ctx,
55 struct gensec_security_ops **old_gensec_list,
56 struct cli_credentials *creds)
58 struct gensec_security_ops **new_gensec_list;
59 int i, j, num_mechs_in;
60 enum credentials_use_kerberos use_kerberos = CRED_AUTO_USE_KERBEROS;
63 use_kerberos = cli_credentials_get_kerberos_state(creds);
66 if (use_kerberos == CRED_AUTO_USE_KERBEROS) {
67 if (!talloc_reference(mem_ctx, old_gensec_list)) {
70 return old_gensec_list;
73 for (num_mechs_in=0; old_gensec_list && old_gensec_list[num_mechs_in]; num_mechs_in++) {
77 new_gensec_list = talloc_array(mem_ctx, struct gensec_security_ops *, num_mechs_in + 1);
78 if (!new_gensec_list) {
83 for (i=0; old_gensec_list && old_gensec_list[i]; i++) {
86 for (oid_idx = 0; old_gensec_list[i]->oid && old_gensec_list[i]->oid[oid_idx]; oid_idx++) {
87 if (strcmp(old_gensec_list[i]->oid[oid_idx], GENSEC_OID_SPNEGO) == 0) {
88 new_gensec_list[j] = old_gensec_list[i];
93 switch (use_kerberos) {
94 case CRED_DONT_USE_KERBEROS:
95 if (old_gensec_list[i]->kerberos == false) {
96 new_gensec_list[j] = old_gensec_list[i];
100 case CRED_MUST_USE_KERBEROS:
101 if (old_gensec_list[i]->kerberos == true) {
102 new_gensec_list[j] = old_gensec_list[i];
107 /* Can't happen or invalid parameter */
111 new_gensec_list[j] = NULL;
113 return new_gensec_list;
116 struct gensec_security_ops **gensec_security_mechs(struct gensec_security *gensec_security,
119 struct gensec_security_ops **backends;
120 backends = gensec_security_all();
121 if (!gensec_security) {
122 if (!talloc_reference(mem_ctx, backends)) {
127 struct cli_credentials *creds = gensec_get_credentials(gensec_security);
129 if (!talloc_reference(mem_ctx, backends)) {
134 return gensec_use_kerberos_mechs(mem_ctx, backends, creds);
138 static const struct gensec_security_ops *gensec_security_by_authtype(struct gensec_security *gensec_security,
142 struct gensec_security_ops **backends;
143 const struct gensec_security_ops *backend;
144 TALLOC_CTX *mem_ctx = talloc_new(gensec_security);
148 backends = gensec_security_mechs(gensec_security, mem_ctx);
149 for (i=0; backends && backends[i]; i++) {
150 if (!gensec_security_ops_enabled(backends[i], gensec_security))
152 if (backends[i]->auth_type == auth_type) {
153 backend = backends[i];
154 talloc_free(mem_ctx);
158 talloc_free(mem_ctx);
163 const struct gensec_security_ops *gensec_security_by_oid(struct gensec_security *gensec_security,
164 const char *oid_string)
167 struct gensec_security_ops **backends;
168 const struct gensec_security_ops *backend;
169 TALLOC_CTX *mem_ctx = talloc_new(gensec_security);
173 backends = gensec_security_mechs(gensec_security, mem_ctx);
174 for (i=0; backends && backends[i]; i++) {
175 if (gensec_security != NULL &&
176 !gensec_security_ops_enabled(backends[i],
179 if (backends[i]->oid) {
180 for (j=0; backends[i]->oid[j]; j++) {
181 if (backends[i]->oid[j] &&
182 (strcmp(backends[i]->oid[j], oid_string) == 0)) {
183 backend = backends[i];
184 talloc_free(mem_ctx);
190 talloc_free(mem_ctx);
195 const struct gensec_security_ops *gensec_security_by_sasl_name(struct gensec_security *gensec_security,
196 const char *sasl_name)
199 struct gensec_security_ops **backends;
200 const struct gensec_security_ops *backend;
201 TALLOC_CTX *mem_ctx = talloc_new(gensec_security);
205 backends = gensec_security_mechs(gensec_security, mem_ctx);
206 for (i=0; backends && backends[i]; i++) {
207 if (!gensec_security_ops_enabled(backends[i], gensec_security))
209 if (backends[i]->sasl_name
210 && (strcmp(backends[i]->sasl_name, sasl_name) == 0)) {
211 backend = backends[i];
212 talloc_free(mem_ctx);
216 talloc_free(mem_ctx);
221 static const struct gensec_security_ops *gensec_security_by_name(struct gensec_security *gensec_security,
225 struct gensec_security_ops **backends;
226 const struct gensec_security_ops *backend;
227 TALLOC_CTX *mem_ctx = talloc_new(gensec_security);
231 backends = gensec_security_mechs(gensec_security, mem_ctx);
232 for (i=0; backends && backends[i]; i++) {
233 if (gensec_security != NULL &&
234 !gensec_security_ops_enabled(backends[i], gensec_security))
236 if (backends[i]->name
237 && (strcmp(backends[i]->name, name) == 0)) {
238 backend = backends[i];
239 talloc_free(mem_ctx);
243 talloc_free(mem_ctx);
248 * Return a unique list of security subsystems from those specified in
249 * the list of SASL names.
251 * Use the list of enabled GENSEC mechanisms from the credentials
252 * attached to the gensec_security, and return in our preferred order.
255 const struct gensec_security_ops **gensec_security_by_sasl_list(struct gensec_security *gensec_security,
257 const char **sasl_names)
259 const struct gensec_security_ops **backends_out;
260 struct gensec_security_ops **backends;
262 int num_backends_out = 0;
268 backends = gensec_security_mechs(gensec_security, mem_ctx);
270 backends_out = talloc_array(mem_ctx, const struct gensec_security_ops *, 1);
274 backends_out[0] = NULL;
276 /* Find backends in our preferred order, by walking our list,
277 * then looking in the supplied list */
278 for (i=0; backends && backends[i]; i++) {
279 if (gensec_security != NULL &&
280 !gensec_security_ops_enabled(backends[i], gensec_security))
282 for (sasl_idx = 0; sasl_names[sasl_idx]; sasl_idx++) {
283 if (!backends[i]->sasl_name ||
284 !(strcmp(backends[i]->sasl_name,
285 sasl_names[sasl_idx]) == 0)) {
289 for (k=0; backends_out[k]; k++) {
290 if (backends_out[k] == backends[i]) {
295 if (k < num_backends_out) {
296 /* already in there */
300 backends_out = talloc_realloc(mem_ctx, backends_out,
301 const struct gensec_security_ops *,
302 num_backends_out + 2);
307 backends_out[num_backends_out] = backends[i];
309 backends_out[num_backends_out] = NULL;
316 * Return a unique list of security subsystems from those specified in
317 * the OID list. That is, where two OIDs refer to the same module,
318 * return that module only once.
320 * Use the list of enabled GENSEC mechanisms from the credentials
321 * attached to the gensec_security, and return in our preferred order.
324 const struct gensec_security_ops_wrapper *gensec_security_by_oid_list(struct gensec_security *gensec_security,
326 const char **oid_strings,
329 struct gensec_security_ops_wrapper *backends_out;
330 struct gensec_security_ops **backends;
331 int i, j, k, oid_idx;
332 int num_backends_out = 0;
338 backends = gensec_security_mechs(gensec_security, gensec_security);
340 backends_out = talloc_array(mem_ctx, struct gensec_security_ops_wrapper, 1);
344 backends_out[0].op = NULL;
345 backends_out[0].oid = NULL;
347 /* Find backends in our preferred order, by walking our list,
348 * then looking in the supplied list */
349 for (i=0; backends && backends[i]; i++) {
350 if (gensec_security != NULL &&
351 !gensec_security_ops_enabled(backends[i], gensec_security))
353 if (!backends[i]->oid) {
356 for (oid_idx = 0; oid_strings[oid_idx]; oid_idx++) {
357 if (strcmp(oid_strings[oid_idx], skip) == 0) {
361 for (j=0; backends[i]->oid[j]; j++) {
362 if (!backends[i]->oid[j] ||
363 !(strcmp(backends[i]->oid[j],
364 oid_strings[oid_idx]) == 0)) {
368 for (k=0; backends_out[k].op; k++) {
369 if (backends_out[k].op == backends[i]) {
374 if (k < num_backends_out) {
375 /* already in there */
379 backends_out = talloc_realloc(mem_ctx, backends_out,
380 struct gensec_security_ops_wrapper,
381 num_backends_out + 2);
386 backends_out[num_backends_out].op = backends[i];
387 backends_out[num_backends_out].oid = backends[i]->oid[j];
389 backends_out[num_backends_out].op = NULL;
390 backends_out[num_backends_out].oid = NULL;
398 * Return OIDS from the security subsystems listed
401 const char **gensec_security_oids_from_ops(struct gensec_security *gensec_security,
403 struct gensec_security_ops **ops,
409 const char **oid_list;
413 oid_list = talloc_array(mem_ctx, const char *, 1);
418 for (i=0; ops && ops[i]; i++) {
419 if (gensec_security != NULL &&
420 !gensec_security_ops_enabled(ops[i], gensec_security)) {
427 for (k = 0; ops[i]->oid[k]; k++) {
428 if (skip && strcmp(skip, ops[i]->oid[k])==0) {
430 oid_list = talloc_realloc(mem_ctx, oid_list, const char *, j + 2);
434 oid_list[j] = ops[i]->oid[k];
445 * Return OIDS from the security subsystems listed
448 const char **gensec_security_oids_from_ops_wrapped(TALLOC_CTX *mem_ctx,
449 const struct gensec_security_ops_wrapper *wops)
454 const char **oid_list;
458 oid_list = talloc_array(mem_ctx, const char *, 1);
463 for (i=0; wops[i].op; i++) {
464 if (!wops[i].op->oid) {
468 for (k = 0; wops[i].op->oid[k]; k++) {
469 oid_list = talloc_realloc(mem_ctx, oid_list, const char *, j + 2);
473 oid_list[j] = wops[i].op->oid[k];
483 * Return all the security subsystems currently enabled on a GENSEC context.
485 * This is taken from a list attached to the cli_credentials, and
486 * skips the OID in 'skip'. (Typically the SPNEGO OID)
490 const char **gensec_security_oids(struct gensec_security *gensec_security,
494 struct gensec_security_ops **ops
495 = gensec_security_mechs(gensec_security, mem_ctx);
496 return gensec_security_oids_from_ops(gensec_security, mem_ctx, ops, skip);
502 Start the GENSEC system, returning a context pointer.
503 @param mem_ctx The parent TALLOC memory context.
504 @param gensec_security Returned GENSEC context pointer.
505 @note The mem_ctx is only a parent and may be NULL.
506 @note, the auth context is moved to be a child of the
507 @ gensec_security return
509 static NTSTATUS gensec_start(TALLOC_CTX *mem_ctx,
510 struct tevent_context *ev,
511 struct gensec_settings *settings,
512 struct auth_context *auth_context,
513 struct gensec_security **gensec_security)
516 DEBUG(0, ("No event context available!\n"));
517 return NT_STATUS_INTERNAL_ERROR;
520 (*gensec_security) = talloc(mem_ctx, struct gensec_security);
521 NT_STATUS_HAVE_NO_MEMORY(*gensec_security);
523 (*gensec_security)->ops = NULL;
524 (*gensec_security)->local_addr = NULL;
525 (*gensec_security)->remote_addr = NULL;
526 (*gensec_security)->private_data = NULL;
528 ZERO_STRUCT((*gensec_security)->target);
530 (*gensec_security)->subcontext = false;
531 (*gensec_security)->want_features = 0;
533 (*gensec_security)->event_ctx = ev;
534 SMB_ASSERT(settings->lp_ctx != NULL);
535 (*gensec_security)->settings = talloc_reference(*gensec_security, settings);
536 (*gensec_security)->auth_context = talloc_steal(*gensec_security, auth_context);
542 * Start a GENSEC subcontext, with a copy of the properties of the parent
543 * @param mem_ctx The parent TALLOC memory context.
544 * @param parent The parent GENSEC context
545 * @param gensec_security Returned GENSEC context pointer.
546 * @note Used by SPNEGO in particular, for the actual implementation mechanism
549 _PUBLIC_ NTSTATUS gensec_subcontext_start(TALLOC_CTX *mem_ctx,
550 struct gensec_security *parent,
551 struct gensec_security **gensec_security)
553 (*gensec_security) = talloc(mem_ctx, struct gensec_security);
554 NT_STATUS_HAVE_NO_MEMORY(*gensec_security);
556 (**gensec_security) = *parent;
557 (*gensec_security)->ops = NULL;
558 (*gensec_security)->private_data = NULL;
560 (*gensec_security)->subcontext = true;
561 (*gensec_security)->want_features = parent->want_features;
562 (*gensec_security)->event_ctx = parent->event_ctx;
563 (*gensec_security)->auth_context = talloc_reference(*gensec_security, parent->auth_context);
564 (*gensec_security)->settings = talloc_reference(*gensec_security, parent->settings);
565 (*gensec_security)->auth_context = talloc_reference(*gensec_security, parent->auth_context);
571 Start the GENSEC system, in client mode, returning a context pointer.
572 @param mem_ctx The parent TALLOC memory context.
573 @param gensec_security Returned GENSEC context pointer.
574 @note The mem_ctx is only a parent and may be NULL.
576 _PUBLIC_ NTSTATUS gensec_client_start(TALLOC_CTX *mem_ctx,
577 struct gensec_security **gensec_security,
578 struct tevent_context *ev,
579 struct gensec_settings *settings)
583 if (settings == NULL) {
584 DEBUG(0,("gensec_client_start: no settings given!\n"));
585 return NT_STATUS_INTERNAL_ERROR;
588 status = gensec_start(mem_ctx, ev, settings, NULL, gensec_security);
589 if (!NT_STATUS_IS_OK(status)) {
592 (*gensec_security)->gensec_role = GENSEC_CLIENT;
598 Start the GENSEC system, in server mode, returning a context pointer.
599 @param mem_ctx The parent TALLOC memory context.
600 @param gensec_security Returned GENSEC context pointer.
601 @note The mem_ctx is only a parent and may be NULL.
603 _PUBLIC_ NTSTATUS gensec_server_start(TALLOC_CTX *mem_ctx,
604 struct tevent_context *ev,
605 struct gensec_settings *settings,
606 struct auth_context *auth_context,
607 struct gensec_security **gensec_security)
612 DEBUG(0,("gensec_server_start: no event context given!\n"));
613 return NT_STATUS_INTERNAL_ERROR;
617 DEBUG(0,("gensec_server_start: no settings given!\n"));
618 return NT_STATUS_INTERNAL_ERROR;
621 status = gensec_start(mem_ctx, ev, settings, auth_context, gensec_security);
622 if (!NT_STATUS_IS_OK(status)) {
625 (*gensec_security)->gensec_role = GENSEC_SERVER;
630 static NTSTATUS gensec_start_mech(struct gensec_security *gensec_security)
633 DEBUG(5, ("Starting GENSEC %smechanism %s\n",
634 gensec_security->subcontext ? "sub" : "",
635 gensec_security->ops->name));
636 switch (gensec_security->gensec_role) {
638 if (gensec_security->ops->client_start) {
639 status = gensec_security->ops->client_start(gensec_security);
640 if (!NT_STATUS_IS_OK(status)) {
641 DEBUG(2, ("Failed to start GENSEC client mech %s: %s\n",
642 gensec_security->ops->name, nt_errstr(status)));
648 if (gensec_security->ops->server_start) {
649 status = gensec_security->ops->server_start(gensec_security);
650 if (!NT_STATUS_IS_OK(status)) {
651 DEBUG(1, ("Failed to start GENSEC server mech %s: %s\n",
652 gensec_security->ops->name, nt_errstr(status)));
658 return NT_STATUS_INVALID_PARAMETER;
662 * Start a GENSEC sub-mechanism by DCERPC allocated 'auth type' number
663 * @param gensec_security GENSEC context pointer.
664 * @param auth_type DCERPC auth type
665 * @param auth_level DCERPC auth level
668 _PUBLIC_ NTSTATUS gensec_start_mech_by_authtype(struct gensec_security *gensec_security,
669 uint8_t auth_type, uint8_t auth_level)
671 gensec_security->ops = gensec_security_by_authtype(gensec_security, auth_type);
672 if (!gensec_security->ops) {
673 DEBUG(3, ("Could not find GENSEC backend for auth_type=%d\n", (int)auth_type));
674 return NT_STATUS_INVALID_PARAMETER;
676 gensec_want_feature(gensec_security, GENSEC_FEATURE_DCE_STYLE);
677 gensec_want_feature(gensec_security, GENSEC_FEATURE_ASYNC_REPLIES);
678 if (auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
679 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
680 } else if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
681 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
682 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
683 } else if (auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
684 /* Default features */
686 DEBUG(2,("auth_level %d not supported in DCE/RPC authentication\n",
688 return NT_STATUS_INVALID_PARAMETER;
691 return gensec_start_mech(gensec_security);
694 _PUBLIC_ const char *gensec_get_name_by_authtype(struct gensec_security *gensec_security, uint8_t authtype)
696 const struct gensec_security_ops *ops;
697 ops = gensec_security_by_authtype(gensec_security, authtype);
705 _PUBLIC_ const char *gensec_get_name_by_oid(struct gensec_security *gensec_security,
706 const char *oid_string)
708 const struct gensec_security_ops *ops;
709 ops = gensec_security_by_oid(gensec_security, oid_string);
718 * Start a GENSEC sub-mechanism with a specifed mechansim structure, used in SPNEGO
722 NTSTATUS gensec_start_mech_by_ops(struct gensec_security *gensec_security,
723 const struct gensec_security_ops *ops)
725 gensec_security->ops = ops;
726 return gensec_start_mech(gensec_security);
730 * Start a GENSEC sub-mechanism by OID, used in SPNEGO
732 * @note This should also be used when you wish to just start NLTMSSP (for example), as it uses a
733 * well-known #define to hook it in.
736 _PUBLIC_ NTSTATUS gensec_start_mech_by_oid(struct gensec_security *gensec_security,
737 const char *mech_oid)
739 SMB_ASSERT(gensec_security != NULL);
741 gensec_security->ops = gensec_security_by_oid(gensec_security, mech_oid);
742 if (!gensec_security->ops) {
743 DEBUG(3, ("Could not find GENSEC backend for oid=%s\n", mech_oid));
744 return NT_STATUS_INVALID_PARAMETER;
746 return gensec_start_mech(gensec_security);
750 * Start a GENSEC sub-mechanism by a well know SASL name
754 _PUBLIC_ NTSTATUS gensec_start_mech_by_sasl_name(struct gensec_security *gensec_security,
755 const char *sasl_name)
757 gensec_security->ops = gensec_security_by_sasl_name(gensec_security, sasl_name);
758 if (!gensec_security->ops) {
759 DEBUG(3, ("Could not find GENSEC backend for sasl_name=%s\n", sasl_name));
760 return NT_STATUS_INVALID_PARAMETER;
762 return gensec_start_mech(gensec_security);
766 * Start a GENSEC sub-mechanism with the preferred option from a SASL name list
770 _PUBLIC_ NTSTATUS gensec_start_mech_by_sasl_list(struct gensec_security *gensec_security,
771 const char **sasl_names)
773 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
774 TALLOC_CTX *mem_ctx = talloc_new(gensec_security);
775 const struct gensec_security_ops **ops;
778 return NT_STATUS_NO_MEMORY;
780 ops = gensec_security_by_sasl_list(gensec_security, mem_ctx, sasl_names);
782 DEBUG(3, ("Could not find GENSEC backend for any of sasl_name = %s\n",
783 str_list_join(mem_ctx,
785 talloc_free(mem_ctx);
786 return NT_STATUS_INVALID_PARAMETER;
788 for (i=0; ops[i]; i++) {
789 nt_status = gensec_start_mech_by_ops(gensec_security, ops[i]);
790 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
794 talloc_free(mem_ctx);
799 * Start a GENSEC sub-mechanism by an internal name
803 _PUBLIC_ NTSTATUS gensec_start_mech_by_name(struct gensec_security *gensec_security,
806 gensec_security->ops = gensec_security_by_name(gensec_security, name);
807 if (!gensec_security->ops) {
808 DEBUG(3, ("Could not find GENSEC backend for name=%s\n", name));
809 return NT_STATUS_INVALID_PARAMETER;
811 return gensec_start_mech(gensec_security);
815 wrappers for the gensec function pointers
817 _PUBLIC_ NTSTATUS gensec_unseal_packet(struct gensec_security *gensec_security,
819 uint8_t *data, size_t length,
820 const uint8_t *whole_pdu, size_t pdu_length,
821 const DATA_BLOB *sig)
823 if (!gensec_security->ops->unseal_packet) {
824 return NT_STATUS_NOT_IMPLEMENTED;
826 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
827 return NT_STATUS_INVALID_PARAMETER;
830 return gensec_security->ops->unseal_packet(gensec_security, mem_ctx,
832 whole_pdu, pdu_length,
836 _PUBLIC_ NTSTATUS gensec_check_packet(struct gensec_security *gensec_security,
838 const uint8_t *data, size_t length,
839 const uint8_t *whole_pdu, size_t pdu_length,
840 const DATA_BLOB *sig)
842 if (!gensec_security->ops->check_packet) {
843 return NT_STATUS_NOT_IMPLEMENTED;
845 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
846 return NT_STATUS_INVALID_PARAMETER;
849 return gensec_security->ops->check_packet(gensec_security, mem_ctx, data, length, whole_pdu, pdu_length, sig);
852 _PUBLIC_ NTSTATUS gensec_seal_packet(struct gensec_security *gensec_security,
854 uint8_t *data, size_t length,
855 const uint8_t *whole_pdu, size_t pdu_length,
858 if (!gensec_security->ops->seal_packet) {
859 return NT_STATUS_NOT_IMPLEMENTED;
861 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
862 return NT_STATUS_INVALID_PARAMETER;
865 return gensec_security->ops->seal_packet(gensec_security, mem_ctx, data, length, whole_pdu, pdu_length, sig);
868 _PUBLIC_ NTSTATUS gensec_sign_packet(struct gensec_security *gensec_security,
870 const uint8_t *data, size_t length,
871 const uint8_t *whole_pdu, size_t pdu_length,
874 if (!gensec_security->ops->sign_packet) {
875 return NT_STATUS_NOT_IMPLEMENTED;
877 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
878 return NT_STATUS_INVALID_PARAMETER;
881 return gensec_security->ops->sign_packet(gensec_security, mem_ctx, data, length, whole_pdu, pdu_length, sig);
884 _PUBLIC_ size_t gensec_sig_size(struct gensec_security *gensec_security, size_t data_size)
886 if (!gensec_security->ops->sig_size) {
889 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
893 return gensec_security->ops->sig_size(gensec_security, data_size);
896 size_t gensec_max_wrapped_size(struct gensec_security *gensec_security)
898 if (!gensec_security->ops->max_wrapped_size) {
902 return gensec_security->ops->max_wrapped_size(gensec_security);
905 size_t gensec_max_input_size(struct gensec_security *gensec_security)
907 if (!gensec_security->ops->max_input_size) {
908 return (1 << 17) - gensec_sig_size(gensec_security, 1 << 17);
911 return gensec_security->ops->max_input_size(gensec_security);
914 _PUBLIC_ NTSTATUS gensec_wrap(struct gensec_security *gensec_security,
919 if (!gensec_security->ops->wrap) {
920 return NT_STATUS_NOT_IMPLEMENTED;
922 return gensec_security->ops->wrap(gensec_security, mem_ctx, in, out);
925 _PUBLIC_ NTSTATUS gensec_unwrap(struct gensec_security *gensec_security,
930 if (!gensec_security->ops->unwrap) {
931 return NT_STATUS_NOT_IMPLEMENTED;
933 return gensec_security->ops->unwrap(gensec_security, mem_ctx, in, out);
936 _PUBLIC_ NTSTATUS gensec_session_key(struct gensec_security *gensec_security,
937 DATA_BLOB *session_key)
939 if (!gensec_security->ops->session_key) {
940 return NT_STATUS_NOT_IMPLEMENTED;
942 if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SESSION_KEY)) {
943 return NT_STATUS_NO_USER_SESSION_KEY;
946 return gensec_security->ops->session_key(gensec_security, session_key);
950 * Return the credentials of a logged on user, including session keys
953 * Only valid after a successful authentication
955 * May only be called once per authentication.
959 _PUBLIC_ NTSTATUS gensec_session_info(struct gensec_security *gensec_security,
960 struct auth_session_info **session_info)
962 if (!gensec_security->ops->session_info) {
963 return NT_STATUS_NOT_IMPLEMENTED;
965 return gensec_security->ops->session_info(gensec_security, session_info);
969 * Next state function for the GENSEC state machine
971 * @param gensec_security GENSEC State
972 * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
973 * @param in The request, as a DATA_BLOB
974 * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
975 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
976 * or NT_STATUS_OK if the user is authenticated.
979 _PUBLIC_ NTSTATUS gensec_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
980 const DATA_BLOB in, DATA_BLOB *out)
982 return gensec_security->ops->update(gensec_security, out_mem_ctx, in, out);
985 static void gensec_update_async_timed_handler(struct tevent_context *ev, struct tevent_timer *te,
986 struct timeval t, void *ptr)
988 struct gensec_update_request *req = talloc_get_type(ptr, struct gensec_update_request);
989 req->status = req->gensec_security->ops->update(req->gensec_security, req, req->in, &req->out);
990 req->callback.fn(req, req->callback.private_data);
994 * Next state function for the GENSEC state machine async version
996 * @param gensec_security GENSEC State
997 * @param in The request, as a DATA_BLOB
998 * @param callback The function that will be called when the operation is
999 * finished, it should return gensec_update_recv() to get output
1000 * @param private_data A private pointer that will be passed to the callback function
1003 _PUBLIC_ void gensec_update_send(struct gensec_security *gensec_security, const DATA_BLOB in,
1004 void (*callback)(struct gensec_update_request *req, void *private_data),
1007 struct gensec_update_request *req = NULL;
1008 struct tevent_timer *te = NULL;
1010 req = talloc(gensec_security, struct gensec_update_request);
1011 if (!req) goto failed;
1012 req->gensec_security = gensec_security;
1014 req->out = data_blob(NULL, 0);
1015 req->callback.fn = callback;
1016 req->callback.private_data = private_data;
1018 te = event_add_timed(gensec_security->event_ctx, req,
1020 gensec_update_async_timed_handler, req);
1021 if (!te) goto failed;
1027 callback(NULL, private_data);
1031 * Next state function for the GENSEC state machine
1033 * @param req GENSEC update request state
1034 * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
1035 * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
1036 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
1037 * or NT_STATUS_OK if the user is authenticated.
1039 _PUBLIC_ NTSTATUS gensec_update_recv(struct gensec_update_request *req, TALLOC_CTX *out_mem_ctx, DATA_BLOB *out)
1043 NT_STATUS_HAVE_NO_MEMORY(req);
1046 talloc_steal(out_mem_ctx, out->data);
1047 status = req->status;
1054 * Set the requirement for a certain feature on the connection
1058 _PUBLIC_ void gensec_want_feature(struct gensec_security *gensec_security,
1061 if (!gensec_security->ops || !gensec_security->ops->want_feature) {
1062 gensec_security->want_features |= feature;
1065 gensec_security->ops->want_feature(gensec_security, feature);
1069 * Check the requirement for a certain feature on the connection
1073 _PUBLIC_ bool gensec_have_feature(struct gensec_security *gensec_security,
1076 if (!gensec_security->ops->have_feature) {
1080 /* We might 'have' features that we don't 'want', because the
1081 * other end demanded them, or we can't neotiate them off */
1082 return gensec_security->ops->have_feature(gensec_security, feature);
1086 * Associate a credentials structure with a GENSEC context - talloc_reference()s it to the context
1090 _PUBLIC_ NTSTATUS gensec_set_credentials(struct gensec_security *gensec_security, struct cli_credentials *credentials)
1092 gensec_security->credentials = talloc_reference(gensec_security, credentials);
1093 NT_STATUS_HAVE_NO_MEMORY(gensec_security->credentials);
1094 gensec_want_feature(gensec_security, cli_credentials_get_gensec_features(gensec_security->credentials));
1095 return NT_STATUS_OK;
1099 * Return the credentials structure associated with a GENSEC context
1103 _PUBLIC_ struct cli_credentials *gensec_get_credentials(struct gensec_security *gensec_security)
1105 if (!gensec_security) {
1108 return gensec_security->credentials;
1112 * Set the target service (such as 'http' or 'host') on a GENSEC context - ensures it is talloc()ed
1116 _PUBLIC_ NTSTATUS gensec_set_target_service(struct gensec_security *gensec_security, const char *service)
1118 gensec_security->target.service = talloc_strdup(gensec_security, service);
1119 if (!gensec_security->target.service) {
1120 return NT_STATUS_NO_MEMORY;
1122 return NT_STATUS_OK;
1125 _PUBLIC_ const char *gensec_get_target_service(struct gensec_security *gensec_security)
1127 if (gensec_security->target.service) {
1128 return gensec_security->target.service;
1135 * Set the target hostname (suitable for kerberos resolutation) on a GENSEC context - ensures it is talloc()ed
1139 _PUBLIC_ NTSTATUS gensec_set_target_hostname(struct gensec_security *gensec_security, const char *hostname)
1141 gensec_security->target.hostname = talloc_strdup(gensec_security, hostname);
1142 if (hostname && !gensec_security->target.hostname) {
1143 return NT_STATUS_NO_MEMORY;
1145 return NT_STATUS_OK;
1148 _PUBLIC_ const char *gensec_get_target_hostname(struct gensec_security *gensec_security)
1150 /* We allow the target hostname to be overriden for testing purposes */
1151 if (gensec_security->settings->target_hostname) {
1152 return gensec_security->settings->target_hostname;
1155 if (gensec_security->target.hostname) {
1156 return gensec_security->target.hostname;
1159 /* We could add use the 'set sockaddr' call, and do a reverse
1160 * lookup, but this would be both insecure (compromising the
1161 * way kerberos works) and add DNS timeouts */
1166 * Set (and talloc_reference) local and peer socket addresses onto a socket
1167 * context on the GENSEC context.
1169 * This is so that kerberos can include these addresses in
1170 * cryptographic tokens, to avoid certain attacks.
1174 * @brief Set the local gensec address.
1176 * @param gensec_security The gensec security context to use.
1178 * @param remote The local address to set.
1180 * @return On success NT_STATUS_OK is returned or an NT_STATUS
1183 _PUBLIC_ NTSTATUS gensec_set_local_address(struct gensec_security *gensec_security,
1184 const struct tsocket_address *local)
1186 TALLOC_FREE(gensec_security->local_addr);
1188 if (local == NULL) {
1189 return NT_STATUS_OK;
1192 gensec_security->local_addr = tsocket_address_copy(local, gensec_security);
1193 if (gensec_security->local_addr == NULL) {
1194 return NT_STATUS_NO_MEMORY;
1197 return NT_STATUS_OK;
1201 * @brief Set the remote gensec address.
1203 * @param gensec_security The gensec security context to use.
1205 * @param remote The remote address to set.
1207 * @return On success NT_STATUS_OK is returned or an NT_STATUS
1210 _PUBLIC_ NTSTATUS gensec_set_remote_address(struct gensec_security *gensec_security,
1211 const struct tsocket_address *remote)
1213 TALLOC_FREE(gensec_security->remote_addr);
1215 if (remote == NULL) {
1216 return NT_STATUS_OK;
1219 gensec_security->remote_addr = tsocket_address_copy(remote, gensec_security);
1220 if (gensec_security->remote_addr == NULL) {
1221 return NT_STATUS_NO_MEMORY;
1224 return NT_STATUS_OK;
1228 * @brief Get the local address from a gensec security context.
1230 * @param gensec_security The security context to get the address from.
1232 * @return The address as tsocket_address which could be NULL if
1233 * no address is set.
1235 _PUBLIC_ const struct tsocket_address *gensec_get_local_address(struct gensec_security *gensec_security)
1237 if (gensec_security == NULL) {
1240 return gensec_security->local_addr;
1244 * @brief Get the remote address from a gensec security context.
1246 * @param gensec_security The security context to get the address from.
1248 * @return The address as tsocket_address which could be NULL if
1249 * no address is set.
1251 _PUBLIC_ const struct tsocket_address *gensec_get_remote_address(struct gensec_security *gensec_security)
1253 if (gensec_security == NULL) {
1256 return gensec_security->remote_addr;
1261 * Set the target principal (assuming it it known, say from the SPNEGO reply)
1262 * - ensures it is talloc()ed
1266 NTSTATUS gensec_set_target_principal(struct gensec_security *gensec_security, const char *principal)
1268 gensec_security->target.principal = talloc_strdup(gensec_security, principal);
1269 if (!gensec_security->target.principal) {
1270 return NT_STATUS_NO_MEMORY;
1272 return NT_STATUS_OK;
1275 const char *gensec_get_target_principal(struct gensec_security *gensec_security)
1277 if (gensec_security->target.principal) {
1278 return gensec_security->target.principal;
1285 register a GENSEC backend.
1287 The 'name' can be later used by other backends to find the operations
1288 structure for this backend.
1290 NTSTATUS gensec_register(const struct gensec_security_ops *ops)
1292 if (gensec_security_by_name(NULL, ops->name) != NULL) {
1293 /* its already registered! */
1294 DEBUG(0,("GENSEC backend '%s' already registered\n",
1296 return NT_STATUS_OBJECT_NAME_COLLISION;
1299 generic_security_ops = talloc_realloc(talloc_autofree_context(),
1300 generic_security_ops,
1301 struct gensec_security_ops *,
1302 gensec_num_backends+2);
1303 if (!generic_security_ops) {
1304 return NT_STATUS_NO_MEMORY;
1307 generic_security_ops[gensec_num_backends] = discard_const_p(struct gensec_security_ops, ops);
1308 gensec_num_backends++;
1309 generic_security_ops[gensec_num_backends] = NULL;
1311 DEBUG(3,("GENSEC backend '%s' registered\n",
1314 return NT_STATUS_OK;
1318 return the GENSEC interface version, and the size of some critical types
1319 This can be used by backends to either detect compilation errors, or provide
1320 multiple implementations for different smbd compilation options in one module
1322 const struct gensec_critical_sizes *gensec_interface_version(void)
1324 static const struct gensec_critical_sizes critical_sizes = {
1325 GENSEC_INTERFACE_VERSION,
1326 sizeof(struct gensec_security_ops),
1327 sizeof(struct gensec_security),
1330 return &critical_sizes;
1333 static int sort_gensec(struct gensec_security_ops **gs1, struct gensec_security_ops **gs2) {
1334 return (*gs2)->priority - (*gs1)->priority;
1337 int gensec_setting_int(struct gensec_settings *settings, const char *mechanism, const char *name, int default_value)
1339 return lp_parm_int(settings->lp_ctx, NULL, mechanism, name, default_value);
1342 bool gensec_setting_bool(struct gensec_settings *settings, const char *mechanism, const char *name, bool default_value)
1344 return lp_parm_bool(settings->lp_ctx, NULL, mechanism, name, default_value);
1348 initialise the GENSEC subsystem
1350 _PUBLIC_ NTSTATUS gensec_init(struct loadparm_context *lp_ctx)
1352 static bool initialized = false;
1353 extern NTSTATUS gensec_sasl_init(void);
1354 extern NTSTATUS gensec_krb5_init(void);
1355 extern NTSTATUS gensec_schannel_init(void);
1356 extern NTSTATUS gensec_spnego_init(void);
1357 extern NTSTATUS gensec_gssapi_init(void);
1358 extern NTSTATUS gensec_ntlmssp_init(void);
1360 init_module_fn static_init[] = { STATIC_gensec_MODULES };
1361 init_module_fn *shared_init;
1363 if (initialized) return NT_STATUS_OK;
1366 shared_init = load_samba_modules(NULL, lp_ctx, "gensec");
1368 run_init_functions(static_init);
1369 run_init_functions(shared_init);
1371 talloc_free(shared_init);
1373 qsort(generic_security_ops, gensec_num_backends, sizeof(*generic_security_ops), QSORT_CAST sort_gensec);
1375 return NT_STATUS_OK;