Ejemplo n.º 1
0
/*
  return the IP of the currently active wins server for the given tag,
  or the zero IP otherwise
*/
struct in_addr wins_srv_ip_tag(const char *tag, struct in_addr src_ip)
{
	const char **list;
	int i;
	struct tagged_ip t_ip;

	/* if we are a wins server then we always just talk to ourselves */
	if (lp_we_are_a_wins_server()) {
		struct in_addr loopback_ip;
		loopback_ip.s_addr = htonl(INADDR_LOOPBACK);
		return loopback_ip;
	}

	list = lp_wins_server_list();
	if (!list || !list[0]) {
		struct in_addr ip;
		zero_ip_v4(&ip);
		return ip;
	}

	/* find the first live one for this tag */
	for (i=0; list[i]; i++) {
		parse_ip(&t_ip, list[i]);
		if (strcmp(tag, t_ip.tag) != 0) {
			/* not for the right tag. Move along */
			continue;
		}
		if (!wins_srv_is_dead(t_ip.ip, src_ip)) {
			fstring src_name;
			fstrcpy(src_name, inet_ntoa(src_ip));
			DEBUG(6,("Current wins server for tag '%s' with source %s is %s\n", 
				 tag, 
				 src_name,
				 inet_ntoa(t_ip.ip)));
			return t_ip.ip;
		}
	}

	/* they're all dead - try the first one until they revive */
	for (i=0; list[i]; i++) {
		parse_ip(&t_ip, list[i]);
		if (strcmp(tag, t_ip.tag) != 0) {
			continue;
		}
		return t_ip.ip;
	}

	/* this can't happen?? */
	zero_ip_v4(&t_ip.ip);
	return t_ip.ip;
}
static struct work_record *create_workgroup(const char *name, int ttl)
{
	struct work_record *work;
	struct subnet_record *subrec;
	int t = -1;
  
	if((work = SMB_MALLOC_P(struct work_record)) == NULL) {
		DEBUG(0,("create_workgroup: malloc fail !\n"));
		return NULL;
	}
	memset((char *)work, '\0', sizeof(*work));

	name_to_unstring(work->work_group, name);

	work->serverlist = NULL;
  
	work->RunningElection = False;
	work->ElectionCount = 0;
	work->announce_interval = 0;
	work->needelection = False;
	work->needannounce = True;
	work->lastannounce_time = time(NULL);
	work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
	work->dom_state = DOMAIN_NONE;
	work->log_state = LOGON_NONE;
  
	work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;

	/* Make sure all token representations of workgroups are unique. */
  
	for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
		struct work_record *w;
		for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
			if (strequal(w->work_group, work->work_group))
				t = w->token;
		}
	}
  
	if (t == -1)
		work->token = ++workgroup_count;
	else
		work->token = t;
  
	/* No known local master browser as yet. */
	*work->local_master_browser_name = '\0';

	/* No known domain master browser as yet. */
	*work->dmb_name.name = '\0';
	zero_ip_v4(&work->dmb_addr);

	/* WfWg  uses 01040b01 */
	/* Win95 uses 01041501 */
	/* NTAS  uses ???????? */
	work->ElectionCriterion  = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8); 
	work->ElectionCriterion |= (lp_os_level() << 24);
	if (lp_domain_master())
		work->ElectionCriterion |= 0x80;
  
	return work;
}
Ejemplo n.º 3
0
bool create_subnets(void)
{
	/* We only count IPv4 interfaces whilst we're waiting. */
	int num_interfaces;
	int i;
	struct in_addr unicast_ip, ipzero;

  try_interfaces_again:

	/* Only count IPv4, non-loopback interfaces. */
	if (iface_count_v4_nl() == 0) {
		DEBUG(0,("create_subnets: No local IPv4 non-loopback interfaces !\n"));
		DEBUG(0,("create_subnets: Waiting for an interface to appear ...\n"));
	}

	/* We only count IPv4, non-loopback interfaces here. */
	while (iface_count_v4_nl() == 0) {
		void (*saved_handler)(int);

		/*
		 * Whilst we're waiting for an interface, allow SIGTERM to
		 * cause us to exit.
		 */

		saved_handler = CatchSignal( SIGTERM, SIGNAL_CAST SIG_DFL );

		sleep(5);
		load_interfaces();

		/*
		 * We got an interface, restore our normal term handler.
		 */

		CatchSignal( SIGTERM, SIGNAL_CAST saved_handler );
	}

	/*
	 * Here we count v4 and v6 - we know there's at least one
	 * IPv4 interface and we filter on it below.
	 */
	num_interfaces = iface_count();

	/*
	 * Create subnets from all the local interfaces and thread them onto
	 * the linked list.
	 */

	for (i = 0 ; i < num_interfaces; i++) {
		const struct interface *iface = get_interface(i);

		if (!iface) {
			DEBUG(2,("create_subnets: can't get interface %d.\n", i ));
			continue;
		}

		/* Ensure we're only dealing with IPv4 here. */
		if (iface->ip.ss_family != AF_INET) {
			DEBUG(2,("create_subnets: "
				"ignoring non IPv4 interface.\n"));
			continue;
		}

		/*
		 * We don't want to add a loopback interface, in case
		 * someone has added 127.0.0.1 for smbd, nmbd needs to
		 * ignore it here. JRA.
		 */

		if (is_loopback_addr((struct sockaddr *)&iface->ip)) {
			DEBUG(2,("create_subnets: Ignoring loopback interface.\n" ));
			continue;
		}

		if (!make_normal_subnet(iface))
			return False;
	}

        /* We must have at least one subnet. */
	if (subnetlist == NULL) {
		void (*saved_handler)(int);

		DEBUG(0,("create_subnets: Unable to create any subnet from "
				"given interfaces. Is your interface line in "
				"smb.conf correct ?\n"));

		saved_handler = CatchSignal( SIGTERM, SIGNAL_CAST SIG_DFL );

		sleep(5);
		load_interfaces();

		CatchSignal( SIGTERM, SIGNAL_CAST saved_handler );
		goto try_interfaces_again;
	}

	if (lp_we_are_a_wins_server()) {
		/* Pick the first interface IPv4 address as the WINS server
		 * ip. */
		const struct in_addr *nip = first_ipv4_iface();

		if (!nip) {
			return False;
		}

		unicast_ip = *nip;
	} else {
		/* note that we do not set the wins server IP here. We just
			set it at zero and let the wins registration code cope
			with getting the IPs right for each packet */
		zero_ip_v4(&unicast_ip);
	}

	/*
	 * Create the unicast and remote broadcast subnets.
	 * Don't put these onto the linked list.
	 * The ip address of the unicast subnet is set to be
	 * the WINS server address, if it exists, or ipzero if not.
	 */

	unicast_subnet = make_subnet( "UNICAST_SUBNET", UNICAST_SUBNET, 
				unicast_ip, unicast_ip, unicast_ip);

	zero_ip_v4(&ipzero);

	remote_broadcast_subnet = make_subnet( "REMOTE_BROADCAST_SUBNET",
				REMOTE_BROADCAST_SUBNET,
				ipzero, ipzero, ipzero);

	if((unicast_subnet == NULL) || (remote_broadcast_subnet == NULL))
		return False;

	/* 
	 * If we are WINS server, create the WINS_SERVER_SUBNET - don't put on
	 * the linked list.
	 */

	if (lp_we_are_a_wins_server()) {
		if( (wins_server_subnet = make_subnet( "WINS_SERVER_SUBNET",
						WINS_SERVER_SUBNET, 
						ipzero, ipzero, ipzero )) == NULL )
			return False;
	}

	return True;
}
Ejemplo n.º 4
0
int vscan_send_warning_message(const char *filename, const char *virname, const char *ipaddr) {
    struct in_addr ip;
    struct sockaddr_storage ss;

        struct nmb_name called, calling;
	pstring myname;
	pstring message;
	pstring shortfilename;
	char* lastslash;

	static pstring lastfile;
	static pstring lastip;

	#if SAMBA_VERSION_MAJOR==3
	fstrcpy(remote_machine, get_remote_machine_name());
	DEBUG(5, ("remote machine is: %s\n", remote_machine));
	#endif

	/* Only notify once for a given virus/ip combo - otherwise the
	 * scanner will go crazy reaccessing the file and sending
	 * messages once the user hits the "okay" button */
	if (strncmp(lastfile,filename,sizeof(pstring)) == 0) {
		if (strncmp(lastip,ipaddr,sizeof(pstring)) == 0) {
			DEBUG(5,("Both IP and Filename are the same, not notifying\n"));
			return 0;
		}
	}

	ZERO_ARRAY(lastfile);
	ZERO_ARRAY(lastip);
	pstrcpy(lastfile,filename);
	pstrcpy(lastip,ipaddr);

	ZERO_ARRAY(myname);
	pstrcpy(myname,myhostname());

	ZERO_ARRAY(username);
	/* could make this configurable */
	snprintf(username,sizeof(pstring)-1,"%s VIRUS SCANNER",myname);

	/* We need to get the real ip structure from the ip string
	 * is this info already available somewhere else in samba? */
       	zero_ip_v4(&ip);
	if (inet_aton(ipaddr,&ip) == 0) {
               	DEBUG(5,("Cannot resolve ip address %s\n", ipaddr));
               	return 1;
	}
    in_addr_to_sockaddr_storage(&ss, ip);


       	make_nmb_name(&calling, myname, 0x0);
       	make_nmb_name(&called , remote_machine, name_type);

	 if (!(cli=cli_initialise())) {
               	DEBUG(5,("Connection to %s failed\n", remote_machine));
               	return 1;
       	}
        cli_set_port(cli, port);
     if (!NT_STATUS_IS_OK(cli_connect(cli, remote_machine, &ss))) {
               	DEBUG(5,("Connection to %s failed\n", remote_machine));
               	return 1;
    }

       	if (!cli_session_request(cli, &calling, &called)) {
               	DEBUG(5,("session request failed\n"));
               	cli_shutdown(cli);
               	return 1;
       	}

	ZERO_ARRAY(shortfilename);
	/* we don't want the entire filename, otherwise the message service may choke
	 * so we chop off the path up to the very last forward-slash
	 * assumption: unix-style pathnames in filename (don't know if there's a
	 * portable file-separator variable... */
	lastslash = strrchr(filename,'/');
	if (lastslash != NULL && lastslash != filename) {
		pstrcpy(shortfilename,lastslash+1);
	} else {
		pstrcpy(shortfilename,filename);
	}

	ZERO_ARRAY(message);
	/* could make the message configurable and language specific? */
	snprintf(message,sizeof(pstring)-1,
		"%s IS INFECTED WITH VIRUS  %s.\r\n\r\nAccess will be denied.\r\nPlease contact your system administrator",
		shortfilename, virname);

	/* actually send the message... */
       	send_message(message);

       	cli_shutdown(cli);
	
        return 0;
}
Ejemplo n.º 5
0
static void find_domain_master_name_query_success(struct subnet_record *subrec,
        struct userdata_struct *userdata_in,
        struct nmb_name *q_name, struct in_addr answer_ip, struct res_rec *rrec)
{
    /*
     * Unfortunately, finding the IP address of the Domain Master Browser,
     * as we have here, is not enough. We need to now do a sync to the
     * SERVERNAME<0x20> NetBIOS name, as only recent NT servers will
     * respond to the SMBSERVER name. To get this name from IP
     * address we do a Node status request, and look for the first
     * NAME<0x20> in the response, and take that as the server name.
     * We also keep a cache of the Domain Master Browser name for this
     * workgroup in the Workgroup struct, so that if the same IP addess
     * is returned every time, we don't need to do the node status
     * request.
     */

    struct work_record *work;
    struct nmb_name nmbname;
    struct userdata_struct *userdata;
    size_t size = sizeof(struct userdata_struct) + sizeof(fstring)+1;
    unstring qname;

    pull_ascii_nstring(qname, sizeof(qname), q_name->name);
    if( !(work = find_workgroup_on_subnet(subrec, qname)) ) {
        if( DEBUGLVL( 0 ) ) {
            dbgtext( "find_domain_master_name_query_success:\n" );
            dbgtext( "Failed to find workgroup %s\n", qname);
        }
        return;
    }

    /* First check if we already have a dmb for this workgroup. */

    if(!is_zero_ip_v4(work->dmb_addr) && ip_equal_v4(work->dmb_addr, answer_ip)) {
        /* Do the local master browser announcement to the domain
        	master browser name and IP. */
        announce_local_master_browser_to_domain_master_browser( work );

        /* Now synchronise lists with the domain master browser. */
        sync_with_dmb(work);
        return;
    } else {
        zero_ip_v4(&work->dmb_addr);
    }

    /* Now initiate the node status request. */

    /* We used to use the name "*",0x0 here, but some Windows
     * servers don't answer that name. However we *know* they
     * have the name workgroup#1b (as we just looked it up).
     * So do the node status request on this name instead.
     * Found at LBL labs. JRA.
     */

    make_nmb_name(&nmbname,work->work_group,0x1b);

    /* Put the workgroup name into the userdata so we know
     what workgroup we're talking to when the reply comes
     back. */

    /* Setup the userdata_struct - this is copied so we can use
    a stack variable for this. */

    if((userdata = (struct userdata_struct *)SMB_MALLOC(size)) == NULL) {
        DEBUG(0, ("find_domain_master_name_query_success: malloc fail.\n"));
        return;
    }

    userdata->copy_fn = NULL;
    userdata->free_fn = NULL;
    userdata->userdata_len = strlen(work->work_group)+1;
    overmalloc_safe_strcpy(userdata->data, work->work_group, size - sizeof(*userdata) - 1);

    node_status( subrec, &nmbname, answer_ip,
                 domain_master_node_status_success,
                 domain_master_node_status_fail,
                 userdata);

    zero_free(userdata, size);
}