summaryrefslogtreecommitdiffstats
path: root/raddb/mods-config/sql/main/mongo/queries.conf
blob: 732e1e802856ce288a90805838669b34b63cbbaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# -*- text -*-
#
#  main/mongo/queries.conf -- Mongo configuration queries
#
#  Note that as Mongo is a "schemaless" database, there is no
#  default schema.
#
#  Note also that the Mongo driver is a work in progress.  If it works
#  for you, great.  If the queries do not work, please send a patch.
#  But the FreeRADIUS team are not experts in Mongo, and cannot help
#  with creating Mongo queries.
#
#  $Id$

#######################################################################
#  Query config:  Username
#######################################################################
# This is the username that will get substituted, escaped, and added
# as attribute 'SQL-User-Name'.  '%{SQL-User-Name}' should be used
# below everywhere a username substitution is needed so you you can
# be sure the username passed from the client is escaped properly.
#
# Uncomment the next line, if you want the sql_user_name to mean:
#
#    Use Stripped-User-Name, if it's there.
#    Else use User-Name, if it's there,
#    Else use hard-coded string "none" as the user name.
#
#sql_user_name = "%{%{Stripped-User-Name}:-%{%{User-Name}:-none}}"

sql_user_name = "%{User-Name}"

#######################################################################
#  Authorization Queries
#######################################################################
#  These queries compare the check items for the user
#  in ${authcheck_table} and setup the reply items in
#  ${authreply_table}.  You can use any query/tables
#  you want, but the return data for each row MUST
#  be in the  following order:
#
#  0. Row ID (currently unused)
#  1. UserName/GroupName
#  2. Item Attr Name
#  3. Item Attr Value
#  4. Item Attr Operation
#######################################################################

#
# Aggregate query that return like for SQL standard N rows with columns <id>,<username>,<attribute>,<value>,<op>
#
# Example of Result:
#
# { "id" : 0, "username": "bob", "attribute" : "User-Name", "Value" : "pippo", "op" : "==" }
# { "id" : 0, "username": "bob", "attribute" : "ClearText-Password", "value" : "pwd1", "op" : ":=" }
# { "id" : 0, "username": "bob", "attribute" : "Cache-TTL", "value" : 1000, "op" : ":=" }
#
authorize_check_query = "db.${authcheck_table}.aggregate([ \
	{ \
		'$match': { \
			'calling_station_id': '%{Calling-Station-id}', \
			'auth_blocked': 'false' \
		} \
	}, \
	{ \
		'$addFields': { \
				'attributes.User-Name': '$usr', \
				'attributes.ClearText-Password': '$pwd', \
				'attributes.Cache-TTL': '$ttlcache', \
				'attributes.Enable-Roaming': '$roaming', \
				'attributes.Pool-Name': '$pool_name' \
		} \
	}, \
	{ \
		'$project': { \
			'calling_station_id': 1, \
			'attributes': { \
				'$objectToArray': '$attributes' \
			} \
		} \
	}, \
	{ \
		'$unwind': '$attributes' \
	}, \
	{ \
		'$project': { \
			'_id': 0, \
			'username': '', \
			'attribute': '$attributes.k', \
			'value': '$attributes.v', \
			'op': ':=' \
		} \
	} \
])" \

# TBD: fill in things here
authorize_reply_query = ""

##################################################################

#
# TBD: fill in things here
#
#authorize_group_check_query = ""
#authorize_group_reply_query = ""

#######################################################################
# Group Membership Queries
#######################################################################
# group_membership_query        - Check user group membership
#######################################################################

#
#  TBD: Fill in things here.
#
#group_membership_query = ""


#######################################################################
# Accounting and Post-Auth Queries
#######################################################################
# These queries insert/update accounting and authentication records.
# The query to use is determined by the value of 'reference'.
# This value is used as a configuration path and should resolve to one
# or more 'query's. If reference points to multiple queries, and a query
# fails, the next query is executed.
#
# Behaviour is identical to the old 1.x/2.x module, except we can now
# fail between N queries, and query selection can be based on any
# combination of attributes, or custom 'Acct-Status-Type' values.
#######################################################################

accounting {
	reference = "%{tolower:type.%{Acct-Status-Type}.query}"

	type {

		start {
			query = "db.connections.findAndModify({ \
				'query': { \
					'calling_station_id': '%{Calling-Station-Id}', \
					'pgw_node': '%{NAS-Identifier}', \
					'acct_session_id': '%{Acct-Session-Id}', \
				}, \
				'update': { \
					'$set': { \
						'update_date':  { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } }, \
						'ip': '%{Framed-IP-Address}', \
						'start_time': '%{Packet-Original-Timestamp}', \
					}, \
					'$push': { \
						'events_data': { \
							'event_id': '%{sha256:%{tolower:%{Calling-Station-Id}', \
							'event_type': 'Accounting-Start', \
							'event_time': '%{Packet-Original-Timestamp}', \
							'creation_date': { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } } \
						} \
					}, \
					'$setOnInsert': { \
						'pool_name': '%{Control:Pool-Name}', \
						'ip': '%{Framed-IP-Address}', \
						'closed': false, \
						'update_counter': 0, \
						'creation_date': { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' }  } \
					} \
				}, \
				'upsert': true \
			})"

			query = "db.simultaneous_connections.findAndModify({ \
				'query': { \
					'pool_name': '%{Control:Pool-Name}' \
				}, \
				'update': { \
					'$inc': { \
						'conns_counter': 1 \
					} \
					'$setOnInsert': { \
						'pool_name': '%{Control:Pool-Name}', \
							'conns_counter': 1 \
					}, \
				}, \
				'upsert': true \
			})"
		# End Start
		}

		interim-update {
			query = "db.connections.findAndModify({ \
				'query': { \
					'calling_station_id': '%{Calling-Station-Id}', \
						'pgw_node': '%{NAS-Identifier}', \
						'acct_session_id': '%{Acct-Session-Id}' \
				}, \
				'update': { \
					'$set': { \
						'update_date':  { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } }, \
						'last_upd_interim': '%{Packet-Original-Timestamp}' \
					}, \
					'$inc': { \
						'update_counter': 1 \
					}, \
					'$push': { \
						'events_data': { \
							'event_id': '%{sha256:%{tolower:%{Calling-Station-Id}', \
							'event_type': 'Accounting-Interim-Update', \
							'event_time': '%{Packet-Original-Timestamp}', \
							'creation_date': { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } } \
						} \
					}, \
					'$setOnInsert': { \
						'pool_name': '%{Control:Pool-Name}', \
						'ip': '%{Framed-IP-Address}', \
						'closed': false, \
						'creation_date': { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } } \
					} \
				},
				'upsert': true \
			})"
		# End Interim-Update
		}

		stop {
			query = "db.connections.findAndModify({ \
				'query': { \
					'calling_station_id': '%{Calling-Station-Id}', \
					'pgw_node': '%{NAS-Identifier}', \
					'acct_session_id': '%{Acct-Session-Id}' \
				}, \
				'update': { \
					'$set': { \
						'update_date':  { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } }, \
						'stop_time': '%{Packet-Original-Timestamp}', \
						'closed': true \
					}, \
					'$push': { \
						'events_data': { \
							'event_id': '%{sha256:%{tolower:%{Calling-Station-Id}', \
							'event_type': 'Accounting-Stop', \
							'event_time': '%{Packet-Original-Timestamp}', \
							'creation_date': { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } } \
						} \
					}, \
					'$setOnInsert': { \
						'pool_name': '%{Control:Pool-Name}', \
						'ip': '%{Framed-IP-Address}', \
						'update_counter': 0, \
						'creation_date': { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } } \
					} \
				}, \
				'upsert': true \
			})"

		# End Stop
		}

	}
}


#######################################################################
# Authentication Logging Queries
#######################################################################
# postauth_query                - Insert some info after authentication
#######################################################################

post-auth {
	query = "db.post_auth.findAndModify({ \
		'query': { \
			'calling_station_id': '%{Calling-Station-Id}', \
			'nas_ip': '%{NAS-Identifier}' \
		}, \
		'update': { \
			'$set': { \
				'update_date':  { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } }, \
				'last_event_ts': '%{Packet-Original-Timestamp}' \
			}, \
			'$inc': { \
				'reject_counter': 1 \
			}, \
			'$setOnInsert': { \
				'calling_station_id': '%{Calling-Station-Id}', \
				'nas_ip': '%{NAS-Identifier}', \
				'creation_date': { '$date': { '$numberLong': '%{expr: (%l * 1000) + (%M / 1000)}' } } \
			} \
		}, \
		'upsert': true \
	})"
}