예제 #1
0
파일: tools.c 프로젝트: Ancient/NetGuard
void sprint_package(char* buffer, unsigned int *vlanid, struct tpacket_hdr *h,struct ether_header *eth, struct iphdr *ip, struct tcphdr *tcp, void *data)
{
	char *buffer2 = (char*)malloc(5000);
	sprintmac(buffer2,eth->ether_shost);
	sprintf(buffer,"(hw)from: %s",buffer2);
	sprintmac(buffer2,eth->ether_dhost);
	sprintf(buffer,"%s (hw)to: %s len:%4d",buffer,buffer2,h->tp_len);

	if ((*vlanid) > 0) {
		sprintf(buffer,"%s (vlan:%4d)",buffer,*vlanid);
	} else  sprintf(buffer,"%s            ",buffer);

	sprintf(buffer,"%s %6s (0x%04x)",buffer,tok2str(str_ethertype_values,"n.k.", ntohs(eth->ether_type)),ntohs(eth->ether_type));
	
	if (eth->ether_type == htons(ETHERTYPE_IP)) {
		sprintf(buffer,"%s %-4s",buffer,tok2str(str_proto_values,"n.k.", ip->protocol)      );
		sprintf(buffer,"%s %-15s:%-6d -->",buffer,inet_ntoa(*(struct in_addr *)&ip->saddr),ntohs(tcp->source));
		sprintf(buffer,"%s %-15s:%-6d",buffer,inet_ntoa(*(struct in_addr *)&ip->daddr), ntohs(tcp->dest));
	} else if (eth->ether_type == htons(ETHERTYPE_ARP)) {
		struct ether_arp * arph = (struct ether_arp *)ip;
		sprintf(buffer,"%s arp %7s",buffer,tok2str(str_arpop_values,"n.k.", ntohs(arph->arp_op)));
		sprintf(buffer,"%s %-15s -->",buffer,inet_ntoa(*(struct in_addr *)arph->arp_spa));
		sprintf(buffer,"%s %-15s",buffer,inet_ntoa(*(struct in_addr *)arph->arp_tpa));
		sprintf(buffer,"%s from: ",buffer);
		
		sprintmac(buffer2,arph->arp_sha);
		sprintf(buffer,"%s %s",buffer,buffer2);

		sprintmac(buffer2,arph->arp_tha);
		sprintf(buffer,"%s to %s",buffer,buffer2);
	}

	free(buffer2);
	return;
}
static int target_set_mac(target_context_t *tc, const unsigned char *mac)
{
	char buf [CMDBUF_LENGTH];

	/* set MAC address on target */
	strcpy(buf, "mac ");
	sprintmac(buf + 4, mac);
	msgf("setting target MAC to `%s'\n", buf + 4);
	target_write_command(tc, buf);
	if(target_confirm_response(tc) == -1){
		return -1;
	}
	
	/* update our local copy on success */
	memcpy(tc->remote_mac, mac, 6);
	return 0;
}
예제 #3
0
bool NetGuard_User_SCE_Maconoff::exec_state_change(NetGuard_User_State *user, NetGuard_State **from, NetGuard_State *to,std::string reason)
{
	char swip[100];
	int swport;
	char *argv[255];

	if (user->Getuser().vlan_id != my_instance->mof_vlan_id) //if it is not our vlan -> return false which  results in use of another handler
		return false;

	//this handler make sure we do the actions we want on enable and disable
	//it always have to return true on the -> enabled or ->disabled state as we handle them - no matter if the transition itself failed or not!
	ng_slogdebug_spam("NetGuard_User_SCE_Maconoff","enter exec state change from <%s> to <%s> (user: %s vlan: %d) - reason %s",(*from)->GetName().c_str(),to->GetName().c_str(),inet_ntoa(*(struct in_addr *)&user->Getuser().saddr),user->Getuser().vlan_id,reason.c_str());

	char *tmp = (char*)malloc(sizeof(unsigned char)*20);
	sprintmac(tmp,(*(user->params()->GetMac("mac"))));
	std::string my_mac = tmp;
	free(tmp);

	ng_slogdebug_spam("NetGuard_User_SCE_Maconoff","have mac %s",my_mac.c_str());


	if ((*to) == GlobalCFG::GetStr("state.disabled","disabled"))
	{
		ng_slogdebug("NetGuard_User_SCE_Maconoff","exec state change from <%s> to <%s> (user: %s vlan: %d) - reason '%s'",(*from)->GetName().c_str(),to->GetName().c_str(),inet_ntoa(*(struct in_addr *)&user->Getuser().saddr),user->Getuser().vlan_id,reason.c_str());

		//can we find a room?
	    if(my_instance->resolve_room(&user->Getuser().saddr,swip,&swport))
	    {	
			char *tmpstr=(char*)malloc(STRSIZE);
			snprintf(tmpstr,STRSIZE-2,"%s -m s -s disable -i %s -p %i -a %s",GlobalCFG::GetStr("mof.maconoff","./maconoff").c_str(),swip,swport,my_mac.c_str());
			my_instance->parse_cmd(tmpstr,argv);
			//can we execute the command?
			if (my_instance->run_maconoff(argv)) {
				set_failure_state(user,"error on run_maconoff - disable"); //we could not execute -> set to failure state
			} else (*from) = to;
			free(tmpstr);			
	    } else {
			ng_slogerror("NetGuard_User_SCE_Maconoff","disable - could not find switch for user (user: %s vlan: %d)",inet_ntoa(*(struct in_addr *)&user->Getuser().saddr),user->Getuser().vlan_id);
			set_failure_state(user,"disable - could not find switch for user");
		}
		return true;
	} else if ((*to) == GlobalCFG::GetStr("state.enabled","enabled")) {
		if ((**from) == GlobalCFG::GetStr("state.learn","learn")) 
			return false;

		ng_slogdebug("NetGuard_User_SCE_Maconoff","exec state change from <%s> to <%s> (user: %s vlan: %d) - reason '%s'",(*from)->GetName().c_str(),to->GetName().c_str(),inet_ntoa(*(struct in_addr *)&user->Getuser().saddr),user->Getuser().vlan_id,reason.c_str());

		//can we find a room?
		if(my_instance->resolve_room(&user->Getuser().saddr,swip,&swport))
	    {	
			char *tmpstr=(char*)malloc(STRSIZE);
			snprintf(tmpstr,STRSIZE-2,"%s -m s -s enable -i %s -p %i -a %s",GlobalCFG::GetStr("mof.maconoff","./maconoff").c_str(),swip,swport,my_mac.c_str());
			my_instance->parse_cmd(tmpstr,argv);
			//can we execute the command?
			if (my_instance->run_maconoff(argv)) {
				set_failure_state(user,"error on run_maconoff - enable"); //we could not execute -> set to failure state
			} else (*from) = to;
			free(tmpstr);			
	    } else {
			ng_slogerror("NetGuard_User_SCE_Maconoff","enable - could not find switch for user (user: %s vlan: %d)",inet_ntoa(*(struct in_addr *)&user->Getuser().saddr),user->Getuser().vlan_id);
			set_failure_state(user,"enable - could not find switch for user");
		}
		return true;
	}
	return false;
}