xref: /illumos-gate/usr/src/cmd/acctadm/aconf.c (revision bbf21555)
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
5074e084fSml  * Common Development and Distribution License (the "License").
6074e084fSml  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22074e084fSml  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/acctctl.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <limits.h>
33da14cebeSEric Cheng #include <libdllink.h>
34074e084fSml #include <libscf.h>
35074e084fSml #include <pwd.h>
36074e084fSml #include <auth_attr.h>
37074e084fSml #include <nss_dbdefs.h>
38074e084fSml #include <secdb.h>
39074e084fSml #include <priv.h>
40074e084fSml #include <zone.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "aconf.h"
437c478bd9Sstevel@tonic-gate #include "utils.h"
447c478bd9Sstevel@tonic-gate #include "res.h"
457c478bd9Sstevel@tonic-gate 
46074e084fSml #define	FMRI_FLOW_ACCT	"svc:/system/extended-accounting:flow"
47074e084fSml #define	FMRI_PROC_ACCT	"svc:/system/extended-accounting:process"
48074e084fSml #define	FMRI_TASK_ACCT	"svc:/system/extended-accounting:task"
49da14cebeSEric Cheng #define	FMRI_NET_ACCT	"svc:/system/extended-accounting:net"
50074e084fSml 
51074e084fSml #define	NELEM(x)	(sizeof (x)) / (sizeof (x[0]))
52074e084fSml 
53074e084fSml typedef struct props {
54074e084fSml 	char *propname;
55074e084fSml 	int proptype;
56074e084fSml 	scf_transaction_entry_t *entry;
57074e084fSml 	scf_value_t *value;
58074e084fSml 	struct props *next;
59074e084fSml } props_t;
60074e084fSml 
61074e084fSml static void	aconf_print_type(acctconf_t *, FILE *, int);
62074e084fSml static int	aconf_get_bool(const char *, const char *, uint8_t *);
63074e084fSml static int	aconf_get_string(const char *, const char *, char *, size_t);
64074e084fSml static props_t	*aconf_prop(const char *, int);
65074e084fSml static int	aconf_fmri2type(const char *);
66074e084fSml 
67074e084fSml static scf_handle_t	*handle = NULL;
68074e084fSml static scf_instance_t	*inst = NULL;
69074e084fSml static props_t		*props = NULL;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void
aconf_init(acctconf_t * acp,int type)72074e084fSml aconf_init(acctconf_t *acp, int type)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	void *buf;
75074e084fSml 	char *tracked;
76074e084fSml 	char *untracked;
777c478bd9Sstevel@tonic-gate 
78074e084fSml 	if ((buf = malloc(AC_BUFSIZE)) == NULL)
797c478bd9Sstevel@tonic-gate 		die(gettext("not enough memory\n"));
807c478bd9Sstevel@tonic-gate 
81074e084fSml 	if (acctctl(type | AC_STATE_GET, &acp->state,
82074e084fSml 	    sizeof (acp->state)) == -1)
83074e084fSml 		die(gettext("cannot get %s accounting state\n"),
84074e084fSml 		    ac_type_name(type));
857c478bd9Sstevel@tonic-gate 
86074e084fSml 	(void) memset(acp->file, 0, sizeof (acp->file));
87074e084fSml 	if (acctctl(type | AC_FILE_GET, acp->file, sizeof (acp->file)) == -1) {
887c478bd9Sstevel@tonic-gate 		if (errno == ENOTACTIVE)
89074e084fSml 			(void) strlcpy(acp->file, AC_STR_NONE,
90074e084fSml 			    sizeof (acp->file));
917c478bd9Sstevel@tonic-gate 		else
92074e084fSml 			die(gettext("cannot get %s accounting file name"),
93074e084fSml 			    ac_type_name(type));
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	(void) memset(buf, 0, AC_BUFSIZE);
96074e084fSml 	if (acctctl(type | AC_RES_GET, buf, AC_BUFSIZE) == -1)
977c478bd9Sstevel@tonic-gate 		die(gettext("cannot obtain the list of enabled resources\n"));
987c478bd9Sstevel@tonic-gate 
99074e084fSml 	tracked = buf2str(buf, AC_BUFSIZE, AC_ON, type);
100074e084fSml 	untracked = buf2str(buf, AC_BUFSIZE, AC_OFF, type);
101074e084fSml 	(void) strlcpy(acp->tracked, tracked, sizeof (acp->tracked));
102074e084fSml 	(void) strlcpy(acp->untracked, untracked, sizeof (acp->untracked));
1037c478bd9Sstevel@tonic-gate 	free(tracked);
1047c478bd9Sstevel@tonic-gate 	free(untracked);
105074e084fSml 	free(buf);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
108074e084fSml /*
109074e084fSml  * SMF start method: configure extended accounting from properties stored in
110074e084fSml  * the repository.  Any errors encountered while retrieving properties from
111074e084fSml  * the repository, such as missing properties or properties of the wrong type,
112074e084fSml  * are fatal as they indicate severe damage to the service (all required
113074e084fSml  * properties are delivered in the service manifest and should thus always be
114074e084fSml  * present).  No attempts will be made to repair such damage;  the service will
115074e084fSml  * be forced into maintenance state by returning SMF_EXIT_ERR_CONFIG.  For all
116074e084fSml  * other errors we we try to configure as much as possible and return
117074e084fSml  * SMF_EXIT_ERR_FATAL.
118074e084fSml  */
1197c478bd9Sstevel@tonic-gate int
aconf_setup(const char * fmri)120074e084fSml aconf_setup(const char *fmri)
1217c478bd9Sstevel@tonic-gate {
122074e084fSml 	char file[MAXPATHLEN];
123074e084fSml 	char tracked[MAXRESLEN];
124074e084fSml 	char untracked[MAXRESLEN];
1257c478bd9Sstevel@tonic-gate 	void *buf;
126074e084fSml 	int type;
127074e084fSml 	int state;
128074e084fSml 	uint8_t b;
129074e084fSml 	int ret = SMF_EXIT_OK;
1307c478bd9Sstevel@tonic-gate 
131074e084fSml 	if ((type = aconf_fmri2type(fmri)) == -1) {
132074e084fSml 		warn(gettext("no accounting type for %s\n"), fmri);
133074e084fSml 		return (SMF_EXIT_ERR_FATAL);
134074e084fSml 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
137da14cebeSEric Cheng 	 * Net/Flow accounting is not available in non-global zones and
138074e084fSml 	 * the service instance should therefore never be 'enabled' in
139*bbf21555SRichard Lowe 	 * non-global zones.  This is enforced by acctadm(8), but there is
140074e084fSml 	 * nothing that prevents someone from calling svcadm enable directly,
141074e084fSml 	 * so we handle that case here by disabling the instance.
1427c478bd9Sstevel@tonic-gate 	 */
143da14cebeSEric Cheng 	if ((type == AC_FLOW || type == AC_NET) &&
144da14cebeSEric Cheng 	    getzoneid() != GLOBAL_ZONEID) {
145074e084fSml 		(void) smf_disable_instance(fmri, 0);
146074e084fSml 		warn(gettext("%s accounting cannot be configured in "
147074e084fSml 		    "non-global zones\n"), ac_type_name(type));
148074e084fSml 		return (SMF_EXIT_OK);
1497c478bd9Sstevel@tonic-gate 	}
150074e084fSml 
151074e084fSml 	if (aconf_scf_init(fmri) == -1) {
152074e084fSml 		warn(gettext("cannot connect to repository\n"));
153074e084fSml 		return (SMF_EXIT_ERR_FATAL);
1547c478bd9Sstevel@tonic-gate 	}
155074e084fSml 	if (aconf_get_string(AC_PGNAME, AC_PROP_TRACKED, tracked,
156074e084fSml 	    sizeof (tracked)) == -1) {
157074e084fSml 		warn(gettext("cannot get %s property\n"), AC_PROP_TRACKED);
158074e084fSml 		ret = SMF_EXIT_ERR_CONFIG;
159074e084fSml 		goto out;
1607c478bd9Sstevel@tonic-gate 	}
161074e084fSml 	if (aconf_get_string(AC_PGNAME, AC_PROP_UNTRACKED, untracked,
162074e084fSml 	    sizeof (untracked)) == -1) {
163074e084fSml 		warn(gettext("cannot get %s property\n"), AC_PROP_UNTRACKED);
164074e084fSml 		ret = SMF_EXIT_ERR_CONFIG;
165074e084fSml 		goto out;
1667c478bd9Sstevel@tonic-gate 	}
167074e084fSml 	if (aconf_get_string(AC_PGNAME, AC_PROP_FILE, file,
168074e084fSml 	    sizeof (file)) == -1) {
169074e084fSml 		warn(gettext("cannot get %s property\n"), AC_PROP_FILE);
170074e084fSml 		ret = SMF_EXIT_ERR_CONFIG;
171074e084fSml 		goto out;
1727c478bd9Sstevel@tonic-gate 	}
173074e084fSml 	if (aconf_get_bool(AC_PGNAME, AC_PROP_STATE, &b) == -1) {
174074e084fSml 		warn(gettext("cannot get %s property\n"), AC_PROP_STATE);
175074e084fSml 		ret = SMF_EXIT_ERR_CONFIG;
176074e084fSml 		goto out;
1777c478bd9Sstevel@tonic-gate 	}
178074e084fSml 	state = (b ? AC_ON : AC_OFF);
1797c478bd9Sstevel@tonic-gate 
180074e084fSml 	if ((buf = malloc(AC_BUFSIZE)) == NULL) {
181074e084fSml 		warn(gettext("not enough memory\n"));
182074e084fSml 		ret = SMF_EXIT_ERR_FATAL;
183074e084fSml 		goto out;
184074e084fSml 	}
1857c478bd9Sstevel@tonic-gate 	(void) memset(buf, 0, AC_BUFSIZE);
186074e084fSml 	str2buf(buf, untracked, AC_OFF, type);
187074e084fSml 	str2buf(buf, tracked, AC_ON, type);
188074e084fSml 
189074e084fSml 	(void) priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_SYS_ACCT, NULL);
190074e084fSml 	if (acctctl(type | AC_RES_SET, buf, AC_BUFSIZE) == -1) {
191074e084fSml 		warn(gettext("cannot enable/disable %s accounting resources"),
192074e084fSml 		    ac_type_name(type));
193074e084fSml 		ret = SMF_EXIT_ERR_FATAL;
1947c478bd9Sstevel@tonic-gate 	}
195074e084fSml 	free(buf);
196074e084fSml 
197074e084fSml 	if (strcmp(file, AC_STR_NONE) != 0) {
198074e084fSml 		if (open_exacct_file(file, type) == -1)
199074e084fSml 			ret = SMF_EXIT_ERR_FATAL;
2007c478bd9Sstevel@tonic-gate 	} else {
201074e084fSml 		if (acctctl(type | AC_FILE_SET, NULL, 0) == -1) {
202074e084fSml 			warn(gettext("cannot close %s accounting file"),
203074e084fSml 			    ac_type_name(type));
204074e084fSml 			ret = SMF_EXIT_ERR_FATAL;
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 	}
207074e084fSml 	if (acctctl(type | AC_STATE_SET, &state, sizeof (state)) == -1) {
208074e084fSml 		warn(gettext("cannot %s %s accounting"),
209074e084fSml 		    state == AC_ON ? gettext("enable") : gettext("disable"),
210074e084fSml 		    ac_type_name(type));
211074e084fSml 		ret = SMF_EXIT_ERR_FATAL;
2127c478bd9Sstevel@tonic-gate 	}
213074e084fSml 	(void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_SYS_ACCT, NULL);
214da14cebeSEric Cheng 
215da14cebeSEric Cheng 	if (state == AC_ON && type == AC_NET) {
216da14cebeSEric Cheng 		/*
217da14cebeSEric Cheng 		 * Start logging.
218da14cebeSEric Cheng 		 */
219da14cebeSEric Cheng 		(void) priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_SYS_DL_CONFIG,
220da14cebeSEric Cheng 		    NULL);
2214ac67f02SAnurag S. Maskey 		(void) dladm_start_usagelog(dld_handle,
2224ac67f02SAnurag S. Maskey 		    strncmp(tracked, "basic", strlen("basic")) == 0 ?
2234ac67f02SAnurag S. Maskey 		    DLADM_LOGTYPE_LINK : DLADM_LOGTYPE_FLOW, 20);
224da14cebeSEric Cheng 		(void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_SYS_DL_CONFIG,
225da14cebeSEric Cheng 		    NULL);
226da14cebeSEric Cheng 	}
227074e084fSml out:
228074e084fSml 	aconf_scf_fini();
229074e084fSml 	return (ret);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
232074e084fSml void
aconf_print(FILE * fp,int types)233074e084fSml aconf_print(FILE *fp, int types)
2347c478bd9Sstevel@tonic-gate {
235074e084fSml 	acctconf_t ac;
236da14cebeSEric Cheng 	int print_order[] = { AC_TASK, AC_PROC, AC_FLOW, AC_NET };
237074e084fSml 	int i;
238074e084fSml 
239074e084fSml 	for (i = 0; i < NELEM(print_order); i++) {
240074e084fSml 		if (types & print_order[i]) {
241074e084fSml 			aconf_init(&ac, print_order[i]);
242074e084fSml 			aconf_print_type(&ac, fp, print_order[i]);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
247074e084fSml static void
aconf_print_type(acctconf_t * acp,FILE * fp,int type)248074e084fSml aconf_print_type(acctconf_t *acp, FILE *fp, int type)
2497c478bd9Sstevel@tonic-gate {
250074e084fSml 	switch (type) {
251074e084fSml 	case AC_TASK:
2527c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2537c478bd9Sstevel@tonic-gate 		    gettext("            Task accounting: %s\n"),
254074e084fSml 		    acp->state == AC_ON ?
2557c478bd9Sstevel@tonic-gate 		    gettext("active") : gettext("inactive"));
2567c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2577c478bd9Sstevel@tonic-gate 		    gettext("       Task accounting file: %s\n"),
258074e084fSml 		    acp->file);
2597c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2607c478bd9Sstevel@tonic-gate 		    gettext("     Tracked task resources: %s\n"),
261074e084fSml 		    acp->tracked);
2627c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2637c478bd9Sstevel@tonic-gate 		    gettext("   Untracked task resources: %s\n"),
264074e084fSml 		    acp->untracked);
265074e084fSml 		break;
266074e084fSml 	case AC_PROC:
2677c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2687c478bd9Sstevel@tonic-gate 		    gettext("         Process accounting: %s\n"),
269074e084fSml 		    acp->state == AC_ON ?
2707c478bd9Sstevel@tonic-gate 		    gettext("active") : gettext("inactive"));
2717c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2727c478bd9Sstevel@tonic-gate 		    gettext("    Process accounting file: %s\n"),
273074e084fSml 		    acp->file);
2747c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2757c478bd9Sstevel@tonic-gate 		    gettext("  Tracked process resources: %s\n"),
276074e084fSml 		    acp->tracked);
2777c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2787c478bd9Sstevel@tonic-gate 		    gettext("Untracked process resources: %s\n"),
279074e084fSml 		    acp->untracked);
280074e084fSml 		break;
281074e084fSml 	case AC_FLOW:
2827c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2837c478bd9Sstevel@tonic-gate 		    gettext("            Flow accounting: %s\n"),
284074e084fSml 		    acp->state == AC_ON ?
2857c478bd9Sstevel@tonic-gate 		    gettext("active") : gettext("inactive"));
2867c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2877c478bd9Sstevel@tonic-gate 		    gettext("       Flow accounting file: %s\n"),
288074e084fSml 		    acp->file);
2897c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2907c478bd9Sstevel@tonic-gate 		    gettext("     Tracked flow resources: %s\n"),
291074e084fSml 		    acp->tracked);
2927c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2937c478bd9Sstevel@tonic-gate 		    gettext("   Untracked flow resources: %s\n"),
294074e084fSml 		    acp->untracked);
295074e084fSml 		break;
296da14cebeSEric Cheng 	case AC_NET:
297da14cebeSEric Cheng 		(void) fprintf(fp,
298da14cebeSEric Cheng 		    gettext("            Net accounting: %s\n"),
299da14cebeSEric Cheng 		    acp->state == AC_ON ?
300da14cebeSEric Cheng 		    gettext("active") : gettext("inactive"));
301da14cebeSEric Cheng 		(void) fprintf(fp,
302da14cebeSEric Cheng 		    gettext("       Net accounting file: %s\n"),
303da14cebeSEric Cheng 		    acp->file);
304da14cebeSEric Cheng 		(void) fprintf(fp,
305da14cebeSEric Cheng 		    gettext("     Tracked net resources: %s\n"),
306da14cebeSEric Cheng 		    acp->tracked);
307da14cebeSEric Cheng 		(void) fprintf(fp,
308da14cebeSEric Cheng 		    gettext("   Untracked net resources: %s\n"),
309da14cebeSEric Cheng 		    acp->untracked);
310da14cebeSEric Cheng 		break;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
314074e084fSml /*
315074e084fSml  * Modified properties are put on the 'props' linked list by aconf_set_string()
316074e084fSml  * and aconf_set_bool().  Walk the list of modified properties and write them
317074e084fSml  * to the repository.  The list is deleted on exit.
318074e084fSml  */
3197c478bd9Sstevel@tonic-gate int
aconf_save(void)320074e084fSml aconf_save(void)
3217c478bd9Sstevel@tonic-gate {
322074e084fSml 	scf_propertygroup_t *pg;
323074e084fSml 	scf_transaction_t *tx;
324074e084fSml 	props_t *p;
325074e084fSml 	props_t *q;
326074e084fSml 	int tx_result;
3277c478bd9Sstevel@tonic-gate 
328074e084fSml 	if (props == NULL)
329074e084fSml 		return (0);
330074e084fSml 
331074e084fSml 	if ((pg = scf_pg_create(handle)) == NULL ||
332074e084fSml 	    scf_instance_get_pg(inst, AC_PGNAME, pg) == -1 ||
333074e084fSml 	    (tx = scf_transaction_create(handle)) == NULL)
334074e084fSml 		goto out;
335074e084fSml 
336074e084fSml 	do {
337074e084fSml 		if (scf_pg_update(pg) == -1 ||
338074e084fSml 		    scf_transaction_start(tx, pg) == -1)
339074e084fSml 			goto out;
340074e084fSml 
341074e084fSml 		for (p = props; p != NULL; p = p->next) {
342074e084fSml 			if (scf_transaction_property_change(tx, p->entry,
343074e084fSml 			    p->propname, p->proptype) == -1)
344074e084fSml 				goto out;
345074e084fSml 			(void) scf_entry_add_value(p->entry, p->value);
346074e084fSml 		}
347074e084fSml 		tx_result = scf_transaction_commit(tx);
348074e084fSml 		scf_transaction_reset(tx);
349074e084fSml 	} while (tx_result == 0);
350074e084fSml 
351074e084fSml out:
352074e084fSml 	p = props;
353074e084fSml 	while (p != NULL) {
354074e084fSml 		scf_value_destroy(p->value);
355074e084fSml 		scf_entry_destroy(p->entry);
356074e084fSml 		free(p->propname);
357074e084fSml 		q = p->next;
358074e084fSml 		free(p);
359074e084fSml 		p = q;
360074e084fSml 	}
361074e084fSml 	props = NULL;
362074e084fSml 	scf_transaction_destroy(tx);
363074e084fSml 	scf_pg_destroy(pg);
364074e084fSml 	return ((tx_result == 1) ? 0 : -1);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
367074e084fSml boolean_t
aconf_have_smf_auths(void)368074e084fSml aconf_have_smf_auths(void)
3697c478bd9Sstevel@tonic-gate {
370074e084fSml 	char auth[NSS_BUFLEN_AUTHATTR];
371074e084fSml 	struct passwd *pw;
372074e084fSml 
373074e084fSml 	if ((pw = getpwuid(getuid())) == NULL)
374074e084fSml 		return (B_FALSE);
375074e084fSml 
376074e084fSml 	if (aconf_get_string("general", "action_authorization", auth,
377074e084fSml 	    sizeof (auth)) == -1 || chkauthattr(auth, pw->pw_name) == 0)
378074e084fSml 		return (B_FALSE);
379074e084fSml 
380074e084fSml 	if (aconf_get_string("general", "value_authorization", auth,
381074e084fSml 	    sizeof (auth)) == -1 || chkauthattr(auth, pw->pw_name) == 0)
382074e084fSml 		return (B_FALSE);
383074e084fSml 
384074e084fSml 	if (aconf_get_string("config", "value_authorization", auth,
385074e084fSml 	    sizeof (auth)) == -1 || chkauthattr(auth, pw->pw_name) == 0)
386074e084fSml 		return (B_FALSE);
387074e084fSml 
388074e084fSml 	return (B_TRUE);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
391074e084fSml const char *
aconf_type2fmri(int type)392074e084fSml aconf_type2fmri(int type)
3937c478bd9Sstevel@tonic-gate {
394074e084fSml 	switch (type) {
395074e084fSml 	case AC_PROC:
396074e084fSml 		return (FMRI_PROC_ACCT);
397074e084fSml 	case AC_TASK:
398074e084fSml 		return (FMRI_TASK_ACCT);
399074e084fSml 	case AC_FLOW:
400074e084fSml 		return (FMRI_FLOW_ACCT);
401da14cebeSEric Cheng 	case AC_NET:
402da14cebeSEric Cheng 		return (FMRI_NET_ACCT);
403074e084fSml 	default:
404074e084fSml 		die(gettext("invalid type %d\n"), type);
405074e084fSml 	}
406074e084fSml 	/* NOTREACHED */
407074e084fSml 	return (NULL);
408074e084fSml }
409074e084fSml 
410074e084fSml static int
aconf_fmri2type(const char * fmri)411074e084fSml aconf_fmri2type(const char *fmri)
412074e084fSml {
413074e084fSml 	if (strcmp(fmri, FMRI_PROC_ACCT) == 0)
414074e084fSml 		return (AC_PROC);
415074e084fSml 	else if (strcmp(fmri, FMRI_TASK_ACCT) == 0)
416074e084fSml 		return (AC_TASK);
417074e084fSml 	else if (strcmp(fmri, FMRI_FLOW_ACCT) == 0)
418074e084fSml 		return (AC_FLOW);
419da14cebeSEric Cheng 	else if (strcmp(fmri, FMRI_NET_ACCT) == 0)
420da14cebeSEric Cheng 		return (AC_NET);
4217c478bd9Sstevel@tonic-gate 	else
4227c478bd9Sstevel@tonic-gate 		return (-1);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate int
aconf_scf_init(const char * fmri)426074e084fSml aconf_scf_init(const char *fmri)
4277c478bd9Sstevel@tonic-gate {
428074e084fSml 	if ((handle = scf_handle_create(SCF_VERSION)) == NULL ||
429074e084fSml 	    scf_handle_bind(handle) == -1 ||
430074e084fSml 	    (inst = scf_instance_create(handle)) == NULL ||
431074e084fSml 	    scf_handle_decode_fmri(handle, fmri, NULL, NULL, inst, NULL, NULL,
432074e084fSml 	    SCF_DECODE_FMRI_EXACT) == -1) {
433074e084fSml 		aconf_scf_fini();
4347c478bd9Sstevel@tonic-gate 		return (-1);
435074e084fSml 	}
4367c478bd9Sstevel@tonic-gate 	return (0);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
439074e084fSml void
aconf_scf_fini(void)440074e084fSml aconf_scf_fini(void)
441074e084fSml {
442074e084fSml 	scf_instance_destroy(inst);
443074e084fSml 	(void) scf_handle_unbind(handle);
444074e084fSml 	scf_handle_destroy(handle);
445074e084fSml }
446074e084fSml 
4477c478bd9Sstevel@tonic-gate static int
aconf_get_string(const char * pgname,const char * propname,char * buf,size_t len)448074e084fSml aconf_get_string(const char *pgname, const char *propname, char *buf,
449074e084fSml     size_t len)
4507c478bd9Sstevel@tonic-gate {
451074e084fSml 	scf_propertygroup_t *pg;
452074e084fSml 	scf_property_t *prop;
453074e084fSml 	scf_value_t *value;
454074e084fSml 	int ret = 0;
455074e084fSml 
456074e084fSml 	if ((pg = scf_pg_create(handle)) == NULL)
457074e084fSml 		return (-1);
458074e084fSml 
459074e084fSml 	if (scf_instance_get_pg_composed(inst, NULL, pgname, pg) == -1) {
460074e084fSml 		scf_pg_destroy(pg);
4617c478bd9Sstevel@tonic-gate 		return (-1);
462074e084fSml 	}
463074e084fSml 
464074e084fSml 	if ((prop = scf_property_create(handle)) == NULL ||
465074e084fSml 	    (value = scf_value_create(handle)) == NULL ||
466074e084fSml 	    scf_pg_get_property(pg, propname, prop) == -1 ||
467074e084fSml 	    scf_property_get_value(prop, value) == -1 ||
468074e084fSml 	    scf_value_get_astring(value, buf, len) == -1)
469074e084fSml 		ret = -1;
470074e084fSml 
471074e084fSml 	scf_value_destroy(value);
472074e084fSml 	scf_property_destroy(prop);
473074e084fSml 	scf_pg_destroy(pg);
474074e084fSml 	return (ret);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate static int
aconf_get_bool(const char * pgname,const char * propname,uint8_t * rval)478074e084fSml aconf_get_bool(const char *pgname, const char *propname, uint8_t *rval)
4797c478bd9Sstevel@tonic-gate {
480074e084fSml 	scf_propertygroup_t *pg;
481074e084fSml 	scf_property_t *prop;
482074e084fSml 	scf_value_t *value;
483074e084fSml 	int ret = 0;
484074e084fSml 
485074e084fSml 	if ((pg = scf_pg_create(handle)) == NULL)
4867c478bd9Sstevel@tonic-gate 		return (-1);
487074e084fSml 
488074e084fSml 	if (scf_instance_get_pg_composed(inst, NULL, pgname, pg) == -1) {
489074e084fSml 		scf_pg_destroy(pg);
490074e084fSml 		return (-1);
491074e084fSml 	}
492074e084fSml 
493074e084fSml 	if ((prop = scf_property_create(handle)) == NULL ||
494074e084fSml 	    (value = scf_value_create(handle)) == NULL ||
495074e084fSml 	    scf_pg_get_property(pg, propname, prop) == -1 ||
496074e084fSml 	    scf_property_get_value(prop, value) == -1 ||
497074e084fSml 	    scf_value_get_boolean(value, rval) == -1)
498074e084fSml 		ret = -1;
499074e084fSml 
500074e084fSml 	scf_value_destroy(value);
501074e084fSml 	scf_property_destroy(prop);
502074e084fSml 	scf_pg_destroy(pg);
503074e084fSml 	return (ret);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
506074e084fSml int
aconf_set_string(const char * propname,const char * value)507074e084fSml aconf_set_string(const char *propname, const char *value)
5087c478bd9Sstevel@tonic-gate {
509074e084fSml 	props_t *p;
510074e084fSml 
511074e084fSml 	if ((p = aconf_prop(propname, SCF_TYPE_ASTRING)) == NULL)
5127c478bd9Sstevel@tonic-gate 		return (-1);
513074e084fSml 
514074e084fSml 	if (scf_value_set_astring(p->value, value) == -1)
515074e084fSml 		return (-1);
516074e084fSml 	return (0);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
519074e084fSml int
aconf_set_bool(const char * propname,boolean_t value)520074e084fSml aconf_set_bool(const char *propname, boolean_t value)
5217c478bd9Sstevel@tonic-gate {
522074e084fSml 	props_t *p;
523074e084fSml 
524074e084fSml 	if ((p = aconf_prop(propname, SCF_TYPE_BOOLEAN)) == NULL)
5257c478bd9Sstevel@tonic-gate 		return (-1);
526074e084fSml 
527074e084fSml 	scf_value_set_boolean(p->value, value);
528074e084fSml 	return (0);
529074e084fSml }
530074e084fSml 
531074e084fSml static props_t *
aconf_prop(const char * propname,int proptype)532074e084fSml aconf_prop(const char *propname, int proptype)
533074e084fSml {
534074e084fSml 	props_t *p;
535074e084fSml 
536074e084fSml 	if ((p = malloc(sizeof (props_t))) != NULL) {
537074e084fSml 		if ((p->propname = strdup(propname)) == NULL) {
538074e084fSml 			free(p);
539074e084fSml 			return (NULL);
540074e084fSml 		}
541074e084fSml 		if ((p->entry = scf_entry_create(handle)) == NULL) {
542074e084fSml 			free(p->propname);
543074e084fSml 			free(p);
544074e084fSml 			return (NULL);
545074e084fSml 		}
546074e084fSml 		if ((p->value = scf_value_create(handle)) == NULL) {
547074e084fSml 			scf_entry_destroy(p->entry);
548074e084fSml 			free(p->propname);
549074e084fSml 			free(p);
550074e084fSml 			return (NULL);
551074e084fSml 		}
552074e084fSml 		p->proptype = proptype;
553074e084fSml 		p->next = props;
554074e084fSml 		props = p;
555074e084fSml 	}
556074e084fSml 	return (p);
5577c478bd9Sstevel@tonic-gate }
558