19781SMoriah.Waterland@Sun.COM /*
29781SMoriah.Waterland@Sun.COM * CDDL HEADER START
39781SMoriah.Waterland@Sun.COM *
49781SMoriah.Waterland@Sun.COM * The contents of this file are subject to the terms of the
59781SMoriah.Waterland@Sun.COM * Common Development and Distribution License (the "License").
69781SMoriah.Waterland@Sun.COM * You may not use this file except in compliance with the License.
79781SMoriah.Waterland@Sun.COM *
89781SMoriah.Waterland@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99781SMoriah.Waterland@Sun.COM * or http://www.opensolaris.org/os/licensing.
109781SMoriah.Waterland@Sun.COM * See the License for the specific language governing permissions
119781SMoriah.Waterland@Sun.COM * and limitations under the License.
129781SMoriah.Waterland@Sun.COM *
139781SMoriah.Waterland@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
149781SMoriah.Waterland@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159781SMoriah.Waterland@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
169781SMoriah.Waterland@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
179781SMoriah.Waterland@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
189781SMoriah.Waterland@Sun.COM *
199781SMoriah.Waterland@Sun.COM * CDDL HEADER END
209781SMoriah.Waterland@Sun.COM */
219781SMoriah.Waterland@Sun.COM
229781SMoriah.Waterland@Sun.COM /*
239781SMoriah.Waterland@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
249781SMoriah.Waterland@Sun.COM * Use is subject to license terms.
259781SMoriah.Waterland@Sun.COM */
269781SMoriah.Waterland@Sun.COM
279781SMoriah.Waterland@Sun.COM
289781SMoriah.Waterland@Sun.COM /* unix system includes */
299781SMoriah.Waterland@Sun.COM
309781SMoriah.Waterland@Sun.COM #include <stdio.h>
319781SMoriah.Waterland@Sun.COM #include <stdarg.h>
329781SMoriah.Waterland@Sun.COM #include <stdlib.h>
339781SMoriah.Waterland@Sun.COM #include <string.h>
349781SMoriah.Waterland@Sun.COM #include <sys/types.h>
359781SMoriah.Waterland@Sun.COM #include <unistd.h>
369781SMoriah.Waterland@Sun.COM #include <locale.h>
379781SMoriah.Waterland@Sun.COM #include <sys/param.h>
389781SMoriah.Waterland@Sun.COM #include <openssl/bio.h>
399781SMoriah.Waterland@Sun.COM
409781SMoriah.Waterland@Sun.COM #include <pkglib.h>
419781SMoriah.Waterland@Sun.COM #include <pkgerr.h>
429781SMoriah.Waterland@Sun.COM #include <keystore.h>
439781SMoriah.Waterland@Sun.COM #include "pkgadm.h"
449781SMoriah.Waterland@Sun.COM #include "pkgadm_msgs.h"
459869SCasper.Dik@Sun.COM #include "libadm.h"
469781SMoriah.Waterland@Sun.COM
479781SMoriah.Waterland@Sun.COM /* initial error message buffer size */
489781SMoriah.Waterland@Sun.COM
499781SMoriah.Waterland@Sun.COM #define ERR_BUFSIZE 2048
509781SMoriah.Waterland@Sun.COM
519781SMoriah.Waterland@Sun.COM /* Local Function Prototypes */
529781SMoriah.Waterland@Sun.COM
539781SMoriah.Waterland@Sun.COM static void print_version();
549781SMoriah.Waterland@Sun.COM int get_dbstatus(int argc, char **argv);
559869SCasper.Dik@Sun.COM int sync_server(int argc, char **argv);
569781SMoriah.Waterland@Sun.COM
579781SMoriah.Waterland@Sun.COM /* holds subcommands and their definitions */
589781SMoriah.Waterland@Sun.COM struct cmd {
599781SMoriah.Waterland@Sun.COM char *c_name;
609781SMoriah.Waterland@Sun.COM int (*c_func)(int, char **);
619781SMoriah.Waterland@Sun.COM };
629781SMoriah.Waterland@Sun.COM
639781SMoriah.Waterland@Sun.COM struct cmd cmds[] = {
649781SMoriah.Waterland@Sun.COM { "dbstatus", get_dbstatus},
659781SMoriah.Waterland@Sun.COM { "lock", admin_lock},
669869SCasper.Dik@Sun.COM { "sync", sync_server},
679781SMoriah.Waterland@Sun.COM /* last one must be all NULLs */
689781SMoriah.Waterland@Sun.COM { NULL, NULL }
699781SMoriah.Waterland@Sun.COM };
709781SMoriah.Waterland@Sun.COM
719781SMoriah.Waterland@Sun.COM struct cmd cert_cmds[] = {
729781SMoriah.Waterland@Sun.COM { "addcert", addcert},
739781SMoriah.Waterland@Sun.COM { "listcert", listcert},
749781SMoriah.Waterland@Sun.COM { "removecert", removecert},
759781SMoriah.Waterland@Sun.COM /* last one must be all NULLs */
769781SMoriah.Waterland@Sun.COM { NULL, NULL }
779781SMoriah.Waterland@Sun.COM };
789781SMoriah.Waterland@Sun.COM
799781SMoriah.Waterland@Sun.COM
809781SMoriah.Waterland@Sun.COM /*
819781SMoriah.Waterland@Sun.COM * Function: main
829781SMoriah.Waterland@Sun.COM *
839781SMoriah.Waterland@Sun.COM * Return: 0 - subprocessing successful
849781SMoriah.Waterland@Sun.COM * scripts and reboot
859781SMoriah.Waterland@Sun.COM * [other] - subprocessing-specific failure
869781SMoriah.Waterland@Sun.COM */
879781SMoriah.Waterland@Sun.COM int
main(int argc,char ** argv)889781SMoriah.Waterland@Sun.COM main(int argc, char **argv)
899781SMoriah.Waterland@Sun.COM {
909781SMoriah.Waterland@Sun.COM char cur_cmd;
919781SMoriah.Waterland@Sun.COM int newargc;
929781SMoriah.Waterland@Sun.COM char **newargv;
939781SMoriah.Waterland@Sun.COM int i;
949781SMoriah.Waterland@Sun.COM
959781SMoriah.Waterland@Sun.COM /* Should be defined by cc -D */
969781SMoriah.Waterland@Sun.COM #if !defined(TEXT_DOMAIN)
979781SMoriah.Waterland@Sun.COM #define TEXT_DOMAIN "SYS_TEST"
989781SMoriah.Waterland@Sun.COM #endif
999781SMoriah.Waterland@Sun.COM
1009781SMoriah.Waterland@Sun.COM /* set the default text domain for messaging */
1019781SMoriah.Waterland@Sun.COM (void) setlocale(LC_ALL, "");
1029781SMoriah.Waterland@Sun.COM (void) textdomain(TEXT_DOMAIN);
1039781SMoriah.Waterland@Sun.COM
1049781SMoriah.Waterland@Sun.COM if (getenv("PKGADM_VERBOSE")) {
1059869SCasper.Dik@Sun.COM set_verbose(B_TRUE);
1069781SMoriah.Waterland@Sun.COM }
1079781SMoriah.Waterland@Sun.COM
1089781SMoriah.Waterland@Sun.COM /* Superficial check of the arguments. */
1099781SMoriah.Waterland@Sun.COM if (argc <= 1) {
1109781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_INFO, MSG_USAGE);
1119781SMoriah.Waterland@Sun.COM return (1);
1129781SMoriah.Waterland@Sun.COM }
1139781SMoriah.Waterland@Sun.COM
1149781SMoriah.Waterland@Sun.COM /* first, process any arguments that can appear before the subcommand */
1159781SMoriah.Waterland@Sun.COM while ((i = getopt(argc, argv, "vV?")) != EOF) {
1169781SMoriah.Waterland@Sun.COM switch (i) {
1179781SMoriah.Waterland@Sun.COM case 'v': /* verbose mode enabled */
1189781SMoriah.Waterland@Sun.COM set_verbose(B_TRUE);
1199781SMoriah.Waterland@Sun.COM break;
1209781SMoriah.Waterland@Sun.COM case 'V':
1219781SMoriah.Waterland@Sun.COM print_version();
1229781SMoriah.Waterland@Sun.COM return (0);
1239781SMoriah.Waterland@Sun.COM case '?':
1249781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_INFO, MSG_USAGE);
1259781SMoriah.Waterland@Sun.COM return (0);
1269781SMoriah.Waterland@Sun.COM }
1279781SMoriah.Waterland@Sun.COM }
1289781SMoriah.Waterland@Sun.COM
1299781SMoriah.Waterland@Sun.COM /* OK, hand it off to the subcommand processors */
1309781SMoriah.Waterland@Sun.COM for (cur_cmd = 0; cmds[cur_cmd].c_name != NULL; cur_cmd++) {
1319781SMoriah.Waterland@Sun.COM if (ci_streq(argv[optind], cmds[cur_cmd].c_name)) {
1329781SMoriah.Waterland@Sun.COM /* make subcommand the first option */
1339781SMoriah.Waterland@Sun.COM newargc = argc - optind;
1349781SMoriah.Waterland@Sun.COM newargv = argv + optind;
1359781SMoriah.Waterland@Sun.COM opterr = optind = 1; optopt = 0;
1369781SMoriah.Waterland@Sun.COM return (cmds[cur_cmd].c_func(newargc, newargv));
1379781SMoriah.Waterland@Sun.COM }
1389781SMoriah.Waterland@Sun.COM }
1399781SMoriah.Waterland@Sun.COM
1409781SMoriah.Waterland@Sun.COM /* initialize security library */
1419781SMoriah.Waterland@Sun.COM sec_init();
1429781SMoriah.Waterland@Sun.COM
1439781SMoriah.Waterland@Sun.COM /* OK, hand it off to the subcommand processors */
1449781SMoriah.Waterland@Sun.COM for (cur_cmd = 0; cert_cmds[cur_cmd].c_name != NULL; cur_cmd++) {
1459781SMoriah.Waterland@Sun.COM if (ci_streq(argv[optind], cert_cmds[cur_cmd].c_name)) {
1469781SMoriah.Waterland@Sun.COM /* make subcommand the first option */
1479781SMoriah.Waterland@Sun.COM newargc = argc - optind;
1489781SMoriah.Waterland@Sun.COM newargv = argv + optind;
1499781SMoriah.Waterland@Sun.COM opterr = optind = 1; optopt = 0;
1509781SMoriah.Waterland@Sun.COM return (cert_cmds[cur_cmd].c_func(newargc, newargv));
1519781SMoriah.Waterland@Sun.COM }
1529781SMoriah.Waterland@Sun.COM }
1539781SMoriah.Waterland@Sun.COM
1549781SMoriah.Waterland@Sun.COM /* bad subcommand */
1559781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_BAD_SUB, argv[optind]);
1569781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_INFO, MSG_USAGE);
1579781SMoriah.Waterland@Sun.COM return (1);
1589781SMoriah.Waterland@Sun.COM }
1599781SMoriah.Waterland@Sun.COM
1609781SMoriah.Waterland@Sun.COM /*
1619781SMoriah.Waterland@Sun.COM * Name: set_verbose
1629781SMoriah.Waterland@Sun.COM * Description: Turns on verbose output
1639781SMoriah.Waterland@Sun.COM * Scope: public
1649781SMoriah.Waterland@Sun.COM * Arguments: verbose = B_TRUE indicates verbose mode
1659781SMoriah.Waterland@Sun.COM * Returns: none
1669781SMoriah.Waterland@Sun.COM */
1679781SMoriah.Waterland@Sun.COM void
set_verbose(boolean_t setting)1689781SMoriah.Waterland@Sun.COM set_verbose(boolean_t setting)
1699781SMoriah.Waterland@Sun.COM {
1709781SMoriah.Waterland@Sun.COM log_set_verbose(setting);
1719781SMoriah.Waterland@Sun.COM }
1729781SMoriah.Waterland@Sun.COM
1739781SMoriah.Waterland@Sun.COM /*
1749781SMoriah.Waterland@Sun.COM * Name: get_verbose
1759781SMoriah.Waterland@Sun.COM * Description: Returns whether or not to output verbose messages
1769781SMoriah.Waterland@Sun.COM * Scope: public
1779781SMoriah.Waterland@Sun.COM * Arguments: none
1789781SMoriah.Waterland@Sun.COM * Returns: B_TRUE - verbose messages should be output
1799781SMoriah.Waterland@Sun.COM */
1809781SMoriah.Waterland@Sun.COM boolean_t
get_verbose()1819781SMoriah.Waterland@Sun.COM get_verbose()
1829781SMoriah.Waterland@Sun.COM {
1839781SMoriah.Waterland@Sun.COM return (log_get_verbose());
1849781SMoriah.Waterland@Sun.COM }
1859781SMoriah.Waterland@Sun.COM
1869781SMoriah.Waterland@Sun.COM /*
1879781SMoriah.Waterland@Sun.COM * Name: log_pkgerr
1889781SMoriah.Waterland@Sun.COM * Description: Outputs pkgerr messages to logging facility.
1899781SMoriah.Waterland@Sun.COM * Scope: public
1909781SMoriah.Waterland@Sun.COM * Arguments: type - the severity of the message
1919781SMoriah.Waterland@Sun.COM * err - error stack to dump to facility
1929781SMoriah.Waterland@Sun.COM * Returns: none
1939781SMoriah.Waterland@Sun.COM */
1949781SMoriah.Waterland@Sun.COM void
log_pkgerr(LogMsgType type,PKG_ERR * err)1959781SMoriah.Waterland@Sun.COM log_pkgerr(LogMsgType type, PKG_ERR *err)
1969781SMoriah.Waterland@Sun.COM {
1979781SMoriah.Waterland@Sun.COM int i;
1989781SMoriah.Waterland@Sun.COM for (i = 0; i < pkgerr_num(err); i++) {
1999781SMoriah.Waterland@Sun.COM log_msg(type, "%s", pkgerr_get(err, i));
2009781SMoriah.Waterland@Sun.COM }
2019781SMoriah.Waterland@Sun.COM }
2029781SMoriah.Waterland@Sun.COM
2039781SMoriah.Waterland@Sun.COM /*
2049781SMoriah.Waterland@Sun.COM * Name: print_Version
2059781SMoriah.Waterland@Sun.COM * Desc: Prints Version of packaging tools
2069781SMoriah.Waterland@Sun.COM * Arguments: none
2079781SMoriah.Waterland@Sun.COM * Returns: none
2089781SMoriah.Waterland@Sun.COM */
2099781SMoriah.Waterland@Sun.COM static void
print_version()2109781SMoriah.Waterland@Sun.COM print_version()
2119781SMoriah.Waterland@Sun.COM {
2129781SMoriah.Waterland@Sun.COM /* ignore any and all arguments, print version only */
2139781SMoriah.Waterland@Sun.COM (void) fprintf(stdout, "%s\n", SUNW_PKGVERS);
2149781SMoriah.Waterland@Sun.COM }
2159781SMoriah.Waterland@Sun.COM
2169781SMoriah.Waterland@Sun.COM /*
2179781SMoriah.Waterland@Sun.COM * usage
2189781SMoriah.Waterland@Sun.COM *
2199781SMoriah.Waterland@Sun.COM * Outputs the usage string.
2209781SMoriah.Waterland@Sun.COM *
2219781SMoriah.Waterland@Sun.COM * Return:1
2229781SMoriah.Waterland@Sun.COM * Side effects: none
2239781SMoriah.Waterland@Sun.COM */
2249781SMoriah.Waterland@Sun.COM static int
usage()2259781SMoriah.Waterland@Sun.COM usage()
2269781SMoriah.Waterland@Sun.COM {
2279781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_INFO, MSG_USAGE);
2289781SMoriah.Waterland@Sun.COM return (1);
2299781SMoriah.Waterland@Sun.COM }
2309781SMoriah.Waterland@Sun.COM
2319781SMoriah.Waterland@Sun.COM /*
2329781SMoriah.Waterland@Sun.COM * get_dbstatus
2339781SMoriah.Waterland@Sun.COM *
2349781SMoriah.Waterland@Sun.COM * Return 'text' as the db status.
2359781SMoriah.Waterland@Sun.COM * Use the command line to determine if there is an alternate root.
2369781SMoriah.Waterland@Sun.COM *
2379781SMoriah.Waterland@Sun.COM * Return: 0 on success, nonzero on failure
2389781SMoriah.Waterland@Sun.COM * Side effects: none
2399781SMoriah.Waterland@Sun.COM */
2409781SMoriah.Waterland@Sun.COM int
get_dbstatus(int argc,char ** argv)2419781SMoriah.Waterland@Sun.COM get_dbstatus(int argc, char **argv)
2429781SMoriah.Waterland@Sun.COM {
2439781SMoriah.Waterland@Sun.COM /* Either accept 1 argument or 3 arguments where the second is -R */
2449781SMoriah.Waterland@Sun.COM if (argc != 1 && (argc != 3 || strcmp(argv[1], "-R")))
2459781SMoriah.Waterland@Sun.COM return (usage());
2469781SMoriah.Waterland@Sun.COM
2479781SMoriah.Waterland@Sun.COM (void) printf("%s\n", PKGADM_DBSTATUS_TEXT);
2489781SMoriah.Waterland@Sun.COM
2499781SMoriah.Waterland@Sun.COM return (0);
2509781SMoriah.Waterland@Sun.COM }
2519869SCasper.Dik@Sun.COM
2529869SCasper.Dik@Sun.COM /*
2539869SCasper.Dik@Sun.COM * sync
2549869SCasper.Dik@Sun.COM *
2559869SCasper.Dik@Sun.COM * Use the command line to determine if there is an alternate root.
2569869SCasper.Dik@Sun.COM *
2579869SCasper.Dik@Sun.COM * Return: 0 on success, nonzero on failure
2589869SCasper.Dik@Sun.COM * Flush the pkgserv's log.
2599869SCasper.Dik@Sun.COM */
2609869SCasper.Dik@Sun.COM int
sync_server(int argc,char ** argv)2619869SCasper.Dik@Sun.COM sync_server(int argc, char **argv)
2629869SCasper.Dik@Sun.COM {
2639869SCasper.Dik@Sun.COM int c;
2649869SCasper.Dik@Sun.COM char *root = NULL;
265*10638SJan.Kryl@Sun.COM char *dryrundir = NULL;
2669869SCasper.Dik@Sun.COM boolean_t quit = B_FALSE;
2679869SCasper.Dik@Sun.COM
268*10638SJan.Kryl@Sun.COM /*
269*10638SJan.Kryl@Sun.COM * Options:
270*10638SJan.Kryl@Sun.COM * -q: Tell pkgserv daemon to quit.
271*10638SJan.Kryl@Sun.COM * -R: Alternate root specification.
272*10638SJan.Kryl@Sun.COM * -D: Dryrun directory specification.
273*10638SJan.Kryl@Sun.COM *
274*10638SJan.Kryl@Sun.COM * -R and -D help pkgadm to locate IPC files used for communication
275*10638SJan.Kryl@Sun.COM * with pkgserv daemon. They should not be used together, though
276*10638SJan.Kryl@Sun.COM * nothing prevents you from doing so. If you use both at once
277*10638SJan.Kryl@Sun.COM * then IPC files will be searched in $ROOTDIR/$DRYRUNDIR directory.
278*10638SJan.Kryl@Sun.COM * So if you want to terminate dryrun pkgserv process, you should
279*10638SJan.Kryl@Sun.COM * always use only -D option.
280*10638SJan.Kryl@Sun.COM */
281*10638SJan.Kryl@Sun.COM while ((c = getopt(argc, argv, "D:R:q")) != EOF) {
2829869SCasper.Dik@Sun.COM switch (c) {
283*10638SJan.Kryl@Sun.COM case 'D':
284*10638SJan.Kryl@Sun.COM dryrundir = optarg;
285*10638SJan.Kryl@Sun.COM break;
2869869SCasper.Dik@Sun.COM case 'R':
2879869SCasper.Dik@Sun.COM root = optarg;
2889869SCasper.Dik@Sun.COM break;
2899869SCasper.Dik@Sun.COM case 'q':
2909869SCasper.Dik@Sun.COM quit = B_TRUE;
2919869SCasper.Dik@Sun.COM break;
2929869SCasper.Dik@Sun.COM default:
2939869SCasper.Dik@Sun.COM return (usage());
2949869SCasper.Dik@Sun.COM }
2959869SCasper.Dik@Sun.COM }
2969869SCasper.Dik@Sun.COM
297*10638SJan.Kryl@Sun.COM if (!pkgsync_needed(root, dryrundir, quit))
2989869SCasper.Dik@Sun.COM return (0);
2999869SCasper.Dik@Sun.COM
3009869SCasper.Dik@Sun.COM set_PKGpaths(root);
301*10638SJan.Kryl@Sun.COM set_cfdir(dryrundir);
3029869SCasper.Dik@Sun.COM
3039869SCasper.Dik@Sun.COM if (pkgWlock(1) == 1) {
3049869SCasper.Dik@Sun.COM /* Flush the log file */
305*10638SJan.Kryl@Sun.COM (void) pkgsync(root, dryrundir, quit);
3069869SCasper.Dik@Sun.COM (void) relslock();
3079869SCasper.Dik@Sun.COM return (0);
3089869SCasper.Dik@Sun.COM }
3099869SCasper.Dik@Sun.COM
3109869SCasper.Dik@Sun.COM return (1);
3119869SCasper.Dik@Sun.COM }
312