File: safe_id_range_list.c

package info (click to toggle)
myproxy 6.1.22-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,628 kB
  • ctags: 1,812
  • sloc: ansic: 25,183; sh: 11,726; perl: 3,673; makefile: 361
file content (919 lines) | stat: -rw-r--r-- 23,623 bytes parent folder | download | duplicates (7)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/*
 * safefile package    http://www.cs.wisc.edu/~kupsch/safefile
 *
 * Copyright 2007-2008 James A. Kupsch
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <dirent.h>
#include <signal.h>

#include <pwd.h>
#include <grp.h>

#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>

#include "safe_id_range_list.h"



/***********************************************************************
 *
 * Initialize global variables
 *
 ***********************************************************************/

const id_t safe_err_id	= (id_t)-1;


/***********************************************************************
 *
 * Initialize global variables
 *
 ***********************************************************************/

typedef struct safe_id_range_list_elem
{
    id_t min_value;
    id_t max_value;
}  safe_id_range_list_elem;




/***********************************************************************
 *
 * Functions for manipulating id range lists
 *
 ***********************************************************************/


/*
 * safe_init_id_range_list
 * 	Initialize an id_range_list structure.
 * parameters
 * 	list
 * 		pointer to an id range list
 * returns
 * 	0   for success
 * 	-1  on failure (errno == ENOMEM or EINVAL)
 */
int safe_init_id_range_list(safe_id_range_list *list)
{
    if (list == NULL)  {
	errno = EINVAL;
	return -1;
    }

    list->count = 0;
    list->capacity = 10;
    list->list = (safe_id_range_list_elem*)malloc(list->capacity * sizeof(list->list[0]));
    if (list->list == 0)  {
	errno = ENOMEM;
	return -1;
    }

    return 0;
}


/*
 * safe_add_id_range_to_list
 * 	Adds a range of ids (all the ids between min_id and max_id inclusive)
 * 	to the id_range_list.
 * parameters
 * 	list
 * 		pointer to an id range list
 * 	min_id
 * 		the minimum id of the range to add
 * 	max_id
 * 		the maximum id of the range to add
 * returns
 * 	0   for success
 * 	-1  on failure (errno == ENOMEM or EINVAL)
 */
int safe_add_id_range_to_list(safe_id_range_list *list, id_t min_id, id_t max_id)
{
    if (list == NULL || min_id > max_id)  {
	errno = EINVAL;
	return -1;
    }

    if (list->count == list->capacity)  {
	size_t new_capacity = 10 + 11 * list->capacity / 10;
	safe_id_range_list_elem *new_list = (safe_id_range_list_elem*)malloc(new_capacity * sizeof(new_list[0]));
	if (new_list == 0)  {
	    errno = ENOMEM;
	    return -1;
	}
	memcpy(new_list, list->list, list->count * sizeof(new_list[0]));
	free(list->list);
	list->list = new_list;
	list->capacity = new_capacity;
    }

    list->list[list->count].min_value = min_id;
    list->list[list->count++].max_value = max_id;

    return 0;
}


/*
 * safe_add_id_to_list
 * 	Add the single id to the list.  This is the same as calling
 * 	safe_add_id_range_to_list with min_id and max_id set to id.
 * parameters
 * 	list
 * 		pointer to an id range list
 * 	id
 * 		the id to add
 * returns
 * 	0   for success
 * 	-1  on failure (errno == ENOMEM)
 */
int safe_add_id_to_list(safe_id_range_list *list, id_t id)
{
    return safe_add_id_range_to_list(list, id, id);
}


/*
 * safe_destroy_id_range_list
 * 	Destroy a id_range_list, including any memory have acquired.
 * parameters
 * 	list
 * 		pointer to id range list structure to destroy
 * returns
 * 	nothing
 */
void safe_destroy_id_range_list(safe_id_range_list *list)
{
    if (list == NULL)  {
	errno = EINVAL;
	return;
    }

    list->capacity = 0;
    list->count = 0;
    free(list->list);
    list->list = 0;
}


/*
 * safe_is_id_in_list
 * 	Check if the id is in one of the id ranges in the id range list.
 * parameters
 * 	list
 * 		pointer to an id range list
 * 	id
 * 		the id to check
 * returns
 * 	1	id is in the list
 * 	0	id is not in the list
 * 	-1	the list is NULL
 */
int safe_is_id_in_list(safe_id_range_list *list, id_t id)
{
    size_t i;

    if (list == NULL)  {
	errno = EINVAL;
	return -1;
    }

    for (i = 0; i < list->count; ++i)  {
	if (list->list[i].min_value <= id && id <= list->list[i].max_value)  {
	    return 1;
	}
    }

    return 0;
}


/*
 * safe_is_id_list_empty
 * 	Returns true if the id_range_list contains 0 ranges.
 * parameters
 * 	list
 * 		pointer to an id range list
 * returns
 * 	1	id is in the list
 * 	0	id is not in the list
 * 	-1	the list is NULL
 */
int safe_is_id_list_empty(safe_id_range_list *list)  {
    if (list == NULL)  {
	errno = EINVAL;
	return -1;
    }

    return (list->count == 0);
}





/***********************************************************************
 *
 * Functions for parsing ids, id ranges and id lists of numbers, uids and gids
 *
 ***********************************************************************/


/*
 * skip_whitespace_const
 * 	Returns a pointer to the first non-whitespace character in the
 * 	const string s.
 * parameters
 * 	s
 * 		the string to skip whitespace
 * returns
 * 	location of first non-whitespace
 */
static const char *skip_whitespace_const(const char *s)
{
    while (*s && isspace((unsigned char)*s))  {
	++s;
    }

    return s;
}


/*
 * name_to_error
 * 	Always return the err id (-1) and set errno to EINVAL.
 * parameters
 * 	name
 * 		unused
 * returns
 * 	safe_err_id and errno = EINVAL
 */
static id_t name_to_error(const char *name)
{
    (void)name;
    errno = EINVAL;
    return safe_err_id;
}


/*
 * name_to_uid
 * 	Return the uid matching the name if it exists.  If the name does
 * 	not exist or there was an error in getpwnam, safe_err_id is
 * 	returned and errno is set to a non-0 value.  If getpwnam fails
 * 	with errno unchanged, errno is set to EINVAL.
 * parameters
 * 	name
 * 		user name to lookup
 * returns
 * 	The uid corresponding to name if it exists.
 * 	safe_err_id if the name does not exist or an error occurs (errno is
 * 	set to the value from getpwnam or EINVAL if getpwnam does not set it).
 */
static id_t name_to_uid(const char *name)
{
    struct passwd	*pw	= getpwnam(name);

    errno = 0;

    if (!pw)  {
	if (errno == 0)  {
	    errno = EINVAL;
	}
	return safe_err_id;
    }

    return pw->pw_uid;
}


/*
 * name_to_gid
 * 	Return the uid matching the name if it exists.  If the name does
 * 	not exist or there was an error in getgrnam, safe_err_id is
 * 	returned and errno is set to a non-0 value.  If getgrnam fails
 * 	with errno unchanged, errno is set to EINVAL.
 * parameters
 * 	name
 * 		group name to lookup
 * returns
 * 	The uid corresponding to name if it exists.
 * 	safe_err_id if the name does not exist or an error occurs (errno is
 * 	set to the value from getgrnam or EINVAL if getgrnam does not set it).
 */
static id_t name_to_gid(const char *name)
{
    struct group	*gr	= getgrnam(name);

    errno = 0;

    if (!gr)  {
	if (errno == 0)  {
	    errno = EINVAL;
	}
	return safe_err_id;
    }

    return gr->gr_gid;
}


/*
 * strto_id
 * 	Return the id corresponding to the longest sequence of characters
 * 	that could form an unsigned integer or a name after skipping leading
 * 	whitespace.  An unsigned integer begins with 0-9 and continues until a
 * 	non-digit character.  A name starts with a non-digit character and
 * 	continues until a whitespace, end of string or a colon.
 *
 * 	The numeric form is converted to an unsigned integer and the name
 * 	form is converted to an unsigned integer using the lookup func.
 *
 * 	If endptr is not NULL *endptr is set to point to the character one past
 * 	the end of the parsed value, just as strtoul() does.
 *
 * 	If there is nothing valid to try to convert to an id_t, then *endptr is
 * 	set to value.  This can only occur if value consists only of whitespace
 * 	characters.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	id
 * 		a pointer to store the id converted to a id_t
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * 	endptr
 * 		a pointer to one character past the end the sequence used to,
 * 		or NULL if the value is not needed
 * 	lookup
 * 		function pointer to convert a name to an id
 * returns
 * 	nothing, but sets *id, *endptr, and errno
 */
typedef id_t (*lookup_func)(const char*);

static int strto_id(id_t *id, const char *value, const char **endptr, lookup_func lookup)
{
    const char	*endp;
    const char	*id_begin;

    if (id == NULL || value == NULL || lookup == NULL)  {
	errno = EINVAL;
	if (id)  {
	    *id = safe_err_id;
	}
	return -1;
    }

    endp = value;

    id_begin = skip_whitespace_const(value);

    errno = 0;

    if (isdigit((unsigned char)*id_begin))  {
	/* is numeric form, parse as a number */
	char *e;
	*id = strtoul(id_begin, &e, 10);
	endp = e;
    }  else if (*id_begin)  {
	/* is not numeric, parse as a name using lookup function */
	char *id_name;
	size_t id_len;
	char small_buf[16];	/* should be big enough to hold most names */

	/* find end - name can contain anything except whitespace and colons */
	endp = id_begin;
	while (*endp && !isspace((unsigned char)*endp) && *endp != ':')  {
	    ++endp;
	}

	id_len = (size_t)(endp - id_begin);

	if (id_len == 0)  {
	    errno = EINVAL;
	    *id = safe_err_id;
	    if (endptr)  {
		*endptr = endp;
	    }
	    return -1;
	}  else if (id_len < sizeof(small_buf))  {
	    /* use small_buf as the id fits */
	    id_name = small_buf;
	}  else  {
	    /* malloc a buffer as id is too large for small_buf */
	    id_name = (char*)malloc(id_len + 1);
	    if (id_name == NULL)  {
		errno = ENOMEM;
		*id = safe_err_id;
		if (endptr)  {
		    *endptr = endp;
		}
		return -1;
	    }
	}

	/* copy the id to the buffer */
	memcpy(id_name, id_begin, id_len);
	id_name[id_len] = '\0';

	*id = lookup(id_name);

	/* free buffer if malloc'd */
	if (id_name != small_buf)  {
	    free(id_name);
	}
    }  else  {
	/* value contains nothing parsable */
	*id = safe_err_id;
	errno = EINVAL;
    }

    if (endptr)  {
	*endptr = endp;
    }

    return 0;
}


/*
 * safe_strto_uid
 * 	Parse the string value and return the user id of the first id in the
 * 	string as a uid_t.  This follows the same rules as strto_id with
 * 	non-numeric ids converted to the matching user id.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * 	endptr
 * 		a pointer to one character past the end the sequence used to,
 * 		or NULL if the value is not needed
 * returns
 * 	the user id, also updates *endptr and errno
 */
uid_t safe_strto_uid(const char *value, const char **endptr)
{
    id_t	id;

    strto_id(&id, value, endptr, name_to_uid);

    return (uid_t)id;
}


/*
 * safe_strto_gid
 * 	Parse the string value and return the group id of the first id in the
 * 	string as gid_t.  This follows the same rules as strto_id with
 * 	non-numeric ids converted to the matching group id.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * 	endptr
 * 		a pointer to one character past the end the sequence used to,
 * 		or NULL if the value is not needed
 * returns
 * 	the group id, also updates *endptr and errno
 */
gid_t safe_strto_gid(const char *value, const char **endptr)
{
    id_t	id;

    strto_id(&id, value, endptr, name_to_gid);

    return (gid_t)id;
}


/*
 * safe_strto_id
 * 	Parse the string value and return the the first number in the string as
 * 	an id_t.  This follows the same rules as strto_id with non-numeric ids
 * 	returning an error.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * 	endptr
 * 		a pointer to one character past the end the sequence used to,
 * 		or NULL if the value is not needed
 * returns
 * 	the group id, also updates *endptr and errno
 */
id_t safe_strto_id(const char *value, const char **endptr)
{
    id_t	id;

    strto_id(&id, value, endptr, name_to_error);

    return id;
}


/*
 * strto_id_range
 * 	Returns a pair of id's denoting a range of ids.  The form of the string
 * 	must be     <SP>* <id> [ <SP>* '-' <SP>* ( <id> | '*' ) ]?
 *
 * 	<id> is of the form parsed by strto_id.  If the option '-' and second
 * 	<id> is not present, the first <id> is returned for both the minimum
 * 	and maximum value.  Since an <id> in a non-numeric form may contain a
 * 	'-', a space must preceed the '-' if the first <id> is in a non-numeric
 * 	form.  The value '*' as the second value specifies the maximum allowed
 * 	id (assumes id_t is an unsigned type, if it is unsigned the code will
 * 	work correctly, but '*' will not work.
 *
 * 	It is an error if min_id is greater than max_id.
 *
 * 	If endptr is not NULL *endptr is set to point to the character one past
 * 	the end of the parsed value, just as strtoul() does.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	min_id
 * 		a pointer to store the minimum id converted to a id_t
 * 	max_id
 * 		a pointer to store the maximum id converted to a id_t
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * 	endptr
 * 		a pointer to one character past the end the sequence used to,
 * 		or NULL if the value is not needed
 * 	lookup
 * 		function pointer to convert a name to an id
 * returns
 * 	nothing, but sets *min_id, *max_id, *endptr, and errno
 */
static void strto_id_range(id_t *min_id, id_t *max_id, const char *value, const char **endptr, lookup_func lookup)
{
    const char *endp;
    strto_id(min_id, value, &endp, lookup);
    if (errno == 0 && value != endp)  {
	/* parsed min correctly, check for a '-' and max value */
	value = skip_whitespace_const(endp);
	if (*value == '-')  {
	    ++value;
	    value = skip_whitespace_const(value);
	    if (*value == '*')  {
		*max_id = UINT_MAX;
		endp = value + 1;
	    }  else  {
		strto_id(max_id, value, &endp, lookup);
	    }
	}  else  {
	    *max_id = *min_id;
	}
    }  else  {
	*max_id = *min_id;
    }

    if (endptr)  {
	*endptr = endp;
    }

    if (*min_id > *max_id)  {
	errno = EINVAL;
    }
}


/*
 * strto_id_list
 * 	Adds the rnages in the value to the list.  Ranges are as specified in
 * 	strto_id_range, and there may be multiple ranges in value that are
 * 	separated by whitespace and a colon of the form:
 * 		<ID_RANGE> [ <SP>* ':' <SP>* <ID_RANGE> ]*
 *
 * 	Each range is of the form required by strto_id_range.
 *
 * 	It is an error if any of the ranges contain an error.
 *
 * 	If endptr is not NULL *endptr is set to point to the character one past
 * 	the end of the parsed value, just as strtoul() does.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	list
 * 		a pointer to a range list to have the ranges parsed added
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * 	endptr
 * 		a pointer to one character past the end the sequence used to,
 * 		or NULL if the value is not needed
 * 	lookup
 * 		function pointer to convert a name to an id
 * returns
 * 	nothing, but adds entries to *list, and sets *endptr, and errno
 */
static void strto_id_list(safe_id_range_list *list, const char *value, const char **endptr, lookup_func lookup)
{
    const char * endp = value;

    if (list == NULL || value == NULL)  {
	errno = EINVAL;
	if (endptr)  {
	    *endptr = value;
	}
	return;
    }

    while (1)  {
	id_t	min_id;
	id_t	max_id;

	strto_id_range(&min_id, &max_id, value, &endp, lookup);
	if (errno != 0 || value == endp)  {
	    break;
	}

	safe_add_id_range_to_list(list, min_id, max_id);

	value = skip_whitespace_const(endp);
	if (*value == ':')  {
	    ++value;
	}  else  {
	    break;
	}
    }

    if (endptr)  {
	*endptr = endp;
    }
}


/*
 * safe_strto_id_list
 * 	Parse the value and store the ranges in the range list in the list
 * 	structure.  Non-numeric ids are treated as errors.
 *
 * 	The value is parsed as described in strto_id_list.
 *
 * 	It is an error if any of the ranges contain an error.
 *
 * 	If endptr is not NULL *endptr is set to point to the character one past
 * 	the end of the parsed value, just as strtoul() does.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	list
 * 		a pointer to a range list to have the ranges parsed added
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * 	endptr
 * 		a pointer to one character past the end the sequence used to,
 * 		or NULL if the value is not needed
 * returns
 * 	nothing, but adds entries to *list, and sets *endptr, and errno
 */
void safe_strto_id_list(safe_id_range_list *list, const char *value, const char **endptr)
{
    strto_id_list(list, value, endptr, name_to_error);
}


/*
 * safe_parse_uid_list
 * 	Parse the value and store the ranges in the range list in the list
 * 	structure.  Non-numeric ids are converted to ids by looking the
 * 	name as a username and returning its uid.
 *
 * 	The value is parsed as described in strto_id_list.
 *
 * 	It is an error if any of the ranges contain an error.
 *
 * 	If endptr is not NULL *endptr is set to point to the character one past
 * 	the end of the parsed value, just as strtoul() does.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	list
 * 		a pointer to a range list to have the ranges parsed added
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * 	endptr
 * 		a pointer to one character past the end the sequence used to,
 * 		or NULL if the value is not needed
 * returns
 * 	nothing, but adds entries to *list, and sets *endptr, and errno
 */
void safe_strto_uid_list(safe_id_range_list *list, const char *value, const char **endptr)
{
    strto_id_list(list, value, endptr, name_to_uid);
}


/*
 * safe_parse_gid_list
 * 	Parse the value and store the ranges in the range list in the list
 * 	structure.  Non-numeric ids are converted to ids by looking the
 * 	name as a group name and returning its gid.
 *
 * 	The value is parsed as described in strto_id_list.
 *
 * 	It is an error if any of the ranges contain an error.
 *
 * 	If endptr is not NULL *endptr is set to point to the character one past
 * 	the end of the parsed value, just as strtoul() does.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	list
 * 		a pointer to a range list to have the ranges parsed added
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * 	endptr
 * 		a pointer to one character past the end the sequence used to,
 * 		or NULL if the value is not needed
 * returns
 * 	nothing, but adds entries to *list, and sets *endptr, and errno
 */
void safe_strto_gid_list(safe_id_range_list *list, const char *value, const char **endptr)
{
    strto_id_list(list, value, endptr, name_to_gid);
}


/*
 * parse_id_list
 * 	Parse the value and store the ranges in the range list in the list
 * 	structure.  Non-numeric ids are converted to ids by looking the
 * 	name as a group name and returning its gid.
 *
 * 	The value is parsed as described in strto_id_list.
 *
 * 	It is an error if any of the ranges contain an error.
 *
 * 	It is an error if there is non-whitespace after the parsed value.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	list
 * 		a pointer to a range list to have the ranges parsed added
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * returns
 * 	0       on success
 * 	-1	on error
 */
static int parse_id_list(safe_id_range_list *list, const char *value, lookup_func lookup)
{
    const char *endp;

    strto_id_list(list, value, &endp, lookup);

    if (errno != 0)  {
	return -1;
    }

    /* check if there is non-whitespace after the parse portion of value */
    endp = skip_whitespace_const(endp);
    if (*endp != '\0')  {
	return -1;
    }

    return 0;
}


/*
 * safe_parse_id_list
 * 	Parse the value and store the ranges in the range list in the list
 * 	structure.
 *
 * 	The value is parsed as described in strto_id_list.
 *
 * 	It is an error if any of the ranges contain an error.
 *
 * 	It is an error if there is non-whitespace after the parsed value.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	list
 * 		a pointer to a range list to have the ranges parsed added
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * returns
 * 	0       on success
 * 	-1	on error
 */
int safe_parse_id_list(safe_id_range_list *list, const char *value)
{
    return parse_id_list(list, value, name_to_error);
}


/*
 * safe_parse_uid_list
 * 	Parse the value and store the ranges in the range list in the list
 * 	structure.  Non-numeric ids are converted to ids by looking the
 * 	name as a user name and returning its uid.
 *
 * 	The value is parsed as described in strto_id_list.
 *
 * 	It is an error if any of the ranges contain an error.
 *
 * 	It is an error if there is non-whitespace after the parsed value.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	list
 * 		a pointer to a range list to have the ranges parsed added
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * returns
 * 	0       on success
 * 	-1	on error
 */
int safe_parse_uid_list(safe_id_range_list *list, const char *value)
{
    return parse_id_list(list, value, name_to_uid);
}


/*
 * safe_parse_gid_list
 * 	Parse the value and store the ranges in the range list in the list
 * 	structure.  Non-numeric ids are converted to ids by looking the
 * 	name as a group name and returning its gid.
 *
 * 	The value is parsed as described in strto_id_list.
 *
 * 	It is an error if any of the ranges contain an error.
 *
 * 	It is an error if there is non-whitespace after the parsed value.
 *
 * 	On error errno is set to a non-zero value including EINVAL and ERANGE.
 * 	On success errno is set to 0.
 * parameters
 * 	list
 * 		a pointer to a range list to have the ranges parsed added
 * 	value
 * 		pointer to the beginning of the string to find the name or
 * 		number
 * returns
 * 	0       on success
 * 	-1	on error
 */
int safe_parse_gid_list(safe_id_range_list *list, const char *value)
{
    return parse_id_list(list, value, name_to_gid);
}