#
# Server configuration
#
[server]

# User to run this daemon as
#user=
#group=

# Filename to store pid of parent process
#pid_file=/var/run/smradius/smradiusd.pid

# Cache file
#cache_file=/var/run/smradius/cache


# Uncommenting the below option will prevent awradiusd going into the background
#background=no

# Preforking configuration
#
# min_server		- Minimum servers to keep around
# min_spare_servers	- Minimum spare servers to keep around ready to 
# 			  handle requests
# max_spare_servers	- Maximum spare servers to have around doing nothing
# max_servers		- Maximum servers alltogether
# max_requests		- Maximum number of requests each child will serve
#
# One may want to use the following as a rough guideline...
# Small : 2, 2,  4, 10, 1000
# Medium: 4, 4, 12, 25, 1000
# Large : 8, 8, 16, 64, 1000
#
#min_servers=4
#min_spare_servers=4
#max_spare_servers=12
#max_servers=25
#max_requests=1000



# Log level:
# 0 - Errors only
# 1 - Warnings and errors
# 2 - Notices, warnings, errors
# 3 - Info, notices, warnings, errors
# 4 - Debugging 
#log_level=2

# File to log to instead of stdout
#log_file=/var/log/smradiusd.log

# Things to log in extreme detail
# modules 	- Log detailed module running information
#
# There is no default for this configuration option. Options can be
# separated by commas. ie. modules
#
#log_detail=

# IP to listen on, * for all
#host=*

# Timeout in communication with clients
#timeout=120

# cidr_allow/cidr_deny
# Comma, whitespace or semi-colon separated. Contains a CIDR block to 
# compare the clients IP to.  If cidr_allow or cidr_deny options are 
# given, the incoming client must match a cidr_allow and not match a 
# cidr_deny or the client connection will be closed.
#cidr_allow=0.0.0.0/0
#cidr_deny=

# Event timestamp timezone, in "Continent/City" format
# Defaults to "GMT"
event_timezone=GMT


[radius]
# Use packet timestamp, if unset, the default is to use the server
# timestamp at the moment the packet is received.
#
# WARNING!!!!
# Not all routers keep time, it may occur that some routers depend on 
# getting date & time apon reboot from an ntp server. The problem 
# will arise when the router cannot get the date and time before the 
# first user logs in .. BAM, you'll have sessions with a period key 
# in current month but an event timestamp in 1970.
#
# Defaults to "no"
#use_packet_timestamp=yes

# Radius server abuse prevention
#
# Abuse prevention will drop packets which flood the radius server,
# or are duplicated in a short timeframe. You probably want this if
# you are not being fed by a radius proxy.
#
# Defaults to "no"
#use_abuse_prevention=yes

# How fast can a NAS spam the same type of request
#
# Access-Request defaults to 10s
#access_request_abuse_threshold=10
#
# Accounting-Request defaults to 5s
#accounting_request_abuse_threshold=5


[database]
#DSN=DBI:SQLite:dbname=smradius.sqlite
DSN=DBI:mysql:database=smradius;host=localhost
Username=root
Password=


# What do we do when we have a database connection problem
# tempfail	- Return temporary failure
# pass		- Return success
bypass_mode=tempfail

# How many seconds before we retry a DB connection
bypass_timeout=5


[dictionary]
load=<<EOT
dicts/dictionary
dicts/dictionary.microsoft
dicts/dictionary.mikrotik
EOT


[authentication]

mechanisms=<<EOT
mod_auth_pap
mod_auth_chap
mod_auth_mschap
EOT

users=<<EOT
mod_userdb_sql
EOT


[system]
modules=<<EOT
mod_config_sql
mod_config_sql_topups
EOT


[features]
modules=<<EOT
mod_feature_capping
mod_feature_user_stats
mod_feature_validity
EOT



[accounting]
modules=<<EOT
mod_accounting_sql
EOT



# MOD_CONFIG_SQL
[mod_config_sql]

get_config_realm_id_query=<<EOT
	SELECT
		ID
	FROM
		@TP@realms
	WHERE
		Name = ?
EOT

get_config_realm_attributes_query=<<EOT
	SELECT
		Name,
		Operator,
		Value
	FROM
		@TP@realm_attributes
	WHERE
		RealmID = ?
EOT

get_config_accesslist_query=<<EOT
	SELECT
		@TP@clients.AccessList,
		@TP@clients.ID
	FROM
		@TP@clients,
		@TP@clients_to_realms
	WHERE
		@TP@clients.ID = @TP@clients_to_realms.ClientID
		AND @TP@clients_to_realms.RealmID = ?
EOT

get_config_client_attributes_query=<<EOT
	SELECT
		Name,
		Operator,
		Value
	FROM
		@TP@client_attributes
	WHERE
		ClientID = ?
EOT



# MOD_CONFIG_SQL_TOPUPS
[mod_config_sql_topups]

get_topups_summary_query=<<EOT
	SELECT
		@TP@topups_summary.Balance,
		@TP@topups.Type,
		@TP@topups.ID
	FROM
		@TP@topups_summary,
		@TP@topups,
		@TP@users
	WHERE
		@TP@topups.ID = @TP@topups_summary.TopupID
		AND @TP@topups.UserID = @TP@users.ID
		AND @TP@topups_summary.PeriodKey = ?
		AND @TP@topups.Depleted = 0
		AND @TP@users.Username = ?
EOT

get_topups_query=<<EOT
	SELECT
		@TP@topups.ID,
		@TP@topups.Type,
		@TP@topups.Value
	FROM
		@TP@topups,
		@TP@users
	WHERE
		@TP@topups.UserID = @TP@users.ID
		AND @TP@topups.ValidFrom = ?
		AND @TP@topups.ValidTo >= ?
		AND @TP@topups.Depleted = 0
		AND @TP@users.Username = ?
EOT


# MOD_ACCOUNTING_SQL
[mod_accounting_sql]

accounting_start_query=<<EOT
	INSERT INTO
		@TP@accounting
	(
		Username,
		ServiceType,
		FramedProtocol,
		NASPort,
		NASPortType,
		CallingStationID,
		CalledStationID,
		NASPortID,
		AcctSessionID,
		FramedIPAddress,
		AcctAuthentic,
		EventTimestamp,
		AcctStatusType,
		NASIdentifier,
		NASIPAddress,
		AcctDelayTime,
		AcctSessionTime,
		AcctInputOctets,
		AcctInputGigawords,
		AcctInputPackets,
		AcctOutputOctets,
		AcctOutputGigawords,
		AcctOutputPackets,
		PeriodKey
	)
	VALUES
	(
		%{user.Username},
		%{request.Service-Type},
		%{request.Framed-Protocol},
		%{request.NAS-Port},
		%{request.NAS-Port-Type},
		%{request.Calling-Station-Id},
		%{request.Called-Station-Id},
		%{request.NAS-Port-Id},
		%{request.Acct-Session-Id},
		%{request.Framed-IP-Address},
		%{request.Acct-Authentic},
		%{request.Timestamp},
		%{request.Acct-Status-Type},
		%{request.NAS-Identifier},
		%{request.NAS-IP-Address},
		%{request.Acct-Delay-Time},
		%{request.SessionTime},
		%{request.InputOctets},
		%{request.InputGigawords},
		%{request.InputPackets},
		%{request.OutputOctets},
		%{request.OutputGigawords},
		%{request.OutputPackets},
		%{query.PeriodKey}
	)
EOT

accounting_update_get_records_query=<<EOT
	SELECT
		SUM(AcctInputOctets) AS InputOctets,
		SUM(AcctInputPackets) AS InputPackets,
		SUM(AcctOutputOctets) AS OutputOctets,
		SUM(AcctOutputPackets) AS OutputPackets,
		SUM(AcctInputGigawords) AS InputGigawords,
		SUM(AcctOutputGigawords) AS OutputGigawords,
		SUM(AcctSessionTime) AS SessionTime,
		PeriodKey
	FROM
		@TP@accounting
	WHERE
		Username = %{user.Username}
		AND AcctSessionID = %{request.Acct-Session-Id}
		AND NASIPAddress = %{request.NAS-IP-Address}
		AND NASPort = %{request.NAS-Port}
	GROUP BY
		PeriodKey
	ORDER BY
		ID ASC
EOT

accounting_update_query=<<EOT
	UPDATE
		@TP@accounting
	SET
		AcctSessionTime = %{query.SessionTime},
		AcctInputOctets = %{query.InputOctets},
		AcctInputGigawords = %{query.InputGigawords},
		AcctInputPackets = %{query.InputPackets},
		AcctOutputOctets = %{query.OutputOctets},
		AcctOutputGigawords = %{query.OutputGigawords},
		AcctOutputPackets = %{query.OutputPackets},
		AcctStatusType = %{request.Acct-Status-Type}
	WHERE
		Username = %{user.Username}
		AND AcctSessionID = %{request.Acct-Session-Id}
		AND NASIPAddress = %{request.NAS-IP-Address}
		AND NASPort = %{request.NAS-Port}
		AND PeriodKey = %{query.PeriodKey}
EOT

accounting_stop_status_query=<<EOT
	UPDATE
		@TP@accounting
	SET
		AcctStatusType = %{request.Acct-Status-Type},
		AcctTerminateCause = %{request.Acct-Terminate-Cause}
	WHERE
		Username = %{user.Username}
		AND AcctSessionID = %{request.Acct-Session-Id}
		AND NASIPAddress = %{request.NAS-IP-Address}
		AND NASPort = %{request.NAS-Port}
EOT

accounting_usage_query=<<EOT
	SELECT
		SUM(AcctInputOctets) AS AcctInputOctets,
		SUM(AcctOutputOctets) AS AcctOutputOctets,
		SUM(AcctInputGigawords) AS AcctInputGigawords,
		SUM(AcctOutputGigawords) AS AcctOutputGigawords,
		SUM(AcctSessionTime) AS AcctSessionTime
	FROM
		@TP@accounting
	WHERE
		Username = %{user.Username}
		AND PeriodKey = %{query.PeriodKey}
EOT

accounting_select_duplicates_query=<<EOT
	SELECT
		ID
	FROM
		@TP@accounting
	WHERE
		Username = %{user.Username}
		AND AcctSessionID = %{request.Acct-Session-Id}
		AND NASIPAddress = %{request.NAS-IP-Address}
		AND NASPort = %{request.NAS-Port}
		AND PeriodKey = %{query.PeriodKey}
	ORDER BY
		ID DESC
		LIMIT 99 OFFSET 1
EOT

accounting_delete_duplicates_query=<<EOT
	DELETE FROM
		@TP@accounting
	WHERE
		ID = %{query.DuplicateID}
EOT

# This is how long we going to cache the usage query for
# Default: 300 (seconds)
#
# You can use  "no", "0", "false"  to disable, specify a number > 1, or use 
# "yes", "1", "true" to enable with the default value
accounting_usage_cache_time=300


# MOD_USERDB_SQL
[mod_userdb_sql]

userdb_find_query=<<EOT
	SELECT
		ID, Disabled
	FROM
		@TP@users
	WHERE
		Username = %{user.Username}
EOT

userdb_get_group_attributes_query=<<EOT
	SELECT
		group_attributes.Name, group_attributes.Operator, group_attributes.Value
	FROM
		@TP@group_attributes, @TP@users_to_groups
	WHERE
		users_to_groups.UserID = %{userdb.ID}
		AND group_attributes.GroupID = users_to_groups.GroupID
		AND group_attributes.Disabled = 0
EOT

userdb_get_user_attributes_query=<<EOT
	SELECT
		Name, Operator, Value
	FROM
		@TP@user_attributes
	WHERE
		UserID = %{userdb.ID}
		AND Disabled = 0
EOT

users_data_set_query=<<EOT
	INSERT INTO
		@TP@users_data (UserID, LastUpdated, Name, Value)
	VALUES
		(
			%{userdb.ID},
			%{query.LastUpdated},
			%{query.Name},
			%{query.Value}
		)
EOT

users_data_update_query=<<EOT
	UPDATE
		@TP@users_data
	SET
		LastUpdated = %{query.LastUpdated},
		Value = %{query.Value}
	WHERE
		UserID = %{userdb.ID}
		AND Name = %{query.Name}
EOT

users_data_get_query=<<EOT
	SELECT
		LastUpdated, Name, Value
	FROM
		@TP@users_data
	WHERE
		UserID = %{userdb.ID}
		AND Name = %{query.Name}
EOT

users_data_delete_query=<<EOT
	DELETE FROM
		@TP@users_data
	WHERE
		UserID = %{userdb.ID}
		AND Name = %{query.Name}
EOT

# This is how long we going to cache the data query for
# Default: 300 (seconds)
#
# You can use  "no", "0", "false"  to disable, specify a number > 1, or use 
# "yes", "1", "true" to enable with the default value
userdb_data_cache_time=300


# MOD_FEATURE_UPDATE_USER_STATS_SQL
[mod_feature_update_user_stats_sql]

update_user_stats_query=<<EOT
	UPDATE
		@TP@users
	SET
		PeriodKey = %{query.PeriodKey},
		TotalTraffic = %{query.TotalTraffic},
		TotalUptime = %{query.TotalUptime},
		NASIdentifier = %{request.NAS-Identifier}
	WHERE
		Username = %{user.Username}
EOT


# MOD_FEATURE_CAPPING
[mod_feature_capping]
# Enable Mikrotik-specific return vattributes
#enable_mikrotik=1

