lib/param: Move all enum declarations to lib/param
[samba.git] / source3 / utils / net_ads.c
index f712ddf2204e0343818c7bb423f4c007202c647f..a4f1588e515800550b39c3177fdf82cdeaa10c41 100644 (file)
@@ -28,7 +28,7 @@
 #include "nsswitch/libwbclient/wbclient.h"
 #include "ads.h"
 #include "libads/cldap.h"
-#include "libads/dns.h"
+#include "../lib/addns/dnsquery.h"
 #include "../libds/common/flags.h"
 #include "librpc/gen_ndr/libnet_join.h"
 #include "libnet/libnet_join.h"
@@ -37,6 +37,7 @@
 #include "krb5_env.h"
 #include "../libcli/security/security.h"
 #include "libsmb/libsmb.h"
+#include "lib/param/loadparm.h"
 
 #ifdef HAVE_ADS
 
@@ -915,7 +916,7 @@ static int net_ads_status(struct net_context *c, int argc, const char **argv)
                return -1;
        }
 
-       rc = ads_find_machine_acct(ads, &res, global_myname());
+       rc = ads_find_machine_acct(ads, &res, lp_netbios_name());
        if (!ADS_ERR_OK(rc)) {
                d_fprintf(stderr, _("ads_find_machine_acct: %s\n"), ads_errstr(rc));
                ads_destroy(&ads);
@@ -923,7 +924,7 @@ static int net_ads_status(struct net_context *c, int argc, const char **argv)
        }
 
        if (ads_count_replies(ads, res) == 0) {
-               d_fprintf(stderr, _("No machine account for '%s' found\n"), global_myname());
+               d_fprintf(stderr, _("No machine account for '%s' found\n"), lp_netbios_name());
                ads_destroy(&ads);
                return -1;
        }
@@ -1018,7 +1019,7 @@ static int net_ads_leave(struct net_context *c, int argc, const char **argv)
                goto done;
        }
 
-       /* Based on what we requseted, we shouldn't get here, but if
+       /* Based on what we requested, we shouldn't get here, but if
           we did, it means the secrets were removed, and therefore
           we have left the domain */
        d_fprintf(stderr, _("Machine '%s' Left domain '%s'\n"),
@@ -1100,10 +1101,10 @@ static WERROR check_ads_config( void )
                return WERR_INVALID_DOMAIN_ROLE;
        }
 
-       if (strlen(global_myname()) > 15) {
+       if (strlen(lp_netbios_name()) > 15) {
                d_printf(_("Our netbios name can be at most 15 chars long, "
-                          "\"%s\" is %u chars long\n"), global_myname(),
-                        (unsigned int)strlen(global_myname()));
+                          "\"%s\" is %u chars long\n"), lp_netbios_name(),
+                        (unsigned int)strlen(lp_netbios_name()));
                return WERR_INVALID_COMPUTERNAME;
        }
 
@@ -1137,6 +1138,7 @@ static NTSTATUS net_update_dns_internal(TALLOC_CTX *ctx, ADS_STRUCT *ads,
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
        DNS_ERROR dns_err;
        fstring dns_server;
+       const char *dns_hosts_file;
        const char *dnsdomain = NULL;
        char *root_domain = NULL;
 
@@ -1148,7 +1150,9 @@ static NTSTATUS net_update_dns_internal(TALLOC_CTX *ctx, ADS_STRUCT *ads,
        }
        dnsdomain++;
 
-       status = ads_dns_lookup_ns( ctx, dnsdomain, &nameservers, &ns_count );
+       dns_hosts_file = lp_parm_const_string(-1, "resolv", "host file", NULL);
+       status = ads_dns_lookup_ns(ctx, dns_hosts_file,
+                                  dnsdomain, &nameservers, &ns_count);
        if ( !NT_STATUS_IS_OK(status) || (ns_count == 0)) {
                /* Child domains often do not have NS records.  Look
                   for the NS record for the forest root domain
@@ -1186,7 +1190,8 @@ static NTSTATUS net_update_dns_internal(TALLOC_CTX *ctx, ADS_STRUCT *ads,
 
                /* try again for NS servers */
 
-               status = ads_dns_lookup_ns( ctx, root_domain, &nameservers, &ns_count );
+               status = ads_dns_lookup_ns(ctx, dns_hosts_file, root_domain,
+                                          &nameservers, &ns_count);
 
                if ( !NT_STATUS_IS_OK(status) || (ns_count == 0)) {
                        DEBUG(3,("net_ads_join: Failed to find name server for the %s "
@@ -1200,6 +1205,8 @@ static NTSTATUS net_update_dns_internal(TALLOC_CTX *ctx, ADS_STRUCT *ads,
 
        for (i=0; i < ns_count; i++) {
 
+               status = NT_STATUS_UNSUCCESSFUL;
+
                /* Now perform the dns update - we'll try non-secure and if we fail,
                   we'll follow it up with a secure update */
 
@@ -1244,7 +1251,7 @@ static NTSTATUS net_update_dns_ext(TALLOC_CTX *mem_ctx, ADS_STRUCT *ads,
        if (hostname) {
                fstrcpy(machine_name, hostname);
        } else {
-               name_to_fqdn( machine_name, global_myname() );
+               name_to_fqdn( machine_name, lp_netbios_name() );
        }
        strlower_m( machine_name );
 
@@ -1302,8 +1309,91 @@ static int net_ads_join_usage(struct net_context *c, int argc, const char **argv
        return -1;
 }
 
-/*******************************************************************
- ********************************************************************/
+
+static void _net_ads_join_dns_updates(TALLOC_CTX *ctx, struct libnet_JoinCtx *r)
+{
+#if defined(WITH_DNS_UPDATES)
+       ADS_STRUCT *ads_dns = NULL;
+       int ret;
+       NTSTATUS status;
+
+       /*
+        * In a clustered environment, don't do dynamic dns updates:
+        * Registering the set of ip addresses that are assigned to
+        * the interfaces of the node that performs the join does usually
+        * not have the desired effect, since the local interfaces do not
+        * carry the complete set of the cluster's public IP addresses.
+        * And it can also contain internal addresses that should not
+        * be visible to the outside at all.
+        * In order to do dns updates in a clustererd setup, use
+        * net ads dns register.
+        */
+       if (lp_clustering()) {
+               d_fprintf(stderr, _("Not doing automatic DNS update in a "
+                                   "clustered setup.\n"));
+               return;
+       }
+
+       if (!r->out.domain_is_ad) {
+               return;
+       }
+
+       /*
+        * We enter this block with user creds.
+        * kinit with the machine password to do dns update.
+        */
+
+       ads_dns = ads_init(lp_realm(), NULL, r->in.dc_name);
+
+       if (ads_dns == NULL) {
+               d_fprintf(stderr, _("DNS update failed: out of memory!\n"));
+               goto done;
+       }
+
+       use_in_memory_ccache();
+
+       ret = asprintf(&ads_dns->auth.user_name, "%s$", lp_netbios_name());
+       if (ret == -1) {
+               d_fprintf(stderr, _("DNS update failed: out of memory\n"));
+               goto done;
+       }
+
+       ads_dns->auth.password = secrets_fetch_machine_password(
+               r->out.netbios_domain_name, NULL, NULL);
+       if (ads_dns->auth.password == NULL) {
+               d_fprintf(stderr, _("DNS update failed: out of memory\n"));
+               goto done;
+       }
+
+       ads_dns->auth.realm = SMB_STRDUP(r->out.dns_domain_name);
+       if (ads_dns->auth.realm == NULL) {
+               d_fprintf(stderr, _("DNS update failed: out of memory\n"));
+               goto done;
+       }
+
+       strupper_m(ads_dns->auth.realm);
+
+       ret = ads_kinit_password(ads_dns);
+       if (ret != 0) {
+               d_fprintf(stderr,
+                         _("DNS update failed: kinit failed: %s\n"),
+                         error_message(ret));
+               goto done;
+       }
+
+       status = net_update_dns(ctx, ads_dns, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf( stderr, _("DNS update failed: %s\n"),
+                         nt_errstr(status));
+       }
+
+done:
+       ads_destroy(&ads_dns);
+#endif
+
+       return;
+}
+
 
 int net_ads_join(struct net_context *c, int argc, const char **argv)
 {
@@ -1442,52 +1532,12 @@ int net_ads_join(struct net_context *c, int argc, const char **argv)
                        r->out.netbios_domain_name);
        }
 
-#if defined(WITH_DNS_UPDATES)
        /*
-        * In a clustered environment, don't do dynamic dns updates:
-        * Registering the set of ip addresses that are assigned to
-        * the interfaces of the node that performs the join does usually
-        * not have the desired effect, since the local interfaces do not
-        * carry the complete set of the cluster's public IP addresses.
-        * And it can also contain internal addresses that should not
-        * be visible to the outside at all.
-        * In order to do dns updates in a clustererd setup, use
-        * net ads dns register.
+        * We try doing the dns update (if it was compiled in).
+        * If the dns update fails, we still consider the join
+        * operation as succeeded if we came this far.
         */
-       if (lp_clustering()) {
-               d_fprintf(stderr, _("Not doing automatic DNS update in a"
-                                   "clustered setup.\n"));
-               goto done;
-       }
-
-       if (r->out.domain_is_ad) {
-               /* We enter this block with user creds */
-               ADS_STRUCT *ads_dns = NULL;
-
-               if ( (ads_dns = ads_init( lp_realm(), NULL, NULL )) != NULL ) {
-                       /* kinit with the machine password */
-
-                       use_in_memory_ccache();
-                       if (asprintf( &ads_dns->auth.user_name, "%s$", global_myname()) == -1) {
-                               goto fail;
-                       }
-                       ads_dns->auth.password = secrets_fetch_machine_password(
-                               r->out.netbios_domain_name, NULL, NULL );
-                       ads_dns->auth.realm = SMB_STRDUP( r->out.dns_domain_name );
-                       strupper_m(ads_dns->auth.realm );
-                       ads_kinit_password( ads_dns );
-               }
-
-               if ( !ads_dns || !NT_STATUS_IS_OK(net_update_dns( ctx, ads_dns, NULL)) ) {
-                       d_fprintf( stderr, _("DNS update failed!\n") );
-               }
-
-               /* exit from this block using machine creds */
-               ads_destroy(&ads_dns);
-       }
-
-done:
-#endif
+       _net_ads_join_dns_updates(ctx, r);
 
        TALLOC_FREE(r);
        TALLOC_FREE( ctx );
@@ -1756,7 +1806,7 @@ static int net_ads_printer_info(struct net_context *c, int argc, const char **ar
        if (argc > 1) {
                servername =  argv[1];
        } else {
-               servername = global_myname();
+               servername = lp_netbios_name();
        }
 
        rc = ads_find_printer_on_server(ads, &res, printername, servername);
@@ -1819,20 +1869,20 @@ static int net_ads_printer_publish(struct net_context *c, int argc, const char *
        if (argc == 2) {
                servername = argv[1];
        } else {
-               servername = global_myname();
+               servername = lp_netbios_name();
        }
 
        /* Get printer data from SPOOLSS */
 
        resolve_name(servername, &server_ss, 0x20, false);
 
-       nt_status = cli_full_connection(&cli, global_myname(), servername,
+       nt_status = cli_full_connection(&cli, lp_netbios_name(), servername,
                                        &server_ss, 0,
                                        "IPC$", "IPC",
                                        c->opt_user_name, c->opt_workgroup,
                                        c->opt_password ? c->opt_password : "",
                                        CLI_FULL_CONNECTION_USE_KERBEROS,
-                                       Undefined);
+                                       SMB_SIGNING_DEFAULT);
 
        if (NT_STATUS_IS_ERR(nt_status)) {
                d_fprintf(stderr, _("Unable to open a connection to %s to "
@@ -1942,7 +1992,7 @@ static int net_ads_printer_remove(struct net_context *c, int argc, const char **
        if (argc > 1) {
                servername = argv[1];
        } else {
-               servername = global_myname();
+               servername = lp_netbios_name();
        }
 
        rc = ads_find_printer_on_server(ads, &res, argv[0], servername);
@@ -2023,7 +2073,7 @@ static int net_ads_password(struct net_context *c, int argc, const char **argv)
        ADS_STRUCT *ads;
        const char *auth_principal = c->opt_user_name;
        const char *auth_password = c->opt_password;
-       char *realm = NULL;
+       const char *realm = NULL;
        const char *new_password = NULL;
        char *chr, *prompt;
        const char *user;
@@ -2135,7 +2185,7 @@ int net_ads_changetrustpw(struct net_context *c, int argc, const char **argv)
                return -1;
        }
 
-       fstrcpy(my_name, global_myname());
+       fstrcpy(my_name, lp_netbios_name());
        strlower_m(my_name);
        if (asprintf(&host_principal, "%s$@%s", my_name, ads->config.realm) == -1) {
                ads_destroy(&ads);
@@ -2206,7 +2256,7 @@ static int net_ads_search(struct net_context *c, int argc, const char **argv)
        ldap_exp = argv[0];
        attrs = (argv + 1);
 
-       rc = ads_do_search_all(ads, ads->config.bind_path,
+       rc = ads_do_search_retry(ads, ads->config.bind_path,
                               LDAP_SCOPE_SUBTREE,
                               ldap_exp, attrs, &res);
        if (!ADS_ERR_OK(rc)) {