summaryrefslogtreecommitdiff
path: root/sysdeps/unix/sysv/solaris2/kopensolaris-gnu/priv.c
blob: 510bb8981c329b7dbc5c3a9e11f3615761695644 (plain)
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
/* Copyright (C) 2008 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by David Bartley <dtbartle@csclub.uwaterloo.ca>, 2008.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#include <bits/libc-lock.h>
#include <inline-syscall.h>
#include <privP.h>
#include <sys/priocntl.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>

DECLARE_INLINE_SYSCALL (int, privsys, int code, priv_op_t op,
    priv_ptype_t type, void *buf, size_t bufsize);

/* Docs: http://illumos.org/man/2/setppriv
         http://illumos.org/man/5/privileges */

__libc_lock_define_initialized_recursive (, __priv_lock);
libc_freeres_ptr (static priv_impl_info_t *__info);

/* Note: Almost everything that uses getprivimplinfo assumes it won't fail so
   we assert that this call succeeds.  */
const priv_impl_info_t * getprivimplinfo (void)
{
  if (__info)
    return __info;

  __libc_lock_lock_recursive (__priv_lock);

  /* First call: get header.  */
  priv_impl_info_t _info;
  int res = INLINE_SYSCALL (privsys, 5, SYS_SUB_getimplinfo, 0, 0, &_info,
      sizeof(_info));
  if (res == 0)
    {
      /* Second call: alloc and get full priv_impl_info_t.  */
      size_t info_size = PRIV_IMPL_INFO_SIZE (&_info);
      __info = malloc (info_size);
      assert (__info);
      res = INLINE_SYSCALL (privsys, 5, SYS_SUB_getimplinfo, 0, 0, __info,
          info_size);
      assert (res == 0);
    }

  __libc_lock_unlock_recursive (__priv_lock);
  return __info;
}


int getppriv (priv_ptype_t which, priv_set_t *set)
{
  int setn = priv_getsetbyname (which);
  if (setn == -1)
    return -1;

  return INLINE_SYSCALL (privsys, 5, SYS_SUB_getppriv, 0, (priv_ptype_t)setn,
      (void *)set, __PRIVSETSIZE);
}


int setppriv (priv_op_t op, priv_ptype_t which, const priv_set_t *set)
{
  int setn = priv_getsetbyname (which);
  if (setn == -1)
    return -1;

  return INLINE_SYSCALL (privsys, 5, SYS_SUB_setppriv, op, (priv_ptype_t)setn,
      (void *)set, __PRIVSETSIZE);
}


priv_set_t *priv_allocset (void)
{
  return malloc (__PRIVSETSIZE);
}


void priv_freeset (priv_set_t *sp)
{
  free (sp);
}


int priv_getbyname (const char *privname)
{
  const priv_data_t *pd = __priv_parse_data_cached ();
  if (!pd)
    return -1;

  for (uint32_t i = 0; i < pd->pd_privnames_cnt; i++)
    {
      if (strcasecmp (pd->pd_privnames[i], privname) == 0)
        return i;
    }

  __set_errno (EINVAL);
  return -1;
}


const char *priv_getbynum (int privnum)
{
  const priv_data_t *pd = __priv_parse_data_cached ();
  if (!pd)
    return NULL;

  if (privnum < 0 || privnum >= pd->pd_privnames_cnt)
    {
      __set_errno (EINVAL);
      return NULL;
    }

  return pd->pd_privnames[privnum];
}


int priv_getsetbyname (const char *privsetname)
{
  const priv_data_t *pd = __priv_parse_data_cached ();
  if (!pd)
    return -1;

  for (uint32_t i = 0; i < pd->pd_setnames_cnt; i++)
    {
      if (strcasecmp (pd->pd_setnames[i], privsetname) == 0)
        return i;
    }

  __set_errno (EINVAL);
  return -1;
}


const char *priv_getsetbynum (int privsetnum)
{
  const priv_data_t *pd = __priv_parse_data_cached ();
  if (!pd)
    return NULL;

  if (privsetnum < 0 || privsetnum >= pd->pd_setnames_cnt)
    {
      __set_errno (EINVAL);
      return NULL;
    }

  return pd->pd_setnames[privsetnum];
}


void priv_emptyset (priv_set_t *sp)
{
  memset (sp, 0, __PRIVSETSIZE);
}


void priv_fillset(priv_set_t *sp)
{
  memset (sp, ~0, __PRIVSETSIZE);
}


void priv_copyset (const priv_set_t *src, priv_set_t *dst)
{
  memcpy (dst, src, __PRIVSETSIZE);
}


int priv_addset (priv_set_t *sp, const char *priv)
{
  int privn = priv_getbyname (priv);
  if (privn == -1)
    return -1;

  ((priv_chunk_t *)sp)[__PRIVELT (privn)] |= __PRIVMASK (privn);
  return 0;
}


int priv_delset (priv_set_t *sp, const char *priv)
{
  int privn = priv_getbyname (priv);
  if (privn == -1)
    return -1;

  ((priv_chunk_t *)sp)[__PRIVELT (privn)] &= ~__PRIVMASK (privn);
  return 0;
}


void priv_intersect (const priv_set_t *src, priv_set_t *dst)
{
  priv_chunk_t *pcsrc = (priv_chunk_t *)src;
  priv_chunk_t *pcdst = (priv_chunk_t *)dst;
  for (int i = 0; i < __PRIVSETCHUNKS; i++)
    pcdst[__PRIVELT (i)] &= pcsrc[__PRIVELT (i)];
}


void priv_union (const priv_set_t *src, priv_set_t *dst)
{
  priv_chunk_t *pcsrc = (priv_chunk_t *)src;
  priv_chunk_t *pcdst = (priv_chunk_t *)dst;
  for (int i = 0; i < __PRIVSETCHUNKS; i++)
    pcdst[__PRIVELT (i)] |= pcsrc[__PRIVELT (i)];
}


void priv_inverse(priv_set_t *sp)
{
  priv_chunk_t *pcsp = (priv_chunk_t *)sp;
  for (int i = 0; i < __PRIVSETCHUNKS; i++)
    pcsp[i] = ~pcsp[i];
}


boolean_t priv_isemptyset (const priv_set_t *sp)
{
  priv_chunk_t *pcsp = (priv_chunk_t *)sp;
  for (int i = 0; i < __PRIVSETCHUNKS; i++)
    if (pcsp[i])
      return _B_FALSE;
  return _B_TRUE;
}


boolean_t priv_isfullset (const priv_set_t *sp)
{
  priv_chunk_t *pcsp = (priv_chunk_t *)sp;
  for (int i = 0; i < __PRIVSETCHUNKS; i++)
    if (~pcsp[i])
      return _B_FALSE;
  return _B_TRUE;
}


boolean_t priv_ismember (const priv_set_t *sp, const char *priv)
{
  int privn = priv_getbyname (priv);
  if (privn == -1)
    return _B_FALSE;

  return (((priv_chunk_t *)sp)[__PRIVELT (privn)] & __PRIVMASK (privn)) ?
      _B_TRUE : _B_FALSE;
}


boolean_t priv_issubset (const priv_set_t *src, const priv_set_t *dst)
{
  priv_chunk_t *pcsrc = (priv_chunk_t *)src;
  priv_chunk_t *pcdst = (priv_chunk_t *)dst;
  for (int i = 0; i < __PRIVSETCHUNKS; i++)
    if ((pcsrc[__PRIVELT (i)] & pcdst[__PRIVELT (i)]) != pcsrc[__PRIVELT (i)])
      return _B_FALSE;
  return _B_TRUE;
}


int priv_set (priv_op_t op, priv_ptype_t which, ...)
{
  va_list ap;
  va_start (ap, which);

  priv_set_t *pset = priv_allocset ();
  if (!pset)
    return -1;

  const char *priv;
  while ((priv = va_arg (ap, const char *)))
    {
      if (priv_addset (pset, priv) == -1)
        return -1;
    }

  int ret;
  if (which == NULL)
    {
      /* Set all sets.  */
      for (int i = 0; i < __PRIVSETCHUNKS; i++)
        {
          ret = setppriv (op, which, pset);
          if (ret == -1)
            break;
        }
    }
  else
    {
      ret = setppriv (op, which, pset);
    }

  priv_freeset (pset);
  return 0;
}

boolean_t priv_ineffect (const char *priv)
{
  priv_set_t *pset = priv_allocset ();
  if (!pset)
    return _B_FALSE;

  int res = getppriv (PRIV_EFFECTIVE, pset);
  if (res == -1)
    return _B_FALSE;

  boolean_t ret = priv_ismember (pset, priv);

  priv_freeset (pset);

  return ret;
}