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