1 /*
2  * Copyright (C) 2014,2016 Red Hat Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *     * Redistributions of source code must retain the above
9  *       copyright notice, this list of conditions and the
10  *       following disclaimer.
11  *     * Redistributions in binary form must reproduce the
12  *       above copyright notice, this list of conditions and
13  *       the following disclaimer in the documentation and/or
14  *       other materials provided with the distribution.
15  *     * The names of contributors to this software may not be
16  *       used to endorse or promote products derived from this
17  *       software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30  * DAMAGE.
31  *
32  * Author: Stef Walter <stefw@redhat.com>
33  */
34 
35 #include "config.h"
36 
37 #include "compat.h"
38 #include "debug.h"
39 #include "iter.h"
40 #include "message.h"
41 #include "p11-kit.h"
42 #include "remote.h"
43 #include "tool.h"
44 
45 #include <assert.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #ifdef ENABLE_NLS
53 #include <libintl.h>
54 #define _(x) dgettext(PACKAGE_NAME, x)
55 #else
56 #define _(x) (x)
57 #endif
58 
59 int
main(int argc,char * argv[])60 main (int argc,
61       char *argv[])
62 {
63 	int opt;
64 	char *provider = NULL;
65 
66 	enum {
67 		opt_verbose = 'v',
68 		opt_help = 'h',
69 		opt_provider = 'p'
70 	};
71 
72 	struct option options[] = {
73 		{ "verbose", no_argument, NULL, opt_verbose },
74 		{ "help", no_argument, NULL, opt_help },
75 		{ "provider", required_argument, NULL, opt_provider },
76 		{ 0 },
77 	};
78 
79 	p11_tool_desc usages[] = {
80 		{ 0, "usage: p11-kit remote <module>\n"
81 		     "       p11-kit remote [-p <provider>] <token> ..." },
82 		{ opt_provider, "specify the module to use" },
83 		{ 0 },
84 	};
85 
86 	while ((opt = p11_tool_getopt (argc, argv, options)) != -1) {
87 		switch (opt) {
88 		case opt_verbose:
89 			p11_kit_be_loud ();
90 			break;
91 		case opt_help:
92 		case '?':
93 			p11_tool_usage (usages, options);
94 			return 0;
95 		case opt_provider:
96 			provider = optarg;
97 			break;
98 		default:
99 			assert_not_reached ();
100 			break;
101 		}
102 	}
103 
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (argc < 1) {
108 		p11_message (_("specify a module or tokens to remote"));
109 		return 2;
110 	}
111 
112 	if (isatty (0)) {
113 		p11_message (_("the 'remote' tool is not meant to be run from a terminal"));
114 		return 2;
115 	}
116 
117 	if (strncmp (argv[0], "pkcs11:", 7) == 0) {
118 		CK_FUNCTION_LIST *module = NULL;
119 		int ret;
120 
121 		if (provider) {
122 			module = p11_kit_module_load (provider, 0);
123 			if (module == NULL)
124 				return 1;
125 		}
126 
127 		ret = p11_kit_remote_serve_tokens ((const char **)argv, argc,
128 						   module,
129 						   STDIN_FILENO, STDOUT_FILENO);
130 		if (module)
131 			p11_kit_module_release (module);
132 
133 		return ret;
134 	} else {
135 		CK_FUNCTION_LIST *module;
136 		int ret;
137 
138 		if (argc != 1) {
139 			p11_message (_("only one module can be specified"));
140 			return 2;
141 		}
142 
143 		module = p11_kit_module_load (argv[0], 0);
144 		if (module == NULL)
145 			return 1;
146 
147 		ret = p11_kit_remote_serve_module (module,
148 						   STDIN_FILENO, STDOUT_FILENO);
149 		p11_kit_module_release (module);
150 
151 		return ret;
152 	}
153 }
154