xref: /illumos-gate/usr/src/cmd/lp/cmd/lpusers.c (revision 55fea89d)
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 /*
23f928ce67Sceastha  * 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 /* lpusers [-q priority-level] -u (user-list | "")
317c478bd9Sstevel@tonic-gate    lpusers -d priority-level
327c478bd9Sstevel@tonic-gate    lpusers -l
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <locale.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "lp.h"
417c478bd9Sstevel@tonic-gate #include "users.h"
427c478bd9Sstevel@tonic-gate #include "msgs.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	WHO_AM_I	I_AM_LPUSERS
457c478bd9Sstevel@tonic-gate #include "oam.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate char message[100],
487c478bd9Sstevel@tonic-gate      reply[100];
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate char	*PRIORITY;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate int add_user(), del_user();
537c478bd9Sstevel@tonic-gate 
54f928ce67Sceastha int
main(int argc,char * argv[])55f928ce67Sceastha main(int argc, char *argv[])
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate     int mtype, size, c,
587c478bd9Sstevel@tonic-gate 	list = FALSE, limit = -1, deflt = -1;
597c478bd9Sstevel@tonic-gate     int fd;
607c478bd9Sstevel@tonic-gate     char *userlist = 0, *user, **users, *p;
61268ffd3aSRichard Lowe     char stroptsw[] = "-X";
627c478bd9Sstevel@tonic-gate     short status;
637c478bd9Sstevel@tonic-gate     struct user_priority *ppri_tbl, *ld_priority_file();
647c478bd9Sstevel@tonic-gate     extern char *optarg;
657c478bd9Sstevel@tonic-gate     extern int optind, opterr, optopt, errno;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate     setlocale(LC_ALL, "");
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
707c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate     (void) textdomain(TEXT_DOMAIN);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate     if(argc == 1) {
767c478bd9Sstevel@tonic-gate usage:
77*55fea89dSDan Cross 	(void) printf(gettext("usage: \n"));
787c478bd9Sstevel@tonic-gate   	(void) printf(gettext("(assign priority limit to users)\n"));
797c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\tlpusers -q priority -u user-list\n"));
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate   	(void) printf(gettext(
827c478bd9Sstevel@tonic-gate 		"(assign default priority limit for balance of users)\n"));
837c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\tlpusers -q priority\n"));
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate   	(void) printf(gettext("(put users back to default priority limit)\n"));
867c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\tlpusers -u user-list\n"));
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate   	(void) printf(gettext("(assign default priority)\n"));
897c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\tlpusers -d priority\n"));
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate   	(void) printf(gettext("(examine priority limits, defaults)\n"));
927c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\tlpusers -l\n"));
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	exit(argc == 1);
957c478bd9Sstevel@tonic-gate     }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate     opterr = 0; /* disable printing of errors by getopt */
987c478bd9Sstevel@tonic-gate     while ((c = getopt(argc, argv, "ld:q:u:")) != -1)
997c478bd9Sstevel@tonic-gate 	switch(c) {
1007c478bd9Sstevel@tonic-gate 	case 'l':
1017c478bd9Sstevel@tonic-gate 	    if (list)
1027c478bd9Sstevel@tonic-gate 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'l');
1037c478bd9Sstevel@tonic-gate 	    list = TRUE;
1047c478bd9Sstevel@tonic-gate 	    break;
1057c478bd9Sstevel@tonic-gate 	case 'd':
1067c478bd9Sstevel@tonic-gate 	    if (deflt != -1)
1077c478bd9Sstevel@tonic-gate 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'd');
1087c478bd9Sstevel@tonic-gate 	    deflt = (int)strtol(optarg,&p,10);
1097c478bd9Sstevel@tonic-gate 	    if (*p || deflt<PRI_MIN || deflt>PRI_MAX) {
1107c478bd9Sstevel@tonic-gate 		LP_ERRMSG1(ERROR, E_LP_BADPRI, optarg);
1117c478bd9Sstevel@tonic-gate 		exit(1);
1127c478bd9Sstevel@tonic-gate 	    }
1137c478bd9Sstevel@tonic-gate 	    break;
1147c478bd9Sstevel@tonic-gate 	case 'q':
1157c478bd9Sstevel@tonic-gate 	    if (limit != -1)
1167c478bd9Sstevel@tonic-gate 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'q');
1177c478bd9Sstevel@tonic-gate 	    limit = (int)strtol(optarg,&p,10);
1187c478bd9Sstevel@tonic-gate 	    if (*p || limit<PRI_MIN || limit>PRI_MAX) {
1197c478bd9Sstevel@tonic-gate 		LP_ERRMSG1(ERROR, E_LP_BADPRI, optarg);
1207c478bd9Sstevel@tonic-gate 		exit(1);
1217c478bd9Sstevel@tonic-gate 	    }
1227c478bd9Sstevel@tonic-gate 	    break;
1237c478bd9Sstevel@tonic-gate 	case 'u':
1247c478bd9Sstevel@tonic-gate 	    if (userlist)
1257c478bd9Sstevel@tonic-gate 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'u');
1267c478bd9Sstevel@tonic-gate 	    userlist = optarg;
1277c478bd9Sstevel@tonic-gate 	    break;
1287c478bd9Sstevel@tonic-gate 	case '?':
129268ffd3aSRichard Lowe 	    if (optopt == '?')
130268ffd3aSRichard Lowe 		    goto usage;
131268ffd3aSRichard Lowe 	    stroptsw[1] = optopt;
1327c478bd9Sstevel@tonic-gate 	    if (strchr("ldqu", optopt))
133268ffd3aSRichard Lowe 		LP_ERRMSG1(ERROR, E_LP_OPTARG, stroptsw);
1347c478bd9Sstevel@tonic-gate 	    else
135268ffd3aSRichard Lowe 		LP_ERRMSG1(ERROR, E_LP_OPTION, stroptsw);
1367c478bd9Sstevel@tonic-gate 	    exit(1);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate     if (optind < argc) {
1407c478bd9Sstevel@tonic-gate 	LP_ERRMSG1(ERROR, E_LP_EXTRA, argv[optind]);
1417c478bd9Sstevel@tonic-gate 	exit(1);
1427c478bd9Sstevel@tonic-gate     }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate     if (((list || deflt != -1) && (limit != -1 || userlist))
1457c478bd9Sstevel@tonic-gate 	|| (list && deflt != -1)) {
1467c478bd9Sstevel@tonic-gate 	LP_ERRMSG(ERROR, E_LP_OPTCOMB);
1477c478bd9Sstevel@tonic-gate 	/* invalid combination of options */
1487c478bd9Sstevel@tonic-gate 	exit(1);
1497c478bd9Sstevel@tonic-gate     }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate     PRIORITY = Lp_Users;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate     /* load existing priorities from file */
1547c478bd9Sstevel@tonic-gate     if (!(ppri_tbl = ld_priority_file(PRIORITY))) {
1557c478bd9Sstevel@tonic-gate 	switch (errno) {
1567c478bd9Sstevel@tonic-gate 	case EBADF:
1577c478bd9Sstevel@tonic-gate 	    LP_ERRMSG1(ERROR, E_LPU_BADFORM, PRIORITY);
1587c478bd9Sstevel@tonic-gate 	    break;
1597c478bd9Sstevel@tonic-gate 	default:
1607c478bd9Sstevel@tonic-gate 	    LP_ERRMSG2(ERROR, E_LPU_BADFILE, PRIORITY, errno);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	exit(1);
1637c478bd9Sstevel@tonic-gate     }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate     if (list) {
1667c478bd9Sstevel@tonic-gate 	print_tbl(ppri_tbl);
1677c478bd9Sstevel@tonic-gate 	exit (0);
1687c478bd9Sstevel@tonic-gate     } else {
1697c478bd9Sstevel@tonic-gate 	if (userlist) {
1707c478bd9Sstevel@tonic-gate 	    users = getlist(userlist, " \t", ",");
1717c478bd9Sstevel@tonic-gate 	    if (users)
1727c478bd9Sstevel@tonic-gate 		while (user = *users++) {
1737c478bd9Sstevel@tonic-gate 		    if (del_user(ppri_tbl, user) && (limit == -1))
1747c478bd9Sstevel@tonic-gate 			LP_ERRMSG1(WARNING, E_LPU_NOUSER, user);
1757c478bd9Sstevel@tonic-gate 		    if (limit != -1) {
1767c478bd9Sstevel@tonic-gate 			if (add_user(ppri_tbl, user, limit))
1777c478bd9Sstevel@tonic-gate 			    LP_ERRMSG1(WARNING, E_LPU_BADU, user);
1787c478bd9Sstevel@tonic-gate 		    }
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 	} else if (deflt != -1)
1817c478bd9Sstevel@tonic-gate 	    ppri_tbl->deflt = deflt;
1827c478bd9Sstevel@tonic-gate 	else
1837c478bd9Sstevel@tonic-gate 	    ppri_tbl->deflt_limit = limit;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(PRIORITY, "w", LPU_MODE)) < 0) {
1867c478bd9Sstevel@tonic-gate 	    LP_ERRMSG1(ERROR, E_LP_ACCESS, PRIORITY);
1877c478bd9Sstevel@tonic-gate 	    exit(1);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	output_tbl(fd, ppri_tbl);
1907c478bd9Sstevel@tonic-gate 	close(fd);
1917c478bd9Sstevel@tonic-gate     }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate     if (mopen()) /* error on mopen == no spooler, exit quietly */
1947c478bd9Sstevel@tonic-gate 	exit(0);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate     (void)putmessage (message, S_LOAD_USER_FILE);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate     if (msend(message))
1997c478bd9Sstevel@tonic-gate 	goto Error;
2007c478bd9Sstevel@tonic-gate     if (mrecv(reply, sizeof(reply)) == -1)
2017c478bd9Sstevel@tonic-gate 	goto Error;
2027c478bd9Sstevel@tonic-gate     mtype = getmessage(reply, R_LOAD_USER_FILE, &status);
2037c478bd9Sstevel@tonic-gate     if (mtype != R_LOAD_USER_FILE) {
2047c478bd9Sstevel@tonic-gate 	LP_ERRMSG1 (ERROR, E_LP_BADREPLY, mtype);
2057c478bd9Sstevel@tonic-gate 	goto NoError;
2067c478bd9Sstevel@tonic-gate     }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate     if (status == 0)
2097c478bd9Sstevel@tonic-gate 	goto NoError;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate Error:	LP_ERRMSG (ERROR, E_LPU_NOLOAD);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate NoError:(void)mclose ();
214f928ce67Sceastha     return (0);
2157c478bd9Sstevel@tonic-gate }
216