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
30*f928ce67Sceastha #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate # include <errno.h>
337c478bd9Sstevel@tonic-gate # include <stdio.h>
347c478bd9Sstevel@tonic-gate # include <stdlib.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate # include "lp.h"
377c478bd9Sstevel@tonic-gate # include "users.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate static long pri;
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate Input: Path name of the user priority file. It has the following
437c478bd9Sstevel@tonic-gate format:
447c478bd9Sstevel@tonic-gate 1 line with a number representing the default priority level.
457c478bd9Sstevel@tonic-gate This must be the first line of the file, and no extra
467c478bd9Sstevel@tonic-gate white space is allowed between the priority value and
477c478bd9Sstevel@tonic-gate the newline.
487c478bd9Sstevel@tonic-gate 1 line anywhere in the file with a number representing
497c478bd9Sstevel@tonic-gate the default priority limit. This number is followed
507c478bd9Sstevel@tonic-gate by a ':', and no extra white space is allowed.
517c478bd9Sstevel@tonic-gate any number of lines with a number followed by a ':', followed
527c478bd9Sstevel@tonic-gate by a white space (blank, tab or newline) separated
537c478bd9Sstevel@tonic-gate list of user names. No white space is allowed
547c478bd9Sstevel@tonic-gate between the priority value and the colon (:), but any
557c478bd9Sstevel@tonic-gate amount is ok in the UID list.
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate Note: If the default priority level is missing, a value of 20 will
587c478bd9Sstevel@tonic-gate be used. If the default limit is missing, zero will be used.
597c478bd9Sstevel@tonic-gate Also, the st_priority_file writes out the priority file in the
607c478bd9Sstevel@tonic-gate same order as the fields occur in the user_priority structure,
617c478bd9Sstevel@tonic-gate but the only order restriction is that the default level is
627c478bd9Sstevel@tonic-gate the first this. A priority level may occur more than once, and
637c478bd9Sstevel@tonic-gate this function will group them together (but the defaults may
647c478bd9Sstevel@tonic-gate only occur once, however the defaults may occur only once each.
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate Output: This function returns a pointer to a statically stored
677c478bd9Sstevel@tonic-gate structure containing the priority information.
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate Effect: The user priority file is read and parsed. Storage for
707c478bd9Sstevel@tonic-gate the priorities are allocated and loaded. In case of an error,
717c478bd9Sstevel@tonic-gate it prints out an error message, and returns 0 (NULL).
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate
ld_priority_file(char * path)747c478bd9Sstevel@tonic-gate struct user_priority * ld_priority_file ( char * path )
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate char line[BUFSIZ],
777c478bd9Sstevel@tonic-gate *p,
787c478bd9Sstevel@tonic-gate *user,
797c478bd9Sstevel@tonic-gate *next_user();
807c478bd9Sstevel@tonic-gate static struct user_priority pri_tbl;
817c478bd9Sstevel@tonic-gate int line_no = 1,
827c478bd9Sstevel@tonic-gate opri;
837c478bd9Sstevel@tonic-gate int fd;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate if ((fd = open_locked(path, "r", 0)) < 0) {
867c478bd9Sstevel@tonic-gate if (errno == ENOENT) {
877c478bd9Sstevel@tonic-gate empty:
887c478bd9Sstevel@tonic-gate pri_tbl.deflt = LEVEL_DFLT;
897c478bd9Sstevel@tonic-gate pri_tbl.deflt_limit = LIMIT_DFLT;
907c478bd9Sstevel@tonic-gate memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
917c478bd9Sstevel@tonic-gate return (&pri_tbl);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate return(0);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /* initialize table to empty */
977c478bd9Sstevel@tonic-gate pri_tbl.deflt = -1;
987c478bd9Sstevel@tonic-gate pri_tbl.deflt_limit = -1;
997c478bd9Sstevel@tonic-gate memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /* this loop reads the line containing the default priority,
1027c478bd9Sstevel@tonic-gate if any, and the first priority limit. p is left pointing
1037c478bd9Sstevel@tonic-gate to the colon (:) in the line with the first limit. */
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate while (1)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate if (!(p = fdgets(line, BUFSIZ, fd)))
1087c478bd9Sstevel@tonic-gate goto empty;
1097c478bd9Sstevel@tonic-gate p = line;
1107c478bd9Sstevel@tonic-gate pri = strtol(line, &p, 10);
1117c478bd9Sstevel@tonic-gate if (p == line)
1127c478bd9Sstevel@tonic-gate goto Error;
1137c478bd9Sstevel@tonic-gate if (pri < PRI_MIN || pri > PRI_MAX)
1147c478bd9Sstevel@tonic-gate goto Error;
1157c478bd9Sstevel@tonic-gate if (line_no == 1 && *p == '\n' && !p[1])
1167c478bd9Sstevel@tonic-gate pri_tbl.deflt = pri;
1177c478bd9Sstevel@tonic-gate else
1187c478bd9Sstevel@tonic-gate if (*p == ':')
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate p++;
1217c478bd9Sstevel@tonic-gate break;
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate else
1247c478bd9Sstevel@tonic-gate goto Error;
1257c478bd9Sstevel@tonic-gate line_no++;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate do
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate /* search list for this priority */
1317c478bd9Sstevel@tonic-gate opri = pri;
1327c478bd9Sstevel@tonic-gate if (!(user = next_user(fd, line, &p)))
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate if (pri_tbl.deflt_limit == -1)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate pri_tbl.deflt_limit = opri;
1377c478bd9Sstevel@tonic-gate if (pri == -1) break;
1387c478bd9Sstevel@tonic-gate if (!(user = next_user(fd, line, &p))) goto Error;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate else
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate Error:
1437c478bd9Sstevel@tonic-gate errno = EBADF;
1447c478bd9Sstevel@tonic-gate close(fd);
1457c478bd9Sstevel@tonic-gate return(0);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate do
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate add_user (&pri_tbl, user, pri);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate while ((user = next_user(fd, line, &p)));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate while (pri != -1);
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate if (pri_tbl.deflt == -1)
1587c478bd9Sstevel@tonic-gate pri_tbl.deflt = LEVEL_DFLT;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate if (pri_tbl.deflt_limit == -1)
1617c478bd9Sstevel@tonic-gate pri_tbl.deflt_limit = LIMIT_DFLT;
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate close(fd);
1647c478bd9Sstevel@tonic-gate return (&pri_tbl);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate Inputs: A pointer to a limit structure, and a user.
1697c478bd9Sstevel@tonic-gate Ouputs: The limit structure is modified.
1707c478bd9Sstevel@tonic-gate Effects: Adds <user> to the list of users, if it is not already
1717c478bd9Sstevel@tonic-gate there.
1727c478bd9Sstevel@tonic-gate */
1737c478bd9Sstevel@tonic-gate
add_user(struct user_priority * ppri_tbl,char * user,int limit)1747c478bd9Sstevel@tonic-gate int add_user ( struct user_priority * ppri_tbl, char * user, int limit )
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate if (limit < PRI_MIN || PRI_MAX < limit)
1777c478bd9Sstevel@tonic-gate return 1;
1787c478bd9Sstevel@tonic-gate addlist (&(ppri_tbl->users[limit - PRI_MIN]), user);
1797c478bd9Sstevel@tonic-gate return 0;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate Inputs: The input file to read additional lines, a pointer to
1847c478bd9Sstevel@tonic-gate a buffer containing the current line, and to read additional
1857c478bd9Sstevel@tonic-gate lines into, and a pointer to the location pointer (a pointer
1867c478bd9Sstevel@tonic-gate into buf).
1877c478bd9Sstevel@tonic-gate Outputs: The routine returns the next user-id read or 0 if all the
1887c478bd9Sstevel@tonic-gate users for this priority are read. The buffer, the location
1897c478bd9Sstevel@tonic-gate pointer, and the variable pri are modified as a side effect.
1907c478bd9Sstevel@tonic-gate Effects: The input buffer is scanned starting at *pp for the next
1917c478bd9Sstevel@tonic-gate user-id, if the end of the line is reached, the next line is
1927c478bd9Sstevel@tonic-gate read from the file. If it scans the next priority value, the
1937c478bd9Sstevel@tonic-gate variable pri (static to this file), is set to that priority.
1947c478bd9Sstevel@tonic-gate EOF is indicated by setting this variable to -1, and also
1957c478bd9Sstevel@tonic-gate returning 0.
1967c478bd9Sstevel@tonic-gate */
next_user(int fd,char * buf,char ** pp)1977c478bd9Sstevel@tonic-gate char * next_user (int fd, char * buf, char ** pp )
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate long temp;
2007c478bd9Sstevel@tonic-gate char *p;
201*f928ce67Sceastha static int beg_line = 0; /* assumes a partial line is in buf to start */
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate do
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate while (**pp == ' ' || **pp == '\n' || **pp == '\t')
2067c478bd9Sstevel@tonic-gate (*pp)++;
2077c478bd9Sstevel@tonic-gate p = *pp;
2087c478bd9Sstevel@tonic-gate if (*p)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate if (*p >= '0' && *p <= '9')
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate temp = strtol(p, pp, 10);
2137c478bd9Sstevel@tonic-gate if (beg_line && **pp == ':')
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate (*pp)++;
2167c478bd9Sstevel@tonic-gate pri = temp;
2177c478bd9Sstevel@tonic-gate beg_line = 0;
2187c478bd9Sstevel@tonic-gate return (0);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate for (; **pp && **pp != ' ' && **pp != '\n' && **pp != '\t'; (*pp)++)
2237c478bd9Sstevel@tonic-gate ;
2247c478bd9Sstevel@tonic-gate if (**pp)
2257c478bd9Sstevel@tonic-gate *(*pp)++ = 0;
2267c478bd9Sstevel@tonic-gate beg_line = 0;
2277c478bd9Sstevel@tonic-gate return (p);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate beg_line = 1;
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate while (*pp = fdgets(buf, BUFSIZ, fd));
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate pri = -1;
2347c478bd9Sstevel@tonic-gate return (0);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate Inputs: A pointer to a priority table and a user.
2397c478bd9Sstevel@tonic-gate Outputs: Zero if user found, else 1, and priority table is modified.
2407c478bd9Sstevel@tonic-gate Effects: All occurences of <user> in the priority table will be removed.
2417c478bd9Sstevel@tonic-gate (There should only be one at most.)
2427c478bd9Sstevel@tonic-gate */
del_user(struct user_priority * ppri_tbl,char * user)2437c478bd9Sstevel@tonic-gate int del_user ( struct user_priority * ppri_tbl, char * user )
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate int limit;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate for (limit = PRI_MIN; limit <= PRI_MAX; limit++)
2487c478bd9Sstevel@tonic-gate if (searchlist(user, ppri_tbl->users[limit - PRI_MIN]))
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate dellist (&(ppri_tbl->users[limit - PRI_MIN]), user);
2517c478bd9Sstevel@tonic-gate return (0);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate return (1);
2547c478bd9Sstevel@tonic-gate }
255