summaryrefslogtreecommitdiff
path: root/source3/utils/net_conf.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/utils/net_conf.c')
-rw-r--r--source3/utils/net_conf.c195
1 files changed, 152 insertions, 43 deletions
diff --git a/source3/utils/net_conf.c b/source3/utils/net_conf.c
index 38cdeacc11..26ed41b2a0 100644
--- a/source3/utils/net_conf.c
+++ b/source3/utils/net_conf.c
@@ -31,9 +31,11 @@
#include "utils/net.h"
#include "libnet/libnet.h"
-/*
+/**********************************************************************
+ *
* usage functions
- */
+ *
+ **********************************************************************/
static int net_conf_list_usage(int argc, const char **argv)
{
@@ -109,10 +111,16 @@ static int net_conf_delparm_usage(int argc, const char **argv)
}
-/*
+/**********************************************************************
+ *
* Helper functions
- */
+ *
+ **********************************************************************/
+/**
+ * This formats an in-memory smbconf parameter to a string.
+ * The result string is allocated with talloc.
+ */
static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
struct share_params *share)
{
@@ -187,7 +195,12 @@ static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
return valstr;
}
+/**
+ * This functions imports a configuration that has previously
+ * been loaded with lp_load() to registry.
+ */
static int import_process_service(TALLOC_CTX *ctx,
+ struct libnet_conf_ctx *conf_ctx,
struct share_params *share)
{
int ret = -1;
@@ -210,12 +223,16 @@ static int import_process_service(TALLOC_CTX *ctx,
if (opt_testmode) {
d_printf("[%s]\n", servicename);
} else {
- if (libnet_conf_share_exists(servicename)) {
- werr = libnet_conf_delete_share(servicename);
+ if (libnet_conf_share_exists(conf_ctx, servicename)) {
+ werr = libnet_conf_delete_share(conf_ctx, servicename);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
}
+ werr = libnet_conf_create_share(conf_ctx, servicename);
+ if (!W_ERROR_IS_OK(werr)) {
+ goto done;
+ }
}
while ((parm = lp_next_parameter(share->service, &pnum, 0)))
@@ -232,7 +249,8 @@ static int import_process_service(TALLOC_CTX *ctx,
if (opt_testmode) {
d_printf("\t%s = %s\n", parm->label, valstr);
} else {
- werr = libnet_conf_set_parameter(servicename,
+ werr = libnet_conf_set_parameter(conf_ctx,
+ servicename,
parm->label,
valstr);
if (!W_ERROR_IS_OK(werr)) {
@@ -257,7 +275,10 @@ done:
return ret;
}
-/* return true iff there are nondefault globals */
+/**
+ * Return true iff there are nondefault globals in the
+ * currently loaded configuration.
+ */
static bool globals_exist(void)
{
int i = 0;
@@ -271,11 +292,15 @@ static bool globals_exist(void)
return false;
}
-/*
- * the conf functions
- */
-static int net_conf_list(int argc, const char **argv)
+/**********************************************************************
+ *
+ * the main conf functions
+ *
+ **********************************************************************/
+
+static int net_conf_list(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
WERROR werr = WERR_OK;
int ret = -1;
@@ -294,9 +319,8 @@ static int net_conf_list(int argc, const char **argv)
goto done;
}
- werr = libnet_conf_get_config(ctx, &num_shares, &share_names,
- &num_params, &param_names,
- &param_values);
+ werr = libnet_conf_get_config(ctx, conf_ctx, &num_shares, &share_names,
+ &num_params, &param_names, &param_values);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error getting config: %s\n",
dos_errstr(werr));
@@ -322,7 +346,8 @@ done:
return ret;
}
-static int net_conf_import(int argc, const char **argv)
+static int net_conf_import(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
int ret = -1;
const char *filename = NULL;
@@ -369,7 +394,7 @@ static int net_conf_import(int argc, const char **argv)
strequal(servicename, GLOBAL_NAME))
{
service_found = true;
- if (import_process_service(ctx, &global_share) != 0) {
+ if (import_process_service(ctx, conf_ctx, &global_share) != 0) {
goto done;
}
}
@@ -388,7 +413,7 @@ static int net_conf_import(int argc, const char **argv)
|| strequal(servicename, lp_servicename(share->service)))
{
service_found = true;
- if (import_process_service(ctx, share)!= 0) {
+ if (import_process_service(ctx, conf_ctx, share)!= 0) {
goto done;
}
}
@@ -408,7 +433,8 @@ done:
return ret;
}
-static int net_conf_listshares(int argc, const char **argv)
+static int net_conf_listshares(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
WERROR werr = WERR_OK;
int ret = -1;
@@ -423,7 +449,8 @@ static int net_conf_listshares(int argc, const char **argv)
goto done;
}
- werr = libnet_conf_get_share_names(ctx, &num_shares, &share_names);
+ werr = libnet_conf_get_share_names(ctx, conf_ctx, &num_shares,
+ &share_names);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
@@ -440,7 +467,8 @@ done:
return ret;
}
-static int net_conf_drop(int argc, const char **argv)
+static int net_conf_drop(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
int ret = -1;
WERROR werr;
@@ -450,7 +478,7 @@ static int net_conf_drop(int argc, const char **argv)
goto done;
}
- werr = libnet_conf_drop();
+ werr = libnet_conf_drop(conf_ctx);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error deleting configuration: %s\n",
dos_errstr(werr));
@@ -463,7 +491,8 @@ done:
return ret;
}
-static int net_conf_showshare(int argc, const char **argv)
+static int net_conf_showshare(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
int ret = -1;
WERROR werr = WERR_OK;
@@ -483,7 +512,7 @@ static int net_conf_showshare(int argc, const char **argv)
sharename = argv[0];
- werr = libnet_conf_get_share(ctx, sharename, &num_params,
+ werr = libnet_conf_get_share(ctx, conf_ctx, sharename, &num_params,
&param_names, &param_values);
if (!W_ERROR_IS_OK(werr)) {
d_printf("error getting share parameters: %s\n",
@@ -511,7 +540,8 @@ done:
* This is a high level utility function of the net conf utility,
* not a direct frontend to the libnet_conf API.
*/
-static int net_conf_addshare(int argc, const char **argv)
+static int net_conf_addshare(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
int ret = -1;
WERROR werr = WERR_OK;
@@ -599,7 +629,7 @@ static int net_conf_addshare(int argc, const char **argv)
goto done;
}
- if (libnet_conf_share_exists(sharename)) {
+ if (libnet_conf_share_exists(conf_ctx, sharename)) {
d_fprintf(stderr, "ERROR: share %s already exists.\n",
sharename);
goto done;
@@ -634,7 +664,7 @@ static int net_conf_addshare(int argc, const char **argv)
* create the share
*/
- werr = libnet_conf_create_share(sharename);
+ werr = libnet_conf_create_share(conf_ctx, sharename);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error creating share %s: %s\n",
sharename, dos_errstr(werr));
@@ -645,7 +675,7 @@ static int net_conf_addshare(int argc, const char **argv)
* fill the share with parameters
*/
- werr = libnet_conf_set_parameter(sharename, "path", path);
+ werr = libnet_conf_set_parameter(conf_ctx, sharename, "path", path);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error setting parameter %s: %s\n",
"path", dos_errstr(werr));
@@ -653,7 +683,8 @@ static int net_conf_addshare(int argc, const char **argv)
}
if (comment != NULL) {
- werr = libnet_conf_set_parameter(sharename, "comment", comment);
+ werr = libnet_conf_set_parameter(conf_ctx, sharename, "comment",
+ comment);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error setting parameter %s: %s\n",
"comment", dos_errstr(werr));
@@ -661,14 +692,16 @@ static int net_conf_addshare(int argc, const char **argv)
}
}
- werr = libnet_conf_set_parameter(sharename, "guest ok", guest_ok);
+ werr = libnet_conf_set_parameter(conf_ctx, sharename, "guest ok",
+ guest_ok);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error setting parameter %s: %s\n",
"'guest ok'", dos_errstr(werr));
goto done;
}
- werr = libnet_conf_set_parameter(sharename, "writeable", writeable);
+ werr = libnet_conf_set_parameter(conf_ctx, sharename, "writeable",
+ writeable);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error setting parameter %s: %s\n",
"writeable", dos_errstr(werr));
@@ -682,7 +715,8 @@ done:
return ret;
}
-static int net_conf_delshare(int argc, const char **argv)
+static int net_conf_delshare(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
int ret = -1;
const char *sharename = NULL;
@@ -694,7 +728,7 @@ static int net_conf_delshare(int argc, const char **argv)
}
sharename = argv[0];
- werr = libnet_conf_delete_share(sharename);
+ werr = libnet_conf_delete_share(conf_ctx, sharename);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error deleting share %s: %s\n",
sharename, dos_errstr(werr));
@@ -706,7 +740,8 @@ done:
return ret;
}
-static int net_conf_setparm(int argc, const char **argv)
+static int net_conf_setparm(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
int ret = -1;
WERROR werr = WERR_OK;
@@ -722,8 +757,8 @@ static int net_conf_setparm(int argc, const char **argv)
param = strdup_lower(argv[1]);
value_str = argv[2];
- if (!libnet_conf_share_exists(service)) {
- werr = libnet_conf_create_share(service);
+ if (!libnet_conf_share_exists(conf_ctx, service)) {
+ werr = libnet_conf_create_share(conf_ctx, service);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error creating share '%s': %s\n",
service, dos_errstr(werr));
@@ -731,7 +766,7 @@ static int net_conf_setparm(int argc, const char **argv)
}
}
- werr = libnet_conf_set_parameter(service, param, value_str);
+ werr = libnet_conf_set_parameter(conf_ctx, service, param, value_str);
if (!W_ERROR_IS_OK(werr)) {
d_fprintf(stderr, "Error setting value '%s': %s\n",
@@ -747,7 +782,8 @@ done:
return ret;
}
-static int net_conf_getparm(int argc, const char **argv)
+static int net_conf_getparm(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
int ret = -1;
WERROR werr = WERR_OK;
@@ -765,7 +801,7 @@ static int net_conf_getparm(int argc, const char **argv)
service = strdup_lower(argv[0]);
param = strdup_lower(argv[1]);
- werr = libnet_conf_get_parameter(ctx, service, param, &valstr);
+ werr = libnet_conf_get_parameter(ctx, conf_ctx, service, param, &valstr);
if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
d_fprintf(stderr,
@@ -793,7 +829,8 @@ done:
return ret;
}
-static int net_conf_delparm(int argc, const char **argv)
+static int net_conf_delparm(struct libnet_conf_ctx *conf_ctx,
+ int argc, const char **argv)
{
int ret = -1;
WERROR werr = WERR_OK;
@@ -807,7 +844,7 @@ static int net_conf_delparm(int argc, const char **argv)
service = strdup_lower(argv[0]);
param = strdup_lower(argv[1]);
- werr = libnet_conf_delete_parameter(service, param);
+ werr = libnet_conf_delete_parameter(conf_ctx, service, param);
if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
d_fprintf(stderr,
@@ -833,6 +870,78 @@ done:
return ret;
}
+
+/**********************************************************************
+ *
+ * Wrapper and net_conf_run_function mechanism.
+ *
+ **********************************************************************/
+
+/**
+ * Wrapper function to call the main conf functions.
+ * The wrapper calls handles opening and closing of the
+ * configuration.
+ */
+static int net_conf_wrap_function(int (*fn)(struct libnet_conf_ctx *,
+ int, const char **),
+ int argc, const char **argv)
+{
+ WERROR werr;
+ TALLOC_CTX *mem_ctx = talloc_stackframe();
+ struct libnet_conf_ctx *conf_ctx;
+ int ret = -1;
+
+ werr = libnet_conf_open(mem_ctx, &conf_ctx);
+
+ if (!W_ERROR_IS_OK(werr)) {
+ return -1;
+ }
+
+ ret = fn(conf_ctx, argc, argv);
+
+ libnet_conf_close(conf_ctx);
+
+ return ret;
+}
+
+/*
+ * We need a functable struct of our own, because the
+ * functions are called through a wrapper that handles
+ * the opening and closing of the configuration, and so on.
+ */
+struct conf_functable {
+ const char *funcname;
+ int (*fn)(struct libnet_conf_ctx *ctx, int argc, const char **argv);
+ const char *helptext;
+};
+
+/**
+ * This imitates net_run_function2 but calls the main functions
+ * through the wrapper net_conf_wrap_function().
+ */
+static int net_conf_run_function(int argc, const char **argv,
+ const char *whoami,
+ struct conf_functable *table)
+{
+ int i;
+
+ if (argc != 0) {
+ for (i=0; table[i].funcname; i++) {
+ if (StrCaseCmp(argv[0], table[i].funcname) == 0)
+ return net_conf_wrap_function(table[i].fn,
+ argc-1,
+ argv+1);
+ }
+ }
+
+ for (i=0; table[i].funcname; i++) {
+ d_printf("%s %-15s %s\n", whoami, table[i].funcname,
+ table[i].helptext);
+ }
+
+ return -1;
+}
+
/*
* Entry-point for all the CONF functions.
*/
@@ -840,7 +949,7 @@ done:
int net_conf(int argc, const char **argv)
{
int ret = -1;
- struct functable2 func[] = {
+ struct conf_functable func_table[] = {
{"list", net_conf_list,
"Dump the complete configuration in smb.conf like format."},
{"import", net_conf_import,
@@ -864,7 +973,7 @@ int net_conf(int argc, const char **argv)
{NULL, NULL, NULL}
};
- ret = net_run_function2(argc, argv, "net conf", func);
+ ret = net_conf_run_function(argc, argv, "net conf", func_table);
return ret;
}