r26580: Include sentinel in build.h, in case the list is empty.
[samba.git] / source4 / auth / gensec / gensec.c
index 57bc1c6b5afc227c28224f3e3bb07b7ee2c673fa..cd2066d9fd5f93044fa92e68497a9794350410f0 100644 (file)
@@ -8,7 +8,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -17,8 +17,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -28,6 +27,7 @@
 #include "librpc/rpc/dcerpc.h"
 #include "auth/credentials/credentials.h"
 #include "auth/gensec/gensec.h"
+#include "param/param.h"
 
 /* the list of currently registered GENSEC backends */
 static struct gensec_security_ops **generic_security_ops;
@@ -47,10 +47,15 @@ struct gensec_security_ops **gensec_security_all(void)
 
 struct gensec_security_ops **gensec_use_kerberos_mechs(TALLOC_CTX *mem_ctx, 
                                                       struct gensec_security_ops **old_gensec_list, 
-                                                      enum credentials_use_kerberos use_kerberos) 
+                                                      struct cli_credentials *creds)
 {
        struct gensec_security_ops **new_gensec_list;
        int i, j, num_mechs_in;
+       enum credentials_use_kerberos use_kerberos = CRED_AUTO_USE_KERBEROS;
+
+       if (creds) {
+               use_kerberos = cli_credentials_get_kerberos_state(creds);
+       }
 
        if (use_kerberos == CRED_AUTO_USE_KERBEROS) {
                if (!talloc_reference(mem_ctx, old_gensec_list)) {
@@ -80,13 +85,13 @@ struct gensec_security_ops **gensec_use_kerberos_mechs(TALLOC_CTX *mem_ctx,
                }
                switch (use_kerberos) {
                case CRED_DONT_USE_KERBEROS:
-                       if (old_gensec_list[i]->kerberos == False) {
+                       if (old_gensec_list[i]->kerberos == false) {
                                new_gensec_list[j] = old_gensec_list[i];
                                j++;
                        }
                        break;
                case CRED_MUST_USE_KERBEROS:
-                       if (old_gensec_list[i]->kerberos == True) {
+                       if (old_gensec_list[i]->kerberos == true) {
                                new_gensec_list[j] = old_gensec_list[i];
                                j++;
                        }
@@ -112,7 +117,6 @@ struct gensec_security_ops **gensec_security_mechs(struct gensec_security *gense
                }
                return backends;
        } else {
-               enum credentials_use_kerberos use_kerberos;
                struct cli_credentials *creds = gensec_get_credentials(gensec_security);
                if (!creds) {
                        if (!talloc_reference(mem_ctx, backends)) {
@@ -120,8 +124,7 @@ struct gensec_security_ops **gensec_security_mechs(struct gensec_security *gense
                        }
                        return backends;
                }
-               use_kerberos = cli_credentials_get_kerberos_state(creds);
-               return gensec_use_kerberos_mechs(mem_ctx, backends, use_kerberos);
+               return gensec_use_kerberos_mechs(mem_ctx, backends, creds);
        }
 }
 
@@ -450,7 +453,7 @@ const char **gensec_security_oids_from_ops_wrapped(TALLOC_CTX *mem_ctx,
 /**
  * Return all the security subsystems currently enabled on a GENSEC context.
  * 
- * This is taken from a list attached to the cli_credentails, and
+ * This is taken from a list attached to the cli_credentials, and
  * skips the OID in 'skip'.  (Typically the SPNEGO OID)
  * 
  */
@@ -474,6 +477,7 @@ const char **gensec_security_oids(struct gensec_security *gensec_security,
 */
 static NTSTATUS gensec_start(TALLOC_CTX *mem_ctx, 
                             struct event_context *ev,
+                            struct loadparm_context *lp_ctx,
                             struct messaging_context *msg,
                             struct gensec_security **gensec_security)
 {
@@ -486,7 +490,7 @@ static NTSTATUS gensec_start(TALLOC_CTX *mem_ctx,
        ZERO_STRUCT((*gensec_security)->peer_addr);
        ZERO_STRUCT((*gensec_security)->my_addr);
 
-       (*gensec_security)->subcontext = False;
+       (*gensec_security)->subcontext = false;
        (*gensec_security)->want_features = 0;
        
        if (ev == NULL) {
@@ -499,6 +503,7 @@ static NTSTATUS gensec_start(TALLOC_CTX *mem_ctx,
 
        (*gensec_security)->event_ctx = ev;
        (*gensec_security)->msg_ctx = msg;
+       (*gensec_security)->lp_ctx = lp_ctx;
 
        return NT_STATUS_OK;
 }
@@ -522,9 +527,10 @@ _PUBLIC_ NTSTATUS gensec_subcontext_start(TALLOC_CTX *mem_ctx,
        (*gensec_security)->ops = NULL;
        (*gensec_security)->private_data = NULL;
 
-       (*gensec_security)->subcontext = True;
+       (*gensec_security)->subcontext = true;
        (*gensec_security)->event_ctx = parent->event_ctx;
        (*gensec_security)->msg_ctx = parent->msg_ctx;
+       (*gensec_security)->lp_ctx = parent->lp_ctx;
 
        return NT_STATUS_OK;
 }
@@ -537,7 +543,8 @@ _PUBLIC_ NTSTATUS gensec_subcontext_start(TALLOC_CTX *mem_ctx,
 */
 _PUBLIC_ NTSTATUS gensec_client_start(TALLOC_CTX *mem_ctx, 
                             struct gensec_security **gensec_security,
-                            struct event_context *ev)
+                            struct event_context *ev,
+                            struct loadparm_context *lp_ctx)
 {
        NTSTATUS status;
        struct event_context *new_ev = NULL;
@@ -548,7 +555,7 @@ _PUBLIC_ NTSTATUS gensec_client_start(TALLOC_CTX *mem_ctx,
                ev = new_ev;
        }
 
-       status = gensec_start(mem_ctx, ev, NULL, gensec_security);
+       status = gensec_start(mem_ctx, ev, lp_ctx, NULL, gensec_security);
        if (!NT_STATUS_IS_OK(status)) {
                talloc_free(new_ev);
                return status;
@@ -567,6 +574,7 @@ _PUBLIC_ NTSTATUS gensec_client_start(TALLOC_CTX *mem_ctx,
 */
 NTSTATUS gensec_server_start(TALLOC_CTX *mem_ctx, 
                             struct event_context *ev,
+                            struct loadparm_context *lp_ctx,
                             struct messaging_context *msg,
                             struct gensec_security **gensec_security)
 {
@@ -582,7 +590,7 @@ NTSTATUS gensec_server_start(TALLOC_CTX *mem_ctx,
                return NT_STATUS_INTERNAL_ERROR;
        }
 
-       status = gensec_start(mem_ctx, ev, msg, gensec_security);
+       status = gensec_start(mem_ctx, ev, lp_ctx, msg, gensec_security);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -1027,11 +1035,11 @@ _PUBLIC_ void gensec_want_feature(struct gensec_security *gensec_security,
  *
  */
 
-_PUBLIC_ BOOL gensec_have_feature(struct gensec_security *gensec_security,
+_PUBLIC_ bool gensec_have_feature(struct gensec_security *gensec_security,
                         uint32_t feature) 
 {
        if (!gensec_security->ops->have_feature) {
-               return False;
+               return false;
        }
        
        /* We might 'have' features that we don't 'want', because the
@@ -1105,7 +1113,7 @@ _PUBLIC_ NTSTATUS gensec_set_target_hostname(struct gensec_security *gensec_secu
 _PUBLIC_ const char *gensec_get_target_hostname(struct gensec_security *gensec_security) 
 {
        /* We allow the target hostname to be overriden for testing purposes */
-       const char *target_hostname = lp_parm_string(-1, "gensec", "target_hostname");
+       const char *target_hostname = lp_parm_string(gensec_security->lp_ctx, NULL, "gensec", "target_hostname");
        if (target_hostname) {
                return target_hostname;
        }
@@ -1203,7 +1211,7 @@ const char *gensec_get_target_principal(struct gensec_security *gensec_security)
 */
 NTSTATUS gensec_register(const struct gensec_security_ops *ops)
 {
-       if (!lp_parm_bool(-1, "gensec", ops->name, ops->enabled)) {
+       if (!lp_parm_bool(global_loadparm, NULL, "gensec", ops->name, ops->enabled)) {
                DEBUG(2,("gensec subsystem %s is disabled\n", ops->name));
                return NT_STATUS_OK;
        }
@@ -1223,7 +1231,7 @@ NTSTATUS gensec_register(const struct gensec_security_ops *ops)
                return NT_STATUS_NO_MEMORY;
        }
 
-       generic_security_ops[gensec_num_backends] = discard_const(ops);
+       generic_security_ops[gensec_num_backends] = discard_const_p(struct gensec_security_ops, ops);
        gensec_num_backends++;
        generic_security_ops[gensec_num_backends] = NULL;
 
@@ -1256,17 +1264,17 @@ static int sort_gensec(struct gensec_security_ops **gs1, struct gensec_security_
 /*
   initialise the GENSEC subsystem
 */
-NTSTATUS gensec_init(void)
+NTSTATUS gensec_init(struct loadparm_context *lp_ctx)
 {
-       static BOOL initialized = False;
+       static bool initialized = false;
 
-       init_module_fn static_init[] = STATIC_gensec_MODULES;
+       init_module_fn static_init[] = { STATIC_gensec_MODULES };
        init_module_fn *shared_init;
 
        if (initialized) return NT_STATUS_OK;
-       initialized = True;
+       initialized = true;
        
-       shared_init = load_samba_modules(NULL, "gensec");
+       shared_init = load_samba_modules(NULL, lp_ctx, "gensec");
 
        run_init_functions(static_init);
        run_init_functions(shared_init);