utils/net_lookup.c

説明を見る。
00001 /* 
00002    Samba Unix/Linux SMB client library 
00003    net lookup command
00004    Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
00019 
00020 #include "includes.h"
00021 #include "utils/net.h"
00022 
00023 int net_lookup_usage(int argc, const char **argv)
00024 {
00025         d_printf(
00026 "  net lookup [host] HOSTNAME[#<type>]\n\tgives IP for a hostname\n\n"
00027 "  net lookup ldap [domain]\n\tgives IP of domain's ldap server\n\n"
00028 "  net lookup kdc [realm]\n\tgives IP of realm's kerberos KDC\n\n"
00029 "  net lookup dc [domain]\n\tgives IP of domains Domain Controllers\n\n"
00030 "  net lookup master [domain|wg]\n\tgive IP of master browser\n\n"
00031 "  net lookup name [name]\n\tLookup name's sid and type\n\n"
00032 "  net lookup sid [sid]\n\tGive sid's name and type\n\n"
00033 );
00034         return -1;
00035 }
00036 
00037 /* lookup a hostname giving an IP */
00038 static int net_lookup_host(int argc, const char **argv)
00039 {
00040         struct in_addr ip;
00041         int name_type = 0x20;
00042         const char *name = argv[0];
00043         char *p;
00044 
00045         if (argc == 0) 
00046                 return net_lookup_usage(argc, argv);
00047 
00048         p = strchr_m(name,'#');
00049         if (p) {
00050                 *p = '\0';
00051                 sscanf(++p,"%x",&name_type);
00052         }
00053         
00054         if (!resolve_name(name, &ip, name_type)) {
00055                 /* we deliberately use DEBUG() here to send it to stderr 
00056                    so scripts aren't mucked up */
00057                 DEBUG(0,("Didn't find %s#%02x\n", name, name_type));
00058                 return -1;
00059         }
00060 
00061         d_printf("%s\n", inet_ntoa(ip));
00062         return 0;
00063 }
00064 
00065 #ifdef HAVE_ADS
00066 static void print_ldap_srvlist(struct dns_rr_srv *dclist, int numdcs )
00067 {
00068         struct in_addr ip;
00069         int i;
00070 
00071         for ( i=0; i<numdcs; i++ ) {
00072                 if ( resolve_name(dclist[i].hostname, &ip, 0x20) ) {
00073                         d_printf("%s:%d\n", inet_ntoa(ip), dclist[i].port); 
00074                 }
00075         }
00076 }
00077 #endif
00078 
00079 static int net_lookup_ldap(int argc, const char **argv)
00080 {
00081 #ifdef HAVE_ADS
00082         const char *domain;
00083         struct in_addr addr;
00084         struct hostent *hostent;
00085         struct dns_rr_srv *dcs = NULL;
00086         int numdcs = 0;
00087         char *sitename;
00088         TALLOC_CTX *ctx;
00089         NTSTATUS status;
00090 
00091         if (argc > 0)
00092                 domain = argv[0];
00093         else
00094                 domain = opt_target_workgroup;
00095 
00096         sitename = sitename_fetch(domain);
00097 
00098         if ( (ctx = talloc_init("net_lookup_ldap")) == NULL ) {
00099                 d_fprintf(stderr, "net_lookup_ldap: talloc_inti() failed!\n");
00100                 SAFE_FREE(sitename);
00101                 return -1;
00102         }
00103 
00104         DEBUG(9, ("Lookup up ldap for domain %s\n", domain));
00105 
00106         status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
00107         if ( NT_STATUS_IS_OK(status) && numdcs ) {
00108                 print_ldap_srvlist(dcs, numdcs);
00109                 TALLOC_FREE( ctx );
00110                 SAFE_FREE(sitename);
00111                 return 0;
00112         }
00113 
00114         DEBUG(9, ("Looking up DC for domain %s\n", domain));
00115         if (!get_pdc_ip(domain, &addr)) {
00116                 TALLOC_FREE( ctx );
00117                 SAFE_FREE(sitename);
00118                 return -1;
00119         }
00120 
00121         hostent = gethostbyaddr((char *) &addr.s_addr, sizeof(addr.s_addr),
00122                                 AF_INET);
00123         if (!hostent) {
00124                 TALLOC_FREE( ctx );
00125                 SAFE_FREE(sitename);
00126                 return -1;
00127         }
00128 
00129         DEBUG(9, ("Found DC with DNS name %s\n", hostent->h_name));
00130         domain = strchr(hostent->h_name, '.');
00131         if (!domain) {
00132                 TALLOC_FREE( ctx );
00133                 SAFE_FREE(sitename);
00134                 return -1;
00135         }
00136         domain++;
00137 
00138         DEBUG(9, ("Looking up ldap for domain %s\n", domain));
00139 
00140         status = ads_dns_query_dcs( ctx, domain, sitename, &dcs, &numdcs );
00141         if ( NT_STATUS_IS_OK(status) && numdcs ) {
00142                 print_ldap_srvlist(dcs, numdcs);
00143                 TALLOC_FREE( ctx );
00144                 SAFE_FREE(sitename);
00145                 return 0;
00146         }
00147 
00148         TALLOC_FREE( ctx );
00149         SAFE_FREE(sitename);
00150 
00151         return -1;
00152 #endif
00153         DEBUG(1,("No ADS support\n"));
00154         return -1;
00155 }
00156 
00157 static int net_lookup_dc(int argc, const char **argv)
00158 {
00159         struct ip_service *ip_list;
00160         struct in_addr addr;
00161         char *pdc_str = NULL;
00162         const char *domain=opt_target_workgroup;
00163         char *sitename = NULL;
00164         int count, i;
00165 
00166         if (argc > 0)
00167                 domain=argv[0];
00168 
00169         /* first get PDC */
00170         if (!get_pdc_ip(domain, &addr))
00171                 return -1;
00172 
00173         asprintf(&pdc_str, "%s", inet_ntoa(addr));
00174         d_printf("%s\n", pdc_str);
00175 
00176         sitename = sitename_fetch(domain);
00177         if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, sitename, &ip_list, &count, False))) {
00178                 SAFE_FREE(pdc_str);
00179                 SAFE_FREE(sitename);
00180                 return 0;
00181         }
00182         SAFE_FREE(sitename);
00183         for (i=0;i<count;i++) {
00184                 char *dc_str = inet_ntoa(ip_list[i].ip);
00185                 if (!strequal(pdc_str, dc_str))
00186                         d_printf("%s\n", dc_str);
00187         }
00188         SAFE_FREE(pdc_str);
00189         return 0;
00190 }
00191 
00192 static int net_lookup_master(int argc, const char **argv)
00193 {
00194         struct in_addr master_ip;
00195         const char *domain=opt_target_workgroup;
00196 
00197         if (argc > 0)
00198                 domain=argv[0];
00199 
00200         if (!find_master_ip(domain, &master_ip))
00201                 return -1;
00202         d_printf("%s\n", inet_ntoa(master_ip));
00203         return 0;
00204 }
00205 
00206 static int net_lookup_kdc(int argc, const char **argv)
00207 {
00208 #ifdef HAVE_KRB5
00209         krb5_error_code rc;
00210         krb5_context ctx;
00211         struct sockaddr_in *addrs;
00212         int num_kdcs,i;
00213         krb5_data realm;
00214         char **realms;
00215 
00216         initialize_krb5_error_table();
00217         rc = krb5_init_context(&ctx);
00218         if (rc) {
00219                 DEBUG(1,("krb5_init_context failed (%s)\n", 
00220                          error_message(rc)));
00221                 return -1;
00222         }
00223 
00224         if (argc>0) {
00225                 realm.data = CONST_DISCARD(char *, argv[0]);
00226                 realm.length = strlen(argv[0]);
00227         } else if (lp_realm() && *lp_realm()) {
00228                 realm.data = lp_realm();
00229                 realm.length = strlen((const char *)realm.data);
00230         } else {
00231                 rc = krb5_get_host_realm(ctx, NULL, &realms);
00232                 if (rc) {
00233                         DEBUG(1,("krb5_gethost_realm failed (%s)\n",
00234                                  error_message(rc)));
00235                         return -1;
00236                 }
00237                 realm.data = (char *) *realms;
00238                 realm.length = strlen((const char *)realm.data);
00239         }
00240 
00241         rc = smb_krb5_locate_kdc(ctx, &realm, (struct sockaddr **)(void *)&addrs, &num_kdcs, 0);
00242         if (rc) {
00243                 DEBUG(1, ("smb_krb5_locate_kdc failed (%s)\n", error_message(rc)));
00244                 return -1;
00245         }
00246         for (i=0;i<num_kdcs;i++)
00247                 if (addrs[i].sin_family == AF_INET) 
00248                         d_printf("%s:%hd\n", inet_ntoa(addrs[i].sin_addr),
00249                                  ntohs(addrs[i].sin_port));
00250         return 0;
00251 
00252 #endif  
00253         DEBUG(1, ("No kerberos support\n"));
00254         return -1;
00255 }
00256 
00257 static int net_lookup_name(int argc, const char **argv)
00258 {
00259         const char *dom, *name;
00260         DOM_SID sid;
00261         enum lsa_SidType type;
00262 
00263         if (argc != 1) {
00264                 d_printf("usage: net lookup name <name>\n");
00265                 return -1;
00266         }
00267 
00268         if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ALL,
00269                          &dom, &name, &sid, &type)) {
00270                 d_printf("Could not lookup name %s\n", argv[0]);
00271                 return -1;
00272         }
00273 
00274         d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid),
00275                  type, sid_type_lookup(type), dom, name);
00276         return 0;
00277 }
00278 
00279 static int net_lookup_sid(int argc, const char **argv)
00280 {
00281         const char *dom, *name;
00282         DOM_SID sid;
00283         enum lsa_SidType type;
00284 
00285         if (argc != 1) {
00286                 d_printf("usage: net lookup sid <sid>\n");
00287                 return -1;
00288         }
00289 
00290         if (!string_to_sid(&sid, argv[0])) {
00291                 d_printf("Could not convert %s to SID\n", argv[0]);
00292                 return -1;
00293         }
00294 
00295         if (!lookup_sid(tmp_talloc_ctx(), &sid,
00296                         &dom, &name, &type)) {
00297                 d_printf("Could not lookup name %s\n", argv[0]);
00298                 return -1;
00299         }
00300 
00301         d_printf("%s %d (%s) %s\\%s\n", sid_string_static(&sid),
00302                  type, sid_type_lookup(type), dom, name);
00303         return 0;
00304 }
00305 
00306 /* lookup hosts or IP addresses using internal samba lookup fns */
00307 int net_lookup(int argc, const char **argv)
00308 {
00309         int i;
00310 
00311         struct functable table[] = {
00312                 {"HOST", net_lookup_host},
00313                 {"LDAP", net_lookup_ldap},
00314                 {"DC", net_lookup_dc},
00315                 {"MASTER", net_lookup_master},
00316                 {"KDC", net_lookup_kdc},
00317                 {"NAME", net_lookup_name},
00318                 {"SID", net_lookup_sid},
00319                 {NULL, NULL}
00320         };
00321 
00322         if (argc < 1) {
00323                 d_printf("\nUsage: \n");
00324                 return net_lookup_usage(argc, argv);
00325         }
00326         for (i=0; table[i].funcname; i++) {
00327                 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
00328                         return table[i].fn(argc-1, argv+1);
00329         }
00330 
00331         /* Default to lookup a hostname so 'net lookup foo#1b' can be 
00332            used instead of 'net lookup host foo#1b'.  The host syntax
00333            is a bit confusing as non #00 names can't really be 
00334            considered hosts as such. */
00335 
00336         return net_lookup_host(argc, argv);
00337 }

Sambaに対してSat Aug 29 21:23:28 2009に生成されました。  doxygen 1.4.7