1
/* Editor Settings: expandtabs and use 4 spaces for indentation
2
* ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
3
* -*- mode: c, c-basic-offset: 4 -*- */
6
* Copyright Likewise Software 2004-2008
9
* This library is free software; you can redistribute it and/or modify it
10
* under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation; either version 2.1 of the license, or (at
12
* your option) any later version.
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
17
* General Public License for more details. You should have received a copy
18
* of the GNU Lesser General Public License along with this program. If
19
* not, see <http://www.gnu.org/licenses/>.
21
* LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
22
* TERMS AS WELL. IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
23
* WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
24
* TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
25
* LESSER GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE. IF YOU
26
* HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
27
* TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
28
* license@likewisesoftware.com
34
#if !defined(MAXHOSTNAMELEN)
35
#define MAXHOSTNAMELEN (256)
38
NET_API_STATUS NetUnjoinDomainLocal(const wchar16_t *machine,
39
const wchar16_t *domain,
40
const wchar16_t *account,
41
const wchar16_t *password,
44
const UINT32 domain_access = DOMAIN_ACCESS_ENUM_ACCOUNTS |
45
DOMAIN_ACCESS_OPEN_ACCOUNT |
46
DOMAIN_ACCESS_LOOKUP_INFO_2 |
47
DOMAIN_ACCESS_CREATE_USER;
48
WINERR err = ERROR_SUCCESS;
49
NTSTATUS status = STATUS_SUCCESS;
50
ACCOUNT_HANDLE hAccount = NULL;
51
HANDLE hStore = (HANDLE)NULL;
52
PLWPS_PASSWORD_INFO pi = NULL;
54
char *localname = NULL;
55
wchar16_t *domain_controller_name = NULL;
56
wchar16_t *machine_name = NULL;
57
PIO_CREDS creds = NULL;
59
BAIL_ON_INVALID_PTR(machine);
60
BAIL_ON_INVALID_PTR(domain);
62
machine_name = wc16sdup(machine);
63
BAIL_ON_NO_MEMORY(machine_name);
65
err = NetGetHostInfo(&localname);
66
BAIL_ON_WINERR_ERROR(err);
68
status = NetpGetRwDcName(domain, FALSE, &domain_controller_name);
69
BAIL_ON_NTSTATUS_ERROR(status);
71
status = LwpsOpenPasswordStore(LWPS_PASSWORD_STORE_DEFAULT, &hStore);
72
BAIL_ON_NTSTATUS_ERROR(status);
74
status = LwpsGetPasswordByHostName(hStore, localname, &pi);
75
BAIL_ON_NTSTATUS_ERROR(status);
77
/* zero the machine password */
78
memset((void*)pi->pwszMachinePassword, 0,
79
wc16slen(pi->pwszMachinePassword));
80
pi->last_change_time = time(NULL);
82
status = LwpsWritePasswordToAllStores(pi);
83
BAIL_ON_NTSTATUS_ERROR(status);
85
/* disable the account only if requested */
86
if (options & NETSETUP_ACCT_DELETE) {
87
if (account && password)
89
status = LwIoCreatePlainCredsW(account, domain, password, &creds);
90
BAIL_ON_NTSTATUS_ERROR(status);
94
status = LwIoGetActiveCreds(NULL, &creds);
95
BAIL_ON_NTSTATUS_ERROR(status);
98
status = NetConnectSamr(&conn, domain_controller_name, domain_access, 0, creds);
99
BAIL_ON_NTSTATUS_ERROR(status);
101
status = DisableWksAccount(conn, pi->pwszMachineAccount, &hAccount);
102
BAIL_ON_NTSTATUS_ERROR(status);
104
status = NetDisconnectSamr(conn);
105
BAIL_ON_NTSTATUS_ERROR(status);
112
NetFreeMemory(localname);
115
SAFE_FREE(domain_controller_name);
116
SAFE_FREE(machine_name);
119
LwpsFreePasswordInfo(hStore, pi);
122
if (hStore != (HANDLE)NULL) {
123
LwpsClosePasswordStore(hStore);
126
if (err == ERROR_SUCCESS &&
127
status != STATUS_SUCCESS) {
128
err = NtStatusToWin32Error(status);
136
LwIoDeleteCreds(creds);
139
SAFE_FREE(machine_name);
145
NET_API_STATUS NetUnjoinDomain(const wchar16_t *hostname,
146
const wchar16_t *account,
147
const wchar16_t *password,
150
NET_API_STATUS err = ERROR_SUCCESS;
151
NTSTATUS status = STATUS_SUCCESS;
152
wchar16_t *domain = NULL;
153
HANDLE hStore = (HANDLE)NULL;
154
PLWPS_PASSWORD_INFO pi = NULL;
155
char *localname = NULL;
157
/* at the moment we support only locally triggered unjoin */
159
status = STATUS_NOT_IMPLEMENTED;
162
wchar16_t host[MAXHOSTNAMELEN];
164
err = NetGetHostInfo(&localname);
165
BAIL_ON_WINERR_ERROR(err);
167
mbstowc16s(host, localname, sizeof(wchar16_t)*MAXHOSTNAMELEN);
169
status = LwpsOpenPasswordStore(LWPS_PASSWORD_STORE_DEFAULT, &hStore);
170
BAIL_ON_NTSTATUS_ERROR(status);
172
status = LwpsGetPasswordByHostName(hStore, localname, &pi);
173
BAIL_ON_NTSTATUS_ERROR(status);
175
domain = pi->pwszDnsDomainName;
176
err = NetUnjoinDomainLocal(host, domain, account, password, options);
177
BAIL_ON_WINERR_ERROR(err);
180
if (hStore != (HANDLE)NULL) {
181
status = LwpsClosePasswordStore(hStore);
182
BAIL_ON_NTSTATUS_ERROR(status);
188
NetFreeMemory(localname);
192
LwpsFreePasswordInfo(hStore, pi);
195
if (err == ERROR_SUCCESS &&
196
status != STATUS_SUCCESS) {
197
err = NtStatusToWin32Error(status);
211
indent-tabs-mode: nil