Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4f3408f5a399e5b5ccd0ea5849e758ab120126d0 (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
/*******************************************************************************
 * Copyright (c) 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.framework.internal.core;

import java.io.*;
import java.security.*;
import java.util.Vector;
import org.eclipse.osgi.framework.adaptor.PermissionStorage;
import org.eclipse.osgi.framework.debug.Debug;
import org.osgi.framework.FrameworkEvent;
import org.osgi.service.permissionadmin.PermissionAdmin;
import org.osgi.service.permissionadmin.PermissionInfo;

/**
 * Permission Admin service for the OSGi specification.
 *
 * The Permission Admin service allows operators to
 * manage the permissions of bundles. There is at most one Permission Admin
 * service present in the Framework.
 * <p>
 * Access to the Permission Admin service is protected by
 * corresponding
 * <tt>ServicePermission</tt>. In addition the <tt>AdminPermission</tt>
 * is required to actually set permissions.
 *
 * <p>Bundle permissions are managed using a permission table. A bundle's location
 * serves as the key into this permission table. The value of a table entry is
 * the set of permissions (of type <tt>PermissionInfo</tt>) granted to the
 * bundle with the given location.
 * A bundle may have an entry in the permission table prior to being installed
 * in the Framework.
 *
 * <p>The permissions specified in <tt>setDefaultPermissions</tt> are used as the
 * default
 * permissions which are granted to all bundles that do not have an entry in
 * the permission table.
 *
 * <p>Any changes to a bundle's permissions in the permission table will take
 * effect no later than when bundle's <tt>java.security.ProtectionDomain</tt>
 * is involved in a permission check, and will be made persistent.
 *
 * <p>Only permission classes on the system classpath or from an exported
 * package are considered during a permission check.
 * Additionally, only permission classes that are subclasses of
 * <tt>java.security.Permission</tt> and define a 2-argument constructor
 * that takes a <i>name</i> string and an <i>actions</i> string can be used.
 * <p>
 * Permissions implicitly granted by the Framework (for example, a bundle's
 * permission to access its persistent storage area) cannot be changed, and
 * are not reflected in the permissions returned by <tt>getPermissions</tt>
 * and <tt>getDefaultPermissions</tt>.
 */
public class PermissionAdminImpl implements PermissionAdmin {
	/** framework object */
	protected Framework framework;

	/** permission storage object */
	protected PermissionStorage storage;

	/** The permissions to use if no other permissions can be determined */
	protected PermissionInfo[] defaultDefaultPermissionInfos;

	/** The basic implied permissions for a bundle */
	protected PermissionInfo[] baseImpliedPermissionInfos;

	/** The permission collection containing the default assigned permissions */
	protected BundleCombinedPermissions defaultAssignedPermissions;

	/**
	 * Construstor.
	 *
	 * @param framework Framework object.
	 */
	protected PermissionAdminImpl(Framework framework, PermissionStorage storage) {
		this.framework = framework;
		this.storage = storage;

		defaultDefaultPermissionInfos = getPermissionInfos(Constants.OSGI_DEFAULT_DEFAULT_PERMISSIONS);
		baseImpliedPermissionInfos = getPermissionInfos(Constants.OSGI_BASE_IMPLIED_PERMISSIONS);

		//      if (Debug.DEBUG)
		//      {
		//          // This is necessary to allow File.getAbsolutePath() in debug statements.
		//          int _length = baseImpliedPermissionInfos.length;
		//
		//          PermissionInfo[] debugBaseImpliedPermissionInfos = new PermissionInfo[_length + 1];
		//
		//          System.arraycopy(baseImpliedPermissionInfos, 0, debugBaseImpliedPermissionInfos, 0, _length);
		//
		//          debugBaseImpliedPermissionInfos[_length] = new PermissionInfo("(java.util.PropertyPermission \"user.dir\" \"read\")");
		//
		//          baseImpliedPermissionInfos = debugBaseImpliedPermissionInfos;
		//      }

		if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
			Debug.println("Default default assigned bundle permissions"); //$NON-NLS-1$
			if (defaultDefaultPermissionInfos == null) {
				Debug.println("  <none>"); //$NON-NLS-1$
			} else {
				for (int i = 0; i < defaultDefaultPermissionInfos.length; i++) {
					Debug.println("  " + defaultDefaultPermissionInfos[i]); //$NON-NLS-1$
				}
			}

			Debug.println("Base implied bundle permissions"); //$NON-NLS-1$
			if (baseImpliedPermissionInfos == null) {
				Debug.println("  <none>"); //$NON-NLS-1$
			} else {
				for (int i = 0; i < baseImpliedPermissionInfos.length; i++) {
					Debug.println("  " + baseImpliedPermissionInfos[i]); //$NON-NLS-1$
				}
			}
		}

		defaultAssignedPermissions = new BundleCombinedPermissions(null);
		defaultAssignedPermissions.setAssignedPermissions(createDefaultAssignedPermissions(getDefaultPermissions()));
	}

	/**
	 * Gets the permissions assigned to the bundle with the specified
	 * location.
	 *
	 * @param location The location of the bundle whose permissions are to
	 * be returned.
	 *
	 * @return The permissions assigned to the bundle with the specified
	 * location, or <tt>null</tt> if that bundle has not been assigned any
	 * permissions.
	 */
	public PermissionInfo[] getPermissions(String location) {
		if (location == null) {
			throw new NullPointerException();
		}

		PermissionStorage storage = new org.eclipse.osgi.framework.util.SecurePermissionStorage(this.storage);

		try {
			String[] data = storage.getPermissionData(location);

			if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
				Debug.println("Getting permissions for location: " + location); //$NON-NLS-1$
				if (data == null) {
					Debug.println("  <none>"); //$NON-NLS-1$
				} else {
					for (int i = 0; i < data.length; i++) {
						Debug.println("  " + data[i]); //$NON-NLS-1$
					}
				}
			}

			return makePermissionInfo(data);
		} catch (IOException e) {
			framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, e);

			return null;
		}
	}

	/**
	 * Assigns the specified permissions to the bundle with the specified
	 * location.
	 *
	 * @param location The location of the bundle that will be assigned the
	 *                 permissions.
	 * @param permissions The permissions to be assigned, or <tt>null</tt>
	 * if the specified location is to be removed from the permission table.
	 * @exception SecurityException if the caller does not have the
	 * <tt>AdminPermission</tt>.
	 */
	public void setPermissions(String location, PermissionInfo[] permissions) {
		framework.checkAdminPermission();

		if (location == null) {
			throw new NullPointerException();
		}

		PermissionStorage storage = new org.eclipse.osgi.framework.util.SecurePermissionStorage(this.storage);

		try {
			String[] data = makePermissionData(permissions);

			if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
				Debug.println("Setting permissions for location: " + location); //$NON-NLS-1$
				if (data == null) {
					Debug.println("  <none>"); //$NON-NLS-1$
				} else {
					for (int i = 0; i < data.length; i++) {
						Debug.println("  " + data[i]); //$NON-NLS-1$
					}
				}
			}

			storage.setPermissionData(location, data);
		} catch (IOException e) {
			framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, e);

			return;
		}

		AbstractBundle bundle = framework.getBundleByLocation(location);

		if ((bundle != null) && (bundle.getBundleId() != 0)) {
			ProtectionDomain domain = bundle.getProtectionDomain();

			if (domain != null) {
				BundleCombinedPermissions combined = (BundleCombinedPermissions) domain.getPermissions();

				if (permissions == null) {
					combined.setAssignedPermissions(defaultAssignedPermissions);
				} else {
					combined.setAssignedPermissions(createPermissions(permissions, bundle));
				}
			}
		}
	}

	/**
	 * Returns the bundle locations that have permissions assigned to them,
	 * that is, bundle locations for which an entry
	 * exists in the permission table.
	 *
	 * @return The locations of bundles that have been assigned any
	 * permissions, or <tt>null</tt> if the permission table is empty.
	 */
	public String[] getLocations() {
		PermissionStorage storage = new org.eclipse.osgi.framework.util.SecurePermissionStorage(this.storage);

		try {
			String[] locations = storage.getLocations();

			return locations;
		} catch (IOException e) {
			framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, e);

			return null;
		}
	}

	/**
	 * Gets the default permissions.
	 *
	 * <p>These are the permissions granted to any bundle that does not
	 * have permissions assigned to its location.
	 *
	 * @return The default permissions, or <tt>null</tt> if default
	 * permissions have not been defined.
	 */
	public PermissionInfo[] getDefaultPermissions() {
		PermissionStorage storage = new org.eclipse.osgi.framework.util.SecurePermissionStorage(this.storage);

		try {
			String[] data = storage.getPermissionData(null);

			if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
				Debug.println("Getting default permissions"); //$NON-NLS-1$
				if (data == null) {
					Debug.println("  <none>"); //$NON-NLS-1$
				} else {
					for (int i = 0; i < data.length; i++) {
						Debug.println("  " + data[i]); //$NON-NLS-1$
					}
				}
			}

			return makePermissionInfo(data);
		} catch (IOException e) {
			framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, e);

			return null;
		}
	}

	/**
	 * Sets the default permissions.
	 *
	 * <p>These are the permissions granted to any bundle that does not
	 * have permissions assigned to its location.
	 *
	 * @param permissions The default permissions.
	 * @exception SecurityException if the caller does not have the
	 * <tt>AdminPermission</tt>.
	 */
	public void setDefaultPermissions(PermissionInfo[] permissions) {
		framework.checkAdminPermission();

		PermissionStorage storage = new org.eclipse.osgi.framework.util.SecurePermissionStorage(this.storage);

		try {
			String[] data = makePermissionData(permissions);

			if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
				Debug.println("Setting default permissions"); //$NON-NLS-1$
				if (data == null) {
					Debug.println("  <none>"); //$NON-NLS-1$
				} else {
					for (int i = 0; i < data.length; i++) {
						Debug.println("  " + data[i]); //$NON-NLS-1$
					}
				}
			}

			storage.setPermissionData(null, data);
		} catch (IOException e) {
			framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, e);

			return;
		}

		defaultAssignedPermissions.setAssignedPermissions(createDefaultAssignedPermissions(permissions));
	}

	/**
	 * Make a PermissionInfo array from an array of encoded permission Strings.
	 *
	 * @param data Array of encoded permission Strings
	 * @return Array of PermissionInfo objects.
	 */
	protected PermissionInfo[] makePermissionInfo(String[] data) {
		if (data == null) {
			return null;
		}

		int size = data.length;

		PermissionInfo[] permissions = new PermissionInfo[size];

		for (int i = 0; i < size; i++) {
			permissions[i] = new PermissionInfo(data[i]);
		}

		return permissions;
	}

	/**
	 * Make an array of encoded permission Strings from a PermissionInfo array.
	 *
	 * @param permissions Array of PermissionInfor objects.
	 * @return Array of encoded permission Strings
	 */
	protected String[] makePermissionData(PermissionInfo[] permissions) {
		if (permissions == null) {
			return null;
		}

		int size = permissions.length;

		String[] data = new String[size];

		for (int i = 0; i < size; i++) {
			data[i] = permissions[i].getEncoded();
		}

		return data;
	}

	/**
	 * This method is called by the Bundle object to create the
	 * PermissionCollection used by the bundle's ProtectionDomain.
	 *
	 * @param bundle the bundle object
	 * @return BundleCombinedPermission object with the bundle's
	 * dynamic permissions.
	 */
	protected PermissionCollection createPermissionCollection(AbstractBundle bundle) {
		BundlePermissionCollection implied = getImpliedPermissions(bundle);

		BundleCombinedPermissions combined = new BundleCombinedPermissions(implied);

		BundlePermissionCollection assigned = getAssignedPermissions(bundle);

		combined.setAssignedPermissions(assigned);

		return combined;
	}

	/**
	 * Creates the default assigned permissions for bundles that
	 * have no assigned permissions.
	 * The default permissions are assigned via the PermissionAdmin service
	 * and may change dynamically.
	 *
	 * @return A PermissionCollection of the default assigned permissions.
	 */
	protected BundlePermissionCollection createDefaultAssignedPermissions(PermissionInfo[] info) {
		if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
			Debug.println("Creating default assigned permissions"); //$NON-NLS-1$
		}

		if (info == null) {
			info = defaultDefaultPermissionInfos;
		}

		return createPermissions(info, null);
	}

	/**
	 * Returns the assigned permissions for a bundle.
	 * These permissions are assigned via the PermissionAdmin service
	 * and may change dynamically.
	 *
	 * @param bundle The bundle to create the permissions for.
	 * @return A PermissionCollection of the assigned permissions.
	 */
	protected BundlePermissionCollection getAssignedPermissions(AbstractBundle bundle) {
		String location = bundle.getLocation();

		PermissionInfo[] info = getPermissions(location);

		if (info == null) {
			return defaultAssignedPermissions;
		}

		if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
			Debug.println("Creating assigned permissions for " + bundle); //$NON-NLS-1$
		}

		return createPermissions(info, bundle);
	}

	/**
	 * Returns the implied permissions for a bundle.
	 * These permissions never change.
	 *
	 * @param bundle The bundle to create the permissions for.
	 * @return A PermissionCollection of the implied permissions.
	 */
	protected BundlePermissionCollection getImpliedPermissions(AbstractBundle bundle) {
		if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
			Debug.println("Creating implied permissions for " + bundle); //$NON-NLS-1$
		}

		BundlePermissionCollection collection = createPermissions(baseImpliedPermissionInfos, bundle);

		Permission permission = new BundleResourcePermission(bundle.getBundleId());

		if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
			Debug.println("Created permission: " + permission); //$NON-NLS-1$
		}

		collection.add(permission);

		return collection;
	}

	/**
	 * Read the permissions from the specified resource.
	 *
	 * @return An array of PermissionInfo objects from the specified
	 * resource.
	 */
	protected PermissionInfo[] getPermissionInfos(String resource) {
		PermissionInfo[] info = null;

		InputStream in = getClass().getResourceAsStream(resource);

		if (in != null) {
			try {
				Vector permissions = new Vector();

				BufferedReader reader;
				try {
					reader = new BufferedReader(new InputStreamReader(in, "UTF8")); //$NON-NLS-1$
				} catch (UnsupportedEncodingException e) {
					reader = new BufferedReader(new InputStreamReader(in));
				}

				while (true) {
					String line = reader.readLine();

					if (line == null) /* EOF */{
						break;
					}

					line = line.trim();

					if ((line.length() == 0) || line.startsWith("#") || line.startsWith("//")) /* comments */{ //$NON-NLS-1$ //$NON-NLS-2$
						continue;
					}

					try {
						permissions.addElement(new PermissionInfo(line));
					} catch (IllegalArgumentException iae) {
						/* incorrectly encoded permission */

						framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, iae);
					}
				}

				int size = permissions.size();

				if (size > 0) {
					info = new PermissionInfo[size];

					permissions.copyInto(info);
				}
			} catch (IOException e) {
			} finally {
				try {
					in.close();
				} catch (IOException ee) {
				}
			}
		}

		return info;
	}

	/**
	 * Create a PermissionCollection from a PermissionInfo array.
	 *
	 * @param info Array of PermissionInfo objects.
	 * @param bundle The target bundle for the permissions.
	 * @return A PermissionCollection containing Permission objects.
	 */
	protected BundlePermissionCollection createPermissions(PermissionInfo[] info, final AbstractBundle bundle) {
		BundlePermissionCollection collection = new BundlePermissions(framework.packageAdmin);

		/* add the permissions */
		int size = info.length;
		for (int i = 0; i < size; i++) {
			PermissionInfo perm = info[i];

			String type = perm.getType();

			if (type.equals("java.io.FilePermission")) { //$NON-NLS-1$
				/* map FilePermissions for relative names to
				 * the bundle's data area
				 */
				String name = perm.getName();

				if (!name.equals("<<ALL FILES>>")) { //$NON-NLS-1$
					File file = new File(name);

					if (!file.isAbsolute()) /* relative name */{
						if (bundle == null) /* default permissions */{
							continue; /* no relative file permissions */
						}

						File target = framework.getDataFile(bundle, name);

						if (target == null) /* no bundle data file area */{
							continue; /* no relative file permissions */
						}

						perm = new PermissionInfo(type, target.getPath(), perm.getActions());
					}
				}
			}

			collection.add(createPermission(perm));
		}

		return collection;
	}

	/**
	 * Create a Permission object from a PermissionInfo object.
	 * If the type of the permission is not loadable from
	 * this object's classloader (i.e. the system classloader)
	 * then an UnresolvedPermission is returned.
	 *
	 * @param info Description of the desired permission.
	 * @return A permission object.
	 */
	protected Permission createPermission(PermissionInfo info) {
		String type = info.getType();
		String name = info.getName();
		String actions = info.getActions();

		UnresolvedPermission permission = new UnresolvedPermission(type, name, actions);

		try {
			/* Only search the system classloader (ours) at this point.
			 * Permission classes exported by bundles will be
			 * resolved later.
			 * This is done so that permission classes exported by bundles
			 * may be easily unresolved during packageRefresh.
			 */
			Class clazz = Class.forName(type);

			Permission resolved = permission.resolve(clazz);

			if (resolved != null) {
				if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
					Debug.println("Created permission: " + resolved); //$NON-NLS-1$
				}

				return resolved;
			}
		} catch (ClassNotFoundException e) {
		}

		if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
			Debug.println("Created permission: " + permission); //$NON-NLS-1$
		}

		return permission;
	}
}

Back to the top