~ubuntu-branches/ubuntu/oneiric/likewise-open/oneiric

« back to all changes in this revision

Viewing changes to librpc/netlib/net_unjoindomain.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Salley
  • Date: 2010-11-22 12:06:00 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20101122120600-8lba1fpceot71wlb
Tags: 6.0.0.53010-1
Likewise Open 6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 -*- */
4
 
 
5
 
/*
6
 
 * Copyright Likewise Software    2004-2008
7
 
 * All rights reserved.
8
 
 *
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.
13
 
 *
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/>.
20
 
 *
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
29
 
 */
30
 
 
31
 
#include "includes.h"
32
 
 
33
 
 
34
 
#if !defined(MAXHOSTNAMELEN)
35
 
#define MAXHOSTNAMELEN (256)
36
 
#endif
37
 
 
38
 
NET_API_STATUS NetUnjoinDomainLocal(const wchar16_t *machine,
39
 
                                    const wchar16_t *domain,
40
 
                                    const wchar16_t *account,
41
 
                                    const wchar16_t *password,
42
 
                                    UINT32 options)
43
 
{
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;
53
 
    NetConn *conn = NULL;
54
 
    char *localname = NULL;
55
 
    wchar16_t *domain_controller_name = NULL;
56
 
    wchar16_t *machine_name = NULL;
57
 
    PIO_CREDS creds = NULL;
58
 
 
59
 
    BAIL_ON_INVALID_PTR(machine);
60
 
    BAIL_ON_INVALID_PTR(domain);
61
 
 
62
 
    machine_name = wc16sdup(machine);
63
 
    BAIL_ON_NO_MEMORY(machine_name);
64
 
 
65
 
    err = NetGetHostInfo(&localname);
66
 
    BAIL_ON_WINERR_ERROR(err);
67
 
 
68
 
    status = NetpGetRwDcName(domain, FALSE, &domain_controller_name);
69
 
    BAIL_ON_NTSTATUS_ERROR(status);
70
 
 
71
 
    status = LwpsOpenPasswordStore(LWPS_PASSWORD_STORE_DEFAULT, &hStore);
72
 
    BAIL_ON_NTSTATUS_ERROR(status);
73
 
 
74
 
    status = LwpsGetPasswordByHostName(hStore, localname, &pi);
75
 
    BAIL_ON_NTSTATUS_ERROR(status);
76
 
 
77
 
    /* zero the machine password */
78
 
    memset((void*)pi->pwszMachinePassword, 0,
79
 
           wc16slen(pi->pwszMachinePassword));
80
 
    pi->last_change_time = time(NULL);
81
 
 
82
 
    status = LwpsWritePasswordToAllStores(pi);
83
 
    BAIL_ON_NTSTATUS_ERROR(status);
84
 
 
85
 
    /* disable the account only if requested */
86
 
    if (options & NETSETUP_ACCT_DELETE) {
87
 
        if (account && password)
88
 
        {
89
 
            status = LwIoCreatePlainCredsW(account, domain, password, &creds);
90
 
            BAIL_ON_NTSTATUS_ERROR(status);
91
 
        }
92
 
        else
93
 
        {
94
 
            status = LwIoGetActiveCreds(NULL, &creds);
95
 
            BAIL_ON_NTSTATUS_ERROR(status);
96
 
        }
97
 
 
98
 
        status = NetConnectSamr(&conn, domain_controller_name, domain_access, 0, creds);
99
 
        BAIL_ON_NTSTATUS_ERROR(status);
100
 
 
101
 
        status = DisableWksAccount(conn, pi->pwszMachineAccount, &hAccount);
102
 
        BAIL_ON_NTSTATUS_ERROR(status);
103
 
 
104
 
        status = NetDisconnectSamr(conn);
105
 
        BAIL_ON_NTSTATUS_ERROR(status);
106
 
    }
107
 
 
108
 
cleanup:
109
 
 
110
 
    if (localname)
111
 
    {
112
 
        NetFreeMemory(localname);
113
 
    }
114
 
 
115
 
    SAFE_FREE(domain_controller_name);
116
 
    SAFE_FREE(machine_name);
117
 
 
118
 
    if (pi) {
119
 
        LwpsFreePasswordInfo(hStore, pi);
120
 
    }
121
 
 
122
 
    if (hStore != (HANDLE)NULL) {
123
 
       LwpsClosePasswordStore(hStore);
124
 
    }
125
 
 
126
 
    if (err == ERROR_SUCCESS &&
127
 
        status != STATUS_SUCCESS) {
128
 
        err = NtStatusToWin32Error(status);
129
 
    }
130
 
 
131
 
    return err;
132
 
 
133
 
error:
134
 
    if (creds)
135
 
    {
136
 
        LwIoDeleteCreds(creds);
137
 
    }
138
 
 
139
 
    SAFE_FREE(machine_name);
140
 
 
141
 
    goto cleanup;
142
 
}
143
 
 
144
 
 
145
 
NET_API_STATUS NetUnjoinDomain(const wchar16_t *hostname,
146
 
                               const wchar16_t *account,
147
 
                               const wchar16_t *password,
148
 
                               UINT32 options)
149
 
{
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;
156
 
 
157
 
    /* at the moment we support only locally triggered unjoin */
158
 
    if (hostname) {
159
 
        status = STATUS_NOT_IMPLEMENTED;
160
 
 
161
 
    } else {
162
 
        wchar16_t host[MAXHOSTNAMELEN];
163
 
 
164
 
        err = NetGetHostInfo(&localname);
165
 
        BAIL_ON_WINERR_ERROR(err);
166
 
 
167
 
        mbstowc16s(host, localname, sizeof(wchar16_t)*MAXHOSTNAMELEN);
168
 
 
169
 
        status = LwpsOpenPasswordStore(LWPS_PASSWORD_STORE_DEFAULT, &hStore);
170
 
        BAIL_ON_NTSTATUS_ERROR(status);
171
 
 
172
 
        status = LwpsGetPasswordByHostName(hStore, localname, &pi);
173
 
        BAIL_ON_NTSTATUS_ERROR(status);
174
 
 
175
 
        domain = pi->pwszDnsDomainName;
176
 
        err = NetUnjoinDomainLocal(host, domain, account, password, options);
177
 
        BAIL_ON_WINERR_ERROR(err);
178
 
    }
179
 
 
180
 
    if (hStore != (HANDLE)NULL) {
181
 
       status = LwpsClosePasswordStore(hStore);
182
 
       BAIL_ON_NTSTATUS_ERROR(status);
183
 
    }
184
 
 
185
 
cleanup:
186
 
    if (localname)
187
 
    {
188
 
        NetFreeMemory(localname);
189
 
    }
190
 
 
191
 
    if (pi) {
192
 
       LwpsFreePasswordInfo(hStore, pi);
193
 
    }
194
 
 
195
 
    if (err == ERROR_SUCCESS &&
196
 
        status != STATUS_SUCCESS) {
197
 
        err = NtStatusToWin32Error(status);
198
 
    }
199
 
 
200
 
    return err;
201
 
 
202
 
error:
203
 
    goto cleanup;
204
 
}
205
 
 
206
 
 
207
 
/*
208
 
local variables:
209
 
mode: c
210
 
c-basic-offset: 4
211
 
indent-tabs-mode: nil
212
 
tab-width: 4
213
 
end:
214
 
*/