Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b950fbd8763e5641e7c3c61eeecc36910fa9136f (plain) (blame)
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
 * Copyright (c) OSGi Alliance (2001, 2016). All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.osgi.service.useradmin;

import java.io.IOException;
import java.security.BasicPermission;
import java.security.Permission;
import java.security.PermissionCollection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

/**
 * Permission to configure and access the {@link Role} objects managed by a User
 * Admin service.
 * 
 * <p>
 * This class represents access to the {@code Role} objects managed by a User
 * Admin service and their properties and credentials (in the case of
 * {@link User} objects).
 * <p>
 * The permission name is the name (or name prefix) of a property or credential.
 * The naming convention follows the hierarchical property naming convention.
 * Also, an asterisk may appear at the end of the name, following a
 * &quot;.&quot;, or by itself, to signify a wildcard match. For example:
 * &quot;org.osgi.security.protocol.*&quot; or &quot;*&quot; is valid, but
 * &quot;*protocol&quot; or &quot;a*b&quot; are not valid.
 * 
 * <p>
 * The {@code UserAdminPermission} with the reserved name &quot;admin&quot;
 * represents the permission required for creating and removing {@code Role}
 * objects in the User Admin service, as well as adding and removing members in
 * a {@code Group} object. This {@code UserAdminPermission} does not have any
 * actions associated with it.
 * 
 * <p>
 * The actions to be granted are passed to the constructor in a string
 * containing a list of one or more comma-separated keywords. The possible
 * keywords are: {@code changeProperty},{@code changeCredential}, and
 * {@code getCredential}. Their meaning is defined as follows:
 * 
 * <pre>
 *
 *  action
 *  changeProperty    Permission to change (i.e., add and remove)
 *                    Role object properties whose names start with
 *                    the name argument specified in the constructor.
 *  changeCredential  Permission to change (i.e., add and remove)
 *                    User object credentials whose names start
 *                    with the name argument specified in the constructor.
 *  getCredential     Permission to retrieve and check for the
 *                    existence of User object credentials whose names
 *                    start with the name argument specified in the
 *                    constructor.
 *
 * </pre>
 * 
 * The action string is converted to lowercase before processing.
 * 
 * <p>
 * Following is a PermissionInfo style policy entry which grants a user
 * administration bundle a number of {@code UserAdminPermission} object:
 * 
 * <pre>
 *
 *  (org.osgi.service.useradmin.UserAdminPermission &quot;admin&quot;)
 *  (org.osgi.service.useradmin.UserAdminPermission &quot;com.foo.*&quot;
 *                &quot;changeProperty,getCredential,changeCredential&quot;)
 *  (org.osgi.service.useradmin.UserAdminPermission &quot;user.*&quot;
 *                              &quot;changeProperty,changeCredential&quot;)
 *
 * </pre>
 * 
 * The first permission statement grants the bundle the permission to perform
 * any User Admin service operations of type "admin", that is, create and remove
 * roles and configure {@code Group} objects.
 * 
 * <p>
 * The second permission statement grants the bundle the permission to change
 * any properties as well as get and change any credentials whose names start
 * with {@code com.foo.}.
 * 
 * <p>
 * The third permission statement grants the bundle the permission to change any
 * properties and credentials whose names start with {@code user.}. This means
 * that the bundle is allowed to change, but not retrieve any credentials with
 * the given prefix.
 * 
 * <p>
 * The following policy entry empowers the Http Service bundle to perform user
 * authentication:
 * 
 * <pre>
 *
 *  grant codeBase &quot;${jars}http.jar&quot; {
 *    permission org.osgi.service.useradmin.UserAdminPermission
 *      &quot;user.password&quot;, &quot;getCredential&quot;;
 *  };
 *
 * </pre>
 * 
 * <p>
 * The permission statement grants the Http Service bundle the permission to
 * validate any password credentials (for authentication purposes), but the
 * bundle is not allowed to change any properties or credentials.
 * 
 * @ThreadSafe
 * @author $Id$
 */
public final class UserAdminPermission extends BasicPermission {
	static final long			serialVersionUID			= -1179971692401603789L;
	/**
	 * The permission name &quot;admin&quot;.
	 */
	public static final String	ADMIN						= "admin";
	/**
	 * The action string &quot;changeProperty&quot;.
	 */
	public static final String	CHANGE_PROPERTY				= "changeProperty";
	private static final int	ACTION_CHANGE_PROPERTY		= 0x1;
	/**
	 * The action string &quot;changeCredential&quot;.
	 */
	public static final String	CHANGE_CREDENTIAL			= "changeCredential";
	private static final int	ACTION_CHANGE_CREDENTIAL	= 0x2;
	/**
	 * The action string &quot;getCredential&quot;.
	 */
	public static final String	GET_CREDENTIAL				= "getCredential";
	private static final int	ACTION_GET_CREDENTIAL		= 0x4;
	/**
	 * All actions
	 */
	private static final int	ACTION_ALL					= ACTION_CHANGE_PROPERTY | ACTION_CHANGE_CREDENTIAL | ACTION_GET_CREDENTIAL;
	/**
	 * No actions.
	 */
	static final int			ACTION_NONE					= 0;
	/**
	 * The actions in canonical form.
	 * 
	 * @serial
	 */
	private volatile String		actions						= null;
	/**
	 * The actions mask.
	 */
	private transient int		action_mask;

	/**
	 * Creates a new {@code UserAdminPermission} with the specified name and
	 * actions. {@code name} is either the reserved string &quot;admin&quot; or
	 * the name of a credential or property, and {@code actions} contains a
	 * comma-separated list of the actions granted on the specified name. Valid
	 * actions are {@code changeProperty},{@code changeCredential}, and
	 * getCredential.
	 * 
	 * @param name the name of this {@code UserAdminPermission}
	 * @param actions the action string.
	 * 
	 * @throws IllegalArgumentException If {@code name} equals &quot;admin&quot;
	 *         and {@code actions} are specified.
	 */
	public UserAdminPermission(String name, String actions) {
		this(name, parseActions(actions));
	}

	/**
	 * Package private constructor used by {@code UserAdminPermissionCollection}
	 * .
	 * 
	 * @param name class name
	 * @param mask action mask
	 */
	UserAdminPermission(String name, int mask) {
		super(name);
		setTransients(mask);
	}

	/**
	 * Called by constructors and when deserialized.
	 * 
	 * @param mask action mask
	 */
	private synchronized void setTransients(int mask) {
		if (getName().equals(ADMIN)) {
			if (mask != ACTION_NONE) {
				throw new IllegalArgumentException("Actions specified for " + "no-action " + "UserAdminPermission");
			}
		} else {
			if ((mask == ACTION_NONE) || ((mask & ACTION_ALL) != mask)) {
				throw new IllegalArgumentException("Invalid action string");
			}
		}
		action_mask = mask;
	}

	/**
	 * Returns the current action mask.
	 * <p>
	 * Used by the UserAdminPermissionCollection class.
	 * 
	 * @return Current action mask.
	 */
	synchronized int getActionsMask() {
		return action_mask;
	}

	/**
	 * Parse action string into action mask.
	 * 
	 * @param actions Action string.
	 * @return action mask.
	 */
	private static int parseActions(String actions) {
		boolean seencomma = false;
		int mask = ACTION_NONE;
		if (actions == null) {
			return mask;
		}
		char[] a = actions.toCharArray();
		int i = a.length - 1;
		if (i < 0)
			return mask;
		while (i != -1) {
			char c;
			// skip whitespace
			while ((i != -1) && ((c = a[i]) == ' ' || c == '\r' || c == '\n' || c == '\f' || c == '\t'))
				i--;
			// check for the known strings
			int matchlen;
			if (i >= 12 && match_get(a, i - 10) && match_credential(a, i)) {
				matchlen = 13;
				mask |= ACTION_GET_CREDENTIAL;
			} else
				if (i >= 13 && match_change(a, i - 8) && match_property(a, i)) {
					matchlen = 14;
					mask |= ACTION_CHANGE_PROPERTY;
				} else
					if (i >= 15 && match_change(a, i - 10) && match_credential(a, i)) {
						matchlen = 16;
						mask |= ACTION_CHANGE_CREDENTIAL;
					} else {
						// parse error
						throw new IllegalArgumentException("invalid permission: " + actions);
					}
			// make sure we didn't just match the tail of a word
			// like "ackbarfimport". Also, skip to the comma.
			seencomma = false;
			while (i >= matchlen && !seencomma) {
				switch (a[i - matchlen]) {
					case ',' :
						seencomma = true;
						/* FALLTHROUGH */
					case ' ' :
					case '\r' :
					case '\n' :
					case '\f' :
					case '\t' :
						break;
					default :
						throw new IllegalArgumentException("invalid permission: " + actions);
				}
				i--;
			}
			// point i at the location of the comma minus one (or -1).
			i -= matchlen;
		}
		if (seencomma) {
			throw new IllegalArgumentException("invalid permission: " + actions);
		}
		return mask;
	}

	private static boolean match_change(char[] a, int i) {
		return ((a[i - 5] == 'c' || a[i - 5] == 'C') && (a[i - 4] == 'h' || a[i - 4] == 'H') && (a[i - 3] == 'a' || a[i - 3] == 'A') && (a[i - 2] == 'n' || a[i - 2] == 'N')
				&& (a[i - 1] == 'g' || a[i - 1] == 'G') && (a[i - 0] == 'e' || a[i - 0] == 'E'));
	}

	private static boolean match_get(char[] a, int i) {
		return ((a[i - 2] == 'g' || a[i - 2] == 'G') && (a[i - 1] == 'e' || a[i - 1] == 'E') && (a[i - 0] == 't' || a[i - 0] == 'T'));
	}

	private static boolean match_property(char[] a, int i) {
		return ((a[i - 7] == 'p' || a[i - 7] == 'P') && (a[i - 6] == 'r' || a[i - 6] == 'R') && (a[i - 5] == 'o' || a[i - 5] == 'O') && (a[i - 4] == 'p' || a[i - 4] == 'P')
				&& (a[i - 3] == 'e' || a[i - 3] == 'E') && (a[i - 2] == 'r' || a[i - 2] == 'R') && (a[i - 1] == 't' || a[i - 1] == 'T') && (a[i - 0] == 'y' || a[i - 0] == 'Y'));
	}

	private static boolean match_credential(char[] a, int i) {
		return ((a[i - 9] == 'c' || a[i - 9] == 'C') && (a[i - 8] == 'r' || a[i - 8] == 'R') && (a[i - 7] == 'e' || a[i - 7] == 'E') && (a[i - 6] == 'd' || a[i - 6] == 'D')
				&& (a[i - 5] == 'e' || a[i - 5] == 'E') && (a[i - 4] == 'n' || a[i - 4] == 'N') && (a[i - 3] == 't' || a[i - 3] == 'T') && (a[i - 2] == 'i' || a[i - 2] == 'I')
				&& (a[i - 1] == 'a' || a[i - 1] == 'A') && (a[i - 0] == 'l' || a[i - 0] == 'L'));
	}

	/**
	 * Checks if this {@code UserAdminPermission} object &quot;implies&quot; the
	 * specified permission.
	 * <P>
	 * More specifically, this method returns {@code true} if:
	 * <p>
	 * <ul>
	 * <li><i>p</i> is an instanceof {@code UserAdminPermission},</li>
	 * <li><i>p</i>'s actions are a proper subset of this object's actions, and</li>
	 * <li><i>p</i>'s name is implied by this object's name. For example,
	 * &quot;java.*&quot; implies &quot;java.home&quot;.</li>
	 * </ul>
	 * 
	 * @param p the permission to check against.
	 * 
	 * @return {@code true} if the specified permission is implied by this
	 *         object; {@code false} otherwise.
	 */
	@Override
	public boolean implies(Permission p) {
		if (p instanceof UserAdminPermission) {
			UserAdminPermission requested = (UserAdminPermission) p;
			int mask = getActionsMask();
			int targetMask = requested.getActionsMask();
			return // Check that the we have the requested action
			((targetMask & mask) == targetMask) &&
			// If the target action mask is ACTION_NONE, it must be an
			// admin permission, and then we must be that too
					(targetMask != ACTION_NONE || mask == ACTION_NONE) &&
					// Check that name name matches
					super.implies(p);
		}
		return false;
	}

	/**
	 * Returns the canonical string representation of the actions, separated by
	 * comma.
	 * 
	 * @return the canonical string representation of the actions.
	 */
	@Override
	public String getActions() {
		String result = actions;
		if (result == null) {
			StringBuilder sb = new StringBuilder();
			boolean comma = false;
			int mask = getActionsMask();
			if ((mask & ACTION_CHANGE_CREDENTIAL) == ACTION_CHANGE_CREDENTIAL) {
				sb.append(CHANGE_CREDENTIAL);
				comma = true;
			}
			if ((mask & ACTION_CHANGE_PROPERTY) == ACTION_CHANGE_PROPERTY) {
				if (comma)
					sb.append(',');
				sb.append(CHANGE_PROPERTY);
				comma = true;
			}
			if ((mask & ACTION_GET_CREDENTIAL) == ACTION_GET_CREDENTIAL) {
				if (comma)
					sb.append(',');
				sb.append(GET_CREDENTIAL);
			}
			actions = result = sb.toString();
		}
		return result;
	}

	/**
	 * Returns a new {@code PermissionCollection} object for storing
	 * {@code UserAdminPermission} objects.
	 * 
	 * @return a new {@code PermissionCollection} object suitable for storing
	 *         {@code UserAdminPermission} objects.
	 */
	@Override
	public PermissionCollection newPermissionCollection() {
		return new UserAdminPermissionCollection();
	}

	/**
	 * Checks two {@code UserAdminPermission} objects for equality. Checks that
	 * {@code obj} is a {@code UserAdminPermission}, and has the same name and
	 * actions as this object.
	 * 
	 * @param obj the object to be compared for equality with this object.
	 * 
	 * @return {@code true} if {@code obj} is a {@code UserAdminPermission}
	 *         object, and has the same name and actions as this
	 *         {@code UserAdminPermission} object.
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof UserAdminPermission)) {
			return false;
		}

		UserAdminPermission uap = (UserAdminPermission) obj;

		return (getActionsMask() == uap.getActionsMask()) && getName().equals(uap.getName());
	}

	/**
	 * Returns the hash code value for this object.
	 * 
	 * @return A hash code value for this object.
	 */
	@Override
	public int hashCode() {
		int h = 31 * 17 + getName().hashCode();
		h = 31 * h + getActions().hashCode();
		return h;
	}

	/**
	 * writeObject is called to save the state of this object to a stream. The
	 * actions are serialized, and the superclass takes care of the name.
	 */
	private synchronized void writeObject(java.io.ObjectOutputStream s) throws IOException {
		// Write out the actions. The superclass takes care of the name
		// call getActions to make sure actions field is initialized
		if (actions == null)
			getActions();
		s.defaultWriteObject();
	}

	/*
	 * Restores this object from a stream (i.e., deserializes it).
	 */
	private synchronized void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
		// Read in the action, then initialize the rest
		s.defaultReadObject();
		setTransients(parseActions(actions));
	}

	/**
	 * Returns a string describing this {@code UserAdminPermission} object. This
	 * string must be in {@code PermissionInfo} encoded format.
	 * 
	 * @return The {@code PermissionInfo} encoded string for this
	 *         {@code UserAdminPermission} object.
	 * @see "org.osgi.service.permissionadmin.PermissionInfo.getEncoded()"
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append('(');
		sb.append(getClass().getName());
		sb.append(" \"");
		sb.append(getName());
		String a = getActions();
		if (a.length() > 0) {
			sb.append("\" \"");
			sb.append(a);
		}
		sb.append("\")");
		return sb.toString();
	}
}

/**
 * A {@code UserAdminPermissionCollection} stores a set of
 * {@code UserAdminPermission} permissions.
 */

final class UserAdminPermissionCollection extends PermissionCollection {
	static final long		serialVersionUID	= -7222111885230120581L;
	/**
	 * Table of permissions.
	 * 
	 * @serial
	 * @GuardedBy this
	 */
	private final Hashtable<String, UserAdminPermission>	permissions;
	/**
	 * Boolean saying if "*" is in the collection.
	 * 
	 * @serial
	 * @GuardedBy this
	 */
	private boolean			all_allowed;

	/**
	 * Creates an empty {@code UserAdminPermissionCollection} object.
	 */
	public UserAdminPermissionCollection() {
		permissions = new Hashtable<>();
		all_allowed = false;
	}

	/**
	 * Adds the given permission to this {@code UserAdminPermissionCollection}.
	 * The key for the hash is the name.
	 * 
	 * @param permission the {@code Permission} object to add.
	 * 
	 * @throws IllegalArgumentException If the given permission is not a
	 *         {@code UserAdminPermission}
	 * @throws SecurityException If this {@code UserAdminPermissionCollection}
	 *         object has been marked readonly
	 */
	@Override
	public void add(Permission permission) {
		if (!(permission instanceof UserAdminPermission))
			throw new IllegalArgumentException("Invalid permission: " + permission);
		if (isReadOnly()) {
			throw new SecurityException("Attempt to add a Permission to a " + "readonly PermissionCollection");
		}
		final UserAdminPermission uap = (UserAdminPermission) permission;
		final String name = uap.getName();
		synchronized (this) {
			final UserAdminPermission existing = permissions.get(name);
			if (existing != null) {
				int oldMask = existing.getActionsMask();
				int newMask = uap.getActionsMask();
				if (oldMask != newMask) {
					permissions.put(name, new UserAdminPermission(name, oldMask | newMask));
				}
			} else {
				permissions.put(name, uap);
			}
			if (!all_allowed) {
				if (name.equals("*")) {
					all_allowed = true;
				}
			}
		}
	}

	/**
	 * Checks to see if this {@code PermissionCollection} implies the given
	 * permission.
	 * 
	 * @param permission the {@code Permission} object to check against
	 * 
	 * @return true if the given permission is implied by this
	 *         {@code PermissionCollection}, false otherwise.
	 */
	@Override
	public boolean implies(Permission permission) {
		if (!(permission instanceof UserAdminPermission)) {
			return false;
		}
		final UserAdminPermission requested = (UserAdminPermission) permission;
		String name = requested.getName();
		final int desired = requested.getActionsMask();
		UserAdminPermission x;
		int effective = 0;
		synchronized (this) {
			// Short circuit if the "*" Permission was added.
			// desired can only be ACTION_NONE when name is "admin".
			if (all_allowed && (desired != UserAdminPermission.ACTION_NONE)) {
				x = permissions.get("*");
				if (x != null) {
					effective |= x.getActionsMask();
					if ((effective & desired) == desired) {
						return true;
					}
				}
			}
			// strategy:
			// Check for full match first. Then work our way up the
			// name looking for matches on a.b.*

			x = permissions.get(name);
		}
		if (x != null) {
			// we have a direct hit!
			effective |= x.getActionsMask();
			if ((effective & desired) == desired) {
				return true;
			}
		}
		// work our way up the tree...
		int last;
		int offset = name.length() - 1;
		while ((last = name.lastIndexOf(".", offset)) != -1) {
			name = name.substring(0, last + 1) + "*";
			synchronized (this) {
				x = permissions.get(name);
			}
			if (x != null) {
				effective |= x.getActionsMask();
				if ((effective & desired) == desired) {
					return true;
				}
			}
			offset = last - 1;
		}
		// we don't have to check for "*" as it was already checked
		// at the top (all_allowed), so we just return false
		return false;
	}

	/**
	 * Returns an enumeration of all the {@code UserAdminPermission} objects in
	 * the container.
	 * 
	 * @return an enumeration of all the {@code UserAdminPermission} objects.
	 */
	@Override
	public Enumeration<Permission> elements() {
		List<Permission> all = new ArrayList<Permission>(permissions.values());
		return Collections.enumeration(all);
	}
}

Back to the top