Undernet Radius

Матеріал з NoDeny
Версія від 17:48, 22 червня 2023, створена Sv (обговорення | внесок) (Защитил страницу Undernet Radius ([Редактирование=Разрешено только администраторам] (бессрочно) [Переименование=Разрешено только администраторам] (бессрочно)))
Перейти до навігації Перейти до пошуку

Установка Radius

apt install freeradius freeradius-mysql
rm /etc/freeradius/3.0/sites-enabled/default
cp /usr/local/nodeny/etc/raddb/clients.conf /etc/freeradius/3.0/

Конфигурирование

nano /etc/freeradius/3.0/sites-enabled/nodeny

Вставляем следующий текст:

server default {
    listen {
        type = auth
        ipaddr = *
        port = 1812
    }
    listen {
        type = acct
        ipaddr = *
        port = 0
    }
    authorize {
        sql
        update control {
          Auth-Type := Accept
        }
    }
    authenticate {
    }
    preacct {
        acct_unique
        preprocess
    }
    accounting {
        sql
    }
    session {
        radutmp
        sql
    }
    post-auth {
        sql
    }
    Post-Auth-Type ACCEPT {
        sql
    }
}

Sql конфиг

В консоли:

nano /etc/freeradius/3.0/mods-enabled/sql

Вставляем следующий текст:

sql {
    driver = "rlm_sql_mysql"
    mysql {
        warnings = auto
    }
    server = "localhost"
    port = 3306
    login = "nodeny"
    password = "hardpass"
    radius_db = "nodeny"

    read_groups = no
    #authorize_check_query = "call radcheck('%{User-Name}')"
    authorize_reply_query = "call radreply('%{User-Name}')"
    accounting {
        reference = "%{tolower:type.%{Acct-Status-Type}.query}"
        type {
            start {
                query = "call radupdate('%{reply:Callback-Number}','%{Framed-IP-Address}',\
                        'user=%{Calling-Station-Id};nas=%{NAS-IP-Address};ses=%{Acct-Session-Id}')"
            }
            interim-update {
                query = "${..start.query}"
            }
            stop {
                query = "call radstop('%{reply:Callback-Number}', '%{Framed-IP-Address}')"
            }
        }
    }
    post-auth {
        query = "call radupdate('%{reply:Callback-Number}','%{reply:Framed-IP-Address}',\
                'user=%{Calling-Station-Id};nas=%{NAS-IP-Address};ses=%{Acct-Session-Id}')"
    }
}

Mysql процедуры

ALTER DATABASE nodeny CHARACTER SET utf8 COLLATE utf8_general_ci;
DELIMITER $$
CREATE FUNCTION `strSplit`(x MEDIUMTEXT, delim MEDIUMTEXT, pos int) RETURNS mediumtext CHARSET utf8
    DETERMINISTIC
RETURN 
    TRIM(BOTH '\r' FROM TRIM(
        REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos), LENGTH(SUBSTRING_INDEX(x, delim, pos - 1)) + 1), delim, '')
    ))$$
DELIMITER ;

DROP PROCEDURE IF EXISTS `radreply`;
DELIMITER $$
CREATE PROCEDURE `radreply`(IN encoded_user_name VARCHAR(128))
BEGIN
    DECLARE nas_name VARCHAR(32);
    DECLARE usr_mac VARCHAR(12);
    DECLARE vlans VARCHAR(64);
    DECLARE svlan VARCHAR(6);
    DECLARE cvlan VARCHAR(6);
    DECLARE usr_id INT;
    DECLARE usr_ip VARCHAR(15) DEFAULT NULL;
    DECLARE add_attr MEDIUMTEXT;
    DECLARE line MEDIUMTEXT;
    DECLARE i INT DEFAULT 1;

    SELECT strSplit(encoded_user_name, '=7C', 1) INTO nas_name;
    SELECT REPLACE(strSplit(encoded_user_name, '=7C', 2), '.', '') INTO usr_mac;
    SELECT strSplit(encoded_user_name, '=7C', 3) INTO vlans;
    SELECT strSplit(vlans, ':', 2) INTO vlans;
    SELECT strSplit(vlans, '-', 1) INTO svlan;
    SELECT strSplit(vlans, '-', 2) INTO cvlan;

    IF( LENGTH(cvlan) > 0 ) THEN
        SELECT m.uid INTO usr_id
          FROM mac_uid m
          JOIN data0 d ON m.device_mac=d._mac
          WHERE d._bras=nas_name AND d._svlan=svlan AND m.device_port=cvlan;
    ELSE
        SELECT m.uid INTO usr_id
          FROM mac_uid m
          JOIN data0 d ON m.device_mac=d._mac
          WHERE d._bras=nas_name AND m.device_port=svlan AND m.mac=usr_mac;
    END IF;

    IF( usr_id IS NULL ) THEN
        SELECT NULL,encoded_user_name,'ERX-Service-Activate:1','svc-guest-ipoe(svc-filter-notregistered)','+=';
    ELSE
      SELECT get_ip(usr_id) INTO usr_ip;
      SELECT radius_attr INTO add_attr FROM users_services
        WHERE uid=usr_id AND tags LIKE '%,inet,%' LIMIT 1;

      SELECT NULL,encoded_user_name,'Callback-Number',usr_id,'=';
      SELECT NULL,encoded_user_name,'Framed-IP-Address',usr_ip,'=';

      attr_loop: WHILE TRUE DO
        SELECT strSplit(add_attr, '\n', i) INTO line;
        IF LENGTH(line) = 0 OR i > 20 THEN LEAVE attr_loop; END IF;
        IF line LIKE '%+=%' THEN
            SELECT NULL,encoded_user_name,strSplit(line, '+=', 1),strSplit(line, '+=', 2),'+=';
        ELSEIF line LIKE '%=%' THEN
            SELECT NULL,encoded_user_name,strSplit(line, '=', 1),strSplit(line, '=', 2),'=';
        END IF;
        SET i = i + 1;
      END WHILE;
    END IF;
END$$
DELIMITER ;