Blame


1 ce3844d2 2021-12-14 op /*
2 ce3844d2 2021-12-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 ce3844d2 2021-12-14 op *
4 ce3844d2 2021-12-14 op * Permission to use, copy, modify, and distribute this software for any
5 ce3844d2 2021-12-14 op * purpose with or without fee is hereby granted, provided that the above
6 ce3844d2 2021-12-14 op * copyright notice and this permission notice appear in all copies.
7 ce3844d2 2021-12-14 op *
8 ce3844d2 2021-12-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ce3844d2 2021-12-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ce3844d2 2021-12-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ce3844d2 2021-12-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ce3844d2 2021-12-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ce3844d2 2021-12-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ce3844d2 2021-12-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 ce3844d2 2021-12-14 op */
16 ce3844d2 2021-12-14 op
17 ce3844d2 2021-12-14 op #include "compat.h"
18 ce3844d2 2021-12-14 op
19 ce3844d2 2021-12-14 op #include <sys/types.h>
20 ce3844d2 2021-12-14 op #include <sys/socket.h>
21 ce3844d2 2021-12-14 op
22 ce3844d2 2021-12-14 op #include <netdb.h>
23 ce3844d2 2021-12-14 op #include <limits.h>
24 ce3844d2 2021-12-14 op #include <stdio.h>
25 ce3844d2 2021-12-14 op #include <stdlib.h>
26 ce3844d2 2021-12-14 op #include <string.h>
27 ce3844d2 2021-12-14 op #include <syslog.h>
28 ce3844d2 2021-12-14 op #include <tls.h>
29 ce3844d2 2021-12-14 op #include <unistd.h>
30 ce3844d2 2021-12-14 op
31 688f54f0 2021-12-14 op #if HAVE_READLINE
32 ce3844d2 2021-12-14 op #include <readline/readline.h>
33 ce3844d2 2021-12-14 op #include <readline/history.h>
34 688f54f0 2021-12-14 op #endif
35 ce3844d2 2021-12-14 op
36 ce3844d2 2021-12-14 op #include "9pclib.h"
37 ce3844d2 2021-12-14 op #include "kamid.h"
38 ce3844d2 2021-12-14 op #include "utils.h"
39 ce3844d2 2021-12-14 op #include "log.h"
40 ce3844d2 2021-12-14 op
41 ce3844d2 2021-12-14 op /* flags */
42 ce3844d2 2021-12-14 op int tls;
43 ce3844d2 2021-12-14 op const char *crtpath;
44 ce3844d2 2021-12-14 op const char *keypath;
45 ce3844d2 2021-12-14 op
46 ce3844d2 2021-12-14 op /* state */
47 ce3844d2 2021-12-14 op struct tls_config *tlsconf;
48 ce3844d2 2021-12-14 op struct tls *ctx;
49 ce3844d2 2021-12-14 op int sock;
50 ce3844d2 2021-12-14 op
51 ce3844d2 2021-12-14 op #define PWDFID 0
52 ce3844d2 2021-12-14 op
53 688f54f0 2021-12-14 op #if HAVE_READLINE
54 688f54f0 2021-12-14 op static char *
55 688f54f0 2021-12-14 op read_line(const char *prompt)
56 688f54f0 2021-12-14 op {
57 688f54f0 2021-12-14 op char *line;
58 688f54f0 2021-12-14 op
59 688f54f0 2021-12-14 op again:
60 688f54f0 2021-12-14 op if ((line = readline(prompt)) == NULL)
61 688f54f0 2021-12-14 op return NULL;
62 688f54f0 2021-12-14 op /* XXX: trim spaces? */
63 688f54f0 2021-12-14 op if (*line == '\0') {
64 688f54f0 2021-12-14 op free(line);
65 688f54f0 2021-12-14 op goto again;
66 688f54f0 2021-12-14 op }
67 688f54f0 2021-12-14 op
68 688f54f0 2021-12-14 op add_history(line);
69 688f54f0 2021-12-14 op return line;
70 688f54f0 2021-12-14 op }
71 688f54f0 2021-12-14 op #else
72 688f54f0 2021-12-14 op static char *
73 688f54f0 2021-12-14 op read_line(const char *prompt)
74 688f54f0 2021-12-14 op {
75 688f54f0 2021-12-14 op char *ch, *line = NULL;
76 688f54f0 2021-12-14 op size_t linesize = 0;
77 688f54f0 2021-12-14 op ssize_t linelen;
78 688f54f0 2021-12-14 op
79 688f54f0 2021-12-14 op linelen = getline(&line, &linesize, stdin);
80 688f54f0 2021-12-14 op if (linelen == -1)
81 688f54f0 2021-12-14 op return NULL;
82 688f54f0 2021-12-14 op
83 688f54f0 2021-12-14 op if ((ch = strchr(line, '\n')) != NULL)
84 688f54f0 2021-12-14 op *ch = '\0';
85 688f54f0 2021-12-14 op return line;
86 688f54f0 2021-12-14 op }
87 688f54f0 2021-12-14 op #endif
88 688f54f0 2021-12-14 op
89 ce3844d2 2021-12-14 op static void ATTR_DEAD
90 ce3844d2 2021-12-14 op usage(int ret)
91 ce3844d2 2021-12-14 op {
92 ce3844d2 2021-12-14 op fprintf(stderr, "usage: %s [-c] host[:port] [path]\n",
93 ce3844d2 2021-12-14 op getprogname());
94 ce3844d2 2021-12-14 op fprintf(stderr, PACKAGE_NAME " suite version " PACKAGE VERSION "\n");
95 ce3844d2 2021-12-14 op exit(ret);
96 ce3844d2 2021-12-14 op }
97 ce3844d2 2021-12-14 op
98 ce3844d2 2021-12-14 op static void
99 ce3844d2 2021-12-14 op do_version(void)
100 ce3844d2 2021-12-14 op {
101 ce3844d2 2021-12-14 op tversion(VERSION9P, MSIZE9P);
102 ce3844d2 2021-12-14 op /* TODO: get reply */
103 ce3844d2 2021-12-14 op }
104 ce3844d2 2021-12-14 op
105 ce3844d2 2021-12-14 op static void
106 ce3844d2 2021-12-14 op do_attach(const char *path)
107 ce3844d2 2021-12-14 op {
108 ce3844d2 2021-12-14 op if (path == NULL)
109 ce3844d2 2021-12-14 op path = "/";
110 ce3844d2 2021-12-14 op
111 ce3844d2 2021-12-14 op /* TODO: do attach */
112 ce3844d2 2021-12-14 op }
113 ce3844d2 2021-12-14 op
114 ce3844d2 2021-12-14 op static void
115 ce3844d2 2021-12-14 op do_connect(const char *connspec, const char *path)
116 ce3844d2 2021-12-14 op {
117 ce3844d2 2021-12-14 op int handshake;
118 ce3844d2 2021-12-14 op char *host, *colon;
119 ce3844d2 2021-12-14 op const char *port;
120 ce3844d2 2021-12-14 op
121 ce3844d2 2021-12-14 op host = xstrdup(connspec);
122 ce3844d2 2021-12-14 op if ((colon = strchr(host, ':')) != NULL) {
123 ce3844d2 2021-12-14 op *colon = '\0';
124 ce3844d2 2021-12-14 op port = ++colon;
125 ce3844d2 2021-12-14 op } else
126 ce3844d2 2021-12-14 op port = "1337";
127 ce3844d2 2021-12-14 op
128 ce3844d2 2021-12-14 op if (!tls)
129 ce3844d2 2021-12-14 op fatalx("non-tls mode is not supported");
130 ce3844d2 2021-12-14 op
131 ce3844d2 2021-12-14 op if ((tlsconf = tls_config_new()) == NULL)
132 ce3844d2 2021-12-14 op fatalx("tls_config_new");
133 ce3844d2 2021-12-14 op tls_config_insecure_noverifycert(tlsconf);
134 ce3844d2 2021-12-14 op tls_config_insecure_noverifyname(tlsconf);
135 ce3844d2 2021-12-14 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
136 ce3844d2 2021-12-14 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
137 ce3844d2 2021-12-14 op
138 ce3844d2 2021-12-14 op if ((ctx = tls_client()) == NULL)
139 ce3844d2 2021-12-14 op fatal("tls_client");
140 ce3844d2 2021-12-14 op if (tls_configure(ctx, tlsconf) == -1)
141 ce3844d2 2021-12-14 op fatalx("tls_configure: %s", tls_error(ctx));
142 ce3844d2 2021-12-14 op
143 ce3844d2 2021-12-14 op printf("connecting to %s:%s...", host, port);
144 ce3844d2 2021-12-14 op fflush(stdout);
145 ce3844d2 2021-12-14 op
146 ce3844d2 2021-12-14 op if (tls_connect(ctx, host, port) == -1)
147 ce3844d2 2021-12-14 op fatalx("can't connect to %s:%s: %s", host, port,
148 ce3844d2 2021-12-14 op tls_error(ctx));
149 ce3844d2 2021-12-14 op
150 ce3844d2 2021-12-14 op for (handshake = 0; !handshake;) {
151 ce3844d2 2021-12-14 op switch (tls_handshake(ctx)) {
152 ce3844d2 2021-12-14 op case -1:
153 ce3844d2 2021-12-14 op fatalx("tls_handshake: %s", tls_error(ctx));
154 ce3844d2 2021-12-14 op case 0:
155 ce3844d2 2021-12-14 op handshake = 1;
156 ce3844d2 2021-12-14 op break;
157 ce3844d2 2021-12-14 op }
158 ce3844d2 2021-12-14 op }
159 ce3844d2 2021-12-14 op
160 ce3844d2 2021-12-14 op printf(" done!\n");
161 ce3844d2 2021-12-14 op
162 ce3844d2 2021-12-14 op do_version();
163 ce3844d2 2021-12-14 op do_attach(path);
164 ce3844d2 2021-12-14 op
165 ce3844d2 2021-12-14 op free(host);
166 ce3844d2 2021-12-14 op }
167 ce3844d2 2021-12-14 op
168 ce3844d2 2021-12-14 op int
169 ce3844d2 2021-12-14 op main(int argc, char **argv)
170 ce3844d2 2021-12-14 op {
171 ce3844d2 2021-12-14 op int ch;
172 ce3844d2 2021-12-14 op
173 ce3844d2 2021-12-14 op log_init(1, LOG_DAEMON);
174 ce3844d2 2021-12-14 op log_setverbose(1);
175 ce3844d2 2021-12-14 op log_procinit(getprogname());
176 ce3844d2 2021-12-14 op
177 ce3844d2 2021-12-14 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
178 ce3844d2 2021-12-14 op switch (ch) {
179 ce3844d2 2021-12-14 op case 'C':
180 ce3844d2 2021-12-14 op crtpath = optarg;
181 ce3844d2 2021-12-14 op break;
182 ce3844d2 2021-12-14 op case 'c':
183 ce3844d2 2021-12-14 op tls = 1;
184 ce3844d2 2021-12-14 op break;
185 ce3844d2 2021-12-14 op case 'K':
186 ce3844d2 2021-12-14 op keypath = optarg;
187 ce3844d2 2021-12-14 op break;
188 ce3844d2 2021-12-14 op default:
189 ce3844d2 2021-12-14 op usage(1);
190 ce3844d2 2021-12-14 op }
191 ce3844d2 2021-12-14 op }
192 ce3844d2 2021-12-14 op argc -= optind;
193 ce3844d2 2021-12-14 op argv += optind;
194 ce3844d2 2021-12-14 op
195 ce3844d2 2021-12-14 op if (argc == 0)
196 ce3844d2 2021-12-14 op usage(1);
197 ce3844d2 2021-12-14 op
198 ce3844d2 2021-12-14 op if ((evb = evbuffer_new()) == NULL)
199 ce3844d2 2021-12-14 op fatal("evbuffer_new");
200 ce3844d2 2021-12-14 op
201 ce3844d2 2021-12-14 op do_connect(argv[0], argv[1]);
202 ce3844d2 2021-12-14 op
203 ce3844d2 2021-12-14 op for (;;) {
204 ce3844d2 2021-12-14 op char *line;
205 ce3844d2 2021-12-14 op
206 688f54f0 2021-12-14 op if ((line = read_line("ftp> ")) == NULL)
207 ce3844d2 2021-12-14 op break;
208 688f54f0 2021-12-14 op printf("read: %s\n", line);
209 ce3844d2 2021-12-14 op }
210 ce3844d2 2021-12-14 op
211 ce3844d2 2021-12-14 op printf("\n");
212 ce3844d2 2021-12-14 op }