1/* 2 * Unix SMB/CIFS implementation. 3 * Join Support (cmdline + netapi) 4 * Copyright (C) Guenther Deschner 2007-2008 5 * 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. 10 * 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. 15 * 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/>. 18 */ 19 20#include <sys/types.h> 21#include <inttypes.h> 22#include <stdio.h> 23#include <stdlib.h> 24#include <string.h> 25 26#include <netapi.h> 27 28#include "common.h" 29 30enum { 31 OPT_OU = 1000 32}; 33 34int main(int argc, const char **argv) 35{ 36 NET_API_STATUS status; 37 const char *host_name = NULL; 38 const char *domain_name = NULL; 39 const char *account_ou = NULL; 40 const char *account = NULL; 41 const char *password = NULL; 42 uint32_t join_flags = NETSETUP_JOIN_DOMAIN | 43 NETSETUP_ACCT_CREATE | 44 NETSETUP_DOMAIN_JOIN_IF_JOINED; 45 struct libnetapi_ctx *ctx = NULL; 46 47 poptContext pc; 48 int opt; 49 50 struct poptOption long_options[] = { 51 POPT_AUTOHELP 52 { "ou", 0, POPT_ARG_STRING, &account_ou, 'U', "Account ou", "ACCOUNT_OU" }, 53 { "domain", 0, POPT_ARG_STRING, &domain_name, 'U', "Domain name (required)", "DOMAIN" }, 54 { "userd", 0, POPT_ARG_STRING, &account, 'U', "Domain admin account", "USERNAME" }, 55 { "passwordd", 0, POPT_ARG_STRING, &password, 'U', "Domain admin password", "PASSWORD" }, 56 POPT_COMMON_LIBNETAPI_EXAMPLES 57 POPT_TABLEEND 58 }; 59 60 61 status = libnetapi_init(&ctx); 62 if (status != 0) { 63 return status; 64 } 65 66 pc = poptGetContext("netdomjoin", argc, argv, long_options, 0); 67 68 poptSetOtherOptionHelp(pc, "hostname"); 69 while((opt = poptGetNextOpt(pc)) != -1) { 70 } 71 72 if (!poptPeekArg(pc)) { 73 poptPrintHelp(pc, stderr, 0); 74 goto out; 75 } 76 host_name = poptGetArg(pc); 77 78 if (!domain_name) { 79 poptPrintHelp(pc, stderr, 0); 80 goto out; 81 } 82 83 /* NetJoinDomain */ 84 85 status = NetJoinDomain(host_name, 86 domain_name, 87 account_ou, 88 account, 89 password, 90 join_flags); 91 if (status != 0) { 92 const char *errstr = NULL; 93 errstr = libnetapi_get_error_string(ctx, status); 94 printf("Join failed with: %s\n", errstr); 95 } else { 96 printf("Successfully joined\n"); 97 } 98 99 out: 100 libnetapi_free(ctx); 101 poptFreeContext(pc); 102 103 return status; 104} 105