utils: Free host realm after processing
[kai/samba-autobuild/.git] / source3 / utils / net_lookup.c
index 8b9ddb62bacc111d74b74a0af4303b45261400e6..140f9900795e5c029f98d6f41ddf8a0a9b56cb7c 100644 (file)
 
 #include "includes.h"
 #include "utils/net.h"
+#include "libsmb/namequery.h"
+#include "libads/sitename_cache.h"
+#include "../lib/addns/dnsquery.h"
+#include "../librpc/gen_ndr/ndr_netlogon.h"
+#include "smb_krb5.h"
+#include "../libcli/security/security.h"
+#include "passdb/lookup_sid.h"
+#include "libsmb/dsgetdcname.h"
 
 int net_lookup_usage(struct net_context *c, int argc, const char **argv)
 {
-       d_printf(
+       d_printf(_(
 "  net lookup [host] HOSTNAME[#<type>]\n\tgives IP for a hostname\n\n"
 "  net lookup ldap [domain]\n\tgives IP of domain's ldap server\n\n"
 "  net lookup kdc [realm]\n\tgives IP of realm's kerberos KDC\n\n"
@@ -31,7 +39,7 @@ int net_lookup_usage(struct net_context *c, int argc, const char **argv)
 "  net lookup name [name]\n\tLookup name's sid and type\n\n"
 "  net lookup sid [sid]\n\tGive sid's name and type\n\n"
 "  net lookup dsgetdcname [name] [flags] [sitename]\n\n"
-);
+));
        return -1;
 }
 
@@ -53,7 +61,7 @@ static int net_lookup_host(struct net_context *c, int argc, const char **argv)
                sscanf(++p,"%x",&name_type);
        }
 
-       if (!resolve_name(name, &ss, name_type)) {
+       if (!resolve_name(name, &ss, name_type, false)) {
                /* we deliberately use DEBUG() here to send it to stderr
                   so scripts aren't mucked up */
                DEBUG(0,("Didn't find %s#%02x\n", name, name_type));
@@ -72,7 +80,7 @@ static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
        int i;
 
        for ( i=0; i<numdcs; i++ ) {
-               if (resolve_name(dclist[i].hostname, &ss, 0x20) ) {
+               if (resolve_name(dclist[i].hostname, &ss, 0x20, true) ) {
                        char addr[INET6_ADDRSTRLEN];
                        print_sockaddr(addr, sizeof(addr), &ss);
 #ifdef HAVE_IPV6
@@ -106,28 +114,30 @@ static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
        else
                domain = c->opt_target_workgroup;
 
-       sitename = sitename_fetch(domain);
-
        if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
-               d_fprintf(stderr, "net_lookup_ldap: talloc_init() failed!\n");
-               SAFE_FREE(sitename);
+               d_fprintf(stderr,"net_lookup_ldap: talloc_init() %s!\n",
+                         _("failed"));
                return -1;
        }
 
+       sitename = sitename_fetch(ctx, domain);
+
        DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
 
-       status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
+       status = ads_dns_query_dcs(ctx,
+                                  domain,
+                                  sitename,
+                                  &dcs,
+                                  &numdcs);
        if ( NT_STATUS_IS_OK(status) && numdcs ) {
                print_ldap_srvlist(dcs, numdcs);
                TALLOC_FREE( ctx );
-               SAFE_FREE(sitename);
                return 0;
        }
 
        DEBUG(9, ("Looking up PDC for domain %s\n", domain));
        if (!get_pdc_ip(domain, &ss)) {
                TALLOC_FREE( ctx );
-               SAFE_FREE(sitename);
                return -1;
        }
 
@@ -139,7 +149,6 @@ static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
 
        if (ret) {
                TALLOC_FREE( ctx );
-               SAFE_FREE(sitename);
                return -1;
        }
 
@@ -147,23 +156,24 @@ static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
        domain = strchr(h_name, '.');
        if (!domain) {
                TALLOC_FREE( ctx );
-               SAFE_FREE(sitename);
                return -1;
        }
        domain++;
 
        DEBUG(9, ("Looking up ldap for domain %s\n", domain));
 
-       status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
+       status = ads_dns_query_dcs(ctx,
+                                  domain,
+                                  sitename,
+                                  &dcs,
+                                  &numdcs);
        if ( NT_STATUS_IS_OK(status) && numdcs ) {
                print_ldap_srvlist(dcs, numdcs);
                TALLOC_FREE( ctx );
-               SAFE_FREE(sitename);
                return 0;
        }
 
        TALLOC_FREE( ctx );
-       SAFE_FREE(sitename);
 
        return -1;
 #endif
@@ -196,17 +206,19 @@ static int net_lookup_dc(struct net_context *c, int argc, const char **argv)
                return -1;
 
        print_sockaddr(addr, sizeof(addr), &ss);
-       asprintf(&pdc_str, "%s", addr);
+       if (asprintf(&pdc_str, "%s", addr) == -1) {
+               return -1;
+       }
        d_printf("%s\n", pdc_str);
 
-       sitename = sitename_fetch(domain);
+       sitename = sitename_fetch(talloc_tos(), domain);
        if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename,
                                        &ip_list, &count, sec_ads))) {
                SAFE_FREE(pdc_str);
-               SAFE_FREE(sitename);
+               TALLOC_FREE(sitename);
                return 0;
        }
-       SAFE_FREE(sitename);
+       TALLOC_FREE(sitename);
        for (i=0;i<count;i++) {
                print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
                if (!strequal(pdc_str, addr))
@@ -237,7 +249,9 @@ static int net_lookup_pdc(struct net_context *c, int argc, const char **argv)
                return -1;
 
        print_sockaddr(addr, sizeof(addr), &ss);
-       asprintf(&pdc_str, "%s", addr);
+       if (asprintf(&pdc_str, "%s", addr) == -1) {
+               return -1;
+       }
        d_printf("%s\n", pdc_str);
        SAFE_FREE(pdc_str);
        return 0;
@@ -265,10 +279,12 @@ static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
 #ifdef HAVE_KRB5
        krb5_error_code rc;
        krb5_context ctx;
-       struct sockaddr_in *addrs;
-       int num_kdcs,i;
-       krb5_data realm;
-       char **realms;
+       struct ip_service *kdcs;
+       const char *realm;
+       char **get_host_realms = NULL;
+       int num_kdcs = 0;
+       int i;
+       NTSTATUS status;
 
        initialize_krb5_error_table();
        rc = krb5_init_context(&ctx);
@@ -278,35 +294,41 @@ static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
                return -1;
        }
 
-       if (argc>0) {
-                realm.data = CONST_DISCARD(char *, argv[0]);
-               realm.length = strlen(argv[0]);
+       if (argc > 0) {
+               realm = argv[0];
        } else if (lp_realm() && *lp_realm()) {
-               realm.data = lp_realm();
-               realm.length = strlen((const char *)realm.data);
+               realm = lp_realm();
        } else {
-               rc = krb5_get_host_realm(ctx, NULL, &realms);
+               rc = krb5_get_host_realm(ctx, NULL, &get_host_realms);
                if (rc) {
                        DEBUG(1,("krb5_gethost_realm failed (%s)\n",
                                 error_message(rc)));
+                       krb5_free_context(ctx);
                        return -1;
                }
-               realm.data = (char *) *realms;
-               realm.length = strlen((const char *)realm.data);
+               realm = (const char *) *get_host_realms;
        }
 
-       rc = smb_krb5_locate_kdc(ctx, &realm, (struct sockaddr **)(void *)&addrs, &num_kdcs, 0);
-       if (rc) {
-               DEBUG(1, ("smb_krb5_locate_kdc failed (%s)\n", error_message(rc)));
+       status = get_kdc_list(realm, NULL, &kdcs, &num_kdcs);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1,("get_kdc_list failed (%s)\n", nt_errstr(status)));
+               krb5_free_host_realm(ctx, get_host_realms);
+               krb5_free_context(ctx);
                return -1;
        }
-       for (i=0;i<num_kdcs;i++)
-               if (addrs[i].sin_family == AF_INET) 
-                       d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
-                                ntohs(addrs[i].sin_port));
-       return 0;
 
-#endif 
+       for (i = 0; i < num_kdcs; i++) {
+               char addr[INET6_ADDRSTRLEN];
+
+               print_sockaddr(addr, sizeof(addr), &kdcs[i].ss);
+
+               d_printf("%s:%u\n", addr, kdcs[i].port);
+       }
+
+       krb5_free_host_realm(ctx, get_host_realms);
+       krb5_free_context(ctx);
+       return 0;
+#endif
        DEBUG(1, ("No kerberos support\n"));
        return -1;
 }
@@ -314,17 +336,19 @@ static int net_lookup_kdc(struct net_context *c, int argc, const char **argv)
 static int net_lookup_name(struct net_context *c, int argc, const char **argv)
 {
        const char *dom, *name;
-       DOM_SID sid;
+       struct dom_sid sid;
        enum lsa_SidType type;
 
        if (argc != 1) {
-               d_printf("usage: net lookup name <name>\n");
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _(" net lookup name <name>\n"));
                return -1;
        }
 
        if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_ALL,
                         &dom, &name, &sid, &type)) {
-               d_printf("Could not lookup name %s\n", argv[0]);
+               d_printf(_("Could not lookup name %s\n"), argv[0]);
                return -1;
        }
 
@@ -336,22 +360,24 @@ static int net_lookup_name(struct net_context *c, int argc, const char **argv)
 static int net_lookup_sid(struct net_context *c, int argc, const char **argv)
 {
        const char *dom, *name;
-       DOM_SID sid;
+       struct dom_sid sid;
        enum lsa_SidType type;
 
        if (argc != 1) {
-               d_printf("usage: net lookup sid <sid>\n");
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _(" net lookup sid <sid>\n"));
                return -1;
        }
 
        if (!string_to_sid(&sid, argv[0])) {
-               d_printf("Could not convert %s to SID\n", argv[0]);
+               d_printf(_("Could not convert %s to SID\n"), argv[0]);
                return -1;
        }
 
        if (!lookup_sid(talloc_tos(), &sid,
                        &dom, &name, &type)) {
-               d_printf("Could not lookup name %s\n", argv[0]);
+               d_printf(_("Could not lookup name %s\n"), argv[0]);
                return -1;
        }
 
@@ -371,8 +397,10 @@ static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **
        char *s = NULL;
 
        if (argc < 1 || argc > 3) {
-               d_printf("usage: net lookup dsgetdcname "
-                        "<name> <flags> <sitename>\n");
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _(" net lookup dsgetdcname "
+                          "<name> <flags> <sitename>\n"));
                return -1;
        }
 
@@ -383,21 +411,28 @@ static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **
 
        domain_name = argv[0];
 
-       if (argc >= 2)
+       if (argc >= 2) {
                sscanf(argv[1], "%x", &flags);
+       }
 
-       if (!flags) {
-               flags |= DS_DIRECTORY_SERVICE_REQUIRED;
+       if (flags == 0) {
+               flags = DS_DIRECTORY_SERVICE_REQUIRED;
        }
 
        if (argc == 3) {
                site_name = argv[2];
        }
 
-       status = dsgetdcname(mem_ctx, NULL, domain_name, NULL, site_name,
+        if (!c->msg_ctx) {
+               d_fprintf(stderr, _("Could not initialise message context. "
+                       "Try running as root\n"));
+               return -1;
+        }
+
+       status = dsgetdcname(mem_ctx, c->msg_ctx, domain_name, NULL, site_name,
                             flags, &info);
        if (!NT_STATUS_IS_OK(status)) {
-               d_printf("failed with: %s\n", nt_errstr(status));
+               d_printf(_("failed with: %s\n"), nt_errstr(status));
                TALLOC_FREE(mem_ctx);
                return -1;
        }
@@ -430,11 +465,11 @@ int net_lookup(struct net_context *c, int argc, const char **argv)
        };
 
        if (argc < 1) {
-               d_printf("\nUsage: \n");
+               d_printf(_("\nUsage: \n"));
                return net_lookup_usage(c, argc, argv);
        }
        for (i=0; table[i].funcname; i++) {
-               if (StrCaseCmp(argv[0], table[i].funcname) == 0)
+               if (strcasecmp_m(argv[0], table[i].funcname) == 0)
                        return table[i].fn(c, argc-1, argv+1);
        }