1*5c51f124SMoriah Waterland /* 2*5c51f124SMoriah Waterland * CDDL HEADER START 3*5c51f124SMoriah Waterland * 4*5c51f124SMoriah Waterland * The contents of this file are subject to the terms of the 5*5c51f124SMoriah Waterland * Common Development and Distribution License (the "License"). 6*5c51f124SMoriah Waterland * You may not use this file except in compliance with the License. 7*5c51f124SMoriah Waterland * 8*5c51f124SMoriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*5c51f124SMoriah Waterland * or http://www.opensolaris.org/os/licensing. 10*5c51f124SMoriah Waterland * See the License for the specific language governing permissions 11*5c51f124SMoriah Waterland * and limitations under the License. 12*5c51f124SMoriah Waterland * 13*5c51f124SMoriah Waterland * When distributing Covered Code, include this CDDL HEADER in each 14*5c51f124SMoriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*5c51f124SMoriah Waterland * If applicable, add the following below this CDDL HEADER, with the 16*5c51f124SMoriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying 17*5c51f124SMoriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner] 18*5c51f124SMoriah Waterland * 19*5c51f124SMoriah Waterland * CDDL HEADER END 20*5c51f124SMoriah Waterland */ 21*5c51f124SMoriah Waterland 22*5c51f124SMoriah Waterland /* 23*5c51f124SMoriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24*5c51f124SMoriah Waterland * Use is subject to license terms. 25*5c51f124SMoriah Waterland */ 26*5c51f124SMoriah Waterland 27*5c51f124SMoriah Waterland 28*5c51f124SMoriah Waterland #include <stdio.h> 29*5c51f124SMoriah Waterland #include <stdarg.h> 30*5c51f124SMoriah Waterland #include <stdlib.h> 31*5c51f124SMoriah Waterland #include <string.h> 32*5c51f124SMoriah Waterland #include <sys/types.h> 33*5c51f124SMoriah Waterland #include <unistd.h> 34*5c51f124SMoriah Waterland #include <signal.h> 35*5c51f124SMoriah Waterland #include <locale.h> 36*5c51f124SMoriah Waterland #include <sys/param.h> 37*5c51f124SMoriah Waterland #include <openssl/bio.h> 38*5c51f124SMoriah Waterland 39*5c51f124SMoriah Waterland #include <libinst.h> 40*5c51f124SMoriah Waterland #include <pkglib.h> 41*5c51f124SMoriah Waterland #include <pkgerr.h> 42*5c51f124SMoriah Waterland #include <keystore.h> 43*5c51f124SMoriah Waterland #include "pkgadm.h" 44*5c51f124SMoriah Waterland #include "pkgadm_msgs.h" 45*5c51f124SMoriah Waterland 46*5c51f124SMoriah Waterland /* 47*5c51f124SMoriah Waterland * Name: removecert 48*5c51f124SMoriah Waterland * Desc: Removes a user certificate and associated private key, 49*5c51f124SMoriah Waterland * or a trusted certificate, from the keystore. 50*5c51f124SMoriah Waterland * Syntax: addcert [-a app] [-k keystore] -n name [-P passarg] [-R altroot] 51*5c51f124SMoriah Waterland */ 52*5c51f124SMoriah Waterland int 53*5c51f124SMoriah Waterland removecert(int argc, char **argv) 54*5c51f124SMoriah Waterland { 55*5c51f124SMoriah Waterland int i; 56*5c51f124SMoriah Waterland char keystore_file[MAXPATHLEN] = ""; 57*5c51f124SMoriah Waterland char *keystore_base = NULL; 58*5c51f124SMoriah Waterland char *homedir; 59*5c51f124SMoriah Waterland char *passarg = NULL; 60*5c51f124SMoriah Waterland char *altroot = NULL; 61*5c51f124SMoriah Waterland char *prog = NULL; 62*5c51f124SMoriah Waterland char *alias = NULL; 63*5c51f124SMoriah Waterland int ret = 1; 64*5c51f124SMoriah Waterland PKG_ERR *err = NULL; 65*5c51f124SMoriah Waterland keystore_handle_t keystore = NULL; 66*5c51f124SMoriah Waterland 67*5c51f124SMoriah Waterland while ((i = getopt(argc, argv, ":a:k:n:P:R:")) != EOF) { 68*5c51f124SMoriah Waterland switch (i) { 69*5c51f124SMoriah Waterland case 'a': 70*5c51f124SMoriah Waterland prog = optarg; 71*5c51f124SMoriah Waterland break; 72*5c51f124SMoriah Waterland case 'k': 73*5c51f124SMoriah Waterland keystore_base = optarg; 74*5c51f124SMoriah Waterland break; 75*5c51f124SMoriah Waterland case 'n': 76*5c51f124SMoriah Waterland alias = optarg; 77*5c51f124SMoriah Waterland break; 78*5c51f124SMoriah Waterland case 'P': 79*5c51f124SMoriah Waterland passarg = optarg; 80*5c51f124SMoriah Waterland break; 81*5c51f124SMoriah Waterland case 'R': 82*5c51f124SMoriah Waterland altroot = optarg; 83*5c51f124SMoriah Waterland break; 84*5c51f124SMoriah Waterland case ':': 85*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_MISSING_OPERAND, optopt); 86*5c51f124SMoriah Waterland /* fallthrough intentional */ 87*5c51f124SMoriah Waterland case '?': 88*5c51f124SMoriah Waterland default: 89*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_USAGE); 90*5c51f124SMoriah Waterland goto cleanup; 91*5c51f124SMoriah Waterland } 92*5c51f124SMoriah Waterland } 93*5c51f124SMoriah Waterland 94*5c51f124SMoriah Waterland /* we require a name */ 95*5c51f124SMoriah Waterland if (alias == NULL) { 96*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_USAGE); 97*5c51f124SMoriah Waterland goto cleanup; 98*5c51f124SMoriah Waterland } 99*5c51f124SMoriah Waterland 100*5c51f124SMoriah Waterland /* should be no arguments left */ 101*5c51f124SMoriah Waterland if ((argc-optind) > 0) { 102*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_USAGE); 103*5c51f124SMoriah Waterland goto cleanup; 104*5c51f124SMoriah Waterland } 105*5c51f124SMoriah Waterland 106*5c51f124SMoriah Waterland /* set up proper keystore */ 107*5c51f124SMoriah Waterland if (keystore_base == NULL) { 108*5c51f124SMoriah Waterland if (geteuid() == 0 || altroot != NULL) { 109*5c51f124SMoriah Waterland /* 110*5c51f124SMoriah Waterland * If we have an alternate 111*5c51f124SMoriah Waterland * root, then we have no choice but to use 112*5c51f124SMoriah Waterland * root's keystore on that alternate root, 113*5c51f124SMoriah Waterland * since there is no way to resolve a 114*5c51f124SMoriah Waterland * user's home dir given an alternate root 115*5c51f124SMoriah Waterland */ 116*5c51f124SMoriah Waterland if (strlcat(keystore_file, PKGSEC, 117*5c51f124SMoriah Waterland MAXPATHLEN) >= MAXPATHLEN) { 118*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_TOO_LONG, 119*5c51f124SMoriah Waterland keystore_file); 120*5c51f124SMoriah Waterland goto cleanup; 121*5c51f124SMoriah Waterland } 122*5c51f124SMoriah Waterland } else { 123*5c51f124SMoriah Waterland if ((homedir = getenv("HOME")) == NULL) { 124*5c51f124SMoriah Waterland /* 125*5c51f124SMoriah Waterland * not superuser, but no home dir, so 126*5c51f124SMoriah Waterland * use superuser's keystore 127*5c51f124SMoriah Waterland */ 128*5c51f124SMoriah Waterland if (strlcat(keystore_file, PKGSEC, 129*5c51f124SMoriah Waterland MAXPATHLEN) >= MAXPATHLEN) { 130*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_TOO_LONG, 131*5c51f124SMoriah Waterland keystore_file); 132*5c51f124SMoriah Waterland goto cleanup; 133*5c51f124SMoriah Waterland } 134*5c51f124SMoriah Waterland } else { 135*5c51f124SMoriah Waterland if (strlcat(keystore_file, homedir, 136*5c51f124SMoriah Waterland MAXPATHLEN) >= MAXPATHLEN) { 137*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_TOO_LONG, 138*5c51f124SMoriah Waterland homedir); 139*5c51f124SMoriah Waterland goto cleanup; 140*5c51f124SMoriah Waterland } 141*5c51f124SMoriah Waterland if (strlcat(keystore_file, "/.pkg/security", 142*5c51f124SMoriah Waterland MAXPATHLEN) >= MAXPATHLEN) { 143*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_TOO_LONG, 144*5c51f124SMoriah Waterland keystore_file); 145*5c51f124SMoriah Waterland goto cleanup; 146*5c51f124SMoriah Waterland } 147*5c51f124SMoriah Waterland } 148*5c51f124SMoriah Waterland } 149*5c51f124SMoriah Waterland } else { 150*5c51f124SMoriah Waterland if (strlcat(keystore_file, keystore_base, 151*5c51f124SMoriah Waterland MAXPATHLEN) >= MAXPATHLEN) { 152*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_TOO_LONG, 153*5c51f124SMoriah Waterland keystore_base); 154*5c51f124SMoriah Waterland goto cleanup; 155*5c51f124SMoriah Waterland } 156*5c51f124SMoriah Waterland } 157*5c51f124SMoriah Waterland 158*5c51f124SMoriah Waterland err = pkgerr_new(); 159*5c51f124SMoriah Waterland 160*5c51f124SMoriah Waterland /* now load the key store */ 161*5c51f124SMoriah Waterland log_msg(LOG_MSG_DEBUG, "Loading keystore <%s>", keystore_file); 162*5c51f124SMoriah Waterland 163*5c51f124SMoriah Waterland set_passphrase_prompt(MSG_KEYSTORE_PASSPROMPT); 164*5c51f124SMoriah Waterland set_passphrase_passarg(passarg); 165*5c51f124SMoriah Waterland 166*5c51f124SMoriah Waterland if (open_keystore(err, keystore_file, prog, pkg_passphrase_cb, 167*5c51f124SMoriah Waterland KEYSTORE_ACCESS_READWRITE | KEYSTORE_PATH_HARD, &keystore) != 0) { 168*5c51f124SMoriah Waterland log_pkgerr(LOG_MSG_ERR, err); 169*5c51f124SMoriah Waterland goto cleanup; 170*5c51f124SMoriah Waterland } 171*5c51f124SMoriah Waterland 172*5c51f124SMoriah Waterland /* now remove the selected certs */ 173*5c51f124SMoriah Waterland log_msg(LOG_MSG_DEBUG, "Removing certificate(s) with name <%s>", 174*5c51f124SMoriah Waterland alias); 175*5c51f124SMoriah Waterland if (delete_cert_and_keys(err, keystore, alias) != 0) { 176*5c51f124SMoriah Waterland log_pkgerr(LOG_MSG_ERR, err); 177*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_REMOVECERT, alias); 178*5c51f124SMoriah Waterland goto cleanup; 179*5c51f124SMoriah Waterland } 180*5c51f124SMoriah Waterland 181*5c51f124SMoriah Waterland /* now write it back out */ 182*5c51f124SMoriah Waterland log_msg(LOG_MSG_DEBUG, "Closing keystore"); 183*5c51f124SMoriah Waterland set_passphrase_prompt(MSG_KEYSTORE_PASSOUTPROMPT); 184*5c51f124SMoriah Waterland set_passphrase_passarg(passarg); 185*5c51f124SMoriah Waterland if (close_keystore(err, keystore, pkg_passphrase_cb) != 0) { 186*5c51f124SMoriah Waterland log_pkgerr(LOG_MSG_ERR, err); 187*5c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_REMOVECERT, alias); 188*5c51f124SMoriah Waterland goto cleanup; 189*5c51f124SMoriah Waterland } 190*5c51f124SMoriah Waterland 191*5c51f124SMoriah Waterland log_msg(LOG_MSG_INFO, MSG_REMOVED, alias); 192*5c51f124SMoriah Waterland 193*5c51f124SMoriah Waterland ret = 0; 194*5c51f124SMoriah Waterland /* fallthrough intentional */ 195*5c51f124SMoriah Waterland cleanup: 196*5c51f124SMoriah Waterland 197*5c51f124SMoriah Waterland if (err != NULL) 198*5c51f124SMoriah Waterland pkgerr_free(err); 199*5c51f124SMoriah Waterland 200*5c51f124SMoriah Waterland return (ret); 201*5c51f124SMoriah Waterland } 202