Ejemplo n.º 1
0
void  my_Update_PowerDNS( dhcp_message *message ) {
    MYSQL_RES	*res;
    char		*hname, *dname;
    char		qbuf[500];
    int		numr;

    hname = Lookup_ConfigOptsMacs( message, 12 );
    if (hname == NULL) return;
    dname = Lookup_ConfigOpts( message, 15 );
    if (dname == NULL) return;

    snprintf(qbuf,500,"delete from records where "
             "name = concat('%s','.','%s') and content != '%s'", hname, dname, message->s_ipaddr );
    fprintf(stderr,"qv %d %60s\n", strlen(qbuf), qbuf );
    if ( mysql_query( my_pdns_sock, qbuf ) ) {
        fprintf( stderr, "sql error\nq=%s \ne=%s\n", qbuf,
                 mysql_error( my_pdns_sock ) );
        return;
    }

    snprintf(qbuf,500,"delete from records where "
             "name != concat('%s','.','%s') and content = '%s'", hname, dname, message->s_ipaddr );
    fprintf(stderr,"qv %d %60s\n", strlen(qbuf), qbuf );
    if ( mysql_query( my_pdns_sock, qbuf ) ) {
        fprintf( stderr, "sql error\nq=%s \ne=%s\n", qbuf,
                 mysql_error( my_pdns_sock ) );
        return;
    }

    snprintf(qbuf,500,"select * from records where name = concat('%s','.','%s') and content = '%s'",
             hname, dname, message->s_ipaddr );
    fprintf(stderr,"qv %d %60s\n", strlen(qbuf), qbuf );
    if ( mysql_query( my_pdns_sock, qbuf ) ) {
        fprintf( stderr, "sql error\nq=%s \ne=%s\n", qbuf,
                 mysql_error( my_pdns_sock ) );
        return;
    }
    res = mysql_store_result( my_pdns_sock );
    if (res != NULL) {
        numr = mysql_num_rows( res );
        if (numr != 0) {
            mysql_free_result(res);
            return;
        }
        mysql_free_result(res);
    }

    snprintf(qbuf,500,"insert into records (domain_id,name,type,content,ttl) values "
             "( (select id from domains where name = '%s'), "
             "concat('%s','.','%s'),'A','%s',600)",
             dname, hname, dname, message->s_ipaddr );

    fprintf(stderr,"qv %d %60s\n", strlen(qbuf), qbuf );
    if ( mysql_query( my_pdns_sock, qbuf ) ) {
        fprintf( stderr, "sql error\nq=%s \ne=%s\n", qbuf,
                 mysql_error( my_pdns_sock ) );
        return;
    }
}
Ejemplo n.º 2
0
int send_positive_message( dhcp_message *message, int mess_type ) {
	static char	*newip_pref = NULL;
	u_int32_t	lease_time;
	ConfigNets	*netopts;

	/* check SQL table for this MAC address */
	my_findMAC( message );

	if (message->lease_type == LEASE_NOT_FOUND && message->in_opts.docsis_modem == 1 ) {
		my_syslog(LOG_INFO, "NAK -- unknown docsis modem: %s : %s ", message->s_macaddr,message->in_opts.vsi_model );
		// my_findMAC_CMUNKNOWN( message );
		send_NAK( message );
		return 1;
	}

	if (message->lease_type == LEASE_NOT_FOUND && message->in_opts.docsis_modem == 2 ) {
		my_syslog(LOG_INFO, "NAK -- unknown mta from modem: %s ", message->s_modem_macaddr);
		// message->lease_type = LEASE_REJECT;
		send_NAK( message );
		return 1;
	}

	if (message->lease_type == LEASE_REJECT) { return 0; }

	if (message->lease_type == LEASE_CPE &&
	    message->cpe_type == CPE_DYNAMIC &&
	    message->lockip == 0 &&
	    message->lease_time < 60 ) {
		// Your Lease has expired. You get a new IP!
		my_DeleteLease( message );
		message->lease_type = LEASE_NOT_FOUND;
	}

	if ( Verify_Vlan( message ) ) {
		if (message->lease_type == LEASE_CM ) {
			my_syslog(LOG_WARNING,
								"NEW CM GIaddr mismatch (the CM moved) gi %s ip %s vlan %d",
								message->in_giaddr, message->s_ipaddr, message->vlan );
			message->ipaddr = message->b_ipaddr = 0;
			message->s_ipaddr[0] = 0;
		}
    if (message->lease_type == LEASE_MTA ) {
      my_syslog(LOG_WARNING,
	              "NEW MTA GIaddr mismatch (the CM moved) gi %s ip %s vlan %d",
	              message->in_giaddr, message->s_ipaddr, message->vlan );
      message->ipaddr = message->b_ipaddr = 0;
      message->s_ipaddr[0] = 0;
    }
		if (message->lease_type == LEASE_CPE ) {
			my_syslog(LOG_WARNING, "NEW -- GIaddr does not match vlan - gi %s ip %s vlan %d",
				message->in_giaddr, message->s_ipaddr, message->vlan );
			if (message->cpe_type == CPE_STATIC) {
				my_DeleteLease( message );
				message->lease_type = LEASE_NOT_FOUND;
			} else {
				// if message->cpe_type == CPE_DYNAMIC
				my_DeleteLease( message ); /* added 29.11.2014 */
				message->lease_type = LEASE_NOAUTH;
			}
		}
	}

	if (message->lease_type == LEASE_NOT_FOUND && message->in_opts.agent_mac_len == 0) {
		my_syslog(LOG_WARNING, "NAK -- CPE asking for new IP directly - mac %s",
			message->s_macaddr );
		send_NAK( message );
		return 0;
	}

	if (newip_pref == NULL) newip_pref = GetConfigVar( "assign-new-ip" );

	if (message->lease_type == LEASE_NOT_FOUND) {
		if (*newip_pref == 'n') return 0;

		my_getNewIP_CPE( message );

		if (message->ipaddr == 0) {
			send_NAK( message );
			return 1;
		}
		if (message->lease_type == LEASE_CPE) {
			if (message->cpe_type == CPE_STATIC) {
				my_syslog(LOG_INFO, "NEW STATIC -- mac %s ip %s vlan %d",
					message->s_macaddr, message->s_ipaddr, message->vlan );
			}
			if (message->cpe_type == CPE_DYNAMIC) {
				my_syslog(LOG_INFO, "NEW DYNAMIC -- mac %s ip %s vlan %d",
					message->s_macaddr, message->s_ipaddr, message->vlan );
			}
		}
		if (message->lease_type == LEASE_NOAUTH) {
			my_syslog(LOG_INFO, "NEW NOAUTH -- mac %s ip %s vlan %d",
				message->s_macaddr, message->s_ipaddr, message->vlan );
                	send_NAK( message ); /* added 29.11.2014 */
                	return 1;
		}
	}

	if ( my_Get_Net( message ) ) {
		my_syslog(LOG_WARNING, "NAK -- no subnet for ip %s mac %s vlan %d, lease_type %d",
			message->s_ipaddr, message->s_macaddr, message->vlan, message->lease_type );
		send_NAK( message );
		return 1;
	}

	if (message->in_opts.request_addr != 0 ) {
		if (message->ipaddr != message->in_opts.request_addr ) {
			my_syslog(LOG_ERR, "WARN -- bad ip requested %s mac %s real-ip %s",
				message->in_opts.s_request_addr, message->s_macaddr, message->s_ipaddr );
		}
	}

	netopts = Get_Net_Opts( message->netptr );

	message->out_pack.op    = BOOTREPLY;
	message->out_pack.htype = ETH_10MB;
	message->out_pack.hlen  = ETH_10MB_LEN;
	message->out_pack.hops  = message->in_pack.hops;
	message->out_pack.flags = message->in_pack.flags;
	message->out_pack.xid   = message->in_pack.xid;

	message->out_pack.ciaddr = message->in_pack.ciaddr;
	message->out_pack.yiaddr = message->ipaddr;
	message->out_pack.siaddr = message->server_ip;
	message->out_pack.giaddr = message->in_pack.giaddr;
	message->out_pack.cookie = DHCP_MAGIC;

	memcpy( message->out_pack.macaddr, message->macaddr, 6 );

	initOpt( message );

	if (mess_type == DHCP_OFFER) {
		addOpt( 0x35, 0x01, "\x02");
	}
	if (mess_type == DHCP_ACK) {
		addOpt( 0x35, 0x01, "\x05");
	}

	// subnet mask
	addOpt( 0x01, 0x04, (char *) &(netopts->mask) );

	// gateway
	addOpt( 0x03, 0x04, (char *) &(netopts->gateway) );

	// Broadcast Address
	addOpt( 0x1c, 0x04, (char *) &(netopts->broadcast) );

	// Lease Times
        message->lease_time = netopts->lease_time;
        lease_time = htonl( message->lease_time );
        addOpt( 0x33, 0x04, (char *) &lease_time );
        lease_time = htonl( message->lease_time / 2 );
        addOpt( 0x3A, 0x04, (char *) &lease_time );
        lease_time = htonl( message->lease_time / 100 * 87.5 );
        addOpt( 0x3B, 0x04, (char *) &lease_time );


	addBigOpt( my_Get_Opt( 1, message->b_macaddr ) );
	addBigOpt( my_Get_Opt( message->opt, 0 ) );
	if (netopts->opt1 != message->opt)
		addBigOpt( my_Get_Opt( netopts->opt1, 0 ) );
	if (netopts->opt2 != message->opt)
		addBigOpt( my_Get_Opt( netopts->opt2, 0 ) );
	if (netopts->opt3 != message->opt)
		addBigOpt( my_Get_Opt( netopts->opt3, 0 ) );

	/* Config File Name */
	if (message->cfname != NULL && message->cfname[0] != 0) {
		strncpy( message->out_pack.file, message->cfname, 127 );
		addOpt( 0x43, strlen( message->out_pack.file ), message->cfname );
	} else {
		char	*cf = Lookup_ConfigOptsMacs(message,0x43);
		if (cf != NULL)
			strncpy( message->out_pack.file, cf, 127 );
	}

	/* Add Terminator */
	addOpt( 0xff, 0x00, NULL );

	my_dhcplog( mess_type, message );

	sendPacket( message );

	// if (mess_type == DHCP_OFFER &&
	if (message->lease_type == LEASE_CM) {
		// Update AgentID of cablemodem
		my_UpdateAgent( message );
	}
        if (message->lease_type == LEASE_MTA) {
                // Update AgentID of cablemodem
                my_UpdateAgent( message );
        }

	if (message->lease_type == LEASE_UNKNOWN) {
		// Update AgentID of cablemodem
		my_UpdateAgent( message );
	}

	/* if a customer pc, update the leases table, if a cable modem dont bother */
	if (message->lease_type == LEASE_CPE || message->lease_type == LEASE_NOAUTH ) {
		my_UpdateLease( message );
	}
	return 1;
}