Handle case where Acct-Session-Time might not exist
[freeradius.git] / raddb / sql / postgresql / dialup.conf
1 # -*- text -*-
2 ##
3 ## dialup.conf -- PostgreSQL configuration for default schema (schema.sql)
4 ##
5 ##      $Id$
6
7 # Safe characters list for sql queries. Everything else is replaced
8 # with their mime-encoded equivalents.
9 # The default list should be ok
10 # safe-characters = "@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_: /"
11
12 #######################################################################
13 #  Query config:  Username
14 #######################################################################
15 # This is the username that will get substituted, escaped, and added
16 # as attribute 'SQL-User-Name'.  '%{SQL-User-Name}' should be used
17 # below everywhere a username substitution is needed so you you can
18 # be sure the username passed from the client is escaped properly.
19 #
20 # Uncomment the next line, if you want the sql_user_name to mean:
21 #
22 #    Use Stripped-User-Name, if it's there.
23 #    Else use User-Name, if it's there,
24 #    Else use hard-coded string "none" as the user name.
25 #
26 #sql_user_name = "%{%{Stripped-User-Name}:-%{%{User-Name}:-none}}"
27
28 sql_user_name = "%{User-Name}"
29
30 #######################################################################
31 #  Default profile
32 #######################################################################
33 # This is the default profile. It is found in SQL by group membership.
34 # That means that this profile must be a member of at least one group
35 # which will contain the corresponding check and reply items.
36 # This profile will be queried in the authorize section for every user.
37 # The point is to assign all users a default profile without having to
38 # manually add each one to a group that will contain the profile.
39 # The SQL module will also honor the User-Profile attribute. This
40 # attribute can be set anywhere in the authorize section (ie the users
41 # file). It is found exactly as the default profile is found.
42 # If it is set then it will *overwrite* the default profile setting.
43 # The idea is to select profiles based on checks on the incoming
44 # packets, not on user group membership. For example:
45 # -- users file --
46 # DEFAULT       Service-Type == Outbound-User, User-Profile := "outbound"
47 # DEFAULT       Service-Type == Framed-User, User-Profile := "framed"
48 #
49 # By default the default_user_profile is not set
50 #
51 # default_user_profile = "DEFAULT"
52
53 #######################################################################
54 #  NAS Query
55 #######################################################################
56 #  This query retrieves the radius clients
57 #
58 #  0. Row ID (currently unused)
59 #  1. Name (or IP address)
60 #  2. Shortname
61 #  3. Type
62 #  4. Secret
63 #######################################################################
64
65 nas_query = "SELECT id, nasname, shortname, type, secret FROM ${nas_table}"
66
67 #######################################################################
68 #  Authorization Queries
69 #######################################################################
70 #  These queries compare the check items for the user
71 #  in ${authcheck_table} and setup the reply items in
72 #  ${authreply_table}.  You can use any query/tables
73 #  you want, but the return data for each row MUST
74 #  be in the  following order:
75 #
76 #  0. Row ID (currently unused)
77 #  1. UserName/GroupName
78 #  2. Item Attr Name
79 #  3. Item Attr Value
80 #  4. Item Attr Operation
81 #######################################################################
82
83 # Use these for case insensitive usernames. WARNING: Slower queries!
84 # authorize_check_query = "SELECT id, UserName, Attribute, Value, Op \
85 #   FROM ${authcheck_table} \
86 #   WHERE LOWER(UserName) = LOWER('%{SQL-User-Name}') \
87 #   ORDER BY id"
88 # authorize_reply_query = "SELECT id, UserName, Attribute, Value, Op \
89 #   FROM ${authreply_table} \
90 #   WHERE LOWER(UserName) = LOWER('%{SQL-User-Name}') \
91 #   ORDER BY id"
92
93 authorize_check_query = "SELECT id, UserName, Attribute, Value, Op \
94   FROM ${authcheck_table} \
95   WHERE Username = '%{SQL-User-Name}' \
96   ORDER BY id"
97
98 authorize_reply_query = "SELECT id, UserName, Attribute, Value, Op \
99   FROM ${authreply_table} \
100   WHERE Username = '%{SQL-User-Name}' \
101   ORDER BY id"
102
103 # Use these for case insensitive usernames. WARNING: Slower queries!
104 # authorize_group_check_query = "SELECT ${groupcheck_table}.id, ${groupcheck_table}.GroupName, \
105 #   ${groupcheck_table}.Attribute, ${groupcheck_table}.Value, ${groupcheck_table}.Op \
106 #   FROM ${groupcheck_table}, ${usergroup_table} \
107 #   WHERE LOWER(${usergroup_table}.UserName) = LOWER('%{SQL-User-Name}') AND ${usergroup_table}.GroupName = ${groupcheck_table}.GroupName \
108 #   ORDER BY ${groupcheck_table}.id"
109 # authorize_group_reply_query = "SELECT ${groupreply_table}.id, ${groupreply_table}.GroupName, \
110 #   ${groupreply_table}.Attribute, ${groupreply_table}.Value, ${groupreply_table}.Op \
111 #   FROM ${groupreply_table}, ${usergroup_table} \
112 #   WHERE LOWER(${usergroup_table}.UserName) = LOWER('%{SQL-User-Name}') AND ${usergroup_table}.GroupName = ${groupreply_table}.GroupName \
113 #   ORDER BY ${groupreply_table}.id"
114
115 authorize_group_check_query = "SELECT id, GroupName, Attribute, Value, op \
116   FROM ${groupcheck_table} \
117   WHERE GroupName = '%{Sql-Group}' \
118   ORDER BY id"
119
120 authorize_group_reply_query = "SELECT id, GroupName, Attribute, Value, op \
121   FROM ${groupreply_table} \
122   WHERE GroupName = '%{Sql-Group}' \
123   ORDER BY id"
124
125 #######################################################################
126 # Simultaneous Use Checking Queries
127 #######################################################################
128 # simul_count_query     - query for the number of current connections
129 #                       - If this is not defined, no simultaneous use checking
130 #                       - will be performed by this module instance
131 # simul_verify_query    - query to return details of current connections for verification
132 #                       - Leave blank or commented out to disable verification step
133 #                       - Note that the returned field order should not be changed.
134 #######################################################################
135
136 # Uncomment simul_count_query to enable simultaneous use checking
137 # simul_count_query = "SELECT COUNT(*) FROM ${acct_table1} WHERE UserName='%{SQL-User-Name}' AND AcctStopTime IS NULL"
138 # simul_verify_query = "SELECT RadAcctId, AcctSessionId, UserName, NASIPAddress, NASPortId, FramedIPAddress, CallingStationId, FramedProtocol FROM ${acct_table1} WHERE UserName='%{SQL-User-Name}' AND AcctStopTime IS NULL"
139
140
141
142 #######################################################################
143 #  Accounting Queries
144 #######################################################################
145 # accounting_onoff_query        - query for Accounting On/Off packets
146 # accounting_update_query       - query for Accounting update packets
147 # accounting_update_query_alt   - query for Accounting update packets
148 #                               (alternate in case first query fails)
149 # accounting_start_query        - query for Accounting start packets
150 # accounting_start_query_alt    - query for Accounting start packets
151 #                               (alternate in case first query fails)
152 # accounting_stop_query         - query for Accounting stop packets
153 # accounting_stop_query_alt     - query for Accounting start packets
154 #                               (alternate in case first query doesn't
155 #                                affect any existing rows in the table)
156 #######################################################################
157
158 accounting_onoff_query = "UPDATE ${acct_table1} \
159   SET AcctStopTime = ('%S'::timestamp - '%{%{Acct-Delay-Time}:-0}'::interval), \
160   AcctSessionTime = (EXTRACT(EPOCH FROM ('%S'::timestamp with time zone - AcctStartTime::timestamp with time zone \
161   - '%{%{Acct-Delay-Time}:-0}'::interval)))::BIGINT, \
162   AcctTerminateCause = '%{Acct-Terminate-Cause}', \
163   AcctStopDelay = 0 \
164   WHERE AcctStopTime IS NULL \
165   AND NASIPAddress= '%{NAS-IP-Address}' \
166   AND AcctStartTime <= '%S'::timestamp"
167
168 accounting_update_query = "UPDATE ${acct_table1} \
169   SET FramedIPAddress = NULLIF('%{Framed-IP-Address}', '')::inet, \
170   AcctSessionTime = '%{Acct-Session-Time}', \
171   AcctInputOctets = (('%{%{Acct-Input-Gigawords}:-0}'::bigint << 32) + '%{%{Acct-Input-Octets}:-0}'::bigint), \
172   AcctOutputOctets = (('%{%{Acct-Output-Gigawords}:-0}'::bigint << 32) + '%{%{Acct-Output-Octets}:-0}'::bigint) \
173   WHERE AcctSessionId = '%{Acct-Session-Id}' AND UserName = '%{SQL-User-Name}' \
174   AND NASIPAddress= '%{NAS-IP-Address}' AND AcctStopTime IS NULL"
175
176 accounting_update_query_alt = "INSERT INTO ${acct_table1} \
177   (AcctSessionId, AcctUniqueId, UserName, Realm, NASIPAddress, \
178   NASPortId, NASPortType, AcctStartTime, \
179   AcctSessionTime, AcctAuthentic, AcctInputOctets, \
180   AcctOutputOctets, CalledStationId, CallingStationId, \
181   ServiceType, FramedProtocol, FramedIPAddress, XAscendSessionSvrKey) \
182   VALUES('%{Acct-Session-Id}', '%{Acct-Unique-Session-Id}', \
183   '%{SQL-User-Name}', NULLIF('%{Realm}', ''), '%{NAS-IP-Address}', \
184   %{%{NAS-Port}:-NULL}::integer, '%{NAS-Port-Type}', \
185   ('%S'::timestamp - '%{%{Acct-Delay-Time}:-0}'::interval - '%{%{Acct-Session-Time}:-0}'::interval), \
186   '%{Acct-Session-Time}', '%{Acct-Authentic}', \
187   (('%{%{Acct-Input-Gigawords}:-0}'::bigint << 32) + '%{%{Acct-Input-Octets}:-0}'::bigint), \
188   (('%{%{Acct-Output-Gigawords}:-0}'::bigint << 32) + '%{%{Acct-Output-Octets}:-0}'::bigint), \
189   '%{Called-Station-Id}', \
190   '%{Calling-Station-Id}', '%{Service-Type}', '%{Framed-Protocol}', \
191   NULLIF('%{Framed-IP-Address}', '')::inet, '%{X-Ascend-Session-Svr-Key}')"
192
193 accounting_start_query = "INSERT INTO ${acct_table1} \
194   (AcctSessionId, AcctUniqueId, UserName, Realm, NASIPAddress, \
195    NASPortId, NASPortType, AcctStartTime, AcctAuthentic, \
196   ConnectInfo_start, CalledStationId, CallingStationId, ServiceType, \
197   FramedProtocol, FramedIPAddress, AcctStartDelay, XAscendSessionSvrKey) \
198   VALUES('%{Acct-Session-Id}', \
199   '%{Acct-Unique-Session-Id}', \
200   '%{SQL-User-Name}', \
201   NULLIF('%{Realm}', ''), \
202   '%{NAS-IP-Address}', \
203   %{%{NAS-Port}:-NULL}::integer, \
204   '%{NAS-Port-Type}', \
205   ('%S'::timestamp - '%{%{Acct-Delay-Time}:-0}'::interval), \
206   '%{Acct-Authentic}', \
207   '%{Connect-Info}', \
208   '%{Called-Station-Id}', \
209   '%{Calling-Station-Id}', \
210   '%{Service-Type}', \
211   '%{Framed-Protocol}', \
212   NULLIF('%{Framed-IP-Address}', '')::inet, \
213   0, \
214   '%{X-Ascend-Session-Svr-Key}')"
215
216 accounting_start_query_alt  = "UPDATE ${acct_table1} \
217   SET AcctStartTime = ('%S'::timestamp - '%{%{Acct-Delay-Time}:-0}'::interval), \
218   AcctStartDelay = 0, \
219   ConnectInfo_start = '%{Connect-Info}' \
220   WHERE AcctSessionId = '%{Acct-Session-Id}' \
221   AND UserName = '%{SQL-User-Name}' \
222   AND NASIPAddress = '%{NAS-IP-Address}' \
223   AND AcctStopTime IS NULL"
224
225 accounting_stop_query = "UPDATE ${acct_table2} \
226   SET AcctStopTime = ('%S'::timestamp - '%{%{Acct-Delay-Time}:-0}'::interval), \
227   AcctSessionTime = CASE WHEN '%{Acct-Session-Time}' = '' THEN \
228   (EXTRACT(EPOCH FROM ('%S'::TIMESTAMP WITH TIME ZONE - AcctStartTime::TIMESTAMP WITH TIME ZONE \
229   - '%{%{Acct-Delay-Time}:-0}'::INTERVAL)))::BIGINT \
230   ELSE NULLIF('%{Acct-Session-Time}','')::BIGINT END, \
231   AcctInputOctets = (('%{%{Acct-Input-Gigawords}:-0}'::bigint << 32) + '%{%{Acct-Input-Octets}:-0}'::bigint), \
232   AcctOutputOctets = (('%{%{Acct-Output-Gigawords}:-0}'::bigint << 32) + '%{%{Acct-Output-Octets}:-0}'::bigint), \
233   AcctTerminateCause = '%{Acct-Terminate-Cause}', \
234   AcctStopDelay = 0, \
235   FramedIPAddress = NULLIF('%{Framed-IP-Address}', '')::inet, \
236   ConnectInfo_stop = '%{Connect-Info}' \
237   WHERE AcctSessionId = '%{Acct-Session-Id}' \
238   AND UserName = '%{SQL-User-Name}' \
239   AND NASIPAddress = '%{NAS-IP-Address}' \
240   AND AcctStopTime IS NULL"
241
242 accounting_stop_query_alt = "INSERT INTO ${acct_table2} \
243   (AcctSessionId, AcctUniqueId, UserName, Realm, NASIPAddress, NASPortId, NASPortType, AcctStartTime, AcctStopTime, \
244   AcctSessionTime, AcctAuthentic, ConnectInfo_stop, AcctInputOctets, AcctOutputOctets, CalledStationId, \
245   CallingStationId, AcctTerminateCause, ServiceType, FramedProtocol, FramedIPAddress, AcctStopDelay) \
246   values('%{Acct-Session-Id}', \
247   '%{Acct-Unique-Session-Id}', \
248   '%{SQL-User-Name}', \
249   NULLIF('%{Realm}', ''), \
250   '%{NAS-IP-Address}', \
251   %{%{NAS-Port}:-NULL}::integer, \
252   '%{NAS-Port-Type}', \
253   ('%S'::timestamp - '%{%{Acct-Delay-Time}:-0}'::interval - '%{%{Acct-Session-Time}:-0}'::interval), \
254   ('%S'::timestamp - '%{%{Acct-Delay-Time}:-0}'::interval), \
255   NULLIF('%{Acct-Session-Time}', '')::bigint, '%{Acct-Authentic}', \
256   '%{Connect-Info}', \
257   (('%{%{Acct-Input-Gigawords}:-0}'::bigint << 32) + '%{%{Acct-Input-Octets}:-0}'::bigint), \
258   (('%{%{Acct-Output-Gigawords}:-0}'::bigint << 32) + '%{%{Acct-Output-Octets}:-0}'::bigint), \
259   '%{Called-Station-Id}', \
260   '%{Calling-Station-Id}', \
261   '%{Acct-Terminate-Cause}', \
262   '%{Service-Type}', \
263   '%{Framed-Protocol}', \
264   NULLIF('%{Framed-IP-Address}', '')::inet, 0)"
265
266 #######################################################################
267 # Group Membership Queries
268 #######################################################################
269 # group_membership_query        - Check user group membership
270 #######################################################################
271
272 # Use these for case insensitive usernames. WARNING: Slower queries!
273 # group_membership_query = "SELECT GroupName FROM ${usergroup_table} WHERE LOWER(UserName) = LOWER('%{SQL-User-Name}') ORDER BY priority"
274
275 group_membership_query = "SELECT GroupName FROM ${usergroup_table} WHERE UserName='%{SQL-User-Name}' ORDER BY priority"
276
277 #######################################################################
278 # Authentication Logging Queries
279 #######################################################################
280 # postauth_query                - Insert some info after authentication
281 #######################################################################
282 postauth_query = "INSERT INTO ${postauth_table} (username, pass, reply, authdate) \
283   VALUES ('%{User-Name}', '%{%{User-Password}:-Chap-Password}', '%{reply:Packet-Type}', NOW())"
284