2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: General/Generic functions for the fwknop server.
10 * Copyright 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 *****************************************************************************
31 #include "fwknopd_common.h"
34 /* Generic hex dump function.
37 hex_dump(const unsigned char *data, const int size)
40 char ascii_str[17] = {0};
46 printf(" %s\n 0x%.4x: ", ascii_str, i);
47 memset(ascii_str, 0x0, 17);
51 printf("%.2x ", data[i]);
53 ascii_str[j++] = (data[i] < 0x20 || data[i] > 0x7e) ? '.' : data[i];
61 ln = strlen(ascii_str);
64 for(i=0; i < 16-ln; i++)
67 printf(" %s\n\n", ascii_str);
71 /* Show the fields of the FKO context.
74 dump_ctx(fko_ctx_t ctx)
76 static char buf[CTX_DUMP_BUFSIZE];
80 char *rand_val = NULL;
81 char *username = NULL;
83 char *spa_message = NULL;
84 char *nat_access = NULL;
85 char *server_auth = NULL;
86 char *enc_data = NULL;
87 char *spa_digest = NULL;
88 char *spa_data = NULL;
92 short digest_type = -1;
93 int client_timeout = -1;
95 /* Should be checking return values, but this is temp code. --DSS
97 fko_get_rand_value(ctx, &rand_val);
98 fko_get_username(ctx, &username);
99 fko_get_timestamp(ctx, ×tamp);
100 fko_get_version(ctx, &version);
101 fko_get_spa_message_type(ctx, &msg_type);
102 fko_get_spa_message(ctx, &spa_message);
103 fko_get_spa_nat_access(ctx, &nat_access);
104 fko_get_spa_server_auth(ctx, &server_auth);
105 fko_get_spa_client_timeout(ctx, &client_timeout);
106 fko_get_spa_digest_type(ctx, &digest_type);
107 fko_get_encoded_data(ctx, &enc_data);
108 fko_get_spa_digest(ctx, &spa_digest);
109 fko_get_spa_data(ctx, &spa_data);
111 memset(buf, 0x0, CTX_DUMP_BUFSIZE);
115 cp = sprintf(ndx, "SPA Field Values:\n=================\n");
117 cp = sprintf(ndx, " Random Value: %s\n", rand_val == NULL ? "<NULL>" : rand_val);
119 cp = sprintf(ndx, " Username: %s\n", username == NULL ? "<NULL>" : username);
121 cp = sprintf(ndx, " Timestamp: %u\n", (unsigned int) timestamp);
123 cp = sprintf(ndx, " FKO Version: %s\n", version == NULL ? "<NULL>" : version);
125 cp = sprintf(ndx, " Message Type: %i\n", msg_type);
127 cp = sprintf(ndx, " Message String: %s\n", spa_message == NULL ? "<NULL>" : spa_message);
129 cp = sprintf(ndx, " Nat Access: %s\n", nat_access == NULL ? "<NULL>" : nat_access);
131 cp = sprintf(ndx, " Server Auth: %s\n", server_auth == NULL ? "<NULL>" : server_auth);
133 cp = sprintf(ndx, " Client Timeout: %u\n", client_timeout);
135 cp = sprintf(ndx, " Digest Type: %u\n", digest_type);
137 cp = sprintf(ndx, " Encoded Data: %s\n", enc_data == NULL ? "<NULL>" : enc_data);
139 cp = sprintf(ndx, "SPA Data Digest: %s\n", spa_digest == NULL ? "<NULL>" : spa_digest);
144 /* Basic directory checks (stat() and whether the path is actually
148 is_valid_dir(const char *path)
153 /* If we are unable to stat the given dir, then return with error.
155 if(stat(path, &st) != 0)
157 fprintf(stderr, "[-] unable to stat() directory: %s: %s\n",
158 path, strerror(errno));
162 if(!S_ISDIR(st.st_mode))
164 #endif /* HAVE_STAT */
170 set_file_perms(const char *file)
174 res = chmod(file, S_IRUSR | S_IWUSR);
178 fprintf(stderr, "[-] unable to chmod file %s to user read/write: %s\n",
179 file, strerror(errno));
185 verify_file_perms_ownership(const char *file)
190 /* Every file that the fwknop client deals with should be owned
191 * by the user and permissions set to 600 (user read/write)
193 if((stat(file, &st)) != 0)
195 fprintf(stderr, "[-] unable to stat() file: %s: %s\n",
196 file, strerror(errno));
200 /* Make sure it is a regular file
202 if(S_ISREG(st.st_mode) != 1 && S_ISLNK(st.st_mode) != 1)
205 "[-] file: %s is not a regular file or symbolic link.\n",
211 if((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != (S_IRUSR|S_IWUSR))
214 "[-] file: %s permissions should only be user read/write (0600, -rw-------)\n",
220 if(st.st_uid != getuid())
222 fprintf(stderr, "[-] file: %s not owned by current effective user id\n",
231 /* Determine if a buffer contains only characters from the base64
235 is_base64(const unsigned char *buf, unsigned short int len)
237 unsigned short int i;
242 if(!(isalnum(buf[i]) || buf[i] == '/' || buf[i] == '+' || buf[i] == '='))