summaryrefslogtreecommitdiff
path: root/libshouldbeinlibc/idvec.c
blob: c60fc9fb0ff35e23877af21f6cd8e17cc27eb72c (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Routines for vectors of uids/gids

   Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

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

   This program 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <malloc.h>
#include <string.h>

#include "idvec.h"

/* Return a new, empty, idvec, or NULL if there wasn't enough memory.  */
struct idvec *
make_idvec ()
{
  struct idvec *idvec = malloc (sizeof (struct idvec));
  if (idvec)
    {
      idvec->alloced = idvec->num = 0;
      idvec->ids = 0;
    }
  return idvec;
}

/* Free's IDVEC, but not the storage pointed to by the IDS field.  */
void
idvec_free_wrapper (struct idvec *idvec)
{
  free (idvec);
}

void
idvec_free_contents (struct idvec *idvec)
{
  if (idvec->alloced)
    free (idvec->ids);
}

void
idvec_free (struct idvec *idvec)
{
  idvec_free_contents (idvec);
  idvec_free_wrapper (idvec);
}

/* Ensure that IDVEC has enough spaced allocated to hold NUM ids, thus
   ensuring that any subsequent ids added won't return a memory allocation
   error unless it would result in more ids that NUM.  ENOMEM is returned if
   a memory allocation error occurs.  */
error_t
idvec_ensure (struct idvec *idvec, unsigned num)
{
  if (num > idvec->alloced)
    {
      uid_t *_ids = realloc (idvec->ids, num * sizeof (uid_t));
      if (! _ids)
	return ENOMEM;
      idvec->ids = _ids;
      idvec->alloced = num;
    }
  return 0;
}

/* Like idvec_ensure(), but takes INC, the increment of the number of ids
   already in IDVEC as an argument.  */
error_t
idvec_grow (struct idvec *idvec, unsigned inc)
{
  return idvec_ensure (idvec, idvec->num + inc);
}

/* Returns true if IDVEC contains ID, at or after position POS.  */
int
idvec_tail_contains (const struct idvec *idvec, unsigned pos, uid_t id)
{
  uid_t *ids = idvec->ids, *end = ids + idvec->num, *p = ids + pos;
  while (p < end)
    if (*p++ == id)
      return  1;
  return 0;
}

/* Insert ID into IDVEC at position POS, returning ENOMEM if there wasn't
   enough memory, or 0.  */
error_t
idvec_insert (struct idvec *idvec, unsigned pos, uid_t id)
{
  error_t err = 0;
  unsigned num = idvec->num;
  unsigned new_num = (pos < num ? num + 1 : pos + 1);

  if (idvec->alloced == num)
    /* If we seem to be growing by just one, actually prealloc some more. */
    err = idvec_ensure (idvec, new_num + num);
  else
    err = idvec_ensure (idvec, new_num);

  if (! err)
    {
      uid_t *ids = idvec->ids;
      if (pos < num)
	bcopy (ids + pos, ids + pos + 1, (num - pos) * sizeof (uid_t));
      else if (pos > num)
	memset (ids + num, 0, (pos - num) * sizeof(uid_t));
      ids[pos] = id;
      idvec->num = new_num;
    }

  return err;
}

/* Add ID onto the end of IDVEC, returning ENOMEM if there's not enough memory,
   or 0.  */
error_t
idvec_add (struct idvec *idvec, uid_t id)
{
  return idvec_insert (idvec, idvec->num, id);
}

/* If IDVEC doesn't contain ID, add it onto the end, returning ENOMEM if
   there's not enough memory; otherwise, do nothing.  */
error_t
idvec_add_new (struct idvec *idvec, uid_t id)
{
  if (idvec_contains (idvec, id))
    return 0;
  else
    return idvec_add (idvec, id);
}

/* If IDVEC doesn't contain ID at position POS or after, insert it at POS,
   returning ENOMEM if there's not enough memory; otherwise, do nothing.  */
error_t
idvec_insert_new (struct idvec *idvec, unsigned pos, uid_t id)
{
  if (idvec_tail_contains (idvec, pos, id))
    return 0;
  else
    return idvec_insert (idvec, pos, id);
}

/* Set the ids in IDVEC to IDS (NUM elements long); delete whatever
   the previous ids were. */
error_t
idvec_set_ids (struct idvec *idvec, const uid_t *ids, unsigned num)
{
  error_t err;

  err = idvec_ensure (idvec, num);
  if (!err)
    {
      bcopy (ids, idvec->ids, num * sizeof (uid_t));
      idvec->num = num;
    }
  return err;
}

/* Like idvec_set_ids, but get the new ids from new. */
error_t
idvec_set (struct idvec *idvec, const struct idvec *new)
{
  return idvec_set_ids (idvec, new->ids, new->num);
}

/* Adds each id in the vector IDS (NUM elements long) to IDVEC, as long as it
   wasn't previously in IDVEC.  */
error_t
idvec_merge_ids (struct idvec *idvec, const uid_t *ids, unsigned num)
{
  error_t err = 0;
  unsigned num_old = idvec->num;
  while (num-- > 0 && !err)
    {
      unsigned int i;
      for (i = 0; i < num_old; i++)
	if (idvec->ids[i] == *ids)
	  break;
      if (i == num_old)
	err = idvec_add (idvec, *ids);
      ids++;
    }
  return err;
}

/* Adds each id from  NEW to IDVEC, as if with idvec_add_new().  */
error_t
idvec_merge (struct idvec *idvec, const struct idvec *new)
{
  return idvec_merge_ids (idvec, new->ids, new->num);
}

/* Remove any occurrences of ID in IDVEC after position POS.
   Returns true if anything was done.  */
int
idvec_remove (struct idvec *idvec, unsigned pos, uid_t id)
{
  if (pos < idvec->num)
    {
      int left = idvec->num - pos;
      uid_t *ids = idvec->ids + pos, *targ = ids;
      while (left--)
	{
	  if (*ids != id)
	    {
	      if (ids != targ)
		*targ = *ids;
	      targ++;
	    }
	  ids++;
	}
      if (ids == targ)
	return 0;
      idvec->num = targ - idvec->ids;
      return 1;
    }
  else
    return 0;
}

/* Remove all ids in SUB from IDVEC, returning true if anything was done. */
int
idvec_subtract (struct idvec *idvec, const struct idvec *sub)
{
  unsigned int i;
  int done = 0;
  for (i = 0; i < sub->num; i++)
    done |= idvec_remove (idvec, 0, sub->ids[i]);
  return done;
}

/* Remove all ids from IDVEC that are *not* in KEEP, returning true if
   anything was changed. */
int
idvec_keep (struct idvec *idvec, const struct idvec *keep)
{
  uid_t *old = idvec->ids, *new = old, *end = old + idvec->num;

  while (old < end)
    {
      uid_t id = *old++;
      if (idvec_contains (keep, id))
	{
	  if (old != new)
	    *new = id;
	  new++;
	}
    }

  if (old != new)
    {
      idvec->num = new - idvec->ids;
      return 1;
    }
  else
    return 0;
}

/* Deleted the id at position POS in IDVEC.  */
void
idvec_delete (struct idvec *idvec, unsigned pos)
{
  unsigned num = idvec->num;
  if (pos < num)
    {
      uid_t *ids = idvec->ids;
      idvec->num = --num;
      if (num > pos)
	bcopy (ids + pos + 1, ids + pos, (num - pos) * sizeof (uid_t));
    }
}

/* Insert ID at position POS in IDVEC, remove any instances of ID previously
   present at POS or after.  ENOMEM is returned if there's not enough memory,
   otherwise 0.  */
error_t
idvec_insert_only (struct idvec *idvec, unsigned pos, uid_t id)
{
  if (idvec->num > pos && idvec->ids[pos] == id)
    return 0;
  else
    {
      idvec_remove (idvec, pos, id);
      return idvec_insert (idvec, pos, id);
    }
}

/* EFF and AVAIL should be idvec's corresponding to a processes
   effective and available ids.  ID replaces the first id in EFF, and,
   if there are any IDs in AVAIL, replaces the second ID in AVAIL;
   what it replaces in any case is preserved by adding it to AVAIL if
   not already present.  In addition, the If SECURE is non-NULL, and
   ID was not previously present in either EFF or AVAIL, then *SECURE
   is set to true.  ENOMEM is returned if a malloc fails, otherwise 0.
   The return parameters are only touched if this call succeeds.  */
error_t
idvec_setid (struct idvec *eff, struct idvec *avail, uid_t id, int *secure)
{
  error_t err;
  /* True if ID was not previously present in either EFF or AVAIL.  */
  int _secure = !idvec_contains (eff, id) &&  !idvec_contains (avail, id);

  if (eff->num > 0)
    {
      /* If there are any old effective ids, we replace eff[0] with
	 ID, and try to preserve the old eff[0] by putting it in AVAIL
	 list if necessary.  */
      err = idvec_add_new (avail, eff->ids[0]);
      if (!err)
	eff->ids[0] = id;
    }
  else
    /* No previous effective ids, just make ID the first one.  */
    err = idvec_add (eff, id);

  if (avail->num > 0 && !err)
    err = idvec_insert_only (avail, 1, id);
  
  if (err)
    return err;

  if (_secure && secure && !*secure)
    *secure = 1;

  return 0;
}