xref: /titanic_51/usr/src/cmd/cmd-inet/usr.sbin/inetadm/inetadm.c (revision ba1637f8b78b432c41b36839c92aecf1f5f9fafb)
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 /*
23*ba1637f8Smh138676  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * inetadm - administer services controlled by inetd and print out inetd
317c478bd9Sstevel@tonic-gate  * service related information.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <locale.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <libscf.h>
377c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
387c478bd9Sstevel@tonic-gate #include <libuutil.h>
397c478bd9Sstevel@tonic-gate #include <stddef.h>
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <syslog.h>
447c478bd9Sstevel@tonic-gate #include <inetsvc.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
487c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
497c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* Strings for output to the user, and checking user's input */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	INETADM_TRUE_STR		"TRUE"
547c478bd9Sstevel@tonic-gate #define	INETADM_FALSE_STR		"FALSE"
557c478bd9Sstevel@tonic-gate #define	INETADM_ENABLED_STR		"enabled"
567c478bd9Sstevel@tonic-gate #define	INETADM_DISABLED_STR		"disabled"
577c478bd9Sstevel@tonic-gate #define	INETADM_DEFAULT_STR		"default"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* String for checking if an instance is under inetd's control. */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	INETADM_INETD_STR		"network/inetd"
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Used to hold a list of scf_value_t's whilst performing a transaction
657c478bd9Sstevel@tonic-gate  * to write a proto list back.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate typedef struct scf_val_el {
687c478bd9Sstevel@tonic-gate 	scf_value_t		*val;
697c478bd9Sstevel@tonic-gate 	uu_list_node_t		link;
707c478bd9Sstevel@tonic-gate } scf_val_el_t;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Structure used to encapsulate argc and argv so they can be passed using the
747c478bd9Sstevel@tonic-gate  * single data argument supplied by scf_walk_fmri() for the consumption of
757c478bd9Sstevel@tonic-gate  * modify_inst_props_cb().
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate typedef struct arglist {
787c478bd9Sstevel@tonic-gate 	int	argc;
797c478bd9Sstevel@tonic-gate 	char	**argv;
807c478bd9Sstevel@tonic-gate } arglist_t;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static scf_handle_t *h;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static void
857c478bd9Sstevel@tonic-gate scfdie()
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	uu_die(gettext("Unexpected libscf error: %s.  Exiting.\n"),
887c478bd9Sstevel@tonic-gate 	    scf_strerror(scf_error()));
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static void
927c478bd9Sstevel@tonic-gate usage(boolean_t detailed)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	uu_warn(gettext(
967c478bd9Sstevel@tonic-gate 	    "Usage:\n"
977c478bd9Sstevel@tonic-gate 	    "  inetadm\n"
987c478bd9Sstevel@tonic-gate 	    "  inetadm -?\n"
997c478bd9Sstevel@tonic-gate 	    "  inetadm -p\n"
1007c478bd9Sstevel@tonic-gate 	    "  inetadm -l {FMRI | pattern}...\n"
1017c478bd9Sstevel@tonic-gate 	    "  inetadm -e {FMRI | pattern}...\n"
1027c478bd9Sstevel@tonic-gate 	    "  inetadm -d {FMRI | pattern}...\n"
1037c478bd9Sstevel@tonic-gate 	    "  inetadm -m {FMRI | pattern}... {name=value}...\n"
1047c478bd9Sstevel@tonic-gate 	    "  inetadm -M {name=value}...\n"));
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (!detailed)
1077c478bd9Sstevel@tonic-gate 		exit(UU_EXIT_USAGE);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, gettext(
1107c478bd9Sstevel@tonic-gate 	    "\n"
1117c478bd9Sstevel@tonic-gate 	    "Without any options inetadm lists all inetd managed services.\n"
1127c478bd9Sstevel@tonic-gate 	    "\n"
1137c478bd9Sstevel@tonic-gate 	    "Options:\n"
1147c478bd9Sstevel@tonic-gate 	    "  -?	Print help.\n"
1157c478bd9Sstevel@tonic-gate 	    "  -p	List all default inetd property values.\n"
1167c478bd9Sstevel@tonic-gate 	    "  -l 	List all inetd property values for the inet "
1177c478bd9Sstevel@tonic-gate 	    "service(s).\n"
1187c478bd9Sstevel@tonic-gate 	    "  -e	Enable the inet service(s).\n"
1197c478bd9Sstevel@tonic-gate 	    "  -d	Disable the inet service(s).\n"
1207c478bd9Sstevel@tonic-gate 	    "  -m	Modify the inet service(s) inetd property values.\n"
1217c478bd9Sstevel@tonic-gate 	    "  -M	Modify default inetd property values.\n"));
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * Add the proto list contained in array 'plist' to entry 'entry', storing
1267c478bd9Sstevel@tonic-gate  * aside the scf_value_t's created and added to the entry in a list that the
1277c478bd9Sstevel@tonic-gate  * pointer referenced by sv_list is made to point at.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate static void
1307c478bd9Sstevel@tonic-gate add_proto_list(scf_transaction_entry_t *entry, scf_handle_t *hdl,
1317c478bd9Sstevel@tonic-gate     char **plist, uu_list_t **sv_list)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	scf_val_el_t		*sv_el;
1347c478bd9Sstevel@tonic-gate 	int			i;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	static uu_list_pool_t	*sv_pool = NULL;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if ((sv_pool == NULL) &&
1397c478bd9Sstevel@tonic-gate 	    ((sv_pool = uu_list_pool_create("sv_pool",
1407c478bd9Sstevel@tonic-gate 	    sizeof (scf_val_el_t), offsetof(scf_val_el_t, link), NULL,
1417c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG)) == NULL))
1427c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: %s.\n"), uu_strerror(uu_error()));
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if ((*sv_list = uu_list_create(sv_pool, NULL, 0)) == NULL)
1457c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: %s.\n"), uu_strerror(uu_error()));
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	for (i = 0; plist[i] != NULL; i++) {
1487c478bd9Sstevel@tonic-gate 		if ((sv_el = malloc(sizeof (scf_val_el_t))) == NULL)
1497c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error:"));
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		if (((sv_el->val = scf_value_create(hdl)) == NULL) ||
1527c478bd9Sstevel@tonic-gate 		    (scf_value_set_astring(sv_el->val, plist[i]) != 0) ||
1537c478bd9Sstevel@tonic-gate 		    (scf_entry_add_value(entry, sv_el->val) != 0))
1547c478bd9Sstevel@tonic-gate 			scfdie();
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		uu_list_node_init(sv_el, &sv_el->link, sv_pool);
1577c478bd9Sstevel@tonic-gate 		(void) uu_list_insert_after(*sv_list, NULL, sv_el);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * A counterpart to add_proto_list(), this function removes and frees the
1637c478bd9Sstevel@tonic-gate  * scf_value_t's it added to entry 'entry'.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate static void
1667c478bd9Sstevel@tonic-gate remove_proto_list(scf_transaction_entry_t *entry, uu_list_t *sv_list)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	scf_val_el_t	*sv_el;
1697c478bd9Sstevel@tonic-gate 	void		*cookie = NULL;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	scf_entry_reset(entry);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	while ((sv_el = uu_list_teardown(sv_list, &cookie)) != NULL) {
1747c478bd9Sstevel@tonic-gate 		scf_value_destroy(sv_el->val);
1757c478bd9Sstevel@tonic-gate 		free(sv_el);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	uu_list_destroy(sv_list);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * modify_prop takes an instance, property group, property name, type, and
1837c478bd9Sstevel@tonic-gate  * value, and modifies the specified property in the repository to the
1847c478bd9Sstevel@tonic-gate  * submitted value.
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static void
1887c478bd9Sstevel@tonic-gate modify_prop(const scf_instance_t *inst, const char *pg, const char *prop,
1897c478bd9Sstevel@tonic-gate     scf_type_t type, void *value)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	scf_transaction_t		*tx;
1927c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t		*ent;
1937c478bd9Sstevel@tonic-gate 	scf_propertygroup_t		*gpg;
1947c478bd9Sstevel@tonic-gate 	scf_property_t			*eprop;
1957c478bd9Sstevel@tonic-gate 	scf_value_t			*v;
1967c478bd9Sstevel@tonic-gate 	int				ret, create = 0;
1977c478bd9Sstevel@tonic-gate 	char				*fmri;
1987c478bd9Sstevel@tonic-gate 	ssize_t				max_fmri_len;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
2017c478bd9Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
2027c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL)
2037c478bd9Sstevel@tonic-gate 		scfdie();
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/* Get the property group or create it if it is missing. */
2067c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pg, gpg) == -1) {
2077c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
2087c478bd9Sstevel@tonic-gate 			scfdie();
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		max_fmri_len = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2117c478bd9Sstevel@tonic-gate 		if ((fmri = malloc(max_fmri_len + 1)) == NULL)
2127c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: Out of memory.\n"));
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(inst, fmri, max_fmri_len + 1) < 0)
2157c478bd9Sstevel@tonic-gate 			scfdie();
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE, "inetadm: Property group \"%s\" missing "
2187c478bd9Sstevel@tonic-gate 		    "from \"%s\", attempting to add it.\n", pg, fmri);
2197c478bd9Sstevel@tonic-gate 		free(fmri);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, pg, SCF_GROUP_FRAMEWORK, 0,
2227c478bd9Sstevel@tonic-gate 		    gpg) == -1) {
2237c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
2247c478bd9Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
2257c478bd9Sstevel@tonic-gate 				break;
2267c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
2277c478bd9Sstevel@tonic-gate 				uu_die(gettext("Error: Permission denied.\n"));
2287c478bd9Sstevel@tonic-gate 			default:
2297c478bd9Sstevel@tonic-gate 				scfdie();
2307c478bd9Sstevel@tonic-gate 			}
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(gpg, prop, eprop) == -1) {
2357c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
2367c478bd9Sstevel@tonic-gate 			scfdie();
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		create = 1;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if ((tx = scf_transaction_create(h)) == NULL ||
2427c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
2437c478bd9Sstevel@tonic-gate 		scfdie();
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	do {
2467c478bd9Sstevel@tonic-gate 		uu_list_t	*sv_list;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1) {
2497c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
2507c478bd9Sstevel@tonic-gate 				scfdie();
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: Permission denied.\n"));
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		/* Modify the property or create it if it is missing */
2567c478bd9Sstevel@tonic-gate 		if (create)
2577c478bd9Sstevel@tonic-gate 			ret = scf_transaction_property_new(tx, ent, prop, type);
2587c478bd9Sstevel@tonic-gate 		else
2597c478bd9Sstevel@tonic-gate 			ret = scf_transaction_property_change_type(tx, ent,
2607c478bd9Sstevel@tonic-gate 			    prop, type);
2617c478bd9Sstevel@tonic-gate 		if (ret == -1)
2627c478bd9Sstevel@tonic-gate 			scfdie();
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		switch (type) {
2657c478bd9Sstevel@tonic-gate 		case SCF_TYPE_BOOLEAN:
2667c478bd9Sstevel@tonic-gate 			scf_value_set_boolean(v, *(uint8_t *)value);
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 		case SCF_TYPE_INTEGER:
2697c478bd9Sstevel@tonic-gate 			scf_value_set_integer(v, *(int64_t *)value);
2707c478bd9Sstevel@tonic-gate 			break;
2717c478bd9Sstevel@tonic-gate 		case SCF_TYPE_ASTRING:
2727c478bd9Sstevel@tonic-gate 			if (strcmp(prop, PR_PROTO_NAME) == 0) {
2737c478bd9Sstevel@tonic-gate 				add_proto_list(ent, h, (char **)value,
2747c478bd9Sstevel@tonic-gate 				    &sv_list);
2757c478bd9Sstevel@tonic-gate 			} else if (scf_value_set_astring(v, value) == -1) {
2767c478bd9Sstevel@tonic-gate 				scfdie();
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 		default:
2807c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: Internal inetadm error"));
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		if ((strcmp(prop, PR_PROTO_NAME) != 0) &&
2847c478bd9Sstevel@tonic-gate 		    (scf_entry_add_value(ent, v) == -1))
2857c478bd9Sstevel@tonic-gate 			scfdie();
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		ret = scf_transaction_commit(tx);
2887c478bd9Sstevel@tonic-gate 		if (ret == -1) {
2897c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
2907c478bd9Sstevel@tonic-gate 				scfdie();
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: Permission denied.\n"));
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		if (ret == 0) {
2987c478bd9Sstevel@tonic-gate 			if (scf_pg_update(gpg) == -1)
2997c478bd9Sstevel@tonic-gate 				scfdie();
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		if (strcmp(prop, PR_PROTO_NAME) == 0)
3037c478bd9Sstevel@tonic-gate 			remove_proto_list(ent, sv_list);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	} while (ret == 0);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
3087c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
3097c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
3107c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
3117c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * delete_prop takes an instance, property group name and property, and
3167c478bd9Sstevel@tonic-gate  * deletes the specified property from the repository.
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate static void
3207c478bd9Sstevel@tonic-gate delete_prop(const scf_instance_t *inst, const char *pg, const char *prop)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	scf_transaction_t		*tx;
3237c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t		*ent;
3247c478bd9Sstevel@tonic-gate 	scf_propertygroup_t		*gpg;
3257c478bd9Sstevel@tonic-gate 	scf_property_t			*eprop;
3267c478bd9Sstevel@tonic-gate 	int				ret;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
3297c478bd9Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
3307c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
3317c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
3327c478bd9Sstevel@tonic-gate 		scfdie();
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pg, gpg) != SCF_SUCCESS) {
3357c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
3367c478bd9Sstevel@tonic-gate 			scfdie();
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: \"%s\" property group missing.\n"), pg);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	do {
3427c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) != SCF_SUCCESS) {
3437c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
3447c478bd9Sstevel@tonic-gate 				scfdie();
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: Permission denied.\n"));
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_delete(tx, ent,
3507c478bd9Sstevel@tonic-gate 		    prop) != SCF_SUCCESS) {
3517c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
3527c478bd9Sstevel@tonic-gate 				scfdie();
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 			uu_die(
3557c478bd9Sstevel@tonic-gate 			    gettext("Error: \"%s\" property does not exist.\n"),
3567c478bd9Sstevel@tonic-gate 			    prop);
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		ret = scf_transaction_commit(tx);
3607c478bd9Sstevel@tonic-gate 		if (ret < 0) {
3617c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
3627c478bd9Sstevel@tonic-gate 				scfdie();
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: Permission denied.\n"));
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		if (ret == 0) {
3677c478bd9Sstevel@tonic-gate 			scf_transaction_reset(tx);
3687c478bd9Sstevel@tonic-gate 			if (scf_pg_update(gpg) == -1)
3697c478bd9Sstevel@tonic-gate 				scfdie();
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 	} while (ret == 0);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	(void) scf_entry_destroy(ent);
3747c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
3757c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
3767c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate  * commit_props evaluates an entire property list that has been created
3817c478bd9Sstevel@tonic-gate  * based on command line options, and either deletes or modifies properties
3827c478bd9Sstevel@tonic-gate  * as requested.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate static void
3867c478bd9Sstevel@tonic-gate commit_props(const scf_instance_t *inst, inetd_prop_t *mod, boolean_t defaults)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	int			i;
3897c478bd9Sstevel@tonic-gate 	uint8_t			new_bool;
3907c478bd9Sstevel@tonic-gate 
391*ba1637f8Smh138676 	for (i = 0; mod[i].ip_name != NULL; i++) {
3927c478bd9Sstevel@tonic-gate 		switch (mod[i].ip_error) {
3937c478bd9Sstevel@tonic-gate 		case IVE_UNSET:
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate 		case IVE_INVALID:
3967c478bd9Sstevel@tonic-gate 			delete_prop(inst, mod[i].ip_pg, mod[i].ip_name);
3977c478bd9Sstevel@tonic-gate 			break;
3987c478bd9Sstevel@tonic-gate 		case IVE_VALID:
3997c478bd9Sstevel@tonic-gate 			switch (mod[i].ip_type) {
400*ba1637f8Smh138676 			case INET_TYPE_STRING:
4017c478bd9Sstevel@tonic-gate 				modify_prop(inst,
4027c478bd9Sstevel@tonic-gate 				    defaults ? PG_NAME_SERVICE_DEFAULTS :
4037c478bd9Sstevel@tonic-gate 				    mod[i].ip_pg, mod[i].ip_name,
4047c478bd9Sstevel@tonic-gate 				    SCF_TYPE_ASTRING,
405*ba1637f8Smh138676 				    mod[i].ip_value.iv_string);
4067c478bd9Sstevel@tonic-gate 				break;
407*ba1637f8Smh138676 			case INET_TYPE_STRING_LIST:
408*ba1637f8Smh138676 				modify_prop(inst,
409*ba1637f8Smh138676 				    defaults ? PG_NAME_SERVICE_DEFAULTS :
410*ba1637f8Smh138676 				    mod[i].ip_pg, mod[i].ip_name,
411*ba1637f8Smh138676 				    SCF_TYPE_ASTRING,
412*ba1637f8Smh138676 				    mod[i].ip_value.iv_string_list);
413*ba1637f8Smh138676 				break;
414*ba1637f8Smh138676 			case INET_TYPE_INTEGER:
4157c478bd9Sstevel@tonic-gate 				modify_prop(inst,
4167c478bd9Sstevel@tonic-gate 				    defaults ? PG_NAME_SERVICE_DEFAULTS :
4177c478bd9Sstevel@tonic-gate 				    mod[i].ip_pg, mod[i].ip_name,
4187c478bd9Sstevel@tonic-gate 				    SCF_TYPE_INTEGER, &mod[i].ip_value.iv_int);
4197c478bd9Sstevel@tonic-gate 				break;
420*ba1637f8Smh138676 			case INET_TYPE_BOOLEAN:
4217c478bd9Sstevel@tonic-gate 				new_bool = (mod[i].ip_value.iv_boolean) ? 1 : 0;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 				modify_prop(inst,
4247c478bd9Sstevel@tonic-gate 				    defaults ? PG_NAME_SERVICE_DEFAULTS :
4257c478bd9Sstevel@tonic-gate 				    mod[i].ip_pg, mod[i].ip_name,
4267c478bd9Sstevel@tonic-gate 				    SCF_TYPE_BOOLEAN, &new_bool);
4277c478bd9Sstevel@tonic-gate 				break;
4287c478bd9Sstevel@tonic-gate 			}
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * list_callback is the callback function to be handed to simple_walk_instances
4357c478bd9Sstevel@tonic-gate  * in list_services.  It is called once on every instance on a machine.  If
4367c478bd9Sstevel@tonic-gate  * that instance is controlled by inetd, it prints enabled/disabled, state,
4377c478bd9Sstevel@tonic-gate  * and instance FMRI.
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4417c478bd9Sstevel@tonic-gate static int
4427c478bd9Sstevel@tonic-gate list_callback(scf_handle_t *hin, scf_instance_t *inst, void *buf)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	ssize_t			max_name_length;
4457c478bd9Sstevel@tonic-gate 	char			*inst_name;
4467c478bd9Sstevel@tonic-gate 	scf_simple_prop_t	*prop = NULL, *prop2 = NULL;
4477c478bd9Sstevel@tonic-gate 	const uint8_t		*enabled;
4487c478bd9Sstevel@tonic-gate 	const char		*state, *restart_str;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	max_name_length = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
4517c478bd9Sstevel@tonic-gate 	if ((inst_name = malloc(max_name_length + 1)) == NULL)
4527c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: Out of memory.\n"));
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/*
4557c478bd9Sstevel@tonic-gate 	 * Get the FMRI of the instance, and check if its delegated restarter
4567c478bd9Sstevel@tonic-gate 	 * is inetd.
4577c478bd9Sstevel@tonic-gate 	 */
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (scf_instance_to_fmri(inst, inst_name, max_name_length + 1) < 0)
4607c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(hin, inst_name, SCF_PG_GENERAL,
4637c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_RESTARTER)) == NULL)
4647c478bd9Sstevel@tonic-gate 		goto out;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if ((restart_str = scf_simple_prop_next_ustring(prop)) == NULL)
4677c478bd9Sstevel@tonic-gate 		goto out;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (strstr(restart_str, INETADM_INETD_STR) == NULL)
4707c478bd9Sstevel@tonic-gate 		goto out;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/* Free restarter prop so it can be reused below */
4737c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * We know that this instance is managed by inetd.
4777c478bd9Sstevel@tonic-gate 	 * Now get the enabled and state properties.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (((prop = scf_simple_prop_get(hin, inst_name, SCF_PG_GENERAL,
4817c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_ENABLED)) == NULL) ||
4827c478bd9Sstevel@tonic-gate 	    ((enabled = scf_simple_prop_next_boolean(prop)) == NULL)) {
4837c478bd9Sstevel@tonic-gate 		(void) uu_warn(gettext("Error: Instance %s is missing enabled "
4847c478bd9Sstevel@tonic-gate 		    "property.\n"), inst_name);
4857c478bd9Sstevel@tonic-gate 		goto out;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (((prop2 = scf_simple_prop_get(hin, inst_name, SCF_PG_RESTARTER,
4897c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL) ||
4907c478bd9Sstevel@tonic-gate 	    ((state = scf_simple_prop_next_astring(prop2)) == NULL)) {
4917c478bd9Sstevel@tonic-gate 		(void) uu_warn(gettext("Error: Instance %s is missing state "
4927c478bd9Sstevel@tonic-gate 		    "property.\n"), inst_name);
4937c478bd9Sstevel@tonic-gate 		goto out;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* Print enabled/disabled, state, and FMRI for the instance. */
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if (*enabled)
4997c478bd9Sstevel@tonic-gate 		(void) printf("%-10s%-15s%s\n", INETADM_ENABLED_STR, state,
5007c478bd9Sstevel@tonic-gate 		    inst_name);
5017c478bd9Sstevel@tonic-gate 	else
5027c478bd9Sstevel@tonic-gate 		(void) printf("%-10s%-15s%s\n", INETADM_DISABLED_STR, state,
5037c478bd9Sstevel@tonic-gate 		    inst_name);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate out:
5067c478bd9Sstevel@tonic-gate 	free(inst_name);
5077c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
5087c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop2);
5097c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate  * list_services calls list_callback on every instance on the machine.
5147c478bd9Sstevel@tonic-gate  */
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate static void
5177c478bd9Sstevel@tonic-gate list_services()
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	(void) printf("%-10s%-15s%s\n", "ENABLED", "STATE", "FMRI");
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (scf_simple_walk_instances(SCF_STATE_ALL, NULL, list_callback) ==
5227c478bd9Sstevel@tonic-gate 	    SCF_FAILED)
5237c478bd9Sstevel@tonic-gate 		scfdie();
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate static void
527*ba1637f8Smh138676 print_prop_val(inetd_prop_t *prop)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	switch (prop->ip_type) {
530*ba1637f8Smh138676 	case INET_TYPE_STRING:
531*ba1637f8Smh138676 		(void) printf("\"%s\"\n", prop->ip_value.iv_string);
532*ba1637f8Smh138676 		break;
533*ba1637f8Smh138676 	case INET_TYPE_STRING_LIST:
534*ba1637f8Smh138676 		{
5357c478bd9Sstevel@tonic-gate 			int	j = 0;
536*ba1637f8Smh138676 			char	**cpp = prop->ip_value.iv_string_list;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 			/*
539*ba1637f8Smh138676 			 * Print string list as comma separated list.
5407c478bd9Sstevel@tonic-gate 			 */
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 			(void) printf("\"%s", cpp[j]);
5437c478bd9Sstevel@tonic-gate 			while (cpp[++j] != NULL)
5447c478bd9Sstevel@tonic-gate 				(void) printf(",%s", cpp[j]);
5457c478bd9Sstevel@tonic-gate 			(void) printf("\"\n");
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		break;
548*ba1637f8Smh138676 	case INET_TYPE_INTEGER:
5497c478bd9Sstevel@tonic-gate 		(void) printf("%lld\n", prop->ip_value.iv_int);
5507c478bd9Sstevel@tonic-gate 		break;
551*ba1637f8Smh138676 	case INET_TYPE_BOOLEAN:
5527c478bd9Sstevel@tonic-gate 		if (prop->ip_value.iv_boolean)
5537c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", INETADM_TRUE_STR);
5547c478bd9Sstevel@tonic-gate 		else
5557c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", INETADM_FALSE_STR);
5567c478bd9Sstevel@tonic-gate 		break;
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate  * list_props_cb is a callback function for scf_walk_fmri that lists all
5627c478bd9Sstevel@tonic-gate  * relevant inetd properties for an instance managed by inetd.
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
5667c478bd9Sstevel@tonic-gate static int
5677c478bd9Sstevel@tonic-gate list_props_cb(void *data, scf_walkinfo_t *wip)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 	int			i;
5707c478bd9Sstevel@tonic-gate 	const char		*instname = wip->fmri;
5717c478bd9Sstevel@tonic-gate 	scf_simple_prop_t	*prop;
5727c478bd9Sstevel@tonic-gate 	inetd_prop_t		*proplist;
5737c478bd9Sstevel@tonic-gate 	const char		*restart_str;
5747c478bd9Sstevel@tonic-gate 	boolean_t		is_rpc = B_FALSE;
5757c478bd9Sstevel@tonic-gate 	size_t			numprops;
5767c478bd9Sstevel@tonic-gate 	scf_handle_t		*h;
5777c478bd9Sstevel@tonic-gate 	scf_error_t		err;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (((h = scf_handle_create(SCF_VERSION)) == NULL) ||
5807c478bd9Sstevel@tonic-gate 	    (scf_handle_bind(h) == -1))
5817c478bd9Sstevel@tonic-gate 		scfdie();
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * Get the property that holds the name of this instance's
5857c478bd9Sstevel@tonic-gate 	 * restarter, and make sure that it is inetd.
5867c478bd9Sstevel@tonic-gate 	 */
5877c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(h, instname, SCF_PG_GENERAL,
5887c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_RESTARTER)) == NULL) {
5897c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
5907c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: Specified service instance "
5917c478bd9Sstevel@tonic-gate 			    "\"%s\" has no restarter property.  inetd is not "
5927c478bd9Sstevel@tonic-gate 			    "the delegated restarter of this instance.\n"),
5937c478bd9Sstevel@tonic-gate 			    instname);
5947c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_INVALID_ARGUMENT)
5957c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: \"%s\" is not a valid service "
5967c478bd9Sstevel@tonic-gate 			    "instance.\n"), instname);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		scfdie();
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (((restart_str = scf_simple_prop_next_ustring(prop)) == NULL) ||
6027c478bd9Sstevel@tonic-gate 	    (strstr(restart_str, INETADM_INETD_STR) == NULL)) {
6037c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: inetd is not the delegated restarter of "
6047c478bd9Sstevel@tonic-gate 		    "specified service instance \"%s\".\n"), instname);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * This instance is controlled by inetd, so now we display all
6117c478bd9Sstevel@tonic-gate 	 * of its properties.  First the mandatory properties, and then
6127c478bd9Sstevel@tonic-gate 	 * the properties that have default values, substituting the
6137c478bd9Sstevel@tonic-gate 	 * default values inherited from inetd as necessary (this is done
6147c478bd9Sstevel@tonic-gate 	 * for us by read_instance_props()).
6157c478bd9Sstevel@tonic-gate 	 */
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if ((proplist = read_instance_props(h, instname, &numprops, &err)) ==
6187c478bd9Sstevel@tonic-gate 	    NULL) {
6197c478bd9Sstevel@tonic-gate 		uu_die(gettext("Unexpected libscf error: %s.  Exiting.\n"),
6207c478bd9Sstevel@tonic-gate 		    scf_strerror(err));
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	(void) printf("%-9s%s\n", "SCOPE", "NAME=VALUE");
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	for (i = 0; i < numprops; i++) {
6277c478bd9Sstevel@tonic-gate 		/* Skip rpc version properties if it's not an RPC service */
6287c478bd9Sstevel@tonic-gate 		if ((strcmp(PR_RPC_LW_VER_NAME, proplist[i].ip_name) == 0) ||
6297c478bd9Sstevel@tonic-gate 		    (strcmp(PR_RPC_HI_VER_NAME, proplist[i].ip_name) == 0))
6307c478bd9Sstevel@tonic-gate 			if (!is_rpc)
6317c478bd9Sstevel@tonic-gate 				continue;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		/* If it's not an unset property, print it out. */
6347c478bd9Sstevel@tonic-gate 		if (proplist[i].ip_error != IVE_UNSET) {
6357c478bd9Sstevel@tonic-gate 			if (strcmp(PR_ISRPC_NAME, proplist[i].ip_name) == 0)
6367c478bd9Sstevel@tonic-gate 				is_rpc = proplist[i].ip_value.iv_boolean;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 			(void) printf("%-9s%s=",
6397c478bd9Sstevel@tonic-gate 			    proplist[i].from_inetd ? INETADM_DEFAULT_STR : "",
6407c478bd9Sstevel@tonic-gate 			    proplist[i].ip_name);
641*ba1637f8Smh138676 			print_prop_val(&proplist[i]);
6427c478bd9Sstevel@tonic-gate 			continue;
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		/* arg0 is non-default, but also doesn't have to be set. */
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		if (i == PT_ARG0_INDEX)
6487c478bd9Sstevel@tonic-gate 			continue;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		/* all other properties should have values. */
6517c478bd9Sstevel@tonic-gate 		if (proplist[i].ip_default) {
6527c478bd9Sstevel@tonic-gate 			(void) uu_warn(gettext("Error: Property %s is missing "
6537c478bd9Sstevel@tonic-gate 			    "and has no defined default value.\n"),
6547c478bd9Sstevel@tonic-gate 			    proplist[i].ip_name);
6557c478bd9Sstevel@tonic-gate 		} else {
6567c478bd9Sstevel@tonic-gate 			(void) uu_warn(gettext("Error: Required property %s is "
6577c478bd9Sstevel@tonic-gate 			    "missing.\n"), proplist[i].ip_name);
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	free_instance_props(proplist);
6627c478bd9Sstevel@tonic-gate 	return (0);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * set_svc_enable_cb is a callback function for scf_walk_fmri that sets the
6677c478bd9Sstevel@tonic-gate  * enabled property in the repository for an instance based on the value given
6687c478bd9Sstevel@tonic-gate  * by 'data'.
6697c478bd9Sstevel@tonic-gate  */
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate static int
6727c478bd9Sstevel@tonic-gate set_svc_enable_cb(void *data, scf_walkinfo_t *wip)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	uint8_t			desired = *(uint8_t *)data;
6757c478bd9Sstevel@tonic-gate 	const char		*instname = wip->fmri;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (desired) {
6787c478bd9Sstevel@tonic-gate 		if (smf_enable_instance(instname, 0) == 0)
6797c478bd9Sstevel@tonic-gate 			return (0);
6807c478bd9Sstevel@tonic-gate 	} else {
6817c478bd9Sstevel@tonic-gate 		if (smf_disable_instance(instname, 0) == 0)
6827c478bd9Sstevel@tonic-gate 			return (0);
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	switch (scf_error()) {
6867c478bd9Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
6877c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: \"%s\" is not a valid service "
6887c478bd9Sstevel@tonic-gate 		    "instance.\n"), instname);
6897c478bd9Sstevel@tonic-gate 		break;
6907c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
6917c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: Service instance \"%s\" not found.\n"),
6927c478bd9Sstevel@tonic-gate 		    instname);
6937c478bd9Sstevel@tonic-gate 		break;
6947c478bd9Sstevel@tonic-gate 	default:
6957c478bd9Sstevel@tonic-gate 		scfdie();
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	return (0);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * list_defaults lists all the default property values being provided by
7037c478bd9Sstevel@tonic-gate  * inetd.
7047c478bd9Sstevel@tonic-gate  */
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate static void
7077c478bd9Sstevel@tonic-gate list_defaults()
7087c478bd9Sstevel@tonic-gate {
7097c478bd9Sstevel@tonic-gate 	scf_handle_t		*h;
7107c478bd9Sstevel@tonic-gate 	scf_error_t		err;
7117c478bd9Sstevel@tonic-gate 	int			i;
7127c478bd9Sstevel@tonic-gate 	inetd_prop_t		*proptable;
7137c478bd9Sstevel@tonic-gate 	size_t			numprops;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (((h = scf_handle_create(SCF_VERSION)) == NULL) ||
7167c478bd9Sstevel@tonic-gate 	    (scf_handle_bind(h) == -1))
7177c478bd9Sstevel@tonic-gate 		scfdie();
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if ((proptable = read_default_props(h, &numprops, &err)) == NULL) {
7207c478bd9Sstevel@tonic-gate 		uu_die(gettext("Unexpected libscf error: %s.  Exiting.\n"),
7217c478bd9Sstevel@tonic-gate 		    scf_strerror(err));
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	(void) printf("NAME=VALUE\n");
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	for (i = 0; i < numprops; i++) {
7277c478bd9Sstevel@tonic-gate 		if (!proptable[i].ip_default)
7287c478bd9Sstevel@tonic-gate 			continue;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		if (proptable[i].ip_error == IVE_UNSET) {
7317c478bd9Sstevel@tonic-gate 			(void) uu_warn(gettext("Error: Default property %s "
7327c478bd9Sstevel@tonic-gate 			    "missing.\n"), proptable[i].ip_name);
7337c478bd9Sstevel@tonic-gate 			continue;
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		(void) printf("%s=", proptable[i].ip_name);
737*ba1637f8Smh138676 		print_prop_val(&proptable[i]);
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	free_instance_props(proptable);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate /*
7447c478bd9Sstevel@tonic-gate  * modify_inst_props_cb is a callback function for scf_walk_fmri that modifies
7457c478bd9Sstevel@tonic-gate  * the properties that are given as name=value pairs on the command line
7467c478bd9Sstevel@tonic-gate  * to the requested value.
7477c478bd9Sstevel@tonic-gate  */
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate static int
7507c478bd9Sstevel@tonic-gate modify_inst_props_cb(void *data, scf_walkinfo_t *wip)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	int			i, j;
7537c478bd9Sstevel@tonic-gate 	char			*value;
7547c478bd9Sstevel@tonic-gate 	const char		*fmri = wip->fmri;
7557c478bd9Sstevel@tonic-gate 	scf_instance_t		*inst = wip->inst;
7567c478bd9Sstevel@tonic-gate 	inetd_prop_t		*mod, *prop_table;
7577c478bd9Sstevel@tonic-gate 	size_t			numprops;
7587c478bd9Sstevel@tonic-gate 	ssize_t			max_val;
7597c478bd9Sstevel@tonic-gate 	int64_t			new_int;
7607c478bd9Sstevel@tonic-gate 	int			argc = ((arglist_t *)data)->argc;
7617c478bd9Sstevel@tonic-gate 	char			**argv = ((arglist_t *)data)->argv;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	if ((max_val = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH)) < 0)
7647c478bd9Sstevel@tonic-gate 		scfdie();
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	prop_table = get_prop_table(&numprops);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	if ((mod = malloc(numprops * sizeof (inetd_prop_t))) == NULL)
7697c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: Out of memory.\n"));
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	(void) memcpy(mod, prop_table, numprops * sizeof (inetd_prop_t));
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	/*
7747c478bd9Sstevel@tonic-gate 	 * For each property to be changed, look up the property name in the
7757c478bd9Sstevel@tonic-gate 	 * property table.  Change each property in the mod array, and then
7767c478bd9Sstevel@tonic-gate 	 * write the entire thing back.
7777c478bd9Sstevel@tonic-gate 	 */
7787c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
7797c478bd9Sstevel@tonic-gate 		/* Separate argument into name and value pair */
7807c478bd9Sstevel@tonic-gate 		if ((value = strchr(argv[i], '=')) == NULL)
7817c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: Malformed name=value pair "
7827c478bd9Sstevel@tonic-gate 			    "\"%s\"\n"), argv[i]);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		*value = '\0';
7857c478bd9Sstevel@tonic-gate 		value++;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		/* Find property name in array of properties */
788*ba1637f8Smh138676 		for (j = 0; mod[j].ip_name != NULL; j++) {
7897c478bd9Sstevel@tonic-gate 			if (strcmp(mod[j].ip_name, argv[i]) == 0)
7907c478bd9Sstevel@tonic-gate 				break;
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 
793*ba1637f8Smh138676 		if (mod[j].ip_name == NULL)
7947c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: \"%s\" is not a valid "
7957c478bd9Sstevel@tonic-gate 			    "property.\n"), argv[i]);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		if (*value == '\0') {
7987c478bd9Sstevel@tonic-gate 			if ((mod[j].ip_default) || (j == PT_ARG0_INDEX)) {
7997c478bd9Sstevel@tonic-gate 				/* mark property for deletion */
8007c478bd9Sstevel@tonic-gate 				mod[j].ip_error = IVE_INVALID;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 				/* return the '=' taken out above */
8037c478bd9Sstevel@tonic-gate 				*(--value) = '=';
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 				continue;
8067c478bd9Sstevel@tonic-gate 			} else {
8077c478bd9Sstevel@tonic-gate 				uu_die(gettext("\"%s\" is a mandatory "
8087c478bd9Sstevel@tonic-gate 				    "property and can not be deleted.\n"),
8097c478bd9Sstevel@tonic-gate 				    argv[i]);
8107c478bd9Sstevel@tonic-gate 			}
8117c478bd9Sstevel@tonic-gate 		}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		switch (mod[j].ip_type) {
814*ba1637f8Smh138676 		case INET_TYPE_INTEGER:
8157c478bd9Sstevel@tonic-gate 			if (uu_strtoint(value, &new_int, sizeof (new_int), NULL,
8167c478bd9Sstevel@tonic-gate 			    NULL, NULL) == -1)
8177c478bd9Sstevel@tonic-gate 				uu_die(gettext("Error: \"%s\" is not a valid "
8187c478bd9Sstevel@tonic-gate 				    "integer value.\n"), value);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 			mod[j].ip_value.iv_int = new_int;
8217c478bd9Sstevel@tonic-gate 			break;
822*ba1637f8Smh138676 		case INET_TYPE_STRING:
823*ba1637f8Smh138676 			if (strlen(value) >= max_val) {
824*ba1637f8Smh138676 				uu_die(gettext("Error: String value is longer "
825*ba1637f8Smh138676 				    "than %l characters.\n"), max_val);
826*ba1637f8Smh138676 			} else if ((mod[j].ip_value.iv_string = strdup(value))
827*ba1637f8Smh138676 			    == NULL) {
828*ba1637f8Smh138676 				uu_die(gettext("Error: Out of memory.\n"));
829*ba1637f8Smh138676 			}
830*ba1637f8Smh138676 			break;
831*ba1637f8Smh138676 		case INET_TYPE_STRING_LIST:
832*ba1637f8Smh138676 			if ((mod[j].ip_value.iv_string_list =
8337c478bd9Sstevel@tonic-gate 			    get_protos(value)) == NULL) {
8347c478bd9Sstevel@tonic-gate 				if (errno == ENOMEM) {
8357c478bd9Sstevel@tonic-gate 					uu_die(gettext(
8367c478bd9Sstevel@tonic-gate 					    "Error: Out of memory.\n"));
8377c478bd9Sstevel@tonic-gate 				} else if (errno == E2BIG) {
8387c478bd9Sstevel@tonic-gate 					uu_die(gettext(
8397c478bd9Sstevel@tonic-gate 					    "Error: String value in "
8407c478bd9Sstevel@tonic-gate 					    "%s property longer than "
8417c478bd9Sstevel@tonic-gate 					    "%l characters.\n"),
8427c478bd9Sstevel@tonic-gate 					    PR_PROTO_NAME, max_val);
8437c478bd9Sstevel@tonic-gate 				} else {
8447c478bd9Sstevel@tonic-gate 					uu_die(gettext(
8457c478bd9Sstevel@tonic-gate 					    "Error: No values "
8467c478bd9Sstevel@tonic-gate 					    "specified for %s "
8477c478bd9Sstevel@tonic-gate 					    "property.\n"),
8487c478bd9Sstevel@tonic-gate 					    PR_PROTO_NAME);
8497c478bd9Sstevel@tonic-gate 				}
8507c478bd9Sstevel@tonic-gate 			}
8517c478bd9Sstevel@tonic-gate 			break;
852*ba1637f8Smh138676 		case INET_TYPE_BOOLEAN:
8537c478bd9Sstevel@tonic-gate 			if (strcasecmp(value, INETADM_TRUE_STR) == 0)
8547c478bd9Sstevel@tonic-gate 				mod[j].ip_value.iv_boolean = B_TRUE;
8557c478bd9Sstevel@tonic-gate 			else if (strcasecmp(value, INETADM_FALSE_STR) == 0)
8567c478bd9Sstevel@tonic-gate 				mod[j].ip_value.iv_boolean = B_FALSE;
8577c478bd9Sstevel@tonic-gate 			else
8587c478bd9Sstevel@tonic-gate 				uu_die(gettext("Error: \"%s\" is not a valid "
8597c478bd9Sstevel@tonic-gate 				    "boolean value. (TRUE or FALSE)\n"), value);
8607c478bd9Sstevel@tonic-gate 		}
8617c478bd9Sstevel@tonic-gate 		/* mark property for modification */
8627c478bd9Sstevel@tonic-gate 		mod[j].ip_error = IVE_VALID;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 		/* return the '=' taken out above */
8657c478bd9Sstevel@tonic-gate 		*(--value) = '=';
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	commit_props(inst, mod, B_FALSE);
8697c478bd9Sstevel@tonic-gate 	free(mod);
8707c478bd9Sstevel@tonic-gate 	if (smf_refresh_instance(fmri) != 0)
8717c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: Unable to refresh instance %s.\n"),
8727c478bd9Sstevel@tonic-gate 		    fmri);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	return (0);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate /*
8787c478bd9Sstevel@tonic-gate  * modify_defaults takes n name=value pairs for inetd default property values,
8797c478bd9Sstevel@tonic-gate  * parses them, and then modifies the values in the repository.
8807c478bd9Sstevel@tonic-gate  */
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate static void
8837c478bd9Sstevel@tonic-gate modify_defaults(int argc, char *argv[])
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate 	int			i, j;
8867c478bd9Sstevel@tonic-gate 	char			*value;
8877c478bd9Sstevel@tonic-gate 	scf_instance_t		*inst;
8887c478bd9Sstevel@tonic-gate 	inetd_prop_t		*mod, *prop_table;
8897c478bd9Sstevel@tonic-gate 	size_t			numprops;
8907c478bd9Sstevel@tonic-gate 	ssize_t			max_val;
8917c478bd9Sstevel@tonic-gate 	int64_t			new_int;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL)
8947c478bd9Sstevel@tonic-gate 		scfdie();
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, INETD_INSTANCE_FMRI, NULL, NULL,
8977c478bd9Sstevel@tonic-gate 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) {
8987c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND) {
8997c478bd9Sstevel@tonic-gate 			uu_die(gettext("inetd instance missing in repository."
9007c478bd9Sstevel@tonic-gate 			    "\n"));
9017c478bd9Sstevel@tonic-gate 		} else {
9027c478bd9Sstevel@tonic-gate 			scfdie();
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if ((max_val = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH)) < 0)
9077c478bd9Sstevel@tonic-gate 		scfdie();
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	prop_table = get_prop_table(&numprops);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	if ((mod = malloc(numprops * sizeof (inetd_prop_t))) == NULL)
9127c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: Out of memory.\n"));
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	(void) memcpy(mod, prop_table, numprops * sizeof (inetd_prop_t));
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
9177c478bd9Sstevel@tonic-gate 		/* Separate argument into name and value pair */
9187c478bd9Sstevel@tonic-gate 		if ((value = strchr(argv[i], '=')) == NULL)
9197c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: Malformed name=value pair \"%s"
9207c478bd9Sstevel@tonic-gate 			    "\"\n"), argv[i]);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		*value = '\0';
9237c478bd9Sstevel@tonic-gate 		value++;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		/* Find property name in array of defaults */
926*ba1637f8Smh138676 		for (j = 0; mod[j].ip_name != NULL; j++) {
9277c478bd9Sstevel@tonic-gate 			if (!mod[j].ip_default)
9287c478bd9Sstevel@tonic-gate 				continue;
9297c478bd9Sstevel@tonic-gate 			if (strcmp(mod[j].ip_name, argv[i]) == 0)
9307c478bd9Sstevel@tonic-gate 				break;
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 
933*ba1637f8Smh138676 		if (mod[j].ip_name == NULL)
9347c478bd9Sstevel@tonic-gate 			uu_die(gettext("Error: \"%s\" is not a default inetd "
9357c478bd9Sstevel@tonic-gate 			    "property.\n"), argv[i]);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		if (*value == '\0')
9387c478bd9Sstevel@tonic-gate 			uu_die(gettext("Cannot accept NULL values for default "
9397c478bd9Sstevel@tonic-gate 			    "properties.\n"));
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		switch (mod[j].ip_type) {
942*ba1637f8Smh138676 		case INET_TYPE_INTEGER:
9437c478bd9Sstevel@tonic-gate 			if (uu_strtoint(value, &new_int, sizeof (new_int), NULL,
9447c478bd9Sstevel@tonic-gate 			    NULL, NULL) == -1)
9457c478bd9Sstevel@tonic-gate 				uu_die(gettext("Error: \"%s\" is not a valid "
9467c478bd9Sstevel@tonic-gate 				    "integer value.\n"), value);
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 			mod[j].ip_value.iv_int = new_int;
9497c478bd9Sstevel@tonic-gate 			break;
950*ba1637f8Smh138676 		case INET_TYPE_STRING:
9517c478bd9Sstevel@tonic-gate 			if (strlen(value) >= max_val)
9527c478bd9Sstevel@tonic-gate 				uu_die(gettext("Error: String value is longer "
9537c478bd9Sstevel@tonic-gate 				    "than %l characters.\n"), max_val);
954*ba1637f8Smh138676 			if ((mod[j].ip_value.iv_string = strdup(value))
9557c478bd9Sstevel@tonic-gate 			    == NULL)
9567c478bd9Sstevel@tonic-gate 				uu_die(gettext("Error: Out of memory.\n"));
9577c478bd9Sstevel@tonic-gate 			break;
958*ba1637f8Smh138676 		case INET_TYPE_BOOLEAN:
9597c478bd9Sstevel@tonic-gate 			if (strcasecmp(value, INETADM_TRUE_STR) == 0)
9607c478bd9Sstevel@tonic-gate 				mod[j].ip_value.iv_boolean = B_TRUE;
9617c478bd9Sstevel@tonic-gate 			else if (strcasecmp(value, INETADM_FALSE_STR) == 0)
9627c478bd9Sstevel@tonic-gate 				mod[j].ip_value.iv_boolean = B_FALSE;
9637c478bd9Sstevel@tonic-gate 			else
9647c478bd9Sstevel@tonic-gate 				uu_die(gettext("Error: \"%s\" is not a valid "
9657c478bd9Sstevel@tonic-gate 				    "boolean value. (TRUE or FALSE)\n"), value);
9667c478bd9Sstevel@tonic-gate 		}
9677c478bd9Sstevel@tonic-gate 		/* mark property for modification */
9687c478bd9Sstevel@tonic-gate 		mod[j].ip_error = IVE_VALID;
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	commit_props(inst, mod, B_TRUE);
9727c478bd9Sstevel@tonic-gate 	free(mod);
9737c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
9747c478bd9Sstevel@tonic-gate 	if (refresh_inetd() != 0)
9757c478bd9Sstevel@tonic-gate 		uu_warn(gettext("Warning: Unable to refresh inetd.\n"));
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate int
9797c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
9807c478bd9Sstevel@tonic-gate {
9817c478bd9Sstevel@tonic-gate 	int		opt;
9827c478bd9Sstevel@tonic-gate 	uint_t		lflag, eflag, dflag, pflag, mflag, Mflag;
9837c478bd9Sstevel@tonic-gate 	uint8_t		enable;
9847c478bd9Sstevel@tonic-gate 	scf_error_t	serr;
9857c478bd9Sstevel@tonic-gate 	int		exit_status = 0;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
9887c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
9917c478bd9Sstevel@tonic-gate 		scfdie();
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	if (scf_handle_bind(h) == -1)
9947c478bd9Sstevel@tonic-gate 		uu_die(gettext("Error: Couldn't bind to svc.configd.\n"));
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	if (argc == 1) {
9977c478bd9Sstevel@tonic-gate 		list_services();
9987c478bd9Sstevel@tonic-gate 		goto out;
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	lflag = eflag = dflag = pflag = mflag = Mflag = 0;
10027c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "ledpMm?")) != -1) {
10037c478bd9Sstevel@tonic-gate 		switch (opt) {
10047c478bd9Sstevel@tonic-gate 		case 'l':
10057c478bd9Sstevel@tonic-gate 			lflag = 1;
10067c478bd9Sstevel@tonic-gate 			break;
10077c478bd9Sstevel@tonic-gate 		case 'e':
10087c478bd9Sstevel@tonic-gate 			eflag = 1;
10097c478bd9Sstevel@tonic-gate 			break;
10107c478bd9Sstevel@tonic-gate 		case 'd':
10117c478bd9Sstevel@tonic-gate 			dflag = 1;
10127c478bd9Sstevel@tonic-gate 			break;
10137c478bd9Sstevel@tonic-gate 		case 'p':
10147c478bd9Sstevel@tonic-gate 			pflag = 1;
10157c478bd9Sstevel@tonic-gate 			break;
10167c478bd9Sstevel@tonic-gate 		case 'M':
10177c478bd9Sstevel@tonic-gate 			Mflag = 1;
10187c478bd9Sstevel@tonic-gate 			break;
10197c478bd9Sstevel@tonic-gate 		case 'm':
10207c478bd9Sstevel@tonic-gate 			mflag = 1;
10217c478bd9Sstevel@tonic-gate 			break;
10227c478bd9Sstevel@tonic-gate 		case '?':
10237c478bd9Sstevel@tonic-gate 			if (optopt == '?') {
10247c478bd9Sstevel@tonic-gate 				usage(B_TRUE);
10257c478bd9Sstevel@tonic-gate 				goto out;
10267c478bd9Sstevel@tonic-gate 			} else {
10277c478bd9Sstevel@tonic-gate 				usage(B_FALSE);
10287c478bd9Sstevel@tonic-gate 			}
10297c478bd9Sstevel@tonic-gate 		default:
10307c478bd9Sstevel@tonic-gate 			usage(B_FALSE);
10317c478bd9Sstevel@tonic-gate 		}
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	/*
10357c478bd9Sstevel@tonic-gate 	 * All options are mutually exclusive, and we must have an option
10367c478bd9Sstevel@tonic-gate 	 * if we reached here.
10377c478bd9Sstevel@tonic-gate 	 */
10387c478bd9Sstevel@tonic-gate 	if (lflag + eflag + dflag + pflag + mflag + Mflag != 1)
10397c478bd9Sstevel@tonic-gate 		usage(B_FALSE);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	argv += optind;
10427c478bd9Sstevel@tonic-gate 	argc -= optind;
10437c478bd9Sstevel@tonic-gate 	if ((pflag == 0) && (argc == 0))
10447c478bd9Sstevel@tonic-gate 		usage(B_FALSE);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	serr = 0;
10477c478bd9Sstevel@tonic-gate 	if (lflag) {
10487c478bd9Sstevel@tonic-gate 		serr = scf_walk_fmri(h, argc, argv, 0, list_props_cb, NULL,
10497c478bd9Sstevel@tonic-gate 		    &exit_status, uu_warn);
10507c478bd9Sstevel@tonic-gate 	} else if (dflag) {
10517c478bd9Sstevel@tonic-gate 		enable = 0;
10527c478bd9Sstevel@tonic-gate 		serr = scf_walk_fmri(h, argc, argv, 0, set_svc_enable_cb,
10537c478bd9Sstevel@tonic-gate 		    &enable, &exit_status, uu_warn);
10547c478bd9Sstevel@tonic-gate 	} else if (eflag) {
10557c478bd9Sstevel@tonic-gate 		enable = 1;
10567c478bd9Sstevel@tonic-gate 		serr = scf_walk_fmri(h, argc, argv, 0, set_svc_enable_cb,
10577c478bd9Sstevel@tonic-gate 		    &enable, &exit_status, uu_warn);
10587c478bd9Sstevel@tonic-gate 	} else if (mflag) {
10597c478bd9Sstevel@tonic-gate 		arglist_t	args;
10607c478bd9Sstevel@tonic-gate 		char		**cpp = argv;
10617c478bd9Sstevel@tonic-gate 		uint_t		fmri_args = 0;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 		/* count number of fmri arguments */
10647c478bd9Sstevel@tonic-gate 		while ((fmri_args < argc) && (strchr(*cpp, '=') == NULL)) {
10657c478bd9Sstevel@tonic-gate 			fmri_args++;
10667c478bd9Sstevel@tonic-gate 			cpp++;
10677c478bd9Sstevel@tonic-gate 		}
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 		/* if no x=y args or no fmri, show usage */
10707c478bd9Sstevel@tonic-gate 		if ((fmri_args == argc) || (fmri_args == 0))
10717c478bd9Sstevel@tonic-gate 			usage(B_FALSE);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 		/* setup args for modify_inst_props_cb */
10747c478bd9Sstevel@tonic-gate 		args.argc = argc - fmri_args;
10757c478bd9Sstevel@tonic-gate 		args.argv = argv + fmri_args;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 		serr = scf_walk_fmri(h, fmri_args, argv, 0,
10787c478bd9Sstevel@tonic-gate 		    modify_inst_props_cb, &args, &exit_status, uu_warn);
10797c478bd9Sstevel@tonic-gate 	} else if (Mflag) {
10807c478bd9Sstevel@tonic-gate 		modify_defaults(argc, argv);
10817c478bd9Sstevel@tonic-gate 	} else if (pflag) {
10827c478bd9Sstevel@tonic-gate 		/* ensure there's no trailing garbage */
10837c478bd9Sstevel@tonic-gate 		if (argc != 0)
10847c478bd9Sstevel@tonic-gate 			usage(B_FALSE);
10857c478bd9Sstevel@tonic-gate 		list_defaults();
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate 	if (serr != 0) {
10887c478bd9Sstevel@tonic-gate 		uu_warn(gettext("failed to iterate over instances: %s"),
10897c478bd9Sstevel@tonic-gate 		    scf_strerror(serr));
10907c478bd9Sstevel@tonic-gate 		exit(UU_EXIT_FATAL);
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate out:
10947c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
10957c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	return (exit_status);
10987c478bd9Sstevel@tonic-gate }
1099