utils: Free host realm after processing
[kai/samba-autobuild/.git] / source3 / utils / net_lookup.c
index ba4e32ae84ef04b504dd701dfc1c2a37b283a3c5..140f9900795e5c029f98d6f41ddf8a0a9b56cb7c 100644 (file)
 
 #include "includes.h"
 #include "utils/net.h"
-
-int net_lookup_usage(int argc, const char **argv)
+#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,12 +39,12 @@ int net_lookup_usage(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;
 }
 
 /* lookup a hostname giving an IP */
-static int net_lookup_host(int argc, const char **argv)
+static int net_lookup_host(struct net_context *c, int argc, const char **argv)
 {
        struct sockaddr_storage ss;
        int name_type = 0x20;
@@ -45,7 +53,7 @@ static int net_lookup_host(int argc, const char **argv)
        char *p;
 
        if (argc == 0)
-               return net_lookup_usage(argc, argv);
+               return net_lookup_usage(c, argc, argv);
 
        p = strchr_m(name,'#');
        if (p) {
@@ -53,7 +61,7 @@ static int net_lookup_host(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
@@ -88,7 +96,7 @@ static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
 }
 #endif
 
-static int net_lookup_ldap(int argc, const char **argv)
+static int net_lookup_ldap(struct net_context *c, int argc, const char **argv)
 {
 #ifdef HAVE_ADS
        const char *domain;
@@ -104,30 +112,32 @@ static int net_lookup_ldap(int argc, const char **argv)
        if (argc > 0)
                domain = argv[0];
        else
-               domain = opt_target_workgroup;
-
-       sitename = sitename_fetch(domain);
+               domain = c->opt_target_workgroup;
 
        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(int argc, const char **argv)
 
        if (ret) {
                TALLOC_FREE( ctx );
-               SAFE_FREE(sitename);
                return -1;
        }
 
@@ -147,23 +156,24 @@ static int net_lookup_ldap(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
@@ -171,7 +181,7 @@ static int net_lookup_ldap(int argc, const char **argv)
        return -1;
 }
 
-static int net_lookup_dc(int argc, const char **argv)
+static int net_lookup_dc(struct net_context *c, int argc, const char **argv)
 {
        struct ip_service *ip_list;
        struct sockaddr_storage ss;
@@ -185,7 +195,7 @@ static int net_lookup_dc(int argc, const char **argv)
        if (sec_ads) {
                domain = lp_realm();
        } else {
-               domain = opt_target_workgroup;
+               domain = c->opt_target_workgroup;
        }
 
        if (argc > 0)
@@ -196,17 +206,19 @@ static int net_lookup_dc(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))
@@ -216,7 +228,7 @@ static int net_lookup_dc(int argc, const char **argv)
        return 0;
 }
 
-static int net_lookup_pdc(int argc, const char **argv)
+static int net_lookup_pdc(struct net_context *c, int argc, const char **argv)
 {
        struct sockaddr_storage ss;
        char *pdc_str = NULL;
@@ -226,7 +238,7 @@ static int net_lookup_pdc(int argc, const char **argv)
        if (lp_security() == SEC_ADS) {
                domain = lp_realm();
        } else {
-               domain = opt_target_workgroup;
+               domain = c->opt_target_workgroup;
        }
 
        if (argc > 0)
@@ -237,17 +249,19 @@ static int net_lookup_pdc(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;
 }
 
 
-static int net_lookup_master(int argc, const char **argv)
+static int net_lookup_master(struct net_context *c, int argc, const char **argv)
 {
        struct sockaddr_storage master_ss;
-       const char *domain=opt_target_workgroup;
+       const char *domain = c->opt_target_workgroup;
        char addr[INET6_ADDRSTRLEN];
 
        if (argc > 0)
@@ -260,71 +274,81 @@ static int net_lookup_master(int argc, const char **argv)
        return 0;
 }
 
-static int net_lookup_kdc(int argc, const char **argv)
+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);
        if (rc) {
-               DEBUG(1,("krb5_init_context failed (%s)\n", 
+               DEBUG(1,("krb5_init_context failed (%s)\n",
                         error_message(rc)));
                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;
 }
 
-static int net_lookup_name(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;
        }
 
@@ -333,25 +357,27 @@ static int net_lookup_name(int argc, const char **argv)
        return 0;
 }
 
-static int net_lookup_sid(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;
        }
 
@@ -360,19 +386,21 @@ static int net_lookup_sid(int argc, const char **argv)
        return 0;
 }
 
-static int net_lookup_dsgetdcname(int argc, const char **argv)
+static int net_lookup_dsgetdcname(struct net_context *c, int argc, const char **argv)
 {
        NTSTATUS status;
        const char *domain_name = NULL;
-       char *site_name = NULL;
+       const char *site_name = NULL;
        uint32_t flags = 0;
        struct netr_DsRGetDCNameInfo *info = NULL;
        TALLOC_CTX *mem_ctx;
        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,30 +411,28 @@ static int net_lookup_dsgetdcname(int argc, const char **argv)
 
        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 = SMB_STRDUP(argv[2]);
-               if (!site_name) {
-                       TALLOC_FREE(mem_ctx);
-                       return -1;
-               }
+               site_name = argv[2];
        }
 
-       if (!site_name) {
-               site_name = sitename_fetch(domain_name);
-       }
+        if (!c->msg_ctx) {
+               d_fprintf(stderr, _("Could not initialise message context. "
+                       "Try running as root\n"));
+               return -1;
+        }
 
-       status = dsgetdcname(mem_ctx, NULL, domain_name, NULL, site_name,
+       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));
-               SAFE_FREE(site_name);
+               d_printf(_("failed with: %s\n"), nt_errstr(status));
                TALLOC_FREE(mem_ctx);
                return -1;
        }
@@ -415,14 +441,13 @@ static int net_lookup_dsgetdcname(int argc, const char **argv)
        printf("%s\n", s);
        TALLOC_FREE(s);
 
-       SAFE_FREE(site_name);
        TALLOC_FREE(mem_ctx);
        return 0;
 }
 
 
 /* lookup hosts or IP addresses using internal samba lookup fns */
-int net_lookup(int argc, const char **argv)
+int net_lookup(struct net_context *c, int argc, const char **argv)
 {
        int i;
 
@@ -440,18 +465,18 @@ int net_lookup(int argc, const char **argv)
        };
 
        if (argc < 1) {
-               d_printf("\nUsage: \n");
-               return net_lookup_usage(argc, argv);
+               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)
-                       return table[i].fn(argc-1, argv+1);
+               if (strcasecmp_m(argv[0], table[i].funcname) == 0)
+                       return table[i].fn(c, argc-1, argv+1);
        }
 
-       /* Default to lookup a hostname so 'net lookup foo#1b' can be 
+       /* Default to lookup a hostname so 'net lookup foo#1b' can be
           used instead of 'net lookup host foo#1b'.  The host syntax
-          is a bit confusing as non #00 names can't really be 
+          is a bit confusing as non #00 names can't really be
           considered hosts as such. */
 
-       return net_lookup_host(argc, argv);
+       return net_lookup_host(c, argc, argv);
 }