]> git.tuebingen.mpg.de Git - paraslash.git/blob - client_common.c
a3a471aedaab1f79a3f3091bf61572a716248598
[paraslash.git] / client_common.c
1 /*
2  * Copyright (C) 1997-2013 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file client_common.c Common functions of para_client and para_audiod. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "client.cmdline.h"
17 #include "crypt.h"
18 #include "net.h"
19 #include "fd.h"
20 #include "sideband.h"
21 #include "string.h"
22 #include "client.cmdline.h"
23 #include "client.h"
24 #include "buffer_tree.h"
25 #include "version.h"
26
27 /** The size of the receiving buffer. */
28 #define CLIENT_BUFSIZE 4000
29
30 /**
31  * Close the connection to para_server and deallocate per-command ressources.
32  *
33  * \param ct The client task.
34  *
35  * This frees all ressources of the current command but keeps the configuration
36  * in \p ct->conf.
37  *
38  * \sa \ref client_close().
39  */
40 void client_disconnect(struct client_task *ct)
41 {
42         if (!ct)
43                 return;
44         if (ct->scc.fd >= 0)
45                 close(ct->scc.fd);
46         free_argv(ct->features);
47         ct->features = NULL;
48         sc_free(ct->scc.recv);
49         ct->scc.recv = NULL;
50         sc_free(ct->scc.send);
51         ct->scc.send = NULL;
52         btr_remove_node(&ct->btrn);
53 }
54
55 /**
56  * Close the connection to para_server and free all resources.
57  *
58  * \param ct Pointer to the client data.
59  *
60  * \sa \ref client_open(), \ref client_disconnect().
61  */
62 void client_close(struct client_task *ct)
63 {
64         if (!ct)
65                 return;
66         client_disconnect(ct);
67         free(ct->user);
68         free(ct->config_file);
69         free(ct->key_file);
70         client_cmdline_parser_free(&ct->conf);
71         free(ct->challenge_hash);
72         sb_free(ct->sbc);
73         free(ct);
74 }
75
76 /**
77  * The preselect hook for server commands.
78  *
79  * \param s Pointer to the scheduler.
80  * \param t Pointer to the task struct for this command.
81  *
82  * The task pointer must contain a pointer to the initialized client data
83  * structure as it is returned by client_open().
84  *
85  * This function checks the state of the connection and adds the file descriptor
86  * of the connection to the read or write fd set of \a s accordingly.
87  *
88  * \sa register_task() client_open(), struct sched, struct task.
89  */
90 static void client_pre_select(struct sched *s, struct task *t)
91 {
92         int ret;
93         struct client_task *ct = container_of(t, struct client_task, task);
94         struct btr_node *btrn = ct->btrn;
95
96         if (ct->scc.fd < 0)
97                 return;
98         switch (ct->status) {
99         case CL_CONNECTED:
100         case CL_SENT_AUTH:
101         case CL_SENT_CH_RESPONSE:
102         case CL_SENT_COMMAND:
103                 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
104                 return;
105
106         case CL_RECEIVED_WELCOME:
107         case CL_RECEIVED_PROCEED:
108         case CL_RECEIVED_CHALLENGE:
109                 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
110                 return;
111
112         case CL_RECEIVING:
113                 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
114                 if (ret != 0) {
115                         if (ret < 0)
116                                 sched_min_delay(s);
117                         else
118                                 para_fd_set(ct->scc.fd, &s->rfds,
119                                         &s->max_fileno);
120                 }
121                 return;
122         case CL_SENDING:
123                 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
124                 if (ret != 0) {
125                         if (ret < 0)
126                                 sched_min_delay(s);
127                         else
128                                 para_fd_set(ct->scc.fd, &s->wfds,
129                                         &s->max_fileno);
130                 }
131                 return;
132         }
133 }
134
135 static int client_recv_buffer(struct client_task *ct, fd_set *rfds,
136                 char *buf, size_t sz, size_t *n)
137 {
138         int ret;
139
140         if (ct->status < CL_SENT_CH_RESPONSE)
141                 return read_nonblock(ct->scc.fd, buf, sz, rfds, n);
142
143         *n = 0;
144         ret = sc_recv_buffer(&ct->scc, buf, sz);
145         /*
146          * sc_recv_buffer is used with blocking fds elsewhere, so it
147          * does not use the nonblock-API. Therefore we need to
148          * check for EOF and EAGAIN.
149          */
150         if (ret == 0)
151                 return -E_SERVER_EOF;
152         if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
153                 return 0;
154         if (ret < 0)
155                 return ret;
156         *n = ret;
157         return 0;
158 }
159
160 static int send_sb(struct client_task *ct, void *buf, size_t numbytes,
161                 enum sb_designator band, bool dont_free)
162 {
163         int ret, fd = ct->scc.fd;
164         struct iovec iov[2];
165
166         if (!ct->sbc) {
167                 struct sb_buffer sbb;
168                 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
169                         NULL : sc_trafo;
170                 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
171                 ct->sbc = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
172         }
173         ret = sb_get_send_buffers(ct->sbc, iov);
174         ret = xwritev(fd, iov, ret);
175         if (ret < 0) {
176                 sb_free(ct->sbc);
177                 ct->sbc = NULL;
178                 return ret;
179         }
180         if (sb_sent(ct->sbc, ret)) {
181                 ct->sbc = NULL;
182                 return 1;
183         }
184         return 0;
185 }
186
187 static int recv_sb(struct client_task *ct, fd_set *rfds,
188                 struct sb_buffer *result)
189 {
190         int ret;
191         size_t n;
192         sb_transformation trafo;
193         void *trafo_context;
194         struct iovec iov;
195
196         if (!FD_ISSET(ct->scc.fd, rfds))
197                 return 0;
198         if (ct->status < CL_SENT_CH_RESPONSE)
199                 trafo = trafo_context = NULL;
200         else {
201                 trafo = sc_trafo;
202                 trafo_context = ct->scc.recv;
203         }
204         if (!ct->sbc)
205                 ct->sbc = sb_new_recv(0, trafo, trafo_context);
206 again:
207         sb_get_recv_buffer(ct->sbc, &iov);
208         ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
209         if (ret < 0) {
210                 sb_free(ct->sbc);
211                 ct->sbc = NULL;
212                 return ret;
213         }
214         if (n == 0)
215                 return 0;
216         if (!sb_received(ct->sbc, n, result))
217                 goto again;
218         ct->sbc = NULL;
219         return 1;
220 }
221
222
223 static char **parse_features(char *buf)
224 {
225         int i;
226         const char id[] = "\nFeatures: ";
227         char *p, *q, **features;
228
229         p = strstr(buf, id);
230         if (!p)
231                 return NULL;
232         p += strlen(id);
233         q = strchr(p, '\n');
234         if (!q)
235                 return NULL;
236         *q = '\0';
237         create_argv(p, ",", &features);
238         for (i = 0; features[i]; i++)
239                 PARA_INFO_LOG("server feature: %s\n", features[i]);
240         return features;
241 }
242
243 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
244 {
245         int ret;
246         const char *designator[] = {SB_DESIGNATORS_ARRAY};
247
248         if (!sbb)
249                 return 0;
250         if (sbb->band < NUM_SB_DESIGNATORS)
251                 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
252
253         switch (sbb->band) {
254         case SBD_OUTPUT:
255                 if (iov_valid(&sbb->iov))
256                         btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
257                                 ct->btrn);
258                 ret = 1;
259                 goto out;
260         case SBD_DEBUG_LOG:
261         case SBD_INFO_LOG:
262         case SBD_NOTICE_LOG:
263         case SBD_WARNING_LOG:
264         case SBD_ERROR_LOG:
265         case SBD_CRIT_LOG:
266         case SBD_EMERG_LOG:
267                 if (iov_valid(&sbb->iov)) {
268                         int ll = sbb->band - SBD_DEBUG_LOG;
269                         para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
270                 }
271                 ret = 1;
272                 goto deallocate;
273         case SBD_EXIT__SUCCESS:
274                 ret = -E_SERVER_CMD_SUCCESS;
275                 goto deallocate;
276         case SBD_EXIT__FAILURE:
277                 ret = -E_SERVER_CMD_FAILURE;
278                 goto deallocate;
279         default:
280                 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
281                 ret = -E_BAD_BAND;
282                 goto deallocate;
283         }
284 deallocate:
285         free(sbb->iov.iov_base);
286 out:
287         sbb->iov.iov_base = NULL;
288         return ret;
289 }
290
291 static bool has_feature(const char *feature, struct client_task *ct)
292 {
293         return find_arg(feature, ct->features) >= 0? true : false;
294 }
295
296 static int send_sb_command(struct client_task *ct)
297 {
298         int i;
299         char *command, *p;
300         size_t len = 0;
301
302         if (ct->sbc)
303                 return send_sb(ct, NULL, 0, 0, false);
304
305         for (i = 0; i < ct->conf.inputs_num; i++)
306                 len += strlen(ct->conf.inputs[i]) + 1;
307         p = command = para_malloc(len);
308         for (i = 0; i < ct->conf.inputs_num; i++) {
309                 strcpy(p, ct->conf.inputs[i]);
310                 p += strlen(ct->conf.inputs[i]) + 1;
311         }
312         PARA_DEBUG_LOG("--> %s\n", command);
313         return send_sb(ct, command, len, SBD_COMMAND, false);
314 }
315
316 /**
317  * The post select hook for client commands.
318  *
319  * \param s Pointer to the scheduler.
320  * \param t Pointer to the task struct for this command.
321  *
322  * Depending on the current state of the connection and the status of the read
323  * and write fd sets of \a s, this function performs the necessary steps to
324  * authenticate the connection, to send the command given by \a t->private_data
325  * and to receive para_server's output, if any.
326  *
327  * \sa struct sched, struct task.
328  */
329 static void client_post_select(struct sched *s, struct task *t)
330 {
331         struct client_task *ct = container_of(t, struct client_task, task);
332         struct btr_node *btrn = ct->btrn;
333         int ret = 0;
334         size_t n;
335         char buf[CLIENT_BUFSIZE];
336
337         ret = task_get_notification(t);
338         if (ret < 0)
339                 goto out;
340         if (ct->scc.fd < 0)
341                 return;
342         switch (ct->status) {
343         case CL_CONNECTED: /* receive welcome message */
344                 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
345                 if (ret < 0 || n == 0)
346                         goto out;
347                 ct->features = parse_features(buf);
348                 if (!has_feature("sideband", ct)) {
349                         PARA_ERROR_LOG("server has no sideband support\n");
350                         ret = -E_INCOMPAT_FEAT;
351                         goto out;
352                 }
353                 ct->status = CL_RECEIVED_WELCOME;
354                 return;
355         case CL_RECEIVED_WELCOME: /* send auth command */
356                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
357                         return;
358                 if (has_feature("sideband", ct)) {
359                         ct->use_sideband = true;
360                         sprintf(buf, AUTH_REQUEST_MSG "%s sideband", ct->user);
361                 } else
362                         sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
363                 PARA_INFO_LOG("--> %s\n", buf);
364                 ret = write_buffer(ct->scc.fd, buf);
365                 if (ret < 0)
366                         goto out;
367                 ct->status = CL_SENT_AUTH;
368                 return;
369         case CL_SENT_AUTH:
370                 /*
371                  * Receive challenge and session keys, decrypt the challenge and
372                  * send back the hash of the decrypted challenge.
373                  */
374                 {
375                 /* decrypted challenge/session key buffer */
376                 unsigned char crypt_buf[1024];
377                 /* the SHA1 of the decrypted challenge */
378
379                 if (ct->use_sideband) {
380                         struct sb_buffer sbb;
381                         ret = recv_sb(ct, &s->rfds, &sbb);
382                         if (ret <= 0)
383                                 goto out;
384                         if (sbb.band != SBD_CHALLENGE) {
385                                 ret = -E_BAD_BAND;
386                                 free(sbb.iov.iov_base);
387                                         goto out;
388                         }
389                         n = sbb.iov.iov_len;
390                         PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
391                         ret = priv_decrypt(ct->key_file, crypt_buf,
392                                 sbb.iov.iov_base, n);
393                         free(sbb.iov.iov_base);
394                         if (ret < 0)
395                                 goto out;
396                 } else {
397                         ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
398                         if (ret < 0 || n == 0)
399                                 goto out;
400                         PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
401                         ret = priv_decrypt(ct->key_file, crypt_buf,
402                                 (unsigned char *)buf, n);
403                         if (ret < 0)
404                                 goto out;
405                 }
406                 ct->challenge_hash = para_malloc(HASH_SIZE);
407                 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
408                 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
409                 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
410                         SESSION_KEY_LEN);
411                 hash_to_asc(ct->challenge_hash, buf);
412                 PARA_INFO_LOG("--> %s\n", buf);
413                 ct->status = CL_RECEIVED_CHALLENGE;
414                 return;
415                 }
416         case CL_RECEIVED_CHALLENGE:
417                 if (ct->use_sideband) {
418                         ret = send_sb(ct, ct->challenge_hash, HASH_SIZE,
419                                 SBD_CHALLENGE_RESPONSE, false);
420                         if (ret != 0)
421                                 ct->challenge_hash = NULL;
422                         if (ret <= 0)
423                                 goto out;
424                 } else {
425                         ret = write_all(ct->scc.fd, (char *)ct->challenge_hash, HASH_SIZE);
426                         if (ret < 0)
427                                 goto out;
428                 }
429                 ct->status = CL_SENT_CH_RESPONSE;
430                 goto out;
431         case CL_SENT_CH_RESPONSE: /* read server response */
432                 {
433                 if (ct->use_sideband) {
434                         struct sb_buffer sbb;
435                         ret = recv_sb(ct, &s->rfds, &sbb);
436                         if (ret <= 0)
437                                 goto out;
438                         free(sbb.iov.iov_base);
439                         if (sbb.band != SBD_PROCEED)
440                                 ret = -E_BAD_BAND;
441                         else
442                                 ct->status = CL_RECEIVED_PROCEED;
443                         goto out;
444                 }
445                 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
446                 if (ret < 0 || n == 0)
447                         goto out;
448                 /* check if server has sent "Proceed" message */
449                 ret = -E_CLIENT_AUTH;
450                 if (n < PROCEED_MSG_LEN)
451                         goto out;
452                 if (!strstr(buf, PROCEED_MSG))
453                         goto out;
454                 ct->status = CL_RECEIVED_PROCEED;
455                 return;
456                 }
457         case CL_RECEIVED_PROCEED: /* concat args and send command */
458                 {
459                 int i;
460                 char *command = NULL;
461                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
462                         return;
463                 if (ct->use_sideband) {
464                         ret = send_sb_command(ct);
465                         if (ret <= 0)
466                                 goto out;
467                         ct->status = CL_SENT_COMMAND;
468                         return;
469                 }
470                 for (i = 0; i < ct->conf.inputs_num; i++) {
471                         char *tmp = command;
472                         command = make_message("%s\n%s", command?
473                                 command : "", ct->conf.inputs[i]);
474                         free(tmp);
475                 }
476                 command = para_strcat(command, EOC_MSG "\n");
477                 PARA_DEBUG_LOG("--> %s\n", command);
478                 ret = sc_send_buffer(&ct->scc, command);
479                 free(command);
480                 if (ret < 0)
481                         goto out;
482                 ct->status = CL_SENT_COMMAND;
483                 return;
484                 }
485         case CL_SENT_COMMAND:
486                 {
487                 char *buf2;
488                 if (ct->use_sideband) {
489                         struct sb_buffer sbb;
490                         ret = recv_sb(ct, &s->rfds, &sbb);
491                         if (ret <= 0)
492                                 goto out;
493                         if (sbb.band == SBD_AWAITING_DATA) {
494                                 ct->status = CL_SENDING;
495                                 free(sbb.iov.iov_base);
496                                 goto out;
497                         }
498                         ct->status = CL_RECEIVING;
499                         ret = dispatch_sbb(ct, &sbb);
500                         goto out;
501                 }
502                 /* can not use "buf" here because we need a malloced buffer */
503                 buf2 = para_malloc(CLIENT_BUFSIZE);
504                 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
505                 if (n > 0) {
506                         if (strstr(buf2, AWAITING_DATA_MSG)) {
507                                 free(buf2);
508                                 ct->status = CL_SENDING;
509                                 return;
510                         }
511                         ct->status = CL_RECEIVING;
512                         btr_add_output(buf2, n, btrn);
513                 } else
514                         free(buf2);
515                 goto out;
516                 }
517         case CL_SENDING:
518                 {
519                 char *buf2;
520                 size_t sz;
521                 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
522                 if (ret < 0)
523                         goto out;
524                 if (ret == 0)
525                         return;
526                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
527                         return;
528                 sz = btr_next_buffer(btrn, &buf2);
529                 ret = sc_send_bin_buffer(&ct->scc, buf2, sz);
530                 if (ret < 0)
531                         goto out;
532                 btr_consume(btrn, sz);
533                 return;
534                 }
535         case CL_RECEIVING:
536                 {
537                 char *buf2;
538                 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
539                 if (ret < 0)
540                         goto out;
541                 if (ret == 0)
542                         return;
543                 /*
544                  * The FD_ISSET() is not strictly necessary, but is allows us
545                  * to skip the malloc below if there is nothing to read anyway.
546                  */
547                 if (!FD_ISSET(ct->scc.fd, &s->rfds))
548                         return;
549                 if (ct->use_sideband) {
550                         struct sb_buffer sbb;
551                         ret = recv_sb(ct, &s->rfds, &sbb);
552                         if (ret > 0)
553                                 ret = dispatch_sbb(ct, &sbb);
554                         goto out;
555                 }
556                 buf2 = para_malloc(CLIENT_BUFSIZE);
557                 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
558                 if (n > 0) {
559                         buf2 = para_realloc(buf2, n);
560                         btr_add_output(buf2, n, btrn);
561                 } else
562                         free(buf2);
563                 goto out;
564                 }
565         }
566 out:
567         t->error = ret;
568         if (ret < 0) {
569                 if (!ct->use_sideband && ret != -E_SERVER_EOF &&
570                                 ret != -E_BTR_EOF && ret != -E_EOF)
571                         PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
572                 btr_remove_node(&ct->btrn);
573         }
574 }
575
576 /**
577  * Connect to para_server and register the client task.
578  *
579  * \param ct The initialized client task structure.
580  * \param s The scheduler instance to register the client task to.
581  * \param parent The parent node of the client btr node.
582  * \param child The child node of the client node.
583  *
584  * The client task structure given by \a ct  must be allocated and initialized
585  * by \ref client_parse_config() before this function is called.
586  *
587  * \return Standard.
588  */
589 int client_connect(struct client_task *ct, struct sched *s,
590                 struct btr_node *parent, struct btr_node *child)
591 {
592         int ret;
593
594         PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
595                 ct->conf.server_port_arg);
596         ct->scc.fd = -1;
597         ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
598                                                ct->conf.server_port_arg);
599         if (ret < 0)
600                 return ret;
601         ct->scc.fd = ret;
602         ret = mark_fd_nonblocking(ct->scc.fd);
603         if (ret < 0)
604                 goto err_out;
605         ct->status = CL_CONNECTED;
606         ct->btrn = btr_new_node(&(struct btr_node_description)
607                 EMBRACE(.name = "client", .parent = parent, .child = child));
608         ct->task.pre_select = client_pre_select;
609         ct->task.post_select = client_post_select;
610         ct->task.error = 0;
611         sprintf(ct->task.status, "client");
612         register_task(s, &ct->task);
613         return 1;
614 err_out:
615         close(ct->scc.fd);
616         ct->scc.fd = -1;
617         return ret;
618 }
619
620 /**
621  * Parse a client configuration.
622  *
623  * \param argc Usual argument count.
624  * \param argv Usual argument vector.
625  * \param ct_ptr Filled in by this function.
626  * \param loglevel If not \p NULL, the number of the loglevel is stored here.
627  *
628  * This checks the command line options given by \a argc and \a argv, sets
629  * default values for the user name and the name of the rsa key file and reads
630  * further options from the config file.
631  *
632  * Upon successful return, \a ct_ptr points to a dynamically allocated and
633  * initialized client task struct.
634  *
635  * \return The number of non-option arguments in \a argc/argv on success,
636  * negative on errors.
637  */
638 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
639                 int *loglevel)
640 {
641         char *home = para_homedir();
642         int ret;
643         struct client_task *ct = para_calloc(sizeof(struct client_task));
644
645         *ct_ptr = ct;
646         ct->scc.fd = -1;
647         ret = -E_CLIENT_SYNTAX;
648         if (client_cmdline_parser(argc, argv, &ct->conf))
649                 goto out;
650         HANDLE_VERSION_FLAG("client", ct->conf);
651
652         ct->config_file = ct->conf.config_file_given?
653                 para_strdup(ct->conf.config_file_arg) :
654                 make_message("%s/.paraslash/client.conf", home);
655         ret = file_exists(ct->config_file);
656         if (!ret && ct->conf.config_file_given) {
657                 ret = -E_NO_CONFIG;
658                 goto out;
659         }
660         if (ret) {
661                 struct client_cmdline_parser_params params = {
662                         .override = 0,
663                         .initialize = 0,
664                         .check_required = 0,
665                         .check_ambiguity = 0,
666                         .print_errors = 0
667                 };
668                 ret = -E_BAD_CONFIG;
669                 if (client_cmdline_parser_config_file(ct->config_file,
670                         &ct->conf, &params))
671                         goto out;
672         }
673         ct->user = ct->conf.user_given?
674                 para_strdup(ct->conf.user_arg) : para_logname();
675
676         if (ct->conf.key_file_given)
677                 ct->key_file = para_strdup(ct->conf.key_file_arg);
678         else {
679                 ct->key_file = make_message("%s/.paraslash/key.%s",
680                         home, ct->user);
681                 if (!file_exists(ct->key_file)) {
682                         free(ct->key_file);
683                         ct->key_file = make_message("%s/.ssh/id_rsa", home);
684                 }
685         }
686
687         if (loglevel)
688                 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
689         PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
690         PARA_INFO_LOG("config_file: %s\n", ct->config_file);
691         PARA_INFO_LOG("key_file: %s\n", ct->key_file);
692         ret = ct->conf.inputs_num;
693 out:
694         free(home);
695         if (ret < 0) {
696                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
697                 client_close(ct);
698                 *ct_ptr = NULL;
699         }
700         return ret;
701 }
702
703 /**
704  * Parse the client configuration and open a connection to para_server.
705  *
706  * \param argc See \ref client_parse_config.
707  * \param argv See \ref client_parse_config.
708  * \param ct_ptr See \ref client_parse_config.
709  * \param loglevel See \ref client_parse_config.
710  * \param parent See \ref client_connect().
711  * \param child See \ref client_connect().
712  * \param sched See \ref client_connect().
713  *
714  * This function combines client_parse_config() and client_connect(). It is
715  * considered a syntax error if no command was given, i.e. if the number
716  * of non-option arguments is zero.
717  *
718  * \return Standard.
719  */
720 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
721                 int *loglevel, struct btr_node *parent, struct btr_node *child,
722                 struct sched *sched)
723 {
724         int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
725
726         if (ret < 0)
727                 return ret;
728         if (ret == 0) {
729                 ret = -E_CLIENT_SYNTAX;
730                 goto fail;
731         }
732         ret = client_connect(*ct_ptr, sched, parent, child);
733         if (ret < 0)
734                 goto fail;
735         return 1;
736 fail:
737         client_close(*ct_ptr);
738         *ct_ptr = NULL;
739         return ret;
740 }