xref: /illumos-gate/usr/src/cmd/nlsadmin/nlsadmin.c (revision 67e41408)
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 /*
23bdcaf822Sbasabi  * 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 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * nlsadmin.c -- control program for the network listener service
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * This program replaces a previous version of nlsadmin.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * This version of nlsadmin works with the service access facility to
397c478bd9Sstevel@tonic-gate  * control the network listener.  The functionality of the SVR3.2 nlsadmin
407c478bd9Sstevel@tonic-gate  * command is supported through calls to the more general sacadm and pmadm
417c478bd9Sstevel@tonic-gate  * commands available through SAF.  Users should migrate away from nlsadmin
427c478bd9Sstevel@tonic-gate  * to sacadm and pmadm for these functions.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * The -m option of the SVR3.2 nlsadmin command is now ignored.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * The -t option associates an address with service code 1 (same as in SVR3.2).
477c478bd9Sstevel@tonic-gate  * The -l option associates an address with service code 0.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * nlsadmin also contains new functionality -- the ability to format a
507c478bd9Sstevel@tonic-gate  * "listener-specific" string to put in the _pmtab database.  This
517c478bd9Sstevel@tonic-gate  * functionality is required by SAF.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
557c478bd9Sstevel@tonic-gate #include <sys/stat.h>
567c478bd9Sstevel@tonic-gate #include <stdio.h>
57bdcaf822Sbasabi #include <stdlib.h>
587c478bd9Sstevel@tonic-gate #include <ctype.h>
597c478bd9Sstevel@tonic-gate #include <errno.h>
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <sac.h>
627c478bd9Sstevel@tonic-gate #include "nlsadmin.h"
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define OPTIONS	"a:c:d:e:ikl:mo:p:qr:st:vw:xy:z:A:N:VDR:"
657c478bd9Sstevel@tonic-gate #ifndef FALSE
667c478bd9Sstevel@tonic-gate #define TRUE	1
677c478bd9Sstevel@tonic-gate #define FALSE	0
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * defines for -q exit codes: QZERO is used for conditions that the
717c478bd9Sstevel@tonic-gate  * man page documents as returning 0, QONE for those that return 1
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate #define QZERO	0
747c478bd9Sstevel@tonic-gate #define QONE	1
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * defines for simulated standard error format code
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate #define MM_NOSEV        0
807c478bd9Sstevel@tonic-gate #define MM_HALT         1
817c478bd9Sstevel@tonic-gate #define MM_ERROR        2
827c478bd9Sstevel@tonic-gate #define MM_WARNING      3
837c478bd9Sstevel@tonic-gate #define MM_INFO         4
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate char	*Nlsname;		/* set to argv[0]			*/
867c478bd9Sstevel@tonic-gate char	Label[25];		/* label component for fmtmsg		*/
877c478bd9Sstevel@tonic-gate int	Quietflag = FALSE;	/* set to TRUE when -q used		*/
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate extern	int errno;
907c478bd9Sstevel@tonic-gate void	nlsmesg();
917c478bd9Sstevel@tonic-gate uid_t	geteuid();
927c478bd9Sstevel@tonic-gate char	*nexttok();
937c478bd9Sstevel@tonic-gate char	*pflags();
947c478bd9Sstevel@tonic-gate char	*gencmdstr();
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate struct	svcfields {
977c478bd9Sstevel@tonic-gate 	char	*pmtag;
987c478bd9Sstevel@tonic-gate 	char	*pmtype;
997c478bd9Sstevel@tonic-gate 	char	*svc_code;
1007c478bd9Sstevel@tonic-gate 	char	*flags;
1017c478bd9Sstevel@tonic-gate 	char	*id;
1027c478bd9Sstevel@tonic-gate 	char	*res1;
1037c478bd9Sstevel@tonic-gate 	char	*res2;
1047c478bd9Sstevel@tonic-gate 	char	*res3;
1057c478bd9Sstevel@tonic-gate 	char	*addr;
1067c478bd9Sstevel@tonic-gate 	char	*rpc;
1077c478bd9Sstevel@tonic-gate 	char	*lflags;
1087c478bd9Sstevel@tonic-gate 	char	*modules;
1097c478bd9Sstevel@tonic-gate 	char	*command;
1107c478bd9Sstevel@tonic-gate 	char	*comment;
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
113bdcaf822Sbasabi void no_permission(void) __NORETURN;
114bdcaf822Sbasabi void usage(int flag);
1157c478bd9Sstevel@tonic-gate 
116bdcaf822Sbasabi int
main(int argc,char ** argv)117bdcaf822Sbasabi main(int argc, char **argv)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	extern	char *optarg;
1207c478bd9Sstevel@tonic-gate 	extern	int optind;
1217c478bd9Sstevel@tonic-gate 	int	c;			/* used for return from getopt  */
1227c478bd9Sstevel@tonic-gate 	char	*addrptr = NULL;	/* set when -A address is used	*/
1237c478bd9Sstevel@tonic-gate 	char	*rpcptr = NULL;		/* set when -R rpcinfo is used	*/
1247c478bd9Sstevel@tonic-gate 	char	*cmdptr = NULL;		/* set with -c command		*/
1257c478bd9Sstevel@tonic-gate 	char	*comptr = NULL;		/* set with -y comment (old)	*/
1267c478bd9Sstevel@tonic-gate 	char	*idptr = NULL;		/* set with -w id (old)		*/
1277c478bd9Sstevel@tonic-gate 	char	*lptr = NULL;		/* set with -l addr (old)	*/
1287c478bd9Sstevel@tonic-gate 	char	*moduleptr = NULL;	/* set with -m modules		*/
1297c478bd9Sstevel@tonic-gate 	char	*pipeptr = NULL;	/* set with -o pipe		*/
1307c478bd9Sstevel@tonic-gate 	char	*svcptr = NULL;		/* set when service code used (old) */
1317c478bd9Sstevel@tonic-gate 	char	*tptr = NULL;		/* set when -t addr used (old)	*/
1327c478bd9Sstevel@tonic-gate 	char	*netspec = NULL;	/* set to the network specification */
1337c478bd9Sstevel@tonic-gate 	int	flag = 0;		/* bit flag of type of command	*/
1347c478bd9Sstevel@tonic-gate 	int	exitcode = 0;		/* exit status of this command	*/
1357c478bd9Sstevel@tonic-gate 	int	lflags = 0;		/* listener flags		*/
1367c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];		/* temp buffer #1		*/
1377c478bd9Sstevel@tonic-gate 	char	mesg[BUFSIZ];		/* temp buffer #2		*/
1387c478bd9Sstevel@tonic-gate 	FILE	*fp;			/* used for checking netspec	*/
1397c478bd9Sstevel@tonic-gate 	char	*ptr;			/* temp pointer			*/
1407c478bd9Sstevel@tonic-gate 	char	*ptr2;			/* temp pointer			*/
1417c478bd9Sstevel@tonic-gate 	int	sawsep = 0;		/* flag for RPC separator	*/
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	Nlsname = argv[0];
1447c478bd9Sstevel@tonic-gate 	sprintf(Label, "UX:%.14s", argv[0]);	/* for standard message fmt */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, OPTIONS)) != -1) {
1477c478bd9Sstevel@tonic-gate 		switch (c) {
1487c478bd9Sstevel@tonic-gate 		case 'a':
1497c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != CMDFLAG)) || svcptr || Quietflag
1507c478bd9Sstevel@tonic-gate 			      || addrptr || rpcptr || lflags)
1517c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
1527c478bd9Sstevel@tonic-gate 			svcptr = optarg;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 'c':
1557c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != CMDFLAG)) || cmdptr || Quietflag )
1567c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
1577c478bd9Sstevel@tonic-gate 			cmdptr = optarg;
1587c478bd9Sstevel@tonic-gate 			flag |= CMDFLAG;
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 		case 'd':
1617c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || comptr || addrptr
1627c478bd9Sstevel@tonic-gate 			     || rpcptr || cmdptr || idptr || lflags )
1637c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
1647c478bd9Sstevel@tonic-gate 			svcptr = optarg;
1657c478bd9Sstevel@tonic-gate 			flag |= DISFLAG;
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		case 'e':
1687c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || comptr || addrptr
1697c478bd9Sstevel@tonic-gate 			     || rpcptr || cmdptr || idptr || lflags )
1707c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
1717c478bd9Sstevel@tonic-gate 			svcptr = optarg;
1727c478bd9Sstevel@tonic-gate 			flag |= ENAFLAG;
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		case 'i':
1757c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || comptr || addrptr
1767c478bd9Sstevel@tonic-gate 			     || rpcptr || cmdptr || idptr || lflags )
1777c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
1787c478bd9Sstevel@tonic-gate 			flag |= INIFLAG;
1797c478bd9Sstevel@tonic-gate 			break;
1807c478bd9Sstevel@tonic-gate 		case 'k':
1817c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || comptr || addrptr
1827c478bd9Sstevel@tonic-gate 			     || rpcptr || cmdptr || idptr || lflags )
1837c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
1847c478bd9Sstevel@tonic-gate 			flag |= KILFLAG;
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 		case 'l':
1877c478bd9Sstevel@tonic-gate 			if ( ( flag && (flag != ADRFLAG)) || svcptr || lptr
1887c478bd9Sstevel@tonic-gate 			      || Quietflag || comptr || addrptr || rpcptr
1897c478bd9Sstevel@tonic-gate 			      || cmdptr || idptr || lflags )
1907c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
1917c478bd9Sstevel@tonic-gate 			lptr = optarg;
1927c478bd9Sstevel@tonic-gate 			flag |= ADRFLAG;
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		case 'm':
1957c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != CMDFLAG)) || Quietflag || rpcptr || lflags )
1967c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
1977c478bd9Sstevel@tonic-gate 			flag |= CMDFLAG;
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 		case 'o':
2007c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || comptr || idptr || netspec )
2017c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2027c478bd9Sstevel@tonic-gate 			pipeptr = optarg;
2037c478bd9Sstevel@tonic-gate 			flag |= PIPFLAG;
2047c478bd9Sstevel@tonic-gate 			break;
2057c478bd9Sstevel@tonic-gate 		case 'p':
2067c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != CMDFLAG) && (flag != PIPFLAG)) || Quietflag )
2077c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2087c478bd9Sstevel@tonic-gate 			moduleptr = optarg;
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 		case 'q':
2117c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != ZZZFLAG)) || Quietflag || comptr
2127c478bd9Sstevel@tonic-gate 			     || rpcptr || lflags || idptr )
2137c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2147c478bd9Sstevel@tonic-gate 			Quietflag = TRUE;
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 		case 'r':
2177c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || comptr || addrptr
2187c478bd9Sstevel@tonic-gate 			     || rpcptr || cmdptr || idptr || lflags )
2197c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2207c478bd9Sstevel@tonic-gate 			flag |= REMFLAG;
2217c478bd9Sstevel@tonic-gate 			svcptr = optarg;
2227c478bd9Sstevel@tonic-gate 			break;
2237c478bd9Sstevel@tonic-gate 		case 's':
2247c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || comptr || addrptr
2257c478bd9Sstevel@tonic-gate 			     || rpcptr || cmdptr || idptr || lflags )
2267c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2277c478bd9Sstevel@tonic-gate 			flag |= STAFLAG;
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 		case 't':
2307c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != ADRFLAG)) || svcptr || tptr
2317c478bd9Sstevel@tonic-gate 			      || Quietflag || comptr || addrptr || rpcptr
2327c478bd9Sstevel@tonic-gate 			      || cmdptr || idptr || lflags )
2337c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2347c478bd9Sstevel@tonic-gate 			tptr = optarg;
2357c478bd9Sstevel@tonic-gate 			flag |= ADRFLAG;
2367c478bd9Sstevel@tonic-gate 			break;
2377c478bd9Sstevel@tonic-gate 		case 'v':
2387c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || comptr || rpcptr
2397c478bd9Sstevel@tonic-gate 			     || addrptr || idptr || lflags )
2407c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2417c478bd9Sstevel@tonic-gate 			flag |= VBSFLAG;
2427c478bd9Sstevel@tonic-gate 			break;
2437c478bd9Sstevel@tonic-gate 		case 'w':
2447c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != CMDFLAG)) || Quietflag || idptr
2457c478bd9Sstevel@tonic-gate 			     || rpcptr || addrptr || lflags )
2467c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2477c478bd9Sstevel@tonic-gate 			idptr = optarg;
2487c478bd9Sstevel@tonic-gate 			break;
2497c478bd9Sstevel@tonic-gate 		case 'x':
2507c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || netspec || comptr
2517c478bd9Sstevel@tonic-gate 			     || rpcptr || addrptr || lflags || idptr )
2527c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2537c478bd9Sstevel@tonic-gate 			flag |= NETFLAG;
2547c478bd9Sstevel@tonic-gate 			break;
2557c478bd9Sstevel@tonic-gate 		case 'y':
2567c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != CMDFLAG)) || Quietflag || comptr
2577c478bd9Sstevel@tonic-gate 			     || rpcptr || addrptr || lflags )
2587c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2597c478bd9Sstevel@tonic-gate 			comptr = optarg;
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 		case 'z':
2627c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || comptr || addrptr || rpcptr
2637c478bd9Sstevel@tonic-gate 			     || idptr || lflags )
2647c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2657c478bd9Sstevel@tonic-gate 			flag |= ZZZFLAG;
2667c478bd9Sstevel@tonic-gate 			svcptr = optarg;
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 		case 'A':
2697c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != CMDFLAG) && (flag != PIPFLAG))
2707c478bd9Sstevel@tonic-gate 			     || netspec || svcptr || idptr || comptr )
2717c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2727c478bd9Sstevel@tonic-gate 			addrptr = optarg;
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 		case 'D':
2757c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != CMDFLAG) && (flag != PIPFLAG))
2767c478bd9Sstevel@tonic-gate 			     || netspec || svcptr || idptr || comptr || addrptr
2777c478bd9Sstevel@tonic-gate 			     || lflags )
2787c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2797c478bd9Sstevel@tonic-gate 			lflags |= DFLAG;
2807c478bd9Sstevel@tonic-gate 			break;
2817c478bd9Sstevel@tonic-gate 		case 'N':
2827c478bd9Sstevel@tonic-gate 			if ( netspec )
2837c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2847c478bd9Sstevel@tonic-gate 			netspec = optarg;
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 		case 'R':
2877c478bd9Sstevel@tonic-gate 			if ( (flag && (flag != CMDFLAG) && (flag != PIPFLAG))
2887c478bd9Sstevel@tonic-gate 			     || netspec || svcptr || idptr || comptr )
2897c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
2907c478bd9Sstevel@tonic-gate 			for (ptr = optarg; *ptr; ++ptr) {
2917c478bd9Sstevel@tonic-gate 				if ((*ptr == ':') && !sawsep) {
2927c478bd9Sstevel@tonic-gate 					/*
2937c478bd9Sstevel@tonic-gate 					 * skip separator - note that if
2947c478bd9Sstevel@tonic-gate 					 * separator has been seen, it's not
2957c478bd9Sstevel@tonic-gate 					 * a digit so it will generate a usage
2967c478bd9Sstevel@tonic-gate 					 * message below like we want
2977c478bd9Sstevel@tonic-gate 					 */
2987c478bd9Sstevel@tonic-gate 					sawsep++;
2997c478bd9Sstevel@tonic-gate 					continue;
3007c478bd9Sstevel@tonic-gate 				}
3017c478bd9Sstevel@tonic-gate 				if (!isdigit(*ptr))
3027c478bd9Sstevel@tonic-gate 					usage(USAGE);
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 			ptr = strchr(optarg, ':');
3057c478bd9Sstevel@tonic-gate 			if (ptr)
3067c478bd9Sstevel@tonic-gate 				/* change the ':' to a ',' */
3077c478bd9Sstevel@tonic-gate 				*ptr = ',';
3087c478bd9Sstevel@tonic-gate 			else
3097c478bd9Sstevel@tonic-gate 				usage(USAGE);
3107c478bd9Sstevel@tonic-gate 			rpcptr = optarg;
3117c478bd9Sstevel@tonic-gate 			break;
3127c478bd9Sstevel@tonic-gate 		case 'V':
3137c478bd9Sstevel@tonic-gate 			if ( flag || svcptr || Quietflag || comptr || netspec
3147c478bd9Sstevel@tonic-gate 			     || rpcptr || addrptr || idptr || lflags )
3157c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
3167c478bd9Sstevel@tonic-gate 			flag |= VERFLAG;
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 		case '?':
3197c478bd9Sstevel@tonic-gate 			usage(USAGE);
3207c478bd9Sstevel@tonic-gate 		}
321bdcaf822Sbasabi 		/* NOTREACHED */
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if ((optind < argc) && ! netspec)
3257c478bd9Sstevel@tonic-gate 		netspec = argv[optind++];
3267c478bd9Sstevel@tonic-gate 	if (optind < argc)
3277c478bd9Sstevel@tonic-gate 		usage(USAGE);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/* determine if this command requires a netspec */
3317c478bd9Sstevel@tonic-gate 	if (flag != CMDFLAG) {
3327c478bd9Sstevel@tonic-gate 		/* if flag is CMDFLAG, more complicated checking of netspec
3337c478bd9Sstevel@tonic-gate 		 * is done below in switch
3347c478bd9Sstevel@tonic-gate 		 */
3357c478bd9Sstevel@tonic-gate 		if ((flag == PIPFLAG || flag == VERFLAG || flag == NETFLAG)) {
3367c478bd9Sstevel@tonic-gate 			if (netspec)
3377c478bd9Sstevel@tonic-gate 				usage(USAGE);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 		else if (!netspec)
3407c478bd9Sstevel@tonic-gate 			usage(USAGE);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (netspec && (flag != INIFLAG)) {
3447c478bd9Sstevel@tonic-gate 		sprintf(buf, SAC_LSPM, netspec);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		if ((fp = popen(buf, "r")) == NULL) {
3477c478bd9Sstevel@tonic-gate 			nlsmesg(MM_ERROR, "System error");
3487c478bd9Sstevel@tonic-gate 			exit(NLS_SYSERR);
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		if (fgets(buf, BUFSIZ, fp) == NULL) {
3527c478bd9Sstevel@tonic-gate 			nlsmesg(MM_ERROR, "Invalid network specification");
3537c478bd9Sstevel@tonic-gate 			exit(NLS_BADPM);
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 		else {
3567c478bd9Sstevel@tonic-gate 			ptr = strchr(buf, ':');
3577c478bd9Sstevel@tonic-gate 			ptr++;
3587c478bd9Sstevel@tonic-gate 			ptr2 = strchr(ptr, ':');
359*67e41408SToomas Soome 			*ptr2 = '\0';
3607c478bd9Sstevel@tonic-gate 			if (strcmp(ptr, LISTENTYPE) != 0) {
3617c478bd9Sstevel@tonic-gate 				sprintf(mesg, "Network specification \"%s\" is not of type %s", netspec, LISTENTYPE);
3627c478bd9Sstevel@tonic-gate 				nlsmesg(MM_ERROR, mesg);
3637c478bd9Sstevel@tonic-gate 				exit(NLS_BADPM);
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		pclose(fp);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (svcptr) {
3717c478bd9Sstevel@tonic-gate 		/* check to see if service code is "correct" -- right range
3727c478bd9Sstevel@tonic-gate 		 * and format.  The -m flag is ignored, so no check for
3737c478bd9Sstevel@tonic-gate 		 * "administrative" service codes (0-100) is done.
3747c478bd9Sstevel@tonic-gate 		 */
3757c478bd9Sstevel@tonic-gate 		c = strlen(svcptr);
3767c478bd9Sstevel@tonic-gate 		if ((c == 0) || (c >= SVC_CODE_SZ)) {
3777c478bd9Sstevel@tonic-gate 			sprintf(mesg, "Service code contains more than %d characters", SVC_CODE_SZ);
3787c478bd9Sstevel@tonic-gate 			nlsmesg(MM_ERROR, mesg);
3797c478bd9Sstevel@tonic-gate 			exit(NLS_SERV);
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	switch (flag) {
3847c478bd9Sstevel@tonic-gate 	default:
3857c478bd9Sstevel@tonic-gate 		usage(USAGE);
3867c478bd9Sstevel@tonic-gate 		break;
3877c478bd9Sstevel@tonic-gate 	case NONE:
3887c478bd9Sstevel@tonic-gate 		if ( svcptr || comptr || rpcptr || lflags || idptr )
3897c478bd9Sstevel@tonic-gate 			usage(INCONSISTENT);
3907c478bd9Sstevel@tonic-gate 		exitcode = prt_nets(netspec);
3917c478bd9Sstevel@tonic-gate 		break;
3927c478bd9Sstevel@tonic-gate 	case INIFLAG:
3937c478bd9Sstevel@tonic-gate 		if (geteuid() != ROOT)
3947c478bd9Sstevel@tonic-gate 			no_permission();
3957c478bd9Sstevel@tonic-gate 		exitcode = add_pm(netspec);
3967c478bd9Sstevel@tonic-gate 		break;
3977c478bd9Sstevel@tonic-gate 	case CMDFLAG:
3987c478bd9Sstevel@tonic-gate 		if ( svcptr || comptr || idptr || netspec ) {
3997c478bd9Sstevel@tonic-gate 			if (geteuid() != ROOT)
4007c478bd9Sstevel@tonic-gate 				no_permission();
4017c478bd9Sstevel@tonic-gate 			if ((exitcode = old_addsvc(svcptr, "", cmdptr, comptr, moduleptr, idptr, NULL, netspec)) != NLS_OK)
4027c478bd9Sstevel@tonic-gate 				switch (exitcode) {
4037c478bd9Sstevel@tonic-gate 				case NLS_SERV:
4047c478bd9Sstevel@tonic-gate 					nlsmesg(MM_ERROR, "Service code already exists");
4057c478bd9Sstevel@tonic-gate 					break;
4067c478bd9Sstevel@tonic-gate 				default:
4077c478bd9Sstevel@tonic-gate 					nlsmesg(MM_ERROR, "Could not add service");
4087c478bd9Sstevel@tonic-gate 					break;
4097c478bd9Sstevel@tonic-gate 				}
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 		else {
4127c478bd9Sstevel@tonic-gate 			if (netspec)
4137c478bd9Sstevel@tonic-gate 				usage(INCONSISTENT);
4147c478bd9Sstevel@tonic-gate 			exitcode = prt_cmd(cmdptr, CFLAG | lflags, moduleptr, addrptr, rpcptr);
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 		break;
4177c478bd9Sstevel@tonic-gate 	case PIPFLAG:
4187c478bd9Sstevel@tonic-gate 		if (geteuid() != ROOT)
4197c478bd9Sstevel@tonic-gate 			no_permission();
4207c478bd9Sstevel@tonic-gate 		exitcode = prt_cmd(pipeptr, PFLAG | lflags, moduleptr, addrptr, rpcptr);
4217c478bd9Sstevel@tonic-gate 		break;
4227c478bd9Sstevel@tonic-gate 	case VERFLAG:
4237c478bd9Sstevel@tonic-gate 		printf("%d\n", VERSION);
4247c478bd9Sstevel@tonic-gate 		exit(NLS_OK);
4257c478bd9Sstevel@tonic-gate 		break;
4267c478bd9Sstevel@tonic-gate 	case DISFLAG:
4277c478bd9Sstevel@tonic-gate 		if (geteuid() != ROOT)
4287c478bd9Sstevel@tonic-gate 			no_permission();
4297c478bd9Sstevel@tonic-gate 		exitcode = disable_svc(svcptr, netspec);
4307c478bd9Sstevel@tonic-gate 		break;
4317c478bd9Sstevel@tonic-gate 	case ENAFLAG:
4327c478bd9Sstevel@tonic-gate 		if (geteuid() != ROOT)
4337c478bd9Sstevel@tonic-gate 			no_permission();
4347c478bd9Sstevel@tonic-gate 		exitcode = enable_svc(svcptr, netspec);
4357c478bd9Sstevel@tonic-gate 		break;
4367c478bd9Sstevel@tonic-gate 	case KILFLAG:
4377c478bd9Sstevel@tonic-gate 		if (geteuid() != ROOT)
4387c478bd9Sstevel@tonic-gate 			no_permission();
4397c478bd9Sstevel@tonic-gate 		exitcode = kill_listener(netspec);
4407c478bd9Sstevel@tonic-gate 		break;
4417c478bd9Sstevel@tonic-gate 	case ADRFLAG:
4427c478bd9Sstevel@tonic-gate 		/* check for root permissions in setup_addr */
4437c478bd9Sstevel@tonic-gate 		exitcode = setup_addr(lptr, tptr, netspec);
4447c478bd9Sstevel@tonic-gate 		break;
4457c478bd9Sstevel@tonic-gate 	case REMFLAG:
4467c478bd9Sstevel@tonic-gate 		if (geteuid() != ROOT)
4477c478bd9Sstevel@tonic-gate 			no_permission();
4487c478bd9Sstevel@tonic-gate 		exitcode = remove_svc(svcptr, netspec, TRUE);
4497c478bd9Sstevel@tonic-gate 		break;
4507c478bd9Sstevel@tonic-gate 	case STAFLAG:
4517c478bd9Sstevel@tonic-gate 		if (geteuid() != ROOT)
4527c478bd9Sstevel@tonic-gate 			no_permission();
4537c478bd9Sstevel@tonic-gate 		exitcode = start_listener(netspec);
4547c478bd9Sstevel@tonic-gate 		break;
4557c478bd9Sstevel@tonic-gate 	case VBSFLAG:
4567c478bd9Sstevel@tonic-gate 		exitcode = prt_svcs(NULL, netspec);
4577c478bd9Sstevel@tonic-gate 		break;
4587c478bd9Sstevel@tonic-gate 	case NETFLAG:
4597c478bd9Sstevel@tonic-gate 		exitcode = prt_nets(NULL);
4607c478bd9Sstevel@tonic-gate 		break;
4617c478bd9Sstevel@tonic-gate 	case ZZZFLAG:
4627c478bd9Sstevel@tonic-gate 		exitcode = prt_svcs(svcptr, netspec);
4637c478bd9Sstevel@tonic-gate 		break;
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 	if (exitcode == NLS_SYSERR)
4667c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "System error in SAC command");
467bdcaf822Sbasabi 	return (exitcode);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate static char umsg[] = "usage: %s -x\n\
4727c478bd9Sstevel@tonic-gate        %s [ options ] netspec\n\
4737c478bd9Sstevel@tonic-gate        %s [ options ] -N port_monitor_tag\n\
4747c478bd9Sstevel@tonic-gate        %s -V\n\
4757c478bd9Sstevel@tonic-gate        %s -c cmd | -o pipename [ -p modules ] [ -A addr | -D ] \\\n\
4767c478bd9Sstevel@tonic-gate           [ -R prognum:versnum ]\n\
4777c478bd9Sstevel@tonic-gate \n\
4787c478bd9Sstevel@tonic-gate        [ options ] are:\n\
4797c478bd9Sstevel@tonic-gate        [ -a svc_code -c \"cmd\" -y \"cmt\" [-p modules] [-w id] ]\n\
4807c478bd9Sstevel@tonic-gate        [-q] | [-v] | [-s] | [-k] | [-i] |\n\
4817c478bd9Sstevel@tonic-gate        [-e svc_code] | [-d svc_code] | [-r svc_code] | [[-q] -z svc_code]\n\
4827c478bd9Sstevel@tonic-gate        [[-l addr | -] [-t addr | -]] |\n\
4837c478bd9Sstevel@tonic-gate ";
4847c478bd9Sstevel@tonic-gate 
485bdcaf822Sbasabi void
usage(int flag)486bdcaf822Sbasabi usage(int flag)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	switch (flag) {
4897c478bd9Sstevel@tonic-gate 	case INCONSISTENT:
4907c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Inconsistent options");
4917c478bd9Sstevel@tonic-gate 		break;
4927c478bd9Sstevel@tonic-gate 	case MISSINGARG:
4937c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Missing argument");
4947c478bd9Sstevel@tonic-gate 		break;
4957c478bd9Sstevel@tonic-gate 	case USAGE:
4967c478bd9Sstevel@tonic-gate 		break;
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	fprintf(stderr, umsg, Nlsname, Nlsname, Nlsname, Nlsname, Nlsname);
4997c478bd9Sstevel@tonic-gate 	exit(NLS_CMD);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * no_permission:  print out error message and exit when the user needs to
5057c478bd9Sstevel@tonic-gate  *                 needs to be root and isn't.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate 
508bdcaf822Sbasabi void
no_permission(void)509bdcaf822Sbasabi no_permission(void)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	nlsmesg(MM_ERROR, "Must be super user");
5127c478bd9Sstevel@tonic-gate 	exit(NLS_PERM);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate  * nlsmesg:  print out either an error or a warning message.  severity must
5177c478bd9Sstevel@tonic-gate  *           be either MM_ERROR or MM_WARNING.  this routine will be converted
5187c478bd9Sstevel@tonic-gate  *           to use the standard message format later.
5197c478bd9Sstevel@tonic-gate  */
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate void
nlsmesg(int severity,char * text)522bdcaf822Sbasabi nlsmesg(int severity, char *text)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	int	class;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (severity == MM_ERROR)
5277c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: error: %s\n", Nlsname, text);
5287c478bd9Sstevel@tonic-gate 	else
5297c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: warning: %s\n", Nlsname, text);
5307c478bd9Sstevel@tonic-gate 	return;
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate  * prt_cmd:  print out the listener-dependent string for sacadm.
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate 
537bdcaf822Sbasabi int
prt_cmd(char * path,long flags,char * modules,char * addr,char * rpcp)538bdcaf822Sbasabi prt_cmd(char *path, long flags, char *modules, char *addr, char *rpcp)
539bdcaf822Sbasabi 	/* path: full path of command or pipe	*/
540bdcaf822Sbasabi 	/* flags: listener flags		*/
541bdcaf822Sbasabi 	/* PFLAG for pipe			*/
542bdcaf822Sbasabi 	/* CFLAG for command			*/
543bdcaf822Sbasabi 	/* DFLAG for dynamic addr		*/
544bdcaf822Sbasabi  	/* modules: STREAMS modules to push	*/
545bdcaf822Sbasabi 	/* addr: private address		*/
546bdcaf822Sbasabi 	/* rpcp: RPC prog and ver #		*/
5477c478bd9Sstevel@tonic-gate {
5487c478bd9Sstevel@tonic-gate 	struct	stat	sbuf;
5497c478bd9Sstevel@tonic-gate 	char	mesgbuf[BUFSIZ];
5507c478bd9Sstevel@tonic-gate 	char	*tmp;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	if (*path != '/') {
5537c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Must specify full path name");
5547c478bd9Sstevel@tonic-gate 		return(NLS_CMD);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if ((tmp = strchr(path, ' ')) != NULL)
558*67e41408SToomas Soome 		*tmp = '\0';
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (stat(path, &sbuf) < 0) {
5617c478bd9Sstevel@tonic-gate 		if (errno != EFAULT) {
5627c478bd9Sstevel@tonic-gate 			sprintf(mesgbuf, "%s does not exist", path);
5637c478bd9Sstevel@tonic-gate 			nlsmesg(MM_WARNING, mesgbuf);
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 		else
5667c478bd9Sstevel@tonic-gate 			return(NLS_SYSERR);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if (tmp)
5707c478bd9Sstevel@tonic-gate 		*tmp = ' ';
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	printf("%s:%s:%s:%s:%s\n", (addr ? addr : ""), (rpcp ? rpcp : ""),
5737c478bd9Sstevel@tonic-gate 		pflags(flags), (modules ? modules : ""), path);
5747c478bd9Sstevel@tonic-gate 	return(NLS_OK);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * old_addsvc:  use pmadm to add a service code to the listener.  this will
5797c478bd9Sstevel@tonic-gate  *              not allow specification of a private address -- use pmadm!
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate 
582bdcaf822Sbasabi int
old_addsvc(char * svc,char * addr,char * cmd,char * com,char * module,char * id,char * flags,char * netspec)583bdcaf822Sbasabi old_addsvc(char *svc, char *addr, char *cmd, char *com, char *module,
584bdcaf822Sbasabi     char *id, char *flags, char *netspec)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
5877c478bd9Sstevel@tonic-gate 	char	mesgbuf[BUFSIZ];
5887c478bd9Sstevel@tonic-gate 	int	rtn;
5897c478bd9Sstevel@tonic-gate 	struct	stat	sbuf;
5907c478bd9Sstevel@tonic-gate 	char	*tmp;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (!svc || !cmd || !com || !netspec)
5937c478bd9Sstevel@tonic-gate 		usage(MISSINGARG);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/* create "port-monitor specific" info in the same way as prt_cmd */
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (*cmd != '/') {
5987c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Must specify full path name");
5997c478bd9Sstevel@tonic-gate 		return(NLS_CMD);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	if ((tmp = strchr(cmd, ' ')) != NULL)
603*67e41408SToomas Soome 		*tmp = '\0';
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	if (stat(cmd, &sbuf) < 0) {
6067c478bd9Sstevel@tonic-gate 		if (errno != EFAULT) {
6077c478bd9Sstevel@tonic-gate 			sprintf(mesgbuf, "%s does not exist", cmd);
6087c478bd9Sstevel@tonic-gate 			nlsmesg(MM_WARNING, mesgbuf);
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 		else
6117c478bd9Sstevel@tonic-gate 			return(NLS_SYSERR);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (tmp)
6157c478bd9Sstevel@tonic-gate 		*tmp = ' ';
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (addr)
6187c478bd9Sstevel@tonic-gate 		sprintf(mesgbuf, "'%s::c:%s:%s'", addr, module ? module : "" , cmd);
6197c478bd9Sstevel@tonic-gate 	else
6207c478bd9Sstevel@tonic-gate 		sprintf(mesgbuf, "'::c:%s:%s'", module ? module : "" , cmd);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	if (flags && *flags)
6237c478bd9Sstevel@tonic-gate 		sprintf(buf, PM_ADDSVCF, netspec, svc, (id)?id:DEFAULTID, flags, mesgbuf, VERSION, com ? com : "");
6247c478bd9Sstevel@tonic-gate 	else
6257c478bd9Sstevel@tonic-gate 		sprintf(buf, PM_ADDSVC, netspec, svc, (id)?id:DEFAULTID, mesgbuf, VERSION, com ? com : "");
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	if ((rtn = system(buf)) < 0) {
6287c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 	rtn = (rtn>>8) & 0xff;	/* get child return value out of exit word */
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	switch (rtn) {
6337c478bd9Sstevel@tonic-gate 	case 0:
6347c478bd9Sstevel@tonic-gate 		return(NLS_OK);
6357c478bd9Sstevel@tonic-gate 		break;
6367c478bd9Sstevel@tonic-gate 	case E_BADARGS:
6377c478bd9Sstevel@tonic-gate 	case E_SAFERR:
6387c478bd9Sstevel@tonic-gate 	case E_SYSERR:
6397c478bd9Sstevel@tonic-gate 	case E_NOEXIST:
6407c478bd9Sstevel@tonic-gate 	case E_PMRUN:
6417c478bd9Sstevel@tonic-gate 	case E_PMNOTRUN:
6427c478bd9Sstevel@tonic-gate 	case E_RECOVER:
6437c478bd9Sstevel@tonic-gate 	case E_SACNOTRUN:
6447c478bd9Sstevel@tonic-gate 	default:
6457c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
6467c478bd9Sstevel@tonic-gate 		break;
6477c478bd9Sstevel@tonic-gate 	case E_DUP:
6487c478bd9Sstevel@tonic-gate 		return(NLS_SERV);
6497c478bd9Sstevel@tonic-gate 		break;
6507c478bd9Sstevel@tonic-gate 	case E_NOPRIV:
6517c478bd9Sstevel@tonic-gate 		no_permission();
6527c478bd9Sstevel@tonic-gate 		break;
6537c478bd9Sstevel@tonic-gate 	}
654bdcaf822Sbasabi 	/* NOTREACHED */
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate  * prt_nets:  print the status of one network, or all nets if netspec
6597c478bd9Sstevel@tonic-gate  *            is NULL
6607c478bd9Sstevel@tonic-gate  */
661bdcaf822Sbasabi int
prt_nets(char * netspec)662bdcaf822Sbasabi prt_nets(char *netspec)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
6657c478bd9Sstevel@tonic-gate 	FILE	*fp;
6667c478bd9Sstevel@tonic-gate 	char	*name;
6677c478bd9Sstevel@tonic-gate 	char	*state;
6687c478bd9Sstevel@tonic-gate 	char	*type;
6697c478bd9Sstevel@tonic-gate 	int	found = FALSE;
6707c478bd9Sstevel@tonic-gate 	int	rtn = NLS_OK;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if (netspec == NULL)
6737c478bd9Sstevel@tonic-gate 		sprintf(buf, SAC_LSTY, LISTENTYPE);
6747c478bd9Sstevel@tonic-gate 	else
6757c478bd9Sstevel@tonic-gate 		sprintf(buf, SAC_LSPM, netspec);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if ((fp = popen(buf, "r")) == NULL)
6787c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp) != NULL) {
6817c478bd9Sstevel@tonic-gate 		if ((name = nexttok(buf, ":")) == NULL)
6827c478bd9Sstevel@tonic-gate 			return(NLS_SYSERR);
6837c478bd9Sstevel@tonic-gate 		if ((type = nexttok(NULL, ":")) == NULL)
6847c478bd9Sstevel@tonic-gate 			return(NLS_SYSERR);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		if (strcmp(type, LISTENTYPE) != 0)
6877c478bd9Sstevel@tonic-gate 			continue;  /* ignore other types of port monitors */
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		found = TRUE;
6907c478bd9Sstevel@tonic-gate 		if (nexttok(NULL, ":") == NULL)
6917c478bd9Sstevel@tonic-gate 			return(NLS_SYSERR);
6927c478bd9Sstevel@tonic-gate 		if (nexttok(NULL, ":") == NULL)
6937c478bd9Sstevel@tonic-gate 			return(NLS_SYSERR);
6947c478bd9Sstevel@tonic-gate 		if ((state = nexttok(NULL, ":")) == NULL)
6957c478bd9Sstevel@tonic-gate 			return(NLS_SYSERR);
696*67e41408SToomas Soome 		if (strcmp(state, "ENABLED") == 0 ||
697*67e41408SToomas Soome 		    strcmp(state, "STARTING") == 0) {
6987c478bd9Sstevel@tonic-gate 			rtn = QZERO;
6997c478bd9Sstevel@tonic-gate 			if (!Quietflag)
7007c478bd9Sstevel@tonic-gate 				printf("%s\t%s\n", name, "ACTIVE");
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 		else {
7037c478bd9Sstevel@tonic-gate 			rtn = QONE;
7047c478bd9Sstevel@tonic-gate 			if (!Quietflag)
7057c478bd9Sstevel@tonic-gate 				printf("%s\t%s\n", name, "INACTIVE");
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 	pclose(fp);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	if (netspec && !found) {
7117c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Invalid network specification");
7127c478bd9Sstevel@tonic-gate 		return(NLS_BADPM);
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (netspec)
7167c478bd9Sstevel@tonic-gate 		return(rtn);
7177c478bd9Sstevel@tonic-gate 	else
7187c478bd9Sstevel@tonic-gate 		return(NLS_OK);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate /*
7247c478bd9Sstevel@tonic-gate  * print info about service on netspec, or all services on netspec
7257c478bd9Sstevel@tonic-gate  * if svc is NULL
7267c478bd9Sstevel@tonic-gate  */
7277c478bd9Sstevel@tonic-gate 
728bdcaf822Sbasabi int
prt_svcs(char * svc,char * netspec)729bdcaf822Sbasabi prt_svcs(char *svc, char *netspec)
7307c478bd9Sstevel@tonic-gate {
7317c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
7327c478bd9Sstevel@tonic-gate 	char	mesg[BUFSIZ];
7337c478bd9Sstevel@tonic-gate 	FILE	*fp;
7347c478bd9Sstevel@tonic-gate 	struct	svcfields entry;
7357c478bd9Sstevel@tonic-gate 	int	rtn;
7367c478bd9Sstevel@tonic-gate 	int	found = FALSE;
7377c478bd9Sstevel@tonic-gate 	char	*p;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (svc == NULL)
7407c478bd9Sstevel@tonic-gate 		sprintf(buf, PM_LSALL, netspec);
7417c478bd9Sstevel@tonic-gate 	else
7427c478bd9Sstevel@tonic-gate 		sprintf(buf, PM_LSONE, netspec, svc);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if ((fp = popen(buf, "r")) == NULL)
7457c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp) != NULL) {
7487c478bd9Sstevel@tonic-gate 		if ((rtn = svc_format(buf, &entry)) != 0) {
7497c478bd9Sstevel@tonic-gate 			switch (rtn) {
7507c478bd9Sstevel@tonic-gate 			case NOTLISTEN:
7517c478bd9Sstevel@tonic-gate 				continue;
7527c478bd9Sstevel@tonic-gate 				break;
7537c478bd9Sstevel@tonic-gate 			case BADPMFMT:
7547c478bd9Sstevel@tonic-gate 				return(NLS_SYSERR);
7557c478bd9Sstevel@tonic-gate 				break;
7567c478bd9Sstevel@tonic-gate 			case BADLISFMT:
7577c478bd9Sstevel@tonic-gate 				sprintf(mesg, "Entry for code \"%s\" has incorrect format", entry.svc_code);
7587c478bd9Sstevel@tonic-gate 				nlsmesg(MM_WARNING, mesg);
7597c478bd9Sstevel@tonic-gate 				continue;
7607c478bd9Sstevel@tonic-gate 				break;
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 		found = TRUE;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		if (!Quietflag) {
7667c478bd9Sstevel@tonic-gate 			printf("%s\t", entry.svc_code);
7677c478bd9Sstevel@tonic-gate 			if (*entry.addr)
7687c478bd9Sstevel@tonic-gate 				printf("%s\t", entry.addr);
7697c478bd9Sstevel@tonic-gate 			else if (strchr(entry.lflags, 'd'))
7707c478bd9Sstevel@tonic-gate 				printf("DYNAMIC\t");
7717c478bd9Sstevel@tonic-gate 			else
7727c478bd9Sstevel@tonic-gate 				printf("NOADDR\t");
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 			if (strchr(entry.flags, 'x') == NULL)
7757c478bd9Sstevel@tonic-gate 				printf("ENABLED \t");
7767c478bd9Sstevel@tonic-gate 			else
7777c478bd9Sstevel@tonic-gate 				printf("DISABLED\t");
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 			printf("%s\t%s\t%s\t%s\t# %s",
7817c478bd9Sstevel@tonic-gate 				(*entry.rpc)?entry.rpc:"NORPC", entry.id,
7827c478bd9Sstevel@tonic-gate 				(*entry.modules)?entry.modules:"NOMODULES",
7837c478bd9Sstevel@tonic-gate 				entry.command, (*entry.comment)?entry.comment:"");
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 		else {
7867c478bd9Sstevel@tonic-gate 			if (strchr(entry.flags, 'x') == NULL)
7877c478bd9Sstevel@tonic-gate 				return(QZERO);
7887c478bd9Sstevel@tonic-gate 			else
7897c478bd9Sstevel@tonic-gate 				return(QONE);
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	pclose(fp);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (rtn == NOTLISTEN) { /* check last return to see if error */
7967c478bd9Sstevel@tonic-gate 		sprintf(mesg, "Network specification \"%s\" is not of type %s", netspec, LISTENTYPE);
7977c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, mesg);
7987c478bd9Sstevel@tonic-gate 		return(NLS_BADPM);
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 	if (svc && !found) {
8017c478bd9Sstevel@tonic-gate 		if (!Quietflag) {
8027c478bd9Sstevel@tonic-gate 			sprintf(mesg, "Service \"%s\" unknown", svc);
8037c478bd9Sstevel@tonic-gate 			nlsmesg(MM_ERROR, mesg);
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 		return(NLS_SERV);
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	return(NLS_OK);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate  * disable_svc:  use pmadm to disable a service
8137c478bd9Sstevel@tonic-gate  */
8147c478bd9Sstevel@tonic-gate 
815bdcaf822Sbasabi int
disable_svc(char * svc,char * netspec)816bdcaf822Sbasabi disable_svc(char *svc, char *netspec)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
8197c478bd9Sstevel@tonic-gate 	int	rtn;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	sprintf(buf, PM_DISABLE, netspec, svc);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if ((rtn = system(buf)) < 0) {
8247c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 	rtn = (rtn>>8) & 0xff;	/* get child return value out of exit word */
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	switch (rtn) {
8297c478bd9Sstevel@tonic-gate 	case 0:
8307c478bd9Sstevel@tonic-gate 		return(NLS_OK);
8317c478bd9Sstevel@tonic-gate 		break;
8327c478bd9Sstevel@tonic-gate 	case E_BADARGS:
8337c478bd9Sstevel@tonic-gate 	case E_SAFERR:
8347c478bd9Sstevel@tonic-gate 	case E_SYSERR:
8357c478bd9Sstevel@tonic-gate 	case E_PMRUN:
8367c478bd9Sstevel@tonic-gate 	case E_PMNOTRUN:
8377c478bd9Sstevel@tonic-gate 	case E_RECOVER:
8387c478bd9Sstevel@tonic-gate 	case E_SACNOTRUN:
8397c478bd9Sstevel@tonic-gate 	default:
8407c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
8417c478bd9Sstevel@tonic-gate 		break;
8427c478bd9Sstevel@tonic-gate 	case E_NOEXIST:
8437c478bd9Sstevel@tonic-gate 	case E_DUP:
8447c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Non-existent service.");
8457c478bd9Sstevel@tonic-gate 		return(NLS_SERV);
8467c478bd9Sstevel@tonic-gate 		break;
8477c478bd9Sstevel@tonic-gate 	case E_NOPRIV:
8487c478bd9Sstevel@tonic-gate 		no_permission();
8497c478bd9Sstevel@tonic-gate 		break;
8507c478bd9Sstevel@tonic-gate 	}
851bdcaf822Sbasabi 	/* NOTREACHED */
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 
855bdcaf822Sbasabi int
enable_svc(char * svc,char * netspec)856bdcaf822Sbasabi enable_svc(char *svc, char *netspec)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
8597c478bd9Sstevel@tonic-gate 	int	rtn;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	sprintf(buf, PM_ENABLE, netspec, svc);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	if ((rtn = system(buf)) < 0) {
8647c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	rtn = (rtn>>8) & 0xff;	/* get child return value out of exit word */
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	switch (rtn) {
8697c478bd9Sstevel@tonic-gate 	case 0:
8707c478bd9Sstevel@tonic-gate 		return(NLS_OK);
8717c478bd9Sstevel@tonic-gate 		break;
8727c478bd9Sstevel@tonic-gate 	case E_BADARGS:
8737c478bd9Sstevel@tonic-gate 	case E_SAFERR:
8747c478bd9Sstevel@tonic-gate 	case E_SYSERR:
8757c478bd9Sstevel@tonic-gate 	case E_PMRUN:
8767c478bd9Sstevel@tonic-gate 	case E_PMNOTRUN:
8777c478bd9Sstevel@tonic-gate 	case E_RECOVER:
8787c478bd9Sstevel@tonic-gate 	case E_SACNOTRUN:
8797c478bd9Sstevel@tonic-gate 	default:
8807c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
8817c478bd9Sstevel@tonic-gate 		break;
8827c478bd9Sstevel@tonic-gate 	case E_NOEXIST:
8837c478bd9Sstevel@tonic-gate 	case E_DUP:
8847c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Non-existent service.");
8857c478bd9Sstevel@tonic-gate 		return(NLS_SERV);
8867c478bd9Sstevel@tonic-gate 		break;
8877c478bd9Sstevel@tonic-gate 	case E_NOPRIV:
8887c478bd9Sstevel@tonic-gate 		no_permission();
8897c478bd9Sstevel@tonic-gate 		break;
8907c478bd9Sstevel@tonic-gate 	}
891bdcaf822Sbasabi 	/* NOTREACHED */
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 
895bdcaf822Sbasabi int
remove_svc(char * svc,char * netspec,int printerrors)896bdcaf822Sbasabi remove_svc(char *svc, char *netspec, int printerrors)
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
8997c478bd9Sstevel@tonic-gate 	int	rtn;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	sprintf(buf, PM_REMSVC, netspec, svc);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	if ((rtn = system(buf)) < 0) {
9047c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	rtn = (rtn>>8) & 0xff;	/* get child return value out of exit word */
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	switch (rtn) {
9097c478bd9Sstevel@tonic-gate 	case 0:
9107c478bd9Sstevel@tonic-gate 		return(NLS_OK);
9117c478bd9Sstevel@tonic-gate 		break;
9127c478bd9Sstevel@tonic-gate 	case E_BADARGS:
9137c478bd9Sstevel@tonic-gate 	case E_SAFERR:
9147c478bd9Sstevel@tonic-gate 	case E_SYSERR:
9157c478bd9Sstevel@tonic-gate 	case E_PMRUN:
9167c478bd9Sstevel@tonic-gate 	case E_PMNOTRUN:
9177c478bd9Sstevel@tonic-gate 	case E_RECOVER:
9187c478bd9Sstevel@tonic-gate 	case E_SACNOTRUN:
9197c478bd9Sstevel@tonic-gate 	default:
9207c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
9217c478bd9Sstevel@tonic-gate 		break;
9227c478bd9Sstevel@tonic-gate 	case E_NOEXIST:
9237c478bd9Sstevel@tonic-gate 	case E_DUP:
9247c478bd9Sstevel@tonic-gate 		if (printerrors)
9257c478bd9Sstevel@tonic-gate 			nlsmesg(MM_ERROR, "Non-existent service.");
9267c478bd9Sstevel@tonic-gate 		return(NLS_SERV);
9277c478bd9Sstevel@tonic-gate 		break;
9287c478bd9Sstevel@tonic-gate 	case E_NOPRIV:
9297c478bd9Sstevel@tonic-gate 		no_permission();
9307c478bd9Sstevel@tonic-gate 		break;
9317c478bd9Sstevel@tonic-gate 	}
932bdcaf822Sbasabi 	/* NOTREACHED */
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 
936bdcaf822Sbasabi int
kill_listener(char * netspec)937bdcaf822Sbasabi kill_listener(char *netspec)
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
9407c478bd9Sstevel@tonic-gate 	char	mesg[BUFSIZ];
9417c478bd9Sstevel@tonic-gate 	int	rtn;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	sprintf(buf, SAC_KILLPM, netspec);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	if ((rtn = system(buf)) < 0) {
9467c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 	rtn = (rtn>>8) & 0xff;	/* get child return value out of exit word */
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	switch (rtn) {
9517c478bd9Sstevel@tonic-gate 	case 0:
9527c478bd9Sstevel@tonic-gate 		return(NLS_OK);
9537c478bd9Sstevel@tonic-gate 		break;
9547c478bd9Sstevel@tonic-gate 	case E_BADARGS:
9557c478bd9Sstevel@tonic-gate 	case E_DUP:
9567c478bd9Sstevel@tonic-gate 	case E_SAFERR:
9577c478bd9Sstevel@tonic-gate 	case E_SYSERR:
9587c478bd9Sstevel@tonic-gate 	case E_PMRUN:
9597c478bd9Sstevel@tonic-gate 	case E_RECOVER:
9607c478bd9Sstevel@tonic-gate 	case E_SACNOTRUN:
9617c478bd9Sstevel@tonic-gate 	default:
9627c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
9637c478bd9Sstevel@tonic-gate 		break;
9647c478bd9Sstevel@tonic-gate 	case E_PMNOTRUN:
9657c478bd9Sstevel@tonic-gate 		sprintf(mesg, "No listener active on network \"%s\"", netspec);
9667c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, mesg);
9677c478bd9Sstevel@tonic-gate 		return(NLS_FAILED);
9687c478bd9Sstevel@tonic-gate 	case E_NOEXIST:
9697c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Non-existent port monitor.");
9707c478bd9Sstevel@tonic-gate 		return(NLS_SERV);
9717c478bd9Sstevel@tonic-gate 		break;
9727c478bd9Sstevel@tonic-gate 	case E_NOPRIV:
9737c478bd9Sstevel@tonic-gate 		no_permission();
9747c478bd9Sstevel@tonic-gate 		break;
9757c478bd9Sstevel@tonic-gate 	}
976bdcaf822Sbasabi 	/* NOTREACHED */
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate  * add_pm:  add a port monitor (initialize directories) using sacadm
9827c478bd9Sstevel@tonic-gate  */
9837c478bd9Sstevel@tonic-gate 
984bdcaf822Sbasabi int
add_pm(char * netspec)985bdcaf822Sbasabi add_pm(char *netspec)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
9887c478bd9Sstevel@tonic-gate 	char	mesg[BUFSIZ];
9897c478bd9Sstevel@tonic-gate 	int	rtn;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	sprintf(buf, SAC_ADDPM, netspec, LISTENTYPE, gencmdstr(netspec), VERSION);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	if ((rtn = system(buf)) < 0) {
9947c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 	rtn = (rtn>>8) & 0xff;	/* get child return value out of exit word */
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	switch (rtn) {
9997c478bd9Sstevel@tonic-gate 	case 0:
10007c478bd9Sstevel@tonic-gate 		old_addsvc(NLPSSVCCODE, NULL, NLPSSRV, "NLPS server", "", "root", NULL, netspec);
10017c478bd9Sstevel@tonic-gate 		return(NLS_OK);
10027c478bd9Sstevel@tonic-gate 		break;
10037c478bd9Sstevel@tonic-gate 	case E_BADARGS:
10047c478bd9Sstevel@tonic-gate 	case E_SAFERR:
10057c478bd9Sstevel@tonic-gate 	case E_SYSERR:
10067c478bd9Sstevel@tonic-gate 	case E_RECOVER:
10077c478bd9Sstevel@tonic-gate 	case E_NOEXIST:
10087c478bd9Sstevel@tonic-gate 	case E_PMNOTRUN:
10097c478bd9Sstevel@tonic-gate 	case E_SACNOTRUN:
10107c478bd9Sstevel@tonic-gate 	default:
10117c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
10127c478bd9Sstevel@tonic-gate 		break;
10137c478bd9Sstevel@tonic-gate 	case E_DUP:
10147c478bd9Sstevel@tonic-gate 	case E_PMRUN:
10157c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Listener already initialized");
10167c478bd9Sstevel@tonic-gate 		return(NLS_FAILED);
10177c478bd9Sstevel@tonic-gate 		break;
10187c478bd9Sstevel@tonic-gate 	case E_NOPRIV:
10197c478bd9Sstevel@tonic-gate 		no_permission();
10207c478bd9Sstevel@tonic-gate 		break;
10217c478bd9Sstevel@tonic-gate 	}
1022bdcaf822Sbasabi 	/* NOTREACHED */
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate /*
10277c478bd9Sstevel@tonic-gate  * gencmdstr:  generate the correct string to invoke the listener (starlan
10287c478bd9Sstevel@tonic-gate  *             requires special handling)
10297c478bd9Sstevel@tonic-gate  */
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate char *
gencmdstr(char * netspec)1032bdcaf822Sbasabi gencmdstr(char *netspec)
10337c478bd9Sstevel@tonic-gate {
10347c478bd9Sstevel@tonic-gate 	static char buf[BUFSIZ];
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, LISTENCMD);
10377c478bd9Sstevel@tonic-gate 	if (!strcmp(netspec, "starlan"))
10387c478bd9Sstevel@tonic-gate 		(void) strcat(buf, " -m slan");
10397c478bd9Sstevel@tonic-gate 	(void) strcat(buf, " ");
10407c478bd9Sstevel@tonic-gate 	(void) strcat(buf, netspec);
10417c478bd9Sstevel@tonic-gate 	return(buf);
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate /*
10467c478bd9Sstevel@tonic-gate  * start_listener: start the listener
10477c478bd9Sstevel@tonic-gate  */
10487c478bd9Sstevel@tonic-gate 
1049bdcaf822Sbasabi int
start_listener(char * netspec)1050bdcaf822Sbasabi start_listener(char *netspec)
10517c478bd9Sstevel@tonic-gate {
10527c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
10537c478bd9Sstevel@tonic-gate 	char	scratch[BUFSIZ];
10547c478bd9Sstevel@tonic-gate 	int	rtn;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	sprintf(buf, SAC_STARTPM, netspec);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	if ((rtn = system(buf)) < 0)
10597c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
10607c478bd9Sstevel@tonic-gate 	rtn = (rtn>>8) & 0xff;
10617c478bd9Sstevel@tonic-gate 	switch (rtn) {
10627c478bd9Sstevel@tonic-gate 	case 0:
10637c478bd9Sstevel@tonic-gate 		break;
10647c478bd9Sstevel@tonic-gate 	case E_BADARGS:
10657c478bd9Sstevel@tonic-gate 	case E_SAFERR:
10667c478bd9Sstevel@tonic-gate 	case E_SYSERR:
10677c478bd9Sstevel@tonic-gate 	case E_RECOVER:
10687c478bd9Sstevel@tonic-gate 	case E_PMNOTRUN:
10697c478bd9Sstevel@tonic-gate 	case E_SACNOTRUN:
10707c478bd9Sstevel@tonic-gate 	default:
10717c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
10727c478bd9Sstevel@tonic-gate 		break;
10737c478bd9Sstevel@tonic-gate 	case E_NOEXIST:
10747c478bd9Sstevel@tonic-gate 	case E_DUP:
10757c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Non-existent port monitor.");
10767c478bd9Sstevel@tonic-gate 		return(NLS_BADPM);
10777c478bd9Sstevel@tonic-gate 		break;
10787c478bd9Sstevel@tonic-gate 	case E_PMRUN:
10797c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Listener already running");
10807c478bd9Sstevel@tonic-gate 		return(NLS_FAILED);
10817c478bd9Sstevel@tonic-gate 	case E_NOPRIV:
10827c478bd9Sstevel@tonic-gate 		no_permission();
10837c478bd9Sstevel@tonic-gate 		break;
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	sprintf(buf, SAC_ENABLPM, netspec);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	if ((rtn = system(buf)) < 0) {
10897c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 	rtn = (rtn>>8) & 0xff;
10927c478bd9Sstevel@tonic-gate 	switch (rtn) {
10937c478bd9Sstevel@tonic-gate 	case 0:
10947c478bd9Sstevel@tonic-gate 		return(NLS_OK);
10957c478bd9Sstevel@tonic-gate 		break;
10967c478bd9Sstevel@tonic-gate 	case E_BADARGS:
10977c478bd9Sstevel@tonic-gate 	case E_SAFERR:
10987c478bd9Sstevel@tonic-gate 	case E_SYSERR:
10997c478bd9Sstevel@tonic-gate 	case E_RECOVER:
11007c478bd9Sstevel@tonic-gate 	case E_SACNOTRUN:
11017c478bd9Sstevel@tonic-gate 	default:
11027c478bd9Sstevel@tonic-gate 		return(NLS_SYSERR);
11037c478bd9Sstevel@tonic-gate 		break;
11047c478bd9Sstevel@tonic-gate 	case E_NOEXIST:
11057c478bd9Sstevel@tonic-gate 	case E_DUP:
11067c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Non-existent port monitor.");
11077c478bd9Sstevel@tonic-gate 		return(NLS_BADPM);
11087c478bd9Sstevel@tonic-gate 		break;
11097c478bd9Sstevel@tonic-gate 	case E_PMRUN:
11107c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Listener already running");
11117c478bd9Sstevel@tonic-gate 		return(NLS_FAILED);
11127c478bd9Sstevel@tonic-gate 	case E_PMNOTRUN:
11137c478bd9Sstevel@tonic-gate 		nlsmesg(MM_ERROR, "Listener start failed");
11147c478bd9Sstevel@tonic-gate 		return(NLS_FAILED);
11157c478bd9Sstevel@tonic-gate 	case E_NOPRIV:
11167c478bd9Sstevel@tonic-gate 		no_permission();
11177c478bd9Sstevel@tonic-gate 		break;
11187c478bd9Sstevel@tonic-gate 	}
1119bdcaf822Sbasabi 	/* NOTREACHED */
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate /*
11247c478bd9Sstevel@tonic-gate  * setup_addr:  setup the -l and -t addresses.
11257c478bd9Sstevel@tonic-gate  */
11267c478bd9Sstevel@tonic-gate 
1127bdcaf822Sbasabi int
setup_addr(char * laddr,char * taddr,char * netspec)1128bdcaf822Sbasabi setup_addr(char *laddr, char *taddr, char *netspec)
11297c478bd9Sstevel@tonic-gate {
11307c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
11317c478bd9Sstevel@tonic-gate 	char	mesg[BUFSIZ];
11327c478bd9Sstevel@tonic-gate 	char	*p;
11337c478bd9Sstevel@tonic-gate 	int	rtn;
11347c478bd9Sstevel@tonic-gate 	int	qlisten = FALSE;
11357c478bd9Sstevel@tonic-gate 	int	qtty = FALSE;
11367c478bd9Sstevel@tonic-gate 	FILE	*fp;
11377c478bd9Sstevel@tonic-gate 	struct	svcfields entry;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	if (laddr && *laddr == '-')
11407c478bd9Sstevel@tonic-gate 		qlisten = TRUE;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	if (taddr && *taddr == '-')
11437c478bd9Sstevel@tonic-gate 		qtty = TRUE;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	if (laddr) {
11467c478bd9Sstevel@tonic-gate 		sprintf(buf, PM_LSONE, netspec, NLPSSVCCODE);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		if ((fp = popen(buf, "r")) == NULL) {
11497c478bd9Sstevel@tonic-gate 			return(NLS_SYSERR);
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 		if (fgets(buf, BUFSIZ, fp) != NULL) {
11537c478bd9Sstevel@tonic-gate 			if ((rtn = svc_format(buf, &entry)) != 0) {
11547c478bd9Sstevel@tonic-gate 				switch (rtn) {
11557c478bd9Sstevel@tonic-gate 				case NOTLISTEN:
11567c478bd9Sstevel@tonic-gate 					nlsmesg(MM_ERROR, "Incorrect port monitor type.  Must be of type listen");
11577c478bd9Sstevel@tonic-gate 					return(NLS_FAILED);
11587c478bd9Sstevel@tonic-gate 					break;
11597c478bd9Sstevel@tonic-gate 				case BADPMFMT:
11607c478bd9Sstevel@tonic-gate 					return(NLS_SYSERR);
11617c478bd9Sstevel@tonic-gate 					break;
11627c478bd9Sstevel@tonic-gate 				case BADLISFMT:
11637c478bd9Sstevel@tonic-gate 					sprintf(mesg, "Entry for code \"%s\" has incorrect format", entry.svc_code);
11647c478bd9Sstevel@tonic-gate 					nlsmesg(MM_WARNING, mesg);
11657c478bd9Sstevel@tonic-gate 					break;
11667c478bd9Sstevel@tonic-gate 				}
11677c478bd9Sstevel@tonic-gate 			}
11687c478bd9Sstevel@tonic-gate 			else {
11697c478bd9Sstevel@tonic-gate 				if (qlisten)
11707c478bd9Sstevel@tonic-gate 					printf("%s\n", entry.addr);
11717c478bd9Sstevel@tonic-gate 				else {
11727c478bd9Sstevel@tonic-gate 					if (geteuid() != ROOT)
11737c478bd9Sstevel@tonic-gate 						no_permission();
11747c478bd9Sstevel@tonic-gate 					/* add address */
11757c478bd9Sstevel@tonic-gate 					remove_svc(NLPSSVCCODE, netspec, FALSE);
11767c478bd9Sstevel@tonic-gate 					p = strchr(entry.comment, '\n');
11777c478bd9Sstevel@tonic-gate 					if (p)
11787c478bd9Sstevel@tonic-gate 						*p = '\0';
11797c478bd9Sstevel@tonic-gate 					old_addsvc(NLPSSVCCODE, laddr, entry.command, entry.comment, entry.modules, entry.id, entry.flags, netspec);
11807c478bd9Sstevel@tonic-gate 				}
11817c478bd9Sstevel@tonic-gate 			}
11827c478bd9Sstevel@tonic-gate 			pclose(fp);
11837c478bd9Sstevel@tonic-gate 		}
11847c478bd9Sstevel@tonic-gate 		else if (!qlisten)
11857c478bd9Sstevel@tonic-gate 			nlsmesg(MM_WARNING, "NLPS service not defined");
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 	if (taddr) {
11887c478bd9Sstevel@tonic-gate 		sprintf(buf, PM_LSONE, netspec, TTYSVCCODE);
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 		if ((fp = popen(buf, "r")) == NULL) {
11917c478bd9Sstevel@tonic-gate 			return(NLS_SYSERR);
1192