smbd: Make reopen_from_fsp() public
[samba4-gss.git] / source3 / lib / netapi / examples / join / provision_computer_account.c
blobcc2dde5b326a85f7d0413b51db4867cbc32c42c9
1 /*
2 * Unix SMB/CIFS implementation.
3 * NetProvisionComputerAccount query
4 * Copyright (C) Guenther Deschner 2021
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <string.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <inttypes.h>
25 #include <netapi.h>
27 #include "common.h"
29 int main(int argc, const char **argv)
31 NET_API_STATUS status;
32 const char *domain = NULL;
33 const char *machine_name = NULL;
34 const char *machine_account_ou = NULL;
35 const char *dcname = NULL;
36 uint32_t options = 0;
37 const char *provision_text_data = NULL;
38 int default_password = 0;
39 int print_blob = 0;
40 const char *savefile = NULL;
41 int reuse = 0;
43 struct libnetapi_ctx *ctx = NULL;
45 poptContext pc;
46 int opt;
48 struct poptOption long_options[] = {
49 POPT_AUTOHELP
50 { "dcname", 0, POPT_ARG_STRING, &dcname, 'D', "Domain Controller Name", "DCNAME" },
51 { "machine_account_ou", 0, POPT_ARG_STRING, &machine_account_ou, 'D', "LDAP DN for Machine Account OU", "MACHINE_ACCOUNT_OU" },
52 { "defpwd", 0, POPT_ARG_NONE, &default_password, 'D', "Use default password for machine account (not recommended)", "" },
53 { "printblob", 0, POPT_ARG_NONE, &print_blob, 'D', "Print base64 encoded ODJ blob (for Windows answer files)", "" },
54 { "savefile", 0, POPT_ARG_STRING, &savefile, 'D', "Save ODJ blob to file (for Windows answer files)", "FILENAME" },
55 { "reuse", 0, POPT_ARG_NONE, &reuse, 'D', "Reuse machine account", "" },
56 POPT_COMMON_LIBNETAPI_EXAMPLES
57 POPT_TABLEEND
60 status = libnetapi_init(&ctx);
61 if (status != 0) {
62 return status;
65 pc = poptGetContext("provision_computer_account", argc, argv, long_options, 0);
67 poptSetOtherOptionHelp(pc, "domain machine_name");
68 while((opt = poptGetNextOpt(pc)) != -1) {
71 if (!poptPeekArg(pc)) {
72 poptPrintHelp(pc, stderr, 0);
73 goto out;
75 domain = poptGetArg(pc);
77 if (!poptPeekArg(pc)) {
78 poptPrintHelp(pc, stderr, 0);
79 goto out;
81 machine_name = poptGetArg(pc);
83 if (default_password) {
84 options |= NETSETUP_PROVISION_USE_DEFAULT_PASSWORD;
86 if (reuse) {
87 options |= NETSETUP_PROVISION_REUSE_ACCOUNT;
90 /* NetProvisionComputerAccount */
92 status = NetProvisionComputerAccount(domain,
93 machine_name,
94 machine_account_ou,
95 dcname,
96 options,
97 NULL,
99 &provision_text_data);
100 if (status != 0) {
101 printf("failed with: %s\n",
102 libnetapi_get_error_string(ctx, status));
103 goto out;
106 if (print_blob) {
107 printf("Provision data: %s\n", provision_text_data);
110 if (savefile != NULL) {
111 status = netapi_save_file_ucs2(savefile, provision_text_data);
112 if (status != 0) {
113 goto out;
117 out:
118 libnetapi_free(ctx);
119 poptFreeContext(pc);
121 return status;