-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpu.c
248 lines (215 loc) · 4.81 KB
/
cpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#define OPENSSL_API_COMPAT 0x10100000L
#include <openssl/ssl.h>
#include <u.h>
#include <args.h>
#include <libc.h>
#include <auth.h>
#include <authsrv.h>
#include <libsec.h>
#include "fncs.h"
char *argv0;
char *authserver;
static char *user, *pass, *askpass;
char *shell[] = {"rc", "-i"};
SSL_CTX *ssl_ctx;
SSL *ssl_conn;
//callback needs access to ai returned from p9any
static AuthInfo *ai;
static uint
psk_client_cb(SSL *ssl, const char *hint, char *identity, uint max_iden_len, uchar *psk, uint max_psk_len)
{
uint nsecret = ai->nsecret;
char i[] = "p9secret";
if(max_iden_len < sizeof i || max_psk_len < ai->nsecret)
sysfatal("psk buffers are too small");
memcpy(identity, i, sizeof i);
memcpy(psk, ai->secret, ai->nsecret);
memset(ai, 0, sizeof *ai);
return nsecret;
}
static int
p9authtls(int fd)
{
ai = p9any(user, pass, fd);
if(ai == nil)
sysfatal("can't authenticate");
memset(pass, 0, strlen(pass));
if(SSL_set_fd(ssl_conn, fd) == 0)
sysfatal("set fd failed");
if(SSL_connect(ssl_conn) < 0)
sysfatal("ssl could not connect");
return fd;
}
static void
doaskpass(void)
{
int p[2];
if(pipe(p) < 0)
sysfatal("pipe failed");
switch(fork()){
case -1:
sysfatal("fork");
case 0:
close(p[0]);
dup2(p[1], 1);
execlp("sh", "sh", "-c", askpass, nil);
sysfatal("failed to exec askpass");
break;
default:
close(p[1]);
pass = mallocz(1024, 1);
int n = read(p[0], pass, 1024);
if(n <= 1)
sysfatal("askpass gave empty password");
pass[n-1] = 0;
break;
}
}
//clean exit signal handler
void suicide(int num) { exit(0); }
typedef ssize_t (*iofunc)(int, void*, size_t);
ssize_t tls_send(int f, void *b, size_t n) { return SSL_write(ssl_conn, b, n); }
ssize_t tls_recv(int f, void *b, size_t n) { return SSL_read(ssl_conn, b, n); }
ssize_t s_send(int f, void *b, size_t n) { return write(f, b, n); }
ssize_t s_recv(int f, void *b, size_t n) { return read(f, b, n); }
void
xfer(int from, int to, iofunc recvf, iofunc sendf)
{
char buf[12*1024];
ssize_t n;
while((n = recvf(from, buf, sizeof buf)) > 0 && sendf(to, buf, n) == n)
;
}
void
usage(void)
{
fprint(2, "Usage: %s [ -Rb ] [ -u user ] [ -h host ] [ -a authserver ] -p port cmd...\n", argv0);
exits("usage");
}
int
main(int argc, char **argv)
{
int Rflag;
int bflag;
int fd;
char buf2[1024];
char buf[1024];
size_t n;
char *port;
char *host;
int pin[2];
int pout[2];
int infd, outfd;
int i;
pid_t xferc;
xferc = 0;
Rflag = bflag = 0;
infd = 0;
outfd = 1;
user = getenv("USER");
host = getenv("CPU");
authserver = getenv("AUTH");
pass = getenv("PASS");
port = nil;
ARGBEGIN {
case 'u': user = EARGF(usage()); break;
case 'h': host = EARGF(usage()); break;
case 'a': authserver = EARGF(usage()); break;
case 'p': port = EARGF(usage()); break;
case 'R': Rflag++; break;
case 'b': bflag++; break;
default: usage(); break;
} ARGEND
if(Rflag)
port = "17019";
if(user == nil || host == nil || port == nil)
usage();
if(pass == nil){
if((askpass = getenv("TLSCLIENT_ASKPASS")) != nil)
doaskpass();
else
pass = getpass("password:");
}
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ssl_ctx = SSL_CTX_new(TLSv1_2_client_method());
SSL_CTX_set_psk_client_callback(ssl_ctx, psk_client_cb);
#if OPENSSL_VERSION_MAJOR==3
/* 9front support for RFC 5746 is not guranteed but we never do renegotiation anyway... */
SSL_CTX_set_options(ssl_ctx, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
#endif
if(ssl_ctx == nil)
sysfatal("could not init openssl");
ssl_conn = SSL_new(ssl_ctx);
if(ssl_conn == nil)
sysfatal("could not init openssl");
if(*argv && !Rflag){
if(pipe(pin) < 0 || pipe(pout) < 0)
sysfatal("pipe");
switch(fork()){
case -1:
sysfatal("fork");
case 0:
close(pin[1]);
close(pout[0]);
dup2(pin[0], 0);
dup2(pout[1], 1);
execvp(argv[0], argv);
sysfatal("exec");
}
close(pout[1]);
close(pin[0]);
infd = pout[0];
outfd = pin[1];
}
fd = unix_dial(host, port);
if(fd < 0)
sysfatal("failed to connect to the client");
p9authtls(fd);
if(Rflag){
if(*argv == nil){
argv = shell;
argc = nelem(shell);
}
for(i=0,n=0; i<argc; i++)
n += snprint(buf+n, sizeof buf - n - 1, "%s ", argv[i]);
if(n <= 0)
usage();
buf[n-1] = '\n';
buf[n] = '\0';
i = strlen(buf);
snprint(buf2, sizeof buf2, "%7d\n", i);
tls_send(-1, buf2, strlen(buf2));
tls_send(-1, buf, i);
}
if(bflag){
switch(fork()){
case -1:
sysfatal("fork");
case 0:
break;
default:
return 0;
}
}
signal(SIGUSR1, suicide);
switch((xferc = fork())){
case -1:
sysfatal("fork");
case 0:
xferc = getppid();
xfer(infd, -1, s_recv, tls_send);
break;
default:
xfer(-1, outfd, tls_recv, s_send);
break;
}
kill(xferc, SIGUSR1);
}