xref: /illumos-gate/usr/src/cmd/lp/lib/users/loadpri.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*f928ce67Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate # include	<errno.h>
317c478bd9Sstevel@tonic-gate # include	<stdio.h>
327c478bd9Sstevel@tonic-gate # include	<stdlib.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate # include	"lp.h"
357c478bd9Sstevel@tonic-gate # include	"users.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static long pri;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate   Input:  Path name of the user priority file.  It has the following
417c478bd9Sstevel@tonic-gate 		format:
427c478bd9Sstevel@tonic-gate 	1 line with a number representing the default priority level.
437c478bd9Sstevel@tonic-gate 		This must be the first line of the file, and no extra
447c478bd9Sstevel@tonic-gate 		white space is allowed between the priority value and
457c478bd9Sstevel@tonic-gate 		the newline.
467c478bd9Sstevel@tonic-gate 	1 line anywhere in the file with a number representing
477c478bd9Sstevel@tonic-gate 		the default priority limit.  This number is followed
487c478bd9Sstevel@tonic-gate 		by a ':', and no extra white space is allowed.
497c478bd9Sstevel@tonic-gate 	any number of lines with a number followed by a ':', followed
507c478bd9Sstevel@tonic-gate 		by a white space (blank, tab or newline) separated
517c478bd9Sstevel@tonic-gate 		list of user names.  No white space is allowed
527c478bd9Sstevel@tonic-gate 		between the priority value and the colon (:), but any
537c478bd9Sstevel@tonic-gate 		amount is ok in the UID list.
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate   Note:  If the default priority level is missing, a value of 20 will
567c478bd9Sstevel@tonic-gate 	be used.  If the default limit is missing, zero will be used.
577c478bd9Sstevel@tonic-gate 	Also, the st_priority_file writes out the priority file in the
587c478bd9Sstevel@tonic-gate 	same order as the fields occur in the user_priority structure,
597c478bd9Sstevel@tonic-gate 	but the only order restriction is that the default level is
607c478bd9Sstevel@tonic-gate 	the first this.  A priority level may occur more than once, and
617c478bd9Sstevel@tonic-gate 	this function will group them together (but the defaults may
627c478bd9Sstevel@tonic-gate 	only occur once, however the defaults may occur only once each.
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate   Output:  This function returns a pointer to a statically stored
657c478bd9Sstevel@tonic-gate 	structure containing the priority information.
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate    Effect:  The user priority file is read and parsed.  Storage for
687c478bd9Sstevel@tonic-gate 	the priorities are allocated and loaded.  In case of an error,
697c478bd9Sstevel@tonic-gate 	it prints out an error message, and returns 0 (NULL).
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate 
ld_priority_file(char * path)727c478bd9Sstevel@tonic-gate struct user_priority * ld_priority_file ( char * path )
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate     char				line[BUFSIZ],
757c478bd9Sstevel@tonic-gate 					*p,
767c478bd9Sstevel@tonic-gate 					*user,
777c478bd9Sstevel@tonic-gate 					*next_user();
787c478bd9Sstevel@tonic-gate     static struct user_priority		pri_tbl;
797c478bd9Sstevel@tonic-gate     int					line_no	= 1,
807c478bd9Sstevel@tonic-gate     					opri;
817c478bd9Sstevel@tonic-gate     int fd;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate     if ((fd = open_locked(path, "r", 0)) < 0) {
847c478bd9Sstevel@tonic-gate 	if (errno == ENOENT) {
857c478bd9Sstevel@tonic-gate empty:
867c478bd9Sstevel@tonic-gate 	    pri_tbl.deflt = LEVEL_DFLT;
877c478bd9Sstevel@tonic-gate 	    pri_tbl.deflt_limit = LIMIT_DFLT;
887c478bd9Sstevel@tonic-gate 	    memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
897c478bd9Sstevel@tonic-gate 	    return (&pri_tbl);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	return(0);
927c478bd9Sstevel@tonic-gate     }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate     /* initialize table to empty */
957c478bd9Sstevel@tonic-gate     pri_tbl.deflt = -1;
967c478bd9Sstevel@tonic-gate     pri_tbl.deflt_limit = -1;
977c478bd9Sstevel@tonic-gate     memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate     /* this loop reads the line containing the default priority,
1007c478bd9Sstevel@tonic-gate        if any, and the first priority limit.  p is left pointing
1017c478bd9Sstevel@tonic-gate        to the colon (:) in the line with the first limit. */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate     while (1)
1047c478bd9Sstevel@tonic-gate     {
1057c478bd9Sstevel@tonic-gate 	if (!(p = fdgets(line, BUFSIZ, fd)))
1067c478bd9Sstevel@tonic-gate 	    goto empty;
1077c478bd9Sstevel@tonic-gate 	p = line;
1087c478bd9Sstevel@tonic-gate 	pri = strtol(line, &p, 10);
1097c478bd9Sstevel@tonic-gate 	if (p == line)
1107c478bd9Sstevel@tonic-gate 	    goto Error;
1117c478bd9Sstevel@tonic-gate 	if (pri < PRI_MIN || pri > PRI_MAX)
1127c478bd9Sstevel@tonic-gate 	    goto Error;
1137c478bd9Sstevel@tonic-gate 	if (line_no == 1 && *p == '\n' && !p[1])
1147c478bd9Sstevel@tonic-gate 	    pri_tbl.deflt = pri;
1157c478bd9Sstevel@tonic-gate 	else
1167c478bd9Sstevel@tonic-gate 	    if (*p == ':')
1177c478bd9Sstevel@tonic-gate 	    {
1187c478bd9Sstevel@tonic-gate 		p++;
1197c478bd9Sstevel@tonic-gate 		break;
1207c478bd9Sstevel@tonic-gate 	    }
1217c478bd9Sstevel@tonic-gate 	    else
1227c478bd9Sstevel@tonic-gate 		goto Error;
1237c478bd9Sstevel@tonic-gate 	line_no++;
1247c478bd9Sstevel@tonic-gate     }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate     do
1277c478bd9Sstevel@tonic-gate     {
1287c478bd9Sstevel@tonic-gate 	/* search list for this priority */
1297c478bd9Sstevel@tonic-gate 	opri = pri;
1307c478bd9Sstevel@tonic-gate 	if (!(user = next_user(fd, line, &p)))
1317c478bd9Sstevel@tonic-gate 	{
1327c478bd9Sstevel@tonic-gate 	    if (pri_tbl.deflt_limit == -1)
1337c478bd9Sstevel@tonic-gate 	    {
1347c478bd9Sstevel@tonic-gate 		pri_tbl.deflt_limit = opri;
1357c478bd9Sstevel@tonic-gate 		if (pri == -1) break;
1367c478bd9Sstevel@tonic-gate 		if (!(user = next_user(fd, line, &p))) goto Error;
1377c478bd9Sstevel@tonic-gate 	    }
1387c478bd9Sstevel@tonic-gate 	    else
1397c478bd9Sstevel@tonic-gate 	    {
1407c478bd9Sstevel@tonic-gate Error:
1417c478bd9Sstevel@tonic-gate 	        errno = EBADF;
1427c478bd9Sstevel@tonic-gate 		close(fd);
1437c478bd9Sstevel@tonic-gate 		return(0);
1447c478bd9Sstevel@tonic-gate 	    }
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	do
1487c478bd9Sstevel@tonic-gate 	{
1497c478bd9Sstevel@tonic-gate 	    add_user (&pri_tbl, user, pri);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	while ((user = next_user(fd, line, &p)));
1527c478bd9Sstevel@tonic-gate     }
1537c478bd9Sstevel@tonic-gate     while (pri != -1);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate     if (pri_tbl.deflt == -1)
1567c478bd9Sstevel@tonic-gate 	pri_tbl.deflt = LEVEL_DFLT;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate     if (pri_tbl.deflt_limit == -1)
1597c478bd9Sstevel@tonic-gate 	pri_tbl.deflt_limit = LIMIT_DFLT;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate     close(fd);
1627c478bd9Sstevel@tonic-gate     return (&pri_tbl);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate Inputs:  A pointer to a limit structure, and a user.
1677c478bd9Sstevel@tonic-gate Ouputs:  The limit structure is modified.
1687c478bd9Sstevel@tonic-gate Effects: Adds <user> to the list of users, if it is not already
1697c478bd9Sstevel@tonic-gate 	 there.
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate 
add_user(struct user_priority * ppri_tbl,char * user,int limit)1727c478bd9Sstevel@tonic-gate int add_user ( struct user_priority * ppri_tbl, char * user, int limit )
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate     if (limit < PRI_MIN || PRI_MAX < limit)
1757c478bd9Sstevel@tonic-gate 	return 1;
1767c478bd9Sstevel@tonic-gate     addlist (&(ppri_tbl->users[limit - PRI_MIN]), user);
1777c478bd9Sstevel@tonic-gate     return 0;
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate Inputs:   The input file to read additional lines, a pointer to
1827c478bd9Sstevel@tonic-gate 	  a buffer containing the current line, and to read additional
1837c478bd9Sstevel@tonic-gate 	  lines into, and a pointer to the location pointer (a pointer
1847c478bd9Sstevel@tonic-gate 	  into buf).
1857c478bd9Sstevel@tonic-gate Outputs:  The routine returns the next user-id read or 0 if all the
1867c478bd9Sstevel@tonic-gate 	  users for this priority are read.  The buffer, the location
1877c478bd9Sstevel@tonic-gate 	  pointer, and the variable pri are modified as a side effect.
1887c478bd9Sstevel@tonic-gate Effects:  The input buffer is scanned starting at *pp for the next
1897c478bd9Sstevel@tonic-gate 	  user-id, if the end of the line is reached, the next line is
1907c478bd9Sstevel@tonic-gate 	  read from the file.  If it scans the next priority value, the
1917c478bd9Sstevel@tonic-gate 	  variable pri (static to this file), is set to that priority.
1927c478bd9Sstevel@tonic-gate 	  EOF is indicated by setting this variable to -1, and also
1937c478bd9Sstevel@tonic-gate 	  returning 0.
1947c478bd9Sstevel@tonic-gate */
next_user(int fd,char * buf,char ** pp)1957c478bd9Sstevel@tonic-gate char * next_user (int fd, char * buf, char ** pp )
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate     long	temp;
1987c478bd9Sstevel@tonic-gate     char	*p;
199*f928ce67Sceastha     static	int beg_line = 0; /* assumes a partial line is in buf to start */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate     do
2027c478bd9Sstevel@tonic-gate     {
2037c478bd9Sstevel@tonic-gate 	while (**pp == ' ' || **pp == '\n' || **pp == '\t')
2047c478bd9Sstevel@tonic-gate 	    (*pp)++;
2057c478bd9Sstevel@tonic-gate 	p = *pp;
2067c478bd9Sstevel@tonic-gate 	if (*p)
2077c478bd9Sstevel@tonic-gate 	{
2087c478bd9Sstevel@tonic-gate 	    if (*p >= '0' && *p <= '9')
2097c478bd9Sstevel@tonic-gate 	    {
2107c478bd9Sstevel@tonic-gate 		temp = strtol(p, pp, 10);
2117c478bd9Sstevel@tonic-gate 		if (beg_line && **pp == ':')
2127c478bd9Sstevel@tonic-gate 		{
2137c478bd9Sstevel@tonic-gate 		    (*pp)++;
2147c478bd9Sstevel@tonic-gate 		    pri = temp;
2157c478bd9Sstevel@tonic-gate 		    beg_line = 0;
2167c478bd9Sstevel@tonic-gate 		    return (0);
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 	    }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	    for (; **pp && **pp != ' ' && **pp != '\n' && **pp != '\t'; (*pp)++)
2217c478bd9Sstevel@tonic-gate 		;
2227c478bd9Sstevel@tonic-gate 	    if (**pp)
2237c478bd9Sstevel@tonic-gate 		*(*pp)++ = 0;
2247c478bd9Sstevel@tonic-gate 	    beg_line = 0;
2257c478bd9Sstevel@tonic-gate 	    return (p);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 	beg_line = 1;
2287c478bd9Sstevel@tonic-gate     }
2297c478bd9Sstevel@tonic-gate     while (*pp = fdgets(buf, BUFSIZ, fd));
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate     pri = -1;
2327c478bd9Sstevel@tonic-gate     return (0);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate Inputs:  A pointer to a priority table and a user.
2377c478bd9Sstevel@tonic-gate Outputs: Zero if user found, else 1, and priority table is modified.
2387c478bd9Sstevel@tonic-gate Effects: All occurences of <user> in the priority table will be removed.
2397c478bd9Sstevel@tonic-gate 	 (There should only be one at most.)
2407c478bd9Sstevel@tonic-gate */
del_user(struct user_priority * ppri_tbl,char * user)2417c478bd9Sstevel@tonic-gate int del_user ( struct user_priority * ppri_tbl, char * user )
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate     int		limit;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate     for (limit = PRI_MIN; limit <= PRI_MAX; limit++)
2467c478bd9Sstevel@tonic-gate 	if (searchlist(user, ppri_tbl->users[limit - PRI_MIN]))
2477c478bd9Sstevel@tonic-gate 	{
2487c478bd9Sstevel@tonic-gate 	    dellist (&(ppri_tbl->users[limit - PRI_MIN]), user);
2497c478bd9Sstevel@tonic-gate 	    return (0);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate     return (1);
2527c478bd9Sstevel@tonic-gate }
253