xref: /illumos-gate/usr/src/cmd/lp/lib/users/loadpri.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.12	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate # include	<errno.h>
34*7c478bd9Sstevel@tonic-gate # include	<stdio.h>
35*7c478bd9Sstevel@tonic-gate # include	<stdlib.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate # include	"lp.h"
38*7c478bd9Sstevel@tonic-gate # include	"users.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate static long pri;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate   Input:  Path name of the user priority file.  It has the following
44*7c478bd9Sstevel@tonic-gate 		format:
45*7c478bd9Sstevel@tonic-gate 	1 line with a number representing the default priority level.
46*7c478bd9Sstevel@tonic-gate 		This must be the first line of the file, and no extra
47*7c478bd9Sstevel@tonic-gate 		white space is allowed between the priority value and
48*7c478bd9Sstevel@tonic-gate 		the newline.
49*7c478bd9Sstevel@tonic-gate 	1 line anywhere in the file with a number representing
50*7c478bd9Sstevel@tonic-gate 		the default priority limit.  This number is followed
51*7c478bd9Sstevel@tonic-gate 		by a ':', and no extra white space is allowed.
52*7c478bd9Sstevel@tonic-gate 	any number of lines with a number followed by a ':', followed
53*7c478bd9Sstevel@tonic-gate 		by a white space (blank, tab or newline) separated
54*7c478bd9Sstevel@tonic-gate 		list of user names.  No white space is allowed
55*7c478bd9Sstevel@tonic-gate 		between the priority value and the colon (:), but any
56*7c478bd9Sstevel@tonic-gate 		amount is ok in the UID list.
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate   Note:  If the default priority level is missing, a value of 20 will
59*7c478bd9Sstevel@tonic-gate 	be used.  If the default limit is missing, zero will be used.
60*7c478bd9Sstevel@tonic-gate 	Also, the st_priority_file writes out the priority file in the
61*7c478bd9Sstevel@tonic-gate 	same order as the fields occur in the user_priority structure,
62*7c478bd9Sstevel@tonic-gate 	but the only order restriction is that the default level is
63*7c478bd9Sstevel@tonic-gate 	the first this.  A priority level may occur more than once, and
64*7c478bd9Sstevel@tonic-gate 	this function will group them together (but the defaults may
65*7c478bd9Sstevel@tonic-gate 	only occur once, however the defaults may occur only once each.
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate   Output:  This function returns a pointer to a statically stored
68*7c478bd9Sstevel@tonic-gate 	structure containing the priority information.
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate    Effect:  The user priority file is read and parsed.  Storage for
71*7c478bd9Sstevel@tonic-gate 	the priorities are allocated and loaded.  In case of an error,
72*7c478bd9Sstevel@tonic-gate 	it prints out an error message, and returns 0 (NULL).
73*7c478bd9Sstevel@tonic-gate */
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate struct user_priority * ld_priority_file ( char * path )
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate     char				line[BUFSIZ],
78*7c478bd9Sstevel@tonic-gate 					*p,
79*7c478bd9Sstevel@tonic-gate 					*user,
80*7c478bd9Sstevel@tonic-gate 					*next_user();
81*7c478bd9Sstevel@tonic-gate     static struct user_priority		pri_tbl;
82*7c478bd9Sstevel@tonic-gate     int					line_no	= 1,
83*7c478bd9Sstevel@tonic-gate     					opri;
84*7c478bd9Sstevel@tonic-gate     int fd;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate     if ((fd = open_locked(path, "r", 0)) < 0) {
87*7c478bd9Sstevel@tonic-gate 	if (errno == ENOENT) {
88*7c478bd9Sstevel@tonic-gate empty:
89*7c478bd9Sstevel@tonic-gate 	    pri_tbl.deflt = LEVEL_DFLT;
90*7c478bd9Sstevel@tonic-gate 	    pri_tbl.deflt_limit = LIMIT_DFLT;
91*7c478bd9Sstevel@tonic-gate 	    memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
92*7c478bd9Sstevel@tonic-gate 	    return (&pri_tbl);
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 	return(0);
95*7c478bd9Sstevel@tonic-gate     }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate     /* initialize table to empty */
98*7c478bd9Sstevel@tonic-gate     pri_tbl.deflt = -1;
99*7c478bd9Sstevel@tonic-gate     pri_tbl.deflt_limit = -1;
100*7c478bd9Sstevel@tonic-gate     memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate     /* this loop reads the line containing the default priority,
103*7c478bd9Sstevel@tonic-gate        if any, and the first priority limit.  p is left pointing
104*7c478bd9Sstevel@tonic-gate        to the colon (:) in the line with the first limit. */
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate     while (1)
107*7c478bd9Sstevel@tonic-gate     {
108*7c478bd9Sstevel@tonic-gate 	if (!(p = fdgets(line, BUFSIZ, fd)))
109*7c478bd9Sstevel@tonic-gate 	    goto empty;
110*7c478bd9Sstevel@tonic-gate 	p = line;
111*7c478bd9Sstevel@tonic-gate 	pri = strtol(line, &p, 10);
112*7c478bd9Sstevel@tonic-gate 	if (p == line)
113*7c478bd9Sstevel@tonic-gate 	    goto Error;
114*7c478bd9Sstevel@tonic-gate 	if (pri < PRI_MIN || pri > PRI_MAX)
115*7c478bd9Sstevel@tonic-gate 	    goto Error;
116*7c478bd9Sstevel@tonic-gate 	if (line_no == 1 && *p == '\n' && !p[1])
117*7c478bd9Sstevel@tonic-gate 	    pri_tbl.deflt = pri;
118*7c478bd9Sstevel@tonic-gate 	else
119*7c478bd9Sstevel@tonic-gate 	    if (*p == ':')
120*7c478bd9Sstevel@tonic-gate 	    {
121*7c478bd9Sstevel@tonic-gate 		p++;
122*7c478bd9Sstevel@tonic-gate 		break;
123*7c478bd9Sstevel@tonic-gate 	    }
124*7c478bd9Sstevel@tonic-gate 	    else
125*7c478bd9Sstevel@tonic-gate 		goto Error;
126*7c478bd9Sstevel@tonic-gate 	line_no++;
127*7c478bd9Sstevel@tonic-gate     }
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate     do
130*7c478bd9Sstevel@tonic-gate     {
131*7c478bd9Sstevel@tonic-gate 	/* search list for this priority */
132*7c478bd9Sstevel@tonic-gate 	opri = pri;
133*7c478bd9Sstevel@tonic-gate 	if (!(user = next_user(fd, line, &p)))
134*7c478bd9Sstevel@tonic-gate 	{
135*7c478bd9Sstevel@tonic-gate 	    if (pri_tbl.deflt_limit == -1)
136*7c478bd9Sstevel@tonic-gate 	    {
137*7c478bd9Sstevel@tonic-gate 		pri_tbl.deflt_limit = opri;
138*7c478bd9Sstevel@tonic-gate 		if (pri == -1) break;
139*7c478bd9Sstevel@tonic-gate 		if (!(user = next_user(fd, line, &p))) goto Error;
140*7c478bd9Sstevel@tonic-gate 	    }
141*7c478bd9Sstevel@tonic-gate 	    else
142*7c478bd9Sstevel@tonic-gate 	    {
143*7c478bd9Sstevel@tonic-gate Error:
144*7c478bd9Sstevel@tonic-gate 	        errno = EBADF;
145*7c478bd9Sstevel@tonic-gate 		close(fd);
146*7c478bd9Sstevel@tonic-gate 		return(0);
147*7c478bd9Sstevel@tonic-gate 	    }
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	do
151*7c478bd9Sstevel@tonic-gate 	{
152*7c478bd9Sstevel@tonic-gate 	    add_user (&pri_tbl, user, pri);
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 	while ((user = next_user(fd, line, &p)));
155*7c478bd9Sstevel@tonic-gate     }
156*7c478bd9Sstevel@tonic-gate     while (pri != -1);
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate     if (pri_tbl.deflt == -1)
159*7c478bd9Sstevel@tonic-gate 	pri_tbl.deflt = LEVEL_DFLT;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate     if (pri_tbl.deflt_limit == -1)
162*7c478bd9Sstevel@tonic-gate 	pri_tbl.deflt_limit = LIMIT_DFLT;
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate     close(fd);
165*7c478bd9Sstevel@tonic-gate     return (&pri_tbl);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate Inputs:  A pointer to a limit structure, and a user.
170*7c478bd9Sstevel@tonic-gate Ouputs:  The limit structure is modified.
171*7c478bd9Sstevel@tonic-gate Effects: Adds <user> to the list of users, if it is not already
172*7c478bd9Sstevel@tonic-gate 	 there.
173*7c478bd9Sstevel@tonic-gate */
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate int add_user ( struct user_priority * ppri_tbl, char * user, int limit )
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate     if (limit < PRI_MIN || PRI_MAX < limit)
178*7c478bd9Sstevel@tonic-gate 	return 1;
179*7c478bd9Sstevel@tonic-gate     addlist (&(ppri_tbl->users[limit - PRI_MIN]), user);
180*7c478bd9Sstevel@tonic-gate     return 0;
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /*
184*7c478bd9Sstevel@tonic-gate Inputs:   The input file to read additional lines, a pointer to
185*7c478bd9Sstevel@tonic-gate 	  a buffer containing the current line, and to read additional
186*7c478bd9Sstevel@tonic-gate 	  lines into, and a pointer to the location pointer (a pointer
187*7c478bd9Sstevel@tonic-gate 	  into buf).
188*7c478bd9Sstevel@tonic-gate Outputs:  The routine returns the next user-id read or 0 if all the
189*7c478bd9Sstevel@tonic-gate 	  users for this priority are read.  The buffer, the location
190*7c478bd9Sstevel@tonic-gate 	  pointer, and the variable pri are modified as a side effect.
191*7c478bd9Sstevel@tonic-gate Effects:  The input buffer is scanned starting at *pp for the next
192*7c478bd9Sstevel@tonic-gate 	  user-id, if the end of the line is reached, the next line is
193*7c478bd9Sstevel@tonic-gate 	  read from the file.  If it scans the next priority value, the
194*7c478bd9Sstevel@tonic-gate 	  variable pri (static to this file), is set to that priority.
195*7c478bd9Sstevel@tonic-gate 	  EOF is indicated by setting this variable to -1, and also
196*7c478bd9Sstevel@tonic-gate 	  returning 0.
197*7c478bd9Sstevel@tonic-gate */
198*7c478bd9Sstevel@tonic-gate char * next_user (int fd, char * buf, char ** pp )
199*7c478bd9Sstevel@tonic-gate {
200*7c478bd9Sstevel@tonic-gate     long	temp;
201*7c478bd9Sstevel@tonic-gate     char	*p;
202*7c478bd9Sstevel@tonic-gate     static	beg_line = 0; /* assumes a partial line is in buf to start */
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate     do
205*7c478bd9Sstevel@tonic-gate     {
206*7c478bd9Sstevel@tonic-gate 	while (**pp == ' ' || **pp == '\n' || **pp == '\t')
207*7c478bd9Sstevel@tonic-gate 	    (*pp)++;
208*7c478bd9Sstevel@tonic-gate 	p = *pp;
209*7c478bd9Sstevel@tonic-gate 	if (*p)
210*7c478bd9Sstevel@tonic-gate 	{
211*7c478bd9Sstevel@tonic-gate 	    if (*p >= '0' && *p <= '9')
212*7c478bd9Sstevel@tonic-gate 	    {
213*7c478bd9Sstevel@tonic-gate 		temp = strtol(p, pp, 10);
214*7c478bd9Sstevel@tonic-gate 		if (beg_line && **pp == ':')
215*7c478bd9Sstevel@tonic-gate 		{
216*7c478bd9Sstevel@tonic-gate 		    (*pp)++;
217*7c478bd9Sstevel@tonic-gate 		    pri = temp;
218*7c478bd9Sstevel@tonic-gate 		    beg_line = 0;
219*7c478bd9Sstevel@tonic-gate 		    return (0);
220*7c478bd9Sstevel@tonic-gate 		}
221*7c478bd9Sstevel@tonic-gate 	    }
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	    for (; **pp && **pp != ' ' && **pp != '\n' && **pp != '\t'; (*pp)++)
224*7c478bd9Sstevel@tonic-gate 		;
225*7c478bd9Sstevel@tonic-gate 	    if (**pp)
226*7c478bd9Sstevel@tonic-gate 		*(*pp)++ = 0;
227*7c478bd9Sstevel@tonic-gate 	    beg_line = 0;
228*7c478bd9Sstevel@tonic-gate 	    return (p);
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 	beg_line = 1;
231*7c478bd9Sstevel@tonic-gate     }
232*7c478bd9Sstevel@tonic-gate     while (*pp = fdgets(buf, BUFSIZ, fd));
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate     pri = -1;
235*7c478bd9Sstevel@tonic-gate     return (0);
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate /*
239*7c478bd9Sstevel@tonic-gate Inputs:  A pointer to a priority table and a user.
240*7c478bd9Sstevel@tonic-gate Outputs: Zero if user found, else 1, and priority table is modified.
241*7c478bd9Sstevel@tonic-gate Effects: All occurences of <user> in the priority table will be removed.
242*7c478bd9Sstevel@tonic-gate 	 (There should only be one at most.)
243*7c478bd9Sstevel@tonic-gate */
244*7c478bd9Sstevel@tonic-gate int del_user ( struct user_priority * ppri_tbl, char * user )
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate     int		limit;
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate     for (limit = PRI_MIN; limit <= PRI_MAX; limit++)
249*7c478bd9Sstevel@tonic-gate 	if (searchlist(user, ppri_tbl->users[limit - PRI_MIN]))
250*7c478bd9Sstevel@tonic-gate 	{
251*7c478bd9Sstevel@tonic-gate 	    dellist (&(ppri_tbl->users[limit - PRI_MIN]), user);
252*7c478bd9Sstevel@tonic-gate 	    return (0);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate     return (1);
255*7c478bd9Sstevel@tonic-gate }
256