/**************************************************************************** load the list of network interfaces ****************************************************************************/ void load_interfaces(void) { const char **ptr; int i; struct iface_struct ifaces[MAX_INTERFACES]; ptr = lp_interfaces(); allones_ip = *interpret_addr2("255.255.255.255"); loopback_ip = *interpret_addr2("127.0.0.1"); SAFE_FREE(probed_ifaces); /* dump the current interfaces if any */ while (local_interfaces) { struct interface *iface = local_interfaces; DLIST_REMOVE(local_interfaces, local_interfaces); ZERO_STRUCTPN(iface); SAFE_FREE(iface); } /* probe the kernel for interfaces */ total_probed = get_interfaces(ifaces, MAX_INTERFACES); if (total_probed > 0) { probed_ifaces = memdup(ifaces, sizeof(ifaces[0])*total_probed); } /* if we don't have a interfaces line then use all broadcast capable interfaces except loopback */ if (!ptr || !*ptr || !**ptr) { if (total_probed <= 0) { DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n")); exit(1); } for (i=0;i<total_probed;i++) { if (probed_ifaces[i].netmask.s_addr != allones_ip.s_addr && probed_ifaces[i].ip.s_addr != loopback_ip.s_addr) { add_interface(probed_ifaces[i].ip, probed_ifaces[i].netmask); } } return; } if (ptr) { while (*ptr) { char *ptr_cpy = strdup(*ptr); if (ptr_cpy) { interpret_interface(ptr_cpy); free(ptr_cpy); } ptr++; } } if (!local_interfaces) { DEBUG(0,("WARNING: no network interfaces found\n")); } }
static int nbtd_wins_randomize1Clist_sort(void *p1,/* (const char **) */ void *p2,/* (const char **) */ struct socket_address *src) { const char *a1 = (const char *)*(const char **)p1; const char *a2 = (const char *)*(const char **)p2; uint32_t match_bits1; uint32_t match_bits2; match_bits1 = ipv4_match_bits(interpret_addr2(a1), interpret_addr2(src->addr)); match_bits2 = ipv4_match_bits(interpret_addr2(a2), interpret_addr2(src->addr)); return match_bits2 - match_bits1; }
int open_udp_socket(const char *host, int port) { int type = SOCK_DGRAM; struct sockaddr_in sock_out; int res; struct in_addr *addr; addr = interpret_addr2(host); res = socket(PF_INET, type, 0); if (res == -1) { return -1; } memset((char *)&sock_out,'\0',sizeof(sock_out)); putip((char *)&sock_out.sin_addr,(char *)addr); sock_out.sin_port = htons(port); sock_out.sin_family = PF_INET; if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) { close(res); return -1; } return res; }
/* parse an IP string that might be in tagged format the result is a tagged_ip structure containing the tag and the ip in in_addr format. If there is no tag then use the tag '*' */ static void parse_ip(struct tagged_ip *ip, const char *str) { char *s = strchr(str, ':'); if (!s) { fstrcpy(ip->tag, "*"); ip->ip = interpret_addr2(str); return; } ip->ip = interpret_addr2(s+1); fstrcpy(ip->tag, str); s = strchr(ip->tag, ':'); if (s) { *s = 0; } }
/* setup our listening sockets on the configured network interfaces */ NTSTATUS nbtd_startup_interfaces(struct nbtd_server *nbtsrv, struct loadparm_context *lp_ctx, struct interface *ifaces) { int num_interfaces = iface_count(ifaces); int i; TALLOC_CTX *tmp_ctx = talloc_new(nbtsrv); NTSTATUS status; /* if we are allowing incoming packets from any address, then we also need to bind to the wildcard address */ if (!lp_bind_interfaces_only(lp_ctx)) { const char *primary_address; /* the primary address is the address we will return for non-WINS queries not made on a specific interface */ if (num_interfaces > 0) { primary_address = iface_n_ip(ifaces, 0); } else { primary_address = inet_ntoa(interpret_addr2( lp_netbios_name(lp_ctx))); } primary_address = talloc_strdup(tmp_ctx, primary_address); NT_STATUS_HAVE_NO_MEMORY(primary_address); status = nbtd_add_socket(nbtsrv, lp_ctx, "0.0.0.0", primary_address, talloc_strdup(tmp_ctx, "255.255.255.255"), talloc_strdup(tmp_ctx, "0.0.0.0")); NT_STATUS_NOT_OK_RETURN(status); } for (i=0; i<num_interfaces; i++) { const char *bcast = iface_n_bcast(ifaces, i); const char *address, *netmask; /* we can't assume every interface is broadcast capable */ if (bcast == NULL) continue; address = talloc_strdup(tmp_ctx, iface_n_ip(ifaces, i)); bcast = talloc_strdup(tmp_ctx, bcast); netmask = talloc_strdup(tmp_ctx, iface_n_netmask(ifaces, i)); status = nbtd_add_socket(nbtsrv, lp_ctx, address, address, bcast, netmask); NT_STATUS_NOT_OK_RETURN(status); } if (lp_wins_server_list(lp_ctx)) { status = nbtd_add_wins_socket(nbtsrv); NT_STATUS_NOT_OK_RETURN(status); } talloc_free(tmp_ctx); return NT_STATUS_OK; }
void announce_remote(time_t t) { char *s; const char *ptr; static time_t last_time = 0; pstring s2; struct in_addr addr; char *comment; int stype = lp_default_server_announce(); if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL))) return; last_time = t; s = lp_remote_announce(); if (!*s) return; comment = string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH); for (ptr=s; next_token(&ptr,s2,NULL,sizeof(s2)); ) { /* The entries are of the form a.b.c.d/WORKGROUP with WORKGROUP being optional */ const char *wgroup; char *pwgroup; int i; pwgroup = strchr_m(s2,'/'); if (pwgroup) *pwgroup++ = 0; if (!pwgroup || !*pwgroup) wgroup = lp_workgroup(); else wgroup = pwgroup; addr = *interpret_addr2(s2); /* Announce all our names including aliases */ /* Give the ip address as the address of our first broadcast subnet. */ for(i=0; my_netbios_names(i); i++) { const char *name = my_netbios_names(i); DEBUG(5,("announce_remote: Doing remote announce for server %s to IP %s.\n", name, inet_ntoa(addr) )); send_announcement(FIRST_SUBNET, ANN_HostAnnouncement, name, /* From nbt name. */ wgroup, 0x1d, /* To nbt name. */ addr, /* To ip. */ REMOTE_ANNOUNCE_INTERVAL, /* Time until next announce. */ name, /* Name to announce. */ stype, /* Type field. */ comment); } } }
char *get_peer_name(int fd, BOOL force_lookup) { static pstring name_buf; pstring tmp_name; static fstring addr_buf; struct hostent *hp; struct in_addr addr; char *p; /* reverse lookups can be *very* expensive, and in many situations won't work because many networks don't link dhcp with dns. To avoid the delay we avoid the lookup if possible */ if (!lp_hostname_lookups() && (force_lookup == False)) { return get_peer_addr(fd); } p = get_peer_addr(fd); /* it might be the same as the last one - save some DNS work */ if (strcmp(p, addr_buf) == 0) return name_buf; pstrcpy(name_buf,"UNKNOWN"); if (fd == -1) return name_buf; fstrcpy(addr_buf, p); addr = *interpret_addr2(p); /* Look up the remote host name. */ if ((hp = gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr), AF_INET)) == 0) { DEBUG(1,("Gethostbyaddr failed for %s\n",p)); pstrcpy(name_buf, p); } else { pstrcpy(name_buf,(char *)hp->h_name); if (!matchname(name_buf, addr)) { DEBUG(0,("Matchname failed on %s %s\n",name_buf,p)); pstrcpy(name_buf,"UNKNOWN"); } } /* can't pass the same source and dest strings in when you use --enable-developer or the clobber_region() call will get you */ pstrcpy( tmp_name, name_buf ); alpha_strcpy(name_buf, tmp_name, "_-.", sizeof(name_buf)); if (strstr(name_buf,"..")) { pstrcpy(name_buf, "UNKNOWN"); } return name_buf; }
/* note that for simplicity of the API, socket_listen() is also use for DGRAM sockets, but in reality only a bind() is done */ static NTSTATUS ipv4_listen(struct socket_context *sock, const struct socket_address *my_address, int queue_size, uint32_t flags) { struct sockaddr_in my_addr; struct in_addr ip_addr; int ret; socket_set_option(sock, "SO_REUSEADDR=1", NULL); if (my_address->sockaddr) { ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen); } else { ip_addr = interpret_addr2(my_address->addr); ZERO_STRUCT(my_addr); #ifdef HAVE_SOCK_SIN_LEN my_addr.sin_len = sizeof(my_addr); #endif my_addr.sin_addr.s_addr = ip_addr.s_addr; my_addr.sin_port = htons(my_address->port); my_addr.sin_family = PF_INET; ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr)); } if (ret == -1) { return map_nt_error_from_unix_common(errno); } if (sock->type == SOCKET_TYPE_STREAM) { ret = listen(sock->fd, queue_size); if (ret == -1) { return map_nt_error_from_unix_common(errno); } } if (!(flags & SOCKET_FLAG_BLOCK)) { ret = set_blocking(sock->fd, false); if (ret == -1) { return map_nt_error_from_unix_common(errno); } } sock->state= SOCKET_STATE_SERVER_LISTEN; return NT_STATUS_OK; }
/******************************************************** resolve via "wins" method *********************************************************/ static BOOL resolve_wins (const char *name, struct in_addr *return_ip, int name_type) { int sock; struct in_addr wins_ip; BOOL wins_ismyip; /* * "wins" means do a unicast lookup to the WINS server. * Ignore if there is no WINS server specified or if the * WINS server is one of our interfaces (if we're being * called from within nmbd - we can't do this call as we * would then block). */ DEBUG (3, ("resolve_name: Attempting wins lookup for name %s<0x%x>\n", name, name_type)); if (!*lp_wins_server ()) { DEBUG (3, ("resolve_name: WINS server resolution selected and no WINS server present.\n")); return False; } wins_ip = *interpret_addr2 (lp_wins_server ()); wins_ismyip = ismyip (wins_ip); if ((wins_ismyip && !global_in_nmbd) || !wins_ismyip) { sock = open_socket_in (SOCK_DGRAM, 0, 3, interpret_addr (lp_socket_address ()), True); if (sock != -1) { struct in_addr *iplist = NULL; int count; iplist = name_query (sock, name, name_type, False, True, wins_ip, &count, NULL); if (iplist != NULL) { *return_ip = iplist[0]; free ((char *) iplist); close (sock); return True; } close (sock); } } return False; }
static struct node_status *lookup_byaddr_backend(char *addr, int *count) { int fd; struct in_addr ip; struct nmb_name nname; struct node_status *status; fd = wins_lookup_open_socket_in(); if (fd == -1) return NULL; make_nmb_name(&nname, "*", 0); ip = *interpret_addr2(addr); status = node_status_query(fd,&nname,ip, count, NULL); close(fd); return status; }
/* test resolution using sync method */ static bool test_sync_resolve(struct torture_context *tctx) { int timelimit = torture_setting_int(tctx, "timelimit", 2); struct timeval tv = timeval_current(); int count = 0; const char *host = torture_setting_string(tctx, "host", NULL); torture_comment(tctx, "Testing sync resolve of '%s' for %d seconds\n", host, timelimit); while (timeval_elapsed(&tv) < timelimit) { inet_ntoa(interpret_addr2(host)); count++; } torture_comment(tctx, "sync rate of %.1f resolves/sec\n", count/timeval_elapsed(&tv)); return true; }
static NTSTATUS ipv4_sendto(struct socket_context *sock, const DATA_BLOB *blob, size_t *sendlen, const struct socket_address *dest_addr) { ssize_t len; if (dest_addr->sockaddr) { len = sendto(sock->fd, blob->data, blob->length, 0, dest_addr->sockaddr, dest_addr->sockaddrlen); } else { struct sockaddr_in srv_addr; struct in_addr addr; SMB_ASSERT(dest_addr->port != 0); ZERO_STRUCT(srv_addr); #ifdef HAVE_SOCK_SIN_LEN srv_addr.sin_len = sizeof(srv_addr); #endif addr = interpret_addr2(dest_addr->addr); if (addr.s_addr == 0) { return NT_STATUS_HOST_UNREACHABLE; } srv_addr.sin_addr.s_addr = addr.s_addr; srv_addr.sin_port = htons(dest_addr->port); srv_addr.sin_family = PF_INET; *sendlen = 0; len = sendto(sock->fd, blob->data, blob->length, 0, (struct sockaddr *)&srv_addr, sizeof(srv_addr)); } if (len == -1) { return map_nt_error_from_unix_common(errno); } *sendlen = len; return NT_STATUS_OK; }
static struct in_addr *lookup_byname_backend(const char *name, int *count) { int fd; struct in_addr *ret = NULL; struct in_addr p; int j, flags; *count = 0; fd = wins_lookup_open_socket_in(); if (fd == -1) return NULL; p = wins_srv_ip(); if( !is_zero_ip(p) ) { ret = name_query(fd,name,0x20,False,True, p, count, &flags); goto out; } if (lp_wins_support()) { /* we are our own WINS server */ ret = name_query(fd,name,0x20,False,True, *interpret_addr2("127.0.0.1"), count, &flags); goto out; } /* uggh, we have to broadcast to each interface in turn */ for (j=iface_count() - 1; j >= 0; j--) { struct in_addr *bcast = iface_n_bcast(j); ret = name_query(fd,name,0x20,True,True,*bcast,count, &flags); if (ret) break; } out: close(fd); return ret; }
/** load the list of network interfaces **/ void load_interfaces(TALLOC_CTX *mem_ctx, const char **interfaces, struct interface **local_interfaces) { const char **ptr = interfaces; int i; struct iface_struct ifaces[MAX_INTERFACES]; struct in_addr loopback_ip; int total_probed; *local_interfaces = NULL; loopback_ip = interpret_addr2("127.0.0.1"); /* probe the kernel for interfaces */ total_probed = get_interfaces(ifaces, MAX_INTERFACES); /* if we don't have a interfaces line then use all interfaces except loopback */ if (!ptr || !*ptr || !**ptr) { if (total_probed <= 0) { DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n")); } for (i=0;i<total_probed;i++) { if (ifaces[i].ip.s_addr != loopback_ip.s_addr) { add_interface(mem_ctx, ifaces[i].ip, ifaces[i].netmask, local_interfaces); } } } while (ptr && *ptr) { interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces); ptr++; } if (!*local_interfaces) { DEBUG(0,("WARNING: no network interfaces found\n")); } }
/* called when a session is destroyed */ void session_yield(user_struct *vuser) { TDB_DATA dbuf; struct sessionid sessionid; struct in_addr *client_ip; TDB_DATA key; if (!tdb) return; if (!vuser->session_keystr) { return; } key.dptr = vuser->session_keystr; key.dsize = strlen(vuser->session_keystr)+1; dbuf = tdb_fetch(tdb, key); if (dbuf.dsize != sizeof(sessionid)) return; memcpy(&sessionid, dbuf.dptr, sizeof(sessionid)); client_ip = interpret_addr2(sessionid.ip_addr); SAFE_FREE(dbuf.dptr); if (lp_utmp()) { sys_utmp_yield(sessionid.username, sessionid.hostname, client_ip, sessionid.id_str, sessionid.id_num); } smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname); tdb_delete(tdb, key); }
static bool do_nodestatus(struct tevent_context *ev_ctx, struct messaging_context *msg_ctx, const struct server_id pid, const int argc, const char **argv) { struct packet_struct p; if (argc != 2) { fprintf(stderr, "Usage: smbcontrol nmbd nodestatus <ip>\n"); return False; } ZERO_STRUCT(p); p.ip = interpret_addr2(argv[1]); p.port = 137; p.packet_type = NMB_PACKET; p.packet.nmb.header.name_trn_id = 10; p.packet.nmb.header.opcode = 0; p.packet.nmb.header.response = False; p.packet.nmb.header.nm_flags.bcast = False; p.packet.nmb.header.nm_flags.recursion_available = False; p.packet.nmb.header.nm_flags.recursion_desired = False; p.packet.nmb.header.nm_flags.trunc = False; p.packet.nmb.header.nm_flags.authoritative = False; p.packet.nmb.header.rcode = 0; p.packet.nmb.header.qdcount = 1; p.packet.nmb.header.ancount = 0; p.packet.nmb.header.nscount = 0; p.packet.nmb.header.arcount = 0; my_make_nmb_name(&p.packet.nmb.question.question_name, "*", 0x00); p.packet.nmb.question.question_type = 0x21; p.packet.nmb.question.question_class = 0x1; return send_message(msg_ctx, pid, MSG_SEND_PACKET, &p, sizeof(p)); }
/**************************************************************************** main program ****************************************************************************/ int main(int argc,char *argv[]) { char *pname = argv[0]; int opt; extern FILE *dbf; extern char *optarg; extern int optind; static pstring servicesf = CONFIGFILE; pstring term_code; BOOL got_pass = False; char *cmd_str=""; enum client_action cli_action = CLIENT_NONE; int nprocs = 1; int numops = 100; pstring logfile; struct client_info cli_info; out_hnd = stdout; rpcclient_init(); #ifdef KANJI pstrcpy(term_code, KANJI); #else /* KANJI */ *term_code = 0; #endif /* KANJI */ if (!lp_load(servicesf,True, False, False)) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf); } codepage_initialise(lp_client_code_page()); DEBUGLEVEL = 0; cli_info.put_total_size = 0; cli_info.put_total_time_ms = 0; cli_info.get_total_size = 0; cli_info.get_total_time_ms = 0; cli_info.dir_total = 0; cli_info.newer_than = 0; cli_info.archive_level = 0; cli_info.print_mode = 1; cli_info.translation = False; cli_info.recurse_dir = False; cli_info.lowercase = False; cli_info.prompt = True; cli_info.abort_mget = True; cli_info.dest_ip.s_addr = 0; cli_info.name_type = 0x20; pstrcpy(cli_info.cur_dir , "\\"); pstrcpy(cli_info.file_sel, ""); pstrcpy(cli_info.base_dir, ""); pstrcpy(smb_cli->domain, ""); pstrcpy(smb_cli->user_name, ""); pstrcpy(cli_info.myhostname, ""); pstrcpy(cli_info.dest_host, ""); pstrcpy(cli_info.svc_type, "A:"); pstrcpy(cli_info.share, ""); pstrcpy(cli_info.service, ""); ZERO_STRUCT(cli_info.dom.level3_sid); pstrcpy(cli_info.dom.level3_dom, ""); ZERO_STRUCT(cli_info.dom.level5_sid); pstrcpy(cli_info.dom.level5_dom, ""); smb_cli->nt_pipe_fnum = 0xffff; setup_logging(pname, True); TimeInit(); charset_initialise(); if (!get_myname(global_myname)) { fprintf(stderr, "Failed to get my hostname.\n"); } password[0] = 0; if (argc < 2) { usage(pname); exit(1); } if (*argv[1] != '-') { pstrcpy(cli_info.service, argv[1]); /* Convert any '/' characters in the service name to '\' characters */ string_replace( cli_info.service, '/','\\'); argc--; argv++; DEBUG(1,("service: %s\n", cli_info.service)); if (count_chars(cli_info.service,'\\') < 3) { usage(pname); printf("\n%s: Not enough '\\' characters in service\n", cli_info.service); exit(1); } /* if (count_chars(cli_info.service,'\\') > 3) { usage(pname); printf("\n%s: Too many '\\' characters in service\n", cli_info.service); exit(1); } */ if (argc > 1 && (*argv[1] != '-')) { got_pass = True; pstrcpy(password,argv[1]); memset(argv[1],'X',strlen(argv[1])); argc--; argv++; } cli_action = CLIENT_SVC; } while ((opt = getopt(argc, argv,"s:O:M:S:i:N:o:n:d:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF) { switch (opt) { case 'm': { /* FIXME ... max_protocol seems to be funny here */ int max_protocol = 0; max_protocol = interpret_protocol(optarg,max_protocol); fprintf(stderr, "max protocol not currently supported\n"); break; } case 'O': { pstrcpy(user_socket_options,optarg); break; } case 'S': { pstrcpy(cli_info.dest_host,optarg); strupper(cli_info.dest_host); cli_action = CLIENT_IPC; break; } case 'i': { pstrcpy(scope, optarg); break; } case 'U': { char *lp; pstrcpy(smb_cli->user_name,optarg); if ((lp=strchr(smb_cli->user_name,'%'))) { *lp = 0; pstrcpy(password,lp+1); got_pass = True; memset(strchr(optarg,'%')+1,'X',strlen(password)); } break; } case 'W': { pstrcpy(smb_cli->domain,optarg); break; } case 'E': { dbf = stderr; break; } case 'I': { cli_info.dest_ip = *interpret_addr2(optarg); if (zero_ip(cli_info.dest_ip)) { exit(1); } break; } case 'N': { nprocs = atoi(optarg); break; } case 'o': { numops = atoi(optarg); break; } case 'n': { fstrcpy(global_myname, optarg); break; } case 'd': { if (*optarg == 'A') DEBUGLEVEL = 10000; else DEBUGLEVEL = atoi(optarg); break; } case 'l': { slprintf(logfile, sizeof(logfile)-1, "%s.client",optarg); lp_set_logfile(logfile); break; } case 'c': { cmd_str = optarg; got_pass = True; break; } case 'h': { usage(pname); exit(0); break; } case 's': { pstrcpy(servicesf, optarg); break; } case 't': { pstrcpy(term_code, optarg); break; } default: { usage(pname); exit(1); break; } } } if (cli_action == CLIENT_NONE) { usage(pname); exit(1); } strupper(global_myname); fstrcpy(cli_info.myhostname, global_myname); DEBUG(3,("%s client started (version %s)\n",timestring(False),VERSION)); if (*smb_cli->domain == 0) { pstrcpy(smb_cli->domain,lp_workgroup()); } strupper(smb_cli->domain); load_interfaces(); if (cli_action == CLIENT_IPC) { pstrcpy(cli_info.share, "IPC$"); pstrcpy(cli_info.svc_type, "IPC"); } fstrcpy(cli_info.mach_acct, cli_info.myhostname); strupper(cli_info.mach_acct); fstrcat(cli_info.mach_acct, "$"); /* set the password cache info */ if (got_pass) { if (password[0] == 0) { pwd_set_nullpwd(&(smb_cli->pwd)); } else { pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */ } } else { char *pwd = getpass("Enter Password:"); safe_strcpy(password, pwd, sizeof(password)); pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */ } create_procs(nprocs, numops, &cli_info, smb_cli, run_enums_test); if (password[0] != 0) { create_procs(nprocs, numops, &cli_info, smb_cli, run_ntlogin_test); } fflush(out_hnd); return(0); }
/**************************************************************************** main program ****************************************************************************/ int main(int argc, const char *argv[]) { int opt; unsigned int lookup_type = 0x0; fstring lookup; static bool find_master=False; static bool lookup_by_ip = False; poptContext pc = NULL; TALLOC_CTX *frame = talloc_stackframe(); int rc = 0; struct poptOption long_options[] = { POPT_AUTOHELP { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" }, { "flags", 'f', POPT_ARG_NONE, NULL, 'f', "List the NMB flags returned" }, { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" }, { "master-browser", 'M', POPT_ARG_NONE, NULL, 'M', "Search for a master browser" }, { "recursion", 'R', POPT_ARG_NONE, NULL, 'R', "Set recursion desired in package" }, { "status", 'S', POPT_ARG_NONE, NULL, 'S', "Lookup node status as well" }, { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" }, { "root-port", 'r', POPT_ARG_NONE, NULL, 'r', "Use root port 137 (Win95 only replies to this)" }, { "lookup-by-ip", 'A', POPT_ARG_NONE, NULL, 'A', "Do a node status on <name> as an IP Address" }, POPT_COMMON_SAMBA POPT_COMMON_CONNECTION { 0, 0, 0, 0 } }; *lookup = 0; load_case_tables(); setup_logging(argv[0], DEBUG_STDOUT); pc = poptGetContext("nmblookup", argc, argv, long_options, POPT_CONTEXT_KEEP_FIRST); poptSetOtherOptionHelp(pc, "<NODE> ..."); while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case 'f': give_flags = true; break; case 'M': find_master = true; break; case 'R': recursion_desired = true; break; case 'S': find_status = true; break; case 'r': RootPort = true; break; case 'A': lookup_by_ip = true; break; case 'B': if (interpret_string_addr(&bcast_addr, poptGetOptArg(pc), NI_NUMERICHOST)) { got_bcast = True; use_bcast = True; } break; case 'U': if (interpret_string_addr(&bcast_addr, poptGetOptArg(pc), 0)) { got_bcast = True; use_bcast = False; } break; case 'T': translate_addresses = !translate_addresses; break; } } poptGetArg(pc); /* Remove argv[0] */ if(!poptPeekArg(pc)) { poptPrintUsage(pc, stderr, 0); rc = 1; goto out; } if (!lp_load_global(get_dyn_CONFIGFILE())) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE()); } load_interfaces(); if (!open_sockets()) { rc = 1; goto out; } while(poptPeekArg(pc)) { char *p; struct in_addr ip; fstrcpy(lookup,poptGetArg(pc)); if(lookup_by_ip) { struct sockaddr_storage ss; ip = interpret_addr2(lookup); in_addr_to_sockaddr_storage(&ss, ip); fstrcpy(lookup,"*"); if (!do_node_status(lookup, lookup_type, &ss)) { rc = 1; } continue; } if (find_master) { if (*lookup == '-') { fstrcpy(lookup,"\01\02__MSBROWSE__\02"); lookup_type = 1; } else { lookup_type = 0x1d; } } p = strchr_m(lookup,'#'); if (p) { *p = '\0'; sscanf(++p,"%x",&lookup_type); } if (!query_one(lookup, lookup_type)) { rc = 1; d_printf( "name_query failed to find name %s", lookup ); if( 0 != lookup_type ) { d_printf( "#%02x", lookup_type ); } d_printf( "\n" ); } } out: poptFreeContext(pc); TALLOC_FREE(frame); return rc; }
/**************************************************************************** load a list of network interfaces ****************************************************************************/ static void interpret_interfaces (char *s, struct interface **interfaces, const char *description) { char *ptr; fstring token; struct interface *iface; struct in_addr ip; ptr = s; ipzero = *interpret_addr2 ("0.0.0.0"); allones_ip = *interpret_addr2 ("255.255.255.255"); loopback_ip = *interpret_addr2 ("127.0.0.1"); while (next_token (&ptr, token, NULL, sizeof (token))) { /* parse it into an IP address/netmasklength pair */ char *p = strchr (token, '/'); if (p) *p++ = 0; ip = *interpret_addr2 (token); /* maybe we already have it listed */ { struct interface *i; for (i = (*interfaces); i; i = i->next) if (ip_equal (ip, i->ip)) break; if (i) continue; } iface = (struct interface *) malloc (sizeof (*iface)); if (!iface) return; iface->ip = ip; if (p) { if (strlen (p) > 2) iface->nmask = *interpret_addr2 (p); else iface->nmask.s_addr = htonl (((ALLONES >> atoi (p)) ^ ALLONES)); } else { default_netmask (&iface->nmask, &iface->ip); } iface->bcast.s_addr = MKBCADDR (iface->ip.s_addr, iface->nmask.s_addr); iface->next = NULL; if (!(*interfaces)) { (*interfaces) = iface; } else { last_iface->next = iface; } last_iface = iface; DEBUG (2, ("Added %s ip=%s ", description, inet_ntoa (iface->ip))); DEBUG (2, ("bcast=%s ", inet_ntoa (iface->bcast))); DEBUG (2, ("nmask=%s\n", inet_ntoa (iface->nmask))); }
/**************************************************************************** interpret a single element from a interfaces= config line This handles the following different forms: 1) wildcard interface name 2) DNS name 3) IP/masklen 4) ip/mask 5) bcast/mask ****************************************************************************/ static void interpret_interface(char *token) { struct in_addr ip, nmask; char *p; int i, added=0; zero_ip(&ip); zero_ip(&nmask); /* first check if it is an interface name */ for (i=0; i<total_probed; i++) { if (gen_fnmatch(token, probed_ifaces[i].name) == 0) { add_interface(probed_ifaces[i].ip, probed_ifaces[i].netmask); added = 1; } } if (added) return; /* maybe it is a DNS name */ p = strchr_m(token,'/'); if (!p) { ip = *interpret_addr2(token); for (i=0; i<total_probed; i++) { if (ip.s_addr == probed_ifaces[i].ip.s_addr && !ip_equal(allones_ip, probed_ifaces[i].netmask)) { add_interface(probed_ifaces[i].ip, probed_ifaces[i].netmask); return; } } DEBUG(2,("can't determine netmask for %s\n", token)); return; } /* parse it into an IP address/netmasklength pair */ *p = 0; ip = *interpret_addr2(token); *p++ = '/'; if (strlen(p) > 2) { nmask = *interpret_addr2(p); } else { nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES)); } /* maybe the first component was a broadcast address */ if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) || ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) { for (i=0; i<total_probed; i++) { if (same_net(ip, probed_ifaces[i].ip, nmask)) { add_interface(probed_ifaces[i].ip, nmask); return; } } DEBUG(2,("Can't determine ip for broadcast address %s\n", token)); return; } add_interface(ip, nmask); }
/**************************************************************************** main program ****************************************************************************/ int main(int argc,char *argv[]) { fstring base_directory; char *pname = argv[0]; int opt; extern FILE *dbf; extern char *optarg; extern int optind; pstring query_host; BOOL nt_domain_logon = False; static pstring servicesf = CONFIGFILE; pstring term_code; char *p; #ifdef KANJI pstrcpy(term_code, KANJI); #else /* KANJI */ *term_code = 0; #endif /* KANJI */ *query_host = 0; *base_directory = 0; DEBUGLEVEL = 2; setup_logging(pname,True); TimeInit(); charset_initialise(); pid = getpid(); uid = getuid(); gid = getgid(); mid = pid + 100; myumask = umask(0); umask(myumask); if (getenv("USER")) { pstrcpy(username,getenv("USER")); /* modification to support userid%passwd syntax in the USER var 25.Aug.97, [email protected] */ if ((p=strchr(username,'%'))) { *p = 0; pstrcpy(password,p+1); got_pass = True; memset(strchr(getenv("USER"),'%')+1,'X',strlen(password)); } strupper(username); } /* modification to support PASSWD environmental var 25.Aug.97, [email protected] */ if (getenv("PASSWD")) pstrcpy(password,getenv("PASSWD")); if (*username == 0 && getenv("LOGNAME")) { pstrcpy(username,getenv("LOGNAME")); strupper(username); } if (argc < 2) { usage(pname); exit(1); } if (*argv[1] != '-') { pstrcpy(service, argv[1]); /* Convert any '/' characters in the service name to '\' characters */ string_replace( service, '/','\\'); argc--; argv++; if (count_chars(service,'\\') < 3) { usage(pname); printf("\n%s: Not enough '\\' characters in service\n",service); exit(1); } if (argc > 1 && (*argv[1] != '-')) { got_pass = True; pstrcpy(password,argv[1]); memset(argv[1],'X',strlen(argv[1])); argc--; argv++; } } while ((opt = getopt(argc, argv,"s:B:O:M:S:i:Nn:d:Pp:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF) switch (opt) { case 'm': max_protocol = interpret_protocol(optarg,max_protocol); break; case 'O': pstrcpy(user_socket_options,optarg); break; case 'S': pstrcpy(desthost,optarg); strupper(desthost); nt_domain_logon = True; break; case 'B': iface_set_default(NULL,optarg,NULL); break; case 'D': pstrcpy(base_directory,optarg); break; case 'i': pstrcpy(scope,optarg); break; case 'U': { char *lp; pstrcpy(username,optarg); if ((lp=strchr(username,'%'))) { *lp = 0; pstrcpy(password,lp+1); got_pass = True; memset(strchr(optarg,'%')+1,'X',strlen(password)); } } break; case 'W': pstrcpy(workgroup,optarg); break; case 'E': dbf = stderr; break; case 'I': { dest_ip = *interpret_addr2(optarg); if (zero_ip(dest_ip)) exit(1); have_ip = True; } break; case 'n': pstrcpy(myname,optarg); break; case 'N': got_pass = True; break; case 'd': if (*optarg == 'A') DEBUGLEVEL = 10000; else DEBUGLEVEL = atoi(optarg); break; case 'l': slprintf(debugf,sizeof(debugf)-1,"%s.client",optarg); break; case 'p': port = atoi(optarg); break; case 'c': cmdstr = optarg; got_pass = True; break; case 'h': usage(pname); exit(0); break; case 's': pstrcpy(servicesf, optarg); break; case 't': pstrcpy(term_code, optarg); break; default: usage(pname); exit(1); } if (!*query_host && !*service) { usage(pname); exit(1); } DEBUG(3,("%s client started (version %s)\n",timestring(),VERSION)); if(!get_myname(myhostname,NULL)) { DEBUG(0,("Failed to get my hostname.\n")); } if (!lp_load(servicesf,True)) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf); } codepage_initialise(lp_client_code_page()); interpret_coding_system(term_code); if (*workgroup == 0) pstrcpy(workgroup,lp_workgroup()); load_interfaces(); get_myname((*myname)?NULL:myname,NULL); strupper(myname); #ifdef NTDOMAIN if (nt_domain_logon) { int ret = 0; slprintf(service,sizeof(service), "\\\\%s\\IPC$",query_host); strupper(service); connect_as_ipc = True; DEBUG(5,("NT Domain Logon. Service: %s\n", service)); if (cli_open_sockets(port)) { if (!cli_send_login(NULL,NULL,True,True,NULL)) return(1); do_nt_login(desthost, myhostname, Client, cnum); cli_send_logout(); close_sockets(); } return(ret); } #endif if (cli_open_sockets(port)) { if (!process(base_directory)) { close_sockets(); return(1); } close_sockets(); } else return(1); return(0); }
/**************************************************************************** main program ****************************************************************************/ int main(int argc,char *argv[]) { BOOL interactive = True; int opt; extern FILE *dbf; extern char *optarg; extern int optind; static pstring servicesf = CONFIGFILE; pstring term_code; char *p; BOOL got_pass = False; char *cmd_str=""; mode_t myumask = 0755; enum client_action cli_action = CLIENT_NONE; struct client_info cli_info; pstring password; /* local copy only, if one is entered */ out_hnd = stdout; fstrcpy(debugf, argv[0]); rpcclient_init(); #ifdef KANJI pstrcpy(term_code, KANJI); #else /* KANJI */ *term_code = 0; #endif /* KANJI */ DEBUGLEVEL = 2; cli_info.put_total_size = 0; cli_info.put_total_time_ms = 0; cli_info.get_total_size = 0; cli_info.get_total_time_ms = 0; cli_info.dir_total = 0; cli_info.newer_than = 0; cli_info.archive_level = 0; cli_info.print_mode = 1; cli_info.translation = False; cli_info.recurse_dir = False; cli_info.lowercase = False; cli_info.prompt = True; cli_info.abort_mget = True; cli_info.dest_ip.s_addr = 0; cli_info.name_type = 0x20; pstrcpy(cli_info.cur_dir , "\\"); pstrcpy(cli_info.file_sel, ""); pstrcpy(cli_info.base_dir, ""); pstrcpy(smb_cli->domain, ""); pstrcpy(smb_cli->user_name, ""); pstrcpy(cli_info.myhostname, ""); pstrcpy(cli_info.dest_host, ""); pstrcpy(cli_info.svc_type, "A:"); pstrcpy(cli_info.share, ""); pstrcpy(cli_info.service, ""); ZERO_STRUCT(cli_info.dom.level3_sid); ZERO_STRUCT(cli_info.dom.level5_sid); fstrcpy(cli_info.dom.level3_dom, ""); fstrcpy(cli_info.dom.level5_dom, ""); smb_cli->nt_pipe_fnum = 0xffff; TimeInit(); charset_initialise(); myumask = umask(0); umask(myumask); if (!get_myname(global_myname)) { fprintf(stderr, "Failed to get my hostname.\n"); } if (getenv("USER")) { pstrcpy(smb_cli->user_name,getenv("USER")); /* modification to support userid%passwd syntax in the USER var 25.Aug.97, [email protected] */ if ((p=strchr(smb_cli->user_name,'%'))) { *p = 0; pstrcpy(password,p+1); got_pass = True; memset(strchr(getenv("USER"),'%')+1,'X',strlen(password)); } strupper(smb_cli->user_name); } password[0] = 0; /* modification to support PASSWD environmental var 25.Aug.97, [email protected] */ if (getenv("PASSWD")) { pstrcpy(password,getenv("PASSWD")); } if (*smb_cli->user_name == 0 && getenv("LOGNAME")) { pstrcpy(smb_cli->user_name,getenv("LOGNAME")); strupper(smb_cli->user_name); } if (argc < 2) { usage(argv[0]); exit(1); } if (*argv[1] != '-') { pstrcpy(cli_info.service, argv[1]); /* Convert any '/' characters in the service name to '\' characters */ string_replace( cli_info.service, '/','\\'); argc--; argv++; fprintf(out_hnd, "service: %s\n", cli_info.service); if (count_chars(cli_info.service,'\\') < 3) { usage(argv[0]); printf("\n%s: Not enough '\\' characters in service\n", cli_info.service); exit(1); } /* if (count_chars(cli_info.service,'\\') > 3) { usage(pname); printf("\n%s: Too many '\\' characters in service\n", cli_info.service); exit(1); } */ if (argc > 1 && (*argv[1] != '-')) { got_pass = True; pstrcpy(password,argv[1]); memset(argv[1],'X',strlen(argv[1])); argc--; argv++; } cli_action = CLIENT_SVC; } while ((opt = getopt(argc, argv,"s:O:M:S:i:N:n:d:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF) { switch (opt) { case 'm': { /* FIXME ... max_protocol seems to be funny here */ int max_protocol = 0; max_protocol = interpret_protocol(optarg,max_protocol); fprintf(stderr, "max protocol not currently supported\n"); break; } case 'O': { pstrcpy(user_socket_options,optarg); break; } case 'S': { pstrcpy(cli_info.dest_host,optarg); strupper(cli_info.dest_host); cli_action = CLIENT_IPC; break; } case 'i': { extern pstring global_scope; pstrcpy(global_scope, optarg); strupper(global_scope); break; } case 'U': { char *lp; pstrcpy(smb_cli->user_name,optarg); if ((lp=strchr(smb_cli->user_name,'%'))) { *lp = 0; pstrcpy(password,lp+1); got_pass = True; memset(strchr(optarg,'%')+1,'X',strlen(password)); } break; } case 'W': { pstrcpy(smb_cli->domain,optarg); break; } case 'E': { dbf = stderr; break; } case 'I': { cli_info.dest_ip = *interpret_addr2(optarg); if (zero_ip(cli_info.dest_ip)) { exit(1); } break; } case 'n': { fstrcpy(global_myname, optarg); break; } case 'N': { got_pass = True; break; } case 'd': { if (*optarg == 'A') DEBUGLEVEL = 10000; else DEBUGLEVEL = atoi(optarg); break; } case 'l': { slprintf(debugf, sizeof(debugf)-1, "%s.client", optarg); interactive = False; break; } case 'c': { cmd_str = optarg; got_pass = True; break; } case 'h': { usage(argv[0]); exit(0); break; } case 's': { pstrcpy(servicesf, optarg); break; } case 't': { pstrcpy(term_code, optarg); break; } default: { usage(argv[0]); exit(1); break; } } } setup_logging(debugf, interactive); if (cli_action == CLIENT_NONE) { usage(argv[0]); exit(1); } strupper(global_myname); fstrcpy(cli_info.myhostname, global_myname); DEBUG(3,("%s client started (version %s)\n",timestring(False),VERSION)); if (!lp_load(servicesf,True, False, False)) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf); } codepage_initialise(lp_client_code_page()); if (*smb_cli->domain == 0) pstrcpy(smb_cli->domain,lp_workgroup()); load_interfaces(); if (cli_action == CLIENT_IPC) { pstrcpy(cli_info.share, "IPC$"); pstrcpy(cli_info.svc_type, "IPC"); } fstrcpy(cli_info.mach_acct, cli_info.myhostname); strupper(cli_info.mach_acct); fstrcat(cli_info.mach_acct, "$"); /* set the password cache info */ if (got_pass) { if (password[0] == 0) { pwd_set_nullpwd(&(smb_cli->pwd)); } else { pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */ } } else { pwd_read(&(smb_cli->pwd), "Enter Password:"******"rpcclient_connect: smb_cli->fd:%d\n", smb_cli->fd)); if (smb_cli->fd <= 0) { fprintf(stderr, "warning: connection could not be established to %s<%02x>\n", cli_info.dest_host, cli_info.name_type); fprintf(stderr, "this version of smbclient may crash if you proceed\n"); exit(-1); } switch (cli_action) { case CLIENT_IPC: { process(&cli_info, cmd_str); break; } default: { fprintf(stderr, "unknown client action requested\n"); break; } } rpcclient_stop(); return(0); }
/** interpret a single element from a interfaces= config line This handles the following different forms: 1) wildcard interface name 2) DNS name 3) IP/masklen 4) ip/mask 5) bcast/mask **/ static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token, struct iface_struct *probed_ifaces, int total_probed, struct interface **local_interfaces) { struct in_addr ip, nmask; char *p; char *address; int i, added=0; ip.s_addr = 0; nmask.s_addr = 0; /* first check if it is an interface name */ for (i=0;i<total_probed;i++) { if (gen_fnmatch(token, probed_ifaces[i].name) == 0) { add_interface(mem_ctx, probed_ifaces[i].ip, probed_ifaces[i].netmask, local_interfaces); added = 1; } } if (added) return; /* maybe it is a DNS name */ p = strchr_m(token,'/'); if (!p) { /* don't try to do dns lookups on wildcard names */ if (strpbrk(token, "*?") != NULL) { return; } ip.s_addr = interpret_addr2(token).s_addr; for (i=0;i<total_probed;i++) { if (ip.s_addr == probed_ifaces[i].ip.s_addr) { add_interface(mem_ctx, probed_ifaces[i].ip, probed_ifaces[i].netmask, local_interfaces); return; } } DEBUG(2,("can't determine netmask for %s\n", token)); return; } address = talloc_strdup(mem_ctx, token); p = strchr_m(address,'/'); /* parse it into an IP address/netmasklength pair */ *p++ = 0; ip.s_addr = interpret_addr2(address).s_addr; if (strlen(p) > 2) { nmask.s_addr = interpret_addr2(p).s_addr; } else { nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES)); } /* maybe the first component was a broadcast address */ if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) || ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) { for (i=0;i<total_probed;i++) { if (same_net_v4(ip, probed_ifaces[i].ip, nmask)) { add_interface(mem_ctx, probed_ifaces[i].ip, nmask, local_interfaces); talloc_free(address); return; } } DEBUG(2,("Can't determine ip for broadcast address %s\n", address)); talloc_free(address); return; } add_interface(mem_ctx, ip, nmask, local_interfaces); talloc_free(address); }
/**************************************************************************** main program ****************************************************************************/ int main(int argc, const char **argv) { int opt,i; char *p; int rc = 0; int argc_new = 0; const char ** argv_new; poptContext pc; struct poptOption long_options[] = { {"help", 'h', POPT_ARG_NONE, 0, 'h'}, {"workgroup", 'w', POPT_ARG_STRING, &opt_target_workgroup}, {"user", 'U', POPT_ARG_STRING, &opt_user_name, 'U'}, {"ipaddress", 'I', POPT_ARG_STRING, 0,'I'}, {"port", 'p', POPT_ARG_INT, &opt_port}, {"myname", 'n', POPT_ARG_STRING, &opt_requester_name}, {"server", 'S', POPT_ARG_STRING, &opt_host}, {"container", 'c', POPT_ARG_STRING, &opt_container}, {"comment", 'C', POPT_ARG_STRING, &opt_comment}, {"maxusers", 'M', POPT_ARG_INT, &opt_maxusers}, {"flags", 'F', POPT_ARG_INT, &opt_flags}, {"long", 'l', POPT_ARG_NONE, &opt_long_list_entries}, {"reboot", 'r', POPT_ARG_NONE, &opt_reboot}, {"force", 'f', POPT_ARG_NONE, &opt_force}, {"stdin", 'i', POPT_ARG_NONE, &opt_stdin}, {"timeout", 't', POPT_ARG_INT, &opt_timeout}, {"machine-pass",'P', POPT_ARG_NONE, &opt_machine_pass}, {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup}, {"verbose", 'v', POPT_ARG_NONE, &opt_verbose}, /* Options for 'net groupmap set' */ {"local", 'L', POPT_ARG_NONE, &opt_localgroup}, {"domain", 'D', POPT_ARG_NONE, &opt_domaingroup}, {"ntname", 'N', POPT_ARG_STRING, &opt_newntname}, {"rid", 'R', POPT_ARG_INT, &opt_rid}, /* Options for 'net rpc share migrate' */ {"acls", 0, POPT_ARG_NONE, &opt_acls}, {"attrs", 0, POPT_ARG_NONE, &opt_attrs}, {"timestamps", 0, POPT_ARG_NONE, &opt_timestamps}, {"exclude", 'e', POPT_ARG_STRING, &opt_exclude}, {"destination", 0, POPT_ARG_STRING, &opt_destination}, {"tallocreport", 0, POPT_ARG_NONE, &do_talloc_report}, POPT_COMMON_SAMBA { 0, 0, 0, 0} }; zero_ip(&opt_dest_ip); load_case_tables(); /* set default debug level to 0 regardless of what smb.conf sets */ DEBUGLEVEL_CLASS[DBGC_ALL] = 0; dbf = x_stderr; pc = poptGetContext(NULL, argc, (const char **) argv, long_options, POPT_CONTEXT_KEEP_FIRST); while((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case 'h': net_help(argc, argv); exit(0); break; case 'I': opt_dest_ip = *interpret_addr2(poptGetOptArg(pc)); if (is_zero_ip(opt_dest_ip)) d_fprintf(stderr, "\nInvalid ip address specified\n"); else opt_have_ip = True; break; case 'U': opt_user_specified = True; opt_user_name = SMB_STRDUP(opt_user_name); p = strchr(opt_user_name,'%'); if (p) { *p = 0; opt_password = p+1; } break; default: d_fprintf(stderr, "\nInvalid option %s: %s\n", poptBadOption(pc, 0), poptStrerror(opt)); net_help(argc, argv); exit(1); } } /* * Don't load debug level from smb.conf. It should be * set by cmdline arg or remain default (0) */ AllowDebugChange = False; lp_load(dyn_CONFIGFILE,True,False,False,True); argv_new = (const char **)poptGetArgs(pc); argc_new = argc; for (i=0; i<argc; i++) { if (argv_new[i] == NULL) { argc_new = i; break; } } if (do_talloc_report) { talloc_enable_leak_report(); } if (opt_requester_name) { set_global_myname(opt_requester_name); } if (!opt_user_name && getenv("LOGNAME")) { opt_user_name = getenv("LOGNAME"); } if (!opt_user_name) { opt_user_name = ""; } if (!opt_workgroup) { opt_workgroup = smb_xstrdup(lp_workgroup()); } if (!opt_target_workgroup) { opt_target_workgroup = smb_xstrdup(lp_workgroup()); } if (!init_names()) exit(1); load_interfaces(); /* this makes sure that when we do things like call scripts, that it won't assert becouse we are not root */ sec_init(); if (opt_machine_pass) { /* it is very useful to be able to make ads queries as the machine account for testing purposes and for domain leave */ net_use_krb_machine_account(); } if (!opt_password) { opt_password = getenv("PASSWD"); } rc = net_run_function(argc_new-1, argv_new+1, net_func, net_help); DEBUG(2,("return code = %d\n", rc)); return rc; }
/** return true if a IP matches a IP/netmask pair */ bool iface_same_net(const char *ip1, const char *ip2, const char *netmask) { return same_net_v4(interpret_addr2(ip1), interpret_addr2(ip2), interpret_addr2(netmask)); }
/******************************************************** Parse the next line in the lmhosts file. *********************************************************/ BOOL getlmhostsent (FILE * fp, pstring name, int *name_type, struct in_addr * ipaddr) { pstring line; while (!feof (fp) && !ferror (fp)) { pstring ip, flags, extra; char *ptr; int count = 0; *name_type = -1; if (!fgets_slash (line, sizeof (pstring), fp)) continue; if (*line == '#') continue; pstrcpy (ip, ""); pstrcpy (name, ""); pstrcpy (flags, ""); ptr = line; if (next_token (&ptr, ip, NULL, sizeof (ip))) ++count; if (next_token (&ptr, name, NULL, sizeof (pstring))) ++count; if (next_token (&ptr, flags, NULL, sizeof (flags))) ++count; if (next_token (&ptr, extra, NULL, sizeof (extra))) ++count; if (count <= 0) continue; if (count > 0 && count < 2) { DEBUG (0, ("getlmhostsent: Ill formed hosts line [%s]\n", line)); continue; } if (count >= 4) { DEBUG (0, ("getlmhostsent: too many columns in lmhosts file (obsolete syntax)\n")); continue; } DEBUG (4, ("getlmhostsent: lmhost entry: %s %s %s\n", ip, name, flags)); if (strchr (flags, 'G') || strchr (flags, 'S')) { DEBUG (0, ("getlmhostsent: group flag in lmhosts ignored (obsolete)\n")); continue; } *ipaddr = *interpret_addr2 (ip); /* Extra feature. If the name ends in '#XX', where XX is a hex number, then only add that name type. */ if ((ptr = strchr (name, '#')) != NULL) { char *endptr; ptr++; *name_type = (int) strtol (ptr, &endptr, 16); if (!*ptr || (endptr == ptr)) { DEBUG (0, ("getlmhostsent: invalid name %s containing '#'.\n", name)); continue; } *(--ptr) = '\0'; /* Truncate at the '#' */ } return True; } return False; }
/**************************************************************************** main program ****************************************************************************/ int main(int argc,char *argv[]) { int opt; unsigned int lookup_type = 0x0; pstring lookup; extern int optind; extern char *optarg; BOOL find_master=False; int i; static pstring servicesf = CONFIGFILE; BOOL lookup_by_ip = False; DEBUGLEVEL = 1; /* Prevent smb.conf setting from overridding */ AllowDebugChange = False; *lookup = 0; TimeInit(); setup_logging(argv[0],True); charset_initialise(); while ((opt = getopt(argc, argv, "d:fB:U:i:s:SMrhART")) != EOF) switch (opt) { case 'B': bcast_addr = *interpret_addr2(optarg); got_bcast = True; use_bcast = True; break; case 'f': give_flags = True; break; case 'U': bcast_addr = *interpret_addr2(optarg); got_bcast = True; use_bcast = False; break; case 'T': translate_addresses = !translate_addresses; break; case 'i': { extern pstring global_scope; pstrcpy(global_scope,optarg); strupper(global_scope); } break; case 'M': find_master = True; break; case 'S': find_status = True; break; case 'R': recursion_desired = True; break; case 'd': DEBUGLEVEL = atoi(optarg); break; case 's': pstrcpy(servicesf, optarg); break; case 'r': RootPort = True; break; case 'h': usage(); exit(0); break; case 'A': lookup_by_ip = True; break; default: usage(); exit(1); } if (argc < 2) { usage(); exit(1); } if (!lp_load(servicesf,True,False,False)) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf); } load_interfaces(); if (!open_sockets()) return(1); for (i=optind;i<argc;i++) { char *p; struct in_addr ip; fstrcpy(lookup,argv[i]); if(lookup_by_ip) { fstrcpy(lookup,"*"); ip = *interpret_addr2(argv[i]); do_node_status(ServerFD, lookup, lookup_type, ip); continue; } if (find_master) { if (*lookup == '-') { fstrcpy(lookup,"\01\02__MSBROWSE__\02"); lookup_type = 1; } else { lookup_type = 0x1d; } } p = strchr(lookup,'#'); if (p) { *p = '\0'; sscanf(++p,"%x",&lookup_type); } if (!query_one(lookup, lookup_type)) { printf( "name_query failed to find name %s", lookup ); if( 0 != lookup_type ) printf( "#%02x", lookup_type ); printf( "\n" ); } } return(0); }
bool ads_cldap_netlogon(TALLOC_CTX *mem_ctx, const char *server, const char *realm, uint32_t nt_version, struct netlogon_samlogon_response **_reply) { struct cldap_socket *cldap; struct cldap_netlogon io; struct netlogon_samlogon_response *reply; NTSTATUS status; struct in_addr addr; char addrstr[INET_ADDRSTRLEN]; const char *dest_str; int ret; struct tsocket_address *dest_addr; addr = interpret_addr2(server); dest_str = inet_ntop(AF_INET, &addr, addrstr, sizeof(addrstr)); if (!dest_str) { DEBUG(2,("Failed to resolve[%s] into an address for cldap\n", server)); return false; } ret = tsocket_address_inet_from_strings(mem_ctx, "ipv4", dest_str, LDAP_PORT, &dest_addr); if (ret != 0) { status = map_nt_error_from_unix(errno); DEBUG(2,("Failed to create cldap tsocket_address for %s - %s\n", dest_str, nt_errstr(status))); return false; } /* * as we use a connected udp socket */ status = cldap_socket_init(mem_ctx, NULL, NULL, dest_addr, &cldap); TALLOC_FREE(dest_addr); if (!NT_STATUS_IS_OK(status)) { DEBUG(2,("Failed to create cldap socket to %s: %s\n", dest_str, nt_errstr(status))); return false; } reply = talloc(cldap, struct netlogon_samlogon_response); if (!reply) { goto failed; } /* * as we use a connected socket, so we don't need to specify the * destination */ io.in.dest_address = NULL; io.in.dest_port = 0; io.in.realm = realm; io.in.host = NULL; io.in.user = NULL; io.in.domain_guid = NULL; io.in.domain_sid = NULL; io.in.acct_control = 0; io.in.version = nt_version; io.in.map_response = false; status = cldap_netlogon(cldap, NULL, reply, &io); if (!NT_STATUS_IS_OK(status)) { DEBUG(2,("cldap_netlogon() failed: %s\n", nt_errstr(status))); goto failed; } *reply = io.out.netlogon; *_reply = talloc_move(mem_ctx, &reply); TALLOC_FREE(cldap); return true; failed: TALLOC_FREE(cldap); return false; }
static NTSTATUS ipv4_connect(struct socket_context *sock, const struct socket_address *my_address, const struct socket_address *srv_address, uint32_t flags) { struct sockaddr_in srv_addr; struct in_addr my_ip; struct in_addr srv_ip; int ret; if (my_address && my_address->sockaddr) { ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen); if (ret == -1) { return map_nt_error_from_unix_common(errno); } } else if (my_address) { my_ip = interpret_addr2(my_address->addr); if (my_ip.s_addr != 0 || my_address->port != 0) { struct sockaddr_in my_addr; ZERO_STRUCT(my_addr); #ifdef HAVE_SOCK_SIN_LEN my_addr.sin_len = sizeof(my_addr); #endif my_addr.sin_addr.s_addr = my_ip.s_addr; my_addr.sin_port = htons(my_address->port); my_addr.sin_family = PF_INET; ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr)); if (ret == -1) { return map_nt_error_from_unix_common(errno); } } } if (srv_address->sockaddr) { ret = connect(sock->fd, srv_address->sockaddr, srv_address->sockaddrlen); if (ret == -1) { return map_nt_error_from_unix_common(errno); } } else { srv_ip = interpret_addr2(srv_address->addr); if (!srv_ip.s_addr) { return NT_STATUS_BAD_NETWORK_NAME; } SMB_ASSERT(srv_address->port != 0); ZERO_STRUCT(srv_addr); #ifdef HAVE_SOCK_SIN_LEN srv_addr.sin_len = sizeof(srv_addr); #endif srv_addr.sin_addr.s_addr= srv_ip.s_addr; srv_addr.sin_port = htons(srv_address->port); srv_addr.sin_family = PF_INET; ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr)); if (ret == -1) { return map_nt_error_from_unix_common(errno); } } return ip_connect_complete(sock, flags); }
/***************************************************** return a connection to a server (existing or new) *******************************************************/ struct smbw_server *smbw_server(char *server, char *share) { struct smbw_server *srv=NULL; struct cli_state c; char *username; char *password; char *workgroup; struct nmb_name called, calling; char *p, *server_n = server; fstring group; pstring ipenv; struct in_addr ip; zero_ip(&ip); ZERO_STRUCT(c); get_auth_data_fn(server, share, &workgroup, &username, &password); /* try to use an existing connection */ for (srv=smbw_srvs;srv;srv=srv->next) { if (strcmp(server,srv->server_name)==0 && strcmp(share,srv->share_name)==0 && strcmp(workgroup,srv->workgroup)==0 && strcmp(username, srv->username) == 0) return srv; } if (server[0] == 0) { errno = EPERM; return NULL; } make_nmb_name(&calling, global_myname, 0x0); make_nmb_name(&called , server, 0x20); DEBUG(4,("server_n=[%s] server=[%s]\n", server_n, server)); if ((p=strchr(server_n,'#')) && (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) { struct in_addr sip; pstring s; fstrcpy(group, server_n); p = strchr(group,'#'); *p = 0; /* cache the workgroup master lookup */ slprintf(s,sizeof(s)-1,"MASTER_%s", group); if (!(server_n = smbw_getshared(s))) { if (!find_master_ip(group, &sip)) { errno = ENOENT; return NULL; } fstrcpy(group, inet_ntoa(sip)); server_n = group; smbw_setshared(s,server_n); } } DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server)); again: slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n); zero_ip(&ip); if ((p=smbw_getshared(ipenv))) { ip = *(interpret_addr2(p)); } /* have to open a new connection */ if (!cli_initialise(&c) || !cli_connect(&c, server_n, &ip)) { errno = ENOENT; return NULL; } if (!cli_session_request(&c, &calling, &called)) { cli_shutdown(&c); if (strcmp(called.name, "*SMBSERVER")) { make_nmb_name(&called , "*SMBSERVER", 0x20); goto again; } errno = ENOENT; return NULL; } DEBUG(4,(" session request ok\n")); if (!cli_negprot(&c)) { cli_shutdown(&c); errno = ENOENT; return NULL; } if (!cli_session_setup(&c, username, password, strlen(password), password, strlen(password), workgroup) && /* try an anonymous login if it failed */ !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) { cli_shutdown(&c); errno = EPERM; return NULL; } DEBUG(4,(" session setup ok\n")); if (!cli_send_tconX(&c, share, "?????", password, strlen(password)+1)) { errno = smbw_errno(&c); cli_shutdown(&c); return NULL; } smbw_setshared(ipenv,inet_ntoa(ip)); DEBUG(4,(" tconx ok\n")); srv = (struct smbw_server *)malloc(sizeof(*srv)); if (!srv) { errno = ENOMEM; goto failed; } ZERO_STRUCTP(srv); srv->cli = c; srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share)); srv->server_name = strdup(server); if (!srv->server_name) { errno = ENOMEM; goto failed; } srv->share_name = strdup(share); if (!srv->share_name) { errno = ENOMEM; goto failed; } srv->workgroup = strdup(workgroup); if (!srv->workgroup) { errno = ENOMEM; goto failed; } srv->username = strdup(username); if (!srv->username) { errno = ENOMEM; goto failed; } /* some programs play with file descriptors fairly intimately. We try to get out of the way by duping to a high fd number */ if (fcntl(SMBW_CLI_FD + srv->cli.fd, F_GETFD) && errno == EBADF) { if (dup2(srv->cli.fd,SMBW_CLI_FD+srv->cli.fd) == srv->cli.fd+SMBW_CLI_FD) { close(srv->cli.fd); srv->cli.fd += SMBW_CLI_FD; } } DLIST_ADD(smbw_srvs, srv); return srv; failed: cli_shutdown(&c); if (!srv) return NULL; SAFE_FREE(srv->server_name); SAFE_FREE(srv->share_name); SAFE_FREE(srv); return NULL; }