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