s3:utils: Remove pointless if-clause for remote_machine
[samba.git] / source3 / utils / net_util.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Helper routines for net
4  *  Copyright (C) Volker Lendecke 2006
5  *  Copyright (C) Kai Blin 2008
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 3 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, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "includes.h"
23 #include "utils/net.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "../librpc/gen_ndr/ndr_lsa_c.h"
26 #include "rpc_client/cli_lsarpc.h"
27 #include "../librpc/gen_ndr/ndr_dssetup_c.h"
28 #include "secrets.h"
29 #include "../libcli/security/security.h"
30 #include "libsmb/libsmb.h"
31
32 NTSTATUS net_rpc_lookup_name(struct net_context *c,
33                              TALLOC_CTX *mem_ctx, struct cli_state *cli,
34                              const char *name, const char **ret_domain,
35                              const char **ret_name, struct dom_sid *ret_sid,
36                              enum lsa_SidType *ret_type)
37 {
38         struct rpc_pipe_client *lsa_pipe = NULL;
39         struct policy_handle pol;
40         NTSTATUS status, result;
41         const char **dom_names;
42         struct dom_sid *sids;
43         enum lsa_SidType *types;
44         struct dcerpc_binding_handle *b;
45
46         ZERO_STRUCT(pol);
47
48         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
49                                           &lsa_pipe);
50         if (!NT_STATUS_IS_OK(status)) {
51                 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
52                 return status;
53         }
54
55         b = lsa_pipe->binding_handle;
56
57         status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
58                                         SEC_FLAG_MAXIMUM_ALLOWED,
59                                         &pol);
60         if (!NT_STATUS_IS_OK(status)) {
61                 d_fprintf(stderr, "open_policy %s: %s\n", _("failed"),
62                           nt_errstr(status));
63                 return status;
64         }
65
66         status = rpccli_lsa_lookup_names(lsa_pipe, mem_ctx, &pol, 1,
67                                          &name, &dom_names, 1, &sids, &types);
68
69         if (!NT_STATUS_IS_OK(status)) {
70                 /* This can happen easily, don't log an error */
71                 goto done;
72         }
73
74         if (ret_domain != NULL) {
75                 *ret_domain = dom_names[0];
76         }
77         if (ret_name != NULL) {
78                 *ret_name = talloc_strdup(mem_ctx, name);
79         }
80         if (ret_sid != NULL) {
81                 sid_copy(ret_sid, &sids[0]);
82         }
83         if (ret_type != NULL) {
84                 *ret_type = types[0];
85         }
86
87  done:
88         if (is_valid_policy_hnd(&pol)) {
89                 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
90         }
91         TALLOC_FREE(lsa_pipe);
92
93         return status;
94 }
95
96 /****************************************************************************
97  Connect to \\server\service.
98 ****************************************************************************/
99
100 NTSTATUS connect_to_service(struct net_context *c,
101                             struct cli_state **cli_ctx,
102                             const struct sockaddr_storage *server_ss,
103                             const char *server_name,
104                             const char *service_name,
105                             const char *service_type)
106 {
107         NTSTATUS nt_status;
108         int flags = 0;
109         enum smb_signing_setting signing_setting = SMB_SIGNING_DEFAULT;
110
111         c->opt_password = net_prompt_pass(c, c->opt_user_name);
112
113         if (c->opt_kerberos) {
114                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
115         }
116
117         if (c->opt_kerberos && c->opt_password) {
118                 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
119         }
120
121         if (c->opt_ccache) {
122                 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
123         }
124
125         if (strequal(service_type, "IPC")) {
126                 signing_setting = SMB_SIGNING_IPC_DEFAULT;
127         }
128
129         nt_status = cli_full_connection(cli_ctx, NULL, server_name,
130                                         server_ss, c->opt_port,
131                                         service_name, service_type,
132                                         c->opt_user_name, c->opt_workgroup,
133                                         c->opt_password, flags,
134                                         signing_setting);
135         if (!NT_STATUS_IS_OK(nt_status)) {
136                 d_fprintf(stderr, _("Could not connect to server %s\n"),
137                           server_name);
138
139                 /* Display a nicer message depending on the result */
140
141                 if (NT_STATUS_V(nt_status) ==
142                     NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
143                         d_fprintf(stderr,
144                                   _("The username or password was not "
145                                     "correct.\n"));
146
147                 if (NT_STATUS_V(nt_status) ==
148                     NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
149                         d_fprintf(stderr, _("The account was locked out.\n"));
150
151                 if (NT_STATUS_V(nt_status) ==
152                     NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
153                         d_fprintf(stderr, _("The account was disabled.\n"));
154                 return nt_status;
155         }
156
157         if (c->smb_encrypt) {
158                 nt_status = cli_cm_force_encryption(*cli_ctx,
159                                                     c->opt_user_name,
160                                                     c->opt_password,
161                                                     c->opt_workgroup,
162                                                     service_name);
163                 if (!NT_STATUS_IS_OK(nt_status)) {
164                         cli_shutdown(*cli_ctx);
165                         *cli_ctx = NULL;
166                 }
167         }
168
169         return nt_status;
170 }
171
172 /****************************************************************************
173  Connect to \\server\ipc$.
174 ****************************************************************************/
175
176 NTSTATUS connect_to_ipc(struct net_context *c,
177                         struct cli_state **cli_ctx,
178                         const struct sockaddr_storage *server_ss,
179                         const char *server_name)
180 {
181         return connect_to_service(c, cli_ctx, server_ss, server_name, "IPC$",
182                                   "IPC");
183 }
184
185 /****************************************************************************
186  Connect to \\server\ipc$ anonymously.
187 ****************************************************************************/
188
189 NTSTATUS connect_to_ipc_anonymous(struct net_context *c,
190                                 struct cli_state **cli_ctx,
191                                 const struct sockaddr_storage *server_ss,
192                                 const char *server_name)
193 {
194         NTSTATUS nt_status;
195
196         nt_status = cli_full_connection(cli_ctx, c->opt_requester_name,
197                                         server_name, server_ss, c->opt_port,
198                                         "IPC$", "IPC",
199                                         "", "",
200                                         "", 0, SMB_SIGNING_DEFAULT);
201
202         if (NT_STATUS_IS_OK(nt_status)) {
203                 return nt_status;
204         } else {
205                 DEBUG(1,("Cannot connect to server (anonymously).  Error was %s\n", nt_errstr(nt_status)));
206                 return nt_status;
207         }
208 }
209
210 /**
211  * Connect a server and open a given pipe
212  *
213  * @param cli_dst               A cli_state
214  * @param pipe                  The pipe to open
215  * @param got_pipe              boolean that stores if we got a pipe
216  *
217  * @return Normal NTSTATUS return.
218  **/
219 NTSTATUS connect_dst_pipe(struct net_context *c, struct cli_state **cli_dst,
220                           struct rpc_pipe_client **pp_pipe_hnd,
221                           const struct ndr_interface_table *table)
222 {
223         NTSTATUS nt_status;
224         char *server_name = SMB_STRDUP("127.0.0.1");
225         struct cli_state *cli_tmp = NULL;
226         struct rpc_pipe_client *pipe_hnd = NULL;
227
228         if (server_name == NULL) {
229                 return NT_STATUS_NO_MEMORY;
230         }
231
232         if (c->opt_destination) {
233                 SAFE_FREE(server_name);
234                 if ((server_name = SMB_STRDUP(c->opt_destination)) == NULL) {
235                         return NT_STATUS_NO_MEMORY;
236                 }
237         }
238
239         /* make a connection to a named pipe */
240         nt_status = connect_to_ipc(c, &cli_tmp, NULL, server_name);
241         if (!NT_STATUS_IS_OK(nt_status)) {
242                 SAFE_FREE(server_name);
243                 return nt_status;
244         }
245
246         nt_status = cli_rpc_pipe_open_noauth(cli_tmp, table,
247                                              &pipe_hnd);
248         if (!NT_STATUS_IS_OK(nt_status)) {
249                 DEBUG(0, ("couldn't not initialize pipe\n"));
250                 cli_shutdown(cli_tmp);
251                 SAFE_FREE(server_name);
252                 return nt_status;
253         }
254
255         *cli_dst = cli_tmp;
256         *pp_pipe_hnd = pipe_hnd;
257         SAFE_FREE(server_name);
258
259         return nt_status;
260 }
261
262 /****************************************************************************
263  Use the local machine account (krb) and password for this session.
264 ****************************************************************************/
265
266 int net_use_krb_machine_account(struct net_context *c)
267 {
268         char *user_name = NULL;
269
270         if (!secrets_init()) {
271                 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
272                 exit(1);
273         }
274
275         c->opt_password = secrets_fetch_machine_password(
276                                 c->opt_target_workgroup, NULL, NULL);
277         if (asprintf(&user_name, "%s$@%s", lp_netbios_name(), lp_realm()) == -1) {
278                 return -1;
279         }
280         c->opt_user_name = user_name;
281         return 0;
282 }
283
284 /****************************************************************************
285  Use the machine account name and password for this session.
286 ****************************************************************************/
287
288 int net_use_machine_account(struct net_context *c)
289 {
290         char *user_name = NULL;
291
292         if (!secrets_init()) {
293                 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
294                 exit(1);
295         }
296
297         c->opt_password = secrets_fetch_machine_password(
298                                 c->opt_target_workgroup, NULL, NULL);
299         if (asprintf(&user_name, "%s$", lp_netbios_name()) == -1) {
300                 return -1;
301         }
302         c->opt_user_name = user_name;
303         return 0;
304 }
305
306 bool net_find_server(struct net_context *c,
307                         const char *domain,
308                         unsigned flags,
309                         struct sockaddr_storage *server_ss,
310                         char **server_name)
311 {
312         const char *d = domain ? domain : c->opt_target_workgroup;
313
314         if (c->opt_host) {
315                 *server_name = SMB_STRDUP(c->opt_host);
316         }
317
318         if (c->opt_have_ip) {
319                 *server_ss = c->opt_dest_ip;
320                 if (!*server_name) {
321                         char addr[INET6_ADDRSTRLEN];
322                         print_sockaddr(addr, sizeof(addr), &c->opt_dest_ip);
323                         *server_name = SMB_STRDUP(addr);
324                 }
325         } else if (*server_name) {
326                 /* resolve the IP address */
327                 if (!resolve_name(*server_name, server_ss, 0x20, false))  {
328                         DEBUG(1,("Unable to resolve server name\n"));
329                         return false;
330                 }
331         } else if (flags & NET_FLAGS_PDC) {
332                 fstring dc_name;
333                 struct sockaddr_storage pdc_ss;
334
335                 if (!get_pdc_ip(d, &pdc_ss)) {
336                         DEBUG(1,("Unable to resolve PDC server address\n"));
337                         return false;
338                 }
339
340                 if (is_zero_addr(&pdc_ss)) {
341                         return false;
342                 }
343
344                 if (!name_status_find(d, 0x1b, 0x20, &pdc_ss, dc_name)) {
345                         return false;
346                 }
347
348                 *server_name = SMB_STRDUP(dc_name);
349                 *server_ss = pdc_ss;
350         } else if (flags & NET_FLAGS_DMB) {
351                 struct sockaddr_storage msbrow_ss;
352                 char addr[INET6_ADDRSTRLEN];
353
354                 /*  if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
355                 if (!resolve_name(d, &msbrow_ss, 0x1B, false))  {
356                         DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
357                         return false;
358                 }
359                 *server_ss = msbrow_ss;
360                 print_sockaddr(addr, sizeof(addr), server_ss);
361                 *server_name = SMB_STRDUP(addr);
362         } else if (flags & NET_FLAGS_MASTER) {
363                 struct sockaddr_storage brow_ss;
364                 char addr[INET6_ADDRSTRLEN];
365                 if (!resolve_name(d, &brow_ss, 0x1D, false))  {
366                                 /* go looking for workgroups */
367                         DEBUG(1,("Unable to resolve master browser via name lookup\n"));
368                         return false;
369                 }
370                 *server_ss = brow_ss;
371                 print_sockaddr(addr, sizeof(addr), server_ss);
372                 *server_name = SMB_STRDUP(addr);
373         } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
374                 if (!interpret_string_addr(server_ss,
375                                         "127.0.0.1", AI_NUMERICHOST)) {
376                         DEBUG(1,("Unable to resolve 127.0.0.1\n"));
377                         return false;
378                 }
379                 *server_name = SMB_STRDUP("127.0.0.1");
380         }
381
382         if (!*server_name) {
383                 DEBUG(1,("no server to connect to\n"));
384                 return false;
385         }
386
387         return true;
388 }
389
390 bool net_find_pdc(struct sockaddr_storage *server_ss,
391                 fstring server_name,
392                 const char *domain_name)
393 {
394         if (!get_pdc_ip(domain_name, server_ss)) {
395                 return false;
396         }
397         if (is_zero_addr(server_ss)) {
398                 return false;
399         }
400
401         if (!name_status_find(domain_name, 0x1b, 0x20, server_ss, server_name)) {
402                 return false;
403         }
404
405         return true;
406 }
407
408 NTSTATUS net_make_ipc_connection(struct net_context *c, unsigned flags,
409                                  struct cli_state **pcli)
410 {
411         return net_make_ipc_connection_ex(c, c->opt_workgroup, NULL, NULL, flags, pcli);
412 }
413
414 NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
415                                     const char *server,
416                                     const struct sockaddr_storage *pss,
417                                     unsigned flags, struct cli_state **pcli)
418 {
419         char *server_name = NULL;
420         struct sockaddr_storage server_ss;
421         struct cli_state *cli = NULL;
422         NTSTATUS nt_status;
423
424         if ( !server || !pss ) {
425                 if (!net_find_server(c, domain, flags, &server_ss,
426                                      &server_name)) {
427                         d_fprintf(stderr, _("Unable to find a suitable server "
428                                 "for domain %s\n"), domain);
429                         nt_status = NT_STATUS_UNSUCCESSFUL;
430                         goto done;
431                 }
432         } else {
433                 server_name = SMB_STRDUP( server );
434                 server_ss = *pss;
435         }
436
437         if (flags & NET_FLAGS_ANONYMOUS) {
438                 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
439                                                      server_name);
440         } else {
441                 nt_status = connect_to_ipc(c, &cli, &server_ss,
442                                            server_name);
443         }
444
445         /* store the server in the affinity cache if it was a PDC */
446
447         if ( (flags & NET_FLAGS_PDC) && NT_STATUS_IS_OK(nt_status) )
448                 saf_store(cli->server_domain, server_name);
449
450         SAFE_FREE(server_name);
451         if (!NT_STATUS_IS_OK(nt_status)) {
452                 d_fprintf(stderr, _("Connection failed: %s\n"),
453                           nt_errstr(nt_status));
454                 cli = NULL;
455         } else if (c->opt_request_timeout) {
456                 cli_set_timeout(cli, c->opt_request_timeout * 1000);
457         }
458
459 done:
460         if (pcli != NULL) {
461                 *pcli = cli;
462         }
463         return nt_status;
464 }
465
466 /****************************************************************************
467 ****************************************************************************/
468
469 const char *net_prompt_pass(struct net_context *c, const char *user)
470 {
471         char *prompt = NULL;
472         char pwd[256] = {0};
473         int rc;
474
475         if (c->opt_password) {
476                 return c->opt_password;
477         }
478
479         if (c->opt_machine_pass) {
480                 return NULL;
481         }
482
483         if (c->opt_kerberos && !c->opt_user_specified) {
484                 return NULL;
485         }
486
487         if (asprintf(&prompt, _("Enter %s's password:"), user) == -1) {
488                 return NULL;
489         }
490
491         rc = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
492         SAFE_FREE(prompt);
493         if (rc < 0) {
494                 return NULL;
495         }
496
497         return SMB_STRDUP(pwd);
498 }
499
500 int net_run_function(struct net_context *c, int argc, const char **argv,
501                       const char *whoami, struct functable *table)
502 {
503         int i;
504
505         if (argc != 0) {
506                 for (i=0; table[i].funcname != NULL; i++) {
507                         if (strcasecmp_m(argv[0], table[i].funcname) == 0)
508                                 return table[i].fn(c, argc-1, argv+1);
509                 }
510         }
511
512         if (c->display_usage == false) {
513                 d_fprintf(stderr, _("Invalid command: %s %s\n"), whoami,
514                           (argc > 0)?argv[0]:"");
515         }
516         d_printf(_("Usage:\n"));
517         for (i=0; table[i].funcname != NULL; i++) {
518                 if(c->display_usage == false)
519                         d_printf("%s %-15s %s\n", whoami, table[i].funcname,
520                                  _(table[i].description));
521                 else
522                         d_printf("%s\n", _(table[i].usage));
523         }
524
525         return c->display_usage?0:-1;
526 }
527
528 void net_display_usage_from_functable(struct functable *table)
529 {
530         int i;
531         for (i=0; table[i].funcname != NULL; i++) {
532                 d_printf("%s\n", _(table[i].usage));
533         }
534 }
535
536 const char *net_share_type_str(int num_type)
537 {
538         switch(num_type) {
539                 case 0: return _("Disk");
540                 case 1: return _("Print");
541                 case 2: return _("Dev");
542                 case 3: return _("IPC");
543                 default: return _("Unknown");
544         }
545 }
546
547 static NTSTATUS net_scan_dc_noad(struct net_context *c,
548                                  struct cli_state *cli,
549                                  struct net_dc_info *dc_info)
550 {
551         TALLOC_CTX *mem_ctx = talloc_tos();
552         struct rpc_pipe_client *pipe_hnd = NULL;
553         struct dcerpc_binding_handle *b;
554         NTSTATUS status, result;
555         struct policy_handle pol;
556         union lsa_PolicyInformation *info;
557
558         ZERO_STRUCTP(dc_info);
559         ZERO_STRUCT(pol);
560
561         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
562                                           &pipe_hnd);
563         if (!NT_STATUS_IS_OK(status)) {
564                 return status;
565         }
566
567         b = pipe_hnd->binding_handle;
568
569         status = dcerpc_lsa_open_policy(b, mem_ctx,
570                                         false,
571                                         SEC_FLAG_MAXIMUM_ALLOWED,
572                                         &pol,
573                                         &result);
574         if (!NT_STATUS_IS_OK(status)) {
575                 goto done;
576         }
577         if (!NT_STATUS_IS_OK(result)) {
578                 status = result;
579                 goto done;
580         }
581
582         status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
583                                             &pol,
584                                             LSA_POLICY_INFO_ACCOUNT_DOMAIN,
585                                             &info,
586                                             &result);
587         if (!NT_STATUS_IS_OK(status)) {
588                 goto done;
589         }
590         if (!NT_STATUS_IS_OK(result)) {
591                 status = result;
592                 goto done;
593         }
594
595         dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info->account_domain.name.string);
596         if (dc_info->netbios_domain_name == NULL) {
597                 status = NT_STATUS_NO_MEMORY;
598                 goto done;
599         }
600
601  done:
602         if (is_valid_policy_hnd(&pol)) {
603                 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
604         }
605
606         TALLOC_FREE(pipe_hnd);
607
608         return status;
609 }
610
611 NTSTATUS net_scan_dc(struct net_context *c,
612                      struct cli_state *cli,
613                      struct net_dc_info *dc_info)
614 {
615         TALLOC_CTX *mem_ctx = talloc_tos();
616         struct rpc_pipe_client *dssetup_pipe = NULL;
617         struct dcerpc_binding_handle *dssetup_handle = NULL;
618         union dssetup_DsRoleInfo info;
619         NTSTATUS status;
620         WERROR werr;
621
622         ZERO_STRUCTP(dc_info);
623
624         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_dssetup,
625                                           &dssetup_pipe);
626         if (!NT_STATUS_IS_OK(status)) {
627                 DEBUG(10,("net_scan_dc: failed to open dssetup pipe with %s, "
628                         "retrying with lsa pipe\n", nt_errstr(status)));
629                 return net_scan_dc_noad(c, cli, dc_info);
630         }
631         dssetup_handle = dssetup_pipe->binding_handle;
632
633         status = dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_handle, mem_ctx,
634                                                                   DS_ROLE_BASIC_INFORMATION,
635                                                                   &info,
636                                                                   &werr);
637         TALLOC_FREE(dssetup_pipe);
638
639         if (NT_STATUS_IS_OK(status)) {
640                 status = werror_to_ntstatus(werr);
641         }
642         if (!NT_STATUS_IS_OK(status)) {
643                 return status;
644         }
645
646         dc_info->is_dc  = (info.basic.role & (DS_ROLE_PRIMARY_DC|DS_ROLE_BACKUP_DC));
647         dc_info->is_pdc = (info.basic.role & DS_ROLE_PRIMARY_DC);
648         dc_info->is_ad  = (info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING);
649         dc_info->is_mixed_mode = (info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE);
650         dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info.basic.domain);
651         dc_info->dns_domain_name = talloc_strdup(mem_ctx, info.basic.dns_domain);
652         dc_info->forest_name = talloc_strdup(mem_ctx, info.basic.forest);
653
654         return NT_STATUS_OK;
655 }