Imported Upstream version 4.5.10
[platform/upstream/findutils.git] / tests / priv-set.c
1 /* Query, remove, or restore a Solaris privilege.
2
3    Copyright (C) 2009-2011 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18    Written by David Bartley.  */
19
20 #include <config.h>
21 #include "priv-set.h"
22
23 #if HAVE_GETPPRIV && HAVE_PRIV_H
24
25 # include <errno.h>
26 # include <stdbool.h>
27 # include <priv.h>
28
29 /* Holds a (cached) copy of the effective set.  */
30 static priv_set_t *eff_set;
31
32 /* Holds a set of privileges that we have removed.  */
33 static priv_set_t *rem_set;
34
35 static bool initialized;
36
37 static int
38 priv_set_initialize (void)
39 {
40   if (! initialized)
41     {
42       eff_set = priv_allocset ();
43       if (!eff_set)
44         {
45           return -1;
46         }
47       rem_set = priv_allocset ();
48       if (!rem_set)
49         {
50           priv_freeset (eff_set);
51           return -1;
52         }
53       if (getppriv (PRIV_EFFECTIVE, eff_set) != 0)
54         {
55           priv_freeset (eff_set);
56           priv_freeset (rem_set);
57           return -1;
58         }
59       priv_emptyset (rem_set);
60       initialized = true;
61     }
62
63   return 0;
64 }
65
66
67 /* Check if priv is in the effective set.
68    Returns 1 if priv is a member and 0 if not.
69    Returns -1 on error with errno set appropriately.  */
70 int
71 priv_set_ismember (const char *priv)
72 {
73   if (! initialized && priv_set_initialize () != 0)
74     return -1;
75
76   return priv_ismember (eff_set, priv);
77 }
78
79
80 /* Try to remove priv from the effective set.
81    Returns 0 if priv was removed.
82    Returns -1 on error with errno set appropriately.  */
83 int
84 priv_set_remove (const char *priv)
85 {
86   if (! initialized && priv_set_initialize () != 0)
87     return -1;
88
89   if (priv_ismember (eff_set, priv))
90     {
91       /* priv_addset/priv_delset can only fail if priv is invalid, which is
92          checked above by the priv_ismember call.  */
93       priv_delset (eff_set, priv);
94       if (setppriv (PRIV_SET, PRIV_EFFECTIVE, eff_set) != 0)
95         {
96           priv_addset (eff_set, priv);
97           return -1;
98         }
99       priv_addset (rem_set, priv);
100     }
101   else
102     {
103       errno = EINVAL;
104       return -1;
105     }
106
107   return 0;
108 }
109
110
111 /* Try to restore priv to the effective set.
112    Returns 0 if priv was re-added to the effective set (after being previously
113    removed by a call to priv_set_remove).
114    Returns -1 on error with errno set appropriately.  */
115 int
116 priv_set_restore (const char *priv)
117 {
118   if (! initialized && priv_set_initialize () != 0)
119     return -1;
120
121   if (priv_ismember (rem_set, priv))
122     {
123       /* priv_addset/priv_delset can only fail if priv is invalid, which is
124          checked above by the priv_ismember call.  */
125       priv_addset (eff_set, priv);
126       if (setppriv (PRIV_SET, PRIV_EFFECTIVE, eff_set) != 0)
127         {
128           priv_delset (eff_set, priv);
129           return -1;
130         }
131       priv_delset (rem_set, priv);
132     }
133   else
134     {
135       errno = EINVAL;
136       return -1;
137     }
138
139   return 0;
140 }
141
142 #endif