15c51f124SMoriah Waterland /* 25c51f124SMoriah Waterland * CDDL HEADER START 35c51f124SMoriah Waterland * 45c51f124SMoriah Waterland * The contents of this file are subject to the terms of the 55c51f124SMoriah Waterland * Common Development and Distribution License (the "License"). 65c51f124SMoriah Waterland * You may not use this file except in compliance with the License. 75c51f124SMoriah Waterland * 85c51f124SMoriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95c51f124SMoriah Waterland * or http://www.opensolaris.org/os/licensing. 105c51f124SMoriah Waterland * See the License for the specific language governing permissions 115c51f124SMoriah Waterland * and limitations under the License. 125c51f124SMoriah Waterland * 135c51f124SMoriah Waterland * When distributing Covered Code, include this CDDL HEADER in each 145c51f124SMoriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155c51f124SMoriah Waterland * If applicable, add the following below this CDDL HEADER, with the 165c51f124SMoriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying 175c51f124SMoriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner] 185c51f124SMoriah Waterland * 195c51f124SMoriah Waterland * CDDL HEADER END 205c51f124SMoriah Waterland */ 215c51f124SMoriah Waterland 225c51f124SMoriah Waterland /* 235c51f124SMoriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 245c51f124SMoriah Waterland * Use is subject to license terms. 255c51f124SMoriah Waterland */ 265c51f124SMoriah Waterland 275c51f124SMoriah Waterland 285c51f124SMoriah Waterland /* unix system includes */ 295c51f124SMoriah Waterland 305c51f124SMoriah Waterland #include <stdio.h> 315c51f124SMoriah Waterland #include <stdarg.h> 325c51f124SMoriah Waterland #include <stdlib.h> 335c51f124SMoriah Waterland #include <string.h> 345c51f124SMoriah Waterland #include <sys/types.h> 355c51f124SMoriah Waterland #include <unistd.h> 365c51f124SMoriah Waterland #include <locale.h> 375c51f124SMoriah Waterland #include <sys/param.h> 385c51f124SMoriah Waterland #include <openssl/bio.h> 395c51f124SMoriah Waterland 405c51f124SMoriah Waterland #include <pkglib.h> 415c51f124SMoriah Waterland #include <pkgerr.h> 425c51f124SMoriah Waterland #include <keystore.h> 435c51f124SMoriah Waterland #include "pkgadm.h" 445c51f124SMoriah Waterland #include "pkgadm_msgs.h" 4562224350SCasper H.S. Dik #include "libadm.h" 465c51f124SMoriah Waterland 475c51f124SMoriah Waterland /* initial error message buffer size */ 485c51f124SMoriah Waterland 495c51f124SMoriah Waterland #define ERR_BUFSIZE 2048 505c51f124SMoriah Waterland 515c51f124SMoriah Waterland /* Local Function Prototypes */ 525c51f124SMoriah Waterland 535c51f124SMoriah Waterland static void print_version(); 545c51f124SMoriah Waterland int get_dbstatus(int argc, char **argv); 5562224350SCasper H.S. Dik int sync_server(int argc, char **argv); 565c51f124SMoriah Waterland 575c51f124SMoriah Waterland /* holds subcommands and their definitions */ 585c51f124SMoriah Waterland struct cmd { 595c51f124SMoriah Waterland char *c_name; 605c51f124SMoriah Waterland int (*c_func)(int, char **); 615c51f124SMoriah Waterland }; 625c51f124SMoriah Waterland 635c51f124SMoriah Waterland struct cmd cmds[] = { 645c51f124SMoriah Waterland { "dbstatus", get_dbstatus}, 655c51f124SMoriah Waterland { "lock", admin_lock}, 6662224350SCasper H.S. Dik { "sync", sync_server}, 675c51f124SMoriah Waterland /* last one must be all NULLs */ 685c51f124SMoriah Waterland { NULL, NULL } 695c51f124SMoriah Waterland }; 705c51f124SMoriah Waterland 715c51f124SMoriah Waterland struct cmd cert_cmds[] = { 725c51f124SMoriah Waterland { "addcert", addcert}, 735c51f124SMoriah Waterland { "listcert", listcert}, 745c51f124SMoriah Waterland { "removecert", removecert}, 755c51f124SMoriah Waterland /* last one must be all NULLs */ 765c51f124SMoriah Waterland { NULL, NULL } 775c51f124SMoriah Waterland }; 785c51f124SMoriah Waterland 795c51f124SMoriah Waterland 805c51f124SMoriah Waterland /* 815c51f124SMoriah Waterland * Function: main 825c51f124SMoriah Waterland * 835c51f124SMoriah Waterland * Return: 0 - subprocessing successful 845c51f124SMoriah Waterland * scripts and reboot 855c51f124SMoriah Waterland * [other] - subprocessing-specific failure 865c51f124SMoriah Waterland */ 875c51f124SMoriah Waterland int 885c51f124SMoriah Waterland main(int argc, char **argv) 895c51f124SMoriah Waterland { 905c51f124SMoriah Waterland char cur_cmd; 915c51f124SMoriah Waterland int newargc; 925c51f124SMoriah Waterland char **newargv; 935c51f124SMoriah Waterland int i; 945c51f124SMoriah Waterland 955c51f124SMoriah Waterland /* Should be defined by cc -D */ 965c51f124SMoriah Waterland #if !defined(TEXT_DOMAIN) 975c51f124SMoriah Waterland #define TEXT_DOMAIN "SYS_TEST" 985c51f124SMoriah Waterland #endif 995c51f124SMoriah Waterland 1005c51f124SMoriah Waterland /* set the default text domain for messaging */ 1015c51f124SMoriah Waterland (void) setlocale(LC_ALL, ""); 1025c51f124SMoriah Waterland (void) textdomain(TEXT_DOMAIN); 1035c51f124SMoriah Waterland 1045c51f124SMoriah Waterland if (getenv("PKGADM_VERBOSE")) { 1055c51f124SMoriah Waterland set_verbose(B_TRUE); 1065c51f124SMoriah Waterland } 1075c51f124SMoriah Waterland 1085c51f124SMoriah Waterland /* Superficial check of the arguments. */ 1095c51f124SMoriah Waterland if (argc <= 1) { 1105c51f124SMoriah Waterland log_msg(LOG_MSG_INFO, MSG_USAGE); 1115c51f124SMoriah Waterland return (1); 1125c51f124SMoriah Waterland } 1135c51f124SMoriah Waterland 1145c51f124SMoriah Waterland /* first, process any arguments that can appear before the subcommand */ 1155c51f124SMoriah Waterland while ((i = getopt(argc, argv, "vV?")) != EOF) { 1165c51f124SMoriah Waterland switch (i) { 1175c51f124SMoriah Waterland case 'v': /* verbose mode enabled */ 1185c51f124SMoriah Waterland set_verbose(B_TRUE); 1195c51f124SMoriah Waterland break; 1205c51f124SMoriah Waterland case 'V': 1215c51f124SMoriah Waterland print_version(); 1225c51f124SMoriah Waterland return (0); 1235c51f124SMoriah Waterland case '?': 1245c51f124SMoriah Waterland log_msg(LOG_MSG_INFO, MSG_USAGE); 1255c51f124SMoriah Waterland return (0); 1265c51f124SMoriah Waterland } 1275c51f124SMoriah Waterland } 1285c51f124SMoriah Waterland 1295c51f124SMoriah Waterland /* OK, hand it off to the subcommand processors */ 1305c51f124SMoriah Waterland for (cur_cmd = 0; cmds[cur_cmd].c_name != NULL; cur_cmd++) { 1315c51f124SMoriah Waterland if (ci_streq(argv[optind], cmds[cur_cmd].c_name)) { 1325c51f124SMoriah Waterland /* make subcommand the first option */ 1335c51f124SMoriah Waterland newargc = argc - optind; 1345c51f124SMoriah Waterland newargv = argv + optind; 1355c51f124SMoriah Waterland opterr = optind = 1; optopt = 0; 1365c51f124SMoriah Waterland return (cmds[cur_cmd].c_func(newargc, newargv)); 1375c51f124SMoriah Waterland } 1385c51f124SMoriah Waterland } 1395c51f124SMoriah Waterland 1405c51f124SMoriah Waterland /* initialize security library */ 1415c51f124SMoriah Waterland sec_init(); 1425c51f124SMoriah Waterland 1435c51f124SMoriah Waterland /* OK, hand it off to the subcommand processors */ 1445c51f124SMoriah Waterland for (cur_cmd = 0; cert_cmds[cur_cmd].c_name != NULL; cur_cmd++) { 1455c51f124SMoriah Waterland if (ci_streq(argv[optind], cert_cmds[cur_cmd].c_name)) { 1465c51f124SMoriah Waterland /* make subcommand the first option */ 1475c51f124SMoriah Waterland newargc = argc - optind; 1485c51f124SMoriah Waterland newargv = argv + optind; 1495c51f124SMoriah Waterland opterr = optind = 1; optopt = 0; 1505c51f124SMoriah Waterland return (cert_cmds[cur_cmd].c_func(newargc, newargv)); 1515c51f124SMoriah Waterland } 1525c51f124SMoriah Waterland } 1535c51f124SMoriah Waterland 1545c51f124SMoriah Waterland /* bad subcommand */ 1555c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_BAD_SUB, argv[optind]); 1565c51f124SMoriah Waterland log_msg(LOG_MSG_INFO, MSG_USAGE); 1575c51f124SMoriah Waterland return (1); 1585c51f124SMoriah Waterland } 1595c51f124SMoriah Waterland 1605c51f124SMoriah Waterland /* 1615c51f124SMoriah Waterland * Name: set_verbose 1625c51f124SMoriah Waterland * Description: Turns on verbose output 1635c51f124SMoriah Waterland * Scope: public 1645c51f124SMoriah Waterland * Arguments: verbose = B_TRUE indicates verbose mode 1655c51f124SMoriah Waterland * Returns: none 1665c51f124SMoriah Waterland */ 1675c51f124SMoriah Waterland void 1685c51f124SMoriah Waterland set_verbose(boolean_t setting) 1695c51f124SMoriah Waterland { 1705c51f124SMoriah Waterland log_set_verbose(setting); 1715c51f124SMoriah Waterland } 1725c51f124SMoriah Waterland 1735c51f124SMoriah Waterland /* 1745c51f124SMoriah Waterland * Name: get_verbose 1755c51f124SMoriah Waterland * Description: Returns whether or not to output verbose messages 1765c51f124SMoriah Waterland * Scope: public 1775c51f124SMoriah Waterland * Arguments: none 1785c51f124SMoriah Waterland * Returns: B_TRUE - verbose messages should be output 1795c51f124SMoriah Waterland */ 1805c51f124SMoriah Waterland boolean_t 1815c51f124SMoriah Waterland get_verbose() 1825c51f124SMoriah Waterland { 1835c51f124SMoriah Waterland return (log_get_verbose()); 1845c51f124SMoriah Waterland } 1855c51f124SMoriah Waterland 1865c51f124SMoriah Waterland /* 1875c51f124SMoriah Waterland * Name: log_pkgerr 1885c51f124SMoriah Waterland * Description: Outputs pkgerr messages to logging facility. 1895c51f124SMoriah Waterland * Scope: public 1905c51f124SMoriah Waterland * Arguments: type - the severity of the message 1915c51f124SMoriah Waterland * err - error stack to dump to facility 1925c51f124SMoriah Waterland * Returns: none 1935c51f124SMoriah Waterland */ 1945c51f124SMoriah Waterland void 1955c51f124SMoriah Waterland log_pkgerr(LogMsgType type, PKG_ERR *err) 1965c51f124SMoriah Waterland { 1975c51f124SMoriah Waterland int i; 1985c51f124SMoriah Waterland for (i = 0; i < pkgerr_num(err); i++) { 1995c51f124SMoriah Waterland log_msg(type, "%s", pkgerr_get(err, i)); 2005c51f124SMoriah Waterland } 2015c51f124SMoriah Waterland } 2025c51f124SMoriah Waterland 2035c51f124SMoriah Waterland /* 2045c51f124SMoriah Waterland * Name: print_Version 2055c51f124SMoriah Waterland * Desc: Prints Version of packaging tools 2065c51f124SMoriah Waterland * Arguments: none 2075c51f124SMoriah Waterland * Returns: none 2085c51f124SMoriah Waterland */ 2095c51f124SMoriah Waterland static void 2105c51f124SMoriah Waterland print_version() 2115c51f124SMoriah Waterland { 2125c51f124SMoriah Waterland /* ignore any and all arguments, print version only */ 2135c51f124SMoriah Waterland (void) fprintf(stdout, "%s\n", SUNW_PKGVERS); 2145c51f124SMoriah Waterland } 2155c51f124SMoriah Waterland 2165c51f124SMoriah Waterland /* 2175c51f124SMoriah Waterland * usage 2185c51f124SMoriah Waterland * 2195c51f124SMoriah Waterland * Outputs the usage string. 2205c51f124SMoriah Waterland * 2215c51f124SMoriah Waterland * Return:1 2225c51f124SMoriah Waterland * Side effects: none 2235c51f124SMoriah Waterland */ 2245c51f124SMoriah Waterland static int 2255c51f124SMoriah Waterland usage() 2265c51f124SMoriah Waterland { 2275c51f124SMoriah Waterland log_msg(LOG_MSG_INFO, MSG_USAGE); 2285c51f124SMoriah Waterland return (1); 2295c51f124SMoriah Waterland } 2305c51f124SMoriah Waterland 2315c51f124SMoriah Waterland /* 2325c51f124SMoriah Waterland * get_dbstatus 2335c51f124SMoriah Waterland * 2345c51f124SMoriah Waterland * Return 'text' as the db status. 2355c51f124SMoriah Waterland * Use the command line to determine if there is an alternate root. 2365c51f124SMoriah Waterland * 2375c51f124SMoriah Waterland * Return: 0 on success, nonzero on failure 2385c51f124SMoriah Waterland * Side effects: none 2395c51f124SMoriah Waterland */ 2405c51f124SMoriah Waterland int 2415c51f124SMoriah Waterland get_dbstatus(int argc, char **argv) 2425c51f124SMoriah Waterland { 2435c51f124SMoriah Waterland /* Either accept 1 argument or 3 arguments where the second is -R */ 2445c51f124SMoriah Waterland if (argc != 1 && (argc != 3 || strcmp(argv[1], "-R"))) 2455c51f124SMoriah Waterland return (usage()); 2465c51f124SMoriah Waterland 2475c51f124SMoriah Waterland (void) printf("%s\n", PKGADM_DBSTATUS_TEXT); 2485c51f124SMoriah Waterland 2495c51f124SMoriah Waterland return (0); 2505c51f124SMoriah Waterland } 25162224350SCasper H.S. Dik 25262224350SCasper H.S. Dik /* 25362224350SCasper H.S. Dik * sync 25462224350SCasper H.S. Dik * 25562224350SCasper H.S. Dik * Use the command line to determine if there is an alternate root. 25662224350SCasper H.S. Dik * 25762224350SCasper H.S. Dik * Return: 0 on success, nonzero on failure 25862224350SCasper H.S. Dik * Flush the pkgserv's log. 25962224350SCasper H.S. Dik */ 26062224350SCasper H.S. Dik int 26162224350SCasper H.S. Dik sync_server(int argc, char **argv) 26262224350SCasper H.S. Dik { 26362224350SCasper H.S. Dik int c; 26462224350SCasper H.S. Dik char *root = NULL; 265*af122237SJan Kryl char *dryrundir = NULL; 26662224350SCasper H.S. Dik boolean_t quit = B_FALSE; 26762224350SCasper H.S. Dik 268*af122237SJan Kryl /* 269*af122237SJan Kryl * Options: 270*af122237SJan Kryl * -q: Tell pkgserv daemon to quit. 271*af122237SJan Kryl * -R: Alternate root specification. 272*af122237SJan Kryl * -D: Dryrun directory specification. 273*af122237SJan Kryl * 274*af122237SJan Kryl * -R and -D help pkgadm to locate IPC files used for communication 275*af122237SJan Kryl * with pkgserv daemon. They should not be used together, though 276*af122237SJan Kryl * nothing prevents you from doing so. If you use both at once 277*af122237SJan Kryl * then IPC files will be searched in $ROOTDIR/$DRYRUNDIR directory. 278*af122237SJan Kryl * So if you want to terminate dryrun pkgserv process, you should 279*af122237SJan Kryl * always use only -D option. 280*af122237SJan Kryl */ 281*af122237SJan Kryl while ((c = getopt(argc, argv, "D:R:q")) != EOF) { 28262224350SCasper H.S. Dik switch (c) { 283*af122237SJan Kryl case 'D': 284*af122237SJan Kryl dryrundir = optarg; 285*af122237SJan Kryl break; 28662224350SCasper H.S. Dik case 'R': 28762224350SCasper H.S. Dik root = optarg; 28862224350SCasper H.S. Dik break; 28962224350SCasper H.S. Dik case 'q': 29062224350SCasper H.S. Dik quit = B_TRUE; 29162224350SCasper H.S. Dik break; 29262224350SCasper H.S. Dik default: 29362224350SCasper H.S. Dik return (usage()); 29462224350SCasper H.S. Dik } 29562224350SCasper H.S. Dik } 29662224350SCasper H.S. Dik 297*af122237SJan Kryl if (!pkgsync_needed(root, dryrundir, quit)) 29862224350SCasper H.S. Dik return (0); 29962224350SCasper H.S. Dik 30062224350SCasper H.S. Dik set_PKGpaths(root); 301*af122237SJan Kryl set_cfdir(dryrundir); 30262224350SCasper H.S. Dik 30362224350SCasper H.S. Dik if (pkgWlock(1) == 1) { 30462224350SCasper H.S. Dik /* Flush the log file */ 305*af122237SJan Kryl (void) pkgsync(root, dryrundir, quit); 30662224350SCasper H.S. Dik (void) relslock(); 30762224350SCasper H.S. Dik return (0); 30862224350SCasper H.S. Dik } 30962224350SCasper H.S. Dik 31062224350SCasper H.S. Dik return (1); 31162224350SCasper H.S. Dik } 312