This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[kai/samba.git] / source / utils / net_rpc_join.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4    Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5    Copyright (C) Tim Potter     2001
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20  
21 #include "includes.h"
22 #include "../utils/net.h"
23
24 /* Macro for checking RPC error codes to make things more readable */
25
26 #define CHECK_RPC_ERR(rpc, msg) \
27         if (!NT_STATUS_IS_OK(result = rpc)) { \
28                 DEBUG(0, (msg ": %s\n", nt_errstr(result))); \
29                 goto done; \
30         }
31
32 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
33         if (!NT_STATUS_IS_OK(result = rpc)) { \
34                 DEBUG(0, debug_args); \
35                 goto done; \
36         }
37
38
39 /**
40  * confirm that a domain join is still valid
41  *
42  * @return A shell status integer (0 for success)
43  *
44  **/
45 int net_rpc_join_ok(const char *domain)
46 {
47         struct cli_state *cli;
48         uchar stored_md4_trust_password[16];
49         int retval = 1;
50         uint32 channel;
51         NTSTATUS result;
52
53         /* Connect to remote machine */
54         if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC))) {
55                 return 1;
56         }
57
58         if (!cli_nt_session_open(cli, PIPE_NETLOGON)) {
59                 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
60                 goto done;
61         }
62
63         if (!secrets_fetch_trust_account_password(domain,
64                                                   stored_md4_trust_password, NULL)) {
65                 DEBUG(0,("Could not reterive domain trust secret"));
66                 goto done;
67         }
68         
69         if (lp_server_role() == ROLE_DOMAIN_BDC || 
70             lp_server_role() == ROLE_DOMAIN_PDC) {
71                 channel = SEC_CHAN_BDC;
72         } else {
73                 channel = SEC_CHAN_WKSTA;
74         }
75
76         CHECK_RPC_ERR(cli_nt_setup_creds(cli, 
77                                          channel,
78                                          stored_md4_trust_password),
79                           "error in domain join verification");
80         
81         retval = 0;             /* Success! */
82         
83 done:
84         /* Close down pipe - this will clean up open policy handles */
85         if (cli->nt_pipe_fnum)
86                 cli_nt_session_close(cli);
87
88         cli_shutdown(cli);
89
90         return retval;
91 }
92
93 /**
94  * Join a domain using the administrator username and password
95  *
96  * @param argc  Standard main() style argc
97  * @param argc  Standard main() style argv.  Initial components are already
98  *              stripped.  Currently not used.
99  * @return A shell status integer (0 for success)
100  *
101  **/
102
103 int net_rpc_join_newstyle(int argc, const char **argv) 
104 {
105
106         extern pstring global_myname;
107
108         /* libsmb variables */
109
110         struct cli_state *cli;
111         TALLOC_CTX *mem_ctx;
112         uint32 acb_info;
113
114         /* rpc variables */
115
116         POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
117         DOM_SID domain_sid;
118         uint32 user_rid;
119
120         /* Password stuff */
121
122         char *clear_trust_password = NULL;
123         fstring ucs2_trust_password;
124         int ucs2_pw_len;
125         uchar pwbuf[516], sess_key[16];
126         SAM_USERINFO_CTR ctr;
127         SAM_USER_INFO_24 p24;
128         SAM_USER_INFO_10 p10;
129
130         /* Misc */
131
132         NTSTATUS result;
133         int retval = 1;
134         fstring domain;
135         uint32 num_rids, *name_types, *user_rids;
136         uint32 flags = 0x3e8;
137         char *acct_name;
138         const char *const_acct_name;
139
140         /* Connect to remote machine */
141
142         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) 
143                 return 1;
144
145         if (!(mem_ctx = talloc_init())) {
146                 DEBUG(0, ("Could not initialise talloc context\n"));
147                 goto done;
148         }
149
150         /* Fetch domain sid */
151
152         if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
153                 DEBUG(0, ("Error connecting to SAM pipe\n"));
154                 goto done;
155         }
156
157
158         CHECK_RPC_ERR(cli_lsa_open_policy(cli, mem_ctx, True,
159                                           SEC_RIGHTS_MAXIMUM_ALLOWED,
160                                           &lsa_pol),
161                       "error opening lsa policy handle");
162
163         CHECK_RPC_ERR(cli_lsa_query_info_policy(cli, mem_ctx, &lsa_pol,
164                                                 5, domain, &domain_sid),
165                       "error querying info policy");
166
167         cli_lsa_close(cli, mem_ctx, &lsa_pol);
168
169         cli_nt_session_close(cli); /* Done with this pipe */
170
171         /* Create domain user */
172         if (!cli_nt_session_open(cli, PIPE_SAMR)) {
173                 DEBUG(0, ("Error connecting to SAM pipe\n"));
174                 goto done;
175         }
176
177         CHECK_RPC_ERR(cli_samr_connect(cli, mem_ctx, 
178                                        SEC_RIGHTS_MAXIMUM_ALLOWED,
179                                        &sam_pol),
180                       "could not connect to SAM database");
181
182         
183         CHECK_RPC_ERR(cli_samr_open_domain(cli, mem_ctx, &sam_pol,
184                                            SEC_RIGHTS_MAXIMUM_ALLOWED,
185                                            &domain_sid, &domain_pol),
186                       "could not open domain");
187
188         /* Create domain user */
189         acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname); 
190         strlower(acct_name);
191         const_acct_name = acct_name;
192
193         acb_info = ((lp_server_role() == ROLE_DOMAIN_BDC) || lp_server_role() == ROLE_DOMAIN_PDC) ? ACB_SVRTRUST : ACB_WSTRUST;
194
195         result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
196                                           acct_name, acb_info,
197                                           0xe005000b, &user_pol, 
198                                           &user_rid);
199
200         if (!NT_STATUS_IS_OK(result) && 
201             !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
202                 d_printf("Create of workstation account failed\n");
203
204                 /* If NT_STATUS_ACCESS_DENIED then we have a valid
205                    username/password combo but the user does not have
206                    administrator access. */
207
208                 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
209                         d_printf("User specified does not have administrator privileges\n");
210
211                 goto done;
212         }
213
214         /* We *must* do this.... don't ask... */
215
216         if (NT_STATUS_IS_OK(result))
217                 cli_samr_close(cli, mem_ctx, &user_pol);
218
219         CHECK_RPC_ERR_DEBUG(cli_samr_lookup_names(cli, mem_ctx,
220                                                   &domain_pol, flags,
221                                                   1, &const_acct_name, 
222                                                   &num_rids,
223                                                   &user_rids, &name_types),
224                             ("error looking up rid for user %s: %s\n",
225                              acct_name, nt_errstr(result)));
226
227         if (name_types[0] != SID_NAME_USER) {
228                 DEBUG(0, ("%s is not a user account\n", acct_name));
229                 goto done;
230         }
231
232         user_rid = user_rids[0];
233                 
234         /* Open handle on user */
235
236         CHECK_RPC_ERR_DEBUG(
237                 cli_samr_open_user(cli, mem_ctx, &domain_pol,
238                                    SEC_RIGHTS_MAXIMUM_ALLOWED,
239                                    user_rid, &user_pol),
240                 ("could not re-open existing user %s: %s\n",
241                  acct_name, nt_errstr(result)));
242         
243         /* Create a random machine account password */
244
245         { 
246                 char *str;
247                 str = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
248                 clear_trust_password = strdup(str);
249         }
250
251         ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password, 
252                                 clear_trust_password, 
253                                 sizeof(ucs2_trust_password), 0);
254                   
255         encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
256                          ucs2_pw_len);
257
258         /* Set password on machine account */
259
260         ZERO_STRUCT(ctr);
261         ZERO_STRUCT(p24);
262
263         init_sam_user_info24(&p24, (char *)pwbuf,24);
264
265         ctr.switch_value = 24;
266         ctr.info.id24 = &p24;
267
268         /* I don't think this is quite the right place for this
269            calculation.  It should be moved somewhere where the credentials
270            are calculated. )-: */
271
272         mdfour(sess_key, cli->pwd.smb_nt_pwd, 16);
273
274         CHECK_RPC_ERR(cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24, 
275                                             sess_key, &ctr),
276                       "error setting trust account password");
277
278         /* Why do we have to try to (re-)set the ACB to be the same as what
279            we passed in the samr_create_dom_user() call?  When a NT
280            workstation is joined to a domain by an administrator the
281            acb_info is set to 0x80.  For a normal user with "Add
282            workstations to the domain" rights the acb_info is 0x84.  I'm
283            not sure whether it is supposed to make a difference or not.  NT
284            seems to cope with either value so don't bomb out if the set
285            userinfo2 level 0x10 fails.  -tpot */
286
287         ZERO_STRUCT(ctr);
288         ctr.switch_value = 0x10;
289         ctr.info.id10 = &p10;
290
291         init_sam_user_info10(&p10, acb_info);
292
293         /* Ignoring the return value is necessary for joining a domain
294            as a normal user with "Add workstation to domain" privilege. */
295
296         result = cli_samr_set_userinfo2(cli, mem_ctx, &user_pol, 0x10, 
297                                         sess_key, &ctr);
298
299         /* Now store the secret in the secrets database */
300
301         strupper(domain);
302
303         if (!secrets_store_domain_sid(domain, &domain_sid)) {
304                 DEBUG(0, ("error storing domain sid for %s\n", domain));
305                 goto done;
306         }
307
308         if (!secrets_store_machine_password(clear_trust_password)) {
309                 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
310         }
311
312         /* Now check the whole process from top-to-bottom */
313         cli_samr_close(cli, mem_ctx, &user_pol);
314         cli_nt_session_close(cli); /* Done with this pipe */
315
316         retval = net_rpc_join_ok(domain);
317         
318 done:
319         /* Close down pipe - this will clean up open policy handles */
320
321         if (cli->nt_pipe_fnum)
322                 cli_nt_session_close(cli);
323
324         /* Display success or failure */
325
326         if (retval != 0) {
327                 trust_password_delete(domain);
328                 fprintf(stderr,"Unable to join domain %s.\n",domain);
329         } else {
330                 printf("Joined domain %s.\n",domain);
331         }
332         
333         cli_shutdown(cli);
334
335         SAFE_FREE(clear_trust_password);
336
337         return retval;
338 }
339
340
341 /**
342  * check that a join is OK
343  *
344  * @return A shell status integer (0 for success)
345  *
346  **/
347 int net_rpc_testjoin(int argc, const char **argv) 
348 {
349         char *domain = lp_workgroup();
350
351         domain = smb_xstrdup(domain);
352
353         /* Display success or failure */
354         if (net_rpc_join_ok(domain) != 0) {
355                 fprintf(stderr,"Join to domain '%s' is not valid\n",domain);
356                 free(domain);
357                 return -1;
358         }
359
360         printf("Join to '%s' is OK\n",domain);
361         free(domain);
362         return 0;
363 }