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