RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / utils / net_conf.c
index bcb116a0c8626e905b82deab94bf4322b4c59da0..6a601d8871b0214b2a771f4247699ad2ca02fe35 100644 (file)
@@ -1,6 +1,6 @@
-/* 
- *  Samba Unix/Linux SMB client library 
- *  Distributed SMB/CIFS Server Management Utility 
+/*
+ *  Samba Unix/Linux SMB client library
+ *  Distributed SMB/CIFS Server Management Utility
  *  Local configuration interface
  *  Copyright (C) Michael Adam 2007
  *
@@ -8,26 +8,26 @@
  *  it under the terms of the GNU General Public License as published by
  *  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,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  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, see <http://www.gnu.org/licenses/>.  
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
- * This is an interface to the configuration stored inside the 
- * samba registry. In the future there might be support for other 
+ * This is an interface to the configuration stored inside the
+ * samba registry. In the future there might be support for other
  * configuration backends as well.
  */
 
 #include "includes.h"
 #include "utils/net.h"
 
-/* 
+/*
  * usage functions
  */
 
@@ -39,7 +39,8 @@ static int net_conf_list_usage(int argc, const char **argv)
 
 static int net_conf_import_usage(int argc, const char**argv)
 {
-       d_printf("USAGE: net conf import [--test|-T] <filename> [<servicename>]\n"
+       d_printf("USAGE: net conf import [--test|-T] <filename> "
+                "[<servicename>]\n"
                 "\t[--test|-T]    testmode - do not act, just print "
                                   "what would be done\n"
                 "\t<servicename>  only import service <servicename>, "
@@ -125,7 +126,7 @@ static char *format_value(TALLOC_CTX *mem_ctx, struct registry_value *value)
        case REG_MULTI_SZ: {
                 uint32 j;
                 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
-                        result = talloc_asprintf(mem_ctx, "\"%s\" ", 
+                        result = talloc_asprintf(mem_ctx, "\"%s\" ",
                                                 value->v.multi_sz.strings[j]);
                 }
                 break;
@@ -142,7 +143,7 @@ static char *format_value(TALLOC_CTX *mem_ctx, struct registry_value *value)
 }
 
 /*
- * add a value to a key. 
+ * add a value to a key.
  */
 static WERROR reg_setvalue_internal(struct registry_key *key,
                                    const char *valname,
@@ -151,22 +152,33 @@ static WERROR reg_setvalue_internal(struct registry_key *key,
        struct registry_value val;
        WERROR werr = WERR_OK;
        char *subkeyname;
+       const char *canon_valname;
+       const char *canon_valstr;
 
-       ZERO_STRUCT(val);
-
-       val.type = REG_SZ;
-       val.v.sz.str = CONST_DISCARD(char *, valstr);
-       val.v.sz.len = strlen(valstr) + 1;
-
-       if (!lp_parameter_is_valid(valname)) {
-               d_fprintf(stderr, "Invalid parameter '%s' given.\n", valname);
+       if (!lp_canonicalize_parameter_with_value(valname, valstr,
+                                                 &canon_valname,
+                                                 &canon_valstr))
+       {
+               if (canon_valname == NULL) {
+                       d_fprintf(stderr, "invalid parameter '%s' given\n",
+                                 valname);
+               } else {
+                       d_fprintf(stderr, "invalid value '%s' given for "
+                                 "parameter '%s'\n", valstr, valname);
+               }
                werr = WERR_INVALID_PARAM;
                goto done;
        }
 
-       if (registry_smbconf_valname_forbidden(valname)) {
+       ZERO_STRUCT(val);
+
+       val.type = REG_SZ;
+       val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
+       val.v.sz.len = strlen(canon_valstr) + 1;
+
+       if (registry_smbconf_valname_forbidden(canon_valname)) {
                d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
-                         valname);
+                         canon_valname);
                werr = WERR_INVALID_PARAM;
                goto done;
        }
@@ -183,30 +195,30 @@ static WERROR reg_setvalue_internal(struct registry_key *key,
            lp_parameter_is_global(valname))
        {
                d_fprintf(stderr, "Global paramter '%s' not allowed in "
-                         "service definition ('%s').\n", valname,
+                         "service definition ('%s').\n", canon_valname,
                          subkeyname);
                werr = WERR_INVALID_PARAM;
                goto done;
        }
 
-       werr = reg_setvalue(key, valname, &val);
+       werr = reg_setvalue(key, canon_valname, &val);
        if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr,
                          "Error adding value '%s' to "
                          "key '%s': %s\n",
-                         valname, key->key->name, dos_errstr(werr));
+                         canon_valname, key->key->name, dos_errstr(werr));
        }
 
 done:
        return werr;
 }
 
-/* 
+/*
  * Open a subkey of KEY_SMBCONF (i.e a service)
  * - variant without error output (q = quiet)-
  */
 static WERROR smbconf_open_path_q(TALLOC_CTX *ctx, const char *subkeyname,
-                                 uint32 desired_access, 
+                                 uint32 desired_access,
                                  struct registry_key **key)
 {
        WERROR werr = WERR_OK;
@@ -220,8 +232,7 @@ static WERROR smbconf_open_path_q(TALLOC_CTX *ctx, const char *subkeyname,
 
        if (subkeyname == NULL) {
                path = talloc_strdup(ctx, KEY_SMBCONF);
-       }
-       else {
+       } else {
                path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, subkeyname);
        }
 
@@ -233,12 +244,12 @@ done:
        return werr;
 }
 
-/* 
+/*
  * Open a subkey of KEY_SMBCONF (i.e a service)
  * - variant with error output -
  */
 static WERROR smbconf_open_path(TALLOC_CTX *ctx, const char *subkeyname,
-                               uint32 desired_access, 
+                               uint32 desired_access,
                                struct registry_key **key)
 {
        WERROR werr = WERR_OK;
@@ -246,8 +257,8 @@ static WERROR smbconf_open_path(TALLOC_CTX *ctx, const char *subkeyname,
        werr = smbconf_open_path_q(ctx, subkeyname, desired_access, key);
        if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, "Error opening registry path '%s\\%s': %s\n",
-                         KEY_SMBCONF, 
-                         (subkeyname == NULL) ? "" : subkeyname, 
+                         KEY_SMBCONF,
+                         (subkeyname == NULL) ? "" : subkeyname,
                          dos_errstr(werr));
        }
 
@@ -312,7 +323,7 @@ static WERROR reg_createkey_internal(TALLOC_CTX *ctx,
                goto done;
        }
 
-       werr = reg_createkey(ctx, create_parent, subkeyname, 
+       werr = reg_createkey(ctx, create_parent, subkeyname,
                             REG_KEY_WRITE, newkey, &action);
        if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
                d_fprintf(stderr, "Key '%s' already exists.\n", subkeyname);
@@ -331,9 +342,9 @@ done:
 /*
  * check if a subkey of KEY_SMBCONF of a given name exists
  */
-static BOOL smbconf_key_exists(TALLOC_CTX *ctx, const char *subkeyname)
+static bool smbconf_key_exists(TALLOC_CTX *ctx, const char *subkeyname)
 {
-       BOOL ret = False;
+       bool ret = False;
        WERROR werr = WERR_OK;
        TALLOC_CTX *mem_ctx;
        struct registry_key *key;
@@ -353,14 +364,14 @@ done:
        return ret;
 }
 
-static BOOL smbconf_value_exists(TALLOC_CTX *ctx, struct registry_key *key,
+static bool smbconf_value_exists(TALLOC_CTX *ctx, struct registry_key *key,
                                 const char *param)
 {
-       BOOL ret = False;
+       bool ret = False;
        WERROR werr = WERR_OK;
        struct registry_value *value = NULL;
 
-       werr = reg_queryvalue(ctx, key, param, &value); 
+       werr = reg_queryvalue(ctx, key, param, &value);
        if (W_ERROR_IS_OK(werr)) {
                ret = True;
        }
@@ -391,7 +402,7 @@ static WERROR list_values(TALLOC_CTX *ctx, struct registry_key *key)
        werr = WERR_OK;
 
 done:
-       return werr; 
+       return werr;
 }
 
 static WERROR drop_smbconf_internal(TALLOC_CTX *ctx)
@@ -412,7 +423,7 @@ static WERROR drop_smbconf_internal(TALLOC_CTX *ctx)
 
        if (!(token = registry_create_admin_token(tmp_ctx))) {
                /* what is the appropriate error code here? */
-               werr = WERR_CAN_NOT_COMPLETE; 
+               werr = WERR_CAN_NOT_COMPLETE;
                goto done;
        }
 
@@ -435,8 +446,8 @@ static WERROR drop_smbconf_internal(TALLOC_CTX *ctx)
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
        }
-       
-       werr = reg_createkey(tmp_ctx, parent_key, p+1, REG_KEY_WRITE, 
+
+       werr = reg_createkey(tmp_ctx, parent_key, p+1, REG_KEY_WRITE,
                             &new_key, &action);
 
 done:
@@ -468,10 +479,10 @@ static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
                valstr = talloc_asprintf(ctx, "%s", (char *)ptr);
                break;
        case P_BOOL:
-               valstr = talloc_asprintf(ctx, "%s", BOOLSTR(*(BOOL *)ptr));
+               valstr = talloc_asprintf(ctx, "%s", BOOLSTR(*(bool *)ptr));
                break;
        case P_BOOLREV:
-               valstr = talloc_asprintf(ctx, "%s", BOOLSTR(!*(BOOL *)ptr));
+               valstr = talloc_asprintf(ctx, "%s", BOOLSTR(!*(bool *)ptr));
                break;
        case P_ENUM:
                for (i = 0; parm->enum_list[i].name; i++) {
@@ -499,8 +510,7 @@ static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
                                                valstr, "\"%s\"%s",
                                                *list,
                                                 ((*(list+1))?", ":""));
-                               }
-                               else {
+                               } else {
                                        valstr = talloc_asprintf_append(
                                                valstr, "%s%s", *list,
                                                 ((*(list+1))?", ":""));
@@ -544,8 +554,7 @@ static int import_process_service(TALLOC_CTX *ctx,
 
        if (opt_testmode) {
                d_printf("[%s]\n", servicename);
-       }
-       else {
+       } else {
                if (smbconf_key_exists(tmp_ctx, servicename)) {
                        werr = reg_delkey_internal(tmp_ctx, servicename);
                        if (!W_ERROR_IS_OK(werr)) {
@@ -569,8 +578,7 @@ static int import_process_service(TALLOC_CTX *ctx,
                if (parm->type != P_SEP) {
                        if (opt_testmode) {
                                d_printf("\t%s = %s\n", parm->label, valstr);
-                       }
-                       else {
+                       } else {
                                werr = reg_setvalue_internal(key, parm->label,
                                                             valstr);
                                if (!W_ERROR_IS_OK(werr)) {
@@ -592,7 +600,7 @@ done:
 }
 
 /* return True iff there are nondefault globals */
-static BOOL globals_exist(void)
+static bool globals_exist(void)
 {
        int i = 0;
        struct parm_struct *parm;
@@ -606,7 +614,7 @@ static BOOL globals_exist(void)
 }
 
 /*
- * the conf functions 
+ * the conf functions
  */
 
 int net_conf_list(int argc, const char **argv)
@@ -632,7 +640,7 @@ int net_conf_list(int argc, const char **argv)
        }
 
        if (smbconf_key_exists(ctx, GLOBAL_NAME))  {
-               werr = reg_openkey(ctx, base_key, GLOBAL_NAME, 
+               werr = reg_openkey(ctx, base_key, GLOBAL_NAME,
                                   REG_KEY_READ, &sub_key);
                if (!W_ERROR_IS_OK(werr)) {
                        d_fprintf(stderr, "Error opening subkey '%s' : %s\n",
@@ -649,17 +657,17 @@ int net_conf_list(int argc, const char **argv)
        for (idx_key = 0;
             W_ERROR_IS_OK(werr = reg_enumkey(ctx, base_key, idx_key,
                                              &subkey_name, NULL));
-            idx_key++) 
+            idx_key++)
        {
                if (strequal(subkey_name, GLOBAL_NAME)) {
                        continue;
                }
                d_printf("[%s]\n", subkey_name);
 
-               werr = reg_openkey(ctx, base_key, subkey_name, 
+               werr = reg_openkey(ctx, base_key, subkey_name,
                                   REG_KEY_READ, &sub_key);
                if (!W_ERROR_IS_OK(werr)) {
-                       d_fprintf(stderr, 
+                       d_fprintf(stderr,
                                  "Error opening subkey '%s': %s\n",
                                  subkey_name, dos_errstr(werr));
                        goto done;
@@ -670,7 +678,7 @@ int net_conf_list(int argc, const char **argv)
                d_printf("\n");
        }
        if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
-               d_fprintf(stderr, "Error enumerating subkeys: %s\n", 
+               d_fprintf(stderr, "Error enumerating subkeys: %s\n",
                          dos_errstr(werr));
                goto done;
        }
@@ -687,7 +695,7 @@ int net_conf_import(int argc, const char **argv)
        int ret = -1;
        const char *filename = NULL;
        const char *servicename = NULL;
-       BOOL service_found = False;
+       bool service_found = False;
        TALLOC_CTX *ctx;
        struct share_iterator *shares;
        struct share_params *share;
@@ -792,12 +800,12 @@ int net_conf_listshares(int argc, const char **argv)
        for (idx = 0;
             W_ERROR_IS_OK(werr = reg_enumkey(ctx, key, idx,
                                              &subkey_name, NULL));
-            idx++) 
+            idx++)
        {
                d_printf("%s\n", subkey_name);
        }
        if (! W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
-               d_fprintf(stderr, "Error enumerating subkeys: %s\n", 
+               d_fprintf(stderr, "Error enumerating subkeys: %s\n",
                          dos_errstr(werr));
                goto done;
        }
@@ -879,7 +887,7 @@ int net_conf_addshare(int argc, const char **argv)
        switch (argc) {
                case 0:
                case 1:
-               default: 
+               default:
                        net_conf_addshare_usage(argc, argv);
                        goto done;
                case 5:
@@ -898,7 +906,7 @@ int net_conf_addshare(int argc, const char **argv)
                                case 'N':
                                        guest_ok = "no";
                                        break;
-                               default: 
+                               default:
                                        net_conf_addshare_usage(argc, argv);
                                        goto done;
                        }
@@ -927,14 +935,14 @@ int net_conf_addshare(int argc, const char **argv)
                        break;
        }
 
-       /* 
-        * validate arguments 
+       /*
+        * validate arguments
         */
 
        /* validate share name */
 
-       if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, 
-                              strlen(sharename))) 
+       if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
+                              strlen(sharename)))
        {
                d_fprintf(stderr, "ERROR: share name %s contains "
                         "invalid characters (any of %s)\n",
@@ -949,7 +957,7 @@ int net_conf_addshare(int argc, const char **argv)
        }
 
        if (strequal(sharename, GLOBAL_NAME)) {
-               d_fprintf(stderr, 
+               d_fprintf(stderr,
                          "ERROR: 'global' is not a valid share name.\n");
                goto done;
        }
@@ -957,7 +965,7 @@ int net_conf_addshare(int argc, const char **argv)
        /* validate path */
 
        if (path[0] != '/') {
-               d_fprintf(stderr, 
+               d_fprintf(stderr,
                          "Error: path '%s' is not an absolute path.\n",
                          path);
                goto done;
@@ -967,7 +975,7 @@ int net_conf_addshare(int argc, const char **argv)
                d_fprintf(stderr,
                          "ERROR: cannot stat path '%s' to ensure "
                          "this is a directory.\n"
-                         "Error was '%s'.\n", 
+                         "Error was '%s'.\n",
                          path, strerror(errno));
                goto done;
        }
@@ -979,8 +987,8 @@ int net_conf_addshare(int argc, const char **argv)
                goto done;
        }
 
-       /* 
-        * create the share 
+       /*
+        * create the share
         */
 
        werr = reg_createkey_internal(NULL, argv[0], &newkey);
@@ -1003,7 +1011,7 @@ int net_conf_addshare(int argc, const char **argv)
        werr = reg_setvalue_internal(newkey, "guest ok", guest_ok);
        if (!W_ERROR_IS_OK(werr))
                goto done;
-       
+
        werr = reg_setvalue_internal(newkey, "writeable", writeable);
        if (!W_ERROR_IS_OK(werr))
                goto done;
@@ -1026,7 +1034,7 @@ int net_conf_delshare(int argc, const char **argv)
                goto done;
        }
        sharename = argv[0];
-       
+
        if (W_ERROR_IS_OK(reg_delkey_internal(NULL, sharename))) {
                ret = 0;
        }
@@ -1056,8 +1064,7 @@ static int net_conf_setparm(int argc, const char **argv)
 
        if (!smbconf_key_exists(ctx, service)) {
                werr = reg_createkey_internal(ctx, service, &key);
-       }
-       else {
+       } else {
                werr = smbconf_open_path(ctx, service, REG_KEY_READ, &key);
        }
        if (!W_ERROR_IS_OK(werr)) {
@@ -1100,7 +1107,7 @@ static int net_conf_getparm(int argc, const char **argv)
        param = strdup_lower(argv[1]);
 
        if (!smbconf_key_exists(ctx, service)) {
-               d_fprintf(stderr, 
+               d_fprintf(stderr,
                          "ERROR: given service '%s' does not exist.\n",
                          service);
                goto done;
@@ -1117,9 +1124,9 @@ static int net_conf_getparm(int argc, const char **argv)
                          param, dos_errstr(werr));
                goto done;
        }
-       
+
        d_printf("%s\n", format_value(ctx, value));
-       
+
        ret = 0;
 done:
        SAFE_FREE(service);
@@ -1147,7 +1154,7 @@ static int net_conf_delparm(int argc, const char **argv)
        param = strdup_lower(argv[1]);
 
        if (!smbconf_key_exists(ctx, service)) {
-               d_fprintf(stderr, 
+               d_fprintf(stderr,
                          "Error: given service '%s' does not exist.\n",
                          service);
                goto done;
@@ -1159,7 +1166,7 @@ static int net_conf_delparm(int argc, const char **argv)
        }
 
        if (!smbconf_value_exists(ctx, key, param)) {
-               d_fprintf(stderr, 
+               d_fprintf(stderr,
                          "Error: given parameter '%s' is not set.\n",
                          param);
                goto done;
@@ -1185,25 +1192,25 @@ int net_conf(int argc, const char **argv)
 {
        int ret = -1;
        struct functable2 func[] = {
-               {"list", net_conf_list, 
+               {"list", net_conf_list,
                 "Dump the complete configuration in smb.conf like format."},
                {"import", net_conf_import,
                 "Import configuration from file in smb.conf format."},
-               {"listshares", net_conf_listshares, 
+               {"listshares", net_conf_listshares,
                 "List the registry shares."},
                {"drop", net_conf_drop,
                 "Delete the complete configuration from registry."},
-               {"showshare", net_conf_showshare, 
+               {"showshare", net_conf_showshare,
                 "Show the definition of a registry share."},
-               {"addshare", net_conf_addshare, 
+               {"addshare", net_conf_addshare,
                 "Create a new registry share."},
-               {"delshare", net_conf_delshare, 
+               {"delshare", net_conf_delshare,
                 "Delete a registry share."},
-               {"setparm", net_conf_setparm, 
+               {"setparm", net_conf_setparm,
                 "Store a parameter."},
-               {"getparm", net_conf_getparm, 
+               {"getparm", net_conf_getparm,
                 "Retrieve the value of a parameter."},
-               {"delparm", net_conf_delparm, 
+               {"delparm", net_conf_delparm,
                 "Delete a parameter."},
                {NULL, NULL, NULL}
        };
@@ -1221,4 +1228,3 @@ done:
        return ret;
 }
 
-/* END */