Home | History | Annotate | Download | only in common
      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 2009 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #include <stdlib.h>
     27 #include <getopt.h>
     28 #include <stdio.h>
     29 #include <strings.h>
     30 #include <ctype.h>
     31 #include <errno.h>
     32 #include <libnvpair.h>
     33 #include <fcntl.h>
     34 
     35 #include "mms_trace.h"
     36 #include "mms_mgmt.h"
     37 #include "mgmt_util.h"
     38 #include "mms_cfg.h"
     39 
     40 extern char *optarg;
     41 extern int optind, opterr, optopt;
     42 
     43 static char *_SrcFile = __FILE__;
     44 
     45 #ifdef	MMS_VAR_CFG
     46 static char *usemsg = 	\
     47 "Usage:  \n\
     48 \tmmsinit [-?|-h]\n\
     49 \tmmsinit -t server -o options [-P passwordfile]\n\
     50 \tmmsinit -t client -M serverhost[:port] [-P passwordfile]\n\
     51 \tmmsinit -u\n";
     52 #else
     53 static char *usemsg = \
     54 "Usage: \n\
     55 \tmmsinit [-h|-?]\n\
     56 \tmmsinit -t server -o options [-P passwordfile]\n";
     57 #endif	/* MMS_VAR_CFG */
     58 
     59 #ifdef	MMS_VAR_CFG
     60 static int uflag = 0;
     61 #endif	/* MMS_VAR_CFG */
     62 static int hflag = 0;
     63 
     64 static char *mmsinit_opts = ":M:P:t:o:fuv?";
     65 
     66 static char *passphrases[2] = {
     67 	"Enter MMS Administrative password: ",
     68 	"Re-enter password: "
     69 };
     70 
     71 
     72 static struct option mmsinit_long[] = {
     73 	{"mmhost", required_argument, NULL, 'M'},
     74 	{"passfile", required_argument, NULL, 'P'},
     75 	{"type", required_argument, NULL, 't'},
     76 	{"force", no_argument, NULL, 'f'},
     77 	{"options", required_argument, NULL, 'o'},
     78 	{"uninit", no_argument, NULL, 'u'},
     79 	{"verbose", no_argument, NULL, 'v'},
     80 	{"help", no_argument, NULL, '?'},
     81 	{NULL, 0, NULL, 0}
     82 };
     83 
     84 static void usage(void);
     85 
     86 int
     87 main(int argc, char **argv)
     88 {
     89 	int		st = 0;
     90 	int		mmsind = 0;
     91 	char		c;
     92 	char		*mmtype = "server";
     93 #ifdef	MMS_VAR_CFG
     94 	char		*mmhost = "localhost";
     95 	char		*mmport = "7151";
     96 	char		*bufp;
     97 	boolean_t	force = B_FALSE;
     98 #endif
     99 	char		*pwfile = NULL;
    100 	nvlist_t	*mmnv = NULL;
    101 	nvlist_t	*errlist = NULL;
    102 	char		buf[2048];
    103 
    104 	(void) mms_trace_open("/var/log/mms/mmsinit.log", MMS_ID_CLI,
    105 	    MMS_SEV_INFO, 5 * MEGA, 0, 0);
    106 	(void) mms_trace_filter(MMS_SEV_DEVP);
    107 	mms_trace(MMS_DEBUG, "mmsinit started ###############################");
    108 	if (argc < 2) {
    109 		st = 1;
    110 	}
    111 
    112 	memset(buf, 0, sizeof (buf));
    113 
    114 	st = nvlist_alloc(&mmnv, NV_UNIQUE_NAME, 0);
    115 	if (st != 0) {
    116 		return (st);
    117 	}
    118 
    119 	while (st == 0) {
    120 		c = getopt_clip(argc, argv, mmsinit_opts, mmsinit_long,
    121 		    &mmsind);
    122 
    123 		/* catch end-of-args */
    124 		if (c == -1) {
    125 			break;
    126 		}
    127 
    128 		switch (c) {
    129 			case 0:
    130 				/* flag set by getopt */
    131 				break;
    132 			case 't':
    133 				mmtype = optarg;
    134 #ifdef	MMS_VAR_CFG
    135 				if (*mmtype == 'c') {
    136 					st = nvlist_add_string(mmnv, O_OBJTYPE,
    137 					    "client");
    138 				} else if (*mmtype == 's') {
    139 					st = nvlist_add_string(mmnv, O_OBJTYPE,
    140 					    "server");
    141 				} else {
    142 					st = 1;
    143 					fprintf(stderr, "Invalid type: %s\n",
    144 					    mmtype);
    145 				}
    146 #else
    147 				if (*mmtype != 's') {
    148 					fprintf(stderr,
    149 					    "Type must be 'server'\n");
    150 					st = 1;
    151 				}
    152 #endif	/* MMS_VAR_CFG */
    153 				break;
    154 #ifdef	MMS_VAR_CFG
    155 			case 'f':
    156 				force = B_TRUE;
    157 				break;
    158 			case 'M':
    159 				mmhost = optarg;
    160 				bufp = strchr(mmhost, ':');
    161 				if (bufp == NULL) {
    162 					mmport = MMS_DEF_MMPORT;
    163 				} else {
    164 					*bufp = '\0';
    165 					mmport = ++bufp;
    166 				}
    167 
    168 				if (strcmp(mmhost, "localhost") == 0) {
    169 					gethostname(buf, sizeof (buf));
    170 
    171 					st = nvlist_add_string(mmnv, O_MMHOST,
    172 					    buf);
    173 				} else {
    174 					st = nvlist_add_string(mmnv, O_MMHOST,
    175 					    mmhost);
    176 				}
    177 				if (st != 0) {
    178 					break;
    179 				}
    180 
    181 				st = nvlist_add_string(mmnv, O_MMPORT, mmport);
    182 
    183 				break;
    184 #endif	/* MMS_VAR_CFG */
    185 			case 'P':
    186 				pwfile = optarg;
    187 				break;
    188 			case 'o':
    189 				st = mgmt_opt_to_var(optarg, B_FALSE, mmnv);
    190 
    191 				break;
    192 #ifdef	MMS_VAR_CFG
    193 			case 'u':
    194 				uflag++;
    195 				break;
    196 #endif	/* MMS_VAR_CFG */
    197 			case '?':
    198 				hflag++;
    199 				break;
    200 			case ':':
    201 				fprintf(stderr,
    202 				    "Option %s requires an operand\n",
    203 				    argv[optind-1]);
    204 				st = 1;
    205 				break;
    206 			default:
    207 				st = 1;
    208 				break;
    209 		}
    210 	}
    211 
    212 	if ((st != 0) || hflag) {
    213 		usage();
    214 		goto done;
    215 	}
    216 
    217 #ifdef	MMS_VAR_CFG
    218 	if (uflag) {
    219 		char	yesno = 'n';
    220 
    221 		/* remove everything except the DB */
    222 		if ((!isatty(STDIN_FILENO)) && !force) {
    223 			fprintf(stderr,
    224 			    "To uninitialize from a script, "
    225 			    "please use the -f option.\n");
    226 			st = 1;
    227 			goto done;
    228 		}
    229 		fprintf(stdout,
    230 		    "Do you really want to stop using MMS services on "
    231 		    "this system? [y|n] ");
    232 
    233 		yesno = fgetc(stdin);
    234 		if ((yesno == 'y') || (yesno == 'Y')) {
    235 			st = mms_mgmt_uninitialize();
    236 		} else {
    237 			fprintf(stdout,
    238 			    "Uninitialize will not be performed.\n");
    239 		}
    240 		goto done;
    241 	}
    242 
    243 	/* check to see if this host has already been initialized first */
    244 	st = mms_cfg_getvar(MMS_CFG_CONFIG_TYPE, buf);
    245 	if ((st == 0) && (buf[0] != '\0')) {
    246 		st = EALREADY;
    247 		goto done;
    248 	} else {
    249 		st = 0;
    250 	}
    251 
    252 	if (!mmtype) {
    253 		fprintf(stdout,
    254 		    "Either -t client or -t server must be specified.\n");
    255 		st = 1;
    256 		goto done;
    257 	}
    258 #endif	/* MMS_VAR_CFG */
    259 
    260 	st = nvlist_add_string(mmnv, O_OBJTYPE, mmtype);
    261 
    262 	/*  Prompt the user for the MM password or read it from a file */
    263 	st = mms_mgmt_get_pwd(pwfile, O_MMPASS, passphrases, mmnv, errlist);
    264 	if (st != 0) {
    265 		fprintf(stderr, "%s\n", mms_mgmt_get_errstr(st));
    266 		goto done;
    267 	}
    268 
    269 #ifdef	MMS_VAR_CFG
    270 	if (*mmtype == 's') {
    271 		/* set mmhost to localhost */
    272 		gethostname(buf, sizeof (buf));
    273 		nvlist_add_string(mmnv, O_MMHOST, buf);
    274 	}
    275 #endif	/* MMS_VAR_CFG */
    276 
    277 	if (st != 0) {
    278 		goto done;
    279 	}
    280 
    281 	st = mms_mgmt_init_host(mmnv, &errlist);
    282 
    283 done:
    284 	if (st != 0) {
    285 		if (st == EOPNOTSUPP) {
    286 			fprintf(stderr,
    287 			    "Cannot change MMS host type.  To change ");
    288 			fprintf(stderr,
    289 			    "this value, first run 'mmsinit -u'.\n");
    290 		} else if (st == EALREADY) {
    291 			fprintf(stderr,
    292 			    "\nMMS already initialized on this system.\nTo ");
    293 			fprintf(stderr,
    294 			    "change options, use the 'mmsadm set' command.\n");
    295 		} else if (errlist) {
    296 			nvpair_t	*nv = nvlist_next_nvpair(errlist, NULL);
    297 			char		*nvo = NULL;
    298 			int		nvl = 0;
    299 			const char	*errmsg = NULL;
    300 
    301 			while (nv != NULL) {
    302 				nvo = nvpair_name(nv);
    303 				(void) nvpair_value_int32(nv, &nvl);
    304 				errmsg = mms_mgmt_get_errstr(nvl);
    305 				if (errmsg != NULL) {
    306 					fprintf(stderr, "\t%s\t%s\n", nvo,
    307 					    errmsg);
    308 				} else {
    309 					fprintf(stderr, "\t%s\terrno = %d\n",
    310 					    nvo, nvl);
    311 				}
    312 
    313 				nv = nvlist_next_nvpair(errlist, nv);
    314 			}
    315 		}
    316 	}
    317 
    318 	if (errlist) {
    319 		nvlist_free(errlist);
    320 	}
    321 
    322 	if (mmnv) {
    323 		nvlist_free(mmnv);
    324 	}
    325 
    326 	if (st != 0) {
    327 		fprintf(stderr, "mmsinit exiting with error %d\n", st);
    328 	}
    329 
    330 	mms_trace_close();
    331 
    332 	return (st);
    333 }
    334 
    335 static void
    336 usage(void)
    337 {
    338 	printf("%s\n", usemsg);
    339 }
    340