Debian Codegraph @

src/istgtcontrol.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2008-2010 Daisuke Aoyama <[email protected]>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "build.h"
33
34 #include <stdint.h>
35 #include <inttypes.h>
36
37 #include <stdarg.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44
45 #include "istgt.h"
46 #include "istgt_ver.h"
47 #include "istgt_conf.h"
48 #include "istgt_sock.h"
49 #include "istgt_misc.h"
50 #include "istgt_md5.h"
51
52 //#define TRACE_UCTL
53
54 #define DEFAULT_UCTL_CONFIG BUILD_ETC_ISTGT "/istgtcontrol.conf"
55 #define DEFAULT_UCTL_TIMEOUT 60
56 #define DEFAULT_UCTL_PORT 3261
57 #define DEFAULT_UCTL_HOST "localhost"
58 #define DEFAULT_UCTL_LUN 0
59 #define DEFAULT_UCTL_MTYPE "-"
60 #define DEFAULT_UCTL_MFLAGS "ro"
61 #define DEFAULT_UCTL_MSIZE "auto"
62
63 #define MAX_LINEBUF 4096
64 #define UCTL_CHAP_CHALLENGE_LEN 1024
65
66 typedef struct istgt_uctl_auth_t {
67 char *user;
68 char *secret;
69 char *muser;
70 char *msecret;
71
72 uint8_t chap_id[1];
73 uint8_t chap_mid[1];
74 int chap_challenge_len;
75 uint8_t chap_challenge[UCTL_CHAP_CHALLENGE_LEN];
76 int chap_mchallenge_len;
77 uint8_t chap_mchallenge[UCTL_CHAP_CHALLENGE_LEN];
78 } UCTL_AUTH;
79
80 typedef struct istgt_uctl_t {
81 CONFIG *config;
82
83 char *host;
84 int port;
85
86 int sock;
87 char *iqn;
88 int lun;
89 char *mflags;
90 char *mfile;
91 char *msize;
92 char *mtype;
93
94 int family;
95 char caddr[MAX_ADDRBUF];
96 char saddr[MAX_ADDRBUF];
97
98 UCTL_AUTH auth;
99
100 int timeout;
101 int req_auth_auto;
102 int req_auth;
103 int req_auth_mutual;
104
105 int recvtmpsize;
106 int recvtmpcnt;
107 int recvtmpidx;
108 int recvbufsize;
109 int sendbufsize;
110 int worksize;
111 char recvtmp[MAX_LINEBUF];
112 char recvbuf[MAX_LINEBUF];
113 char sendbuf[MAX_LINEBUF];
114 char work[MAX_LINEBUF];
115 char *cmd;
116 char *arg;
117 } UCTL;
118 typedef UCTL *UCTL_Ptr;
119
120
121 static void
122 fatal(const char *format, ...)
123 {
124 va_list ap;
125
126 va_start(ap, format);
127 vfprintf(stderr, format, ap);
128 va_end(ap);
129 exit(EXIT_FAILURE);
130 }
131
132 typedef enum {
133 UCTL_CMD_OK = 0,
134 UCTL_CMD_ERR = 1,
135 UCTL_CMD_EOF = 2,
136 UCTL_CMD_QUIT = 3,
137 UCTL_CMD_DISCON = 4,
138 UCTL_CMD_REQAUTH = 5,
139 UCTL_CMD_CHAPSEQ = 6,
140 } UCTL_CMD_STATUS;
141
142 //#define ARGS_DELIM " \t\r\n"
143 #define ARGS_DELIM " \t"
144
145 static int
146 uctl_readline(UCTL_Ptr uctl)
147 {
148 ssize_t total;
149
150 total = istgt_readline_socket(uctl->sock, uctl->recvbuf, uctl->recvbufsize,
151 uctl->recvtmp, uctl->recvtmpsize,
152 &uctl->recvtmpidx, &uctl->recvtmpcnt,
153 uctl->timeout);
154 if (total < 0) {
155 return UCTL_CMD_DISCON;
156 }
157 if (total == 0) {
158 return UCTL_CMD_EOF;
159 }
160 return UCTL_CMD_OK;
161 }
162
163 static int
164 uctl_writeline(UCTL_Ptr uctl)
165 {
166 ssize_t total;
167 ssize_t expect;
168
169 expect = strlen(uctl->sendbuf);
170 total = istgt_writeline_socket(uctl->sock, uctl->sendbuf, uctl->timeout);
171 if (total < 0) {
172 return UCTL_CMD_DISCON;
173 }
174 if (total != expect) {
175 return UCTL_CMD_ERR;
176 }
177 return UCTL_CMD_OK;
178 }
179
180 static int
181 uctl_snprintf(UCTL_Ptr uctl, const char *format, ...)
182 {
183 va_list ap;
184 int rc;
185
186 va_start(ap, format);
187 rc = vsnprintf(uctl->sendbuf, uctl->sendbufsize, format, ap);
188 va_end(ap);
189 return rc;
190 }
191
192 static char *
193 get_banner(UCTL_Ptr uctl)
194 {
195 char *banner;
196 int rc;
197
198 rc = uctl_readline(uctl);
199 if (rc != UCTL_CMD_OK) {
200 return NULL;
201 }
202 banner = xstrdup(trim_string(uctl->recvbuf));
203 return banner;
204 }
205
206 static int
207 is_err_req_auth(UCTL_Ptr uctl, char *s)
208 {
209 const char *req_auth_string = "auth required";
210
211 #ifdef TRACE_UCTL
212 printf("S=%s, Q=%s\n", s, req_auth_string);
213 #endif /* TRCAE_UCTL */
214 if (strncasecmp(s, req_auth_string, strlen(req_auth_string)) == 0)
215 return 1;
216 return 0;
217 }
218
219 static int
220 is_err_chap_seq(UCTL_Ptr uctl, char *s)
221 {
222 const char *chap_seq_string = "CHAP sequence error";
223
224 #ifdef TRACE_UCTL
225 printf("S=%s, Q=%s\n", s, chap_seq_string);
226 #endif /* TRCAE_UCTL */
227 if (strncasecmp(s, chap_seq_string, strlen(chap_seq_string)) == 0)
228 return 1;
229 return 0;
230 }
231
232 static int
233 exec_quit(UCTL_Ptr uctl)
234 {
235 const char *delim = ARGS_DELIM;
236 char *arg;
237 char *result;
238 int rc;
239
240 /* send command */
241 uctl_snprintf(uctl, "QUIT\n");
242 rc = uctl_writeline(uctl);
243 if (rc != UCTL_CMD_OK) {
244 return rc;
245 }
246
247 /* receive result */
248 rc = uctl_readline(uctl);
249 if (rc != UCTL_CMD_OK) {
250 return rc;
251 }
252 arg = trim_string(uctl->recvbuf);
253 result = strsepq(&arg, delim);
254 strupr(result);
255 if (strcmp(result, "OK") != 0) {
256 if (is_err_req_auth(uctl, arg))
257 return UCTL_CMD_REQAUTH;
258 fprintf(stderr, "ERROR %s\n", arg);
259 return UCTL_CMD_ERR;
260 }
261 return UCTL_CMD_OK;
262 }
263
264 static int
265 exec_noop(UCTL_Ptr uctl)
266 {
267 const char *delim = ARGS_DELIM;
268 char *arg;
269 char *result;
270 int rc;
271
272 /* send command */
273 uctl_snprintf(uctl, "NOOP\n");
274 rc = uctl_writeline(uctl);
275 if (rc != UCTL_CMD_OK) {
276 return rc;
277 }
278
279 /* receive result */
280 rc = uctl_readline(uctl);
281 if (rc != UCTL_CMD_OK) {
282 return rc;
283 }
284 arg = trim_string(uctl->recvbuf);
285 result = strsepq(&arg, delim);
286 strupr(result);
287 if (strcmp(result, "OK") != 0) {
288 if (is_err_req_auth(uctl, arg))
289 return UCTL_CMD_REQAUTH;
290 fprintf(stderr, "ERROR %s\n", arg);
291 return UCTL_CMD_ERR;
292 }
293 return UCTL_CMD_OK;
294 }
295
296 static int
297 exec_version(UCTL_Ptr uctl)
298 {
299 const char *delim = ARGS_DELIM;
300 char *arg;
301 char *result;
302 char *version;
303 char *extver;
304 int rc;
305
306 /* send command */
307 uctl_snprintf(uctl, "VERSION\n");
308 rc = uctl_writeline(uctl);
309 if (rc != UCTL_CMD_OK) {
310 return rc;
311 }
312
313 /* receive result */
314 while (1) {
315 rc = uctl_readline(uctl);
316 if (rc != UCTL_CMD_OK) {
317 return rc;
318 }
319 arg = trim_string(uctl->recvbuf);
320 result = strsepq(&arg, delim);
321 strupr(result);
322 if (strcmp(result, uctl->cmd) != 0) {
323 break;
324 }
325 version = strsepq(&arg, delim);
326 extver = strsepq(&arg, delim);
327 printf("target version %s %s\n", version, extver);
328 }
329 if (strcmp(result, "OK") != 0) {
330 if (is_err_req_auth(uctl, arg))
331 return UCTL_CMD_REQAUTH;
332 fprintf(stderr, "ERROR %s\n", arg);
333 return UCTL_CMD_ERR;
334 }
335 return UCTL_CMD_OK;
336 }
337
338 static int
339 exec_unload(UCTL_Ptr uctl)
340 {
341 const char *delim = ARGS_DELIM;
342 char *arg;
343 char *result;
344 int rc;
345
346 /* send command */
347 if (uctl->iqn == NULL || uctl->lun < 0) {
348 return UCTL_CMD_ERR;
349 }
350 uctl_snprintf(uctl, "UNLOAD \"%s\" %d\n",
351 uctl->iqn, uctl->lun);
352 rc = uctl_writeline(uctl);
353 if (rc != UCTL_CMD_OK) {
354 return rc;
355 }
356
357 /* receive result */
358 rc = uctl_readline(uctl);
359 if (rc != UCTL_CMD_OK) {
360 return rc;
361 }
362 arg = trim_string(uctl->recvbuf);
363 result = strsepq(&arg, delim);
364 strupr(result);
365 if (strcmp(result, "OK") != 0) {
366 if (is_err_req_auth(uctl, arg))
367 return UCTL_CMD_REQAUTH;
368 fprintf(stderr, "ERROR %s\n", arg);
369 return UCTL_CMD_ERR;
370 }
371 return UCTL_CMD_OK;
372 }
373
374 static int
375 exec_load(UCTL_Ptr uctl)
376 {
377 const char *delim = ARGS_DELIM;
378 char *arg;
379 char *result;
380 int rc;
381
382 /* send command */
383 if (uctl->iqn == NULL || uctl->lun < 0) {
384 return UCTL_CMD_ERR;
385 }
386 uctl_snprintf(uctl, "LOAD \"%s\" %d\n",
387 uctl->iqn, uctl->lun);
388 rc = uctl_writeline(uctl);
389 if (rc != UCTL_CMD_OK) {
390 return rc;
391 }
392
393 /* receive result */
394 rc = uctl_readline(uctl);
395 if (rc != UCTL_CMD_OK) {
396 return rc;
397 }
398 arg = trim_string(uctl->recvbuf);
399 result = strsepq(&arg, delim);
400 strupr(result);
401 if (strcmp(result, "OK") != 0) {
402 if (is_err_req_auth(uctl, arg))
403 return UCTL_CMD_REQAUTH;
404 fprintf(stderr, "ERROR %s\n", arg);
405 return UCTL_CMD_ERR;
406 }
407 return UCTL_CMD_OK;
408 }
409
410 static int
411 exec_list(UCTL_Ptr uctl)
412 {
413 const char *delim = ARGS_DELIM;
414 char *arg;
415 char *result;
416 char *target;
417 int rc;
418
419 /* send command */
420 if (uctl->iqn != NULL) {
421 uctl_snprintf(uctl, "LIST \"%s\"\n", uctl->iqn);
422 } else {
423 uctl_snprintf(uctl, "LIST\n");
424 }
425 rc = uctl_writeline(uctl);
426 if (rc != UCTL_CMD_OK) {
427 return rc;
428 }
429
430 /* receive result */
431 while (1) {
432 rc = uctl_readline(uctl);
433 if (rc != UCTL_CMD_OK) {
434 return rc;
435 }
436 arg = trim_string(uctl->recvbuf);
437 result = strsepq(&arg, delim);
438 strupr(result);
439 if (strcmp(result, uctl->cmd) != 0)
440 break;
441 if (uctl->iqn != NULL) {
442 printf("%s\n", arg);
443 } else {
444 target = strsepq(&arg, delim);
445 printf("%s\n", target);
446 }
447 }
448 if (strcmp(result, "OK") != 0) {
449 if (is_err_req_auth(uctl, arg))
450 return UCTL_CMD_REQAUTH;
451 fprintf(stderr, "ERROR %s\n", arg);
452 return UCTL_CMD_ERR;
453 }
454 return UCTL_CMD_OK;
455 }
456
457 static int
458 exec_change(UCTL_Ptr uctl)
459 {
460 const char *delim = ARGS_DELIM;
461 char *arg;
462 char *result;
463 int rc;
464
465 /* send command */
466 if (uctl->iqn == NULL || uctl->mfile == NULL || uctl->mtype == NULL
467 || uctl->mflags == NULL || uctl->msize == NULL) {
468 return UCTL_CMD_ERR;
469 }
470 uctl_snprintf(uctl, "CHANGE \"%s\" %d \"%s\" "
471 "\"%s\" \"%s\" \"%s\"\n",
472 uctl->iqn, uctl->lun, uctl->mtype,
473 uctl->mflags, uctl->mfile, uctl->msize);
474 rc = uctl_writeline(uctl);
475 if (rc != UCTL_CMD_OK) {
476 return rc;
477 }
478
479 /* receive result */
480 rc = uctl_readline(uctl);
481 if (rc != UCTL_CMD_OK) {
482 return rc;
483 }
484 arg = trim_string(uctl->recvbuf);
485 result = strsepq(&arg, delim);
486 strupr(result);
487 if (strcmp(result, "OK") != 0) {
488 if (is_err_req_auth(uctl, arg))
489 return UCTL_CMD_REQAUTH;
490 fprintf(stderr, "ERROR %s\n", arg);
491 return UCTL_CMD_ERR;
492 }
493 return UCTL_CMD_OK;
494 }
495
496 static int
•497 exec_refresh(UCTL_Ptr uctl)
498 {
499 const char *delim = ARGS_DELIM;
500 char *arg;
501 char *result;
502 int rc;
503
504 /* send command */
505 uctl_snprintf(uctl, "REFRESH \n");
506 rc = uctl_writeline(uctl);
507 if (rc != UCTL_CMD_OK)
508 return rc;
509
510 /* receive result */
511 rc = uctl_readline(uctl);
512 if (rc != UCTL_CMD_OK)
513 return rc;
514 arg = trim_string(uctl->recvbuf);
515 result = strsepq(&arg, delim);
516 strupr(result);
517 if (strcmp(result, "OK") != 0) {
518 if (is_err_req_auth(uctl, arg))
519 return UCTL_CMD_REQAUTH;
520 fprintf(stderr, "ERROR %s\n", arg);
521 return UCTL_CMD_ERR;
522 }
523 return UCTL_CMD_OK;
524 }
525
526 static int
527 exec_reset(UCTL_Ptr uctl)
528 {
529 const char *delim = ARGS_DELIM;
530 char *arg;
531 char *result;
532 int rc;
533
534 /* send command */
535 if (uctl->iqn == NULL || uctl->lun < 0) {
536 return UCTL_CMD_ERR;
537 }
538 uctl_snprintf(uctl, "RESET \"%s\" %d\n",
539 uctl->iqn, uctl->lun);
540 rc = uctl_writeline(uctl);
541 if (rc != UCTL_CMD_OK) {
542 return rc;
543 }
544
545 /* receive result */
546 rc = uctl_readline(uctl);
547 if (rc != UCTL_CMD_OK) {
548 return rc;
549 }
550 arg = trim_string(uctl->recvbuf);
551 result = strsepq(&arg, delim);
552 strupr(result);
553 if (strcmp(result, "OK") != 0) {
554 if (is_err_req_auth(uctl, arg))
555 return UCTL_CMD_REQAUTH;
556 fprintf(stderr, "ERROR %s\n", arg);
557 return UCTL_CMD_ERR;
558 }
559 return UCTL_CMD_OK;
560 }
561
562 static int
563 exec_info(UCTL_Ptr uctl)
564 {
565 const char *delim = ARGS_DELIM;
566 char *arg;
567 char *result;
568 int rc;
569
570 /* send command */
571 if (uctl->iqn != NULL) {
572 uctl_snprintf(uctl, "INFO \"%s\"\n", uctl->iqn);
573 } else {
574 uctl_snprintf(uctl, "INFO\n");
575 }
576 rc = uctl_writeline(uctl);
577 if (rc != UCTL_CMD_OK) {
578 return rc;
579 }
580
581 /* receive result */
582 while (1) {
583 rc = uctl_readline(uctl);
584 if (rc != UCTL_CMD_OK) {
585 return rc;
586 }
587 arg = trim_string(uctl->recvbuf);
588 result = strsepq(&arg, delim);
589 strupr(result);
590 if (strcmp(result, uctl->cmd) != 0)
591 break;
592 if (uctl->iqn != NULL) {
593 printf("%s\n", arg);
594 } else {
595 printf("%s\n", arg);
596 }
597 }
598 if (strcmp(result, "OK") != 0) {
599 if (is_err_req_auth(uctl, arg))
600 return UCTL_CMD_REQAUTH;
601 fprintf(stderr, "ERROR %s\n", arg);
602 return UCTL_CMD_ERR;
603 }
604 return UCTL_CMD_OK;
605 }
606
607 typedef struct exec_table_t
608 {
609 const char *name;
610 int (*func) (UCTL_Ptr uctl);
611 int req_argc;
612 int req_target;
613 } EXEC_TABLE;
614
615 static EXEC_TABLE exec_table[] =
616 {
617 { "QUIT", exec_quit, 0, 0 },
618 { "NOOP", exec_noop, 0, 0 },
619 { "VERSION", exec_version, 0, 0 },
620 { "LIST", exec_list, 0, 0 },
621 { "UNLOAD", exec_unload, 0, 1 },
622 { "LOAD", exec_load, 0, 1 },
623 { "CHANGE", exec_change, 1, 1 },
•624 { "REFRESH", exec_refresh, 0, 0 },
625 { "RESET", exec_reset, 0, 1 },
626 { "INFO", exec_info, 0, 0 },
627 { NULL, NULL, 0, 0 },
628 };
629
630 static int
631 do_auth(UCTL_Ptr uctl)
632 {
633 uint8_t uctlmd5[ISTGT_MD5DIGEST_LEN];
634 uint8_t resmd5[ISTGT_MD5DIGEST_LEN];
635 ISTGT_MD5CTX md5ctx;
636 const char *delim = ARGS_DELIM;
637 char *arg;
638 char *result;
639 char *label;
640 char *chap_i;
641 char *chap_c;
642 char *chap_n;
643 char *chap_r;
644 char *hexmd5;
645 char *hexchallenge;
646 char *workp;
647 int worksize;
648 int algorithm = 5; /* CHAP with MD5 */
649 int rc;
650
651 #ifdef TRACE_UCTL
652 printf("do_auth: user=%s, secret=%s, muser=%s, msecret=%s\n",
653 uctl->auth.user,
654 uctl->auth.secret,
655 uctl->auth.muser,
656 uctl->auth.msecret);
657 #endif /* TRACE_UCTL */
658
659 /* send algorithm CHAP_A */
660 uctl_snprintf(uctl, "AUTH CHAP_A %d\n",
661 algorithm);
662 rc = uctl_writeline(uctl);
663 if (rc != UCTL_CMD_OK) {
664 return rc;
665 }
666
667 /* receive CHAP_IC */
668 rc = uctl_readline(uctl);
669 if (rc != UCTL_CMD_OK) {
670 return rc;
671 }
672 arg = trim_string(uctl->recvbuf);
673 result = strsepq(&arg, delim);
674 strupr(result);
675 if (strcmp(result, "AUTH") != 0) {
676 fprintf(stderr, "ERROR %s\n", arg);
677 return UCTL_CMD_ERR;
678 }
679
680 label = strsepq(&arg, delim);
681 chap_i = strsepq(&arg, delim);
682 chap_c = strsepq(&arg, delim);
683 if (label == NULL || chap_i == NULL || chap_c == NULL) {
684 fprintf(stderr, "CHAP sequence error\n");
685 return UCTL_CMD_ERR;
686 }
687 if (strcasecmp(label, "CHAP_IC") != 0) {
688 fprintf(stderr, "CHAP sequence error\n");
689 return UCTL_CMD_ERR;
690 }
691
692 /* Identifier */
693 uctl->auth.chap_id[0] = (uint8_t) strtol(chap_i, NULL, 10);
694 /* Challenge Value */
695 rc = istgt_hex2bin(uctl->auth.chap_challenge,
696 UCTL_CHAP_CHALLENGE_LEN,
697 chap_c);
698 if (rc < 0) {
699 fprintf(stderr, "challenge format error\n");
700 return UCTL_CMD_ERR;
701 }
702 uctl->auth.chap_challenge_len = rc;
703
704 if (uctl->auth.user == NULL || uctl->auth.secret == NULL) {
705 fprintf(stderr, "ERROR auth user or secret is missing\n");
706 return UCTL_CMD_ERR;
707 }
708
709 istgt_md5init(&md5ctx);
710 /* Identifier */
711 istgt_md5update(&md5ctx, uctl->auth.chap_id, 1);
712 /* followed by secret */
713 istgt_md5update(&md5ctx, uctl->auth.secret,
714 strlen(uctl->auth.secret));
715 /* followed by Challenge Value */
716 istgt_md5update(&md5ctx, uctl->auth.chap_challenge,
717 uctl->auth.chap_challenge_len);
718 /* uctlmd5 is Response Value */
719 istgt_md5final(uctlmd5, &md5ctx);
720
721 workp = uctl->work;
722 worksize = uctl->worksize;
723
724 istgt_bin2hex(workp, worksize,
725 uctlmd5, ISTGT_MD5DIGEST_LEN);
726 hexmd5 = workp;
727 worksize -= strlen(hexmd5) + 1;
728 workp += strlen(hexmd5) + 1;
729
730 /* mutual CHAP? */
731 if (uctl->req_auth_mutual) {
732 /* Identifier is one octet */
733 istgt_gen_random(uctl->auth.chap_mid, 1);
734 /* Challenge Value is a variable stream of octets */
735 /* (binary length MUST not exceed 1024 bytes) */
736 uctl->auth.chap_mchallenge_len = UCTL_CHAP_CHALLENGE_LEN;
737 istgt_gen_random(uctl->auth.chap_mchallenge,
738 uctl->auth.chap_mchallenge_len);
739
740 istgt_bin2hex(workp, worksize,
741 uctl->auth.chap_mchallenge,
742 uctl->auth.chap_mchallenge_len);
743 hexchallenge = workp;
744 worksize -= strlen(hexchallenge) + 1;
745 workp += strlen(hexchallenge) + 1;
746
747 /* send CHAP_NR with CHAP_IC */
748 uctl_snprintf(uctl, "AUTH CHAP_NR %s %s %d %s\n",
749 uctl->auth.user, hexmd5,
750 (int) uctl->auth.chap_mid[0], hexchallenge);
751 rc = uctl_writeline(uctl);
752 if (rc != UCTL_CMD_OK) {
753 return rc;
754 }
755
756 /* receive CHAP_NR */
757 rc = uctl_readline(uctl);
758 if (rc != UCTL_CMD_OK) {
759 return rc;
760 }
761 arg = trim_string(uctl->recvbuf);
762 result = strsepq(&arg, delim);
763 strupr(result);
764 if (strcmp(result, "AUTH") != 0) {
765 fprintf(stderr, "ERROR %s\n", arg);
766 return UCTL_CMD_ERR;
767 }
768
769 label = strsepq(&arg, delim);
770 chap_n = strsepq(&arg, delim);
771 chap_r = strsepq(&arg, delim);
772 if (label == NULL || chap_n == NULL || chap_r == NULL) {
773 fprintf(stderr, "CHAP sequence error\n");
774 return UCTL_CMD_ERR;
775 }
776 if (strcasecmp(label, "CHAP_NR") != 0) {
777 fprintf(stderr, "CHAP sequence error\n");
778 return UCTL_CMD_ERR;
779 }
780
781 rc = istgt_hex2bin(resmd5, ISTGT_MD5DIGEST_LEN, chap_r);
782 if (rc < 0 || rc != ISTGT_MD5DIGEST_LEN) {
783 fprintf(stderr, "response format error\n");
784 return UCTL_CMD_ERR;
785 }
786
787 if (uctl->auth.muser == NULL || uctl->auth.msecret == NULL) {
788 fprintf(stderr, "ERROR auth user or secret is missing\n");
789 return UCTL_CMD_ERR;
790 }
791
792 istgt_md5init(&md5ctx);
793 /* Identifier */
794 istgt_md5update(&md5ctx, uctl->auth.chap_mid, 1);
795 /* followed by secret */
796 istgt_md5update(&md5ctx, uctl->auth.msecret,
797 strlen(uctl->auth.msecret));
798 /* followed by Challenge Value */
799 istgt_md5update(&md5ctx, uctl->auth.chap_mchallenge,
800 uctl->auth.chap_mchallenge_len);
801 /* uctlmd5 is expecting Response Value */
802 istgt_md5final(uctlmd5, &md5ctx);
803
804 /* compare MD5 digest */
805 if (memcmp(uctlmd5, resmd5, ISTGT_MD5DIGEST_LEN) != 0) {
806 /* not match */
807 fprintf(stderr, "ERROR auth user or secret is missing\n");
808 /* discard result line */
809 if (rc != UCTL_CMD_OK) {
810 return rc;
811 }
812 arg = trim_string(uctl->recvbuf);
813 result = strsepq(&arg, delim);
814 strupr(result);
815 if (strcmp(result, "OK") != 0) {
816 fprintf(stderr, "ERROR %s\n", arg);
817 return UCTL_CMD_ERR;
818 }
819 /* final with ERR */
820 return UCTL_CMD_ERR;
821 }
822 } else {
823 /* not mutual */
824 /* send CHAP_NR */
825 uctl_snprintf(uctl, "AUTH CHAP_NR %s %s\n",
826 uctl->auth.user, hexmd5);
827 rc = uctl_writeline(uctl);
828 if (rc != UCTL_CMD_OK) {
829 return rc;
830 }
831 }
832
833 /* receive result */
834 rc = uctl_readline(uctl);
835 if (rc != UCTL_CMD_OK) {
836 return rc;
837 }
838 arg = trim_string(uctl->recvbuf);
839 result = strsepq(&arg, delim);
840 strupr(result);
841 if (strcmp(result, "OK") != 0) {
842 if (is_err_chap_seq(uctl, arg))
843 return UCTL_CMD_CHAPSEQ;
844 fprintf(stderr, "ERROR %s\n", arg);
845 return UCTL_CMD_ERR;
846 }
847 return UCTL_CMD_OK;
848 }
849
850 static char *
851 uctl_get_nmval(CF_SECTION *sp, const char *key, int idx1, int idx2)
852 {
853 CF_ITEM *ip;
854 CF_VALUE *vp;
855 int i;
856
857 ip = istgt_find_cf_nitem(sp, key, idx1);
858 if (ip == NULL)
859 return NULL;
860 vp = ip->val;
861 if (vp == NULL)
862 return NULL;
863 for (i = 0; vp != NULL; vp = vp->next) {
864 if (i == idx2)
865 return vp->value;
866 i++;
867 }
868 return NULL;
869 }
870
871 static char *
872 uctl_get_nval(CF_SECTION *sp, const char *key, int idx)
873 {
874 CF_ITEM *ip;
875 CF_VALUE *vp;
876
877 ip = istgt_find_cf_nitem(sp, key, idx);
878 if (ip == NULL)
879 return NULL;
880 vp = ip->val;
881 if (vp == NULL)
882 return NULL;
883 return vp->value;
884 }
885
886 static char *
887 uctl_get_val(CF_SECTION *sp, const char *key)
888 {
889 return uctl_get_nval(sp, key, 0);
890 }
891
892 static int
893 uctl_get_nintval(CF_SECTION *sp, const char *key, int idx)
894 {
895 const char *v;
896 int value;
897
898 v = uctl_get_nval(sp, key, idx);
899 if (v == NULL)
900 return -1;
901 value = (int)strtol(v, NULL, 10);
902 return value;
903 }
904
905 static int
906 uctl_get_intval(CF_SECTION *sp, const char *key)
907 {
908 return uctl_get_nintval(sp, key, 0);
909 }
910
911 static int
912 uctl_init(UCTL_Ptr uctl)
913 {
914 CF_SECTION *sp;
915 const char *val;
916 const char *user, *muser;
917 const char *secret, *msecret;
918 int timeout;
919 int port;
920 int lun;
921 int i;
922
923 sp = istgt_find_cf_section(uctl->config, "Global");
924 if (sp == NULL) {
925 fprintf(stderr, "find_cf_section failed()\n");
926 return -1;
927 }
928
929 val = uctl_get_val(sp, "Comment");
930 if (val != NULL) {
931 /* nothing */
932 #ifdef TRACE_UCTL
933 printf("Comment %s\n", val);
934 #endif /* TRACE_UCTL */
935 }
936
937 val = uctl_get_val(sp, "Host");
938 if (val == NULL) {
939 val = DEFAULT_UCTL_HOST;
940 }
941 uctl->host = xstrdup(val);
942 #ifdef TRACE_UCTL
943 printf("Host %s\n", uctl->host);
944 #endif /* TRACE_UCTL */
945
946 port = uctl_get_intval(sp, "Port");
947 if (port < 0) {
948 port = DEFAULT_UCTL_PORT;
949 }
950 uctl->port = port;
951 #ifdef TRACE_UCTL
952 printf("Port %d\n", uctl->port);
953 #endif /* TRACE_UCTL */
954
955 val = uctl_get_val(sp, "TargetName");
956 if (val == NULL) {
957 val = NULL;
958 }
959 uctl->iqn = xstrdup(val);
960 #ifdef TRACE_UCTL
961 printf("TargetName %s\n", uctl->iqn);
962 #endif /* TRACE_UCTL */
963
964 lun = uctl_get_intval(sp, "Lun");
965 if (lun < 0) {
966 lun = DEFAULT_UCTL_LUN;
967 }
968 uctl->lun = lun;
969 #ifdef TRACE_UCTL
970 printf("Lun %d\n", uctl->lun);
971 #endif /* TRACE_UCTL */
972
973 val = uctl_get_val(sp, "Flags");
974 if (val == NULL) {
975 val = DEFAULT_UCTL_MFLAGS;
976 }
977 uctl->mflags = xstrdup(val);
978 #ifdef TRACE_UCTL
979 printf("Flags %s\n", uctl->mflags);
980 #endif /* TRACE_UCTL */
981
982 val = uctl_get_val(sp, "Size");
983 if (val == NULL) {
984 val = DEFAULT_UCTL_MSIZE;
985 }
986 uctl->msize = xstrdup(val);
987 #ifdef TRACE_UCTL
988 printf("Size %s\n", uctl->msize);
989 #endif /* TRACE_UCTL */
990
991 timeout = uctl_get_intval(sp, "Timeout");
992 if (timeout < 0) {
993 timeout = DEFAULT_UCTL_TIMEOUT;
994 }
995 uctl->timeout = timeout;
996 #ifdef TRACE_UCTL
997 printf("Timeout %d\n", uctl->timeout);
998 #endif /* TRACE_UCTL */
999
1000 val = uctl_get_val(sp, "AuthMethod");
1001 if (val == NULL) {
1002 uctl->req_auth_auto = 0;
1003 uctl->req_auth = 0;
1004 } else {
1005 uctl->req_auth_auto = 0;
1006 for (i = 0; ; i++) {
1007 val = uctl_get_nmval(sp, "AuthMethod", 0, i);
1008 if (val == NULL)
1009 break;
1010 if (strcasecmp(val, "CHAP") == 0) {
1011 uctl->req_auth = 1;
1012 } else if (strcasecmp(val, "Mutual") == 0) {
1013 uctl->req_auth_mutual = 1;
1014 } else if (strcasecmp(val, "Auto") == 0) {
1015 uctl->req_auth_auto = 1;
1016 uctl->req_auth = 0;
1017 uctl->req_auth_mutual = 0;
1018 } else {
1019 fprintf(stderr, "unknown auth\n");
1020 return -1;
1021 }
1022 }
1023 if (uctl->req_auth_mutual && !uctl->req_auth) {
1024 fprintf(stderr, "Mutual but not CHAP\n");
1025 return -1;
1026 }
1027 }
1028 #ifdef TRACE_UCTL
1029 if (uctl->req_auth == 0) {
1030 printf("AuthMethod Auto\n");
1031 } else {
1032 printf("AuthMethod %s %s\n",
1033 uctl->req_auth ? "CHAP" : "",
1034 uctl->req_auth_mutual ? "Mutual" : "");
1035 }
1036 #endif /* TRACE_UCTL */
1037
1038 val = uctl_get_nval(sp, "Auth", 0);
1039 if (val == NULL) {
1040 user = secret = muser = msecret = NULL;
1041 } else {
1042 user = uctl_get_nmval(sp, "Auth", 0, 0);
1043 secret = uctl_get_nmval(sp, "Auth", 0, 1);
1044 muser = uctl_get_nmval(sp, "Auth", 0, 2);
1045 msecret = uctl_get_nmval(sp, "Auth", 0, 3);
1046 }
1047 uctl->auth.user = xstrdup(user);
1048 uctl->auth.secret = xstrdup(secret);
1049 uctl->auth.muser = xstrdup(muser);
1050 uctl->auth.msecret = xstrdup(msecret);
1051 #ifdef TRACE_UCTL
1052 printf("user=%s, secret=%s, muser=%s, msecret=%s\n",
1053 user, secret, muser, msecret);
1054 #endif /* TRACE_UCTL */
1055
1056 return 0;
1057 }
1058
1059 static void
1060 usage(void)
1061 {
1062 printf("istgtcotrol [options] <command> [<file>]\n");
1063 printf("options:\n");
1064 printf("default may be changed by configuration file\n");
1065 printf(" -c config config file (default %s)\n", DEFAULT_UCTL_CONFIG);
1066 printf(" -h host target host name or IP (default %s)\n", DEFAULT_UCTL_HOST);
1067 printf(" -p port port number (default %d)\n", DEFAULT_UCTL_PORT);
1068 printf(" -t target target iqn\n");
1069 printf(" -l lun target lun (default %d)\n", DEFAULT_UCTL_LUN);
1070 printf(" -f flags media flags (default %s)\n", DEFAULT_UCTL_MFLAGS);
1071 printf(" -s size media size (default %s)\n", DEFAULT_UCTL_MSIZE);
1072 printf(" -q quiet mode\n");
1073 printf(" -v verbose mode\n");
1074 printf(" -A method authentication method (CHAP/Mutual CHAP/Auto)\n");
1075 printf(" -U user auth user\n");
1076 printf(" -S secret auth secret\n");
1077 printf(" -M muser mutual auth user\n");
1078 printf(" -R msecret mutual auth secret\n");
1079 printf(" -H show this usage\n");
1080 printf(" -V show version\n");
1081 printf("command:\n");
1082 printf(" noop no operation\n");
1083 printf(" version show target version\n");
1084 printf(" list list all or specified target\n");
1085 printf(" load load media to specified unit\n");
1086 printf(" unload unload media from specified unit\n");
1087 printf(" change change media with <file> at specified unit\n");
•1088 printf(" refresh refresh to reload target and lun configuration\n");
1089 printf(" reset reset specified lun of target\n");
1090 printf(" info show connections of target\n");
1091 }
1092
1093 int
1094 main(int argc, char *argv[])
1095 {
1096 const char *config_file = DEFAULT_UCTL_CONFIG;
1097 CONFIG *config;
1098 UCTL xuctl, *uctl;
1099 struct sigaction sigact, sigoldact;
1100 int (*func) (UCTL_Ptr);
1101 int port = -1;
1102 int lun = -1;
1103 const char *host = NULL;
1104 const char *mflags = NULL;
1105 const char *mfile = NULL;
1106 const char *msize = NULL;
1107 const char *mtype = DEFAULT_UCTL_MTYPE;
1108 char *target = NULL;
1109 char *user = NULL;
1110 char *secret = NULL;
1111 char *muser = NULL;
1112 char *msecret = NULL;
1113 char *cmd;
1114 char *banner;
1115 long l;
1116 int exec_result;
1117 int req_argc;
1118 int req_target;
1119 int quiet = 0;
1120 int verbose = 0;
1121 int req_auth = -1;
1122 int ch;
1123 int sock;
1124 int rc;
1125 int i;
1126
1127 #ifdef HAVE_SETPROCTITLE
1128 setproctitle("version %s (%s)",
1129 ISTGT_VERSION, ISTGT_EXTRA_VERSION);
1130 #endif
1131
1132 memset(&xuctl, 0, sizeof xuctl);
1133 uctl = &xuctl;
1134
1135 while ((ch = getopt(argc, argv, "c:h:p:t:l:f:s:qvA:U:S:M:R:VH")) != -1) {
1136 switch (ch) {
1137 case 'c':
1138 config_file = optarg;
1139 break;
1140 case 'h':
1141 host = optarg;
1142 break;
1143 case 'p':
1144 l = strtol(optarg, NULL, 10);
1145 if (l < 0 || l > 65535) {
1146 fatal("invalid port %s\n", optarg);
1147 }
1148 port = (int) l;
1149 break;
1150 case 't':
1151 target = optarg;
1152 break;
1153 case 'l':
1154 l = strtol(optarg, NULL, 10);
1155 if (l < 0 || l > 0x3fffU) {
1156 fatal("invalid lun %s\n", optarg);
1157 }
1158 lun = (int) l;
1159 break;
1160 case 'f':
1161 mflags = optarg;
1162 break;
1163 case 's':
1164 msize = optarg;
1165 break;
1166 case 'q':
1167 quiet = 1;
1168 break;
1169 case 'v':
1170 verbose = 1;
1171 break;
1172 case 'A':
1173 if (strcasecmp(optarg, "CHAP") == 0) {
1174 req_auth = 1;
1175 } else if (strcasecmp(optarg, "Mutual") == 0
1176 || strcasecmp(optarg, "Mutual CHAP") == 0
1177 || strcasecmp(optarg, "CHAP Mutual") == 0) {
1178 req_auth = 2;
1179 } else if (strcasecmp(optarg, "Auto") == 0) {
1180 req_auth = 0;
1181 } else {
1182 usage();
1183 exit(EXIT_SUCCESS);
1184 }
1185 break;
1186 case 'U':
1187 user = optarg;
1188 break;
1189 case 'S':
1190 secret = optarg;
1191 #ifndef HAVE_SETPROCTITLE
1192 secret = xstrdup(optarg);
1193 memset(optarg, 'x', strlen(optarg));
1194 #endif
1195 break;
1196 case 'M':
1197 muser = optarg;
1198 break;
1199 case 'R':
1200 msecret = optarg;
1201 #ifndef HAVE_SETPROCTITLE
1202 msecret = xstrdup(optarg);
1203 memset(optarg, 'x', strlen(optarg));
1204 #endif
1205 break;
1206 case 'V':
1207 printf("istgtcontrol version %s (%s)\n",
1208 ISTGT_VERSION, ISTGT_EXTRA_VERSION);
1209 exit(EXIT_SUCCESS);
1210 case 'H':
1211 default:
1212 usage();
1213 exit(EXIT_SUCCESS);
1214 }
1215 }
1216 argc -= optind;
1217 argv += optind;
1218
1219 /* read config files */
1220 config = istgt_allocate_config();
1221 rc = istgt_read_config(config, config_file);
1222 if (rc < 0) {
1223 fprintf(stderr, "config error\n");
1224 exit(EXIT_FAILURE);
1225 }
1226 if (config->section == NULL) {
1227 fprintf(stderr, "empty config\n");
1228 istgt_free_config(config);
1229 exit(EXIT_FAILURE);
1230 }
1231 uctl->config = config;
1232 //istgt_print_config(config);
1233
1234 /* take specified command */
1235 if (argc < 1) {
1236 error_usage_return:
1237 istgt_free_config(config);
1238 usage();
1239 exit(EXIT_FAILURE);
1240 }
1241 cmd = strupr(xstrdup(argv[0]));
1242 argc--;
1243 argv++;
1244
1245 /* get function pointer and parameters for specified command */
1246 func = NULL;
1247 req_argc = -1;
1248 req_target = -1;
1249 for (i = 0; exec_table[i].name != NULL; i++) {
1250 if (cmd[0] == exec_table[i].name[0]
1251 && strcmp(cmd, exec_table[i].name) == 0) {
1252 func = exec_table[i].func;
1253 req_argc = exec_table[i].req_argc;
1254 req_target = exec_table[i].req_target;
1255 break;
1256 }
1257 }
1258 if (func == NULL) {
1259 istgt_free_config(config);
1260 fatal("unknown command %s\n", cmd);
1261 }
1262
1263 /* patrameter check */
1264 if (argc < req_argc) {
1265 goto error_usage_return;
1266 }
1267 #if 0
1268 if (req_target) {
1269 if (target == NULL) {
1270 goto error_usage_return;
1271 }
1272 }
1273 #endif
1274
1275 /* take args */
1276 if (strcmp(cmd, "CHANGE") == 0) {
1277 /* change require file */
1278 mfile = argv[0];
1279 }
1280
1281 /* build parameters */
1282 uctl_init(uctl);
1283 uctl->recvtmpcnt = 0;
1284 uctl->recvtmpidx = 0;
1285 uctl->recvtmpsize = sizeof uctl->recvtmp;
1286 uctl->recvbufsize = sizeof uctl->recvbuf;
1287 uctl->sendbufsize = sizeof uctl->sendbuf;
1288 uctl->worksize = sizeof uctl->work;
1289
1290 /* override by command line */
1291 if (user != NULL) {
1292 xfree(uctl->auth.user);
1293 uctl->auth.user = xstrdup(user);
1294 }
1295 if (secret != NULL) {
1296 xfree(uctl->auth.secret);
1297 uctl->auth.secret = xstrdup(secret);
1298 }
1299 if (muser != NULL) {
1300 xfree(uctl->auth.muser);
1301 uctl->auth.muser = xstrdup(muser);
1302 }
1303 if (msecret != NULL) {
1304 xfree(uctl->auth.msecret);
1305 uctl->auth.msecret = xstrdup(msecret);
1306 }
1307 if (req_target) {
1308 if (uctl->iqn == NULL
1309 && target == NULL) {
1310 goto error_usage_return;
1311 }
1312 }
1313 if (req_auth >= 0) {
1314 uctl->req_auth_auto = 1;
1315 uctl->req_auth = 0;
1316 uctl->req_auth_mutual = 0;
1317 if (req_auth > 1) {
1318 uctl->req_auth_auto = 0;
1319 uctl->req_auth = 1;
1320 uctl->req_auth_mutual = 1;
1321 } else if (req_auth > 0) {
1322 uctl->req_auth_auto = 0;
1323 uctl->req_auth = 1;
1324 }
1325 }
1326 #ifdef TRACE_UCTL
1327 printf("auto=%d, auth=%d, mutual=%d\n",
1328 uctl->req_auth_auto, uctl->req_auth, uctl->req_auth_mutual);
1329 #endif /* TRACE_UCTL */
1330
1331 if (host != NULL) {
1332 xfree(uctl->host);
1333 uctl->host = xstrdup(host);
1334 }
1335 if (port >= 0) {
1336 uctl->port = port;
1337 }
1338 if (target != NULL) {
1339 xfree(uctl->iqn);
1340 if (strcasecmp(target, "ALL") == 0) {
1341 uctl->iqn = NULL;
1342 } else {
1343 uctl->iqn = escape_string(target);
1344 }
1345 }
1346 if (lun >= 0) {
1347 uctl->lun = lun;
1348 }
1349 if (mflags != NULL) {
1350 xfree(uctl->mflags);
1351 uctl->mflags = escape_string(mflags);
1352 }
1353 uctl->mfile = escape_string(mfile);
1354 if (msize != NULL) {
1355 xfree(uctl->msize);
1356 uctl->msize = escape_string(msize);
1357 }
1358 uctl->mtype = escape_string(mtype);
1359 uctl->cmd = escape_string(cmd);
1360
1361 /* show setting */
1362 #define NULLP(S) ((S) == NULL ? "NULL" : (S))
1363 if (verbose) {
1364 printf("iqn=%s, lun=%d\n", NULLP(uctl->iqn), uctl->lun);
1365 printf("media file=%s, flags=%s, size=%s\n",
1366 NULLP(uctl->mfile), NULLP(uctl->mflags), NULLP(uctl->msize));
1367 }
1368
1369 /* set signals */
1370 memset(&sigact, 0, sizeof sigact);
1371 memset(&sigoldact, 0, sizeof sigoldact);
1372 sigact.sa_handler = SIG_IGN;
1373 sigemptyset(&sigact.sa_mask);
1374 if (sigaction(SIGPIPE, &sigact, &sigoldact) != 0) {
1375 istgt_free_config(config);
1376 fatal("sigaction() failed");
1377 }
1378
1379 /* connect to target */
1380 if (verbose) {
1381 printf("connect to %s:%d\n", uctl->host, uctl->port);
1382 }
1383 sock = istgt_connect(uctl->host, uctl->port);
1384 if (sock < 0) {
1385 istgt_free_config(config);
1386 fatal("istgt_connect(%s:%d) failed\n", uctl->host, uctl->port);
1387 }
1388 uctl->sock = sock;
1389
1390 /* get target banner (ready to send) */
1391 banner = get_banner(uctl);
1392 if (banner == NULL) {
1393 close(uctl->sock);
1394 istgt_free_config(config);
1395 fatal("get_banner() failed\n");
1396 }
1397 if (verbose) {
1398 printf("target banner \"%s\"\n", banner);
1399 }
1400
1401 /* authentication */
1402 retry_auth:
1403 if (uctl->req_auth) {
1404 rc = do_auth(uctl);
1405 if (rc != UCTL_CMD_OK) {
1406 if (rc == UCTL_CMD_REQAUTH
1407 || rc == UCTL_CMD_CHAPSEQ) {
1408 retry_auth_auto:
1409 /* Auth negotiation */
1410 if (uctl->req_auth == 0) {
1411 #ifdef TRCAE_UCTL
1412 printf("Auto negotiation CHAP\n");
1413 #endif /* TRCAE_UCTL */
1414 uctl->req_auth = 1;
1415 goto retry_auth;
1416 } else if (uctl->req_auth_mutual == 0) {
1417 #ifdef TRCAE_UCTL
1418 printf("Auto negotiation Mutual CHAP\n");
1419 #endif /* TRCAE_UCTL */
1420 uctl->req_auth_mutual = 1;
1421 goto retry_auth;
1422 }
1423 }
1424 if (!quiet) {
1425 printf("AUTH failed\n");
1426 }
1427 exec_result = rc;
1428 goto disconnect;
1429 }
1430 }
1431
1432 /* send specified command */
1433 rc = func(uctl);
1434 exec_result = rc;
1435 if (rc != UCTL_CMD_OK) {
1436 if (rc == UCTL_CMD_REQAUTH
1437 || rc == UCTL_CMD_CHAPSEQ) {
1438 goto retry_auth_auto;
1439 }
1440 if (!quiet) {
1441 printf("ABORT %s command\n", uctl->cmd);
1442 }
1443 } else {
1444 if (!quiet) {
1445 printf("DONE %s command\n", uctl->cmd);
1446 }
1447 }
1448
1449 /* disconnect from target */
1450 disconnect:
1451 rc = exec_quit(uctl);
1452 if (rc != UCTL_CMD_OK) {
1453 fprintf(stderr, "QUIT failed\n");
1454 /* error but continue */
1455 }
1456
1457 /* cleanup */
1458 close(sock);
1459 xfree(uctl->host);
1460 xfree(uctl->iqn);
1461 xfree(uctl->mflags);
1462 xfree(uctl->mfile);
1463 xfree(uctl->msize);
1464 xfree(uctl->mtype);
1465 xfree(uctl->cmd);
1466 xfree(banner);
1467 xfree(cmd);
1468 istgt_free_config(config);
1469
1470 /* return value as execution result */
1471 if (exec_result != UCTL_CMD_OK) {
1472 exit(EXIT_FAILURE);
1473 }
1474 return EXIT_SUCCESS;
1475 }