Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <netdb.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <syslog.h>
28 #include <tls.h>
29 #include <unistd.h>
31 #include <readline/readline.h>
32 #include <readline/history.h>
34 #include "9pclib.h"
35 #include "kamid.h"
36 #include "utils.h"
37 #include "log.h"
39 /* flags */
40 int tls;
41 const char *crtpath;
42 const char *keypath;
44 /* state */
45 struct tls_config *tlsconf;
46 struct tls *ctx;
47 int sock;
49 #define PWDFID 0
51 static void ATTR_DEAD
52 usage(int ret)
53 {
54 fprintf(stderr, "usage: %s [-c] host[:port] [path]\n",
55 getprogname());
56 fprintf(stderr, PACKAGE_NAME " suite version " PACKAGE VERSION "\n");
57 exit(ret);
58 }
60 static void
61 do_version(void)
62 {
63 tversion(VERSION9P, MSIZE9P);
64 /* TODO: get reply */
65 }
67 static void
68 do_attach(const char *path)
69 {
70 if (path == NULL)
71 path = "/";
73 /* TODO: do attach */
74 }
76 static void
77 do_connect(const char *connspec, const char *path)
78 {
79 int handshake;
80 char *host, *colon;
81 const char *port;
83 host = xstrdup(connspec);
84 if ((colon = strchr(host, ':')) != NULL) {
85 *colon = '\0';
86 port = ++colon;
87 } else
88 port = "1337";
90 if (!tls)
91 fatalx("non-tls mode is not supported");
93 if ((tlsconf = tls_config_new()) == NULL)
94 fatalx("tls_config_new");
95 tls_config_insecure_noverifycert(tlsconf);
96 tls_config_insecure_noverifyname(tlsconf);
97 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
98 fatalx("can't load certs (%s, %s)", crtpath, keypath);
100 if ((ctx = tls_client()) == NULL)
101 fatal("tls_client");
102 if (tls_configure(ctx, tlsconf) == -1)
103 fatalx("tls_configure: %s", tls_error(ctx));
105 printf("connecting to %s:%s...", host, port);
106 fflush(stdout);
108 if (tls_connect(ctx, host, port) == -1)
109 fatalx("can't connect to %s:%s: %s", host, port,
110 tls_error(ctx));
112 for (handshake = 0; !handshake;) {
113 switch (tls_handshake(ctx)) {
114 case -1:
115 fatalx("tls_handshake: %s", tls_error(ctx));
116 case 0:
117 handshake = 1;
118 break;
122 printf(" done!\n");
124 do_version();
125 do_attach(path);
127 free(host);
130 int
131 main(int argc, char **argv)
133 int ch;
135 log_init(1, LOG_DAEMON);
136 log_setverbose(1);
137 log_procinit(getprogname());
139 while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
140 switch (ch) {
141 case 'C':
142 crtpath = optarg;
143 break;
144 case 'c':
145 tls = 1;
146 break;
147 case 'K':
148 keypath = optarg;
149 break;
150 default:
151 usage(1);
154 argc -= optind;
155 argv += optind;
157 if (argc == 0)
158 usage(1);
160 if ((evb = evbuffer_new()) == NULL)
161 fatal("evbuffer_new");
163 do_connect(argv[0], argv[1]);
165 for (;;) {
166 char *line;
168 if ((line = readline("ftp> ")) == NULL)
169 break;
170 /* XXX: trim spaces */
171 if (*line == '\0')
172 continue;
173 add_history(line);
176 printf("\n");