mirror of
https://github.com/samba-team/samba.git
synced 2025-11-27 08:23:49 +03:00
Use popt in libetapi example code.
Guenther
(This used to be commit 6f239df3f5)
This commit is contained 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 $@
|
||||
|
||||
58
source3/lib/netapi/examples/common.c
Normal file
58
source3/lib/netapi/examples/common.c
Normal file
@@ -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
|
||||
};
|
||||
|
||||
11
source3/lib/netapi/examples/common.h
Normal file
11
source3/lib/netapi/examples/common.h
Normal file
@@ -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 },
|
||||
|
||||
@@ -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);
|
||||
|
||||
status = NetGetDCName(argv[1], argv[2], &buffer);
|
||||
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(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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
#include "common.h"
|
||||
|
||||
p = strchr(param, '=');
|
||||
if (!p) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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 = 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];
|
||||
}
|
||||
|
||||
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);
|
||||
poptSetOtherOptionHelp(pc, "hostname domainname");
|
||||
while((opt = poptGetNextOpt(pc)) != -1) {
|
||||
switch (opt) {
|
||||
case 'D':
|
||||
domain_name = poptGetOptArg(pc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
status = NetGetJoinableOUs(server_name,
|
||||
if (!poptPeekArg(pc)) {
|
||||
poptPrintHelp(pc, stderr, 0);
|
||||
goto out;
|
||||
}
|
||||
host_name = poptGetArg(pc);
|
||||
|
||||
/* NetGetJoinableOUs */
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
if (!domain_name) {
|
||||
poptPrintHelp(pc, stderr, 0);
|
||||
goto out;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
/* NetJoinDomain */
|
||||
|
||||
status = NetJoinDomain(server_name,
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user