xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/kssl/kssladm/kssladm.c (revision 2800:95e099952fa3)
1898Skais /*
2898Skais  * CDDL HEADER START
3898Skais  *
4898Skais  * The contents of this file are subject to the terms of the
5*2800Skrishna  * Common Development and Distribution License (the "License").
6*2800Skrishna  * You may not use this file except in compliance with the License.
7898Skais  *
8898Skais  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9898Skais  * or http://www.opensolaris.org/os/licensing.
10898Skais  * See the License for the specific language governing permissions
11898Skais  * and limitations under the License.
12898Skais  *
13898Skais  * When distributing Covered Code, include this CDDL HEADER in each
14898Skais  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15898Skais  * If applicable, add the following below this CDDL HEADER, with the
16898Skais  * fields enclosed by brackets "[]" replaced with your own identifying
17898Skais  * information: Portions Copyright [yyyy] [name of copyright owner]
18898Skais  *
19898Skais  * CDDL HEADER END
20898Skais  */
21898Skais /*
22*2800Skrishna  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23898Skais  * Use is subject to license terms.
24898Skais  */
25898Skais 
26898Skais #pragma ident	"%Z%%M%	%I%	%E% SMI"
27898Skais 
28898Skais #include <ctype.h>
29898Skais #include <stdio.h>
30898Skais #include <stdlib.h>
31898Skais #include <unistd.h>
32898Skais #include <fcntl.h>
33898Skais #include <strings.h>
34898Skais #include <libscf.h>
35898Skais #include <sys/errno.h>
36898Skais #include <errno.h>
37898Skais #include <sys/stropts.h>
38898Skais #include "kssladm.h"
39898Skais 
40898Skais 
41898Skais /*
42898Skais  * kssladm(1M)
43898Skais  *
44898Skais  * Command to manage the entries in kernel SSL proxy table. This is
45898Skais  * a private command called indirectly from ksslcfg(1M).
46898Skais  */
47898Skais 
48898Skais boolean_t verbose = B_FALSE;
49898Skais 
50898Skais static void
usage_all(void)51898Skais usage_all(void)
52898Skais {
53898Skais 	(void) fprintf(stderr, "Usage:\n");
54898Skais 	usage_create(B_FALSE);
55898Skais 	usage_delete(B_FALSE);
56898Skais }
57898Skais 
58898Skais int
main(int argc,char ** argv)59898Skais main(int argc, char **argv)
60898Skais {
61898Skais 	int rv = SUCCESS;
62898Skais 
63898Skais 	if (argc < 2) {
64898Skais 		usage_all();
65898Skais 		return (SMF_EXIT_ERR_CONFIG);
66898Skais 	}
67898Skais 
68898Skais 	if (strcmp(argv[1], "create") == 0) {
69898Skais 		rv = do_create(argc, argv);
70898Skais 	} else if (strcmp(argv[1], "delete") == 0) {
71898Skais 		rv = do_delete(argc, argv);
72898Skais 	} else {
73898Skais 		(void) fprintf(stderr, "Unknown sub-command: %s\n", argv[1]);
74898Skais 		usage_all();
75898Skais 		rv = SMF_EXIT_ERR_CONFIG;
76898Skais 	}
77898Skais 
78898Skais 	return (rv);
79898Skais }
80898Skais 
81898Skais 
82898Skais /*
83898Skais  * Read a passphrase from the file into the supplied buffer.
84898Skais  * A space character and the characters that follow
85898Skais  * the space character will be ignored.
86898Skais  * Return 0 when no valid passphrase was found in the file.
87898Skais  */
88898Skais static int
read_pass_from_file(const char * filename,char * buffer,size_t bufsize)89898Skais read_pass_from_file(const char *filename, char *buffer, size_t bufsize)
90898Skais {
91898Skais 	char *line;
92898Skais 	char *p;
93898Skais 	FILE *fp;
94898Skais 
95898Skais 	fp = fopen(filename, "r");
96898Skais 	if (fp == NULL) {
97898Skais 		(void) fprintf(stderr,
98898Skais 		    "Unable to open password file for reading");
99898Skais 		return (1);
100898Skais 	}
101898Skais 
102898Skais 	line = fgets(buffer, bufsize, fp);
103898Skais 	(void) fclose(fp);
104898Skais 	if (line == NULL) {
105898Skais 		return (0);
106898Skais 	}
107898Skais 
108898Skais 	for (p = buffer; *p != '\0'; p++) {
109898Skais 		if (isspace(*p)) {
110898Skais 			*p = '\0';
111898Skais 			break;
112898Skais 		}
113898Skais 	}
114898Skais 
115898Skais 	return (p - buffer);
116898Skais }
117898Skais 
118898Skais 
119898Skais int
get_passphrase(const char * password_file,char * buf,int buf_size)120898Skais get_passphrase(const char *password_file, char *buf, int buf_size)
121898Skais {
122898Skais 	if (password_file == NULL) {
123898Skais 		char *passphrase = getpassphrase("Enter passphrase: ");
124898Skais 		if (passphrase) {
125898Skais 			return (strlcpy(buf, passphrase, buf_size));
126898Skais 		}
127898Skais 
128898Skais 		return (0);
129898Skais 	}
130898Skais 
131898Skais 	return (read_pass_from_file(password_file, buf, buf_size));
132898Skais }
133898Skais 
134898Skais 
135898Skais int
kssl_send_command(char * buf,int cmd)136898Skais kssl_send_command(char *buf, int cmd)
137898Skais {
138898Skais 	int ksslfd;
139898Skais 	int rv;
140898Skais 
141898Skais 	ksslfd = open("/dev/kssl", O_RDWR);
142898Skais 	if (ksslfd < 0) {
143898Skais 		perror("Cannot open /dev/kssl");
144*2800Skrishna 		return (-1);
145898Skais 	}
146898Skais 
147898Skais 	if ((rv = ioctl(ksslfd, cmd, buf)) < 0) {
148898Skais 		switch (errno) {
149898Skais 		case EEXIST:
150898Skais 			(void) fprintf(stderr,
151898Skais 			    "Error: Can not create a INADDR_ANY instance"
152898Skais 			    " while another instance exists.\n");
153898Skais 			break;
154898Skais 		case EADDRINUSE:
155898Skais 			(void) fprintf(stderr,
156898Skais 			    "Error: Another instance with the same"
157898Skais 			    " proxy port exists.\n");
158898Skais 			break;
159898Skais 		default:
160898Skais 			perror("ioctl failure");
161898Skais 			break;
162898Skais 		}
163898Skais 	}
164898Skais 
165898Skais 	(void) close(ksslfd);
166898Skais 
167898Skais 	return (rv);
168898Skais }
169