summaryrefslogtreecommitdiff
path: root/client_common.c
blob: cf77acd81beda8b79c1df5b607626c57975b3506 (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
 *
 * Licensed under the GPL v2. For licencing details see COPYING.
 */

/** \file client_common.c Common functions of para_client and para_audiod. */

#include <sys/types.h>
#include <dirent.h>

#include "para.h"
#include "error.h"
#include "list.h"
#include "sched.h"
#include "client.cmdline.h"
#include "crypt.h"
#include "rc4.h"
#include "net.h"
#include "fd.h"
#include "string.h"
#include "client.cmdline.h"
#include "client.h"

/*
 * Rc4-encrypt data before sending.
 *
 * \param len The number of bytes to encrypt.
 * \param indata Pointer to the input data of length \a len to be encrypted.
 * \param outdata Result-pointer that holds the encrypted data.
 * \param private_data Contains the rc4 key.
 */
static void rc4_send(unsigned long len, const unsigned char *indata,
		unsigned char *outdata, void *private_data)
{
	struct client_task *ct = private_data;
	RC4(&ct->rc4_send_key, len, indata, outdata);
}

/*
 * Rc4-decrypt received data.
 *
 * Parameters are identical to those of rc4_send.
 */
static void rc4_recv(unsigned long len, const unsigned char *indata,
		unsigned char *outdata, void *private_data)
{
	struct client_task *ct = private_data;
	RC4(&ct->rc4_recv_key, len, indata, outdata);
}

/**
 * Close the connection to para_server and free all resources.
 *
 * \param ct Pointer to the client data.
 *
 * \sa client_open.
 */
void client_close(struct client_task *ct)
{
	if (!ct)
		return;
	if (ct->fd >= 0) {
		disable_crypt(ct->fd);
		close(ct->fd);
	}
	free(ct->buf);
	free(ct->user);
	free(ct->config_file);
	free(ct->key_file);
	client_cmdline_parser_free(&ct->conf);
	free(ct);
}

/**
 * The preselect hook for server commands.
 *
 * \param s Pointer to the scheduler.
 * \param t Pointer to the task struct for this command.
 *
 * The task pointer must contain a pointer to the initialized client data
 * structure as it is returned by client_open().
 *
 * This function checks the state of the connection and adds the file descriptor
 * of the connection to the read or write fd set of \a s accordingly.
 *
 * \sa register_task() client_open(), struct sched, struct task.
 */
static void client_pre_select(struct sched *s, struct task *t)
{
	struct client_task *ct = container_of(t, struct client_task, task);

	ct->check_r = 0;
	ct->check_w = 0;
	if (ct->fd < 0)
		return;
	switch (ct->status) {
	case CL_CONNECTED:
	case CL_SENT_AUTH:
	case CL_SENT_CH_RESPONSE:
	case CL_SENT_COMMAND:
		para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
		ct->check_r = 1;
		return;

	case CL_RECEIVED_WELCOME:
	case CL_RECEIVED_CHALLENGE:
	case CL_RECEIVED_PROCEED:
		para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
		ct->check_w = 1;
		return;

	case CL_RECEIVING:
		if (ct->loaded < CLIENT_BUFSIZE - 1) {
			para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
			ct->check_r = 1;
		}
		return;
	case CL_SENDING:
		if (!ct->in_loaded) /* stdin task not yet started */
			return;
		if (*ct->in_loaded) {
			PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
			para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
			ct->check_w = 1;
		} else {
			if (*ct->in_error) {
				t->error = *ct->in_error;
				s->timeout.tv_sec = 0;
				s->timeout.tv_usec = 1;
			}
		}
		return;
	}
}

static ssize_t client_recv_buffer(struct client_task *ct)
{
	ssize_t ret = recv_buffer(ct->fd, ct->buf + ct->loaded,
		CLIENT_BUFSIZE - ct->loaded);
	if (!ret)
		return -E_SERVER_EOF;
	if (ret > 0)
		ct->loaded += ret;
	return ret;

}

/**
 * The post select hook for client commands.
 *
 * \param s Pointer to the scheduler.
 * \param t Pointer to the task struct for this command.
 *
 * Depending on the current state of the connection and the status of the read
 * and write fd sets of \a s, this function performs the necessary steps to
 * authenticate the connection, to send the command given by \a t->private_data
 * and to receive para_server's output, if any.
 *
 * \sa struct sched, struct task.
 */
static void client_post_select(struct sched *s, struct task *t)
{
	struct client_task *ct = container_of(t, struct client_task, task);

	t->error = 0;
	if (ct->fd < 0)
		return;
	if (!ct->check_r && !ct->check_w)
		return;
	if (ct->check_r && !FD_ISSET(ct->fd, &s->rfds))
		return;
	if (ct->check_w && !FD_ISSET(ct->fd, &s->wfds))
		return;
	switch (ct->status) {
	case CL_CONNECTED: /* receive welcome message */
		t->error = client_recv_buffer(ct);
		if (t->error > 0)
			ct->status = CL_RECEIVED_WELCOME;
		return;
	case CL_RECEIVED_WELCOME: /* send auth command */
		sprintf(ct->buf, "auth %s%s", ct->conf.plain_given?
			"" : "rc4 ", ct->user);
		PARA_INFO_LOG("--> %s\n", ct->buf);
		t->error = send_buffer(ct->fd, ct->buf);
		if (t->error >= 0)
			ct->status = CL_SENT_AUTH;
		return;
	case CL_SENT_AUTH: /* receive challenge number */
		ct->loaded = 0;
		t->error = client_recv_buffer(ct);
		if (t->error < 0)
			return;
		if (t->error < 64) {
			t->error = -E_INVALID_CHALLENGE;
			PARA_ERROR_LOG("received the following: %s\n", ct->buf);
			return;
		}
		PARA_INFO_LOG("<-- [challenge] (%d bytes)\n", t->error);
		/* decrypt challenge number */
		t->error = para_decrypt_challenge(ct->key_file, &ct->challenge_nr,
			(unsigned char *) ct->buf, t->error);
		if (t->error > 0)
			ct->status = CL_RECEIVED_CHALLENGE;
		return;
	case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
		PARA_INFO_LOG("--> %lu\n", ct->challenge_nr);
		t->error = send_va_buffer(ct->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
			ct->challenge_nr);
		if (t->error > 0)
			ct->status = CL_SENT_CH_RESPONSE;
		return;
	case CL_SENT_CH_RESPONSE: /* read server response */
		{
		size_t bytes_received;
		unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
		ct->loaded = 0;
		t->error = client_recv_buffer(ct);
		if (t->error < 0)
			return;
		bytes_received = t->error;
		PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
			"info ++++\n", ct->buf);
		/* check if server has sent "Proceed" message */
		t->error = -E_CLIENT_AUTH;
		if (!strstr(ct->buf, PROCEED_MSG))
			return;
		t->error = 0;
		ct->status = CL_RECEIVED_PROCEED;
		if (bytes_received < PROCEED_MSG_LEN + 32)
			return;
		PARA_INFO_LOG("decrypting session key\n");
		t->error = para_decrypt_buffer(ct->key_file, rc4_buf,
			(unsigned char *)ct->buf + PROCEED_MSG_LEN + 1,
			bytes_received - PROCEED_MSG_LEN - 1);
		if (t->error < 0)
			return;
		RC4_set_key(&ct->rc4_send_key, RC4_KEY_LEN, rc4_buf);
		RC4_set_key(&ct->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
		enable_crypt(ct->fd, rc4_recv, rc4_send, ct);
		}
	case CL_RECEIVED_PROCEED: /* concat args and send command */
		{
		int i;
		char *command = NULL;
		for (i = 0; i < ct->conf.inputs_num; i++) {
			char *tmp = command;
			command = make_message("%s\n%s", command?
				command : "", ct->conf.inputs[i]);
			free(tmp);
		}
		command = para_strcat(command, EOC_MSG "\n");
		PARA_DEBUG_LOG("--> %s\n", command);
		t->error = send_buffer(ct->fd, command);
		free(command);
		if (t->error > 0)
			ct->status = CL_SENT_COMMAND;
		return;
		}
	case CL_SENT_COMMAND:
		ct->loaded = 0;
		t->error = client_recv_buffer(ct);
		if (t->error < 0)
			return;
		if (strstr(ct->buf, AWAITING_DATA_MSG))
			ct->status = CL_SENDING;
		else
			ct->status = CL_RECEIVING;
		return;
	case CL_SENDING: /* FIXME: might block */
		PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
		t->error = send_bin_buffer(ct->fd, ct->inbuf, *ct->in_loaded);
		if (t->error < 0)
			return;
		*ct->in_loaded = 0;
		return;
	case CL_RECEIVING:
		t->error = client_recv_buffer(ct);
		return;
	}
}

/* connect to para_server and register the client task */
static int client_connect(struct client_task *ct)
{
	int ret;

	ct->fd = -1;
	ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, ct->conf.hostname_arg,
		ct->conf.server_port_arg);
	if (ret < 0)
		return ret;
	ct->fd = ret;
	ct->status = CL_CONNECTED;
	ret = mark_fd_nonblocking(ct->fd);
	if (ret < 0)
		goto err_out;
	ct->task.pre_select = client_pre_select;
	ct->task.post_select = client_post_select;
	sprintf(ct->task.status, "client");
	register_task(&ct->task);
	return 1;
err_out:
	close(ct->fd);
	ct->fd = -1;
	return ret;
}

/**
 * Open connection to para_server.
 *
 * \param argc Usual argument count.
 * \param argv Usual argument vector.
 * \param ct_ptr Points to dynamically allocated and initialized client task
 * struct upon successful return.
 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
 *
 * Check the command line options given by \a argc and argv, set default values
 * for user name and rsa key file, read further option from the config file.
 * Finally, establish a connection to para_server.
 *
 * \return Standard.
 */
int client_open(int argc, char *argv[], struct client_task **ct_ptr,
		int *loglevel)
{
	char *home = para_homedir();
	int ret;
	struct client_task *ct = para_calloc(sizeof(struct client_task));

	ct->buf = para_malloc(CLIENT_BUFSIZE);
	*ct_ptr = ct;
	ct->fd = -1;
	ret = -E_CLIENT_SYNTAX;
	if (client_cmdline_parser(argc, argv, &ct->conf))
		goto out;
	HANDLE_VERSION_FLAG("client", ct->conf);
	ret = -E_CLIENT_SYNTAX;
	if (!ct->conf.inputs_num)
		goto out;
	ct->user = ct->conf.user_given?
		para_strdup(ct->conf.user_arg) : para_logname();

	ct->key_file = ct->conf.key_file_given?
		para_strdup(ct->conf.key_file_arg) :
		make_message("%s/.paraslash/key.%s", home, ct->user);

	ct->config_file = ct->conf.config_file_given?
		para_strdup(ct->conf.config_file_arg) :
		make_message("%s/.paraslash/client.conf", home);
	ret = file_exists(ct->config_file);
	if (!ret && ct->conf.config_file_given) {
		ret = -E_NO_CONFIG;
		goto out;
	}
	if (ret) {
		struct client_cmdline_parser_params params = {
			.override = 0,
			.initialize = 0,
			.check_required = 0,
			.check_ambiguity = 0,
			.print_errors = 0
		};
		ret = -E_BAD_CONFIG;
		if (client_cmdline_parser_config_file(ct->config_file,
			&ct->conf, &params))
			goto out;
	}
	if (loglevel)
		*loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
	PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
	PARA_INFO_LOG("config_file: %s\n", ct->config_file);
	PARA_INFO_LOG("key_file: %s\n", ct->key_file);
	PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
		ct->conf.server_port_arg);
	ret = client_connect(ct);
out:
	free(home);
	if (ret < 0) {
		PARA_ERROR_LOG("%s\n", para_strerror(-ret));
		client_close(ct);
		*ct_ptr = NULL;
	}
	return ret;
}