doxify client.h
[paraslash.git] / client_common.c
1 /*
2 * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file client_common.c common functions of para_client and para_audiod */
20
21 #include "para.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "client.cmdline.h"
25 #include "crypt.h"
26 #include "rc4.h"
27 #include "net.h"
28 #include "fd.h"
29 #include "string.h"
30 #include "client.cmdline.h"
31 #include "client.h"
32 #include "error.h"
33
34 void rc4_send(unsigned long len, const unsigned char *indata,
35 unsigned char *outdata, void *private_data)
36 {
37 struct private_client_data *pcd = private_data;
38 RC4(&pcd->rc4_send_key, len, indata, outdata);
39 }
40
41 void rc4_recv(unsigned long len, const unsigned char *indata,
42 unsigned char *outdata, void *private_data)
43 {
44 struct private_client_data *pcd = private_data;
45 RC4(&pcd->rc4_recv_key, len, indata, outdata);
46 }
47
48
49 void client_close(struct private_client_data *pcd)
50 {
51 if (pcd)
52 return;
53 if (pcd->fd >= 0)
54 close(pcd->fd);
55 free(pcd->user);
56 free(pcd->config_file);
57 free(pcd->key_file);
58 free(pcd);
59 }
60
61 int client_parse_config(int argc, char *argv[],
62 struct private_client_data **pcd_ptr)
63 {
64 char *home = para_homedir();
65 struct stat statbuf;
66 int ret;
67 struct private_client_data *pcd =
68 para_calloc(sizeof(struct private_client_data));
69
70 pcd->fd = -1;
71 client_cmdline_parser(argc, argv, &pcd->conf);
72 ret = - E_CLIENT_SYNTAX;
73 if (!pcd->conf.inputs_num)
74 goto out;
75 pcd->user = pcd->conf.user_given?
76 para_strdup(pcd->conf.user_arg) : para_logname();
77
78 pcd->key_file = pcd->conf.key_file_given?
79 para_strdup(pcd->conf.key_file_arg) :
80 make_message("%s/.paraslash/key.%s", home, pcd->user);
81
82 pcd->config_file = pcd->conf.config_file_given?
83 para_strdup(pcd->conf.config_file_arg) :
84 make_message("%s/.paraslash/client.conf", home);
85 ret = stat(pcd->config_file, &statbuf);
86 if (ret && pcd->conf.config_file_given) {
87 ret = -E_NO_CONFIG;
88 goto out;
89 }
90 if (!ret)
91 client_cmdline_parser_configfile(pcd->config_file,
92 &pcd->conf, 0, 0, 0);
93 ret = 1;
94 *pcd_ptr = pcd;
95 PARA_INFO_LOG("loglevel: %d\n", pcd->conf.loglevel_arg);
96 PARA_INFO_LOG("config_file: %s\n", pcd->config_file);
97 PARA_INFO_LOG("key_file: %s\n", pcd->key_file);
98 PARA_NOTICE_LOG("connecting %s:%d\n", pcd->conf.hostname_arg,
99 pcd->conf.server_port_arg);
100 out:
101 free(home);
102 if (ret < 0)
103 client_close(pcd);
104 return ret;
105 }
106
107 void client_pre_select(struct sched *s, struct task *t)
108 {
109 struct private_client_data *pcd = t->private_data;
110
111 t->ret = 1;
112 pcd->check_r = 0;
113 pcd->check_w = 0;
114 if (pcd->fd < 0)
115 return;
116 switch (pcd->status) {
117 case CL_CONNECTED:
118 case CL_SENT_AUTH:
119 case CL_SENT_CH_RESPONSE:
120 case CL_SENT_COMMAND:
121 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
122 pcd->check_r = 1;
123 return;
124
125 case CL_RECEIVED_WELCOME:
126 case CL_RECEIVED_CHALLENGE:
127 case CL_RECEIVED_PROCEED:
128 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
129 pcd->check_w = 1;
130 return;
131
132 case CL_RECEIVING:
133 if (pcd->loaded < CLIENT_BUFSIZE - 1) {
134 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
135 pcd->check_r = 1;
136 }
137 return;
138 case CL_SENDING:
139 if (*pcd->in_loaded) {
140 PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
141 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
142 pcd->check_w = 1;
143 } else {
144 if (*pcd->in_eof) {
145 t->ret = -E_INPUT_EOF;
146 s->timeout.tv_sec = 0;
147 s->timeout.tv_usec = 1;
148 }
149 }
150 return;
151 }
152 }
153
154 static ssize_t client_recv_buffer(struct private_client_data *pcd)
155 {
156 ssize_t ret = recv_buffer(pcd->fd, pcd->buf + pcd->loaded,
157 CLIENT_BUFSIZE - pcd->loaded);
158 if (!ret)
159 return -E_SERVER_EOF;
160 if (ret > 0)
161 pcd->loaded += ret;
162 return ret;
163
164 }
165
166 void client_post_select(struct sched *s, struct task *t)
167 {
168 struct private_client_data *pcd = t->private_data;
169
170 // PARA_INFO_LOG("status %d\n", pcd->status);
171 t->ret = 1;
172 if (pcd->fd < 0)
173 return;
174 if (!pcd->check_r && !pcd->check_w)
175 return;
176 if (pcd->check_r && !FD_ISSET(pcd->fd, &s->rfds))
177 return;
178 if (pcd->check_w && !FD_ISSET(pcd->fd, &s->wfds))
179 return;
180 switch (pcd->status) {
181 case CL_CONNECTED: /* receive welcome message */
182 t->ret = client_recv_buffer(pcd);
183 if (t->ret > 0)
184 pcd->status = CL_RECEIVED_WELCOME;
185 return;
186 case CL_RECEIVED_WELCOME: /* send auth command */
187 sprintf(pcd->buf, "auth %s%s", pcd->conf.plain_given?
188 "" : "rc4 ", pcd->user);
189 PARA_INFO_LOG("--> %s\n", pcd->buf);
190 t->ret = send_buffer(pcd->fd, pcd->buf);
191 if (t->ret >= 0)
192 pcd->status = CL_SENT_AUTH;
193 return;
194 case CL_SENT_AUTH: /* receive challenge number */
195 pcd->loaded = 0;
196 t->ret = client_recv_buffer(pcd);
197 if (t->ret < 0)
198 return;
199 if (t->ret != 64) {
200 t->ret = -E_INVALID_CHALLENGE;
201 PARA_ERROR_LOG("received the following: %s\n", pcd->buf);
202 return;
203 }
204 PARA_INFO_LOG("%s", "<-- [challenge]\n");
205 /* decrypt challenge number */
206 t->ret = para_decrypt_challenge(pcd->key_file, &pcd->challenge_nr,
207 (unsigned char *) pcd->buf, 64);
208 if (t->ret > 0)
209 pcd->status = CL_RECEIVED_CHALLENGE;
210 return;
211 case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
212 PARA_INFO_LOG("--> %lu\n", pcd->challenge_nr);
213 t->ret = send_va_buffer(pcd->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
214 pcd->challenge_nr);
215 if (t->ret > 0)
216 pcd->status = CL_SENT_CH_RESPONSE;
217 return;
218 case CL_SENT_CH_RESPONSE: /* read server response */
219 {
220 size_t bytes_received;
221 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
222 pcd->loaded = 0;
223 t->ret = client_recv_buffer(pcd);
224 if (t->ret < 0)
225 return;
226 bytes_received = t->ret;
227 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
228 "info ++++\n", pcd->buf);
229 /* check if server has sent "Proceed" message */
230 t->ret = -E_CLIENT_AUTH;
231 if (!strstr(pcd->buf, PROCEED_MSG))
232 return;
233 t->ret = 1;
234 pcd->status = CL_RECEIVED_PROCEED;
235 if (bytes_received < PROCEED_MSG_LEN + 32)
236 return;
237 PARA_INFO_LOG("%s", "decrypting session key\n");
238 t->ret = para_decrypt_buffer(pcd->key_file, rc4_buf,
239 (unsigned char *)pcd->buf + PROCEED_MSG_LEN + 1,
240 bytes_received - PROCEED_MSG_LEN - 1);
241 if (t->ret < 0)
242 return;
243 RC4_set_key(&pcd->rc4_send_key, RC4_KEY_LEN, rc4_buf);
244 RC4_set_key(&pcd->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
245 enable_crypt(pcd->fd, rc4_recv, rc4_send, pcd);
246 }
247 case CL_RECEIVED_PROCEED: /* concat args and send command */
248 {
249 int i;
250 char *command = NULL;
251 for (i = 0; i < pcd->conf.inputs_num; i++) {
252 char *tmp = command;
253 command = make_message("%s\n%s", command?
254 command : "", pcd->conf.inputs[i]);
255 free(tmp);
256 }
257 command = para_strcat(command, EOC_MSG "\n");
258 PARA_DEBUG_LOG("--> %s\n", command);
259 t->ret = send_buffer(pcd->fd, command);
260 free(command);
261 if (t->ret > 0)
262 pcd->status = CL_SENT_COMMAND;
263 return;
264 }
265 case CL_SENT_COMMAND:
266 pcd->loaded = 0;
267 t->ret = client_recv_buffer(pcd);
268 if (t->ret < 0)
269 return;
270 t->ret = -E_HANDSHAKE_COMPLETE;
271 if (strstr(pcd->buf, AWAITING_DATA_MSG))
272 pcd->status = CL_SENDING;
273 else
274 pcd->status = CL_RECEIVING;
275 return;
276 case CL_SENDING: /* FIXME: might block */
277 PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
278 t->ret = send_bin_buffer(pcd->fd, pcd->inbuf, *pcd->in_loaded);
279 if (t->ret <= 0) {
280 if (!t->ret)
281 t->ret = 1;
282 return;
283 }
284 *pcd->in_loaded = 0; /* FIXME: short writes */
285 return;
286 case CL_RECEIVING:
287 t->ret = client_recv_buffer(pcd);
288 return;
289 }
290
291 }
292
293 int client_open(struct private_client_data *pcd)
294 {
295 int ret;
296 struct hostent *he;
297 struct sockaddr_in their_addr;
298
299 ret = get_host_info(pcd->conf.hostname_arg, &he);
300 if (ret < 0)
301 goto out;
302 /* get new socket */
303 ret = get_socket();
304 if (ret < 0)
305 goto out;
306 pcd->fd = ret;
307 /* init their_addr */
308 init_sockaddr(&their_addr, pcd->conf.server_port_arg, he);
309 ret = para_connect(pcd->fd, &their_addr);
310 if (ret < 0)
311 goto out;
312 pcd->status = CL_CONNECTED;
313 ret = mark_fd_nonblock(pcd->fd);
314 if (ret < 0)
315 goto out;
316 pcd->task.pre_select = client_pre_select;
317 pcd->task.post_select = client_post_select;
318 pcd->task.private_data = pcd;
319 sprintf(pcd->task.status, "client");
320 register_task(&pcd->task);
321 ret = 1;
322 out:
323 return ret;
324 }