xref: /titanic_44/usr/src/cmd/lp/cmd/lpusers.c (revision 5fd03bc0f2e00e7ba02316c2e08f45d52aab15db)
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 /* lpusers [-q priority-level] -u (user-list | "")
31    lpusers -d priority-level
32    lpusers -l
33 */
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <sys/types.h>
38 #include <locale.h>
39 
40 #include "lp.h"
41 #include "users.h"
42 #include "msgs.h"
43 
44 #define	WHO_AM_I	I_AM_LPUSERS
45 #include "oam.h"
46 
47 char message[100],
48      reply[100];
49 
50 char	*PRIORITY;
51 
52 int add_user(), del_user();
53 
54 int
55 main(int argc, char *argv[])
56 {
57     int mtype, size, c,
58 	list = FALSE, limit = -1, deflt = -1;
59     int fd;
60     char *userlist = 0, *user, **users, *p;
61     char stroptsw[] = "-X";
62     short status;
63     struct user_priority *ppri_tbl, *ld_priority_file();
64     extern char *optarg;
65     extern int optind, opterr, optopt, errno;
66 
67     setlocale(LC_ALL, "");
68 
69 #if !defined(TEXT_DOMAIN)
70 #define TEXT_DOMAIN "SYS_TEST"
71 #endif
72     (void) textdomain(TEXT_DOMAIN);
73 
74 
75     if(argc == 1) {
76 usage:
77 	(void) printf(gettext("usage: \n"));
78   	(void) printf(gettext("(assign priority limit to users)\n"));
79 	(void) printf(gettext("\tlpusers -q priority -u user-list\n"));
80 
81   	(void) printf(gettext(
82 		"(assign default priority limit for balance of users)\n"));
83 	(void) printf(gettext("\tlpusers -q priority\n"));
84 
85   	(void) printf(gettext("(put users back to default priority limit)\n"));
86 	(void) printf(gettext("\tlpusers -u user-list\n"));
87 
88   	(void) printf(gettext("(assign default priority)\n"));
89 	(void) printf(gettext("\tlpusers -d priority\n"));
90 
91   	(void) printf(gettext("(examine priority limits, defaults)\n"));
92 	(void) printf(gettext("\tlpusers -l\n"));
93 
94 	exit(argc == 1);
95     }
96 
97     opterr = 0; /* disable printing of errors by getopt */
98     while ((c = getopt(argc, argv, "ld:q:u:")) != -1)
99 	switch(c) {
100 	case 'l':
101 	    if (list)
102 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'l');
103 	    list = TRUE;
104 	    break;
105 	case 'd':
106 	    if (deflt != -1)
107 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'd');
108 	    deflt = (int)strtol(optarg,&p,10);
109 	    if (*p || deflt<PRI_MIN || deflt>PRI_MAX) {
110 		LP_ERRMSG1(ERROR, E_LP_BADPRI, optarg);
111 		exit(1);
112 	    }
113 	    break;
114 	case 'q':
115 	    if (limit != -1)
116 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'q');
117 	    limit = (int)strtol(optarg,&p,10);
118 	    if (*p || limit<PRI_MIN || limit>PRI_MAX) {
119 		LP_ERRMSG1(ERROR, E_LP_BADPRI, optarg);
120 		exit(1);
121 	    }
122 	    break;
123 	case 'u':
124 	    if (userlist)
125 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'u');
126 	    userlist = optarg;
127 	    break;
128 	case '?':
129 	    if (optopt == '?')
130 		    goto usage;
131 	    stroptsw[1] = optopt;
132 	    if (strchr("ldqu", optopt))
133 		LP_ERRMSG1(ERROR, E_LP_OPTARG, stroptsw);
134 	    else
135 		LP_ERRMSG1(ERROR, E_LP_OPTION, stroptsw);
136 	    exit(1);
137 	}
138 
139     if (optind < argc) {
140 	LP_ERRMSG1(ERROR, E_LP_EXTRA, argv[optind]);
141 	exit(1);
142     }
143 
144     if (((list || deflt != -1) && (limit != -1 || userlist))
145 	|| (list && deflt != -1)) {
146 	LP_ERRMSG(ERROR, E_LP_OPTCOMB);
147 	/* invalid combination of options */
148 	exit(1);
149     }
150 
151     PRIORITY = Lp_Users;
152 
153     /* load existing priorities from file */
154     if (!(ppri_tbl = ld_priority_file(PRIORITY))) {
155 	switch (errno) {
156 	case EBADF:
157 	    LP_ERRMSG1(ERROR, E_LPU_BADFORM, PRIORITY);
158 	    break;
159 	default:
160 	    LP_ERRMSG2(ERROR, E_LPU_BADFILE, PRIORITY, errno);
161 	}
162 	exit(1);
163     }
164 
165     if (list) {
166 	print_tbl(ppri_tbl);
167 	exit (0);
168     } else {
169 	if (userlist) {
170 	    users = getlist(userlist, " \t", ",");
171 	    if (users)
172 		while (user = *users++) {
173 		    if (del_user(ppri_tbl, user) && (limit == -1))
174 			LP_ERRMSG1(WARNING, E_LPU_NOUSER, user);
175 		    if (limit != -1) {
176 			if (add_user(ppri_tbl, user, limit))
177 			    LP_ERRMSG1(WARNING, E_LPU_BADU, user);
178 		    }
179 		}
180 	} else if (deflt != -1)
181 	    ppri_tbl->deflt = deflt;
182 	else
183 	    ppri_tbl->deflt_limit = limit;
184 
185 	if ((fd = open_locked(PRIORITY, "w", LPU_MODE)) < 0) {
186 	    LP_ERRMSG1(ERROR, E_LP_ACCESS, PRIORITY);
187 	    exit(1);
188 	}
189 	output_tbl(fd, ppri_tbl);
190 	close(fd);
191     }
192 
193     if (mopen()) /* error on mopen == no spooler, exit quietly */
194 	exit(0);
195 
196     (void)putmessage (message, S_LOAD_USER_FILE);
197 
198     if (msend(message))
199 	goto Error;
200     if (mrecv(reply, sizeof(reply)) == -1)
201 	goto Error;
202     mtype = getmessage(reply, R_LOAD_USER_FILE, &status);
203     if (mtype != R_LOAD_USER_FILE) {
204 	LP_ERRMSG1 (ERROR, E_LP_BADREPLY, mtype);
205 	goto NoError;
206     }
207 
208     if (status == 0)
209 	goto NoError;
210 
211 Error:	LP_ERRMSG (ERROR, E_LPU_NOLOAD);
212 
213 NoError:(void)mclose ();
214     return (0);
215 }
216