blob: 5b1d14dc2a9aacfd88c0b8b54b36c453a2e9a314 [file] [log] [blame]
The Android Open Source Project23580ca2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AMEND_PERMISSIONS_H_
18#define AMEND_PERMISSIONS_H_
19
20#include <stdbool.h>
21
22#define PERM_NONE (0)
23#define PERM_STAT (1<<0)
24#define PERM_READ (1<<1)
25#define PERM_WRITE (1<<2) // including create, delete, mkdir, rmdir
26#define PERM_CHMOD (1<<3)
27#define PERM_CHOWN (1<<4)
28#define PERM_CHGRP (1<<5)
29#define PERM_SETUID (1<<6)
30#define PERM_SETGID (1<<7)
31
32#define PERMSET_READ (PERM_STAT | PERM_READ)
33#define PERMSET_WRITE (PERMSET_READ | PERM_WRITE)
34
35#define PERMSET_ALL \
36 (PERM_STAT | PERM_READ | PERM_WRITE | PERM_CHMOD | \
37 PERM_CHOWN | PERM_CHGRP | PERM_SETUID | PERM_SETGID)
38
39typedef struct {
40 unsigned int requested;
41 unsigned int allowed;
42 const char *path;
43 bool recursive;
44} PermissionRequest;
45
46typedef struct {
47 PermissionRequest *requests;
48 int numRequests;
49 int requestsAllocated;
50} PermissionRequestList;
51
52/* Properly clear out a PermissionRequestList.
53 *
54 * @return 0 if list is non-NULL, negative otherwise.
55 */
56int initPermissionRequestList(PermissionRequestList *list);
57
58/* Add a permission request to the list, allocating more space
59 * if necessary.
60 *
61 * @return 0 on success or a negative value on failure.
62 */
63int addPermissionRequestToList(PermissionRequestList *list,
64 const char *path, bool recursive, unsigned int permissions);
65
66/* Free anything allocated by addPermissionRequestToList(). The caller
67 * is responsible for freeing the actual PermissionRequestList.
68 */
69void freePermissionRequestListElements(PermissionRequestList *list);
70
71
72/*
73 * Global permission table
74 */
75
76typedef struct {
77 const char *path;
78 unsigned int allowed;
79} Permission;
80
81int permissionInit(void);
82void permissionCleanup(void);
83
84/* Returns the allowed permissions for the path in "outAllowed".
85 * Returns 0 if successful, negative if a parameter or global state
86 * is bad.
87 */
88int getAllowedPermissions(const char *path, bool recursive,
89 unsigned int *outAllowed);
90
91/* More-recently-registered permissions override older permissions.
92 */
93int registerPermissionSet(int count, Permission *set);
94
95/* Check to make sure that each request is allowed.
96 *
97 * @param requests The list of permission requests
98 * @param updateAllowed If true, update the "allowed" field in each
99 * element of the list
100 * @return the number of requests that were denied, or negative if
101 * an error occurred.
102 */
103int countPermissionConflicts(PermissionRequestList *requests,
104 bool updateAllowed);
105
106/* Inspection/testing/debugging functions
107 */
108int getPermissionCount(void);
109const Permission *getPermissionAt(int index);
110
111#endif // AMEND_PERMISSIONS_H_