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