1c28749e9Skais /*
2c28749e9Skais * CDDL HEADER START
3c28749e9Skais *
4c28749e9Skais * The contents of this file are subject to the terms of the
5*c892ebf1Skrishna * Common Development and Distribution License (the "License").
6*c892ebf1Skrishna * You may not use this file except in compliance with the License.
7c28749e9Skais *
8c28749e9Skais * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c28749e9Skais * or http://www.opensolaris.org/os/licensing.
10c28749e9Skais * See the License for the specific language governing permissions
11c28749e9Skais * and limitations under the License.
12c28749e9Skais *
13c28749e9Skais * When distributing Covered Code, include this CDDL HEADER in each
14c28749e9Skais * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c28749e9Skais * If applicable, add the following below this CDDL HEADER, with the
16c28749e9Skais * fields enclosed by brackets "[]" replaced with your own identifying
17c28749e9Skais * information: Portions Copyright [yyyy] [name of copyright owner]
18c28749e9Skais *
19c28749e9Skais * CDDL HEADER END
20c28749e9Skais */
21c28749e9Skais /*
22*c892ebf1Skrishna * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23c28749e9Skais * Use is subject to license terms.
24c28749e9Skais */
25c28749e9Skais
26c28749e9Skais #pragma ident "%Z%%M% %I% %E% SMI"
27c28749e9Skais
28c28749e9Skais #include <ctype.h>
29c28749e9Skais #include <stdio.h>
30c28749e9Skais #include <stdlib.h>
31c28749e9Skais #include <unistd.h>
32c28749e9Skais #include <fcntl.h>
33c28749e9Skais #include <strings.h>
34c28749e9Skais #include <libscf.h>
35c28749e9Skais #include <sys/errno.h>
36c28749e9Skais #include <errno.h>
37c28749e9Skais #include <sys/stropts.h>
38c28749e9Skais #include "kssladm.h"
39c28749e9Skais
40c28749e9Skais
41c28749e9Skais /*
42c28749e9Skais * kssladm(1M)
43c28749e9Skais *
44c28749e9Skais * Command to manage the entries in kernel SSL proxy table. This is
45c28749e9Skais * a private command called indirectly from ksslcfg(1M).
46c28749e9Skais */
47c28749e9Skais
48c28749e9Skais boolean_t verbose = B_FALSE;
49c28749e9Skais
50c28749e9Skais static void
usage_all(void)51c28749e9Skais usage_all(void)
52c28749e9Skais {
53c28749e9Skais (void) fprintf(stderr, "Usage:\n");
54c28749e9Skais usage_create(B_FALSE);
55c28749e9Skais usage_delete(B_FALSE);
56c28749e9Skais }
57c28749e9Skais
58c28749e9Skais int
main(int argc,char ** argv)59c28749e9Skais main(int argc, char **argv)
60c28749e9Skais {
61c28749e9Skais int rv = SUCCESS;
62c28749e9Skais
63c28749e9Skais if (argc < 2) {
64c28749e9Skais usage_all();
65c28749e9Skais return (SMF_EXIT_ERR_CONFIG);
66c28749e9Skais }
67c28749e9Skais
68c28749e9Skais if (strcmp(argv[1], "create") == 0) {
69c28749e9Skais rv = do_create(argc, argv);
70c28749e9Skais } else if (strcmp(argv[1], "delete") == 0) {
71c28749e9Skais rv = do_delete(argc, argv);
72c28749e9Skais } else {
73c28749e9Skais (void) fprintf(stderr, "Unknown sub-command: %s\n", argv[1]);
74c28749e9Skais usage_all();
75c28749e9Skais rv = SMF_EXIT_ERR_CONFIG;
76c28749e9Skais }
77c28749e9Skais
78c28749e9Skais return (rv);
79c28749e9Skais }
80c28749e9Skais
81c28749e9Skais
82c28749e9Skais /*
83c28749e9Skais * Read a passphrase from the file into the supplied buffer.
84c28749e9Skais * A space character and the characters that follow
85c28749e9Skais * the space character will be ignored.
86c28749e9Skais * Return 0 when no valid passphrase was found in the file.
87c28749e9Skais */
88c28749e9Skais static int
read_pass_from_file(const char * filename,char * buffer,size_t bufsize)89c28749e9Skais read_pass_from_file(const char *filename, char *buffer, size_t bufsize)
90c28749e9Skais {
91c28749e9Skais char *line;
92c28749e9Skais char *p;
93c28749e9Skais FILE *fp;
94c28749e9Skais
95c28749e9Skais fp = fopen(filename, "r");
96c28749e9Skais if (fp == NULL) {
97c28749e9Skais (void) fprintf(stderr,
98c28749e9Skais "Unable to open password file for reading");
99c28749e9Skais return (1);
100c28749e9Skais }
101c28749e9Skais
102c28749e9Skais line = fgets(buffer, bufsize, fp);
103c28749e9Skais (void) fclose(fp);
104c28749e9Skais if (line == NULL) {
105c28749e9Skais return (0);
106c28749e9Skais }
107c28749e9Skais
108c28749e9Skais for (p = buffer; *p != '\0'; p++) {
109c28749e9Skais if (isspace(*p)) {
110c28749e9Skais *p = '\0';
111c28749e9Skais break;
112c28749e9Skais }
113c28749e9Skais }
114c28749e9Skais
115c28749e9Skais return (p - buffer);
116c28749e9Skais }
117c28749e9Skais
118c28749e9Skais
119c28749e9Skais int
get_passphrase(const char * password_file,char * buf,int buf_size)120c28749e9Skais get_passphrase(const char *password_file, char *buf, int buf_size)
121c28749e9Skais {
122c28749e9Skais if (password_file == NULL) {
123c28749e9Skais char *passphrase = getpassphrase("Enter passphrase: ");
124c28749e9Skais if (passphrase) {
125c28749e9Skais return (strlcpy(buf, passphrase, buf_size));
126c28749e9Skais }
127c28749e9Skais
128c28749e9Skais return (0);
129c28749e9Skais }
130c28749e9Skais
131c28749e9Skais return (read_pass_from_file(password_file, buf, buf_size));
132c28749e9Skais }
133c28749e9Skais
134c28749e9Skais
135c28749e9Skais int
kssl_send_command(char * buf,int cmd)136c28749e9Skais kssl_send_command(char *buf, int cmd)
137c28749e9Skais {
138c28749e9Skais int ksslfd;
139c28749e9Skais int rv;
140c28749e9Skais
141c28749e9Skais ksslfd = open("/dev/kssl", O_RDWR);
142c28749e9Skais if (ksslfd < 0) {
143c28749e9Skais perror("Cannot open /dev/kssl");
144*c892ebf1Skrishna return (-1);
145c28749e9Skais }
146c28749e9Skais
147c28749e9Skais if ((rv = ioctl(ksslfd, cmd, buf)) < 0) {
148c28749e9Skais switch (errno) {
149c28749e9Skais case EEXIST:
150c28749e9Skais (void) fprintf(stderr,
151c28749e9Skais "Error: Can not create a INADDR_ANY instance"
152c28749e9Skais " while another instance exists.\n");
153c28749e9Skais break;
154c28749e9Skais case EADDRINUSE:
155c28749e9Skais (void) fprintf(stderr,
156c28749e9Skais "Error: Another instance with the same"
157c28749e9Skais " proxy port exists.\n");
158c28749e9Skais break;
159c28749e9Skais default:
160c28749e9Skais perror("ioctl failure");
161c28749e9Skais break;
162c28749e9Skais }
163c28749e9Skais }
164c28749e9Skais
165c28749e9Skais (void) close(ksslfd);
166c28749e9Skais
167c28749e9Skais return (rv);
168c28749e9Skais }
169