1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
  23  */
  24 /*
  25  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  26  */
  27 
  28 
  29 /*
  30  * XML document manipulation routines
  31  *
  32  * These routines provide translation to and from the internal representation to
  33  * XML.  Directionally-oriented verbs are with respect to the external source,
  34  * so lxml_get_service() fetches a service from the XML file into the
  35  * internal representation.
  36  */
  37 
  38 #include <libxml/parser.h>
  39 #include <libxml/xinclude.h>
  40 
  41 #include <assert.h>
  42 #include <ctype.h>
  43 #include <errno.h>
  44 #include <libintl.h>
  45 #include <libscf.h>
  46 #include <libscf_priv.h>
  47 #include <libuutil.h>
  48 #include <sasl/saslutil.h>
  49 #include <stdlib.h>
  50 #include <string.h>
  51 #include <limits.h>
  52 
  53 #include <sys/types.h>
  54 #include <sys/stat.h>
  55 #include <unistd.h>
  56 
  57 #include <sys/param.h>
  58 #include "manifest_hash.h"
  59 
  60 #include "svccfg.h"
  61 #include "notify_params.h"
  62 
  63 /*
  64  * snprintf(3C) format strings for constructing property names that include
  65  * the locale designation.  Use %s to indicate where the locale should go.
  66  *
  67  * The VALUE_* symbols are an exception.  The firs %s will be replaced with
  68  * "value_".  The second %s will be replaced by the name of the value and
  69  * %%s will be replaced by the locale designation.  These formats are
  70  * processed twice by snprintf(3C).  The first time captures the value name
  71  * and the second time captures the locale.
  72  */
  73 #define LOCALE_ONLY_FMT         ("%s")
  74 #define COMMON_NAME_FMT         ("common_name_%s")
  75 #define DESCRIPTION_FMT         ("description_%s")
  76 #define UNITS_FMT               ("units_%s")
  77 #define VALUE_COMMON_NAME_FMT   ("%s%s_common_name_%%s")
  78 #define VALUE_DESCRIPTION_FMT   ("%s%s_description_%%s")
  79 
  80 /* Attribute names */
  81 const char * const delete_attr = "delete";
  82 const char * const enabled_attr = "enabled";
  83 const char * const lang_attr = "lang";
  84 const char * const manpath_attr = "manpath";
  85 const char * const max_attr = "max";
  86 const char * const min_attr = "min";
  87 const char * const name_attr = "name";
  88 const char * const override_attr = "override";
  89 const char * const required_attr = "required";
  90 const char * const section_attr = "section";
  91 const char * const set_attr = "set";
  92 const char * const target_attr = "target";
  93 const char * const timeout_seconds_attr = "timeout_seconds";
  94 const char * const title_attr = "title";
  95 const char * const type_attr = "type";
  96 const char * const uri_attr = "uri";
  97 const char * const value_attr = "value";
  98 const char * const version_attr = "version";
  99 const char * const xml_lang_attr = "xml:lang";
 100 const char * const active_attr = "active";
 101 
 102 /* Attribute values */
 103 const char * const all_value = "all";
 104 
 105 const char * const true = "true";
 106 const char * const false = "false";
 107 
 108 /*
 109  * The following list must be kept in the same order as that of
 110  * element_t array
 111  */
 112 static const char *lxml_elements[] = {
 113         "astring_list",                 /* SC_ASTRING */
 114         "boolean_list",                 /* SC_BOOLEAN */
 115         "cardinality",                  /* SC_CARDINALITY */
 116         "choices",                      /* SC_CHOICES */
 117         "common_name",                  /* SC_COMMON_NAME */
 118         "constraints",                  /* SC_CONSTRAINTS */
 119         "count_list",                   /* SC_COUNT */
 120         "create_default_instance",      /* SC_INSTANCE_CREATE_DEFAULT */
 121         "dependency",                   /* SC_DEPENDENCY */
 122         "dependent",                    /* SC_DEPENDENT */
 123         "description",                  /* SC_DESCRIPTION */
 124         "doc_link",                     /* SC_DOC_LINK */
 125         "documentation",                /* SC_DOCUMENTATION */
 126         "enabled",                      /* SC_ENABLED */
 127         "event",                        /* SC_EVENT */
 128         "exec_method",                  /* SC_EXEC_METHOD */
 129         "fmri_list",                    /* SC_FMRI */
 130         "host_list",                    /* SC_HOST */
 131         "hostname_list",                /* SC_HOSTNAME */
 132         "include_values",               /* SC_INCLUDE_VALUES */
 133         "instance",                     /* SC_INSTANCE */
 134         "integer_list",                 /* SC_INTEGER */
 135         "internal_separators",          /* SC_INTERNAL_SEPARATORS */
 136         "loctext",                      /* SC_LOCTEXT */
 137         "manpage",                      /* SC_MANPAGE */
 138         "method_context",               /* SC_METHOD_CONTEXT */
 139         "method_credential",            /* SC_METHOD_CREDENTIAL */
 140         "method_profile",               /* SC_METHOD_PROFILE */
 141         "method_environment",           /* SC_METHOD_ENVIRONMENT */
 142         "envvar",                       /* SC_METHOD_ENVVAR */
 143         "net_address_list",             /* SC_NET_ADDR */
 144         "net_address_v4_list",          /* SC_NET_ADDR_V4 */
 145         "net_address_v6_list",          /* SC_NET_ADDR_V6 */
 146         "notification_parameters",      /* SC_NOTIFICATION_PARAMETERS */
 147         "opaque_list",                  /* SC_OPAQUE */
 148         "parameter",                    /* SC_PARAMETER */
 149         "paramval",                     /* SC_PARAMVAL */
 150         "pg_pattern",                   /* SC_PG_PATTERN */
 151         "prop_pattern",                 /* SC_PROP_PATTERN */
 152         "property",                     /* SC_PROPERTY */
 153         "property_group",               /* SC_PROPERTY_GROUP */
 154         "propval",                      /* SC_PROPVAL */
 155         "range",                        /* SC_RANGE */
 156         "restarter",                    /* SC_RESTARTER */
 157         "service",                      /* SC_SERVICE */
 158         "service_bundle",               /* SC_SERVICE_BUNDLE */
 159         "service_fmri",                 /* SC_SERVICE_FMRI */
 160         "single_instance",              /* SC_INSTANCE_SINGLE */
 161         "stability",                    /* SC_STABILITY */
 162         "template",                     /* SC_TEMPLATE */
 163         "time_list",                    /* SC_TIME */
 164         "type",                         /* SC_TYPE */
 165         "units",                        /* SC_UNITS */
 166         "uri_list",                     /* SC_URI */
 167         "ustring_list",                 /* SC_USTRING */
 168         "value",                        /* SC_VALUE */
 169         "value_node",                   /* SC_VALUE_NODE */
 170         "values",                       /* SC_VALUES */
 171         "visibility",                   /* SC_VISIBILITY */
 172         "xi:fallback",                  /* SC_XI_FALLBACK */
 173         "xi:include"                    /* SC_XI_INCLUDE */
 174 };
 175 
 176 /*
 177  * The following list must be kept in the same order as that of
 178  * element_t array
 179  */
 180 static const char *lxml_prop_types[] = {
 181         "astring",                      /* SC_ASTRING */
 182         "boolean",                      /* SC_BOOLEAN */
 183         "",                             /* SC_CARDINALITY */
 184         "",                             /* SC_CHOICES */
 185         "",                             /* SC_COMMON_NAME */
 186         "",                             /* SC_CONSTRAINTS */
 187         "count",                        /* SC_COUNT */
 188         "",                             /* SC_INSTANCE_CREATE_DEFAULT */
 189         "",                             /* SC_DEPENDENCY */
 190         "",                             /* SC_DEPENDENT */
 191         "",                             /* SC_DESCRIPTION */
 192         "",                             /* SC_DOC_LINK */
 193         "",                             /* SC_DOCUMENTATION */
 194         "",                             /* SC_ENABLED */
 195         "",                             /* SC_EVENT */
 196         "",                             /* SC_EXEC_METHOD */
 197         "fmri",                         /* SC_FMRI */
 198         "host",                         /* SC_HOST */
 199         "hostname",                     /* SC_HOSTNAME */
 200         "",                             /* SC_INCLUDE_VALUES */
 201         "",                             /* SC_INSTANCE */
 202         "integer",                      /* SC_INTEGER */
 203         "",                             /* SC_INTERNAL_SEPARATORS */
 204         "",                             /* SC_LOCTEXT */
 205         "",                             /* SC_MANPAGE */
 206         "",                             /* SC_METHOD_CONTEXT */
 207         "",                             /* SC_METHOD_CREDENTIAL */
 208         "",                             /* SC_METHOD_PROFILE */
 209         "",                             /* SC_METHOD_ENVIRONMENT */
 210         "",                             /* SC_METHOD_ENVVAR */
 211         "net_address",                  /* SC_NET_ADDR */
 212         "net_address_v4",               /* SC_NET_ADDR_V4 */
 213         "net_address_v6",               /* SC_NET_ADDR_V6 */
 214         "",                             /* SC_NOTIFICATION_PARAMETERS */
 215         "opaque",                       /* SC_OPAQUE */
 216         "",                             /* SC_PARAMETER */
 217         "",                             /* SC_PARAMVAL */
 218         "",                             /* SC_PG_PATTERN */
 219         "",                             /* SC_PROP_PATTERN */
 220         "",                             /* SC_PROPERTY */
 221         "",                             /* SC_PROPERTY_GROUP */
 222         "",                             /* SC_PROPVAL */
 223         "",                             /* SC_RANGE */
 224         "",                             /* SC_RESTARTER */
 225         "",                             /* SC_SERVICE */
 226         "",                             /* SC_SERVICE_BUNDLE */
 227         "",                             /* SC_SERVICE_FMRI */
 228         "",                             /* SC_INSTANCE_SINGLE */
 229         "",                             /* SC_STABILITY */
 230         "",                             /* SC_TEMPLATE */
 231         "time",                         /* SC_TIME */
 232         "",                             /* SC_TYPE */
 233         "",                             /* SC_UNITS */
 234         "uri",                          /* SC_URI */
 235         "ustring",                      /* SC_USTRING */
 236         "",                             /* SC_VALUE */
 237         "",                             /* SC_VALUE_NODE */
 238         "",                             /* SC_VALUES */
 239         "",                             /* SC_VISIBILITY */
 240         "",                             /* SC_XI_FALLBACK */
 241         ""                              /* SC_XI_INCLUDE */
 242 };
 243 
 244 int
 245 lxml_init()
 246 {
 247         if (getenv("SVCCFG_NOVALIDATE") == NULL) {
 248                 /*
 249                  * DTD validation, with line numbers.
 250                  */
 251                 (void) xmlLineNumbersDefault(1);
 252                 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
 253                 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
 254         }
 255 
 256         return (0);
 257 }
 258 
 259 static bundle_type_t
 260 lxml_xlate_bundle_type(xmlChar *type)
 261 {
 262         if (xmlStrcmp(type, (const xmlChar *)"manifest") == 0)
 263                 return (SVCCFG_MANIFEST);
 264 
 265         if (xmlStrcmp(type, (const xmlChar *)"profile") == 0)
 266                 return (SVCCFG_PROFILE);
 267 
 268         if (xmlStrcmp(type, (const xmlChar *)"archive") == 0)
 269                 return (SVCCFG_ARCHIVE);
 270 
 271         return (SVCCFG_UNKNOWN_BUNDLE);
 272 }
 273 
 274 static service_type_t
 275 lxml_xlate_service_type(xmlChar *type)
 276 {
 277         if (xmlStrcmp(type, (const xmlChar *)"service") == 0)
 278                 return (SVCCFG_SERVICE);
 279 
 280         if (xmlStrcmp(type, (const xmlChar *)"restarter") == 0)
 281                 return (SVCCFG_RESTARTER);
 282 
 283         if (xmlStrcmp(type, (const xmlChar *)"milestone") == 0)
 284                 return (SVCCFG_MILESTONE);
 285 
 286         return (SVCCFG_UNKNOWN_SERVICE);
 287 }
 288 
 289 static element_t
 290 lxml_xlate_element(const xmlChar *tag)
 291 {
 292         int i;
 293 
 294         for (i = 0; i < sizeof (lxml_elements) / sizeof (char *); i++)
 295                 if (xmlStrcmp(tag, (const xmlChar *)lxml_elements[i]) == 0)
 296                         return ((element_t)i);
 297 
 298         return ((element_t)-1);
 299 }
 300 
 301 static uint_t
 302 lxml_xlate_boolean(const xmlChar *value)
 303 {
 304         if (xmlStrcmp(value, (const xmlChar *)true) == 0)
 305                 return (1);
 306 
 307         if (xmlStrcmp(value, (const xmlChar *)false) == 0)
 308                 return (0);
 309 
 310         uu_die(gettext("illegal boolean value \"%s\"\n"), value);
 311 
 312         /*NOTREACHED*/
 313 }
 314 
 315 static scf_type_t
 316 lxml_element_to_type(element_t type)
 317 {
 318         switch (type) {
 319         case SC_ASTRING:        return (SCF_TYPE_ASTRING);
 320         case SC_BOOLEAN:        return (SCF_TYPE_BOOLEAN);
 321         case SC_COUNT:          return (SCF_TYPE_COUNT);
 322         case SC_FMRI:           return (SCF_TYPE_FMRI);
 323         case SC_HOST:           return (SCF_TYPE_HOST);
 324         case SC_HOSTNAME:       return (SCF_TYPE_HOSTNAME);
 325         case SC_INTEGER:        return (SCF_TYPE_INTEGER);
 326         case SC_NET_ADDR:       return (SCF_TYPE_NET_ADDR);
 327         case SC_NET_ADDR_V4:    return (SCF_TYPE_NET_ADDR_V4);
 328         case SC_NET_ADDR_V6:    return (SCF_TYPE_NET_ADDR_V6);
 329         case SC_OPAQUE:         return (SCF_TYPE_OPAQUE);
 330         case SC_TIME:           return (SCF_TYPE_TIME);
 331         case SC_URI:            return (SCF_TYPE_URI);
 332         case SC_USTRING:        return (SCF_TYPE_USTRING);
 333 
 334         default:
 335                 uu_die(gettext("unknown value type (%d)\n"), type);
 336         }
 337 
 338         /* NOTREACHED */
 339 }
 340 
 341 static element_t
 342 lxml_type_to_element(scf_type_t type)
 343 {
 344         switch (type) {
 345         case SCF_TYPE_ASTRING:          return (SC_ASTRING);
 346         case SCF_TYPE_BOOLEAN:          return (SC_BOOLEAN);
 347         case SCF_TYPE_COUNT:            return (SC_COUNT);
 348         case SCF_TYPE_FMRI:             return (SC_FMRI);
 349         case SCF_TYPE_HOST:             return (SC_HOST);
 350         case SCF_TYPE_HOSTNAME:         return (SC_HOSTNAME);
 351         case SCF_TYPE_INTEGER:          return (SC_INTEGER);
 352         case SCF_TYPE_NET_ADDR:         return (SC_NET_ADDR);
 353         case SCF_TYPE_NET_ADDR_V4:      return (SC_NET_ADDR_V4);
 354         case SCF_TYPE_NET_ADDR_V6:      return (SC_NET_ADDR_V6);
 355         case SCF_TYPE_OPAQUE:           return (SC_OPAQUE);
 356         case SCF_TYPE_TIME:             return (SC_TIME);
 357         case SCF_TYPE_URI:              return (SC_URI);
 358         case SCF_TYPE_USTRING:          return (SC_USTRING);
 359 
 360         default:
 361                 uu_die(gettext("unknown value type (%d)\n"), type);
 362         }
 363 
 364         /* NOTREACHED */
 365 }
 366 
 367 /*
 368  * Create a SCF_TYPE_BOOLEAN property name pname and attach it to the
 369  * property group at pgrp.  The value of the property will be set from the
 370  * attribute named attr.  attr must have a value of 0, 1, true or false.
 371  *
 372  * Zero is returned on success.  An error is indicated by -1.  It indicates
 373  * that either the attribute had an invalid value or that we could not
 374  * attach the property to pgrp.  The attribute should not have an invalid
 375  * value if the DTD is correctly written.
 376  */
 377 static int
 378 new_bool_prop_from_attr(pgroup_t *pgrp, const char *pname, xmlNodePtr n,
 379     const char *attr)
 380 {
 381         uint64_t bool;
 382         xmlChar *val;
 383         property_t *p;
 384         int r;
 385 
 386         val = xmlGetProp(n, (xmlChar *)attr);
 387         if (val == NULL)
 388                 return (0);
 389 
 390         if ((xmlStrcmp(val, (xmlChar *)"0") == 0) ||
 391             (xmlStrcmp(val, (xmlChar *)"false") == 0)) {
 392                 bool = 0;
 393         } else if ((xmlStrcmp(val, (xmlChar *)"1") == 0) ||
 394             (xmlStrcmp(val, (xmlChar *)"true") == 0)) {
 395                 bool = 1;
 396         } else {
 397                 xmlFree(val);
 398                 return (-1);
 399         }
 400         xmlFree(val);
 401         p = internal_property_create(pname, SCF_TYPE_BOOLEAN, 1, bool);
 402         r = internal_attach_property(pgrp, p);
 403 
 404         if (r != 0)
 405                 internal_property_free(p);
 406 
 407         return (r);
 408 }
 409 
 410 static int
 411 new_str_prop_from_attr(pgroup_t *pgrp, const char *pname, scf_type_t ty,
 412     xmlNodePtr n, const char *attr)
 413 {
 414         xmlChar *val;
 415         property_t *p;
 416         int r;
 417 
 418         val = xmlGetProp(n, (xmlChar *)attr);
 419 
 420         p = internal_property_create(pname, ty, 1, val);
 421         r = internal_attach_property(pgrp, p);
 422 
 423         if (r != 0)
 424                 internal_property_free(p);
 425 
 426         return (r);
 427 }
 428 
 429 static int
 430 new_opt_str_prop_from_attr(pgroup_t *pgrp, const char *pname, scf_type_t ty,
 431     xmlNodePtr n, const char *attr, const char *dflt)
 432 {
 433         xmlChar *val;
 434         property_t *p;
 435         int r;
 436 
 437         val = xmlGetProp(n, (xmlChar *)attr);
 438         if (val == NULL) {
 439                 if (dflt == NULL) {
 440                         /*
 441                          * A missing attribute is considered to be a
 442                          * success in this function, because many of the
 443                          * attributes are optional.  Missing non-optional
 444                          * attributes will be detected later when template
 445                          * validation is done.
 446                          */
 447                         return (0);
 448                 } else {
 449                         val = (xmlChar *)dflt;
 450                 }
 451         }
 452 
 453         p = internal_property_create(pname, ty, 1, val);
 454         r = internal_attach_property(pgrp, p);
 455 
 456         if (r != 0)
 457                 internal_property_free(p);
 458 
 459         return (r);
 460 }
 461 
 462 static int
 463 lxml_ignorable_block(xmlNodePtr n)
 464 {
 465         return ((xmlStrcmp(n->name, (xmlChar *)"text") == 0 ||
 466             xmlStrcmp(n->name, (xmlChar *)"comment") == 0) ? 1 : 0);
 467 }
 468 
 469 static void
 470 lxml_validate_element(xmlNodePtr n)
 471 {
 472         xmlValidCtxtPtr vcp;
 473 
 474         if (n->doc == NULL)
 475                 uu_die(gettext("Could not validate element\n"));
 476 
 477         if (n->doc->extSubset == NULL) {
 478                 xmlDtdPtr dtd;
 479                 dtd = xmlParseDTD(NULL, n->doc->intSubset->SystemID);
 480 
 481                 if (dtd == NULL) {
 482                         uu_die(gettext("Could not parse DTD \"%s\".\n"),
 483                             n->doc->intSubset->SystemID);
 484                 }
 485 
 486                 n->doc->extSubset = dtd;
 487         }
 488 
 489         vcp = xmlNewValidCtxt();
 490         if (vcp == NULL)
 491                 uu_die(gettext("could not allocate memory"));
 492 
 493         vcp->warning = xmlParserValidityWarning;
 494         vcp->error = xmlParserValidityError;
 495 
 496         if (xmlValidateElement(vcp, n->doc, n) == 0)
 497                 uu_die(gettext("Document is not valid.\n"));
 498 
 499         xmlFreeValidCtxt(vcp);
 500 }
 501 
 502 static int
 503 lxml_validate_string_value(scf_type_t type, const char *v)
 504 {
 505         static scf_value_t *scf_value = NULL;
 506         static scf_handle_t *scf_hndl = NULL;
 507 
 508         if (scf_hndl == NULL && (scf_hndl = scf_handle_create(SCF_VERSION)) ==
 509             NULL)
 510                 return (-1);
 511 
 512         if (scf_value == NULL && (scf_value = scf_value_create(scf_hndl)) ==
 513             NULL)
 514                 return (-1);
 515 
 516         return (scf_value_set_from_string(scf_value, type, v));
 517 }
 518 
 519 static void
 520 lxml_free_str(value_t *val)
 521 {
 522         free(val->sc_u.sc_string);
 523 }
 524 
 525 /*
 526  * Take a value_t structure and a type and value.  Based on the type
 527  * ensure that the value is of that type.  If so store the value in
 528  * the correct location of the value_t structure.
 529  *
 530  * If the value is NULL, the value_t structure will have been created
 531  * and the value would have ultimately been stored as a string value
 532  * but at the time the type was unknown.  Now the type should be known
 533  * so take the type and value from value_t and validate and store
 534  * the value correctly if the value is of the stated type.
 535  */
 536 void
 537 lxml_store_value(value_t *v, element_t type, const xmlChar *value)
 538 {
 539         char *endptr;
 540         int fov = 0;
 541         scf_type_t scf_type = SCF_TYPE_INVALID;
 542 
 543         if (value == NULL) {
 544                 type = lxml_type_to_element(v->sc_type);
 545                 value = (const xmlChar *)v->sc_u.sc_string;
 546                 fov = 1;
 547         }
 548 
 549         switch (type) {
 550         case SC_COUNT:
 551                 /*
 552                  * Although an SC_COUNT represents a uint64_t the use
 553                  * of a negative value is acceptable due to the usage
 554                  * established by inetd(1M).
 555                  */
 556                 errno = 0;
 557                 v->sc_u.sc_count = strtoull((char *)value, &endptr, 10);
 558                 if (errno != 0 || endptr == (char *)value || *endptr)
 559                         uu_die(gettext("illegal value \"%s\" for "
 560                             "%s (%s)\n"), (char *)value,
 561                             lxml_prop_types[type],
 562                             (errno) ? strerror(errno) :
 563                             gettext("Illegal character"));
 564                 break;
 565         case SC_INTEGER:
 566                 errno = 0;
 567                 v->sc_u.sc_integer = strtoll((char *)value, &endptr, 10);
 568                 if (errno != 0 || *endptr)
 569                         uu_die(gettext("illegal value \"%s\" for "
 570                             "%s (%s)\n"), (char *)value,
 571                             lxml_prop_types[type],
 572                             (errno) ? strerror(errno) : "Illegal character");
 573                 break;
 574         case SC_OPAQUE:
 575         case SC_HOST:
 576         case SC_HOSTNAME:
 577         case SC_NET_ADDR:
 578         case SC_NET_ADDR_V4:
 579         case SC_NET_ADDR_V6:
 580         case SC_FMRI:
 581         case SC_URI:
 582         case SC_TIME:
 583         case SC_ASTRING:
 584         case SC_USTRING:
 585                 scf_type = lxml_element_to_type(type);
 586 
 587                 v->sc_u.sc_string = safe_strdup((const char *)value);
 588                 if (lxml_validate_string_value(scf_type,
 589                     v->sc_u.sc_string) != 0)
 590                         uu_die(gettext("illegal value \"%s\" for "
 591                             "%s (%s)\n"), (char *)value,
 592                             lxml_prop_types[type],
 593                             (scf_error()) ? scf_strerror(scf_error()) :
 594                             gettext("Illegal format"));
 595                 v->sc_free = lxml_free_str;
 596                 break;
 597         case SC_BOOLEAN:
 598                 v->sc_u.sc_count = lxml_xlate_boolean(value);
 599                 break;
 600         default:
 601                 uu_die(gettext("unknown value type (%d)\n"), type);
 602                 break;
 603         }
 604 
 605         /* Free the old value */
 606         if (fov && v->sc_free != NULL)
 607                 free((char *)value);
 608 }
 609 
 610 static value_t *
 611 lxml_make_value(element_t type, const xmlChar *value)
 612 {
 613         value_t *v;
 614 
 615         v = internal_value_new();
 616 
 617         v->sc_type = lxml_element_to_type(type);
 618 
 619         lxml_store_value(v, type, value);
 620 
 621         return (v);
 622 }
 623 
 624 static int
 625 lxml_get_value(property_t *prop, element_t vtype, xmlNodePtr value)
 626 {
 627         xmlNodePtr cursor;
 628 
 629         for (cursor = value->xmlChildrenNode; cursor != NULL;
 630             cursor = cursor->next) {
 631                 xmlChar *assigned_value;
 632                 value_t *v;
 633 
 634                 if (lxml_ignorable_block(cursor))
 635                         continue;
 636 
 637                 switch (lxml_xlate_element(cursor->name)) {
 638                 case SC_VALUE_NODE:
 639                         if ((assigned_value = xmlGetProp(cursor,
 640                             (xmlChar *)value_attr)) == NULL)
 641                                 uu_die(gettext("no value on value node?\n"));
 642                         break;
 643                 default:
 644                         uu_die(gettext("value list contains illegal element "
 645                             "\'%s\'\n"), cursor->name);
 646                         break;
 647                 }
 648 
 649                 v = lxml_make_value(vtype, assigned_value);
 650 
 651                 xmlFree(assigned_value);
 652 
 653                 internal_attach_value(prop, v);
 654         }
 655 
 656         return (0);
 657 }
 658 
 659 static int
 660 lxml_get_propval(pgroup_t *pgrp, xmlNodePtr propval)
 661 {
 662         property_t *p;
 663         element_t r;
 664         value_t *v;
 665         xmlChar *type, *val, *override;
 666         int op = pgrp->sc_parent->sc_op;
 667 
 668         p = internal_property_new();
 669 
 670         p->sc_property_name = (char *)xmlGetProp(propval, (xmlChar *)name_attr);
 671         if ((p->sc_property_name == NULL) || (*p->sc_property_name == 0))
 672                 uu_die(gettext("property name missing in group '%s'\n"),
 673                     pgrp->sc_pgroup_name);
 674 
 675         type = xmlGetProp(propval, (xmlChar *)type_attr);
 676         if ((type != NULL) && (*type != 0)) {
 677                 for (r = 0;
 678                     r < sizeof (lxml_prop_types) / sizeof (char *); ++r) {
 679                         if (xmlStrcmp(type,
 680                             (const xmlChar *)lxml_prop_types[r]) == 0)
 681                                 break;
 682                 }
 683 
 684                 if (r >= sizeof (lxml_prop_types) / sizeof (char *))
 685                         uu_die(gettext("property type invalid for "
 686                             "property '%s/%s'\n"), pgrp->sc_pgroup_name,
 687                             p->sc_property_name);
 688 
 689                 p->sc_value_type = lxml_element_to_type(r);
 690         } else if (op == SVCCFG_OP_APPLY) {
 691                 /*
 692                  * Store the property type as invalid, and the value
 693                  * as an ASTRING and let the bundle apply code validate
 694                  * the type/value once the type is found.
 695                  */
 696                 est->sc_miss_type = B_TRUE;
 697                 p->sc_value_type = SCF_TYPE_INVALID;
 698                 r = SC_ASTRING;
 699         } else {
 700                 uu_die(gettext("property type missing for property '%s/%s'\n"),
 701                     pgrp->sc_pgroup_name, p->sc_property_name);
 702         }
 703 
 704         val = xmlGetProp(propval, (xmlChar *)value_attr);
 705         if (val == NULL)
 706                 uu_die(gettext("property value missing for property '%s/%s'\n"),
 707                     pgrp->sc_pgroup_name, p->sc_property_name);
 708 
 709         v = lxml_make_value(r, val);
 710         xmlFree(val);
 711         internal_attach_value(p, v);
 712 
 713         xmlFree(type);
 714 
 715         override = xmlGetProp(propval, (xmlChar *)override_attr);
 716         p->sc_property_override = (xmlStrcmp(override, (xmlChar *)true) == 0);
 717         xmlFree(override);
 718 
 719         return (internal_attach_property(pgrp, p));
 720 }
 721 
 722 static int
 723 lxml_get_property(pgroup_t *pgrp, xmlNodePtr property)
 724 {
 725         property_t *p;
 726         xmlNodePtr cursor;
 727         element_t r;
 728         xmlChar *type, *override;
 729         int op = pgrp->sc_parent->sc_op;
 730 
 731         p = internal_property_new();
 732 
 733         if (((p->sc_property_name = (char *)xmlGetProp(property,
 734             (xmlChar *)name_attr)) == NULL) || (*p->sc_property_name == 0))
 735                 uu_die(gettext("property name missing in group \'%s\'\n"),
 736                     pgrp->sc_pgroup_name);
 737 
 738         type = xmlGetProp(property, (xmlChar *)type_attr);
 739         if ((type != NULL) && (*type != 0)) {
 740                 for (r = 0;
 741                     r < sizeof (lxml_prop_types) / sizeof (char *); r++) {
 742                         if (xmlStrcmp(type,
 743                             (const xmlChar *)lxml_prop_types[r]) == 0)
 744                                 break;
 745                 }
 746 
 747                 if (r >= sizeof (lxml_prop_types) / sizeof (char *))
 748                         uu_die(gettext("property type invalid for "
 749                             "property '%s/%s'\n"), pgrp->sc_pgroup_name,
 750                             p->sc_property_name);
 751 
 752                 p->sc_value_type = lxml_element_to_type(r);
 753         } else if (op == SVCCFG_OP_APPLY) {
 754                 /*
 755                  * Store the property type as invalid, and let the bundle apply
 756                  * code validate the type/value once the type is found.
 757                  */
 758                 p->sc_value_type = SCF_TYPE_INVALID;
 759                 est->sc_miss_type = B_TRUE;
 760         } else {
 761                 uu_die(gettext("property type missing for "
 762                     "property \'%s/%s\'\n"), pgrp->sc_pgroup_name,
 763                     p->sc_property_name);
 764         }
 765 
 766         for (cursor = property->xmlChildrenNode; cursor != NULL;
 767             cursor = cursor->next) {
 768                 if (lxml_ignorable_block(cursor))
 769                         continue;
 770 
 771                 switch (r = lxml_xlate_element(cursor->name)) {
 772                 case SC_ASTRING:
 773                 case SC_BOOLEAN:
 774                 case SC_COUNT:
 775                 case SC_FMRI:
 776                 case SC_HOST:
 777                 case SC_HOSTNAME:
 778                 case SC_INTEGER:
 779                 case SC_NET_ADDR:
 780                 case SC_NET_ADDR_V4:
 781                 case SC_NET_ADDR_V6:
 782                 case SC_OPAQUE:
 783                 case SC_TIME:
 784                 case SC_URI:
 785                 case SC_USTRING:
 786                         /*
 787                          * If the type is invalid then this is an apply
 788                          * operation and the type can be taken from the
 789                          * value list.
 790                          */
 791                         if (p->sc_value_type == SCF_TYPE_INVALID) {
 792                                 p->sc_value_type = lxml_element_to_type(r);
 793                                 type = xmlStrdup((const
 794                                     xmlChar *)lxml_prop_types[r]);
 795 
 796                         } else if (strcmp(lxml_prop_types[r],
 797                             (const char *)type) != 0) {
 798                                 uu_die(gettext("property \'%s\' "
 799                                     "type-to-list mismatch\n"),
 800                                     p->sc_property_name);
 801                         }
 802 
 803                         (void) lxml_get_value(p, r, cursor);
 804                         break;
 805                 default:
 806                         uu_die(gettext("unknown value list type: %s\n"),
 807                             cursor->name);
 808                         break;
 809                 }
 810         }
 811 
 812         xmlFree(type);
 813 
 814         override = xmlGetProp(property, (xmlChar *)override_attr);
 815         p->sc_property_override = (xmlStrcmp(override, (xmlChar *)true) == 0);
 816         xmlFree(override);
 817 
 818         return (internal_attach_property(pgrp, p));
 819 }
 820 
 821 static int
 822 lxml_get_pgroup_stability(pgroup_t *pgrp, xmlNodePtr stab)
 823 {
 824         if (pgrp->sc_parent->sc_op == SVCCFG_OP_APPLY)
 825                 lxml_validate_element(stab);
 826 
 827         return (new_str_prop_from_attr(pgrp, SCF_PROPERTY_STABILITY,
 828             SCF_TYPE_ASTRING, stab, value_attr));
 829 }
 830 
 831 /*
 832  * Property groups can go on any of a service, an instance, or a template.
 833  */
 834 static int
 835 lxml_get_pgroup(entity_t *entity, xmlNodePtr pgroup)
 836 {
 837         pgroup_t *pg;
 838         xmlNodePtr cursor;
 839         xmlChar *name, *type, *delete;
 840 
 841         /*
 842          * property group attributes:
 843          * name: string
 844          * type: string | framework | application
 845          */
 846         name = xmlGetProp(pgroup, (xmlChar *)name_attr);
 847         type = xmlGetProp(pgroup, (xmlChar *)type_attr);
 848         pg = internal_pgroup_find_or_create(entity, (char *)name, (char *)type);
 849         xmlFree(name);
 850         xmlFree(type);
 851 
 852         /*
 853          * Walk the children of this lxml_elements, which are a stability
 854          * element, property elements, or propval elements.
 855          */
 856         for (cursor = pgroup->xmlChildrenNode; cursor != NULL;
 857             cursor = cursor->next) {
 858                 if (lxml_ignorable_block(cursor))
 859                         continue;
 860 
 861                 switch (lxml_xlate_element(cursor->name)) {
 862                 case SC_STABILITY:
 863                         (void) lxml_get_pgroup_stability(pg, cursor);
 864                         break;
 865                 case SC_PROPERTY:
 866                         (void) lxml_get_property(pg, cursor);
 867                         break;
 868                 case SC_PROPVAL:
 869                         (void) lxml_get_propval(pg, cursor);
 870                         break;
 871                 default:
 872                         abort();
 873                         break;
 874                 }
 875         }
 876 
 877         delete = xmlGetProp(pgroup, (xmlChar *)delete_attr);
 878         pg->sc_pgroup_delete = (xmlStrcmp(delete, (xmlChar *)true) == 0);
 879         xmlFree(delete);
 880 
 881         return (0);
 882 }
 883 
 884 
 885 /*
 886  * Dependency groups, execution methods can go on either a service or an
 887  * instance.
 888  */
 889 
 890 static int
 891 lxml_get_method_profile(pgroup_t *pg, xmlNodePtr profile)
 892 {
 893         property_t *p;
 894 
 895         p = internal_property_create(SCF_PROPERTY_USE_PROFILE, SCF_TYPE_BOOLEAN,
 896             1, (uint64_t)1);
 897         if (internal_attach_property(pg, p) != 0)
 898                 return (-1);
 899 
 900         return (new_str_prop_from_attr(pg, SCF_PROPERTY_PROFILE,
 901             SCF_TYPE_ASTRING, profile, name_attr));
 902 }
 903 
 904 static int
 905 lxml_get_method_credential(pgroup_t *pg, xmlNodePtr cred)
 906 {
 907         property_t *p;
 908 
 909         p = internal_property_create(SCF_PROPERTY_USE_PROFILE, SCF_TYPE_BOOLEAN,
 910             1, (uint64_t)0);
 911         if (internal_attach_property(pg, p) != 0)
 912                 return (-1);
 913 
 914         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_USER, SCF_TYPE_ASTRING,
 915             cred, "user", NULL) != 0)
 916                 return (-1);
 917 
 918         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_GROUP, SCF_TYPE_ASTRING,
 919             cred, "group", NULL) != 0)
 920                 return (-1);
 921 
 922         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_SUPP_GROUPS,
 923             SCF_TYPE_ASTRING, cred, "supp_groups", NULL) != 0)
 924                 return (-1);
 925 
 926         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_PRIVILEGES,
 927             SCF_TYPE_ASTRING, cred, "privileges", NULL) != 0)
 928                 return (-1);
 929 
 930         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_LIMIT_PRIVILEGES,
 931             SCF_TYPE_ASTRING, cred, "limit_privileges", NULL) != 0)
 932                 return (-1);
 933 
 934         return (0);
 935 }
 936 
 937 static char *
 938 lxml_get_envvar(xmlNodePtr envvar)
 939 {
 940         char *name;
 941         char *value;
 942         char *ret;
 943 
 944         name = (char *)xmlGetProp(envvar, (xmlChar *)name_attr);
 945         value = (char *)xmlGetProp(envvar, (xmlChar *)value_attr);
 946 
 947         if (strlen(name) == 0 || strchr(name, '=') != NULL)
 948                 uu_die(gettext("Invalid environment variable "
 949                     "\"%s\".\n"), name);
 950         if (strstr(name, "SMF_") == name)
 951                 uu_die(gettext("Invalid environment variable "
 952                     "\"%s\"; \"SMF_\" prefix is reserved.\n"), name);
 953 
 954         ret = uu_msprintf("%s=%s", name, value);
 955         xmlFree(name);
 956         xmlFree(value);
 957         return (ret);
 958 }
 959 
 960 static int
 961 lxml_get_method_environment(pgroup_t *pg, xmlNodePtr environment)
 962 {
 963         property_t *p;
 964         xmlNodePtr cursor;
 965         value_t *val;
 966 
 967         p = internal_property_create(SCF_PROPERTY_ENVIRONMENT,
 968             SCF_TYPE_ASTRING, 0);
 969 
 970         for (cursor = environment->xmlChildrenNode; cursor != NULL;
 971             cursor = cursor->next) {
 972                 char *tmp;
 973 
 974                 if (lxml_ignorable_block(cursor))
 975                         continue;
 976 
 977                 if (lxml_xlate_element(cursor->name) != SC_METHOD_ENVVAR)
 978                         uu_die(gettext("illegal element \"%s\" on "
 979                             "method environment for \"%s\"\n"),
 980                             cursor->name, pg->sc_pgroup_name);
 981 
 982                 if ((tmp = lxml_get_envvar(cursor)) == NULL)
 983                         uu_die(gettext("Out of memory\n"));
 984 
 985                 val = internal_value_new();
 986                 val->sc_u.sc_string = tmp;
 987                 val->sc_type = SCF_TYPE_ASTRING;
 988                 val->sc_free = lxml_free_str;
 989                 internal_attach_value(p, val);
 990         }
 991 
 992         if (internal_attach_property(pg, p) != 0) {
 993                 internal_property_free(p);
 994                 return (-1);
 995         }
 996 
 997         return (0);
 998 }
 999 
1000 static int
1001 lxml_get_method_context(pgroup_t *pg, xmlNodePtr ctx)
1002 {
1003         xmlNodePtr cursor;
1004 
1005         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_WORKING_DIRECTORY,
1006             SCF_TYPE_ASTRING, ctx, "working_directory", NULL) != 0)
1007                 return (-1);
1008 
1009         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_PROJECT,
1010             SCF_TYPE_ASTRING, ctx, "project", NULL) != 0)
1011                 return (-1);
1012 
1013         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_RESOURCE_POOL,
1014             SCF_TYPE_ASTRING, ctx, "resource_pool", NULL) != 0)
1015                 return (-1);
1016 
1017         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_SECFLAGS,
1018             SCF_TYPE_ASTRING, ctx, "security_flags", NULL) != 0)
1019                 return (-1);
1020 
1021         for (cursor = ctx->xmlChildrenNode; cursor != NULL;
1022             cursor = cursor->next) {
1023                 if (lxml_ignorable_block(cursor))
1024                         continue;
1025 
1026                 switch (lxml_xlate_element(cursor->name)) {
1027                 case SC_METHOD_CREDENTIAL:
1028                         (void) lxml_get_method_credential(pg, cursor);
1029                         break;
1030                 case SC_METHOD_PROFILE:
1031                         (void) lxml_get_method_profile(pg, cursor);
1032                         break;
1033                 case SC_METHOD_ENVIRONMENT:
1034                         (void) lxml_get_method_environment(pg, cursor);
1035                         break;
1036                 default:
1037                         semerr(gettext("illegal element \'%s\' in method "
1038                             "context\n"), (char *)cursor);
1039                         break;
1040                 }
1041         }
1042 
1043         return (0);
1044 }
1045 
1046 static int
1047 lxml_get_entity_method_context(entity_t *entity, xmlNodePtr ctx)
1048 {
1049         pgroup_t *pg;
1050 
1051         pg = internal_pgroup_find_or_create(entity, SCF_PG_METHOD_CONTEXT,
1052             (char *)scf_group_framework);
1053 
1054         return (lxml_get_method_context(pg, ctx));
1055 }
1056 
1057 static int
1058 lxml_get_exec_method(entity_t *entity, xmlNodePtr emeth)
1059 {
1060         pgroup_t *pg;
1061         property_t *p;
1062         xmlChar *name, *timeout, *delete;
1063         xmlNodePtr cursor;
1064         int r = 0;
1065 
1066         if (entity->sc_op == SVCCFG_OP_APPLY)
1067                 lxml_validate_element(emeth);
1068 
1069         name = xmlGetProp(emeth, (xmlChar *)name_attr);
1070         pg = internal_pgroup_find_or_create(entity, (char *)name,
1071             (char *)SCF_GROUP_METHOD);
1072         xmlFree(name);
1073 
1074         if (new_str_prop_from_attr(pg, SCF_PROPERTY_TYPE, SCF_TYPE_ASTRING,
1075             emeth, type_attr) != 0 ||
1076             new_str_prop_from_attr(pg, SCF_PROPERTY_EXEC, SCF_TYPE_ASTRING,
1077             emeth, "exec") != 0)
1078                 return (-1);
1079 
1080         timeout = xmlGetProp(emeth, (xmlChar *)timeout_seconds_attr);
1081         if (timeout != NULL) {
1082                 uint64_t u_timeout;
1083                 char *endptr;
1084                 /*
1085                  * Although an SC_COUNT represents a uint64_t the use
1086                  * of a negative value is acceptable due to the usage
1087                  * established by inetd(1M).
1088                  */
1089                 errno = 0;
1090                 u_timeout = strtoull((char *)timeout, &endptr, 10);
1091                 if (errno != 0 || endptr == (char *)timeout || *endptr)
1092                         uu_die(gettext("illegal value \"%s\" for "
1093                             "timeout_seconds (%s)\n"),
1094                             (char *)timeout, (errno) ? strerror(errno):
1095                             gettext("Illegal character"));
1096                 p = internal_property_create(SCF_PROPERTY_TIMEOUT,
1097                     SCF_TYPE_COUNT, 1, u_timeout);
1098                 r = internal_attach_property(pg, p);
1099                 xmlFree(timeout);
1100         }
1101         if (r != 0)
1102                 return (-1);
1103 
1104         /*
1105          * There is a possibility that a method context also exists, in which
1106          * case the following attributes are defined: project, resource_pool,
1107          * working_directory, profile, user, group, privileges,
1108          * limit_privileges, security_flags
1109          */
1110         for (cursor = emeth->xmlChildrenNode; cursor != NULL;
1111             cursor = cursor->next) {
1112                 if (lxml_ignorable_block(cursor))
1113                         continue;
1114 
1115                 switch (lxml_xlate_element(cursor->name)) {
1116                 case SC_STABILITY:
1117                         if (lxml_get_pgroup_stability(pg, cursor) != 0)
1118                                 return (-1);
1119                         break;
1120 
1121                 case SC_METHOD_CONTEXT:
1122                         (void) lxml_get_method_context(pg, cursor);
1123                         break;
1124 
1125                 case SC_PROPVAL:
1126                         (void) lxml_get_propval(pg, cursor);
1127                         break;
1128 
1129                 case SC_PROPERTY:
1130                         (void) lxml_get_property(pg, cursor);
1131                         break;
1132 
1133                 default:
1134                         uu_die(gettext("illegal element \"%s\" on "
1135                             "execution method \"%s\"\n"), cursor->name,
1136                             pg->sc_pgroup_name);
1137                         break;
1138                 }
1139         }
1140 
1141         delete = xmlGetProp(emeth, (xmlChar *)delete_attr);
1142         pg->sc_pgroup_delete = (xmlStrcmp(delete, (xmlChar *)true) == 0);
1143         xmlFree(delete);
1144 
1145         return (0);
1146 }
1147 
1148 static int
1149 lxml_get_dependency(entity_t *entity, xmlNodePtr dependency)
1150 {
1151         pgroup_t *pg;
1152         property_t *p;
1153         xmlNodePtr cursor;
1154         xmlChar *name;
1155         xmlChar *delete;
1156 
1157         /*
1158          * dependency attributes:
1159          * name: string
1160          * grouping: require_all | require_any | exclude_all | optional_all
1161          * reset_on: string (error | restart | refresh | none)
1162          * type:  service / path /host
1163          */
1164 
1165         if (entity->sc_op == SVCCFG_OP_APPLY)
1166                 lxml_validate_element(dependency);
1167 
1168         name = xmlGetProp(dependency, (xmlChar *)name_attr);
1169         pg = internal_pgroup_find_or_create(entity, (char *)name,
1170             (char *)SCF_GROUP_DEPENDENCY);
1171         xmlFree(name);
1172 
1173         if (new_str_prop_from_attr(pg, SCF_PROPERTY_TYPE, SCF_TYPE_ASTRING,
1174             dependency, type_attr) != 0)
1175                 return (-1);
1176 
1177         if (new_str_prop_from_attr(pg, SCF_PROPERTY_RESTART_ON,
1178             SCF_TYPE_ASTRING, dependency, "restart_on") != 0)
1179                 return (-1);
1180 
1181         if (new_str_prop_from_attr(pg, SCF_PROPERTY_GROUPING, SCF_TYPE_ASTRING,
1182             dependency, "grouping") != 0)
1183                 return (-1);
1184 
1185         p = internal_property_create(SCF_PROPERTY_ENTITIES, SCF_TYPE_FMRI, 0);
1186         if (internal_attach_property(pg, p) != 0)
1187                 return (-1);
1188 
1189         for (cursor = dependency->xmlChildrenNode; cursor != NULL;
1190             cursor = cursor->next) {
1191                 xmlChar *value;
1192                 value_t *v;
1193 
1194                 if (lxml_ignorable_block(cursor))
1195                         continue;
1196 
1197                 switch (lxml_xlate_element(cursor->name)) {
1198                 case SC_STABILITY:
1199                         if (lxml_get_pgroup_stability(pg, cursor) != 0)
1200                                 return (-1);
1201                         break;
1202 
1203                 case SC_SERVICE_FMRI:
1204                         value = xmlGetProp(cursor, (xmlChar *)value_attr);
1205                         if (value != NULL) {
1206                                 if (lxml_validate_string_value(SCF_TYPE_FMRI,
1207                                     (char *)value) != 0)
1208                                         uu_die(gettext("illegal value \"%s\" "
1209                                             "for %s (%s)\n"), (char *)value,
1210                                             lxml_prop_types[SC_FMRI],
1211                                             (scf_error()) ?
1212                                             scf_strerror(scf_error()) :
1213                                             gettext("Illegal format"));
1214                                 v = internal_value_new();
1215                                 v->sc_type = SCF_TYPE_FMRI;
1216                                 v->sc_u.sc_string = (char *)value;
1217                                 internal_attach_value(p, v);
1218                         }
1219 
1220                         break;
1221 
1222                 case SC_PROPVAL:
1223                         (void) lxml_get_propval(pg, cursor);
1224                         break;
1225 
1226                 case SC_PROPERTY:
1227                         (void) lxml_get_property(pg, cursor);
1228                         break;
1229 
1230                 default:
1231                         uu_die(gettext("illegal element \"%s\" on "
1232                             "dependency group \"%s\"\n"), cursor->name, name);
1233                         break;
1234                 }
1235         }
1236 
1237         delete = xmlGetProp(dependency, (xmlChar *)delete_attr);
1238         pg->sc_pgroup_delete = (xmlStrcmp(delete, (xmlChar *)true) == 0);
1239         xmlFree(delete);
1240 
1241         return (0);
1242 }
1243 
1244 /*
1245  * Dependents are hairy.  They should cause a dependency pg to be created in
1246  * another service, but we can't do that here; we'll have to wait until the
1247  * import routines.  So for now we'll add the dependency group that should go
1248  * in the other service to the entity's dependent list.
1249  */
1250 static int
1251 lxml_get_dependent(entity_t *entity, xmlNodePtr dependent)
1252 {
1253         xmlChar *name, *or;
1254         xmlNodePtr sf;
1255         xmlChar *fmri, *delete;
1256         pgroup_t *pg;
1257         property_t *p;
1258         xmlNodePtr n;
1259         char *myfmri;
1260 
1261         if (entity->sc_op == SVCCFG_OP_APPLY)
1262                 lxml_validate_element(dependent);
1263 
1264         name = xmlGetProp(dependent, (xmlChar *)name_attr);
1265 
1266         if (internal_pgroup_find(entity, (char *)name, NULL) != NULL) {
1267                 semerr(gettext("Property group and dependent of entity %s "
1268                     "have same name \"%s\".\n"), entity->sc_name, name);
1269                 xmlFree(name);
1270                 return (-1);
1271         }
1272 
1273         or = xmlGetProp(dependent, (xmlChar *)override_attr);
1274 
1275         pg = internal_pgroup_new();
1276         pg->sc_pgroup_name = (char *)name;
1277         pg->sc_pgroup_type = (char *)SCF_GROUP_DEPENDENCY;
1278         pg->sc_pgroup_override = (xmlStrcmp(or, (xmlChar *)true) == 0);
1279         xmlFree(or);
1280         if (internal_attach_dependent(entity, pg) != 0) {
1281                 xmlFree(name);
1282                 internal_pgroup_free(pg);
1283                 return (-1);
1284         }
1285 
1286         for (sf = dependent->children; sf != NULL; sf = sf->next)
1287                 if (xmlStrcmp(sf->name, (xmlChar *)"service_fmri") == 0)
1288                         break;
1289         assert(sf != NULL);
1290         fmri = xmlGetProp(sf, (xmlChar *)value_attr);
1291         pg->sc_pgroup_fmri = (char *)fmri;
1292 
1293         if (new_str_prop_from_attr(pg, SCF_PROPERTY_RESTART_ON,
1294             SCF_TYPE_ASTRING, dependent, "restart_on") != 0)
1295                 return (-1);
1296 
1297         if (new_str_prop_from_attr(pg, SCF_PROPERTY_GROUPING, SCF_TYPE_ASTRING,
1298             dependent, "grouping") != 0)
1299                 return (-1);
1300 
1301         myfmri = safe_malloc(max_scf_fmri_len + 1);
1302         if (entity->sc_etype == SVCCFG_SERVICE_OBJECT) {
1303                 if (snprintf(myfmri, max_scf_fmri_len + 1, "svc:/%s",
1304                     entity->sc_name) < 0)
1305                         bad_error("snprintf", errno);
1306         } else {
1307                 assert(entity->sc_etype == SVCCFG_INSTANCE_OBJECT);
1308                 if (snprintf(myfmri, max_scf_fmri_len + 1, "svc:/%s:%s",
1309                     entity->sc_parent->sc_name, entity->sc_name) < 0)
1310                         bad_error("snprintf", errno);
1311         }
1312 
1313         p = internal_property_create(SCF_PROPERTY_ENTITIES, SCF_TYPE_FMRI, 1,
1314             myfmri);
1315         if (internal_attach_property(pg, p) != 0)
1316                 return (-1);
1317 
1318         /* Create a property to serve as a do-not-export flag. */
1319         p = internal_property_create("external", SCF_TYPE_BOOLEAN, 1,
1320             (uint64_t)1);
1321         if (internal_attach_property(pg, p) != 0)
1322                 return (-1);
1323 
1324         for (n = sf->next; n != NULL; n = n->next) {
1325                 if (lxml_ignorable_block(n))
1326                         continue;
1327 
1328                 switch (lxml_xlate_element(n->name)) {
1329                 case SC_STABILITY:
1330                         if (new_str_prop_from_attr(pg,
1331                             SCF_PROPERTY_ENTITY_STABILITY, SCF_TYPE_ASTRING, n,
1332                             value_attr) != 0)
1333                                 return (-1);
1334                         break;
1335 
1336                 case SC_PROPVAL:
1337                         (void) lxml_get_propval(pg, n);
1338                         break;
1339 
1340                 case SC_PROPERTY:
1341                         (void) lxml_get_property(pg, n);
1342                         break;
1343 
1344                 default:
1345                         uu_die(gettext("unexpected element %s.\n"), n->name);
1346                 }
1347         }
1348 
1349         /* Go back and fill in defaults. */
1350         if (internal_property_find(pg, SCF_PROPERTY_TYPE) == NULL) {
1351                 p = internal_property_create(SCF_PROPERTY_TYPE,
1352                     SCF_TYPE_ASTRING, 1, "service");
1353                 if (internal_attach_property(pg, p) != 0)
1354                         return (-1);
1355         }
1356 
1357         delete = xmlGetProp(dependent, (xmlChar *)delete_attr);
1358         pg->sc_pgroup_delete = (xmlStrcmp(delete, (xmlChar *)true) == 0);
1359         xmlFree(delete);
1360 
1361         pg = internal_pgroup_find_or_create(entity, "dependents",
1362             (char *)scf_group_framework);
1363         p = internal_property_create((char *)name, SCF_TYPE_FMRI, 1, fmri);
1364         if (internal_attach_property(pg, p) != 0)
1365                 return (-1);
1366 
1367         return (0);
1368 }
1369 
1370 static int
1371 lxml_get_entity_stability(entity_t *entity, xmlNodePtr rstr)
1372 {
1373         pgroup_t *pg;
1374         property_t *p;
1375         xmlChar *stabval;
1376 
1377         if (((stabval = xmlGetProp(rstr, (xmlChar *)value_attr)) == NULL) ||
1378             (*stabval == 0)) {
1379                 uu_warn(gettext("no stability value found\n"));
1380                 stabval = (xmlChar *)safe_strdup("External");
1381         }
1382 
1383         pg = internal_pgroup_find_or_create(entity, (char *)scf_pg_general,
1384             (char *)scf_group_framework);
1385 
1386         p = internal_property_create(SCF_PROPERTY_ENTITY_STABILITY,
1387             SCF_TYPE_ASTRING, 1, stabval);
1388 
1389         return (internal_attach_property(pg, p));
1390 }
1391 
1392 static int
1393 lxml_get_restarter(entity_t *entity, xmlNodePtr rstr)
1394 {
1395         pgroup_t *pg;
1396         property_t *p;
1397         xmlChar *restarter;
1398         xmlNode *cursor;
1399         int r;
1400 
1401         /*
1402          * Go find child.  Child is a service_fmri element.  value attribute
1403          * contains restarter FMRI.
1404          */
1405 
1406         pg = internal_pgroup_find_or_create(entity, (char *)scf_pg_general,
1407             (char *)scf_group_framework);
1408 
1409         /*
1410          * Walk its child elements, as appropriate.
1411          */
1412         for (cursor = rstr->xmlChildrenNode; cursor != NULL;
1413             cursor = cursor->next) {
1414                 if (lxml_ignorable_block(cursor))
1415                         continue;
1416 
1417                 switch (lxml_xlate_element(cursor->name)) {
1418                 case SC_SERVICE_FMRI:
1419                         restarter = xmlGetProp(cursor, (xmlChar *)value_attr);
1420                         break;
1421                 default:
1422                         uu_die(gettext("illegal element \"%s\" on restarter "
1423                             "element for \"%s\"\n"), cursor->name,
1424                             entity->sc_name);
1425                         break;
1426                 }
1427         }
1428 
1429         p = internal_property_create(SCF_PROPERTY_RESTARTER, SCF_TYPE_FMRI, 1,
1430             restarter);
1431 
1432         r = internal_attach_property(pg, p);
1433         if (r != 0) {
1434                 internal_property_free(p);
1435                 return (-1);
1436         }
1437 
1438         return (0);
1439 }
1440 
1441 static void
1442 lxml_get_paramval(pgroup_t *pgrp, const char *propname, xmlNodePtr pval)
1443 {
1444         property_t *p;
1445         char *value;
1446         char *prop;
1447 
1448         prop = safe_strdup(propname);
1449 
1450         value = (char *)xmlGetProp(pval, (xmlChar *)value_attr);
1451         if (value == NULL || *value == '\0')
1452                 uu_die(gettext("property value missing for property '%s/%s'\n"),
1453                     pgrp->sc_pgroup_name, propname);
1454         p = internal_property_create(prop, SCF_TYPE_ASTRING, 1, value);
1455 
1456         (void) internal_attach_property(pgrp, p);
1457 }
1458 
1459 static void
1460 lxml_get_parameter(pgroup_t *pgrp, const char *propname, xmlNodePtr param)
1461 {
1462         property_t *p = internal_property_new();
1463 
1464         p->sc_property_name = safe_strdup(propname);
1465         p->sc_value_type = SCF_TYPE_ASTRING;
1466 
1467         (void) lxml_get_value(p, SC_ASTRING, param);
1468 
1469         (void) internal_attach_property(pgrp, p);
1470 }
1471 
1472 static void
1473 lxml_get_type(pgroup_t *pgrp, xmlNodePtr type)
1474 {
1475         property_t *p;
1476         xmlChar *name;
1477         xmlChar *active;
1478         xmlNodePtr cursor;
1479         uint64_t active_val;
1480         size_t sz = max_scf_name_len + 1;
1481         char *propname = safe_malloc(sz);
1482 
1483         if (pgrp->sc_parent->sc_op == SVCCFG_OP_APPLY)
1484                 lxml_validate_element(type);
1485 
1486         name = xmlGetProp(type, (xmlChar *)name_attr);
1487         if (name == NULL || *name == '\0')
1488                 uu_die(gettext("attribute name missing in element 'type'\n"));
1489 
1490         for (cursor = type->xmlChildrenNode; cursor != NULL;
1491             cursor = cursor->next) {
1492                 xmlChar *pname;
1493 
1494                 if (lxml_ignorable_block(cursor))
1495                         continue;
1496 
1497                 pname = xmlGetProp(cursor, (xmlChar *)name_attr);
1498                 if (pname == NULL || *pname == '\0')
1499                         uu_die(gettext(
1500                             "attribute name missing in sub-element of type\n"));
1501 
1502                 if (snprintf(propname, sz, "%s,%s", (char *)name,
1503                     (char *)pname) >= sz)
1504                         uu_die(gettext("name '%s,%s' is too long\n"),
1505                             (char *)name, (char *)pname);
1506                 xmlFree(pname);
1507 
1508                 switch (lxml_xlate_element(cursor->name)) {
1509                 case SC_PARAMETER:
1510                         lxml_get_parameter(pgrp, propname, cursor);
1511                         break;
1512 
1513                 case SC_PARAMVAL:
1514                         lxml_get_paramval(pgrp, propname, cursor);
1515                         break;
1516 
1517                 default:
1518                         uu_die(gettext("unknown element %s\n"), cursor->name);
1519                 }
1520         }
1521 
1522         active = xmlGetProp(type, (xmlChar *)active_attr);
1523         if (active == NULL || strcmp(true, (const char *)active) == 0)
1524                 active_val = 1;
1525         else
1526                 active_val = 0;
1527         xmlFree(active);
1528 
1529         if (snprintf(propname, sz, "%s,%s", (char *)name,
1530             SCF_PROPERTY_ACTIVE_POSTFIX) >= sz)
1531                 uu_die(gettext("name '%s,%s' is too long\n"),
1532                     (char *)name, SCF_PROPERTY_ACTIVE_POSTFIX);
1533 
1534         p = internal_property_create(propname, SCF_TYPE_BOOLEAN, 1, active_val);
1535 
1536         (void) internal_attach_property(pgrp, p);
1537 
1538         xmlFree(name);
1539 }
1540 
1541 static void
1542 lxml_get_event(entity_t *entity, const char *pgname, xmlNodePtr np)
1543 {
1544         xmlNodePtr cursor;
1545         pgroup_t *pgrp;
1546 
1547         pgrp = internal_pgroup_find_or_create(entity, pgname,
1548             SCF_NOTIFY_PARAMS_PG_TYPE);
1549         for (cursor = np->xmlChildrenNode; cursor != NULL;
1550             cursor = cursor->next) {
1551                 if (lxml_ignorable_block(cursor))
1552                         continue;
1553 
1554                 switch (lxml_xlate_element(cursor->name)) {
1555                 case SC_EVENT:
1556                         continue;
1557 
1558                 case SC_TYPE:
1559                         lxml_get_type(pgrp, cursor);
1560                         break;
1561 
1562                 default:
1563                         uu_warn(gettext("illegal element '%s' on "
1564                             "notification parameters\n"), cursor->name);
1565                 }
1566         }
1567 }
1568 
1569 static int
1570 lxml_get_notification_parameters(entity_t *entity, xmlNodePtr np)
1571 {
1572         char *event = NULL;
1573         char **pgs = NULL;
1574         char **p;
1575         char *pgname = NULL;
1576         xmlNodePtr cursor;
1577         int32_t tset, t;
1578         size_t sz = max_scf_name_len + 1;
1579         int count;
1580         int r = -1;
1581 
1582         for (count = 0, cursor = np->xmlChildrenNode; cursor != NULL;
1583             cursor = cursor->next) {
1584                 if (lxml_ignorable_block(cursor))
1585                         continue;
1586 
1587                 if (lxml_xlate_element(cursor->name) == SC_EVENT) {
1588                         xmlChar *s;
1589 
1590                         count++;
1591                         if (count > 1)
1592                                 uu_die(gettext("Can't have more than 1 element "
1593                                     "event in a notification parameter\n"));
1594                         s = xmlGetProp(cursor, (xmlChar *)value_attr);
1595                         if (s == NULL || (event = strdup((char *)s)) == NULL)
1596                                 uu_die(gettext("couldn't allocate memory"));
1597                         xmlFree(s);
1598                 }
1599         }
1600 
1601         pgs = tokenize(event, ",");
1602 
1603         switch (tset = check_tokens(pgs)) {
1604         case INVALID_TOKENS:
1605                 uu_die(gettext("Invalid input.\n"));
1606                 /*NOTREACHED*/
1607         case MIXED_TOKENS:
1608                 semerr(gettext("Can't mix SMF and FMA event definitions\n"));
1609                 goto out;
1610         case FMA_TOKENS:
1611                 /* make sure this is SCF_NOTIFY_PARAMS_INST */
1612                 if (entity->sc_etype != SVCCFG_INSTANCE_OBJECT ||
1613                     strcmp(entity->sc_fmri, SCF_NOTIFY_PARAMS_INST) != 0) {
1614                         semerr(gettext(
1615                             "Non-SMF transition evenst must go to %s\n"),
1616                             SCF_NOTIFY_PARAMS_INST);
1617                         goto out;
1618                 }
1619                 pgname = safe_malloc(sz);
1620                 for (p = pgs; *p; ++p) {
1621                         if (snprintf(pgname, sz, "%s,%s", de_tag(*p),
1622                             SCF_NOTIFY_PG_POSTFIX) >= sz)
1623                                 uu_die(gettext("event name too long: %s\n"),
1624                                     *p);
1625 
1626                         lxml_get_event(entity, pgname, np);
1627                 }
1628 
1629         default:        /* smf state transition tokens */
1630                 if (entity->sc_etype == SVCCFG_SERVICE_OBJECT &&
1631                     strcmp(entity->sc_fmri, SCF_SERVICE_GLOBAL) == 0) {
1632                         semerr(gettext(
1633                             "Can't set events for global service\n"));
1634                         goto out;
1635                 }
1636                 for (t = 0x1; t < SCF_STATE_ALL; t <<= 1) {
1637                         if (t & tset) {
1638                                 lxml_get_event(entity, tset_to_string(t), np);
1639                         }
1640                         if ((t << 16) & tset) {
1641                                 lxml_get_event(entity, tset_to_string(t << 16),
1642                                     np);
1643                         }
1644                 }
1645         }
1646 
1647         r = 0;
1648 out:
1649         free(pgname);
1650         free(pgs);
1651         free(event);
1652 
1653         return (r);
1654 }
1655 
1656 /*
1657  * Add a property containing the localized text from the manifest.  The
1658  * property is added to the property group at pg.  The name of the created
1659  * property is based on the format at pn_format.  This is an snprintf(3C)
1660  * format containing a single %s conversion specification.  At conversion
1661  * time, the %s is replaced by the locale designation.
1662  *
1663  * source is the source element and it is only used for error messages.
1664  */
1665 static int
1666 lxml_get_loctext(entity_t *service, pgroup_t *pg, xmlNodePtr loctext,
1667     const char *pn_format, const char *source)
1668 {
1669         int extra;
1670         xmlNodePtr cursor;
1671         xmlChar *val;
1672         char *stripped, *cp;
1673         property_t *p;
1674         char *prop_name;
1675         int r;
1676 
1677         if (((val = xmlGetProp(loctext, (xmlChar *)xml_lang_attr)) == NULL) ||
1678             (*val == 0)) {
1679                 if (((val = xmlGetProp(loctext,
1680                     (xmlChar *)lang_attr)) == NULL) || (*val == 0)) {
1681                         val = (xmlChar *)"unknown";
1682                 }
1683         }
1684 
1685         _scf_sanitize_locale((char *)val);
1686         prop_name = safe_malloc(max_scf_name_len + 1);
1687         if ((extra = snprintf(prop_name, max_scf_name_len + 1, pn_format,
1688             val)) >= max_scf_name_len + 1) {
1689                 extra -= max_scf_name_len;
1690                 uu_die(gettext("%s attribute is %d characters too long for "
1691                     "%s in %s\n"),
1692                     xml_lang_attr, extra, source, service->sc_name);
1693         }
1694         xmlFree(val);
1695 
1696         for (cursor = loctext->xmlChildrenNode; cursor != NULL;
1697             cursor = cursor->next) {
1698                 if (strcmp("text", (const char *)cursor->name) == 0) {
1699                         break;
1700                 } else if (strcmp("comment", (const char *)cursor->name) != 0) {
1701                         uu_die(gettext("illegal element \"%s\" on loctext "
1702                             "element for \"%s\"\n"), cursor->name,
1703                             service->sc_name);
1704                 }
1705         }
1706 
1707         if (cursor == NULL) {
1708                 uu_die(gettext("loctext element has no content for \"%s\"\n"),
1709                     service->sc_name);
1710         }
1711 
1712         /*
1713          * Remove leading and trailing whitespace.
1714          */
1715         stripped = safe_strdup((const char *)cursor->content);
1716 
1717         for (; isspace(*stripped); stripped++)
1718                 ;
1719         for (cp = stripped + strlen(stripped) - 1; isspace(*cp); cp--)
1720                 ;
1721         *(cp + 1) = '\0';
1722 
1723         p = internal_property_create(prop_name, SCF_TYPE_USTRING, 1,
1724             stripped);
1725 
1726         r = internal_attach_property(pg, p);
1727         if (r != 0) {
1728                 internal_property_free(p);
1729                 free(prop_name);
1730         }
1731 
1732         return (r);
1733 }
1734 
1735 /*
1736  * This function processes all loctext elements in the current XML element
1737  * designated by container.  A property is created for each loctext element
1738  * and added to the property group at pg.  The name of the property is
1739  * derived from the loctext language designation using the format at
1740  * pn_format.  pn_format should be an snprintf format string containing one
1741  * %s which is replaced by the language designation.
1742  *
1743  * The function returns 0 on success and -1 if it is unable to attach the
1744  * newly created property to pg.
1745  */
1746 static int
1747 lxml_get_all_loctext(entity_t *service, pgroup_t *pg, xmlNodePtr container,
1748     const char *pn_format, const char *source)
1749 {
1750         xmlNodePtr cursor;
1751 
1752         /*
1753          * Iterate through one or more loctext elements.  The locale is
1754          * used to generate the property name; the contents are the ustring
1755          * value for the property.
1756          */
1757         for (cursor = container->xmlChildrenNode; cursor != NULL;
1758             cursor = cursor->next) {
1759                 if (lxml_ignorable_block(cursor))
1760                         continue;
1761 
1762                 switch (lxml_xlate_element(cursor->name)) {
1763                 case SC_LOCTEXT:
1764                         if (lxml_get_loctext(service, pg, cursor, pn_format,
1765                             source))
1766                                 return (-1);
1767                         break;
1768                 default:
1769                         uu_die(gettext("illegal element \"%s\" on %s element "
1770                             "for \"%s\"\n"), cursor->name, container->name,
1771                             service->sc_name);
1772                         break;
1773                 }
1774         }
1775 
1776         return (0);
1777 }
1778 
1779 /*
1780  * Obtain the specified cardinality attribute and place it in a property
1781  * named prop_name.  The converted attribute is placed at *value, and the
1782  * newly created property is returned to propp.  NULL is returned to propp
1783  * if the attribute is not provided in the manifest.
1784  *
1785  * 0 is returned upon success, and -1 indicates that the manifest contained
1786  * an invalid cardinality value.
1787  */
1788 static int
1789 lxml_get_cardinality_attribute(entity_t *service, xmlNodePtr cursor,
1790     const char *attr_name, const char *prop_name, uint64_t *value,
1791     property_t **propp)
1792 {
1793         char *c;
1794         property_t *p;
1795         xmlChar *val;
1796         uint64_t count;
1797         char *endptr;
1798 
1799         *propp = NULL;
1800         val = xmlGetProp(cursor, (xmlChar *)attr_name);
1801         if (val == NULL)
1802                 return (0);
1803         if (*val == 0) {
1804                 xmlFree(val);
1805                 return (0);
1806         }
1807 
1808         /*
1809          * Make sure that the string at val doesn't have a leading minus
1810          * sign.  The strtoull() call below does not catch this problem.
1811          */
1812         for (c = (char *)val; *c != 0; c++) {
1813                 if (isspace(*c))
1814                         continue;
1815                 if (isdigit(*c))
1816                         break;
1817                 semerr(gettext("\"%c\" is not a legal character in the %s "
1818                     "attribute of the %s element in %s.\n"), *c,
1819                     attr_name, prop_name, service->sc_name);
1820                 xmlFree(val);
1821                 return (-1);
1822         }
1823         errno = 0;
1824         count = strtoull((char *)val, &endptr, 10);
1825         if (errno != 0 || endptr == (char *)val || *endptr) {
1826                 semerr(gettext("\"%s\" is not a legal number for the %s "
1827                     "attribute of the %s element in %s.\n"), (char *)val,
1828                     attr_name, prop_name, service->sc_name);
1829                 xmlFree(val);
1830                 return (-1);
1831         }
1832 
1833         xmlFree(val);
1834 
1835         /* Value is valid.  Create the property. */
1836         p = internal_property_create(prop_name, SCF_TYPE_COUNT, 1, count);
1837         *value = count;
1838         *propp = p;
1839         return (0);
1840 }
1841 
1842 /*
1843  * The cardinality is specified by two attributes max and min at cursor.
1844  * Both are optional, but if present they must be unsigned integers.
1845  */
1846 static int
1847 lxml_get_tm_cardinality(entity_t *service, pgroup_t *pg, xmlNodePtr cursor)
1848 {
1849         int min_attached = 0;
1850         int compare = 1;
1851         property_t *min_prop;
1852         property_t *max_prop;
1853         uint64_t max;
1854         uint64_t min;
1855         int r;
1856 
1857         r = lxml_get_cardinality_attribute(service, cursor, min_attr,
1858             SCF_PROPERTY_TM_CARDINALITY_MIN, &min, &min_prop);
1859         if (r != 0)
1860                 return (r);
1861         if (min_prop == NULL)
1862                 compare = 0;
1863         r = lxml_get_cardinality_attribute(service, cursor, max_attr,
1864             SCF_PROPERTY_TM_CARDINALITY_MAX, &max, &max_prop);
1865         if (r != 0)
1866                 goto errout;
1867         if ((max_prop != NULL) && (compare == 1)) {
1868                 if (max < min) {
1869                         semerr(gettext("Cardinality max is less than min for "
1870                             "the %s element in %s.\n"), pg->sc_pgroup_name,
1871                             service->sc_fmri);
1872                         goto errout;
1873                 }
1874         }
1875 
1876         /* Attach the properties to the property group. */
1877         if (min_prop) {
1878                 if (internal_attach_property(pg, min_prop) == 0) {
1879                         min_attached = 1;
1880                 } else {
1881                         goto errout;
1882                 }
1883         }
1884         if (max_prop) {
1885                 if (internal_attach_property(pg, max_prop) != 0) {
1886                         if (min_attached)
1887                                 internal_detach_property(pg, min_prop);
1888                         goto errout;
1889                 }
1890         }
1891         return (0);
1892 
1893 errout:
1894         if (min_prop)
1895                 internal_property_free(min_prop);
1896         if (max_prop)
1897                 internal_property_free(max_prop);
1898         return (-1);
1899 }
1900 
1901 /*
1902  * Get the common_name which is present as localized text at common_name in
1903  * the manifest.  The common_name is stored as the value of a property in
1904  * the property group whose name is SCF_PG_TM_COMMON_NAME and type is
1905  * SCF_GROUP_TEMPLATE.  This property group will be created in service if
1906  * it is not already there.
1907  */
1908 static int
1909 lxml_get_tm_common_name(entity_t *service, xmlNodePtr common_name)
1910 {
1911         pgroup_t *pg;
1912 
1913         /*
1914          * Create the property group, if absent.
1915          */
1916         pg = internal_pgroup_find_or_create(service, SCF_PG_TM_COMMON_NAME,
1917             SCF_GROUP_TEMPLATE);
1918 
1919         return (lxml_get_all_loctext(service, pg, common_name, LOCALE_ONLY_FMT,
1920             "common_name"));
1921 }
1922 
1923 /*
1924  * Get the description which is present as localized text at description in
1925  * the manifest.  The description is stored as the value of a property in
1926  * the property group whose name is SCF_PG_TM_DESCRIPTION and type is
1927  * SCF_GROUP_TEMPLATE.  This property group will be created in service if
1928  * it is not already there.
1929  */
1930 static int
1931 lxml_get_tm_description(entity_t *service, xmlNodePtr description)
1932 {
1933         pgroup_t *pg;
1934 
1935         /*
1936          * Create the property group, if absent.
1937          */
1938         pg = internal_pgroup_find_or_create(service, SCF_PG_TM_DESCRIPTION,
1939             SCF_GROUP_TEMPLATE);
1940 
1941         return (lxml_get_all_loctext(service, pg, description,
1942             LOCALE_ONLY_FMT, "description"));
1943 }
1944 
1945 static char *
1946 lxml_label_to_groupname(const char *prefix, const char *in)
1947 {
1948         char *out, *cp;
1949         size_t len, piece_len;
1950 
1951         out = uu_zalloc(2 * max_scf_name_len + 1);
1952         if (out == NULL)
1953                 return (NULL);
1954 
1955         (void) strlcpy(out, prefix, 2 * max_scf_name_len + 1);
1956 
1957         len = strlcat(out, in, 2 * max_scf_name_len + 1);
1958         if (len > max_scf_name_len) {
1959                 /* Use the first half and the second half. */
1960                 piece_len = (max_scf_name_len - 2) / 2;
1961 
1962                 (void) strncpy(out + piece_len, "..", 2);
1963 
1964                 (void) strcpy(out + piece_len + 2, out + (len - piece_len));
1965         }
1966 
1967         /*
1968          * Translate non-property characters to '_'.
1969          */
1970         for (cp = out; *cp != '\0'; ++cp) {
1971                 if (!(isalnum(*cp) || *cp == '_' || *cp == '-'))
1972                         *cp = '_';
1973         }
1974 
1975         return (out);
1976 }
1977 
1978 /*
1979  * If *p is NULL, astring_prop_value() first creates a property with the
1980  * name specified in prop_name.  The address of the newly created property
1981  * is placed in *p.
1982  *
1983  * In either case, newly created property or existing property, a new
1984  * SCF_TYPE_ASTRING value will created and attached to the property at *p.
1985  * The value of the newly created property is prop_value.
1986  *
1987  * free_flag is used to indicate whether or not the memory at prop_value
1988  * should be freed when the property is freed by a call to
1989  * internal_property_free().
1990  */
1991 static void
1992 astring_prop_value(property_t **p, const char *prop_name, char *prop_value,
1993     boolean_t free_flag)
1994 {
1995         value_t *v;
1996 
1997         if (*p == NULL) {
1998                 /* Create the property */
1999                 *p = internal_property_new();
2000                 (*p)->sc_property_name = (char *)prop_name;
2001                 (*p)->sc_value_type = SCF_TYPE_ASTRING;
2002         }
2003 
2004         /* Add the property value to the property's list of values. */
2005         v = internal_value_new();
2006         v->sc_type = SCF_TYPE_ASTRING;
2007         if (free_flag == B_TRUE)
2008                 v->sc_free = lxml_free_str;
2009         v->sc_u.sc_string = prop_value;
2010         internal_attach_value(*p, v);
2011 }
2012 
2013 /*
2014  * If p points to a null pointer, create an internal_separators property
2015  * saving the address at p.  For each character at seps create a property
2016  * value and attach it to the property at p.
2017  */
2018 static void
2019 seps_to_prop_values(property_t **p, xmlChar *seps)
2020 {
2021         value_t *v;
2022         char val_str[2];
2023 
2024         if (*p == NULL) {
2025                 *p = internal_property_new();
2026                 (*p)->sc_property_name =
2027                     (char *)SCF_PROPERTY_INTERNAL_SEPARATORS;
2028                 (*p)->sc_value_type = SCF_TYPE_ASTRING;
2029         }
2030 
2031         /* Add the values to the property's list. */
2032         val_str[1] = 0;         /* Terminate the string. */
2033         for (; *seps != 0; seps++) {
2034                 v = internal_value_new();
2035                 v->sc_type = (*p)->sc_value_type;
2036                 v->sc_free = lxml_free_str;
2037                 val_str[0] = *seps;
2038                 v->sc_u.sc_string = safe_strdup(val_str);
2039                 internal_attach_value(*p, v);
2040         }
2041 }
2042 
2043 /*
2044  * Create an internal_separators property and attach it to the property
2045  * group at pg.  The separator characters are provided in the text nodes
2046  * that are the children of seps.  Each separator character is stored as a
2047  * property value in the internal_separators property.
2048  */
2049 static int
2050 lxml_get_tm_internal_seps(entity_t *service, pgroup_t *pg, xmlNodePtr seps)
2051 {
2052         xmlNodePtr cursor;
2053         property_t *prop = NULL;
2054         int r;
2055 
2056         for (cursor = seps->xmlChildrenNode; cursor != NULL;
2057             cursor = cursor->next) {
2058                 if (strcmp("text", (const char *)cursor->name) == 0) {
2059                         seps_to_prop_values(&prop, cursor->content);
2060                 } else if (strcmp("comment", (const char *)cursor->name) != 0) {
2061                         uu_die(gettext("illegal element \"%s\" on %s element "
2062                             "for \"%s\"\n"), cursor->name, seps->name,
2063                             service->sc_name);
2064                 }
2065         }
2066         if (prop == NULL) {
2067                 semerr(gettext("The %s element in %s had an empty list of "
2068                     "separators.\n"), (const char *)seps->name,
2069                     service->sc_name);
2070                 return (-1);
2071         }
2072         r = internal_attach_property(pg, prop);
2073         if (r != 0)
2074                 internal_property_free(prop);
2075         return (r);
2076 }
2077 
2078 static int
2079 lxml_get_tm_manpage(entity_t *service, xmlNodePtr manpage)
2080 {
2081         pgroup_t *pg;
2082         char *pgname;
2083         char *name;
2084         xmlChar *title;
2085         xmlChar *section;
2086 
2087         /*
2088          * Fetch title and section attributes, convert to something sanitized,
2089          * and create property group.
2090          */
2091         title = xmlGetProp(manpage, (xmlChar *)title_attr);
2092         if (title == NULL)
2093                 return (-1);
2094         section = xmlGetProp(manpage, (xmlChar *)section_attr);
2095         if (section == NULL) {
2096                 xmlFree(title);
2097                 return (-1);
2098         }
2099 
2100         name = safe_malloc(max_scf_name_len + 1);
2101 
2102         /* Find existing property group with underscore separators */
2103         (void) snprintf(name, max_scf_name_len + 1, "%s_%s", title, section);
2104         pgname = lxml_label_to_groupname(SCF_PG_TM_MAN_PREFIX, name);
2105         pg = internal_pgroup_find(service, pgname, SCF_GROUP_TEMPLATE);
2106 
2107         uu_free(pgname);
2108         (void) snprintf(name, max_scf_name_len + 1, "%s%s", title, section);
2109         pgname = lxml_label_to_groupname(SCF_PG_TM_MAN_PREFIX, name);
2110 
2111         if (pg == NULL) {
2112                 pg = internal_pgroup_find_or_create(service, pgname,
2113                     SCF_GROUP_TEMPLATE);
2114         } else {
2115                 /* Rename property group */
2116                 free((char *)pg->sc_pgroup_name);
2117                 pg->sc_pgroup_name = safe_strdup(pgname);
2118         }
2119 
2120         uu_free(pgname);
2121         free(name);
2122         xmlFree(section);
2123         xmlFree(title);
2124 
2125 
2126         /*
2127          * Each attribute is an astring property within the group.
2128          */
2129         if (new_str_prop_from_attr(pg, SCF_PROPERTY_TM_TITLE,
2130             SCF_TYPE_ASTRING, manpage, title_attr) != 0 ||
2131             new_str_prop_from_attr(pg, SCF_PROPERTY_TM_SECTION,
2132             SCF_TYPE_ASTRING, manpage, section_attr) != 0 ||
2133             new_str_prop_from_attr(pg, SCF_PROPERTY_TM_MANPATH,
2134             SCF_TYPE_ASTRING, manpage, manpath_attr) != 0)
2135                 return (-1);
2136 
2137         return (0);
2138 }
2139 
2140 static int
2141 lxml_get_tm_doclink(entity_t *service, xmlNodePtr doc_link)
2142 {
2143         pgroup_t *pg;
2144         char *pgname;
2145         xmlChar *name;
2146 
2147         /*
2148          * Fetch name attribute, convert name to something sanitized, and create
2149          * property group.
2150          */
2151         name = xmlGetProp(doc_link, (xmlChar *)name_attr);
2152         if (name == NULL)
2153                 return (-1);
2154 
2155         pgname = (char *)lxml_label_to_groupname(SCF_PG_TM_DOC_PREFIX,
2156             (const char *)name);
2157 
2158         pg = internal_pgroup_find_or_create(service, pgname,
2159             (char *)SCF_GROUP_TEMPLATE);
2160 
2161         uu_free(pgname);
2162         xmlFree(name);
2163 
2164         /*
2165          * Each attribute is an astring property within the group.
2166          */
2167         if (new_str_prop_from_attr(pg, SCF_PROPERTY_TM_NAME, SCF_TYPE_ASTRING,
2168             doc_link, name_attr) != 0 ||
2169             new_str_prop_from_attr(pg, SCF_PROPERTY_TM_URI, SCF_TYPE_ASTRING,
2170             doc_link, uri_attr) != 0)
2171                 return (-1);
2172 
2173         return (0);
2174 }
2175 
2176 static int
2177 lxml_get_tm_documentation(entity_t *service, xmlNodePtr documentation)
2178 {
2179         xmlNodePtr cursor;
2180 
2181         for (cursor = documentation->xmlChildrenNode; cursor != NULL;
2182             cursor = cursor->next) {
2183                 if (lxml_ignorable_block(cursor))
2184                         continue;
2185 
2186                 switch (lxml_xlate_element(cursor->name)) {
2187                 case SC_MANPAGE:
2188                         (void) lxml_get_tm_manpage(service, cursor);
2189                         break;
2190                 case SC_DOC_LINK:
2191                         (void) lxml_get_tm_doclink(service, cursor);
2192                         break;
2193                 default:
2194                         uu_die(gettext("illegal element \"%s\" on template "
2195                             "for service \"%s\"\n"),
2196                             cursor->name, service->sc_name);
2197                 }
2198         }
2199 
2200         return (0);
2201 }
2202 
2203 static int
2204 lxml_get_prop_pattern_attributes(pgroup_t *pg, xmlNodePtr cursor)
2205 {
2206         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_TM_NAME,
2207             SCF_TYPE_ASTRING, cursor, name_attr, NULL) != 0) {
2208                 return (-1);
2209         }
2210         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_TM_TYPE,
2211             SCF_TYPE_ASTRING, cursor, type_attr, "") != 0) {
2212                 return (-1);
2213         }
2214         if (new_bool_prop_from_attr(pg, SCF_PROPERTY_TM_REQUIRED, cursor,
2215             required_attr) != 0)
2216                 return (-1);
2217         return (0);
2218 }
2219 
2220 static int
2221 lxml_get_tm_include_values(entity_t *service, pgroup_t *pg,
2222     xmlNodePtr include_values, const char *prop_name)
2223 {
2224         boolean_t attach_to_pg = B_FALSE;
2225         property_t *p;
2226         int r = 0;
2227         char *type;
2228 
2229         /* Get the type attribute of the include_values element. */
2230         type = (char *)xmlGetProp(include_values, (const xmlChar *)type_attr);
2231         if ((type == NULL) || (*type == 0)) {
2232                 uu_die(gettext("%s element requires a %s attribute in the %s "
2233                     "service.\n"), include_values->name, type_attr,
2234                     service->sc_name);
2235         }
2236 
2237         /* Add the type to the values of the prop_name property. */
2238         p = internal_property_find(pg, prop_name);
2239         if (p == NULL)
2240                 attach_to_pg = B_TRUE;
2241         astring_prop_value(&p, prop_name, type, B_FALSE);
2242         if (attach_to_pg == B_TRUE) {
2243                 r = internal_attach_property(pg, p);
2244                 if (r != 0)
2245                         internal_property_free(p);
2246         }
2247         return (r);
2248 }
2249 
2250 #define RC_MIN          0
2251 #define RC_MAX          1
2252 #define RC_COUNT        2
2253 
2254 /*
2255  * Verify that the strings at min and max are valid numeric strings.  Also
2256  * verify that max is numerically >= min.
2257  *
2258  * 0 is returned if the range is valid, and -1 is returned if it is not.
2259  */
2260 static int
2261 verify_range(entity_t *service, xmlNodePtr range, char *min, char *max)
2262 {
2263         char *c;
2264         int i;
2265         int is_signed = 0;
2266         int inverted = 0;
2267         const char *limit[RC_COUNT];
2268         char *strings[RC_COUNT];
2269         uint64_t urange[RC_COUNT];      /* unsigned range. */
2270         int64_t srange[RC_COUNT];       /* signed range. */
2271 
2272         strings[RC_MIN] = min;
2273         strings[RC_MAX] = max;
2274         limit[RC_MIN] = min_attr;
2275         limit[RC_MAX] = max_attr;
2276 
2277         /* See if the range is signed. */
2278         for (i = 0; (i < RC_COUNT) && (is_signed == 0); i++) {
2279                 c = strings[i];
2280                 while (isspace(*c)) {
2281                         c++;
2282                 }
2283                 if (*c == '-')
2284                         is_signed = 1;
2285         }
2286 
2287         /* Attempt to convert the strings. */
2288         for (i = 0; i < RC_COUNT; i++) {
2289                 errno = 0;
2290                 if (is_signed) {
2291                         srange[i] = strtoll(strings[i], &c, 0);
2292                 } else {
2293                         urange[i] = strtoull(strings[i], &c, 0);
2294                 }
2295                 if ((errno != 0) || (c == strings[i]) || (*c != 0)) {
2296                         /* Conversion failed. */
2297                         uu_die(gettext("Unable to convert %s for the %s "
2298                             "element in service %s.\n"), limit[i],
2299                             (char *)range->name, service->sc_name);
2300                 }
2301         }
2302 
2303         /* Make sure that min is <= max */
2304         if (is_signed) {
2305                 if (srange[RC_MAX] < srange[RC_MIN])
2306                         inverted = 1;
2307         } else {
2308                 if (urange[RC_MAX] < urange[RC_MIN])
2309                         inverted = 1;
2310         }
2311         if (inverted != 0) {
2312                 semerr(gettext("Maximum less than minimum for the %s element "
2313                     "in service %s.\n"), (char *)range->name,
2314                     service->sc_name);
2315                 return (-1);
2316         }
2317 
2318         return (0);
2319 }
2320 
2321 /*
2322  * This, function creates a property named prop_name.  The range element
2323  * should have two attributes -- min and max.  The property value then
2324  * becomes the concatenation of their value separated by a comma.  The
2325  * property is then attached to the property group at pg.
2326  *
2327  * If pg already contains a property with a name of prop_name, it is only
2328  * necessary to create a new value and attach it to the existing property.
2329  */
2330 static int
2331 lxml_get_tm_range(entity_t *service, pgroup_t *pg, xmlNodePtr range,
2332     const char *prop_name)
2333 {
2334         boolean_t attach_to_pg = B_FALSE;
2335         char *max;
2336         char *min;
2337         property_t *p;
2338         char *prop_value;
2339         int r = 0;
2340 
2341         /* Get max and min from the XML description. */
2342         max = (char *)xmlGetProp(range, (xmlChar *)max_attr);
2343         if ((max == NULL) || (*max == 0)) {
2344                 uu_die(gettext("%s element is missing the %s attribute in "
2345                     "service %s.\n"), (char *)range->name, max_attr,
2346                     service->sc_name);
2347         }
2348         min = (char *)xmlGetProp(range, (xmlChar *)min_attr);
2349         if ((min == NULL) || (*min == 0)) {
2350                 uu_die(gettext("%s element is missing the %s attribute in "
2351                     "service %s.\n"), (char *)range->name, min_attr,
2352                     service->sc_name);
2353         }
2354         if (verify_range(service, range, min, max) != 0) {
2355                 xmlFree(min);
2356                 xmlFree(max);
2357                 return (-1);
2358         }
2359 
2360         /* Property value is concatenation of min and max. */
2361         prop_value = safe_malloc(max_scf_value_len + 1);
2362         if (snprintf(prop_value, max_scf_value_len + 1, "%s,%s", min, max) >=
2363             max_scf_value_len + 1) {
2364                 uu_die(gettext("min and max are too long for the %s element "
2365                     "of %s.\n"), (char *)range->name, service->sc_name);
2366         }
2367         xmlFree(min);
2368         xmlFree(max);
2369 
2370         /*
2371          * If necessary create the property and attach it to the property
2372          * group.
2373          */
2374         p = internal_property_find(pg, prop_name);
2375         if (p == NULL)
2376                 attach_to_pg = B_TRUE;
2377         astring_prop_value(&p, prop_name, prop_value, B_TRUE);
2378         if (attach_to_pg == B_TRUE) {
2379                 r = internal_attach_property(pg, p);
2380                 if (r != 0) {
2381                         internal_property_free(p);
2382                 }
2383         }
2384         return (r);
2385 }
2386 
2387 /*
2388  * Determine how many plain characters are represented by count Base32
2389  * encoded characters.  5 plain text characters are converted to 8 Base32
2390  * characters.
2391  */
2392 static size_t
2393 encoded_count_to_plain(size_t count)
2394 {
2395         return (5 * ((count + 7) / 8));
2396 }
2397 
2398 /*
2399  * The value element contains 0 or 1 common_name element followed by 0 or 1
2400  * description element.  It also has a required attribute called "name".
2401  * The common_name and description are stored as property values in pg.
2402  * The property names are:
2403  *      value_<name>_common_name_<lang>
2404  *      value_<name>_description_<lang>
2405  *
2406  * The <name> portion of the preceeding proper names requires more
2407  * explanation.  Ideally it would just the name attribute of this value
2408  * element.  Unfortunately, the name attribute can contain characters that
2409  * are not legal in a property name.  Thus, we base 32 encode the name
2410  * attribute and use that for <name>.
2411  *
2412  * There are cases where the caller needs to know the name, so it is
2413  * returned through the name_value pointer if it is not NULL.
2414  *
2415  * Parameters:
2416  *      service -       Information about the service that is being
2417  *                      processed.  This function only uses this parameter
2418  *                      for producing error messages.
2419  *
2420  *      pg -            The property group to receive the newly created
2421  *                      properties.
2422  *
2423  *      value -         Pointer to the value element in the XML tree.
2424  *
2425  *      name_value -    Address to receive the value of the name attribute.
2426  *                      The caller must free the memory.
2427  */
2428 static int
2429 lxml_get_tm_value_element(entity_t *service, pgroup_t *pg, xmlNodePtr value,
2430     char **name_value)
2431 {
2432         char *common_name_fmt;
2433         xmlNodePtr cursor;
2434         char *description_fmt;
2435         char *encoded_value = NULL;
2436         size_t extra;
2437         char *value_name;
2438         int r = 0;
2439 
2440         common_name_fmt = safe_malloc(max_scf_name_len + 1);
2441         description_fmt = safe_malloc(max_scf_name_len + 1);
2442 
2443         /*
2444          * Get the value of our name attribute, so that we can use it to
2445          * construct property names.
2446          */
2447         value_name = (char *)xmlGetProp(value, (xmlChar *)name_attr);
2448         /* The value name must be present, but it can be empty. */
2449         if (value_name == NULL) {
2450                 uu_die(gettext("%s element requires a %s attribute in the %s "
2451                     "service.\n"), (char *)value->name, name_attr,
2452                     service->sc_name);
2453         }
2454 
2455         /*
2456          * The value_name may contain characters that are not valid in in a
2457          * property name.  So we will encode value_name and then use the
2458          * encoded value in the property name.
2459          */
2460         encoded_value = safe_malloc(max_scf_name_len + 1);
2461         if (scf_encode32(value_name, strlen(value_name), encoded_value,
2462             max_scf_name_len + 1, &extra, SCF_ENCODE32_PAD) != 0) {
2463                 extra = encoded_count_to_plain(extra - max_scf_name_len);
2464                 uu_die(gettext("Constructed property name is %u characters "
2465                     "too long for value \"%s\" in the %s service.\n"),
2466                     extra, value_name, service->sc_name);
2467         }
2468         if ((extra = snprintf(common_name_fmt, max_scf_name_len + 1,
2469             VALUE_COMMON_NAME_FMT, SCF_PROPERTY_TM_VALUE_PREFIX,
2470             encoded_value)) >= max_scf_name_len + 1) {
2471                 extra = encoded_count_to_plain(extra - max_scf_name_len);
2472                 uu_die(gettext("Name attribute is "
2473                     "%u characters too long for %s in service %s\n"),
2474                     extra, (char *)value->name, service->sc_name);
2475         }
2476         if ((extra = snprintf(description_fmt, max_scf_name_len + 1,
2477             VALUE_DESCRIPTION_FMT, SCF_PROPERTY_TM_VALUE_PREFIX,
2478             encoded_value)) >= max_scf_name_len + 1) {
2479                 extra = encoded_count_to_plain(extra - max_scf_name_len);
2480                 uu_die(gettext("Name attribute is "
2481                     "%u characters too long for %s in service %s\n"),
2482                     extra, (char *)value->name, service->sc_name);
2483         }
2484 
2485         for (cursor = value->xmlChildrenNode;
2486             cursor != NULL;
2487             cursor = cursor->next) {
2488                 if (lxml_ignorable_block(cursor))
2489                         continue;
2490                 switch (lxml_xlate_element(cursor->name)) {
2491                 case SC_COMMON_NAME:
2492                         r = lxml_get_all_loctext(service, pg, cursor,
2493                             common_name_fmt, (const char *)cursor->name);
2494                         break;
2495                 case SC_DESCRIPTION:
2496                         r = lxml_get_all_loctext(service, pg, cursor,
2497                             description_fmt, (const char *)cursor->name);
2498                         break;
2499                 default:
2500                         uu_die(gettext("\"%s\" is an illegal element in %s "
2501                             "of service %s\n"), (char *)cursor->name,
2502                             (char *)value->name, service->sc_name);
2503                 }
2504                 if (r != 0)
2505                         break;
2506         }
2507 
2508         free(description_fmt);
2509         free(common_name_fmt);
2510         if (r == 0) {
2511                 *name_value = safe_strdup(value_name);
2512         }
2513         xmlFree(value_name);
2514         free(encoded_value);
2515         return (r);
2516 }
2517 
2518 static int
2519 lxml_get_tm_choices(entity_t *service, pgroup_t *pg, xmlNodePtr choices)
2520 {
2521         xmlNodePtr cursor;
2522         char *name_value;
2523         property_t *name_prop = NULL;
2524         int r = 0;
2525 
2526         for (cursor = choices->xmlChildrenNode;
2527             (cursor != NULL) && (r == 0);
2528             cursor = cursor->next) {
2529                 if (lxml_ignorable_block(cursor))
2530                         continue;
2531                 switch (lxml_xlate_element(cursor->name)) {
2532                 case SC_INCLUDE_VALUES:
2533                         (void) lxml_get_tm_include_values(service, pg, cursor,
2534                             SCF_PROPERTY_TM_CHOICES_INCLUDE_VALUES);
2535                         break;
2536                 case SC_RANGE:
2537                         r = lxml_get_tm_range(service, pg, cursor,
2538                             SCF_PROPERTY_TM_CHOICES_RANGE);
2539                         if (r != 0)
2540                                 goto out;
2541                         break;
2542                 case SC_VALUE:
2543                         r = lxml_get_tm_value_element(service, pg, cursor,
2544                             &name_value);
2545                         if (r == 0) {
2546                                 /*
2547                                  * There is no need to free the memory
2548                                  * associated with name_value, because the
2549                                  * property value will end up pointing to
2550                                  * the memory.
2551                                  */
2552                                 astring_prop_value(&name_prop,
2553                                     SCF_PROPERTY_TM_CHOICES_NAME, name_value,
2554                                     B_TRUE);
2555                         } else {
2556                                 goto out;
2557                         }
2558                         break;
2559                 default:
2560                         uu_die(gettext("%s is an invalid element of "
2561                             "choices for service %s.\n"),  cursor->name,
2562                             service->sc_name);
2563                 }
2564         }
2565 
2566 out:
2567         /* Attach the name property if we created one. */
2568         if ((r == 0) && (name_prop != NULL)) {
2569                 r = internal_attach_property(pg, name_prop);
2570         }
2571         if ((r != 0) && (name_prop != NULL)) {
2572                 internal_property_free(name_prop);
2573         }
2574 
2575         return (r);
2576 }
2577 
2578 static int
2579 lxml_get_tm_constraints(entity_t *service, pgroup_t *pg, xmlNodePtr constraints)
2580 {
2581         xmlNodePtr cursor;
2582         char *name_value;
2583         property_t *name_prop = NULL;
2584         int r = 0;
2585 
2586         for (cursor = constraints->xmlChildrenNode;
2587             (cursor != NULL) && (r == 0);
2588             cursor = cursor->next) {
2589                 if (lxml_ignorable_block(cursor))
2590                         continue;
2591                 switch (lxml_xlate_element(cursor->name)) {
2592                 case SC_RANGE:
2593                         r = lxml_get_tm_range(service, pg, cursor,
2594                             SCF_PROPERTY_TM_CONSTRAINT_RANGE);
2595                         if (r != 0)
2596                                 goto out;
2597                         break;
2598                 case SC_VALUE:
2599                         r = lxml_get_tm_value_element(service, pg, cursor,
2600                             &name_value);
2601                         if (r == 0) {
2602                                 /*
2603                                  * There is no need to free the memory
2604                                  * associated with name_value, because the
2605                                  * property value will end up pointing to
2606                                  * the memory.
2607                                  */
2608                                 astring_prop_value(&name_prop,
2609                                     SCF_PROPERTY_TM_CONSTRAINT_NAME, name_value,
2610                                     B_TRUE);
2611                         } else {
2612                                 goto out;
2613                         }
2614                         break;
2615                 default:
2616                         uu_die(gettext("%s is an invalid element of "
2617                             "constraints for service %s.\n"),  cursor->name,
2618                             service->sc_name);
2619                 }
2620         }
2621 
2622 out:
2623         /* Attach the name property if we created one. */
2624         if ((r == 0) && (name_prop != NULL)) {
2625                 r = internal_attach_property(pg, name_prop);
2626         }
2627         if ((r != 0) && (name_prop != NULL)) {
2628                 internal_property_free(name_prop);
2629         }
2630 
2631         return (r);
2632 }
2633 
2634 /*
2635  * The values element contains one or more value elements.
2636  */
2637 static int
2638 lxml_get_tm_values(entity_t *service, pgroup_t *pg, xmlNodePtr values)
2639 {
2640         xmlNodePtr cursor;
2641         char *name_value;
2642         property_t *name_prop = NULL;
2643         int r = 0;
2644 
2645         for (cursor = values->xmlChildrenNode;
2646             (cursor != NULL) && (r == 0);
2647             cursor = cursor->next) {
2648                 if (lxml_ignorable_block(cursor))
2649                         continue;
2650                 if (lxml_xlate_element(cursor->name) != SC_VALUE) {
2651                         uu_die(gettext("\"%s\" is an illegal element in the "
2652                             "%s element of %s\n"), (char *)cursor->name,
2653                             (char *)values->name, service->sc_name);
2654                 }
2655                 r = lxml_get_tm_value_element(service, pg, cursor, &name_value);
2656                 if (r == 0) {
2657                         /*
2658                          * There is no need to free the memory
2659                          * associated with name_value, because the
2660                          * property value will end up pointing to
2661                          * the memory.
2662                          */
2663                         astring_prop_value(&name_prop,
2664                             SCF_PROPERTY_TM_VALUES_NAME, name_value,
2665                             B_TRUE);
2666                 }
2667         }
2668 
2669         /* Attach the name property if we created one. */
2670         if ((r == 0) && (name_prop != NULL)) {
2671                 r = internal_attach_property(pg, name_prop);
2672         }
2673         if ((r != 0) && (name_prop != NULL)) {
2674                 internal_property_free(name_prop);
2675         }
2676 
2677         return (r);
2678 }
2679 
2680 /*
2681  * This function processes a prop_pattern element within a pg_pattern XML
2682  * element.  First it creates a property group to hold the prop_pattern
2683  * information.  The name of this property group is the concatenation of:
2684  *      - SCF_PG_TM_PROP_PATTERN_PREFIX
2685  *      - The unique part of the property group name of the enclosing
2686  *        pg_pattern.  The property group name of the enclosing pg_pattern
2687  *        is passed to us in pgpat_name.  The unique part, is the part
2688  *        following SCF_PG_TM_PG_PATTERN_PREFIX.
2689  *      - The name of this prop_pattern element.
2690  *
2691  * After creating the property group, the prop_pattern attributes are saved
2692  * as properties in the PG.  Finally, the prop_pattern elements are
2693  * processed and added to the PG.
2694  */
2695 static int
2696 lxml_get_tm_prop_pattern(entity_t *service, xmlNodePtr prop_pattern,
2697     const char *pgpat_name)
2698 {
2699         xmlNodePtr cursor;
2700         int extra;
2701         pgroup_t *pg;
2702         property_t *p;
2703         char *pg_name;
2704         size_t prefix_len;
2705         xmlChar *prop_pattern_name;
2706         int r;
2707         const char *unique;
2708         value_t *v;
2709 
2710         /* Find the unique part of the pg_pattern property group name. */
2711         prefix_len = strlen(SCF_PG_TM_PG_PAT_BASE);
2712         assert(strncmp(pgpat_name, SCF_PG_TM_PG_PAT_BASE, prefix_len) == 0);
2713         unique = pgpat_name + prefix_len;
2714 
2715         /*
2716          * We need to get the value of the name attribute first.  The
2717          * prop_pattern name as well as the name of the enclosing
2718          * pg_pattern both constitute part of the name of the property
2719          * group that we will create.
2720          */
2721         prop_pattern_name = xmlGetProp(prop_pattern, (xmlChar *)name_attr);
2722         if ((prop_pattern_name == NULL) || (*prop_pattern_name == 0)) {
2723                 semerr(gettext("prop_pattern name is missing for %s\n"),
2724                     service->sc_name);
2725                 return (-1);
2726         }
2727         if (uu_check_name((const char *)prop_pattern_name,
2728             UU_NAME_DOMAIN) != 0) {
2729                 semerr(gettext("prop_pattern name, \"%s\", for %s is not "
2730                     "valid.\n"), prop_pattern_name, service->sc_name);
2731                 xmlFree(prop_pattern_name);
2732                 return (-1);
2733         }
2734         pg_name = safe_malloc(max_scf_name_len + 1);
2735         if ((extra = snprintf(pg_name, max_scf_name_len + 1, "%s%s_%s",
2736             SCF_PG_TM_PROP_PATTERN_PREFIX, unique,
2737             (char *)prop_pattern_name)) >= max_scf_name_len + 1) {
2738                 uu_die(gettext("prop_pattern name, \"%s\", for %s is %d "
2739                     "characters too long\n"), (char *)prop_pattern_name,
2740                     service->sc_name, extra - max_scf_name_len);
2741         }
2742 
2743         /*
2744          * Create the property group, the property referencing the pg_pattern
2745          * name, and add the prop_pattern attributes to the property group.
2746          */
2747         pg = internal_pgroup_create_strict(service, pg_name,
2748             SCF_GROUP_TEMPLATE_PROP_PATTERN);
2749         if (pg == NULL) {
2750                 uu_die(gettext("Property group for prop_pattern, \"%s\", "
2751                     "already exists in %s\n"), prop_pattern_name,
2752                     service->sc_name);
2753         }
2754 
2755         p = internal_property_create(SCF_PROPERTY_TM_PG_PATTERN,
2756             SCF_TYPE_ASTRING, 1, safe_strdup(pgpat_name));
2757         /*
2758          * Unfortunately, internal_property_create() does not set the free
2759          * function for the value, so we'll set it now.
2760          */
2761         v = uu_list_first(p->sc_property_values);
2762         v->sc_free = lxml_free_str;
2763         if (internal_attach_property(pg, p) != 0)
2764                 internal_property_free(p);
2765 
2766 
2767         r = lxml_get_prop_pattern_attributes(pg, prop_pattern);
2768         if (r != 0)
2769                 goto out;
2770 
2771         /*
2772          * Now process the elements of prop_pattern
2773          */
2774         for (cursor = prop_pattern->xmlChildrenNode;
2775             cursor != NULL;
2776             cursor = cursor->next) {
2777                 if (lxml_ignorable_block(cursor))
2778                         continue;
2779 
2780                 switch (lxml_xlate_element(cursor->name)) {
2781                 case SC_CARDINALITY:
2782                         r = lxml_get_tm_cardinality(service, pg, cursor);
2783                         if (r != 0)
2784                                 goto out;
2785                         break;
2786                 case SC_CHOICES:
2787                         r = lxml_get_tm_choices(service, pg, cursor);
2788                         if (r != 0)
2789                                 goto out;
2790                         break;
2791                 case SC_COMMON_NAME:
2792                         (void) lxml_get_all_loctext(service, pg, cursor,
2793                             COMMON_NAME_FMT, (const char *)cursor->name);
2794                         break;
2795                 case SC_CONSTRAINTS:
2796                         r = lxml_get_tm_constraints(service, pg, cursor);
2797                         if (r != 0)
2798                                 goto out;
2799                         break;
2800                 case SC_DESCRIPTION:
2801                         (void) lxml_get_all_loctext(service, pg, cursor,
2802                             DESCRIPTION_FMT, (const char *)cursor->name);
2803                         break;
2804                 case SC_INTERNAL_SEPARATORS:
2805                         r = lxml_get_tm_internal_seps(service, pg, cursor);
2806                         if (r != 0)
2807                                 goto out;
2808                         break;
2809                 case SC_UNITS:
2810                         (void) lxml_get_all_loctext(service, pg, cursor,
2811                             UNITS_FMT, "units");
2812                         break;
2813                 case SC_VALUES:
2814                         (void) lxml_get_tm_values(service, pg, cursor);
2815                         break;
2816                 case SC_VISIBILITY:
2817                         /*
2818                          * The visibility element is empty, so we only need
2819                          * to proccess the value attribute.
2820                          */
2821                         (void) new_str_prop_from_attr(pg,
2822                             SCF_PROPERTY_TM_VISIBILITY, SCF_TYPE_ASTRING,
2823                             cursor, value_attr);
2824                         break;
2825                 default:
2826                         uu_die(gettext("illegal element \"%s\" in prop_pattern "
2827                             "for service \"%s\"\n"), cursor->name,
2828                             service->sc_name);
2829                 }
2830         }
2831 
2832 out:
2833         xmlFree(prop_pattern_name);
2834         free(pg_name);
2835         return (r);
2836 }
2837 
2838 /*
2839  * Get the pg_pattern attributes and save them as properties in the
2840  * property group at pg.  The pg_pattern element accepts four attributes --
2841  * name, type, required and target.
2842  */
2843 static int
2844 lxml_get_pg_pattern_attributes(pgroup_t *pg, xmlNodePtr cursor)
2845 {
2846         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_TM_NAME,
2847             SCF_TYPE_ASTRING, cursor, name_attr, NULL) != 0) {
2848                 return (-1);
2849         }
2850         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_TM_TYPE,
2851             SCF_TYPE_ASTRING, cursor, type_attr, NULL) != 0) {
2852                 return (-1);
2853         }
2854         if (new_opt_str_prop_from_attr(pg, SCF_PROPERTY_TM_TARGET,
2855             SCF_TYPE_ASTRING, cursor, target_attr, NULL) != 0) {
2856                 return (-1);
2857         }
2858         if (new_bool_prop_from_attr(pg, SCF_PROPERTY_TM_REQUIRED, cursor,
2859             required_attr) != 0)
2860                 return (-1);
2861         return (0);
2862 }
2863 
2864 /*
2865  * There are several restrictions on the pg_pattern attributes that cannot
2866  * be specifed in the service bundle DTD.  This function verifies that
2867  * those restrictions have been satisfied.  The restrictions are:
2868  *
2869  *      - The target attribute may have a value of "instance" only when the
2870  *        template block is in a service declaration.
2871  *
2872  *      - The target attribute may have a value of "delegate" only when the
2873  *        template block applies to a restarter.
2874  *
2875  *      - The target attribute may have a value of "all" only when the
2876  *        template block applies to the master restarter.
2877  *
2878  * The function returns 0 on success and -1 on failure.
2879  */
2880 static int
2881 verify_pg_pattern_attributes(entity_t *s, pgroup_t *pg)
2882 {
2883         int is_restarter;
2884         property_t *target;
2885         value_t *v;
2886 
2887         /* Find the value of the target property. */
2888         target = internal_property_find(pg, SCF_PROPERTY_TM_TARGET);
2889         if (target == NULL) {
2890                 uu_die(gettext("pg_pattern is missing the %s attribute "
2891                     "in %s\n"), target_attr, s->sc_name);
2892                 return (-1);
2893         }
2894         v = uu_list_first(target->sc_property_values);
2895         assert(v != NULL);
2896         assert(v->sc_type == SCF_TYPE_ASTRING);
2897 
2898         /*
2899          * If target has a value of instance, the template must be in a
2900          * service object.
2901          */
2902         if (strcmp(v->sc_u.sc_string, "instance") == 0) {
2903                 if (s->sc_etype != SVCCFG_SERVICE_OBJECT) {
2904                         uu_warn(gettext("pg_pattern %s attribute may only "
2905                             "have a value of \"instance\" when it is in a "
2906                             "service declaration.\n"), target_attr);
2907                         return (-1);
2908                 }
2909         }
2910 
2911         /*
2912          * If target has a value of "delegate", the template must be in a
2913          * restarter.
2914          */
2915         if (strcmp(v->sc_u.sc_string, "delegate") == 0) {
2916                 is_restarter = 0;
2917                 if ((s->sc_etype == SVCCFG_SERVICE_OBJECT) &&
2918                     (s->sc_u.sc_service.sc_service_type == SVCCFG_RESTARTER)) {
2919                         is_restarter = 1;
2920                 }
2921                 if ((s->sc_etype == SVCCFG_INSTANCE_OBJECT) &&
2922                     (s->sc_parent->sc_u.sc_service.sc_service_type ==
2923                     SVCCFG_RESTARTER)) {
2924                         is_restarter = 1;
2925                 }
2926                 if (is_restarter == 0) {
2927                         uu_warn(gettext("pg_pattern %s attribute has a "
2928                             "value of \"delegate\" but is not in a "
2929                             "restarter service\n"), target_attr);
2930                         return (-1);
2931                 }
2932         }
2933 
2934         /*
2935          * If target has a value of "all", the template must be in the
2936          * global (SCF_SERVICE_GLOBAL) service.
2937          */
2938         if (strcmp(v->sc_u.sc_string, all_value) == 0) {
2939                 if (s->sc_etype != SVCCFG_SERVICE_OBJECT) {
2940                         uu_warn(gettext("pg_pattern %s attribute has a "
2941                             "value of \"%s\" but is not in a "
2942                             "service entity.\n"), target_attr, all_value);
2943                         return (-1);
2944                 }
2945                 if (strcmp(s->sc_fmri, SCF_SERVICE_GLOBAL) != 0) {
2946                         uu_warn(gettext("pg_pattern %s attribute has a "
2947                             "value of \"%s\" but is in the \"%s\" service.  "
2948                             "pg_patterns with target \"%s\" are only allowed "
2949                             "in the global service.\n"),
2950                             target_attr, all_value, s->sc_fmri, all_value);
2951                         return (-1);
2952                 }
2953         }
2954 
2955         return (0);
2956 }
2957 
2958 static int
2959 lxml_get_tm_pg_pattern(entity_t *service, xmlNodePtr pg_pattern)
2960 {
2961         xmlNodePtr cursor;
2962         int out_len;
2963         xmlChar *name;
2964         pgroup_t *pg = NULL;
2965         char *pg_name;
2966         int r = -1;
2967         xmlChar *type;
2968 
2969         pg_name = safe_malloc(max_scf_name_len + 1);
2970 
2971         /*
2972          * Get the name and type attributes.  Their presence or absence
2973          * determines whcih prefix we will use for the property group name.
2974          * There are four cases -- neither attribute is present, both are
2975          * present, only name is present or only type is present.
2976          */
2977         name = xmlGetProp(pg_pattern, (xmlChar *)name_attr);
2978         type = xmlGetProp(pg_pattern, (xmlChar *)type_attr);
2979         if ((name == NULL) || (*name == 0)) {
2980                 if ((type == NULL) || (*type == 0)) {
2981                         /* PG name contains only the prefix in this case */
2982                         if (strlcpy(pg_name, SCF_PG_TM_PG_PATTERN_PREFIX,
2983                             max_scf_name_len + 1) >= max_scf_name_len + 1) {
2984                                 uu_die(gettext("Unable to create pg_pattern "
2985                                     "property for %s\n"), service->sc_name);
2986                         }
2987                 } else {
2988                         /*
2989                          * If we have a type and no name, the type becomes
2990                          * part of the pg_pattern property group name.
2991                          */
2992                         if ((out_len = snprintf(pg_name, max_scf_name_len + 1,
2993                             "%s%s", SCF_PG_TM_PG_PATTERN_T_PREFIX, type)) >=
2994                             max_scf_name_len + 1) {
2995                                 uu_die(gettext("pg_pattern type is for %s is "
2996                                     "%d bytes too long\n"), service->sc_name,
2997                                     out_len - max_scf_name_len);
2998                         }
2999                 }
3000         } else {
3001                 const char *prefix;
3002 
3003                 /* Make sure that the name is valid. */
3004                 if (uu_check_name((const char *)name, UU_NAME_DOMAIN) != 0) {
3005                         semerr(gettext("pg_pattern name attribute, \"%s\", "
3006                             "for %s is invalid\n"), name, service->sc_name);
3007                         goto out;
3008                 }
3009 
3010                 /*
3011                  * As long as the pg_pattern has a name, it becomes part of
3012                  * the name of the pg_pattern property group name.  We
3013                  * merely need to pick the appropriate prefix.
3014                  */
3015                 if ((type == NULL) || (*type == 0)) {
3016                         prefix = SCF_PG_TM_PG_PATTERN_N_PREFIX;
3017                 } else {
3018                         prefix = SCF_PG_TM_PG_PATTERN_NT_PREFIX;
3019                 }
3020                 if ((out_len = snprintf(pg_name, max_scf_name_len + 1, "%s%s",
3021                     prefix, name)) >= max_scf_name_len + 1) {
3022                         uu_die(gettext("pg_pattern property group name "
3023                             "for %s is %d bytes too long\n"), service->sc_name,
3024                             out_len - max_scf_name_len);
3025                 }
3026         }
3027 
3028         /*
3029          * Create the property group for holding this pg_pattern
3030          * information, and capture the pg_pattern attributes.
3031          */
3032         pg = internal_pgroup_create_strict(service, pg_name,
3033             SCF_GROUP_TEMPLATE_PG_PATTERN);
3034         if (pg == NULL) {
3035                 if ((name == NULL) || (*name == 0)) {
3036                         if ((type == NULL) ||(*type == 0)) {
3037                                 semerr(gettext("pg_pattern with empty name and "
3038                                     "type is not unique in %s\n"),
3039                                     service->sc_name);
3040                         } else {
3041                                 semerr(gettext("pg_pattern with empty name and "
3042                                     "type \"%s\" is not unique in %s\n"),
3043                                     type, service->sc_name);
3044                         }
3045                 } else {
3046                         if ((type == NULL) || (*type == 0)) {
3047                                 semerr(gettext("pg_pattern with name \"%s\" "
3048                                     "and empty type is not unique in %s\n"),
3049                                     name, service->sc_name);
3050                         } else {
3051                                 semerr(gettext("pg_pattern with name \"%s\" "
3052                                     "and type \"%s\" is not unique in %s\n"),
3053                                     name, type, service->sc_name);
3054                         }
3055                 }
3056                 goto out;
3057         }
3058 
3059         /*
3060          * Get the pg_pattern attributes from the manifest and verify
3061          * that they satisfy our restrictions.
3062          */
3063         r = lxml_get_pg_pattern_attributes(pg, pg_pattern);
3064         if (r != 0)
3065                 goto out;
3066         if (verify_pg_pattern_attributes(service, pg) != 0) {
3067                 semerr(gettext("Invalid pg_pattern attributes in %s\n"),
3068                     service->sc_name);
3069                 r = -1;
3070                 goto out;
3071         }
3072 
3073         /*
3074          * Now process all of the elements of pg_pattern.
3075          */
3076         for (cursor = pg_pattern->xmlChildrenNode;
3077             cursor != NULL;
3078             cursor = cursor->next) {
3079                 if (lxml_ignorable_block(cursor))
3080                         continue;
3081 
3082                 switch (lxml_xlate_element(cursor->name)) {
3083                 case SC_COMMON_NAME:
3084                         (void) lxml_get_all_loctext(service, pg, cursor,
3085                             COMMON_NAME_FMT, (const char *)cursor->name);
3086                         break;
3087                 case SC_DESCRIPTION:
3088                         (void) lxml_get_all_loctext(service, pg, cursor,
3089                             DESCRIPTION_FMT, (const char *)cursor->name);
3090                         break;
3091                 case SC_PROP_PATTERN:
3092                         r = lxml_get_tm_prop_pattern(service, cursor,
3093                             pg_name);
3094                         if (r != 0)
3095                                 goto out;
3096                         break;
3097                 default:
3098                         uu_die(gettext("illegal element \"%s\" in pg_pattern "
3099                             "for service \"%s\"\n"), cursor->name,
3100                             service->sc_name);
3101                 }
3102         }
3103 
3104 out:
3105         if ((r != 0) && (pg != NULL)) {
3106                 internal_detach_pgroup(service, pg);
3107                 internal_pgroup_free(pg);
3108         }
3109         free(pg_name);
3110         xmlFree(name);
3111         xmlFree(type);
3112 
3113         return (r);
3114 }
3115 
3116 static int
3117 lxml_get_template(entity_t *service, xmlNodePtr templ)
3118 {
3119         xmlNodePtr cursor;
3120 
3121         for (cursor = templ->xmlChildrenNode; cursor != NULL;
3122             cursor = cursor->next) {
3123                 if (lxml_ignorable_block(cursor))
3124                         continue;
3125 
3126                 switch (lxml_xlate_element(cursor->name)) {
3127                 case SC_COMMON_NAME:
3128                         (void) lxml_get_tm_common_name(service, cursor);
3129                         break;
3130                 case SC_DESCRIPTION:
3131                         (void) lxml_get_tm_description(service, cursor);
3132                         break;
3133                 case SC_DOCUMENTATION:
3134                         (void) lxml_get_tm_documentation(service, cursor);
3135                         break;
3136                 case SC_PG_PATTERN:
3137                         if (lxml_get_tm_pg_pattern(service, cursor) != 0)
3138                                 return (-1);
3139                         break;
3140                 default:
3141                         uu_die(gettext("illegal element \"%s\" on template "
3142                             "for service \"%s\"\n"),
3143                             cursor->name, service->sc_name);
3144                 }
3145         }
3146 
3147         return (0);
3148 }
3149 
3150 static int
3151 lxml_get_default_instance(entity_t *service, xmlNodePtr definst)
3152 {
3153         entity_t *i;
3154         xmlChar *enabled;
3155         pgroup_t *pg;
3156         property_t *p;
3157         char *package;
3158         uint64_t enabled_val = 0;
3159 
3160         i = internal_instance_new("default");
3161 
3162         if ((enabled = xmlGetProp(definst, (xmlChar *)enabled_attr)) != NULL) {
3163                 enabled_val = (strcmp(true, (const char *)enabled) == 0) ?
3164                     1 : 0;
3165                 xmlFree(enabled);
3166         }
3167 
3168         /*
3169          * New general property group with enabled boolean property set.
3170          */
3171 
3172         i->sc_op = service->sc_op;
3173         pg = internal_pgroup_new();
3174         (void) internal_attach_pgroup(i, pg);
3175 
3176         pg->sc_pgroup_name = (char *)scf_pg_general;
3177         pg->sc_pgroup_type = (char *)scf_group_framework;
3178         pg->sc_pgroup_flags = 0;
3179 
3180         p = internal_property_create(SCF_PROPERTY_ENABLED, SCF_TYPE_BOOLEAN, 1,
3181             enabled_val);
3182 
3183         (void) internal_attach_property(pg, p);
3184 
3185         /*
3186          * Add general/package property if PKGINST is set.
3187          */
3188         if ((package = getenv("PKGINST")) != NULL) {
3189                 p = internal_property_create(SCF_PROPERTY_PACKAGE,
3190                     SCF_TYPE_ASTRING, 1, package);
3191 
3192                 (void) internal_attach_property(pg, p);
3193         }
3194 
3195         return (internal_attach_entity(service, i));
3196 }
3197 
3198 /*
3199  * Translate an instance element into an internal property tree, added to
3200  * service.  If op is SVCCFG_OP_APPLY (i.e., apply a profile), set the
3201  * enabled property to override.
3202  *
3203  * If op is SVCCFG_OP_APPLY (i.e., apply a profile), do not allow for
3204  * modification of template data.
3205  */
3206 static int
3207 lxml_get_instance(entity_t *service, xmlNodePtr inst, bundle_type_t bt,
3208     svccfg_op_t op)
3209 {
3210         entity_t *i;
3211         pgroup_t *pg;
3212         property_t *p;
3213         xmlNodePtr cursor;
3214         xmlChar *enabled;
3215         int r, e_val;
3216 
3217         /*
3218          * Fetch its attributes, as appropriate.
3219          */
3220         i = internal_instance_new((char *)xmlGetProp(inst,
3221             (xmlChar *)name_attr));
3222 
3223         /*
3224          * Note that this must be done before walking the children so that
3225          * sc_fmri is set in case we enter lxml_get_dependent().
3226          */
3227         r = internal_attach_entity(service, i);
3228         if (r != 0)
3229                 return (r);
3230 
3231         i->sc_op = op;
3232         enabled = xmlGetProp(inst, (xmlChar *)enabled_attr);
3233 
3234         if (enabled == NULL) {
3235                 if (bt == SVCCFG_MANIFEST) {
3236                         semerr(gettext("Instance \"%s\" missing attribute "
3237                             "\"%s\".\n"), i->sc_name, enabled_attr);
3238                         return (-1);
3239                 }
3240         } else {        /* enabled != NULL */
3241                 if (strcmp(true, (const char *)enabled) != 0 &&
3242                     strcmp(false, (const char *)enabled) != 0) {
3243                         xmlFree(enabled);
3244                         semerr(gettext("Invalid enabled value\n"));
3245                         return (-1);
3246                 }
3247                 pg = internal_pgroup_new();
3248                 (void) internal_attach_pgroup(i, pg);
3249 
3250                 pg->sc_pgroup_name = (char *)scf_pg_general;
3251                 pg->sc_pgroup_type = (char *)scf_group_framework;
3252                 pg->sc_pgroup_flags = 0;
3253 
3254                 e_val = (strcmp(true, (const char *)enabled) == 0);
3255                 p = internal_property_create(SCF_PROPERTY_ENABLED,
3256                     SCF_TYPE_BOOLEAN, 1, (uint64_t)e_val);
3257 
3258                 p->sc_property_override = (op == SVCCFG_OP_APPLY);
3259 
3260                 (void) internal_attach_property(pg, p);
3261 
3262                 xmlFree(enabled);
3263         }
3264 
3265         /*
3266          * Walk its child elements, as appropriate.
3267          */
3268         for (cursor = inst->xmlChildrenNode; cursor != NULL;
3269             cursor = cursor->next) {
3270                 if (lxml_ignorable_block(cursor))
3271                         continue;
3272 
3273                 switch (lxml_xlate_element(cursor->name)) {
3274                 case SC_RESTARTER:
3275                         (void) lxml_get_restarter(i, cursor);
3276                         break;
3277                 case SC_DEPENDENCY:
3278                         (void) lxml_get_dependency(i, cursor);
3279                         break;
3280                 case SC_DEPENDENT:
3281                         (void) lxml_get_dependent(i, cursor);
3282                         break;
3283                 case SC_METHOD_CONTEXT:
3284                         (void) lxml_get_entity_method_context(i, cursor);
3285                         break;
3286                 case SC_EXEC_METHOD:
3287                         (void) lxml_get_exec_method(i, cursor);
3288                         break;
3289                 case SC_PROPERTY_GROUP:
3290                         (void) lxml_get_pgroup(i, cursor);
3291                         break;
3292                 case SC_TEMPLATE:
3293                         if (op == SVCCFG_OP_APPLY) {
3294                                 semerr(gettext("Template data for \"%s\" may "
3295                                     "not be modified in a profile.\n"),
3296                                     i->sc_name);
3297 
3298                                 return (-1);
3299                         }
3300 
3301                         if (lxml_get_template(i, cursor) != 0)
3302                                 return (-1);
3303                         break;
3304                 case SC_NOTIFICATION_PARAMETERS:
3305                         if (lxml_get_notification_parameters(i, cursor) != 0)
3306                                 return (-1);
3307                         break;
3308                 default:
3309                         uu_die(gettext(
3310                             "illegal element \"%s\" on instance \"%s\"\n"),
3311                             cursor->name, i->sc_name);
3312                         break;
3313                 }
3314         }
3315 
3316         return (0);
3317 }
3318 
3319 /* ARGSUSED1 */
3320 static int
3321 lxml_get_single_instance(entity_t *entity, xmlNodePtr si)
3322 {
3323         pgroup_t *pg;
3324         property_t *p;
3325         int r;
3326 
3327         pg = internal_pgroup_find_or_create(entity, (char *)scf_pg_general,
3328             (char *)scf_group_framework);
3329 
3330         p = internal_property_create(SCF_PROPERTY_SINGLE_INSTANCE,
3331             SCF_TYPE_BOOLEAN, 1, (uint64_t)1);
3332 
3333         r = internal_attach_property(pg, p);
3334         if (r != 0) {
3335                 internal_property_free(p);
3336                 return (-1);
3337         }
3338 
3339         return (0);
3340 }
3341 
3342 /*
3343  * Check to see if the service should allow the upgrade
3344  * process to handle adding of the manifestfiles linkage.
3345  *
3346  * If the service exists and does not have a manifestfiles
3347  * property group then the upgrade process should handle
3348  * the service.
3349  *
3350  * If the service doesn't exist or the service exists
3351  * and has a manifestfiles property group then the import
3352  * process can handle the manifestfiles property group
3353  * work.
3354  *
3355  * This prevents potential cleanup of unaccounted for instances
3356  * in early manifest import due to upgrade process needing
3357  * information that has not yet been supplied by manifests
3358  * that are still located in the /var/svc manifests directory.
3359  */
3360 static int
3361 lxml_check_upgrade(const char *service)
3362 {
3363         scf_handle_t    *h = NULL;
3364         scf_scope_t     *sc = NULL;
3365         scf_service_t   *svc = NULL;
3366         scf_propertygroup_t     *pg = NULL;
3367         int rc = SCF_FAILED;
3368 
3369         if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
3370             (sc = scf_scope_create(h)) == NULL ||
3371             (svc = scf_service_create(h)) == NULL ||
3372             (pg = scf_pg_create(h)) == NULL)
3373                 goto out;
3374 
3375         if (scf_handle_bind(h) != 0)
3376                 goto out;
3377 
3378         if (scf_handle_get_scope(h, SCF_FMRI_LOCAL_SCOPE, sc) == -1)
3379                 goto out;
3380 
3381         if (scf_scope_get_service(sc, service, svc) != SCF_SUCCESS) {
3382                 if (scf_error() == SCF_ERROR_NOT_FOUND)
3383                         rc = SCF_SUCCESS;
3384 
3385                 goto out;
3386         }
3387 
3388         if (scf_service_get_pg(svc, SCF_PG_MANIFESTFILES, pg) != SCF_SUCCESS)
3389                 goto out;
3390 
3391         rc = SCF_SUCCESS;
3392 out:
3393         scf_pg_destroy(pg);
3394         scf_service_destroy(svc);
3395         scf_scope_destroy(sc);
3396         scf_handle_destroy(h);
3397 
3398         return (rc);
3399 }
3400 
3401 /*
3402  * Translate a service element into an internal instance/property tree, added
3403  * to bundle.
3404  *
3405  * If op is SVCCFG_OP_APPLY (i.e., apply a profile), do not allow for
3406  * modification of template data.
3407  */
3408 static int
3409 lxml_get_service(bundle_t *bundle, xmlNodePtr svc, svccfg_op_t op)
3410 {
3411         pgroup_t *pg;
3412         property_t *p;
3413         entity_t *s;
3414         xmlNodePtr cursor;
3415         xmlChar *type;
3416         xmlChar *version;
3417         int e;
3418 
3419         /*
3420          * Fetch attributes, as appropriate.
3421          */
3422         s = internal_service_new((char *)xmlGetProp(svc,
3423             (xmlChar *)name_attr));
3424 
3425         version = xmlGetProp(svc, (xmlChar *)version_attr);
3426         s->sc_u.sc_service.sc_service_version = atol((const char *)version);
3427         xmlFree(version);
3428 
3429         type = xmlGetProp(svc, (xmlChar *)type_attr);
3430         s->sc_u.sc_service.sc_service_type = lxml_xlate_service_type(type);
3431         xmlFree(type);
3432 
3433         /*
3434          * Set the global missing type to false before processing the service
3435          */
3436         est->sc_miss_type = B_FALSE;
3437         s->sc_op = op;
3438 
3439         /*
3440          * Now that the service is created create the manifest
3441          * property group and add the property value of the service.
3442          */
3443         if (lxml_check_upgrade(s->sc_name) == SCF_SUCCESS &&
3444             svc->doc->name != NULL &&
3445             bundle->sc_bundle_type == SVCCFG_MANIFEST) {
3446                 char *buf, *base, *fname, *bname;
3447                 size_t  base_sz = 0;
3448 
3449                 /*
3450                  * Must remove the PKG_INSTALL_ROOT, point to the correct
3451                  * directory after install
3452                  */
3453                 bname = uu_zalloc(PATH_MAX + 1);
3454                 if (realpath(svc->doc->name, bname) == NULL) {
3455                         uu_die(gettext("Unable to create the real path of the "
3456                             "manifest file \"%s\" : %d\n"), svc->doc->name,
3457                             errno);
3458                 }
3459 
3460                 base = getenv("PKG_INSTALL_ROOT");
3461                 if (base != NULL && strncmp(bname, base, strlen(base)) == 0) {
3462                         base_sz = strlen(base);
3463                 }
3464                 fname = safe_strdup(bname + base_sz);
3465 
3466                 uu_free(bname);
3467                 buf = mhash_filename_to_propname(svc->doc->name, B_FALSE);
3468 
3469                 pg = internal_pgroup_create_strict(s, SCF_PG_MANIFESTFILES,
3470                     SCF_GROUP_FRAMEWORK);
3471 
3472                 if (pg == NULL) {
3473                         uu_die(gettext("Property group for prop_pattern, "
3474                             "\"%s\", already exists in %s\n"),
3475                             SCF_PG_MANIFESTFILES, s->sc_name);
3476                 }
3477 
3478                 p = internal_property_create(buf, SCF_TYPE_ASTRING, 1, fname);
3479 
3480                 (void) internal_attach_property(pg, p);
3481         }
3482 
3483         /*
3484          * Walk its child elements, as appropriate.
3485          */
3486         for (cursor = svc->xmlChildrenNode; cursor != NULL;
3487             cursor = cursor->next) {
3488                 if (lxml_ignorable_block(cursor))
3489                         continue;
3490 
3491                 e = lxml_xlate_element(cursor->name);
3492 
3493                 switch (e) {
3494                 case SC_INSTANCE:
3495                         if (lxml_get_instance(s, cursor,
3496                             bundle->sc_bundle_type, op) != 0)
3497                                 return (-1);
3498                         break;
3499                 case SC_TEMPLATE:
3500                         if (op == SVCCFG_OP_APPLY) {
3501                                 semerr(gettext("Template data for \"%s\" may "
3502                                     "not be modified in a profile.\n"),
3503                                     s->sc_name);
3504 
3505                                 return (-1);
3506                         }
3507 
3508                         if (lxml_get_template(s, cursor) != 0)
3509                                 return (-1);
3510                         break;
3511                 case SC_NOTIFICATION_PARAMETERS:
3512                         if (lxml_get_notification_parameters(s, cursor) != 0)
3513                                 return (-1);
3514                         break;
3515                 case SC_STABILITY:
3516                         (void) lxml_get_entity_stability(s, cursor);
3517                         break;
3518                 case SC_DEPENDENCY:
3519                         (void) lxml_get_dependency(s, cursor);
3520                         break;
3521                 case SC_DEPENDENT:
3522                         (void) lxml_get_dependent(s, cursor);
3523                         break;
3524                 case SC_RESTARTER:
3525                         (void) lxml_get_restarter(s, cursor);
3526                         break;
3527                 case SC_EXEC_METHOD:
3528                         (void) lxml_get_exec_method(s, cursor);
3529                         break;
3530                 case SC_METHOD_CONTEXT:
3531                         (void) lxml_get_entity_method_context(s, cursor);
3532                         break;
3533                 case SC_PROPERTY_GROUP:
3534                         (void) lxml_get_pgroup(s, cursor);
3535                         break;
3536                 case SC_INSTANCE_CREATE_DEFAULT:
3537                         (void) lxml_get_default_instance(s, cursor);
3538                         break;
3539                 case SC_INSTANCE_SINGLE:
3540                         (void) lxml_get_single_instance(s, cursor);
3541                         break;
3542                 default:
3543                         uu_die(gettext(
3544                             "illegal element \"%s\" on service \"%s\"\n"),
3545                             cursor->name, s->sc_name);
3546                         break;
3547                 }
3548         }
3549 
3550         /*
3551          * Now that the service has been processed set the missing type
3552          * for the service.  So that only the services with missing
3553          * types are processed.
3554          */
3555         s->sc_miss_type = est->sc_miss_type;
3556         if (est->sc_miss_type)
3557                 est->sc_miss_type = B_FALSE;
3558 
3559         return (internal_attach_service(bundle, s));
3560 }
3561 
3562 #ifdef DEBUG
3563 void
3564 lxml_dump(int g, xmlNodePtr p)
3565 {
3566         if (p && p->name) {
3567                 (void) printf("%d %s\n", g, p->name);
3568 
3569                 for (p = p->xmlChildrenNode; p != NULL; p = p->next)
3570                         lxml_dump(g + 1, p);
3571         }
3572 }
3573 #endif /* DEBUG */
3574 
3575 static int
3576 lxml_is_known_dtd(const xmlChar *dtdname)
3577 {
3578         if (dtdname == NULL ||
3579             strcmp(MANIFEST_DTD_PATH, (const char *)dtdname) != 0)
3580                 return (0);
3581 
3582         return (1);
3583 }
3584 
3585 static int
3586 lxml_get_bundle(bundle_t *bundle, bundle_type_t bundle_type,
3587     xmlNodePtr subbundle, svccfg_op_t op)
3588 {
3589         xmlNodePtr cursor;
3590         xmlChar *type;
3591         int e;
3592 
3593         /*
3594          * 1.  Get bundle attributes.
3595          */
3596         type = xmlGetProp(subbundle, (xmlChar *)type_attr);
3597         bundle->sc_bundle_type = lxml_xlate_bundle_type(type);
3598         if (bundle->sc_bundle_type != bundle_type &&
3599             bundle_type != SVCCFG_UNKNOWN_BUNDLE) {
3600                 semerr(gettext("included bundle of different type.\n"));
3601                 return (-1);
3602         }
3603 
3604         xmlFree(type);
3605 
3606         switch (op) {
3607         case SVCCFG_OP_IMPORT:
3608                 if (bundle->sc_bundle_type != SVCCFG_MANIFEST) {
3609                         semerr(gettext("document is not a manifest.\n"));
3610                         return (-1);
3611                 }
3612                 break;
3613         case SVCCFG_OP_APPLY:
3614                 if (bundle->sc_bundle_type != SVCCFG_PROFILE) {
3615                         semerr(gettext("document is not a profile.\n"));
3616                         return (-1);
3617                 }
3618                 break;
3619         case SVCCFG_OP_RESTORE:
3620                 if (bundle->sc_bundle_type != SVCCFG_ARCHIVE) {
3621                         semerr(gettext("document is not an archive.\n"));
3622                         return (-1);
3623                 }
3624                 break;
3625         }
3626 
3627         if (((bundle->sc_bundle_name = xmlGetProp(subbundle,
3628             (xmlChar *)name_attr)) == NULL) || (*bundle->sc_bundle_name == 0)) {
3629                 semerr(gettext("service bundle lacks name attribute\n"));
3630                 return (-1);
3631         }
3632 
3633         /*
3634          * 2.  Get services, descend into each one and build state.
3635          */
3636         for (cursor = subbundle->xmlChildrenNode; cursor != NULL;
3637             cursor = cursor->next) {
3638                 if (lxml_ignorable_block(cursor))
3639                         continue;
3640 
3641                 e = lxml_xlate_element(cursor->name);
3642 
3643                 switch (e) {
3644                 case SC_XI_INCLUDE:
3645                         continue;
3646 
3647                 case SC_SERVICE_BUNDLE:
3648                         if (lxml_get_bundle(bundle, bundle_type, cursor, op))
3649                                 return (-1);
3650                         break;
3651                 case SC_SERVICE:
3652                         if (lxml_get_service(bundle, cursor, op) != 0)
3653                                 return (-1);
3654                         break;
3655                 }
3656         }
3657 
3658         return (0);
3659 }
3660 
3661 /*
3662  * Load an XML tree from filename and translate it into an internal service
3663  * tree bundle.  Require that the bundle be of appropriate type for the
3664  * operation: archive for RESTORE, manifest for IMPORT, profile for APPLY.
3665  */
3666 int
3667 lxml_get_bundle_file(bundle_t *bundle, const char *filename, svccfg_op_t op)
3668 {
3669         xmlDocPtr document;
3670         xmlNodePtr cursor;
3671         xmlDtdPtr dtd = NULL;
3672         xmlValidCtxtPtr vcp;
3673         boolean_t do_validate;
3674         char *dtdpath = NULL;
3675         int r;
3676 
3677         /*
3678          * Verify we can read the file before we try to parse it.
3679          */
3680         if (access(filename, R_OK | F_OK) == -1) {
3681                 semerr(gettext("unable to open file: %s\n"), strerror(errno));
3682                 return (-1);
3683         }
3684 
3685         /*
3686          * Until libxml2 addresses DTD-based validation with XInclude, we don't
3687          * validate service profiles (i.e. the apply path).
3688          */
3689         do_validate = (op != SVCCFG_OP_APPLY) &&
3690             (getenv("SVCCFG_NOVALIDATE") == NULL);
3691         if (do_validate)
3692                 dtdpath = getenv("SVCCFG_DTD");
3693 
3694         if (dtdpath != NULL)
3695                 xmlLoadExtDtdDefaultValue = 0;
3696 
3697         if ((document = xmlReadFile(filename, NULL, 0)) == NULL) {
3698                 semerr(gettext("couldn't parse document\n"));
3699                 return (-1);
3700         }
3701 
3702         document->name = safe_strdup(filename);
3703 
3704         /*
3705          * Verify that this is a document type we understand.
3706          */
3707         if ((dtd = xmlGetIntSubset(document)) == NULL) {
3708                 semerr(gettext("document has no DTD\n"));
3709                 return (-1);
3710         } else if (dtdpath == NULL && !do_validate) {
3711                 /*
3712                  * If apply then setup so that some validation
3713                  * for specific elements can be done.
3714                  */
3715                 dtdpath = (char *)document->intSubset->SystemID;
3716         }
3717 
3718         if (!lxml_is_known_dtd(dtd->SystemID)) {
3719                 semerr(gettext("document DTD unknown; not service bundle?\n"));
3720                 return (-1);
3721         }
3722 
3723         if ((cursor = xmlDocGetRootElement(document)) == NULL) {
3724                 semerr(gettext("document is empty\n"));
3725                 xmlFreeDoc(document);
3726                 return (-1);
3727         }
3728 
3729         if (xmlStrcmp(cursor->name, (const xmlChar *)"service_bundle") != 0) {
3730                 semerr(gettext("document is not a service bundle\n"));
3731                 xmlFreeDoc(document);
3732                 return (-1);
3733         }
3734 
3735 
3736         if (dtdpath != NULL) {
3737                 dtd = xmlParseDTD(NULL, (xmlChar *)dtdpath);
3738                 if (dtd == NULL) {
3739                         semerr(gettext("Could not parse DTD \"%s\".\n"),
3740                             dtdpath);
3741                         return (-1);
3742                 }
3743 
3744                 if (document->extSubset != NULL)
3745                         xmlFreeDtd(document->extSubset);
3746 
3747                 document->extSubset = dtd;
3748         }
3749 
3750         if (xmlXIncludeProcessFlags(document, XML_PARSE_XINCLUDE) == -1) {
3751                 semerr(gettext("couldn't handle XInclude statements "
3752                     "in document\n"));
3753                 return (-1);
3754         }
3755 
3756         if (do_validate) {
3757                 vcp = xmlNewValidCtxt();
3758                 if (vcp == NULL)
3759                         uu_die(gettext("could not allocate memory"));
3760                 vcp->warning = xmlParserValidityWarning;
3761                 vcp->error = xmlParserValidityError;
3762 
3763                 r = xmlValidateDocument(vcp, document);
3764 
3765                 xmlFreeValidCtxt(vcp);
3766 
3767                 if (r == 0) {
3768                         semerr(gettext("Document is not valid.\n"));
3769                         xmlFreeDoc(document);
3770                         return (-1);
3771                 }
3772         }
3773 
3774 #ifdef DEBUG
3775         lxml_dump(0, cursor);
3776 #endif /* DEBUG */
3777 
3778         r = lxml_get_bundle(bundle, SVCCFG_UNKNOWN_BUNDLE, cursor, op);
3779 
3780         xmlFreeDoc(document);
3781 
3782         return (r);
3783 }
3784 
3785 int
3786 lxml_inventory(const char *filename)
3787 {
3788         bundle_t *b;
3789         uu_list_walk_t *svcs, *insts;
3790         entity_t *svc, *inst;
3791 
3792         b = internal_bundle_new();
3793 
3794         if (lxml_get_bundle_file(b, filename, SVCCFG_OP_IMPORT) != 0) {
3795                 internal_bundle_free(b);
3796                 return (-1);
3797         }
3798 
3799         svcs = uu_list_walk_start(b->sc_bundle_services, 0);
3800         if (svcs == NULL)
3801                 uu_die(gettext("Couldn't walk services"));
3802 
3803         while ((svc = uu_list_walk_next(svcs)) != NULL) {
3804                 uu_list_t *inst_list;
3805 
3806                 inst_list = svc->sc_u.sc_service.sc_service_instances;
3807                 insts = uu_list_walk_start(inst_list, 0);
3808                 if (insts == NULL)
3809                         uu_die(gettext("Couldn't walk instances"));
3810 
3811                 while ((inst = uu_list_walk_next(insts)) != NULL)
3812                         (void) printf("svc:/%s:%s\n", svc->sc_name,
3813                             inst->sc_name);
3814 
3815                 uu_list_walk_end(insts);
3816         }
3817 
3818         uu_list_walk_end(svcs);
3819 
3820         svcs = uu_list_walk_start(b->sc_bundle_services, 0);
3821         while ((svc = uu_list_walk_next(svcs)) != NULL) {
3822                 (void) fputs("svc:/", stdout);
3823                 (void) puts(svc->sc_name);
3824         }
3825         uu_list_walk_end(svcs);
3826 
3827         internal_bundle_free(b);
3828 
3829         return (0);
3830 }