xref: /titanic_50/usr/src/cmd/oamuser/user/val_lprj.c (revision c0e7977a434048a8bc7386ea0e8befaa77a646cf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2013 RackTop Systems.
29  */
30 
31 #include	<sys/types.h>
32 #include	<stdio.h>
33 #include	<strings.h>
34 #include	<sys/param.h>
35 #include	<users.h>
36 #include	<userdefs.h>
37 #include	<project.h>
38 #include	<errno.h>
39 #include	"messages.h"
40 
41 
42 static projid_t projlist[NPROJECTS_MAX + 1];
43 static int nproj_max = NPROJECTS_MAX;
44 
45 /* Validate a list of projects */
46 int **
47 valid_lproject(char *list)
48 {
49 	int n_invalid = 0;
50 	int i = 0;
51 	int j;
52 	char *ptr;
53 	struct project projent;
54 	int warning;
55 	char mybuf[PROJECT_BUFSZ];
56 
57 	if (!list || !*list)
58 		return ((int **)NULL);
59 
60 	while ((ptr = strtok((i || n_invalid) ? NULL : list, ","))) {
61 
62 		switch (valid_project(ptr, &projent, mybuf, sizeof (mybuf),
63 		    &warning)) {
64 		case INVALID:
65 			errmsg(M_INVALID, ptr, "project id");
66 			n_invalid++;
67 			break;
68 		case TOOBIG:
69 			errmsg(M_TOOBIG, "projid", ptr);
70 			n_invalid++;
71 			break;
72 		case UNIQUE:
73 			errmsg(M_PROJ_NOTUSED, ptr);
74 			n_invalid++;
75 			break;
76 		case NOTUNIQUE:
77 			if (!i)
78 				/* ignore respecified primary  */
79 				projlist[i++] = projent.pj_projid;
80 			else {
81 				/* Keep out duplicates */
82 				for (j = 0; j < i; j++)
83 					if (projent.pj_projid == projlist[j])
84 						break;
85 
86 				if (j == i)
87 					/* Not a duplicate */
88 					projlist[i++] = projent.pj_projid;
89 			}
90 			break;
91 		}
92 		if (warning)
93 			warningmsg(warning, ptr);
94 
95 		if (i >= nproj_max) {
96 			errmsg(M_MAXPROJECTS, nproj_max);
97 			break;
98 		}
99 	}
100 
101 	/* Terminate the list */
102 	projlist[i] = -1;
103 
104 	if (n_invalid)
105 		exit(EX_BADARG);
106 
107 	return ((int **)projlist);
108 }
109