net conf: adapt output of NULL share params in net conf list.
[ira/wip.git] / source3 / utils / net_conf.c
index 575bfb89cad4023ceccca39ea83f2d0edb53acec..4fffcf8a8c25403bcad33c9064c2a0b62c699bc0 100644 (file)
@@ -109,6 +109,24 @@ static int net_conf_delparm_usage(int argc, const char **argv)
        return -1;
 }
 
+static int net_conf_getincludes_usage(int argc, const char **argv)
+{
+       d_printf("USAGE: net conf getincludes <section>\n");
+       return -1;
+}
+
+static int net_conf_setincludes_usage(int argc, const char **argv)
+{
+       d_printf("USAGE: net conf setincludes <section> [<filename>]*\n");
+       return -1;
+}
+
+static int net_conf_delincludes_usage(int argc, const char **argv)
+{
+       d_printf("USAGE: net conf delincludes <section>\n");
+       return -1;
+}
+
 
 /**********************************************************************
  *
@@ -132,9 +150,13 @@ static WERROR import_process_service(struct smbconf_ctx *conf_ctx,
        TALLOC_CTX *mem_ctx = talloc_stackframe();
 
        if (opt_testmode) {
-               d_printf("[%s]\n", servicename);
-               for (idx = 0; idx < num_params; idx ++) {
-                       d_printf("\t%s = %s\n", param_names[idx],
+               const char *indent = "";
+               if (servicename != NULL) {
+                       d_printf("[%s]\n", servicename);
+                       indent = "\t";
+               }
+               for (idx = 0; idx < num_params; idx++) {
+                       d_printf("%s%s = %s\n", indent, param_names[idx],
                                 param_values[idx]);
                }
                d_printf("\n");
@@ -224,11 +246,16 @@ static int net_conf_list(struct smbconf_ctx *conf_ctx,
        }
 
        for (share_count = 0; share_count < num_shares; share_count++) {
-               d_printf("[%s]\n", share_names[share_count]);
+               const char *indent = "";
+               if (share_names[share_count] != NULL) {
+                       d_printf("[%s]\n", share_names[share_count]);
+                       indent = "\t";
+               }
                for (param_count = 0; param_count < num_params[share_count];
                     param_count++)
                {
-                       d_printf("\t%s = %s\n",
+                       d_printf("%s%s = %s\n",
+                                indent,
                                 param_names[share_count][param_count],
                                 param_values[share_count][param_count]);
                }
@@ -248,6 +275,7 @@ static int net_conf_import(struct smbconf_ctx *conf_ctx,
        int ret = -1;
        const char *filename = NULL;
        const char *servicename = NULL;
+       char *conf_source = NULL;
        TALLOC_CTX *mem_ctx;
        struct smbconf_ctx *txt_ctx;
        WERROR werr;
@@ -260,7 +288,11 @@ static int net_conf_import(struct smbconf_ctx *conf_ctx,
                        net_conf_import_usage(argc, argv);
                        goto done;
                case 2:
-                       servicename = argv[1];
+                       servicename = talloc_strdup_lower(mem_ctx, argv[1]);
+                       if (servicename == NULL) {
+                               d_printf("error: out of memory!\n");
+                               goto done;
+                       }
                case 1:
                        filename = argv[0];
                        break;
@@ -269,8 +301,16 @@ static int net_conf_import(struct smbconf_ctx *conf_ctx,
        DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
                filename));
 
-       werr = smbconf_init_txt_simple(mem_ctx, &txt_ctx, filename, true);
+       conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
+       if (conf_source == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
+
+       werr = smbconf_init(mem_ctx, &txt_ctx, conf_source);
        if (!W_ERROR_IS_OK(werr)) {
+               d_printf("error loading file '%s': %s\n", filename,
+                        dos_errstr(werr));
                goto done;
        }
 
@@ -414,7 +454,11 @@ static int net_conf_showshare(struct smbconf_ctx *conf_ctx,
                goto done;
        }
 
-       sharename = argv[0];
+       sharename = talloc_strdup_lower(mem_ctx, argv[0]);
+       if (sharename == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
 
        werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &num_params,
                                 &param_names, &param_values);
@@ -455,6 +499,7 @@ static int net_conf_addshare(struct smbconf_ctx *conf_ctx,
        const char *guest_ok = "no";
        const char *writeable = "no";
        SMB_STRUCT_STAT sbuf;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
 
        switch (argc) {
                case 0:
@@ -502,7 +547,12 @@ static int net_conf_addshare(struct smbconf_ctx *conf_ctx,
                        }
                case 2:
                        path = argv[1];
-                       sharename = strdup_lower(argv[0]);
+                       sharename = talloc_strdup_lower(mem_ctx, argv[0]);
+                       if (sharename == NULL) {
+                               d_printf("error: out of memory!\n");
+                               goto done;
+                       }
+
                        break;
        }
 
@@ -614,7 +664,7 @@ static int net_conf_addshare(struct smbconf_ctx *conf_ctx,
        ret = 0;
 
 done:
-       SAFE_FREE(sharename);
+       TALLOC_FREE(mem_ctx);
        return ret;
 }
 
@@ -624,12 +674,17 @@ static int net_conf_delshare(struct smbconf_ctx *conf_ctx,
        int ret = -1;
        const char *sharename = NULL;
        WERROR werr = WERR_OK;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
 
        if (argc != 1) {
                net_conf_delshare_usage(argc, argv);
                goto done;
        }
-       sharename = argv[0];
+       sharename = talloc_strdup_lower(mem_ctx, argv[0]);
+       if (sharename == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
 
        werr = smbconf_delete_share(conf_ctx, sharename);
        if (!W_ERROR_IS_OK(werr)) {
@@ -640,6 +695,7 @@ static int net_conf_delshare(struct smbconf_ctx *conf_ctx,
 
        ret = 0;
 done:
+       TALLOC_FREE(mem_ctx);
        return ret;
 }
 
@@ -651,13 +707,22 @@ static int net_conf_setparm(struct smbconf_ctx *conf_ctx,
        char *service = NULL;
        char *param = NULL;
        const char *value_str = NULL;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
 
        if (argc != 3) {
                net_conf_setparm_usage(argc, argv);
                goto done;
        }
-       service = strdup_lower(argv[0]);
-       param = strdup_lower(argv[1]);
+       service = talloc_strdup_lower(mem_ctx, argv[0]);
+       if (service == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
+       param = talloc_strdup_lower(mem_ctx, argv[1]);
+       if (param == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
        value_str = argv[2];
 
        if (!smbconf_share_exists(conf_ctx, service)) {
@@ -680,8 +745,7 @@ static int net_conf_setparm(struct smbconf_ctx *conf_ctx,
        ret = 0;
 
 done:
-       SAFE_FREE(service);
-       SAFE_FREE(param);
+       TALLOC_FREE(mem_ctx);
        return ret;
 }
 
@@ -701,8 +765,16 @@ static int net_conf_getparm(struct smbconf_ctx *conf_ctx,
                net_conf_getparm_usage(argc, argv);
                goto done;
        }
-       service = strdup_lower(argv[0]);
-       param = strdup_lower(argv[1]);
+       service = talloc_strdup_lower(mem_ctx, argv[0]);
+       if (service == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
+       param = talloc_strdup_lower(mem_ctx, argv[1]);
+       if (param == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
 
        werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
 
@@ -726,8 +798,6 @@ static int net_conf_getparm(struct smbconf_ctx *conf_ctx,
 
        ret = 0;
 done:
-       SAFE_FREE(service);
-       SAFE_FREE(param);
        TALLOC_FREE(mem_ctx);
        return ret;
 }
@@ -739,13 +809,22 @@ static int net_conf_delparm(struct smbconf_ctx *conf_ctx,
        WERROR werr = WERR_OK;
        char *service = NULL;
        char *param = NULL;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
 
        if (argc != 2) {
                net_conf_delparm_usage(argc, argv);
                goto done;
        }
-       service = strdup_lower(argv[0]);
-       param = strdup_lower(argv[1]);
+       service = talloc_strdup_lower(mem_ctx, argv[0]);
+       if (service == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
+       param = talloc_strdup_lower(mem_ctx, argv[1]);
+       if (param == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
 
        werr = smbconf_delete_parameter(conf_ctx, service, param);
 
@@ -768,8 +847,120 @@ static int net_conf_delparm(struct smbconf_ctx *conf_ctx,
        ret = 0;
 
 done:
-       SAFE_FREE(service);
-       SAFE_FREE(param);
+       TALLOC_FREE(mem_ctx);
+       return ret;
+}
+
+static int net_conf_getincludes(struct smbconf_ctx *conf_ctx,
+                               int argc, const char **argv)
+{
+       WERROR werr;
+       uint32_t num_includes;
+       uint32_t count;
+       char *service;
+       char **includes = NULL;
+       int ret = -1;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
+
+       if (argc != 1) {
+               net_conf_getincludes_usage(argc, argv);
+               goto done;
+       }
+
+       service = talloc_strdup_lower(mem_ctx, argv[0]);
+       if (service == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
+
+       werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
+                                   &num_includes, &includes);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_printf("error getting includes: %s\n", dos_errstr(werr));
+               goto done;
+       }
+
+       for (count = 0; count < num_includes; count++) {
+               d_printf("include = %s\n", includes[count]);
+       }
+
+       ret = 0;
+
+done:
+       TALLOC_FREE(mem_ctx);
+       return ret;
+}
+
+static int net_conf_setincludes(struct smbconf_ctx *conf_ctx,
+                               int argc, const char **argv)
+{
+       WERROR werr;
+       char *service;
+       uint32_t num_includes;
+       const char **includes;
+       int ret = -1;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
+
+       if (argc < 1) {
+               net_conf_setincludes_usage(argc, argv);
+               goto done;
+       }
+
+       service = talloc_strdup_lower(mem_ctx, argv[0]);
+       if (service == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
+
+       num_includes = argc - 1;
+       if (num_includes == 0) {
+               includes = NULL;
+       } else {
+               includes = argv + 1;
+       }
+
+       werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_printf("error setting includes: %s\n", dos_errstr(werr));
+               goto done;
+       }
+
+       ret = 0;
+
+done:
+       TALLOC_FREE(mem_ctx);
+       return ret;
+}
+
+static int net_conf_delincludes(struct smbconf_ctx *conf_ctx,
+                               int argc, const char **argv)
+{
+       WERROR werr;
+       char *service;
+       int ret = -1;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
+
+       if (argc != 1) {
+               net_conf_delincludes_usage(argc, argv);
+               goto done;
+       }
+
+       service = talloc_strdup_lower(mem_ctx, argv[0]);
+       if (service == NULL) {
+               d_printf("error: out of memory!\n");
+               goto done;
+       }
+
+       werr = smbconf_delete_includes(conf_ctx, service);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_printf("error deleting includes: %s\n", dos_errstr(werr));
+               goto done;
+       }
+
+       ret = 0;
+
+done:
+       TALLOC_FREE(mem_ctx);
        return ret;
 }
 
@@ -794,7 +985,7 @@ static int net_conf_wrap_function(int (*fn)(struct smbconf_ctx *,
        struct smbconf_ctx *conf_ctx;
        int ret = -1;
 
-       werr = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
+       werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
 
        if (!W_ERROR_IS_OK(werr)) {
                return -1;
@@ -873,6 +1064,12 @@ int net_conf(int argc, const char **argv)
                 "Retrieve the value of a parameter."},
                {"delparm", net_conf_delparm,
                 "Delete a parameter."},
+               {"getincludes", net_conf_getincludes,
+                "Show the includes of a share definition."},
+               {"setincludes", net_conf_setincludes,
+                "Set includes for a share."},
+               {"delincludes", net_conf_delincludes,
+                "Delete includes from a share definition."},
                {NULL, NULL, NULL}
        };