6da4548f054a3a22f221231657e050b6448d5dd2
[samba.git] / source3 / lib / netapi / joindomain.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi Join Support
4  *  Copyright (C) Guenther Deschner 2007
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 "includes.h"
21 #include "lib/netapi/joindomain.h"
22
23 extern const char *opt_user_name;
24 extern const char *opt_workgroup;
25 extern const char *opt_password;
26
27 WERROR NetJoinDomain(const char *server_name,
28                      const char *domain_name,
29                      const char *account_ou,
30                      const char *Account,
31                      const char *password,
32                      uint32_t join_flags)
33 {
34         TALLOC_CTX *mem_ctx = NULL;
35         struct cli_state *cli = NULL;
36         struct rpc_pipe_client *pipe_cli = NULL;
37         struct wkssvc_PasswordBuffer encrypted_password;
38         NTSTATUS status;
39         WERROR werr;
40         unsigned int old_timeout = 0;
41
42         ZERO_STRUCT(encrypted_password);
43
44         mem_ctx = talloc_init("NetJoinDomain");
45         if (!mem_ctx) {
46                 werr = WERR_NOMEM;
47                 goto done;
48         }
49
50         if (!server_name || is_myname_or_ipaddr(server_name)) {
51                 werr = WERR_NOT_SUPPORTED;
52                 goto done;
53         }
54
55         if (!domain_name) {
56                 werr = WERR_INVALID_PARAM;
57                 goto done;
58         }
59
60         status = cli_full_connection(&cli, NULL, server_name,
61                                      NULL, 0,
62                                      "IPC$", "IPC",
63                                      opt_user_name, opt_workgroup,
64                                      opt_password, 0, Undefined, NULL);
65
66         if (!NT_STATUS_IS_OK(status)) {
67                 werr = ntstatus_to_werror(status);
68                 goto done;
69         }
70
71         old_timeout = cli_set_timeout(cli, 60000);
72
73         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
74                                             &status);
75         if (!pipe_cli) {
76                 werr = ntstatus_to_werror(status);
77                 goto done;
78         };
79
80         if (password) {
81                 encode_wkssvc_join_password_buffer(mem_ctx,
82                                                    password,
83                                                    &cli->user_session_key,
84                                                    &encrypted_password);
85         }
86
87         old_timeout = cli_set_timeout(cli, 60000);
88
89         status = rpccli_wkssvc_NetrJoinDomain2(pipe_cli, mem_ctx,
90                                                server_name, domain_name,
91                                                account_ou, Account,
92                                                &encrypted_password,
93                                                join_flags, &werr);
94         if (!NT_STATUS_IS_OK(status)) {
95                 werr = ntstatus_to_werror(status);
96                 goto done;
97         }
98
99  done:
100         if (cli) {
101                 cli_set_timeout(cli, old_timeout);
102                 cli_shutdown(cli);
103         }
104         TALLOC_FREE(mem_ctx);
105
106         return werr;
107 }
108
109 WERROR NetUnjoinDomain(const char *server_name,
110                        const char *account,
111                        const char *password,
112                        uint32_t unjoin_flags)
113 {
114         TALLOC_CTX *mem_ctx = NULL;
115         struct cli_state *cli = NULL;
116         struct rpc_pipe_client *pipe_cli = NULL;
117         struct wkssvc_PasswordBuffer encrypted_password;
118         NTSTATUS status;
119         WERROR werr;
120         unsigned int old_timeout = 0;
121
122         ZERO_STRUCT(encrypted_password);
123
124         mem_ctx = talloc_init("NetUnjoinDomain");
125         if (!mem_ctx) {
126                 werr = WERR_NOMEM;
127                 goto done;
128         }
129
130         if (!server_name || is_myname_or_ipaddr(server_name)) {
131                 werr = WERR_NOT_SUPPORTED;
132                 goto done;
133         }
134
135         status = cli_full_connection(&cli, NULL, server_name,
136                                      NULL, 0,
137                                      "IPC$", "IPC",
138                                      opt_user_name, opt_workgroup,
139                                      opt_password, 0, Undefined, NULL);
140
141         if (!NT_STATUS_IS_OK(status)) {
142                 werr = ntstatus_to_werror(status);
143                 goto done;
144         }
145
146         old_timeout = cli_set_timeout(cli, 60000);
147
148         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
149                                             &status);
150         if (!pipe_cli) {
151                 werr = ntstatus_to_werror(status);
152                 goto done;
153         };
154
155         if (password) {
156                 encode_wkssvc_join_password_buffer(mem_ctx,
157                                                    password,
158                                                    &cli->user_session_key,
159                                                    &encrypted_password);
160         }
161
162         old_timeout = cli_set_timeout(cli, 60000);
163
164         status = rpccli_wkssvc_NetrUnjoinDomain2(pipe_cli, mem_ctx,
165                                                  server_name,
166                                                  account,
167                                                  &encrypted_password,
168                                                  unjoin_flags,
169                                                  &werr);
170         if (!NT_STATUS_IS_OK(status)) {
171                 werr = ntstatus_to_werror(status);
172                 goto done;
173         }
174
175  done:
176         if (cli) {
177                 cli_set_timeout(cli, old_timeout);
178                 cli_shutdown(cli);
179         }
180         TALLOC_FREE(mem_ctx);
181
182         return werr;
183 }
184
185 WERROR NetGetJoinInformation(const char *server_name,
186                              const char **name_buffer,
187                              uint16_t *name_type)
188 {
189         TALLOC_CTX *mem_ctx = NULL;
190         struct cli_state *cli = NULL;
191         struct rpc_pipe_client *pipe_cli = NULL;
192         NTSTATUS status;
193         WERROR werr;
194
195         mem_ctx = talloc_init("NetGetJoinInformation");
196         if (!mem_ctx) {
197                 werr = WERR_NOMEM;
198                 goto done;
199         }
200
201         status = cli_full_connection(&cli, NULL, server_name,
202                                      NULL, 0,
203                                      "IPC$", "IPC",
204                                      opt_user_name, opt_workgroup,
205                                      opt_password, 0, Undefined, NULL);
206
207         if (!NT_STATUS_IS_OK(status)) {
208                 werr = ntstatus_to_werror(status);
209                 goto done;
210         }
211
212         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
213                                             &status);
214         if (!pipe_cli) {
215                 werr = ntstatus_to_werror(status);
216                 goto done;
217         };
218
219         status = rpccli_wkssvc_NetrGetJoinInformation(pipe_cli, mem_ctx,
220                                                       server_name,
221                                                       name_buffer,
222                                                       (enum wkssvc_NetJoinStatus *)name_type,
223                                                       &werr);
224         if (!NT_STATUS_IS_OK(status)) {
225                 werr = ntstatus_to_werror(status);
226                 goto done;
227         }
228
229  done:
230         if (cli) {
231                 cli_shutdown(cli);
232         }
233         TALLOC_FREE(mem_ctx);
234
235         return werr;
236 }