diff options
author | Günther Deschner <gd@samba.org> | 2008-04-08 14:34:30 +0200 |
---|---|---|
committer | Günther Deschner <gd@samba.org> | 2008-04-08 14:34:30 +0200 |
commit | cccaef9e0896e9a3fcf9b860283543fd35d3f248 (patch) | |
tree | ba1c3244a682376c347ab1f8974b1e19106ac0f3 /source3/lib | |
parent | 0f86cc18ff2e88a0f1fe244574ff5e3fcfce2f92 (diff) | |
download | samba-cccaef9e0896e9a3fcf9b860283543fd35d3f248.tar.gz samba-cccaef9e0896e9a3fcf9b860283543fd35d3f248.tar.bz2 samba-cccaef9e0896e9a3fcf9b860283543fd35d3f248.zip |
Use popt in libetapi example code.
Guenther
(This used to be commit 6f239df3f5a57c9549f1637e53fd42d2ed604c3f)
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/netapi/examples/Makefile.in | 15 | ||||
-rw-r--r-- | source3/lib/netapi/examples/common.c | 58 | ||||
-rw-r--r-- | source3/lib/netapi/examples/common.h | 11 | ||||
-rw-r--r-- | source3/lib/netapi/examples/getdc/getdc.c | 45 | ||||
-rw-r--r-- | source3/lib/netapi/examples/getjoinableous/getjoinableous.c | 76 | ||||
-rw-r--r-- | source3/lib/netapi/examples/netdomjoin/netdomjoin.c | 96 |
6 files changed, 187 insertions, 114 deletions
diff --git a/source3/lib/netapi/examples/Makefile.in b/source3/lib/netapi/examples/Makefile.in index 000eef118b..9020d60224 100644 --- a/source3/lib/netapi/examples/Makefile.in +++ b/source3/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@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@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@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@EXEEXT@: $(BINARY_PREREQS) $(NETDOMJOIN_GUI_OBJ) @echo Linking $@ diff --git a/source3/lib/netapi/examples/common.c b/source3/lib/netapi/examples/common.c new file mode 100644 index 0000000000..db9ab0a2c9 --- /dev/null +++ b/source3/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/source3/lib/netapi/examples/common.h b/source3/lib/netapi/examples/common.h new file mode 100644 index 0000000000..85df51d868 --- /dev/null +++ b/source3/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/source3/lib/netapi/examples/getdc/getdc.c b/source3/lib/netapi/examples/getdc/getdc.c index 272ba1088e..98bb6a13b2 100644 --- a/source3/lib/netapi/examples/getdc/getdc.c +++ b/source3/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/source3/lib/netapi/examples/getjoinableous/getjoinableous.c b/source3/lib/netapi/examples/getjoinableous/getjoinableous.c index be95198bcf..732f73dd57 100644 --- a/source3/lib/netapi/examples/getjoinableous/getjoinableous.c +++ b/source3/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/source3/lib/netapi/examples/netdomjoin/netdomjoin.c b/source3/lib/netapi/examples/netdomjoin/netdomjoin.c index 29f66a17a2..bd7c36382a 100644 --- a/source3/lib/netapi/examples/netdomjoin/netdomjoin.c +++ b/source3/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; } |