forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuGroupPolicyChecks.r
456 lines (409 loc) · 15.4 KB
/
uuGroupPolicyChecks.r
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# \file
# \brief Group operation policy check rules.
# \author Chris Smeele
# \author Ton Smeele
# \author Lazlo Westerhof
# \copyright Copyright (c) 2015 - 2021, Utrecht University. All rights reserved
# \license GPLv3, see LICENSE
# For every Group Management action (GroupAdd, GroupUserChangeRole, etc.) there
# is a corresponding function in this file that will tell you if someone (the
# *actor) is allowed to perform that action with the supplied parameters.
#
# The result is returned in the output parameters *allowed and *reason. For
# calling rules to be able to receive these output parameters, it is imperative
# that the rules in this file DO NOT FAIL.
#
# Context: The rules in this file are called by:
# - Group Manager implementations of Sudo policies (see uuGroupPolicies.r)
# - Group Manager actions, to figure out the reason for denying a Sudo action.
# Utility functions {{{
# \brief Check if a user name is valid.
#
# User names must:
#
# - contain zero or one '@' signs, but not at the beginning or the end of the name
# - contain only lowercase letters and dots if no '@' sign is present
# - contain only lowercase letters, numbers, hyphens, underscores and dots if an '@' sign is present
# - may contain a zone name after a '#' sign
#
# \param[in] name
#
uuUserNameIsValid(*name)
= *name like regex ``([a-z.]+|[a-z0-9_.-]+@[a-z0-9_.-]+)(#[a-zA-Z0-9_-]+)?``;
# \brief Check if a group name is valid for creation by a priv-group-add member.
#
# Group names must:
#
# - be prefixed with 'intake-' or 'research-' or 'deposit-'
# - contain only lowercase characters, numbers and hyphens
# - not start or end with a hyphen
#
# NB: Update the category name check below if you change the second half of this pattern.
#
# NB: Datamanager is missing in this list. It can only be created by rodsadmin,
# and rodsadmin currently bypasses all checks anyway.
# This check is applicable only to rodsusers with priv-group-add.
#
# \param[in] name
#
uuGroupNameIsValid(*name)
= *name like regex ``(intake|research|deposit)-([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])``;
# \brief Check if a category name is valid.
#
# This must be exactly the same as the part of the group name check after the group prefix.
#
# \param[in] name
#
uuGroupCategoryNameIsValid(*name)
= *name like regex ``([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])``;
# \brief Check if a subcategory name is valid.
#
# Subcategory names must:
#
# - contain only letters, numbers, spaces, commas, periods, underscores and hyphens
#
# \param[in] name
#
uuGroupSubcategoryNameIsValid(*name)
= *name like regex ``[a-zA-Z0-9 ,.()_-]+``;
# \brief Check if a data classification is valid in combination with a group name.
#
# The valid set of classifications depends on the group prefix.
#
# \param[in] groupName
# \param[in] dataClassification
# \param[out] valid
#
uuGroupDataClassificationIsValid(*groupName, *dataClassification, *valid) {
uuChop(*groupName, *prefix, *base, "-", true);
if (*prefix == "research" || *prefix == "intake") {
uuListContains(list("critical", "sensitive", "basic", "public", "unspecified"), *dataClassification, *valid);
} else {
*valid = (*dataClassification == "");
}
}
# }}}
# \brief Group Policy: Can the user create a new group?
#
# \param[in] actor the user whose privileges are checked
# \param[in] groupName the new group name
# \param[in] category
# \param[in] subcategory
# \param[in] description
# \param[in] dataClassification
# \param[out] allowed whether the action is allowed
# \param[out] reason the reason why the action was disallowed, set if allowed is false
#
uuGroupPolicyCanGroupAdd(*actor, *groupName, *category, *subcategory, *description, *dataClassification, *allowed, *reason) {
# Rodsadmin exception.
uuGetUserType(*actor, *actorUserType);
if (*actorUserType == "rodsadmin") { *allowed = 1; *reason = ""; succeed; }
*allowed = 0;
*reason = "";
uuGroupUserExists("priv-group-add", *actor, false, *hasPriv);
if (*hasPriv) {
if (uuGroupNameIsValid(*groupName)) {
uuUserNameIsAvailable(*groupName, *nameAvailable, *existingType);
if (*nameAvailable) {
uuGroupDataClassificationIsValid(*groupName, *dataClassification, *dataclasValid);
if (*dataclasValid) {
uuChop(*groupName, *prefix, *base, "-", true);
# For research and intake groups: Make sure their ro and
# vault groups do not exist yet.
*roName = "read-*base";
uuGroupExists(*roName, *roExists);
*vaultName = "vault-*base";
uuGroupExists(*vaultName, *vaultExists);
if (*roExists || *vaultExists) {
*reason = "This group name is not available.";
} else {
# Last check.
uuGroupPolicyCanUseCategory(*actor, *category, *allowed, *reason);
}
} else {
*reason = "The chosen data classification is invalid for this type of group.";
}
} else {
if (*existingType == "rodsuser") {
*existingType = "user";
} else if (*existingType == "rodsgroup") {
*existingType = "group";
}
*reason = "The name '*groupName' is already in use by another *existingType.";
}
} else {
*reason = "Group names must start with one of 'intake-' or 'research-' or 'deposit-' and may only contain lowercase letters (a-z) and hyphens (-).";
}
} else {
*reason = "You cannot create groups because you are not a member of the priv-group-add group.";
}
}
# \brief Group Policy: Can the user create a group with / modify a group to use a certain category?
#
# This is a utility function for other check functions, there is no
# corresponding group action for this rule.
#
# \param[in] actor the user whose privileges are checked
# \param[in] categoryName the category name
# \param[out] allowed whether the action is allowed
# \param[out] reason the reason why the action was disallowed, set if allowed is false
#
uuGroupPolicyCanUseCategory(*actor, *categoryName, *allowed, *reason) {
uuGetUserType(*actor, *actorUserType);
if (*actorUserType == "rodsadmin") { *allowed = 1; *reason = ""; succeed; }
*allowed = 0;
*reason = "";
uuGroupCategoryExists(*categoryName, *categoryExists);
if (*categoryExists) {
*isManagerInCategory = false;
uuUserGetGroups(*actor, false, *groups);
foreach (*actorGroup in *groups) {
uuGroupGetCategory(*actorGroup, *agCategory, *agSubcategory);
if (*agCategory == *categoryName) {
uuGroupUserIsManager(*actorGroup, *actor, *isManagerInCategory);
if (*isManagerInCategory) {
break;
}
}
}
if (*isManagerInCategory) {
*allowed = 1;
} else {
*reason = "You cannot use this group category because you are not a group manager in the *categoryName category.";
}
} else {
uuGroupUserExists("priv-category-add", *actor, false, *hasPriv);
if (*hasPriv) {
if (uuGroupCategoryNameIsValid(*categoryName)) {
*allowed = 1;
} else {
*reason = "Category names may only contain lowercase letters (a-z), numbers, and hyphens (-), and may not start or end with a hyphen.";
}
} else {
*reason = "You cannot use this group category because you are not a member of the priv-category-add group.";
}
}
}
# \brief Group Policy: Can the user set a certain group attribute to a certain value?
#
# Available attributes are: category, subcategory and description.
#
# \param[in] actor the user whose privileges are checked
# \param[in] groupName the group name
# \param[in] attribute the group attribute to set (one of 'category', 'subcategory', 'description', 'data_classification')
# \param[in] value the new value
# \param[out] allowed whether the action is allowed
# \param[out] reason the reason why the action was disallowed, set if allowed is false
#
uuGroupPolicyCanGroupModify(*actor, *groupName, *attribute, *value, *allowed, *reason) {
uuGetUserType(*actor, *actorUserType);
if (*actorUserType == "rodsadmin") { *allowed = 1; *reason = ""; succeed; }
*allowed = 0;
*reason = "";
uuGroupUserIsManager(*groupName, *actor, *isManager);
if (*isManager) {
if (*attribute == "category") {
if (*groupName like "datamanager-*" && *groupName != "datamanager-*value") {
*reason = "The category of a datamanager group cannot be changed.";
} else {
uuGroupPolicyCanUseCategory(*actor, *value, *allowed, *reason);
}
} else if (*attribute == "subcategory") {
if (uuGroupSubcategoryNameIsValid(*value)) {
*allowed = 1;
} else {
*reason = "Subcategory names may only contain letters (a-z), numbers, spaces, commas, periods, parentheses, hyphens (-) and underscores (_).";
}
} else if (*attribute == "description") {
*allowed = 1;
} else if (*attribute == "data_classification") {
uuGroupDataClassificationIsValid(*groupName, *value, *dataclasValid);
if (*dataclasValid) {
*allowed = 1;
} else {
*reason = "The chosen data classification is invalid for this type of group.";
}
} else {
*reason = "Invalid group attribute name.";
}
} else {
*reason = "You are not a manager of group *groupName.";
}
}
# \brief Group Policy: Can the user remove a group?
#
# \param[in] actor the user whose privileges are checked
# \param[in] groupName the group name
# \param[out] allowed whether the action is allowed
# \param[out] reason the reason why the action was disallowed, set if allowed is false
#
uuGroupPolicyCanGroupRemove(*actor, *groupName, *allowed, *reason) {
uuGetUserType(*actor, *actorUserType);
if (*actorUserType == "rodsadmin") { *allowed = 1; *reason = ""; succeed; }
*allowed = 0;
*reason = "";
uuGroupUserIsManager(*groupName, *actor, *isManager);
if (*isManager) {
# v These groups are user-removable v
if (*groupName like regex "(grp|intake|research|deposit|vault)-.*") {
# NB: Only rodsadmin can remove datamanager groups.
# Even datamanager group managers cannot remove their own group.
*homeCollection = "/$rodsZoneClient/home/*groupName";
*homeCollectionIsEmpty = true;
foreach (*row in SELECT DATA_NAME WHERE COLL_NAME = '*homeCollection') {
*homeCollectionIsEmpty = false; break;
}
foreach (*row in SELECT COLL_ID WHERE COLL_PARENT_NAME LIKE '*homeCollection') {
*homeCollectionIsEmpty = false; break;
}
if (*homeCollectionIsEmpty) {
*allowed = 1;
} else {
*reason = "The group's directory is not empty. Please remove all of its files and subdirectories before removing this group.";
}
} else {
*reason = "'*groupName' is not a regular group. You can only remove groups that have one of the following prefixes: grp, intake, research, vault.";
}
} else {
*reason = "You are not a manager of group *groupName.";
}
}
# \brief Group Policy: Can the user add a new user to a certain group?
#
# \param[in] actor the user whose privileges are checked
# \param[in] groupName the group name
# \param[in] newMember the user to add to the group
# \param[out] allowed whether the action is allowed
# \param[out] reason the reason why the action was disallowed, set if allowed is false
#
uuGroupPolicyCanGroupUserAdd(*actor, *groupName, *newMember, *allowed, *reason) {
uuGetUserType(*actor, *actorUserType);
if (*actorUserType == "rodsadmin") { *allowed = 1; *reason = ""; succeed; }
*allowed = 0;
*reason = "";
uuGroupGetMembers(*groupName, true, false, *members);
if (size(*members) == 0 && *newMember == *actor) {
# Special case for empty groups.
# This is run if a group has just been created. We then allow
# the group creator (already set in the 'manager' field by the
# postproc of GroupAdd) to add himself to the group.
*isCreator = false;
foreach (
*manager in
SELECT META_USER_ATTR_VALUE
WHERE USER_GROUP_NAME = '*groupName'
AND META_USER_ATTR_NAME = 'manager'
AND META_USER_ATTR_VALUE = '*newMember'
) {
*isCreator = true;
}
if (*isCreator) {
*allowed = 1;
} else {
*reason = "You are not a manager of group '*groupName'.";
}
} else {
uuGroupUserIsManager(*groupName, *actor, *isManager);
if (*isManager) {
uuGroupUserExists(*groupName, *newMember, false, *isAlreadyAMember);
if (*isAlreadyAMember) {
*reason = "User '*newMember' is already a member of group '*groupName'.";
} else {
if (uuUserNameIsValid(*newMember)) {
*allowed = 1;
} else {
*reason = "The new member's name is invalid.";
}
}
} else {
*reason = "You are not a manager of group *groupName.";
}
}
}
# \brief Group Policy: Can the user remove a user from a certain group?
#
# \param[in] actor the user whose privileges are checked
# \param[in] groupName the group name
# \param[in] member the user to remove from the group
# \param[out] allowed whether the action is allowed
# \param[out] reason the reason why the action was disallowed, set if allowed is false
#
uuGroupPolicyCanGroupUserRemove(*actor, *groupName, *member, *allowed, *reason) {
uuGetUserType(*actor, *actorUserType);
if (*actorUserType == "rodsadmin") { *allowed = 1; *reason = ""; succeed; }
*allowed = 0;
*reason = "";
uuGroupUserIsManager(*groupName, *actor, *isManager);
if (*isManager) {
if (*member == *actor) {
# This also ensures that groups always have at least one manager.
*reason = "You cannot remove yourself from group *groupName.";
} else {
*allowed = 1;
}
} else {
*reason = "You are not a manager of group *groupName.";
}
}
# \brief Group Policy: Can the user change a group member's role?
#
# \param[in] actor the user whose privileges are checked
# \param[in] groupName
# \param[in] member the target group member
# \param[in] newRole the member's new role
# \param[out] allowed whether the action is allowed
# \param[out] reason the reason why the action was disallowed, set if allowed is false
#
uuGroupPolicyCanGroupUserChangeRole(*actor, *groupName, *member, *newRole, *allowed, *reason) {
uuGetUserType(*actor, *actorUserType);
if (*actorUserType == "rodsadmin") { *allowed = 1; *reason = ""; succeed; }
*allowed = 0;
*reason = "";
uuGroupUserExists(*groupName, *member, true, *exists);
if (*exists) {
if ( (*newRole == "normal" || *newRole == "manager")
|| (*newRole == "reader" && *groupName like regex "(intake|research)-.*")) {
uuGroupUserIsManager(*groupName, *actor, *isManager);
if (*isManager) {
if (*member == *actor) {
*reason = "You cannot change your own role in group *groupName.";
} else {
*allowed = 1;
}
} else {
*reason = "You are not a manager of group *groupName.";
}
} else {
*reason = "'*newRole' is not a valid member role for group *groupName.";
}
} else {
*reason = "'*member' is not a member of group *groupName.";
}
}
# \brief User Policy: Can the user set a certain user attribute to a certain value?
#
# \param[in] actor the user whose privileges are checked
# \param[in] userName the user name
# \param[in] attribute the user attribute to set
# \param[out] allowed whether the action is allowed
# \param[out] reason the reason why the action was disallowed, set if allowed is false
#
uuUserPolicyCanUserModify(*actor, *userName, *attribute, *allowed, *reason) {
uuGetUserType(*actor, *actorUserType);
if (*actorUserType == "rodsadmin") { *allowed = 1; *reason = ""; succeed; }
*allowed = 0;
*reason = "";
# User setting: mail notifications
if (*attribute == "org_settings_mail_notifications") {
if (*actor == *userName) {
*allowed = 1;
} else {
*reason = "Cannot modify settings of other user.";
}
# User notifications
} else if (trimr(*attribute, "_") == "org_notification") {
*allowed = 1;
} else {
*reason = "Invalid user attribute name.";
}
}