2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: General/Generic functions for the 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 *****************************************************************************
31 #include "fwknop_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);
72 set_file_perms(const char *file)
76 res = chmod(file, S_IRUSR | S_IWUSR);
81 "[-] unable to chmod file %s to user read/write (0600, -rw-------): %s\n",
90 verify_file_perms_ownership(const char *file)
95 /* Every file that the fwknop client deals with should be owned
96 * by the user and permissions set to 600 (user read/write)
98 if((stat(file, &st)) != 0)
100 fprintf(stderr, "[-] unable to run stat() against file: %s: %s\n",
101 file, strerror(errno));
105 /* Make sure it is a regular file or symbolic link
107 if(S_ISREG(st.st_mode) != 1 && S_ISLNK(st.st_mode) != 1)
110 "[-] file: %s is not a regular file or symbolic link.\n",
116 if((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != (S_IRUSR|S_IWUSR))
119 "[-] file: %s permissions should only be user read/write (0600, -rw-------)\n",
125 if(st.st_uid != getuid())
127 fprintf(stderr, "[-] file: %s not owned by current effective user id.\n",