2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: An implementation of an fwknop client.
10 * Copyright 2009-2010 Damien Stuart (dstuart@dstuart.org)
12 * License (GNU Public License):
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 *****************************************************************************
32 #include "config_init.h"
35 #include "getpasswd.h"
39 static char * get_user_pw(fko_ctx_t ctx,
40 fko_cli_options_t *options, const int crypt_op);
41 static void display_ctx(fko_ctx_t ctx);
42 static void errmsg(const char *msg, const int err);
43 static void show_last_command(void);
44 static void save_args(int argc, char **argv);
45 static void run_last_args(fko_cli_options_t *options);
46 static int set_message_type(fko_ctx_t ctx, fko_cli_options_t *options);
47 static int set_nat_access(fko_ctx_t ctx, fko_cli_options_t *options);
48 static int get_rand_port(fko_ctx_t ctx);
49 int resolve_ip_http(fko_cli_options_t *options);
51 #define MAX_CMDLINE_ARGS 50 /* should be way more than enough */
54 main(int argc, char **argv)
58 char *spa_data, *version;
59 char access_buf[MAX_LINE_LEN];
61 fko_cli_options_t options;
63 /* Handle command line
65 config_init(&options, argc, argv);
67 /* Handle options that don't require a libfko context
69 if(options.run_last_command)
70 run_last_args(&options);
71 else if(options.show_last_command)
73 else if (!options.no_save_args)
74 save_args(argc, argv);
76 /* Intialize the context
79 if(res != FKO_SUCCESS)
81 errmsg("fko_new", res);
85 /* Display version info and exit.
87 if (options.version) {
88 fko_get_version(ctx, &version);
90 fprintf(stdout, "fwknop client %s, FKO protocol version %s\n",
99 if(options.fw_timeout >= 0)
101 res = fko_set_spa_client_timeout(ctx, options.fw_timeout);
102 if(res != FKO_SUCCESS)
104 errmsg("fko_set_spa_client_timeout", res);
106 return(EXIT_FAILURE);
110 /* Set the SPA packet message type based on command line options
112 res = set_message_type(ctx, &options);
113 if(res != FKO_SUCCESS)
115 errmsg("fko_set_spa_message_type", res);
117 return(EXIT_FAILURE);
120 /* Adjust the SPA timestamp if necessary
122 if(options.time_offset_plus > 0)
124 res = fko_set_timestamp(ctx, options.time_offset_plus);
125 if(res != FKO_SUCCESS)
127 errmsg("fko_set_timestamp", res);
129 return(EXIT_FAILURE);
132 if(options.time_offset_minus > 0)
134 res = fko_set_timestamp(ctx, -options.time_offset_minus);
135 if(res != FKO_SUCCESS)
137 errmsg("fko_set_timestamp", res);
139 return(EXIT_FAILURE);
143 if(options.server_command[0] != 0x0)
145 /* Set the access message to a command that the server will
148 snprintf(access_buf, MAX_LINE_LEN, "%s%s%s",
149 options.allow_ip_str, ",", options.server_command);
153 /* Resolve the client's public facing IP address if requestesd.
154 * if this fails, consider it fatal.
156 if (options.resolve_ip_http)
158 if(resolve_ip_http(&options) < 0)
161 return(EXIT_FAILURE);
165 /* Set a message string by combining the allow IP and the
166 * port/protocol. The fwknopd server allows no port/protocol
167 * to be specified as well, so in this case append the string
168 * "none/0" to the allow IP.
170 if(options.access_str[0] != 0x0)
172 snprintf(access_buf, MAX_LINE_LEN, "%s%s%s",
173 options.allow_ip_str, ",", options.access_str);
177 snprintf(access_buf, MAX_LINE_LEN, "%s%s%s",
178 options.allow_ip_str, ",", "none/0");
181 res = fko_set_spa_message(ctx, access_buf);
182 if(res != FKO_SUCCESS)
184 errmsg("fko_set_spa_message", res);
186 return(EXIT_FAILURE);
189 /* Set NAT access string
191 if (options.nat_local || options.nat_access_str[0] != 0x0)
193 res = set_nat_access(ctx, &options);
194 if(res != FKO_SUCCESS)
196 errmsg("fko_set_nat_access_str", res);
198 return(EXIT_FAILURE);
204 if(options.spoof_user[0] != 0x0)
206 res = fko_set_username(ctx, options.spoof_user);
207 if(res != FKO_SUCCESS)
209 errmsg("fko_set_username", res);
211 return(EXIT_FAILURE);
215 /* Set up for using GPG if specified.
219 /* If use-gpg-agent was not specified, then remove the GPG_AGENT_INFO
220 * ENV variable if it exists.
223 if(!options.use_gpg_agent)
224 unsetenv("GPG_AGENT_INFO");
227 res = fko_set_spa_encryption_type(ctx, FKO_ENCRYPTION_GPG);
228 if(res != FKO_SUCCESS)
230 errmsg("fko_set_spa_encryption_type", res);
232 return(EXIT_FAILURE);
235 /* If a GPG home dir was specified, set it here. Note: Setting
236 * this has to occur before calling any of the other GPG-related
239 if(options.gpg_home_dir != NULL && strlen(options.gpg_home_dir) > 0)
241 res = fko_set_gpg_home_dir(ctx, options.gpg_home_dir);
242 if(res != FKO_SUCCESS)
244 errmsg("fko_set_gpg_home_dir", res);
246 return(EXIT_FAILURE);
250 res = fko_set_gpg_recipient(ctx, options.gpg_recipient_key);
251 if(res != FKO_SUCCESS)
253 errmsg("fko_set_gpg_recipient", res);
255 if(IS_GPG_ERROR(res))
256 fprintf(stderr, "GPG ERR: %s\n", fko_gpg_errstr(ctx));
258 return(EXIT_FAILURE);
261 if(options.gpg_signer_key != NULL && strlen(options.gpg_signer_key))
263 res = fko_set_gpg_signer(ctx, options.gpg_signer_key);
264 if(res != FKO_SUCCESS)
266 errmsg("fko_set_gpg_signer", res);
268 if(IS_GPG_ERROR(res))
269 fprintf(stderr, "GPG ERR: %s\n", fko_gpg_errstr(ctx));
272 return(EXIT_FAILURE);
279 if(options.digest_type)
281 fko_set_spa_digest_type(ctx, options.digest_type);
282 if(res != FKO_SUCCESS)
284 errmsg("fko_set_spa_digest_type", res);
286 return(EXIT_FAILURE);
290 /* Finalize the context data (encrypt and encode the SPA data)
292 res = fko_spa_data_final(ctx, get_user_pw(ctx, &options, CRYPT_OP_ENCRYPT));
293 if(res != FKO_SUCCESS)
295 errmsg("fko_spa_data_final", res);
297 if(IS_GPG_ERROR(res))
298 fprintf(stderr, "GPG ERR: %s\n", fko_gpg_errstr(ctx));
301 return(EXIT_FAILURE);
304 /* Display the context data.
306 if (options.verbose || options.test)
309 /* Save packet data payload if requested.
311 if (options.save_packet_file[0] != 0x0)
312 write_spa_packet_data(ctx, &options);
314 if (options.rand_port)
315 options.spa_dst_port = get_rand_port(ctx);
317 res = send_spa_packet(ctx, &options);
320 fprintf(stderr, "send_spa_packet: packet not sent.\n");
322 return(EXIT_FAILURE);
327 fprintf(stderr, "send_spa_packet: bytes sent: %i\n", res);
330 /* Run through a decode cycle in test mode (--DSS XXX: This test/decode
331 * portion should be moved elsewhere).
335 /************** Decoding now *****************/
337 /* Now we create a new context based on data from the first one.
339 res = fko_get_spa_data(ctx, &spa_data);
340 if(res != FKO_SUCCESS)
342 errmsg("fko_get_spa_data", res);
344 return(EXIT_FAILURE);
347 /* If gpg-home-dir is specified, we have to defer decrypting if we
348 * use the fko_new_with_data() function because we need to set the
349 * gpg home dir after the context is created, but before we attempt
350 * to decrypt the data. Therefore we either pass NULL for the
351 * decryption key to fko_new_with_data() or use fko_new() to create
352 * an empty context, populate it with the encrypted data, set our
353 * options, then decode it.
355 res = fko_new_with_data(&ctx2, spa_data, NULL);
356 if(res != FKO_SUCCESS)
358 errmsg("fko_new_with_data", res);
361 return(EXIT_FAILURE);
364 /* See if we are using gpg and if we need to set the GPG home dir.
368 if(options.gpg_home_dir != NULL && strlen(options.gpg_home_dir) > 0)
370 res = fko_set_gpg_home_dir(ctx2, options.gpg_home_dir);
371 if(res != FKO_SUCCESS)
373 errmsg("fko_set_gpg_home_dir", res);
376 return(EXIT_FAILURE);
381 res = fko_decrypt_spa_data(
382 ctx2, get_user_pw(ctx2, &options, CRYPT_OP_DECRYPT)
385 if(res != FKO_SUCCESS)
387 errmsg("fko_decrypt_spa_data", res);
389 if(IS_GPG_ERROR(res)) {
390 /* we most likely could not decrypt the gpg-encrypted data
391 * because we don't have access to the private key associated
392 * with the public key we used for encryption. Since this is
393 * expected, return 0 instead of an error condition (so calling
394 * programs like the fwknop test suite don't interpret this as
395 * an unrecoverable error), but print the error string for
396 debugging purposes. */
397 fprintf(stderr, "GPG ERR: %s\n%s\n", fko_gpg_errstr(ctx2),
398 "No access to recipient private key?\n");
401 return(EXIT_SUCCESS);
406 return(EXIT_FAILURE);
409 printf("\nDump of the Decoded Data\n");
417 free_configs(&options);
419 return(EXIT_SUCCESS);
423 free_configs(fko_cli_options_t *opts)
425 if (opts->resolve_url != NULL)
426 free(opts->resolve_url);
430 get_rand_port(fko_ctx_t ctx)
432 char *rand_val = NULL;
436 res = fko_get_rand_value(ctx, &rand_val);
437 if(res != FKO_SUCCESS)
439 errmsg("get_rand_port(), fko_get_rand_value", res);
443 /* Convert to a random value between 1024 and 65535
445 port = (MIN_HIGH_PORT + (abs(atoi(rand_val)) % (MAX_PORT - MIN_HIGH_PORT)));
447 /* Force libfko to calculate a new random value since we don't want to
448 * given anyone a hint (via the port value) about the contents of the
449 * encrypted SPA data.
451 res = fko_set_rand_value(ctx, NULL);
452 if(res != FKO_SUCCESS)
454 errmsg("get_rand_port(), fko_get_rand_value", res);
461 /* See if the string is of the format "<ipv4 addr>:<port>",
464 ipv4_str_has_port(char *str)
466 int o1, o2, o3, o4, p;
468 /* Force the ':' (if any) to a ','
470 char *ndx = strchr(str, ':');
474 /* Check format and values.
476 if((sscanf(str, "%u.%u.%u.%u,%u", &o1, &o2, &o3, &o4, &p)) == 5
477 && o1 >= 0 && o1 <= 255
478 && o2 >= 0 && o2 <= 255
479 && o3 >= 0 && o3 <= 255
480 && o4 >= 0 && o4 <= 255
481 && p > 0 && p < 65536)
489 /* Set NAT access string
492 set_nat_access(fko_ctx_t ctx, fko_cli_options_t *options)
494 char nat_access_buf[MAX_LINE_LEN] = "";
497 if (options->nat_rand_port)
498 nat_port = get_rand_port(ctx);
499 else if (options->nat_port)
500 nat_port = options->nat_port;
502 nat_port = DEFAULT_NAT_PORT;
504 if (options->nat_local && options->nat_access_str[0] == 0x0)
506 snprintf(nat_access_buf, MAX_LINE_LEN, "%s,%d",
507 options->spa_server_str, nat_port);
510 if (nat_access_buf[0] == 0x0 && options->nat_access_str[0] != 0x0)
512 if (ipv4_str_has_port(options->nat_access_str))
514 snprintf(nat_access_buf, MAX_LINE_LEN, "%s",
515 options->nat_access_str);
519 snprintf(nat_access_buf, MAX_LINE_LEN, "%s,%d",
520 options->nat_access_str, nat_port);
524 return fko_set_spa_nat_access(ctx, nat_access_buf);
528 get_save_file(char *args_save_file)
530 char *homedir = NULL;
533 homedir = getenv("HOME");
535 if (homedir != NULL) {
536 snprintf(args_save_file, MAX_PATH_LEN, "%s%s%s",
537 homedir, "/", ".fwknop.run");
544 /* Show the last command that was executed
547 show_last_command(void)
549 char args_save_file[MAX_PATH_LEN];
550 char args_str[MAX_LINE_LEN] = "";
551 FILE *args_file_ptr = NULL;
554 /* Not sure what the right thing is here on Win32, just exit
557 fprintf(stderr, "--show-last not implemented on Win32 yet.");
561 if (get_save_file(args_save_file)) {
562 verify_file_perms_ownership(args_save_file);
563 if ((args_file_ptr = fopen(args_save_file, "r")) == NULL) {
564 fprintf(stderr, "Could not open args file: %s\n",
568 if ((fgets(args_str, MAX_LINE_LEN, args_file_ptr)) != NULL) {
569 printf("Last fwknop client command line: %s", args_str);
571 printf("Could not read line from file: %s\n", args_save_file);
573 fclose(args_file_ptr);
579 /* Get the command line arguments from the previous invocation
582 run_last_args(fko_cli_options_t *options)
584 FILE *args_file_ptr = NULL;
586 int current_arg_ctr = 0;
590 char args_save_file[MAX_PATH_LEN] = {0};
591 char args_str[MAX_LINE_LEN] = {0};
592 char arg_tmp[MAX_LINE_LEN] = {0};
593 char *argv_new[MAX_CMDLINE_ARGS]; /* should be way more than enough */
597 /* Not sure what the right thing is here on Win32, just return
603 if (get_save_file(args_save_file))
605 verify_file_perms_ownership(args_save_file);
606 if ((args_file_ptr = fopen(args_save_file, "r")) == NULL)
608 fprintf(stderr, "Could not open args file: %s\n",
612 if ((fgets(args_str, MAX_LINE_LEN, args_file_ptr)) != NULL)
614 args_str[MAX_LINE_LEN-1] = '\0';
615 if (options->verbose)
616 printf("Executing: %s\n", args_str);
617 for (i=0; i < (int)strlen(args_str); i++)
619 if (!isspace(args_str[i]))
621 arg_tmp[current_arg_ctr] = args_str[i];
626 arg_tmp[current_arg_ctr] = '\0';
627 argv_new[argc_new] = malloc(strlen(arg_tmp)+1);
628 if (argv_new[argc_new] == NULL)
630 fprintf(stderr, "[*] malloc failure for cmd line arg.\n");
633 strlcpy(argv_new[argc_new], arg_tmp, strlen(arg_tmp)+1);
636 if(argc_new >= MAX_CMDLINE_ARGS)
638 fprintf(stderr, "[*] max command line args exceeded.\n");
644 fclose(args_file_ptr);
646 /* Reset the options index so we can run through them again.
650 config_init(options, argc_new, argv_new);
656 /* Save our command line arguments
659 save_args(int argc, char **argv)
661 char args_save_file[MAX_PATH_LEN];
662 char args_str[MAX_LINE_LEN] = "";
663 FILE *args_file_ptr = NULL;
664 int i = 0, args_str_len = 0;
667 /* Not sure what the right thing is here on Win32, just return
673 if (get_save_file(args_save_file)) {
674 if ((args_file_ptr = fopen(args_save_file, "w")) == NULL) {
675 fprintf(stderr, "Could not open args file: %s\n",
679 for (i=0; i < argc; i++) {
680 args_str_len += strlen(argv[i]);
681 if (args_str_len >= MAX_PATH_LEN) {
682 fprintf(stderr, "argument string too long, exiting.\n");
685 strlcat(args_str, argv[i], MAX_PATH_LEN);
686 strlcat(args_str, " ", MAX_PATH_LEN);
688 fprintf(args_file_ptr, "%s\n", args_str);
689 fclose(args_file_ptr);
692 set_file_perms(args_save_file);
697 /* Set the SPA packet message type
700 set_message_type(fko_ctx_t ctx, fko_cli_options_t *options)
704 if(options->server_command[0] != 0x0)
706 message_type = FKO_COMMAND_MSG;
708 else if(options->nat_local)
710 if (options->fw_timeout >= 0)
711 message_type = FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG;
713 message_type = FKO_LOCAL_NAT_ACCESS_MSG;
715 else if(options->nat_access_str[0] != 0x0)
717 if (options->fw_timeout >= 0)
718 message_type = FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG;
720 message_type = FKO_NAT_ACCESS_MSG;
724 if (options->fw_timeout >= 0)
725 message_type = FKO_CLIENT_TIMEOUT_ACCESS_MSG;
727 message_type = FKO_ACCESS_MSG;
730 return fko_set_spa_message_type(ctx, message_type);
733 /* Prompt for and receive a user password.
736 get_user_pw(fko_ctx_t ctx, fko_cli_options_t *options, const int crypt_op)
739 static char *no_pw = "";
741 /* First of all if we are using GPG and GPG_AGENT
742 * then there is no password to return.
745 && (options->use_gpg_agent
746 || (crypt_op == CRYPT_OP_ENCRYPT && options->gpg_signer_key == NULL)))
749 /* If --get-key file was specified grab the key/password from it.
751 if (options->get_key_file[0] != 0x0)
753 pw_ptr = getpasswd_file(ctx, options);
755 else if (options->use_gpg)
757 if(crypt_op == CRYPT_OP_DECRYPT)
758 pw_ptr = getpasswd("Enter passphrase for secret key: ");
759 else if(options->gpg_signer_key && strlen(options->gpg_signer_key))
760 pw_ptr = getpasswd("Enter passphrase for signing: ");
766 if(crypt_op == CRYPT_OP_ENCRYPT)
767 pw_ptr = getpasswd("Enter encryption password: ");
768 else if(crypt_op == CRYPT_OP_DECRYPT)
769 pw_ptr = getpasswd("Enter decryption password: ");
771 pw_ptr = getpasswd("Enter password: ");
774 /* Empty password is allowed, NULL password is not.
778 fprintf(stderr, "Received no password data, exiting.\n");
786 /* Display an FKO error message.
789 errmsg(const char *msg, const int err) {
790 fprintf(stderr, "%s: %s: Error %i - %s\n",
791 MY_NAME, msg, err, fko_errstr(err));
794 /* Show the fields of the FKO context.
797 display_ctx(fko_ctx_t ctx)
799 char *rand_val = NULL;
800 char *username = NULL;
801 char *version = NULL;
802 char *spa_message = NULL;
803 char *nat_access = NULL;
804 char *server_auth = NULL;
805 char *enc_data = NULL;
806 char *spa_digest = NULL;
807 char *spa_data = NULL;
809 time_t timestamp = 0;
811 short digest_type = -1;
812 int client_timeout = -1;
814 /* Should be checking return values, but this is temp code. --DSS
816 fko_get_rand_value(ctx, &rand_val);
817 fko_get_username(ctx, &username);
818 fko_get_timestamp(ctx, ×tamp);
819 fko_get_version(ctx, &version);
820 fko_get_spa_message_type(ctx, &msg_type);
821 fko_get_spa_message(ctx, &spa_message);
822 fko_get_spa_nat_access(ctx, &nat_access);
823 fko_get_spa_server_auth(ctx, &server_auth);
824 fko_get_spa_client_timeout(ctx, &client_timeout);
825 fko_get_spa_digest_type(ctx, &digest_type);
826 fko_get_encoded_data(ctx, &enc_data);
827 fko_get_spa_digest(ctx, &spa_digest);
828 fko_get_spa_data(ctx, &spa_data);
830 printf("\nFKO Field Values:\n=================\n\n");
831 printf(" Random Value: %s\n", rand_val == NULL ? "<NULL>" : rand_val);
832 printf(" Username: %s\n", username == NULL ? "<NULL>" : username);
833 printf(" Timestamp: %u\n", (unsigned int) timestamp);
834 printf(" FKO Version: %s\n", version == NULL ? "<NULL>" : version);
835 printf(" Message Type: %i\n", msg_type);
836 printf(" Message String: %s\n", spa_message == NULL ? "<NULL>" : spa_message);
837 printf(" Nat Access: %s\n", nat_access == NULL ? "<NULL>" : nat_access);
838 printf(" Server Auth: %s\n", server_auth == NULL ? "<NULL>" : server_auth);
839 printf(" Client Timeout: %u\n", client_timeout);
840 printf(" Digest Type: %u\n", digest_type);
841 printf("\n Encoded Data: %s\n", enc_data == NULL ? "<NULL>" : enc_data);
842 printf("\nSPA Data Digest: %s\n", spa_digest == NULL ? "<NULL>" : spa_digest);
843 printf("\nFinal Packed/Encrypted/Encoded Data:\n\n%s\n\n", spa_data);