[SCM] Samba Shared Repository - branch v3-2-test updated - release-3-2-0pre2-690-g6f239df

Günther Deschner gd at samba.org
Tue Apr 8 12:35:54 GMT 2008


The branch, v3-2-test has been updated
       via  6f239df3f5a57c9549f1637e53fd42d2ed604c3f (commit)
       via  394bdb89ef350db0ab6aca093054e1048a8ffe1f (commit)
      from  15ba45e567d910c1b2336dcc0c475e12b082f30f (commit)

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=v3-2-test


- Log -----------------------------------------------------------------
commit 6f239df3f5a57c9549f1637e53fd42d2ed604c3f
Author: Günther Deschner <gd at samba.org>
Date:   Tue Apr 8 14:34:30 2008 +0200

    Use popt in libetapi example code.
    
    Guenther

commit 394bdb89ef350db0ab6aca093054e1048a8ffe1f
Author: Günther Deschner <gd at samba.org>
Date:   Tue Apr 8 14:29:21 2008 +0200

    Prefill in username in libnetapi ctx.
    
    Guenther

-----------------------------------------------------------------------

Summary of changes:
 source/lib/netapi/examples/Makefile.in             |   15 ++--
 source/lib/netapi/examples/common.c                |   58 ++++++++++++
 source/lib/netapi/examples/common.h                |   11 +++
 source/lib/netapi/examples/getdc/getdc.c           |   45 ++++++++--
 .../examples/getjoinableous/getjoinableous.c       |   76 +++++++---------
 source/lib/netapi/examples/netdomjoin/netdomjoin.c |   96 ++++++++------------
 source/lib/netapi/netapi.c                         |   10 ++-
 7 files changed, 196 insertions(+), 115 deletions(-)
 create mode 100644 source/lib/netapi/examples/common.c
 create mode 100644 source/lib/netapi/examples/common.h


Changeset truncated at 500 lines:

diff --git a/source/lib/netapi/examples/Makefile.in b/source/lib/netapi/examples/Makefile.in
index 000eef1..9020d60 100644
--- a/source/lib/netapi/examples/Makefile.in
+++ b/source/lib/netapi/examples/Makefile.in
@@ -10,6 +10,8 @@ CC=@CC@
 PICFLAG=@PICFLAG@
 LDFLAGS=@PIE_LDFLAGS@ @LDFLAGS@
 DYNEXP=@DYNEXP@
+NETAPI_LIBS=$(LIBS) $(KRB5LIBS) $(LDAP_LIBS)
+CMDLINE_LIBS=$(NETAPI_LIBS) @POPTLIBS@
 
 # Compile a source file.
 COMPILE_CC = $(CC) -I. $(FLAGS) $(PICFLAG) -c $< -o $@
@@ -46,22 +48,23 @@ bin/.dummy:
 		echo "$(COMPILE_CC)" 1>&2;\
 		$(COMPILE_CC) >/dev/null 2>&1
 
-GETDC_OBJ = getdc/getdc.o
-NETDOMJOIN_OBJ = netdomjoin/netdomjoin.o
+CMDLINE_OBJ = common.o
+GETDC_OBJ = getdc/getdc.o $(CMDLINE_OBJ)
+NETDOMJOIN_OBJ = netdomjoin/netdomjoin.o $(CMDLINE_OBJ)
 NETDOMJOIN_GUI_OBJ = netdomjoin-gui/netdomjoin-gui.o
-GETJOINABLEOUS_OBJ = getjoinableous/getjoinableous.o
+GETJOINABLEOUS_OBJ = getjoinableous/getjoinableous.o $(CMDLINE_OBJ)
 
 bin/getdc at EXEEXT@: $(BINARY_PREREQS) $(GETDC_OBJ)
 	@echo Linking $@
-	@$(CC) $(FLAGS) -o $@ $(GETDC_OBJ) $(LDFLAGS) $(DYNEXP) $(LIBS) $(KRB5LIBS) $(LDAP_LIBS)
+	@$(CC) $(FLAGS) -o $@ $(GETDC_OBJ) $(LDFLAGS) $(DYNEXP) $(CMDLINE_LIBS)
 
 bin/getjoinableous at EXEEXT@: $(BINARY_PREREQS) $(GETJOINABLEOUS_OBJ)
 	@echo Linking $@
-	@$(CC) $(FLAGS) -o $@ $(GETJOINABLEOUS_OBJ) $(LDFLAGS) $(DYNEXP) $(LIBS) $(KRB5LIBS) $(LDAP_LIBS)
+	@$(CC) $(FLAGS) -o $@ $(GETJOINABLEOUS_OBJ) $(LDFLAGS) $(DYNEXP) $(CMDLINE_LIBS)
 
 bin/netdomjoin at EXEEXT@: $(BINARY_PREREQS) $(NETDOMJOIN_OBJ)
 	@echo Linking $@
-	@$(CC) $(FLAGS) -o $@ $(NETDOMJOIN_OBJ) $(LDFLAGS) $(DYNEXP) $(LIBS) $(KRB5LIBS) $(LDAP_LIBS)
+	@$(CC) $(FLAGS) -o $@ $(NETDOMJOIN_OBJ) $(LDFLAGS) $(DYNEXP) $(CMDLINE_LIBS)
 
 bin/netdomjoin-gui at EXEEXT@: $(BINARY_PREREQS) $(NETDOMJOIN_GUI_OBJ)
 	@echo Linking $@
diff --git a/source/lib/netapi/examples/common.c b/source/lib/netapi/examples/common.c
new file mode 100644
index 0000000..db9ab0a
--- /dev/null
+++ b/source/lib/netapi/examples/common.c
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include <string.h>
+#include <netapi.h>
+#include <popt.h>
+
+void popt_common_callback(poptContext con,
+			 enum poptCallbackReason reason,
+			 const struct poptOption *opt,
+			 const char *arg, const void *data)
+{
+	struct libnetapi_ctx *ctx = NULL;
+
+	libnetapi_getctx(&ctx);
+
+	if (reason == POPT_CALLBACK_REASON_PRE) {
+	}
+
+	if (reason == POPT_CALLBACK_REASON_POST) {
+	}
+
+	if (!opt) {
+		return;
+	}
+	switch (opt->val) {
+		case 'U': {
+			char *puser = strdup(arg);
+			char *p = NULL;
+
+			if ((p = strchr(puser,'%'))) {
+				size_t len;
+				*p = 0;
+				libnetapi_set_username(ctx, puser);
+				libnetapi_set_password(ctx, p+1);
+				len = strlen(p+1);
+				memset(strchr(arg,'%')+1,'X',len);
+			} else {
+				libnetapi_set_username(ctx, puser);
+			}
+			free(puser);
+			break;
+		}
+		case 'd':
+			libnetapi_set_debuglevel(ctx, arg);
+			break;
+		case 'p':
+			libnetapi_set_password(ctx, arg);
+			break;
+	}
+}
+
+struct poptOption popt_common_netapi_examples[] = {
+	{ NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
+	{ "user", 'U', POPT_ARG_STRING, NULL, 'U', "Username used for connection", "USERNAME" },
+	{ "password", 'p', POPT_ARG_STRING, NULL, 'p', "Password used for connection", "PASSWORD" },
+	{ "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Debuglevel", "DEBUGLEVEL" },
+	POPT_TABLEEND
+};
+
diff --git a/source/lib/netapi/examples/common.h b/source/lib/netapi/examples/common.h
new file mode 100644
index 0000000..85df51d
--- /dev/null
+++ b/source/lib/netapi/examples/common.h
@@ -0,0 +1,11 @@
+#include <popt.h>
+
+void popt_common_callback(poptContext con,
+			 enum poptCallbackReason reason,
+			 const struct poptOption *opt,
+			 const char *arg, const void *data);
+
+extern struct poptOption popt_common_netapi_examples[];
+
+#define POPT_COMMON_LIBNETAPI_EXAMPLES { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_netapi_examples, 0, "Common samba netapi example options:", NULL },
+
diff --git a/source/lib/netapi/examples/getdc/getdc.c b/source/lib/netapi/examples/getdc/getdc.c
index 272ba10..98bb6a1 100644
--- a/source/lib/netapi/examples/getdc/getdc.c
+++ b/source/lib/netapi/examples/getdc/getdc.c
@@ -25,33 +25,62 @@
 
 #include <netapi.h>
 
-int main(int argc, char **argv)
+#include "common.h"
+
+int main(int argc, const char **argv)
 {
 	NET_API_STATUS status;
 	struct libnetapi_ctx *ctx = NULL;
+
+	const char *hostname = NULL;
+	const char *domain = NULL;
 	uint8_t *buffer = NULL;
 
-	if (argc < 3) {
-		printf("usage: getdc <hostname> <domain>\n");
-		return -1;
-	}
+	poptContext pc;
+	int opt;
+
+	struct poptOption long_options[] = {
+		POPT_AUTOHELP
+		POPT_COMMON_LIBNETAPI_EXAMPLES
+		POPT_TABLEEND
+	};
 
 	status = libnetapi_init(&ctx);
 	if (status != 0) {
 		return status;
 	}
 
-	libnetapi_set_username(ctx, "");
-	libnetapi_set_password(ctx, "");
+	pc = poptGetContext("getdc", argc, argv, long_options, 0);
+
+	poptSetOtherOptionHelp(pc, "hostname domainname");
+	while((opt = poptGetNextOpt(pc)) != -1) {
+	}
+
+	if (!poptPeekArg(pc)) {
+		poptPrintHelp(pc, stderr, 0);
+		goto out;
+	}
+	hostname = poptGetArg(pc);
+
+	if (!poptPeekArg(pc)) {
+		poptPrintHelp(pc, stderr, 0);
+		goto out;
+	}
+	domain = poptGetArg(pc);
+
+	/* NetGetDCName */
 
-	status = NetGetDCName(argv[1], argv[2], &buffer);
+	status = NetGetDCName(hostname, domain, &buffer);
 	if (status != 0) {
 		printf("GetDcName failed with: %s\n", libnetapi_errstr(status));
 	} else {
 		printf("%s\n", (char *)buffer);
 	}
+
+ out:
 	NetApiBufferFree(buffer);
 	libnetapi_free(ctx);
+	poptFreeContext(pc);
 
 	return status;
 }
diff --git a/source/lib/netapi/examples/getjoinableous/getjoinableous.c b/source/lib/netapi/examples/getjoinableous/getjoinableous.c
index be95198..732f73d 100644
--- a/source/lib/netapi/examples/getjoinableous/getjoinableous.c
+++ b/source/lib/netapi/examples/getjoinableous/getjoinableous.c
@@ -19,72 +19,61 @@
 
 #include <string.h>
 #include <stdio.h>
+#include <sys/types.h>
 #include <inttypes.h>
 
 #include <netapi.h>
 
-char *get_string_param(const char *param)
-{
-	char *p;
-
-	p = strchr(param, '=');
-	if (!p) {
-		return NULL;
-	}
-
-	return (p+1);
-}
+#include "common.h"
 
-int main(int argc, char **argv)
+int main(int argc, const char **argv)
 {
 	NET_API_STATUS status;
-	const char *server_name = NULL;
+	const char *host_name = NULL;
 	const char *domain_name = NULL;
-	const char *account = NULL;
-	const char *password = NULL;
 	const char **ous = NULL;
 	uint32_t num_ous = 0;
 	struct libnetapi_ctx *ctx = NULL;
 	int i;
 
+	poptContext pc;
+	int opt;
+
+	struct poptOption long_options[] = {
+		POPT_AUTOHELP
+		{ "domain", 0, POPT_ARG_STRING, NULL, 'D', "Domain name", "DOMAIN" },
+		POPT_COMMON_LIBNETAPI_EXAMPLES
+		POPT_TABLEEND
+	};
+
 	status = libnetapi_init(&ctx);
 	if (status != 0) {
 		return status;
 	}
 
-	if (argc < 2) {
-		printf("usage: getjoinableous\n");
-		printf("\t<hostname> [domain=DOMAIN] <user=USER> <password=PASSWORD>\n");
-		return 0;
-	}
+	pc = poptGetContext("getjoinableous", argc, argv, long_options, 0);
 
-	if (argc > 2) {
-		server_name = argv[1];
+	poptSetOtherOptionHelp(pc, "hostname domainname");
+	while((opt = poptGetNextOpt(pc)) != -1) {
+		switch (opt) {
+			case 'D':
+				domain_name = poptGetOptArg(pc);
+				break;
+		}
 	}
 
-	for (i=0; i<argc; i++) {
-		if (strncasecmp(argv[i], "domain", strlen("domain"))== 0) {
-			domain_name = get_string_param(argv[i]);
-		}
-		if (strncasecmp(argv[i], "user", strlen("user"))== 0) {
-			account = get_string_param(argv[i]);
-			libnetapi_set_username(ctx, account);
-		}
-		if (strncasecmp(argv[i], "password", strlen("password"))== 0) {
-			password = get_string_param(argv[i]);
-			libnetapi_set_password(ctx, password);
-		}
-		if (strncasecmp(argv[i], "debug", strlen("debug"))== 0) {
-			const char *str = NULL;
-			str = get_string_param(argv[i]);
-			libnetapi_set_debuglevel(ctx, str);
-		}
+	if (!poptPeekArg(pc)) {
+		poptPrintHelp(pc, stderr, 0);
+		goto out;
 	}
+	host_name = poptGetArg(pc);
+
+	/* NetGetJoinableOUs */
 
-	status = NetGetJoinableOUs(server_name,
+	status = NetGetJoinableOUs(host_name,
 				   domain_name,
-				   account,
-				   password,
+				   ctx->username,
+				   ctx->password,
 				   &num_ous,
 				   &ous);
 	if (status != 0) {
@@ -97,9 +86,10 @@ int main(int argc, char **argv)
 		}
 	}
 
+ out:
 	NetApiBufferFree(ous);
-
 	libnetapi_free(ctx);
+	poptFreeContext(pc);
 
 	return status;
 }
diff --git a/source/lib/netapi/examples/netdomjoin/netdomjoin.c b/source/lib/netapi/examples/netdomjoin/netdomjoin.c
index 29f66a1..bd7c363 100644
--- a/source/lib/netapi/examples/netdomjoin/netdomjoin.c
+++ b/source/lib/netapi/examples/netdomjoin/netdomjoin.c
@@ -25,96 +25,78 @@
 
 #include <netapi.h>
 
-char *get_string_param(const char *param)
-{
-	char *p;
+#include "common.h"
 
-	p = strchr(param, '=');
-	if (!p) {
-		return NULL;
-	}
+enum {
+	OPT_OU = 1000
+};
 
-	return (p+1);
-}
-
-int main(int argc, char **argv)
+int main(int argc, const char **argv)
 {
 	NET_API_STATUS status;
-	const char *server_name = NULL;
+	const char *host_name = NULL;
 	const char *domain_name = NULL;
 	const char *account_ou = NULL;
-	const char *Account = NULL;
+	const char *account = NULL;
 	const char *password = NULL;
-	uint32_t join_flags = 3;
+	uint32_t join_flags = 0x00000023;
 	struct libnetapi_ctx *ctx = NULL;
-	int i;
+
+	poptContext pc;
+	int opt;
+
+	struct poptOption long_options[] = {
+		POPT_AUTOHELP
+		{ "ou", 0, POPT_ARG_STRING, &account_ou, 'U', "Account ou", "ACCOUNT_OU" },
+		{ "domain", 0, POPT_ARG_STRING, &domain_name, 'U', "Domain name (required)", "DOMAIN" },
+		{ "userd", 0, POPT_ARG_STRING, &account, 'U', "Domain admin account", "USERNAME" },
+		{ "passwordd", 0, POPT_ARG_STRING, &password, 'U', "Domain admin password", "PASSWORD" },
+		POPT_COMMON_LIBNETAPI_EXAMPLES
+		POPT_TABLEEND
+	};
+
 
 	status = libnetapi_init(&ctx);
 	if (status != 0) {
 		return status;
 	}
 
-	if (argc < 2) {
-		printf("usage: netdomjoin\n");
-		printf("\t[hostname] [domain=DOMAIN] <ou=OU> "
-		       "<usero=USERO> <passwordo=PASSWORDO> "
-		       "<userd=USERD> <passwordd=PASSWORDD> "
-		       "<debug=DEBUGLEVEL>\n");
-		return 0;
+	pc = poptGetContext("netdomjoin", argc, argv, long_options, 0);
+
+	poptSetOtherOptionHelp(pc, "hostname");
+	while((opt = poptGetNextOpt(pc)) != -1) {
 	}
 
-	if (argc > 2) {
-		server_name = argv[1];
+	if (!poptPeekArg(pc)) {
+		poptPrintHelp(pc, stderr, 0);
+		goto out;
 	}
+	host_name = poptGetArg(pc);
 
-	for (i=0; i<argc; i++) {
-		if (strncasecmp(argv[i], "ou", strlen("ou")) == 0) {
-			account_ou = get_string_param(argv[i]);
-		}
-		if (strncasecmp(argv[i], "domain", strlen("domain"))== 0) {
-			domain_name = get_string_param(argv[i]);
-		}
-		if (strncasecmp(argv[i], "userd", strlen("userd"))== 0) {
-			Account = get_string_param(argv[i]);
-		}
-		if (strncasecmp(argv[i], "passwordd", strlen("passwordd"))== 0) {
-			password = get_string_param(argv[i]);
-		}
-		if (strncasecmp(argv[i], "usero", strlen("usero"))== 0) {
-			const char *str = NULL;
-			str = get_string_param(argv[i]);
-			libnetapi_set_username(ctx, str);
-		}
-		if (strncasecmp(argv[i], "passwordo", strlen("passwordo"))== 0) {
-			const char *str = NULL;
-			str = get_string_param(argv[i]);
-			libnetapi_set_password(ctx, str);
-		}
-		if (strncasecmp(argv[i], "debug", strlen("debug"))== 0) {
-			const char *str = NULL;
-			str = get_string_param(argv[i]);
-			libnetapi_set_debuglevel(ctx, str);
-		}
+	if (!domain_name) {
+		poptPrintHelp(pc, stderr, 0);
+		goto out;
 	}
 
-	status = NetJoinDomain(server_name,
+	/* NetJoinDomain */
+
+	status = NetJoinDomain(host_name,
 			       domain_name,
 			       account_ou,
-			       Account,
+			       account,
 			       password,
 			       join_flags);
 	if (status != 0) {
 		const char *errstr = NULL;
 		errstr = libnetapi_get_error_string(ctx, status);
-		if (!errstr) {
-			errstr = libnetapi_errstr(status);
-		}
 		printf("Join failed with: %s\n", errstr);
 	} else {
 		printf("Successfully joined\n");
 	}
 
+ out:
 	libnetapi_free(ctx);
+	poptFreeContext(pc);
 
 	return status;
 }
diff --git a/source/lib/netapi/netapi.c b/source/lib/netapi/netapi.c
index 82a8a8d..fbec275 100644
--- a/source/lib/netapi/netapi.c
+++ b/source/lib/netapi/netapi.c
@@ -81,6 +81,13 @@ NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
 		setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
 	}
 
+	ctx->username = talloc_strdup(frame, getenv("USER"));
+	if (!ctx->username) {
+		TALLOC_FREE(frame);
+		fprintf(stderr, "out of memory\n");
+		return W_ERROR_V(WERR_NOMEM);
+	}
+
 	libnetapi_initialized = true;
 
 	*context = stat_ctx = ctx;
@@ -162,7 +169,8 @@ NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
 				      const char *username)
 {
 	TALLOC_FREE(ctx->username);
-	ctx->username = talloc_strdup(ctx, username);
+	ctx->username = talloc_strdup(ctx, username ? username : "");
+
 	if (!ctx->username) {
 		return W_ERROR_V(WERR_NOMEM);
 	}


-- 
Samba Shared Repository


More information about the samba-cvs mailing list