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