Change new style join function name for clarity in net_rpc.c
[tprouty/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", get_nt_error_msg(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  * Join a domain using the administrator username and password
40  *
41  * @param argc  Standard main() style argc
42  * @param argc  Standard main() style argv.  Initial components are already
43  *              stripped.  Currently not used.
44  * @return A shell status integer (0 for success)
45  *
46  **/
47
48 int net_rpc_join_newstyle(int argc, const char **argv) 
49 {
50
51         extern pstring global_myname;
52
53         /* libsmb variables */
54
55         struct cli_state *cli;
56         fstring acct_name;
57         TALLOC_CTX *mem_ctx;
58         uint32 acb_info;
59
60         /* rpc variables */
61
62         POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
63         DOM_SID domain_sid;
64         uint32 user_rid;
65
66         /* Password stuff */
67
68         char *clear_trust_password = NULL;
69         fstring ucs2_trust_password;
70         int ucs2_pw_len;
71         uchar stored_md4_trust_password[16];
72         uchar pwbuf[516], sess_key[16];
73         SAM_USERINFO_CTR ctr;
74         SAM_USER_INFO_24 p24;
75         SAM_USER_INFO_10 p10;
76
77         /* Misc */
78
79         NTSTATUS result;
80         int retval = 1;
81         fstring domain;
82         uint32 num_rids, *name_types, *user_rids;
83         uint32 flags = 0x3e8;
84         char *names;
85         
86         /* Connect to remote machine */
87
88         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) 
89                 return 1;
90
91         if (!(mem_ctx = talloc_init())) {
92                 DEBUG(0, ("Could not initialise talloc context\n"));
93                 goto done;
94         }
95
96         /* Fetch domain sid */
97
98         if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
99                 DEBUG(0, ("Error connecting to SAM pipe\n"));
100                 goto done;
101         }
102
103
104         CHECK_RPC_ERR(cli_lsa_open_policy(cli, mem_ctx, True,
105                                           SEC_RIGHTS_MAXIMUM_ALLOWED,
106                                           &lsa_pol),
107                       "error opening lsa policy handle");
108
109         CHECK_RPC_ERR(cli_lsa_query_info_policy(cli, mem_ctx, &lsa_pol,
110                                                 5, domain, &domain_sid),
111                       "error querying info policy");
112
113         cli_lsa_close(cli, mem_ctx, &lsa_pol);
114
115         cli_nt_session_close(cli); /* Done with this pipe */
116
117         /* Create domain user */
118         if (!cli_nt_session_open(cli, PIPE_SAMR)) {
119                 DEBUG(0, ("Error connecting to SAM pipe\n"));
120                 goto done;
121         }
122
123         CHECK_RPC_ERR(cli_samr_connect(cli, mem_ctx, 
124                                        SEC_RIGHTS_MAXIMUM_ALLOWED,
125                                        &sam_pol),
126                       "could not connect to SAM database");
127
128         
129         CHECK_RPC_ERR(cli_samr_open_domain(cli, mem_ctx, &sam_pol,
130                                            SEC_RIGHTS_MAXIMUM_ALLOWED,
131                                            &domain_sid, &domain_pol),
132                       "could not open domain");
133
134         /* Create domain user */
135         fstrcpy(acct_name, global_myname);
136         fstrcat(acct_name, "$");
137         strlower(acct_name);
138
139         acb_info = (lp_server_role() == ROLE_DOMAIN_BDC) ? ACB_SVRTRUST : ACB_WSTRUST;
140
141         result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
142                                           acct_name, acb_info,
143                                           0xe005000b, &user_pol, 
144                                           &user_rid);
145
146         if (!NT_STATUS_IS_OK(result) && 
147             !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
148                 d_printf("Create of workstation account failed\n");
149
150                 /* If NT_STATUS_ACCESS_DENIED then we have a valid
151                    username/password combo but the user does not have
152                    administrator access. */
153
154                 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
155                         d_printf("User specified does not have administrator privileges\n");
156
157                 goto done;
158         }
159
160         /* We *must* do this.... don't ask... */
161
162         if (NT_STATUS_IS_OK(result))
163                 cli_samr_close(cli, mem_ctx, &user_pol);
164
165         names = (char *)&acct_name[0];
166
167         CHECK_RPC_ERR_DEBUG(cli_samr_lookup_names(cli, mem_ctx,
168                                                   &domain_pol, flags,
169                                                   1, &names, &num_rids,
170                                                   &user_rids, &name_types),
171                             ("error looking up rid for user %s: %s\n",
172                              acct_name, get_nt_error_msg(result)));
173
174         if (name_types[0] != SID_NAME_USER) {
175                 DEBUG(0, ("%s is not a user account\n", acct_name));
176                 goto done;
177         }
178
179         user_rid = user_rids[0];
180                 
181         /* Open handle on user */
182
183         CHECK_RPC_ERR_DEBUG(
184                 cli_samr_open_user(cli, mem_ctx, &domain_pol,
185                                    SEC_RIGHTS_MAXIMUM_ALLOWED,
186                                    user_rid, &user_pol),
187                 ("could not re-open existing user %s: %s\n",
188                  acct_name, get_nt_error_msg(result)));
189         
190         /* Create a random machine account password */
191
192         { 
193                 char *str;
194                 str = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
195                 clear_trust_password = strdup(str);
196         }
197
198         ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password, 
199                                 clear_trust_password, 
200                                 sizeof(ucs2_trust_password), 0);
201                   
202         encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
203                          ucs2_pw_len);
204
205         /* Set password on machine account */
206
207         ZERO_STRUCT(ctr);
208         ZERO_STRUCT(p24);
209
210         init_sam_user_info24(&p24, (char *)pwbuf,24);
211
212         ctr.switch_value = 24;
213         ctr.info.id24 = &p24;
214
215         /* I don't think this is quite the right place for this
216            calculation.  It should be moved somewhere where the credentials
217            are calculated. )-: */
218
219         mdfour(sess_key, cli->pwd.smb_nt_pwd, 16);
220
221         CHECK_RPC_ERR(cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24, 
222                                             sess_key, &ctr),
223                       "error setting trust account password");
224
225         /* Why do we have to try to (re-)set the ACB to be the same as what
226            we passed in the samr_create_dom_user() call?  When a NT
227            workstation is joined to a domain by an administrator the
228            acb_info is set to 0x80.  For a normal user with "Add
229            workstations to the domain" rights the acb_info is 0x84.  I'm
230            not sure whether it is supposed to make a difference or not.  NT
231            seems to cope with either value so don't bomb out if the set
232            userinfo2 level 0x10 fails.  -tpot */
233
234         ZERO_STRUCT(ctr);
235         ctr.switch_value = 0x10;
236         ctr.info.id10 = &p10;
237
238         init_sam_user_info10(&p10, acb_info);
239
240         /* Ignoring the return value is necessary for joining a domain
241            as a normal user with "Add workstation to domain" privilege. */
242
243         result = cli_samr_set_userinfo2(cli, mem_ctx, &user_pol, 0x10, 
244                                         sess_key, &ctr);
245
246         /* Now store the secret in the secrets database */
247
248         strupper(domain);
249
250         if (!secrets_store_domain_sid(domain, &domain_sid)) {
251                 DEBUG(0, ("error storing domain sid for %s\n", domain));
252                 goto done;
253         }
254
255         if (!secrets_store_machine_password(clear_trust_password)) {
256                 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
257         }
258
259         /* Now check the whole process from top-to-bottom */
260
261         cli_samr_close(cli, mem_ctx, &user_pol);
262
263         cli_nt_session_close(cli); /* Done with this pipe */
264
265         if (!cli_nt_session_open(cli, PIPE_NETLOGON)) {
266                 DEBUG(0, ("Error connecting to NETLOGON pipe\n"));
267                 goto done;
268         }
269
270         if (!secrets_fetch_trust_account_password(domain, 
271                                                   stored_md4_trust_password, NULL)) {
272                 DEBUG(0, ("Could not reterive secrets we just stored!"));
273                 goto done;
274         }
275         
276         CHECK_RPC_ERR(new_cli_nt_setup_creds(cli, 
277                                              (acb_info & ACB_SVRTRUST) ? SEC_CHAN_BDC : SEC_CHAN_WKSTA, 
278                                              stored_md4_trust_password),
279                           "error in domain join verification");
280         
281         retval = 0;             /* Success! */
282         
283 done:
284         /* Close down pipe - this will clean up open policy handles */
285
286         if (cli->nt_pipe_fnum)
287                 cli_nt_session_close(cli);
288
289         /* Display success or failure */
290
291         if (retval != 0) {
292                 trust_password_delete(domain);
293                 fprintf(stderr,"Unable to join domain %s.\n",domain);
294         } else {
295                 printf("Joined domain %s.\n",domain);
296         }
297         
298         cli_shutdown(cli);
299
300         SAFE_FREE(clear_trust_password);
301
302         return retval;
303 }