xref: /illumos-gate/usr/src/cmd/lp/lib/users/usermgmt.c (revision 2a8bcb4e)
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 /* LINTLIBRARY */
31 
32 # include	<stdio.h>
33 
34 # include	"lp.h"
35 # include	"users.h"
36 
37 static int loaded = 0;
38 static struct user_priority *ppri_tbl;
39 struct user_priority *ld_priority_file();
40 static USER usr;
41 
putuser(char * user,USER * pri_s)42 int putuser ( char * user, USER * pri_s )
43 {
44     int fd;
45 
46     if (!loaded)
47     {
48 	if (!(ppri_tbl = ld_priority_file(Lp_Users)))
49 	    return(-1);
50 	loaded = 1;
51     }
52 
53     if (!add_user(ppri_tbl, user, pri_s->priority_limit))
54     {
55 	return(-1);
56     }
57 
58     if ((fd = open_locked(Lp_Users, "w", LPU_MODE)) < 0)
59 	return(-1);
60     output_tbl(fd, ppri_tbl);
61     close(fd);
62     return(0);
63 }
64 
getuser(char * user)65 USER * getuser ( char * user )
66 {
67     int limit;
68 
69     /* root and lp do not get a limit */
70     if (STREQU(user, "root") || STREQU(user, LPUSER))
71     {
72 	usr.priority_limit = 0;
73 	return(&usr);
74     }
75 
76     if (!loaded)
77     {
78 	if (!(ppri_tbl = ld_priority_file(Lp_Users)))
79 	    return((USER *)0);
80 
81 	loaded = 1;
82     }
83 
84     for (limit = PRI_MIN; limit <= PRI_MAX; limit++)
85 	if (bang_searchlist(user, ppri_tbl->users[limit - PRI_MIN]))
86 	{
87 	    usr.priority_limit = limit;
88 	    return(&usr);
89 	}
90 
91     usr.priority_limit = ppri_tbl->deflt_limit;
92     return(&usr);
93 }
94 
deluser(char * user)95 int deluser ( char * user )
96 {
97     int fd;
98 
99     if (!loaded)
100     {
101 	if (!(ppri_tbl = ld_priority_file(Lp_Users)))
102 	    return(-1);
103 
104 	loaded = 1;
105     }
106 
107     del_user(ppri_tbl, user);
108 
109     if ((fd = open_locked(Lp_Users, "w", LPU_MODE)) < 0)
110 	return(-1);
111 
112     output_tbl(fd, ppri_tbl);
113     close(fd);
114     return(0);
115 }
116 
getdfltpri(void)117 int getdfltpri ( void )
118 {
119     if (!loaded)
120     {
121 	if (!(ppri_tbl = ld_priority_file(Lp_Users)))
122 	    return(-1);
123 
124 	loaded = 1;
125     }
126 
127     return (ppri_tbl->deflt);
128 }
129 
130 void
trashusers(void)131 trashusers(void)
132 {
133     int limit;
134 
135     if (loaded)
136     {
137 	if (ppri_tbl)
138 	{
139 	    for (limit = PRI_MIN; limit <= PRI_MAX; limit++)
140 		freelist (ppri_tbl->users[limit - PRI_MIN]);
141 	    ppri_tbl = 0;
142 	}
143 	loaded = 0;
144     }
145 }
146 
147