[1] | 1 | /* SAMHAIN file system integrity testing */
|
---|
| 2 | /* Copyright (C) 1999, 2000 Rainer Wichmann */
|
---|
| 3 | /* */
|
---|
| 4 | /* This program is free software; you can redistribute it */
|
---|
| 5 | /* and/or modify */
|
---|
| 6 | /* it under the terms of the GNU General Public License as */
|
---|
| 7 | /* published by */
|
---|
| 8 | /* the Free Software Foundation; either version 2 of the License, or */
|
---|
| 9 | /* (at your option) any later version. */
|
---|
| 10 | /* */
|
---|
| 11 | /* This program is distributed in the hope that it will be useful, */
|
---|
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
---|
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
---|
| 14 | /* GNU General Public License for more details. */
|
---|
| 15 | /* */
|
---|
| 16 | /* You should have received a copy of the GNU General Public License */
|
---|
| 17 | /* along with this program; if not, write to the Free Software */
|
---|
| 18 | /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
| 19 |
|
---|
| 20 | #include "config_xor.h"
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | #include <stdio.h>
|
---|
| 24 | #include <stdlib.h>
|
---|
| 25 | #include <string.h>
|
---|
| 26 | #include <ctype.h>
|
---|
| 27 |
|
---|
| 28 | /* Must be early on FreeBSD
|
---|
| 29 | */
|
---|
| 30 | #include <sys/types.h>
|
---|
| 31 |
|
---|
| 32 | #ifdef HAVE_MEMORY_H
|
---|
| 33 | #include <memory.h>
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
| 36 | #ifdef HAVE_SYS_SELECT_H
|
---|
| 37 | #include <sys/select.h>
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
| 40 | #ifdef HAVE_UNISTD_H
|
---|
| 41 | #include <errno.h>
|
---|
| 42 | #include <signal.h>
|
---|
| 43 | #include <setjmp.h>
|
---|
| 44 | #include <pwd.h>
|
---|
| 45 | #include <grp.h>
|
---|
| 46 | #include <sys/stat.h>
|
---|
| 47 | #include <sys/resource.h>
|
---|
| 48 | #include <fcntl.h>
|
---|
| 49 | #include <sys/wait.h>
|
---|
| 50 | #include <unistd.h>
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 | #include <sys/socket.h>
|
---|
| 54 |
|
---|
| 55 | #ifdef HOST_IS_HPUX
|
---|
| 56 | #define _XOPEN_SOURCE_EXTENDED
|
---|
| 57 | #endif
|
---|
| 58 | #include <netinet/in.h>
|
---|
| 59 | #include <arpa/inet.h>
|
---|
| 60 | #include <netdb.h>
|
---|
| 61 |
|
---|
| 62 | #ifndef FD_SET
|
---|
| 63 | #define NFDBITS 32
|
---|
| 64 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
|
---|
| 65 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
|
---|
| 66 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
|
---|
| 67 | #endif /* !FD_SET */
|
---|
| 68 | #ifndef FD_SETSIZE
|
---|
| 69 | #define FD_SETSIZE 32
|
---|
| 70 | #endif
|
---|
| 71 | #ifndef FD_ZERO
|
---|
| 72 | #define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
|
---|
| 73 | #endif
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
| 77 | #include <sys/mman.h>
|
---|
| 78 | #endif
|
---|
| 79 |
|
---|
[11] | 80 | #define SH_REAL_SET
|
---|
| 81 |
|
---|
[1] | 82 | #include "samhain.h"
|
---|
| 83 | #include "sh_mem.h"
|
---|
| 84 | #include "sh_error.h"
|
---|
| 85 | #include "sh_tools.h"
|
---|
| 86 | #include "sh_utils.h"
|
---|
| 87 | #include "sh_tiger.h"
|
---|
| 88 | #define SH_NEED_GETHOSTBYXXX
|
---|
| 89 | #include "sh_static.h"
|
---|
[137] | 90 | #include "sh_pthread.h"
|
---|
[1] | 91 |
|
---|
| 92 | #undef FIL__
|
---|
| 93 | #define FIL__ _("sh_tools.c")
|
---|
| 94 |
|
---|
| 95 | #ifdef SH_ENCRYPT
|
---|
| 96 | #include "rijndael-api-fst.h"
|
---|
[132] | 97 | char * errorExplain (int err_num, char * buffer, size_t len)
|
---|
[1] | 98 | {
|
---|
[132] | 99 | char * p;
|
---|
| 100 |
|
---|
[1] | 101 | if (err_num == BAD_KEY_DIR)
|
---|
[132] | 102 | p = (_("Key direction is invalid"));
|
---|
[1] | 103 | else if (err_num == BAD_KEY_MAT)
|
---|
[132] | 104 | p = (_("Key material not of correct length"));
|
---|
[1] | 105 | else if (err_num == BAD_KEY_INSTANCE)
|
---|
[132] | 106 | p = (_("Key passed is not valid"));
|
---|
[1] | 107 | else if (err_num == BAD_CIPHER_MODE)
|
---|
[132] | 108 | p = (_("Params struct passed to cipherInit invalid"));
|
---|
[1] | 109 | else if (err_num == BAD_CIPHER_STATE)
|
---|
[132] | 110 | p = (_("Cipher in wrong state"));
|
---|
[1] | 111 | else if (err_num == BAD_BLOCK_LENGTH)
|
---|
[132] | 112 | p = (_("Bad block length"));
|
---|
[1] | 113 | else if (err_num == BAD_CIPHER_INSTANCE)
|
---|
[132] | 114 | p = (_("Bad cipher instance"));
|
---|
[1] | 115 | else if (err_num == BAD_DATA)
|
---|
[132] | 116 | p = (_("Data contents are invalid"));
|
---|
[1] | 117 | else
|
---|
[132] | 118 | p = (_("Unknown error"));
|
---|
| 119 | sl_strlcpy (buffer, p, len);
|
---|
| 120 | return buffer;
|
---|
[1] | 121 | }
|
---|
| 122 |
|
---|
| 123 | #endif
|
---|
| 124 |
|
---|
| 125 | /* --- recode all \blah escapes to '=XX' format, and also code all
|
---|
| 126 | * remaining unprintable chars ---
|
---|
| 127 | */
|
---|
[12] | 128 | #define SH_PUT_4(p, a, b, c) (p)[0] = (a); (p)[1] = (b); (p)[2] = (c);
|
---|
| 129 |
|
---|
[1] | 130 | char * sh_tools_safe_name (const char * instr, int flag)
|
---|
| 131 | {
|
---|
[18] | 132 | unsigned char c, d;
|
---|
[1] | 133 | const char * p;
|
---|
| 134 | char tmp[4];
|
---|
| 135 | char * outstr;
|
---|
[34] | 136 | size_t len = 1;
|
---|
[1] | 137 | int i = 0;
|
---|
| 138 | unsigned char val_octal = '\0';
|
---|
[18] | 139 | static char ctable[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
---|
| 140 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
---|
[1] | 141 |
|
---|
| 142 | SL_ENTER(_("sh_tools_safe_name"));
|
---|
| 143 |
|
---|
| 144 | if (instr)
|
---|
[34] | 145 | {
|
---|
| 146 | len = strlen(instr);
|
---|
| 147 | if (sl_ok_muls (3, len) && sl_ok_adds ((3*len), 4))
|
---|
| 148 | {
|
---|
| 149 | len = (3 * len) + 4;
|
---|
| 150 | p = instr;
|
---|
| 151 | }
|
---|
| 152 | else
|
---|
| 153 | {
|
---|
| 154 | len = 1;
|
---|
| 155 | p = NULL;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | else
|
---|
| 159 | {
|
---|
| 160 | p = NULL;
|
---|
| 161 | }
|
---|
[1] | 162 |
|
---|
| 163 | outstr = SH_ALLOC(len);
|
---|
| 164 |
|
---|
| 165 | outstr[0] = '\0';
|
---|
| 166 | tmp[3] = '\0';
|
---|
| 167 |
|
---|
[11] | 168 | #if !defined(SH_USE_XML)
|
---|
| 169 | (void) flag; /* fix compiler warning */
|
---|
| 170 | #endif
|
---|
| 171 |
|
---|
| 172 | if (!p)
|
---|
| 173 | goto end;
|
---|
| 174 |
|
---|
| 175 | while (*p)
|
---|
[1] | 176 | {
|
---|
| 177 | c = *p;
|
---|
| 178 |
|
---|
| 179 | if (*p == '\n')
|
---|
| 180 | {
|
---|
| 181 | outstr[i] = ' '; ++i; ++p;
|
---|
| 182 | continue;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | #ifdef SH_USE_XML
|
---|
[11] | 186 | if (flag == 1)
|
---|
| 187 | {
|
---|
| 188 | if ((*p) == '"')
|
---|
| 189 | {
|
---|
[12] | 190 | SH_PUT_4(&outstr[i], '=', '2', '2');
|
---|
| 191 | i+=3; ++p;
|
---|
[11] | 192 | continue;
|
---|
| 193 | }
|
---|
| 194 | else if ((*p) == '&')
|
---|
| 195 | {
|
---|
[12] | 196 | SH_PUT_4(&outstr[i], '=', '2', '6');
|
---|
| 197 | i+=3; ++p;
|
---|
[11] | 198 | continue;
|
---|
| 199 | }
|
---|
| 200 | else if ((*p) == '<')
|
---|
| 201 | { /* left angle */
|
---|
[12] | 202 | SH_PUT_4(&outstr[i], '=', '3', 'c');
|
---|
| 203 | i+=3; ++p;
|
---|
[11] | 204 | continue;
|
---|
| 205 | }
|
---|
| 206 | else if ((*p) == '>')
|
---|
| 207 | { /* right angle */
|
---|
[12] | 208 | SH_PUT_4(&outstr[i], '=', '3', 'e');
|
---|
| 209 | i+=3; ++p;
|
---|
[11] | 210 | continue;
|
---|
| 211 | }
|
---|
[1] | 212 | }
|
---|
| 213 | #endif
|
---|
| 214 |
|
---|
[12] | 215 | if ( (*p) != '\\' && (*p) != '&' && (*p) != '=' && (*p) != '\'')
|
---|
[1] | 216 | {
|
---|
[11] | 217 | outstr[i] = *p; ++i;
|
---|
| 218 | ++p;
|
---|
| 219 |
|
---|
[81] | 220 | if (c < 32 || c > 126)
|
---|
[1] | 221 | {
|
---|
[11] | 222 | --i;
|
---|
[18] | 223 | d = c % 16; c = c / 16;
|
---|
| 224 | outstr[i] = '='; ++i;
|
---|
| 225 | outstr[i] = ctable[c]; ++i;
|
---|
| 226 | outstr[i] = ctable[d]; ++i;
|
---|
[1] | 227 | }
|
---|
[11] | 228 |
|
---|
[1] | 229 | continue;
|
---|
| 230 | }
|
---|
| 231 | else if ((*p) == '\'')
|
---|
| 232 | {
|
---|
[12] | 233 | SH_PUT_4(&outstr[i], '=', '2', '7');
|
---|
| 234 | i+=3; ++p;
|
---|
[1] | 235 | }
|
---|
| 236 | else if (*p == '=')
|
---|
| 237 | {
|
---|
| 238 | if (p[1] != '"' && p[1] != '<')
|
---|
| 239 | {
|
---|
[12] | 240 | SH_PUT_4(&outstr[i], '=', '3', 'd');
|
---|
| 241 | i+=3; ++p;
|
---|
[1] | 242 | }
|
---|
| 243 | else
|
---|
| 244 | { outstr[i] = *p; ++i; ++p; }
|
---|
| 245 | }
|
---|
| 246 | else if (*p == '\\')
|
---|
| 247 | {
|
---|
| 248 | ++p;
|
---|
| 249 | if (!p)
|
---|
| 250 | break;
|
---|
| 251 | if (!(*p))
|
---|
| 252 | break;
|
---|
| 253 |
|
---|
| 254 | c = *p;
|
---|
| 255 |
|
---|
| 256 | switch (*p) {
|
---|
| 257 | case '\\':
|
---|
[12] | 258 | SH_PUT_4(&outstr[i], '=', '5', 'c');
|
---|
| 259 | i+=3; ++p;
|
---|
[1] | 260 | break;
|
---|
| 261 | case 'n':
|
---|
[12] | 262 | SH_PUT_4(&outstr[i], '=', '0', 'a');
|
---|
[1] | 263 | i+=3; ++p;
|
---|
| 264 | break;
|
---|
| 265 | case 'b':
|
---|
[12] | 266 | SH_PUT_4(&outstr[i], '=', '0', '8');
|
---|
[1] | 267 | i+=3; ++p;
|
---|
| 268 | break;
|
---|
| 269 | case 'r':
|
---|
[12] | 270 | SH_PUT_4(&outstr[i], '=', '0', 'd');
|
---|
[1] | 271 | i+=3; ++p;
|
---|
| 272 | break;
|
---|
| 273 | case 't':
|
---|
[12] | 274 | SH_PUT_4(&outstr[i], '=', '0', '9');
|
---|
[1] | 275 | i+=3; ++p;
|
---|
| 276 | break;
|
---|
| 277 | case 'v':
|
---|
[12] | 278 | SH_PUT_4(&outstr[i], '=', '0', 'b');
|
---|
[1] | 279 | i+=3; ++p;
|
---|
| 280 | break;
|
---|
| 281 | case 'f':
|
---|
[12] | 282 | SH_PUT_4(&outstr[i], '=', '0', 'c');
|
---|
[1] | 283 | i+=3; ++p;
|
---|
| 284 | break;
|
---|
| 285 | case '\'':
|
---|
[12] | 286 | SH_PUT_4(&outstr[i], '=', '2', '7');
|
---|
[1] | 287 | i+=3; ++p;
|
---|
| 288 | break;
|
---|
[12] | 289 | case '"': /* also encode quoted '"' */
|
---|
| 290 | SH_PUT_4(&outstr[i], '=', '2', '2');
|
---|
[1] | 291 | i+=3; ++p;
|
---|
| 292 | break;
|
---|
| 293 | case ' ':
|
---|
[12] | 294 | SH_PUT_4(&outstr[i], '=', '2', '0');
|
---|
[1] | 295 | i+=3; ++p;
|
---|
| 296 | break;
|
---|
| 297 | default:
|
---|
[18] | 298 | if (strlen(p) < 3) /* certainly not an octal number, skip */
|
---|
[1] | 299 | {
|
---|
| 300 | p += strlen(p);
|
---|
| 301 | }
|
---|
| 302 | else
|
---|
| 303 | {
|
---|
| 304 | tmp[0] = p[0]; tmp[1] = p[1]; tmp[2] = p[2];
|
---|
| 305 | val_octal = (unsigned char) strtoul(tmp, (char **)NULL, 8);
|
---|
| 306 | if (val_octal != '\0') {
|
---|
| 307 | c = val_octal;
|
---|
[18] | 308 | d = c % 16; c = c / 16;
|
---|
| 309 | outstr[i] = '='; ++i;
|
---|
| 310 | outstr[i] = ctable[c]; ++i;
|
---|
| 311 | outstr[i] = ctable[d]; ++i;
|
---|
[1] | 312 | }
|
---|
| 313 | p += 3;
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
[11] | 317 | else if (*p == '&')
|
---|
[1] | 318 | {
|
---|
| 319 | ++p;
|
---|
[18] | 320 | if (!p || !(*p))
|
---|
| 321 | {
|
---|
| 322 | outstr[i] = '&'; ++i;
|
---|
| 323 | break;
|
---|
| 324 | }
|
---|
[1] | 325 |
|
---|
| 326 | if (p[0] == 'a' && p[1] == 'm' && p[2] == 'p' && p[3] == ';')
|
---|
| 327 | {
|
---|
[12] | 328 | SH_PUT_4(&outstr[i], '=', '2', '6');
|
---|
[1] | 329 | i+=3; p += 4;
|
---|
| 330 | }
|
---|
| 331 | else if (p[0] == 'q' && p[1] == 'u' && p[2] == 'o' && p[3] == 't' &&
|
---|
| 332 | p[4] == ';')
|
---|
| 333 | {
|
---|
[12] | 334 | SH_PUT_4(&outstr[i], '=', '2', '2');
|
---|
[1] | 335 | i+=3; p += 5;
|
---|
| 336 | }
|
---|
| 337 | else if (p[0] == 'l' && p[1] == 't' && p[2] == ';')
|
---|
| 338 | {
|
---|
[12] | 339 | SH_PUT_4(&outstr[i], '=', '3', 'c');
|
---|
[1] | 340 | i+=3; p += 3;
|
---|
| 341 | }
|
---|
| 342 | else if (p[0] == 'g' && p[1] == 't' && p[2] == ';')
|
---|
| 343 | {
|
---|
[12] | 344 | SH_PUT_4(&outstr[i], '=', '3', 'e');
|
---|
[1] | 345 | i+=3; p += 3;
|
---|
| 346 | }
|
---|
| 347 | else /* conserve the '&' */
|
---|
| 348 | {
|
---|
| 349 | outstr[i] = '&'; ++i;
|
---|
| 350 | }
|
---|
[11] | 351 | }
|
---|
| 352 | else
|
---|
| 353 | {
|
---|
| 354 | outstr[i] = *p; ++i;
|
---|
| 355 | ++p;
|
---|
| 356 | }
|
---|
[1] | 357 | } /* while (p && *p) */
|
---|
[11] | 358 |
|
---|
| 359 | end:
|
---|
[1] | 360 |
|
---|
| 361 | outstr[i] = '\0';
|
---|
| 362 | SL_RETURN( outstr, _("sh_tools_safe_name"));
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 |
|
---|
| 366 | /* extern int h_errno; */
|
---|
| 367 |
|
---|
[132] | 368 | char * sh_tools_errmessage (int tellme, char * errbuf, size_t len)
|
---|
[1] | 369 | {
|
---|
[132] | 370 | char * p = NULL;
|
---|
[1] | 371 | #ifdef HOST_NOT_FOUND
|
---|
| 372 | if (tellme == HOST_NOT_FOUND)
|
---|
[132] | 373 | p = _("The specified host is unknown: ");
|
---|
[1] | 374 | #endif
|
---|
| 375 | #ifdef NO_ADDRESS
|
---|
| 376 | if (tellme == NO_ADDRESS)
|
---|
[132] | 377 | p = _("The requested name is valid but does not have an IP address: ");
|
---|
[1] | 378 | #endif
|
---|
| 379 | #ifdef NO_RECOVERY
|
---|
| 380 | if (tellme == NO_RECOVERY)
|
---|
[132] | 381 | p = _("A non-recoverable name server error occurred: ");
|
---|
[1] | 382 | #endif
|
---|
| 383 | #ifdef TRY_AGAIN
|
---|
| 384 | if (tellme == TRY_AGAIN)
|
---|
[132] | 385 | p = _("A temporary error occurred on an authoritative name server. The specified host is unknown: ");
|
---|
[1] | 386 | #endif
|
---|
[132] | 387 | if (!p) p = _("Unknown error");
|
---|
| 388 | sl_strlcpy(errbuf, p, len);
|
---|
| 389 | return errbuf;
|
---|
[1] | 390 | }
|
---|
| 391 |
|
---|
| 392 | int is_numeric (const char * address)
|
---|
| 393 | {
|
---|
| 394 | int j;
|
---|
| 395 | int len = sl_strlen(address);
|
---|
| 396 |
|
---|
| 397 | for (j = 0; j < len; ++j)
|
---|
| 398 | if ( (address[j] < '0' || address[j] > '9') && address[j] != '.')
|
---|
| 399 | return (1 == 0);
|
---|
| 400 | return (1 == 1);
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | #if defined (SH_WITH_SERVER)
|
---|
| 404 |
|
---|
| 405 | int get_open_max ()
|
---|
| 406 | {
|
---|
| 407 | int value;
|
---|
| 408 |
|
---|
| 409 | #ifdef _SC_OPEN_MAX
|
---|
| 410 | value = sysconf (_SC_OPEN_MAX);
|
---|
| 411 | #else
|
---|
| 412 | #ifdef OPEN_MAX
|
---|
| 413 | value = OPEN_MAX;
|
---|
| 414 | #else
|
---|
| 415 | value = _POSIX_OPEN_MAX;
|
---|
| 416 | #endif
|
---|
| 417 | #endif
|
---|
| 418 |
|
---|
| 419 | if (value < 0)
|
---|
| 420 | value = 8; /* POSIX lower limit */
|
---|
| 421 |
|
---|
| 422 | if (value > 4096)
|
---|
| 423 | value = 4096;
|
---|
| 424 |
|
---|
| 425 | return value;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | #endif
|
---|
| 429 |
|
---|
| 430 | typedef struct _sin_cache {
|
---|
| 431 | char * address;
|
---|
| 432 | struct sockaddr_in sin;
|
---|
| 433 | struct _sin_cache * next;
|
---|
| 434 | } sin_cache;
|
---|
| 435 |
|
---|
| 436 | static sin_cache * conn_cache = NULL;
|
---|
| 437 | static int cached_addr = 0;
|
---|
| 438 |
|
---|
| 439 | void delete_cache()
|
---|
| 440 | {
|
---|
| 441 | sin_cache * check_cache = conn_cache;
|
---|
| 442 | sin_cache * old_entry = conn_cache;
|
---|
| 443 |
|
---|
| 444 | SL_ENTER(_("delete_cache"));
|
---|
| 445 |
|
---|
| 446 | while (check_cache != NULL)
|
---|
| 447 | {
|
---|
| 448 | old_entry = check_cache;
|
---|
| 449 | check_cache = check_cache->next;
|
---|
| 450 | SH_FREE(old_entry->address);
|
---|
| 451 | SH_FREE(old_entry);
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | cached_addr = 0;
|
---|
| 455 |
|
---|
| 456 | conn_cache = NULL;
|
---|
| 457 | SL_RET0(_("delete_cache"));
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | int DoReverseLookup = S_TRUE;
|
---|
| 461 |
|
---|
[22] | 462 | int set_reverse_lookup (const char * c)
|
---|
[1] | 463 | {
|
---|
| 464 | return sh_util_flagval(c, &DoReverseLookup);
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | int connect_port (char * address, int port,
|
---|
| 468 | char * ecall, int * errnum, char * errmsg, int errsiz)
|
---|
| 469 | {
|
---|
| 470 | struct in_addr haddr; /* host address from numeric */
|
---|
| 471 | /* host details returned by the DNS */
|
---|
| 472 | struct hostent *host_entry = NULL;
|
---|
| 473 | struct sockaddr_in sinr; /* socket to the remote host */
|
---|
| 474 |
|
---|
| 475 | char * host_name;
|
---|
| 476 |
|
---|
| 477 | int fd = (-1);
|
---|
| 478 | int status;
|
---|
| 479 | int fail = 0;
|
---|
| 480 | int cached = 0;
|
---|
| 481 |
|
---|
| 482 | int retval;
|
---|
[132] | 483 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 484 |
|
---|
| 485 | sin_cache * check_cache = conn_cache;
|
---|
| 486 |
|
---|
| 487 | SL_ENTER(_("connect_port"));
|
---|
| 488 |
|
---|
| 489 | /* paranoia -- should not happen
|
---|
| 490 | */
|
---|
| 491 | if (cached_addr > 128)
|
---|
| 492 | delete_cache();
|
---|
| 493 |
|
---|
| 494 | if (check_cache != NULL)
|
---|
| 495 | {
|
---|
| 496 | while (check_cache && check_cache->address)
|
---|
| 497 | {
|
---|
| 498 | if ( 0 == sl_strncmp(check_cache->address,
|
---|
| 499 | address, sl_strlen(address)))
|
---|
| 500 | {
|
---|
| 501 | memcpy (&sinr, &(check_cache->sin), sizeof(struct sockaddr_in));
|
---|
| 502 | sinr.sin_family = AF_INET;
|
---|
| 503 | sinr.sin_port = htons (port);
|
---|
| 504 | cached = 1;
|
---|
| 505 | break;
|
---|
| 506 | }
|
---|
| 507 | if (check_cache->next)
|
---|
| 508 | check_cache = check_cache->next;
|
---|
| 509 | else
|
---|
| 510 | check_cache = NULL;
|
---|
| 511 | }
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | /* only use gethostbyname() if neccessary
|
---|
| 515 | */
|
---|
| 516 | if (cached == 0)
|
---|
| 517 | {
|
---|
| 518 | #ifdef HAVE_INET_ATON
|
---|
| 519 | if (0 == inet_aton(address, &haddr))
|
---|
| 520 | #else
|
---|
| 521 | if ((unsigned long)-1 == (haddr.s_addr = inet_addr(address)))
|
---|
| 522 | #endif
|
---|
| 523 | {
|
---|
[143] | 524 | host_name = NULL;
|
---|
| 525 |
|
---|
[134] | 526 | SH_MUTEX_LOCK(mutex_resolv);
|
---|
| 527 |
|
---|
[1] | 528 | host_entry = sh_gethostbyname(address);
|
---|
| 529 |
|
---|
| 530 | if (host_entry == NULL || host_entry->h_addr == NULL)
|
---|
| 531 | {
|
---|
| 532 | sl_strlcpy(ecall, _("gethostbyname"), SH_MINIBUF);
|
---|
| 533 | #ifndef NO_H_ERRNO
|
---|
| 534 | *errnum = h_errno;
|
---|
| 535 | #else
|
---|
| 536 | *errnum = 666;
|
---|
| 537 | #endif
|
---|
[132] | 538 | (void) sh_tools_errmessage (*errnum, errmsg, errsiz);
|
---|
[1] | 539 | sl_strlcat(errmsg, address, errsiz);
|
---|
| 540 | fail = (-1);
|
---|
| 541 | }
|
---|
| 542 | else
|
---|
| 543 | {
|
---|
| 544 | sinr.sin_family = AF_INET;
|
---|
| 545 | sinr.sin_port = htons (port);
|
---|
| 546 | sinr.sin_addr = *(struct in_addr *) host_entry->h_addr;
|
---|
| 547 |
|
---|
| 548 |
|
---|
| 549 | /* reverse DNS lookup
|
---|
| 550 | */
|
---|
| 551 | if (DoReverseLookup == S_TRUE)
|
---|
| 552 | {
|
---|
| 553 | if (host_entry->h_name == NULL)
|
---|
| 554 | {
|
---|
| 555 | host_name = SH_ALLOC(1);
|
---|
| 556 | host_name[0] = '\0';
|
---|
| 557 | }
|
---|
| 558 | else
|
---|
| 559 | {
|
---|
[34] | 560 | host_name = sh_util_strdup(host_entry->h_name);
|
---|
[1] | 561 | }
|
---|
| 562 |
|
---|
| 563 | host_entry = sh_gethostbyaddr ((char *) &sinr.sin_addr,
|
---|
| 564 | sizeof(struct in_addr),
|
---|
| 565 | AF_INET);
|
---|
| 566 | if (host_entry == NULL || host_entry->h_name == NULL)
|
---|
| 567 | {
|
---|
| 568 | sl_strlcpy(ecall, _("gethostbyaddr"), SH_MINIBUF);
|
---|
| 569 | #ifndef NO_H_ERRNO
|
---|
| 570 | *errnum = h_errno;
|
---|
| 571 | #else
|
---|
| 572 | *errnum = 666;
|
---|
| 573 | #endif
|
---|
[132] | 574 | (void) sh_tools_errmessage (*errnum, errmsg, errsiz);
|
---|
[1] | 575 | sl_strlcat(errmsg,
|
---|
| 576 | inet_ntoa (*(struct in_addr *) &(sinr.sin_addr)),
|
---|
| 577 | errsiz);
|
---|
| 578 | fail = (-1);
|
---|
| 579 | }
|
---|
| 580 | else
|
---|
| 581 | {
|
---|
| 582 | *errnum = 0;
|
---|
| 583 | if (sl_strlen(host_entry->h_name) == 0 ||
|
---|
| 584 | (*errnum = sl_strcmp(host_name,host_entry->h_name)) != 0)
|
---|
| 585 | {
|
---|
| 586 | if (*errnum)
|
---|
| 587 | sl_strlcpy(ecall, _("strcmp"), SH_MINIBUF);
|
---|
| 588 | else
|
---|
| 589 | sl_strlcpy(ecall, _("strlen"), SH_MINIBUF);
|
---|
| 590 | sl_strlcpy(errmsg, _("Reverse lookup failed."),
|
---|
| 591 | errsiz);
|
---|
| 592 | sl_strlcat(errmsg, address, errsiz);
|
---|
| 593 | sl_strlcat(errmsg, _(" vs "), errsiz);
|
---|
| 594 | sl_strlcat(errmsg,
|
---|
| 595 | inet_ntoa (*(struct in_addr *) &(sinr.sin_addr)),
|
---|
| 596 | errsiz);
|
---|
| 597 | fail = -1;
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 | }
|
---|
| 601 | }
|
---|
[134] | 602 | SH_MUTEX_UNLOCK(mutex_resolv);
|
---|
[143] | 603 | if (host_name) SH_FREE(host_name);
|
---|
[1] | 604 | }
|
---|
| 605 |
|
---|
| 606 | else /* address was numeric */
|
---|
| 607 | {
|
---|
| 608 | sinr.sin_family = AF_INET;
|
---|
| 609 | sinr.sin_port = htons (port);
|
---|
| 610 | sinr.sin_addr = haddr;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 |
|
---|
| 614 | if (fail != -1)
|
---|
| 615 | {
|
---|
| 616 | /* put it into the cache
|
---|
| 617 | */
|
---|
| 618 | check_cache = SH_ALLOC(sizeof(sin_cache));
|
---|
| 619 | check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
|
---|
| 620 | sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
|
---|
| 621 | memcpy(&(check_cache->sin), &sinr, sizeof(struct sockaddr_in));
|
---|
| 622 | ++cached_addr;
|
---|
| 623 |
|
---|
| 624 | if (conn_cache)
|
---|
| 625 | {
|
---|
| 626 | if (conn_cache->next)
|
---|
| 627 | check_cache->next = conn_cache->next;
|
---|
| 628 | else
|
---|
| 629 | check_cache->next = NULL;
|
---|
| 630 | conn_cache->next = check_cache;
|
---|
| 631 | }
|
---|
| 632 | else
|
---|
| 633 | {
|
---|
| 634 | check_cache->next = NULL;
|
---|
| 635 | conn_cache = check_cache;
|
---|
| 636 | }
|
---|
| 637 | }
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 |
|
---|
| 641 | if (fail != (-1))
|
---|
| 642 | {
|
---|
| 643 | fd = socket(AF_INET, SOCK_STREAM, 0);
|
---|
| 644 | if (fd < 0) {
|
---|
| 645 | fail = (-1);
|
---|
| 646 | status = errno;
|
---|
| 647 | sl_strlcpy(ecall, _("socket"), SH_MINIBUF);
|
---|
| 648 | *errnum = status;
|
---|
[132] | 649 | sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
|
---|
[1] | 650 | sl_strlcat(errmsg, _(", address "), errsiz);
|
---|
| 651 | sl_strlcat(errmsg, address, errsiz);
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | if (fail != (-1)) {
|
---|
| 656 |
|
---|
| 657 | if ( retry_connect(FIL__, __LINE__, fd,
|
---|
| 658 | (struct sockaddr *) &sinr, sizeof(sinr)) < 0)
|
---|
| 659 | {
|
---|
| 660 | status = errno;
|
---|
| 661 | sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
|
---|
| 662 | *errnum = status;
|
---|
[132] | 663 | sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
|
---|
[1] | 664 | sl_strlcat(errmsg, _(", address "), errsiz);
|
---|
| 665 | sl_strlcat(errmsg, address, errsiz);
|
---|
| 666 | close(fd);
|
---|
| 667 | fail = (-1);
|
---|
| 668 | }
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | retval = (fail < 0) ? (-1) : fd;
|
---|
| 672 | SL_RETURN(retval, _("connect_port"));
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | int connect_port_2 (char * address1, char * address2, int port,
|
---|
| 676 | char * ecall, int * errnum, char * errmsg, int errsiz)
|
---|
| 677 | {
|
---|
| 678 | int retval = (-1);
|
---|
| 679 |
|
---|
| 680 | SL_ENTER(_("connect_port_2"));
|
---|
| 681 |
|
---|
| 682 | errmsg[0] = '\0';
|
---|
| 683 | *errnum = 0;
|
---|
| 684 |
|
---|
| 685 | if (address1 != NULL && address1[0] != '\0')
|
---|
| 686 | retval = connect_port (address1, port,
|
---|
| 687 | ecall, errnum,
|
---|
| 688 | errmsg, errsiz);
|
---|
| 689 |
|
---|
| 690 | if (retval < 0 && address2 != NULL && address2[0] != '\0')
|
---|
| 691 | {
|
---|
| 692 | /* can't use sh_error_handle here, as this would cause an infinite
|
---|
| 693 | * loop if called from sh_unix_time
|
---|
| 694 | */
|
---|
| 695 | TPT(( 0, FIL__, __LINE__, _("msg=<Using alternative server %s.>\n"),
|
---|
| 696 | address2));
|
---|
| 697 | retval = connect_port (address2, port,
|
---|
| 698 | ecall, errnum,
|
---|
| 699 | errmsg, errsiz);
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | if ((retval < 0) &&
|
---|
| 703 | (address1 == NULL || address1[0] == '\0') &&
|
---|
| 704 | (address1 == NULL || address1[0] == '\0'))
|
---|
| 705 | {
|
---|
| 706 | sl_strlcpy(ecall, _("connect_port_2"), SH_MINIBUF);
|
---|
| 707 | sl_strlcpy(errmsg, _("No server address known"), errsiz);
|
---|
| 708 | }
|
---|
| 709 | SL_RETURN(retval, _("connect_port_2"));
|
---|
| 710 | /* return retval; */
|
---|
| 711 | }
|
---|
| 712 |
|
---|
[27] | 713 | #if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
[1] | 714 | static
|
---|
| 715 | int sh_write_select(int type, int sockfd,
|
---|
| 716 | char *buf, int nbytes,
|
---|
| 717 | int * w_error, int timeout)
|
---|
| 718 | {
|
---|
| 719 | int countbytes, count;
|
---|
| 720 | fd_set fds;
|
---|
| 721 | struct timeval tv;
|
---|
| 722 | int select_now;
|
---|
| 723 | int num_sel;
|
---|
| 724 |
|
---|
| 725 | struct sigaction new_act;
|
---|
| 726 | struct sigaction old_act;
|
---|
[137] | 727 | #if defined(WITH_TPT)
|
---|
[132] | 728 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[137] | 729 | #endif
|
---|
[1] | 730 |
|
---|
| 731 | SL_ENTER(_("sh_write_select"));
|
---|
| 732 |
|
---|
| 733 | /* ignore SIGPIPE (instead get EPIPE if connection is closed)
|
---|
| 734 | */
|
---|
| 735 | new_act.sa_handler = SIG_IGN;
|
---|
[22] | 736 | sigemptyset( &new_act.sa_mask ); /* set an empty mask */
|
---|
| 737 | new_act.sa_flags = 0; /* init sa_flags */
|
---|
[1] | 738 | sigaction (SIGPIPE, &new_act, &old_act);
|
---|
| 739 |
|
---|
| 740 | FD_ZERO(&fds);
|
---|
| 741 | FD_SET(sockfd, &fds);
|
---|
| 742 |
|
---|
| 743 | countbytes = 0;
|
---|
| 744 | tv.tv_sec = 1;
|
---|
| 745 | tv.tv_usec = 0;
|
---|
| 746 | select_now = 0;
|
---|
| 747 |
|
---|
| 748 | *w_error = 0;
|
---|
| 749 |
|
---|
| 750 | while ( countbytes < nbytes ) {
|
---|
| 751 |
|
---|
| 752 | FD_ZERO(&fds);
|
---|
| 753 | FD_SET(sockfd, &fds);
|
---|
| 754 |
|
---|
| 755 | if (type == SH_DO_WRITE)
|
---|
| 756 | {
|
---|
| 757 | if ( (num_sel = select (sockfd+1, NULL, &fds, NULL, &tv)) == -1)
|
---|
| 758 | {
|
---|
| 759 | if (sig_raised == 1)
|
---|
| 760 | {
|
---|
| 761 | sig_raised = 2;
|
---|
| 762 | continue;
|
---|
| 763 | }
|
---|
| 764 | if ( errno == EINTR) /* try again */
|
---|
| 765 | continue;
|
---|
| 766 | *w_error = errno;
|
---|
| 767 | TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"),
|
---|
[132] | 768 | sh_error_message(*w_error, errbuf, sizeof(errbuf))));
|
---|
[1] | 769 | sigaction (SIGPIPE, &old_act, NULL);
|
---|
| 770 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
| 771 | }
|
---|
| 772 | }
|
---|
| 773 | else
|
---|
| 774 | {
|
---|
| 775 | if ( (num_sel = select (sockfd+1, &fds, NULL, NULL, &tv)) == -1)
|
---|
| 776 | {
|
---|
| 777 | if (sig_raised == 1)
|
---|
| 778 | {
|
---|
| 779 | sig_raised = 2;
|
---|
| 780 | continue;
|
---|
| 781 | }
|
---|
| 782 | if ( errno == EINTR ) /* try again */
|
---|
| 783 | continue;
|
---|
| 784 | *w_error = errno;
|
---|
| 785 | TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"),
|
---|
[132] | 786 | sh_error_message(*w_error, errbuf, sizeof(errbuf))));
|
---|
[1] | 787 | sigaction (SIGPIPE, &old_act, NULL);
|
---|
| 788 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
| 789 | }
|
---|
| 790 | }
|
---|
| 791 |
|
---|
| 792 | /* on Linux, timeout is modified to reflect the amount of
|
---|
| 793 | * time not slept
|
---|
| 794 | */
|
---|
| 795 | tv.tv_sec = 1;
|
---|
| 796 | tv.tv_usec = 0;
|
---|
| 797 |
|
---|
| 798 |
|
---|
| 799 | /* let's not hang on forever
|
---|
| 800 | */
|
---|
| 801 | if (num_sel == 0)
|
---|
| 802 | {
|
---|
| 803 | ++select_now; /* timeout */
|
---|
| 804 | if ( select_now > timeout ) /* 5 minutes */
|
---|
| 805 | {
|
---|
| 806 | #ifdef ETIMEDOUT
|
---|
| 807 | *w_error = ETIMEDOUT;
|
---|
| 808 | #else
|
---|
| 809 | *w_error = 0;
|
---|
| 810 | #endif
|
---|
| 811 | sigaction (SIGPIPE, &old_act, NULL);
|
---|
| 812 | TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
|
---|
| 813 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
| 814 | }
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | if ( FD_ISSET (sockfd, &fds) )
|
---|
| 818 | {
|
---|
| 819 | if (type == SH_DO_WRITE)
|
---|
| 820 | count = write (sockfd, buf, nbytes-countbytes);
|
---|
| 821 | else
|
---|
| 822 | count = read (sockfd, buf, nbytes-countbytes);
|
---|
| 823 |
|
---|
| 824 | if (count > 0)
|
---|
| 825 | {
|
---|
| 826 | countbytes += count;
|
---|
| 827 | buf += count; /* move buffer pointer forward */
|
---|
| 828 | if (countbytes < nbytes) FD_SET( sockfd, &fds );
|
---|
| 829 | }
|
---|
| 830 | else if (count < 0 && errno == EINTR)
|
---|
| 831 | {
|
---|
| 832 | FD_SET( sockfd, &fds );
|
---|
| 833 | }
|
---|
| 834 | else if (count < 0)
|
---|
| 835 | {
|
---|
| 836 | *w_error = errno;
|
---|
| 837 | sigaction (SIGPIPE, &old_act, NULL);
|
---|
| 838 | TPT(( 0, FIL__, __LINE__, _("msg=<count < 0>\n")));
|
---|
| 839 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
| 840 | }
|
---|
| 841 | else /* count == 0 */
|
---|
| 842 | {
|
---|
| 843 | *w_error = errno;
|
---|
| 844 | sigaction (SIGPIPE, &old_act, NULL);
|
---|
| 845 | TPT(( 0, FIL__, __LINE__, _("msg=<count == 0>\n")));
|
---|
| 846 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
| 847 | }
|
---|
| 848 | }
|
---|
| 849 | }
|
---|
| 850 |
|
---|
| 851 |
|
---|
| 852 | /* restore signal handler
|
---|
| 853 | */
|
---|
| 854 | sigaction (SIGPIPE, &old_act, NULL);
|
---|
| 855 |
|
---|
| 856 | *w_error = 0;
|
---|
| 857 |
|
---|
| 858 | TPT(( 0, FIL__, __LINE__, _("msg=<count = %d>\n"), countbytes));
|
---|
| 859 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
| 860 | }
|
---|
| 861 | #endif
|
---|
| 862 |
|
---|
[27] | 863 | #if defined (SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
[1] | 864 | unsigned long write_port (int sockfd, char *buf, unsigned long nbytes,
|
---|
| 865 | int * w_error, int timeout)
|
---|
| 866 | {
|
---|
| 867 | unsigned long bytes;
|
---|
| 868 |
|
---|
| 869 | SL_ENTER(_("write_port"));
|
---|
| 870 |
|
---|
| 871 | bytes = sh_write_select(SH_DO_WRITE, sockfd, buf, nbytes, w_error, timeout);
|
---|
| 872 | if (*w_error != 0)
|
---|
| 873 | {
|
---|
[132] | 874 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 875 | sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP,
|
---|
[132] | 876 | sh_error_message (*w_error, errbuf, sizeof(errbuf)),
|
---|
| 877 | (long) sockfd, _("write_port"));
|
---|
[1] | 878 | }
|
---|
| 879 | SL_RETURN( bytes, _("write_port"));
|
---|
| 880 | }
|
---|
| 881 | #endif
|
---|
| 882 |
|
---|
[27] | 883 | #if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
[1] | 884 |
|
---|
| 885 | unsigned long read_port (int sockfd, char *buf, unsigned long nbytes,
|
---|
| 886 | int * w_error, int timeout)
|
---|
| 887 | {
|
---|
| 888 | unsigned long bytes;
|
---|
| 889 |
|
---|
| 890 | SL_ENTER(_("read_port"));
|
---|
| 891 |
|
---|
| 892 | bytes = sh_write_select(SH_DO_READ, sockfd, buf, nbytes, w_error, timeout);
|
---|
| 893 | if (*w_error != 0)
|
---|
| 894 | {
|
---|
[132] | 895 | char errbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 896 | sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP,
|
---|
[132] | 897 | sh_error_message (*w_error, errbuf, sizeof(errbuf)),
|
---|
| 898 | (long) sockfd, _("read_port"));
|
---|
[1] | 899 | }
|
---|
| 900 | SL_RETURN( bytes, _("read_port"));
|
---|
| 901 | }
|
---|
| 902 | #endif
|
---|
| 903 |
|
---|
| 904 | #if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
| 905 |
|
---|
| 906 | int check_request_nerr (char * have, char * need)
|
---|
| 907 | {
|
---|
| 908 | SL_ENTER(_("check_request_nerr"));
|
---|
| 909 | ASSERT_RET((have != NULL && need != NULL),
|
---|
| 910 | _("have != NULL && need != NULL"), (-1))
|
---|
| 911 |
|
---|
| 912 | if ( (have[0] == need[0]) && (have[1] == need[1]) &&
|
---|
| 913 | (have[2] == need[2]) && (have[3] == need[3]))
|
---|
| 914 | SL_RETURN(0, _("check_request_nerr"));
|
---|
| 915 | SL_RETURN((-1), _("check_request_nerr"));
|
---|
| 916 | }
|
---|
| 917 | #endif
|
---|
| 918 |
|
---|
[27] | 919 | #if defined (SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
[1] | 920 |
|
---|
| 921 | int check_request (char * have, char * need)
|
---|
| 922 | {
|
---|
| 923 | char first[21], second[5];
|
---|
| 924 | int i;
|
---|
| 925 |
|
---|
| 926 | SL_ENTER(_("check_request"));
|
---|
| 927 | i = check_request_nerr (have, need);
|
---|
| 928 |
|
---|
| 929 | if (i == 0)
|
---|
| 930 | SL_RETURN(0, _("check_request"));
|
---|
| 931 |
|
---|
| 932 | for (i = 0; i < 4; ++i)
|
---|
| 933 | {
|
---|
| 934 | second[i] = need[i];
|
---|
| 935 | sprintf(&first[i*4], _("%c%03o"), /* known to fit */
|
---|
| 936 | '\\', (unsigned char) have[i]);
|
---|
| 937 | }
|
---|
| 938 |
|
---|
| 939 | first[20] = '\0'; second[4] = '\0';
|
---|
| 940 |
|
---|
| 941 | sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_NETST,
|
---|
| 942 | second, first);
|
---|
| 943 | SL_RETURN((-1), _("check_request"));
|
---|
| 944 | }
|
---|
| 945 | #endif
|
---|
| 946 |
|
---|
| 947 | #if defined (SH_WITH_SERVER)
|
---|
| 948 |
|
---|
| 949 | int check_request_s (char * have, char * need, char * clt)
|
---|
| 950 | {
|
---|
| 951 | char first[21], second[5];
|
---|
| 952 | int i;
|
---|
| 953 |
|
---|
| 954 | SL_ENTER(_("check_request_s"));
|
---|
| 955 | i = check_request_nerr (have, need);
|
---|
| 956 |
|
---|
| 957 | if (i == 0)
|
---|
| 958 | SL_RETURN( (0), _("check_request_s"));
|
---|
| 959 |
|
---|
| 960 | for (i = 0; i < 4; ++i)
|
---|
| 961 | {
|
---|
| 962 | second[i] = need[i];
|
---|
| 963 | sprintf(&first[i*4], _("%c%03o"), /* known to fit */
|
---|
| 964 | '\\', (unsigned char) have[i]);
|
---|
| 965 | }
|
---|
| 966 | first[20] = '\0'; second[4] = '\0';
|
---|
| 967 | sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_NETST1,
|
---|
| 968 | second, first, clt);
|
---|
| 969 | SL_RETURN( (-1), _("check_request_s"));
|
---|
| 970 | }
|
---|
| 971 | #endif
|
---|
| 972 |
|
---|
[27] | 973 | #if defined (SH_WITH_CLIENT) || defined (SH_WITH_SERVER)
|
---|
[1] | 974 |
|
---|
| 975 | void get_header (unsigned char * head, unsigned long * bytes, char * u)
|
---|
| 976 | {
|
---|
| 977 | SL_ENTER(_("get_header"));
|
---|
| 978 |
|
---|
| 979 | *bytes =
|
---|
| 980 | (256 * (unsigned int)head[1] + (unsigned int)head[2]);
|
---|
| 981 |
|
---|
| 982 | if (u != NULL)
|
---|
| 983 | {
|
---|
| 984 | u[0] = head[3];
|
---|
| 985 | u[1] = head[4];
|
---|
| 986 | u[2] = head[5];
|
---|
| 987 | u[3] = head[6];
|
---|
| 988 | u[4] = '\0';
|
---|
| 989 | }
|
---|
| 990 |
|
---|
| 991 | SL_RET0(_("get_header"));
|
---|
| 992 | }
|
---|
| 993 | #endif
|
---|
| 994 |
|
---|
| 995 | #if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
| 996 |
|
---|
| 997 | #ifdef SH_ENCRYPT_2
|
---|
| 998 | #define TRANS_BYTES 65120
|
---|
| 999 | #else
|
---|
| 1000 | #define TRANS_BYTES 65280
|
---|
| 1001 | #endif
|
---|
| 1002 |
|
---|
| 1003 | void put_header (unsigned char * head, int protocol,
|
---|
| 1004 | unsigned long * length, char * u)
|
---|
| 1005 | {
|
---|
| 1006 |
|
---|
| 1007 | /* static long transfer_limit = (8 * SH_BUFSIZE); V0.8 */
|
---|
| 1008 | static unsigned long transfer_limit = TRANS_BYTES + 6 + KEY_LEN;
|
---|
| 1009 |
|
---|
| 1010 | SL_ENTER(_("put_header"));
|
---|
| 1011 |
|
---|
| 1012 | head[0] = protocol;
|
---|
| 1013 |
|
---|
| 1014 | ASSERT((*length < transfer_limit), _("*length < transfer_limit"))
|
---|
| 1015 |
|
---|
| 1016 | if (*length > transfer_limit)
|
---|
| 1017 | *length = transfer_limit;
|
---|
| 1018 |
|
---|
| 1019 | head[1] = (unsigned int)(*length/256);
|
---|
| 1020 | head[2] = (unsigned int)(*length-256 * head[1]);
|
---|
| 1021 | if (u == NULL)
|
---|
| 1022 | {
|
---|
| 1023 | head[3] = 0x01;
|
---|
| 1024 | head[4] = 0x01;
|
---|
| 1025 | head[5] = 0x01;
|
---|
| 1026 | head[6] = 0x01;
|
---|
| 1027 | }
|
---|
| 1028 | else
|
---|
| 1029 | {
|
---|
| 1030 | head[3] = u[0];
|
---|
| 1031 | head[4] = u[1];
|
---|
| 1032 | head[5] = u[2];
|
---|
| 1033 | head[6] = u[3];
|
---|
| 1034 | }
|
---|
| 1035 |
|
---|
| 1036 | SL_RET0(_("put_header"));
|
---|
| 1037 | }
|
---|
| 1038 | #endif
|
---|
| 1039 |
|
---|
| 1040 | /* ------------------------------------------
|
---|
| 1041 | *
|
---|
| 1042 | * version 2 client/server protocol
|
---|
| 1043 | *
|
---|
| 1044 | * ------------------------------------------
|
---|
| 1045 | *
|
---|
| 1046 | * header : flag size[2]
|
---|
| 1047 | *
|
---|
| 1048 | * payload: random_pad[8] protocol[4] size[4] payload[payload_size] padding
|
---|
| 1049 | *
|
---|
| 1050 | * full_size <= 8192; payload_size <= 8176 (511*16); msg_size <= 8128 (508*16)
|
---|
| 1051 | * (msg_size = payload_size - key_len = payload_size - 48)
|
---|
| 1052 | */
|
---|
[27] | 1053 |
|
---|
| 1054 | /*
|
---|
| 1055 | * only SH_V2_FULLSIZE is used, and only once
|
---|
| 1056 | */
|
---|
| 1057 | #if 0
|
---|
[1] | 1058 | #ifdef SH_WITH_SERVER
|
---|
| 1059 | #define SH_V2_FULLSIZE 240
|
---|
| 1060 | #define SH_V2_PAYLOAD 224
|
---|
| 1061 | #define SH_V2_MESSAGE 176
|
---|
| 1062 | #else
|
---|
| 1063 | #define SH_V2_FULLSIZE 1024
|
---|
| 1064 | #define SH_V2_PAYLOAD 1008
|
---|
| 1065 | #define SH_V2_MESSAGE 960
|
---|
| 1066 | #endif
|
---|
[27] | 1067 | #endif
|
---|
| 1068 | #define SH_V2_FULLSIZE 1024
|
---|
[1] | 1069 |
|
---|
| 1070 | #ifdef SH_ENCRYPT
|
---|
| 1071 | #include "rijndael-api-fst.h"
|
---|
| 1072 | #endif
|
---|
| 1073 |
|
---|
| 1074 | void sh_tools_show_header (unsigned char * head, char sign)
|
---|
| 1075 | {
|
---|
| 1076 | #define SH_IS_ASCII(c) (((c) & ~0x7f) == 0)
|
---|
| 1077 |
|
---|
| 1078 |
|
---|
| 1079 | int msg_size = (256 * (unsigned int)head[1] + (unsigned int)head[2]);
|
---|
| 1080 | char code[32];
|
---|
| 1081 | char * p = &code[0];
|
---|
| 1082 |
|
---|
| 1083 | memset (code, ' ', 32); /* space */
|
---|
| 1084 |
|
---|
| 1085 | if ((head[0] & SH_PROTO_SRP) != 0) { p[0]='S';p[1]='R';p[2]='P';}
|
---|
| 1086 | p += 4;
|
---|
| 1087 | if ((head[0] & SH_PROTO_MSG) != 0) { p[0]='M';p[1]='S';p[2]='G';}
|
---|
| 1088 | p += 4;
|
---|
| 1089 | if ((head[0] & SH_PROTO_BIG) != 0) { p[0]='B';p[1]='I';p[2]='G';}
|
---|
| 1090 | p += 4;
|
---|
| 1091 | if ((head[0] & SH_PROTO_END) != 0) { p[0]='E';p[1]='N';p[2]='D';}
|
---|
| 1092 | p += 4;
|
---|
| 1093 | if ((head[0] & SH_PROTO_ENC) != 0) { p[0]='E';p[1]='N';p[2]='C';}
|
---|
| 1094 | p += 4;
|
---|
| 1095 | if ((head[0] & SH_PROTO_EN2) != 0) { p[0]='E';p[1]='N';p[2]='2';}
|
---|
| 1096 | code[23] = '\0';
|
---|
| 1097 |
|
---|
| 1098 | if (SH_IS_ASCII(head[3]) && isalpha(head[3]) &&
|
---|
| 1099 | SH_IS_ASCII(head[4]) && isalpha(head[4]) &&
|
---|
| 1100 | SH_IS_ASCII(head[5]) && isalpha(head[5]) &&
|
---|
| 1101 | SH_IS_ASCII(head[6]) && isalpha(head[6])) {
|
---|
| 1102 | fprintf(stderr, "%c %3o %s %5d %c %c %c %c\n", sign,
|
---|
| 1103 | head[0], code, msg_size, head[3], head[4], head[5], head[6]);
|
---|
| 1104 | } else {
|
---|
| 1105 | fprintf(stderr, "%c %3o %s %5d %2X %2X %2X %2X\n", sign,
|
---|
| 1106 | head[0], code, msg_size, head[3], head[4], head[5], head[6]);
|
---|
| 1107 | }
|
---|
| 1108 | return;
|
---|
| 1109 | }
|
---|
| 1110 |
|
---|
| 1111 | #ifdef SH_ENCRYPT
|
---|
| 1112 | /*
|
---|
| 1113 | * #define DEBUG_EN2
|
---|
| 1114 | *
|
---|
| 1115 | * ingest version 1 7-byte header and payload, return version2 header/payload
|
---|
| 1116 | * last 4 bytes of outgoing header are set to dummy value
|
---|
| 1117 | */
|
---|
| 1118 | char * sh_tools_makePack (unsigned char * header,
|
---|
[34] | 1119 | char * payload, unsigned long payload_size,
|
---|
[1] | 1120 | keyInstance * keyInstE)
|
---|
| 1121 | {
|
---|
| 1122 | UINT32 rpad[3];
|
---|
| 1123 | unsigned char head[16];
|
---|
| 1124 | double epad;
|
---|
[34] | 1125 | unsigned long i_epad = 0;
|
---|
| 1126 | unsigned long i_blk = payload_size / 16;
|
---|
| 1127 | unsigned long i_blkmax = SH_V2_FULLSIZE / 16;
|
---|
| 1128 | unsigned long pads = 0;
|
---|
| 1129 | size_t full_size;
|
---|
[1] | 1130 | char * full_ret;
|
---|
| 1131 |
|
---|
| 1132 | char * p;
|
---|
| 1133 | RIJ_BYTE inBlock[B_SIZ];
|
---|
| 1134 | RIJ_BYTE outBlock[B_SIZ];
|
---|
| 1135 | int j;
|
---|
| 1136 | cipherInstance cipherInst;
|
---|
| 1137 | int err_num;
|
---|
| 1138 | int blkfac;
|
---|
[34] | 1139 | int oflow = 0;
|
---|
[1] | 1140 |
|
---|
| 1141 | /*
|
---|
| 1142 | SL_REQUIRE (i_blk*16 == payload_size, _("payload_size % 16 != 0"));
|
---|
| 1143 | */
|
---|
| 1144 | if ((i_blk * 16) != payload_size) ++i_blk;
|
---|
| 1145 | #ifdef DEBUG_EN2
|
---|
| 1146 | fprintf(stderr, "SEND <%d> blocks <%d>\n", payload_size, i_blk);
|
---|
| 1147 | #endif
|
---|
| 1148 | /* random_pad
|
---|
| 1149 | */
|
---|
[137] | 1150 | rpad[1] = taus_get ();
|
---|
[1] | 1151 | memcpy (head, &rpad[1], 4);
|
---|
[137] | 1152 | rpad[0] = taus_get ();
|
---|
[1] | 1153 | memcpy (&head[4], &rpad[0], 4);
|
---|
[137] | 1154 | rpad[2] = taus_get ();
|
---|
[1] | 1155 | memcpy (&head[8], &rpad[2], 4);
|
---|
| 1156 |
|
---|
| 1157 | /* protocol
|
---|
| 1158 | */
|
---|
| 1159 | /* memcpy (&head[8], &header[3], 4); */
|
---|
| 1160 |
|
---|
| 1161 | /* size (payload)
|
---|
| 1162 | */
|
---|
| 1163 | head[12] = header[1];
|
---|
| 1164 | head[13] = header[2];
|
---|
| 1165 | head[14] = '\0';
|
---|
| 1166 | head[15] = '\0';
|
---|
| 1167 |
|
---|
| 1168 | if (i_blk < i_blkmax)
|
---|
| 1169 | {
|
---|
| 1170 | pads = i_blkmax - i_blk;
|
---|
| 1171 | /* memcpy((char *) &rpad[2], &head[12], 4); */
|
---|
| 1172 | epad = taus_get_double (&rpad);
|
---|
| 1173 | #ifdef DEBUG_EN2
|
---|
| 1174 | fprintf(stderr, "PAD1 <%d> <%f>\n", pads, epad);
|
---|
| 1175 | #endif
|
---|
[34] | 1176 | i_epad = (unsigned long) (pads * epad);
|
---|
[1] | 1177 | #ifdef DEBUG_EN2
|
---|
| 1178 | fprintf(stderr, "PAD2 <%d> <%d>\n", i_epad, (i_epad*16));
|
---|
| 1179 | #endif
|
---|
| 1180 | }
|
---|
| 1181 |
|
---|
[34] | 1182 | full_size = 16; /* head */
|
---|
| 1183 | if (sl_ok_muls(i_blk, 16) && sl_ok_adds(full_size, (i_blk*16)))
|
---|
| 1184 | full_size = full_size + (i_blk*16); /* payload */
|
---|
| 1185 | else
|
---|
| 1186 | oflow = 1;
|
---|
| 1187 | if (sl_ok_adds(full_size, (i_epad*16)))
|
---|
| 1188 | full_size = full_size + (i_epad*16); /* pad */
|
---|
| 1189 | else
|
---|
| 1190 | i_epad = 0;
|
---|
[1] | 1191 |
|
---|
[34] | 1192 | if (oflow)
|
---|
| 1193 | {
|
---|
| 1194 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
| 1195 | _("integer overflow"),
|
---|
| 1196 | _("sh_tools_makePack"));
|
---|
| 1197 | }
|
---|
| 1198 |
|
---|
[1] | 1199 | full_ret = SH_ALLOC(full_size);
|
---|
| 1200 | memcpy(full_ret, head, 16);
|
---|
[34] | 1201 | if (payload != NULL && !oflow)
|
---|
[1] | 1202 | {
|
---|
| 1203 | memcpy(&full_ret[16], payload, payload_size);
|
---|
| 1204 | }
|
---|
[34] | 1205 | if ((i_blk*16) > payload_size && !oflow)
|
---|
[1] | 1206 | {
|
---|
| 1207 | #ifdef DEBUG_EN2
|
---|
| 1208 | fprintf(stderr, "SEN2 <%d>\n", (i_blk*16) - payload_size);
|
---|
| 1209 | #endif
|
---|
| 1210 | memset(&full_ret[16+payload_size], '\0', (i_blk*16) - payload_size);
|
---|
| 1211 | payload_size = i_blk * 16;
|
---|
| 1212 | }
|
---|
| 1213 | memset(&full_ret[16+payload_size], '\0', i_epad*16);
|
---|
| 1214 | #ifdef DEBUG_EN2
|
---|
| 1215 | fprintf(stderr, "SEN3 <%d> <%d>\n", full_size, i_epad*16);
|
---|
| 1216 | #endif
|
---|
| 1217 |
|
---|
| 1218 | /* rewrite header
|
---|
| 1219 | */
|
---|
| 1220 | header[1] = (unsigned int)(full_size/256);
|
---|
| 1221 | header[2] = (unsigned int)(full_size - (256 * header[1]));
|
---|
| 1222 | /* don't erase protocol from header
|
---|
| 1223 | memset(&header[3], '\0', 4);
|
---|
| 1224 | */
|
---|
| 1225 | p = full_ret; blkfac = full_size / 16;
|
---|
| 1226 |
|
---|
| 1227 | err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
|
---|
| 1228 |
|
---|
| 1229 | if (err_num < 0)
|
---|
| 1230 | {
|
---|
[132] | 1231 | char expbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 1232 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
[132] | 1233 | errorExplain(err_num, expbuf, sizeof(expbuf)),
|
---|
[1] | 1234 | _("sh_tools_makePack: cipherInit"));
|
---|
| 1235 | }
|
---|
| 1236 | for (j = 0; j < blkfac; ++j)
|
---|
| 1237 | {
|
---|
| 1238 | memcpy(inBlock, p, B_SIZ);
|
---|
| 1239 | err_num = blockEncrypt(&cipherInst, keyInstE,
|
---|
| 1240 | inBlock, 128 * BNUM, outBlock);
|
---|
| 1241 | if (err_num < 0)
|
---|
| 1242 | {
|
---|
[132] | 1243 | char expbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 1244 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
[132] | 1245 | errorExplain(err_num, expbuf, sizeof(expbuf)),
|
---|
[1] | 1246 | _("sh_tools_makePack: blockEncrypt"));
|
---|
| 1247 | }
|
---|
| 1248 | memcpy(p, outBlock, B_SIZ);
|
---|
| 1249 | p += B_SIZ;
|
---|
| 1250 | }
|
---|
| 1251 |
|
---|
| 1252 | return full_ret;
|
---|
| 1253 | }
|
---|
| 1254 |
|
---|
| 1255 | /* write a 7-byte header and return payload as expected by version 1
|
---|
| 1256 | * last 4 bytes of incoming header are dummy
|
---|
| 1257 | */
|
---|
| 1258 | char * sh_tools_revertPack (unsigned char * header, char * message,
|
---|
| 1259 | keyInstance * keyInstD,
|
---|
| 1260 | unsigned long message_size)
|
---|
| 1261 | {
|
---|
| 1262 | unsigned long msg_size;
|
---|
| 1263 | char * msg_ret;
|
---|
| 1264 |
|
---|
| 1265 | char * p;
|
---|
| 1266 | RIJ_BYTE inBlock[B_SIZ];
|
---|
| 1267 | RIJ_BYTE outBlock[B_SIZ];
|
---|
| 1268 | int j;
|
---|
| 1269 | cipherInstance cipherInst;
|
---|
| 1270 | int err_num;
|
---|
| 1271 | int blkfac;
|
---|
[132] | 1272 | char expbuf[SH_ERRBUF_SIZE];
|
---|
[1] | 1273 |
|
---|
| 1274 | msg_size = (256 * (unsigned int)header[1] + (unsigned int)header[2]);
|
---|
| 1275 | #ifdef DEBUG_EN2
|
---|
| 1276 | fprintf(stderr, "RECV <%lu>\n", msg_size);
|
---|
| 1277 | #endif
|
---|
| 1278 | if (msg_size > message_size) {
|
---|
| 1279 | msg_size = message_size;
|
---|
| 1280 | #ifdef DEBUG_EN2
|
---|
| 1281 | fprintf(stderr, "RECV TRUNC1 <%lu>\n", msg_size);
|
---|
| 1282 | #endif
|
---|
| 1283 | }
|
---|
| 1284 |
|
---|
| 1285 | p = message; blkfac = msg_size / 16;
|
---|
| 1286 |
|
---|
| 1287 | err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
|
---|
| 1288 |
|
---|
| 1289 | if (err_num < 0)
|
---|
| 1290 | {
|
---|
| 1291 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
[132] | 1292 | errorExplain(err_num, expbuf, sizeof(expbuf)),
|
---|
[1] | 1293 | _("sh_tools_revertPack: cipherInit"));
|
---|
| 1294 | }
|
---|
| 1295 | for (j = 0; j < blkfac; ++j)
|
---|
| 1296 | {
|
---|
| 1297 | memcpy(inBlock, p, B_SIZ);
|
---|
| 1298 | err_num = blockDecrypt(&cipherInst, keyInstD,
|
---|
| 1299 | inBlock, 128 * BNUM, outBlock);
|
---|
| 1300 | if (err_num < 0)
|
---|
| 1301 | {
|
---|
| 1302 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
[132] | 1303 | errorExplain(err_num, expbuf, sizeof(expbuf)),
|
---|
[1] | 1304 | _("sh_tools_revertPack: blockDecrypt"));
|
---|
| 1305 | }
|
---|
| 1306 | memcpy(p, outBlock, B_SIZ);
|
---|
| 1307 | p += B_SIZ;
|
---|
| 1308 | }
|
---|
| 1309 |
|
---|
| 1310 | /* rewrite size in header
|
---|
| 1311 | */
|
---|
| 1312 | header[1] = message[12];
|
---|
| 1313 | header[2] = message[13];
|
---|
| 1314 | msg_size = (256 * (unsigned int)header[1] + (unsigned int)header[2]);
|
---|
| 1315 |
|
---|
| 1316 | if (msg_size > (message_size-16))
|
---|
| 1317 | {
|
---|
| 1318 | msg_size = message_size-16;
|
---|
| 1319 | header[1] = (unsigned int)(msg_size/256);
|
---|
| 1320 | header[2] = (unsigned int)(msg_size - (256 * header[1]));
|
---|
| 1321 | #ifdef DEBUG_EN2
|
---|
| 1322 | fprintf(stderr, "RECV TRUNC2 <%lu>\n", msg_size);
|
---|
| 1323 | #endif
|
---|
| 1324 | }
|
---|
| 1325 | #ifdef DEBUG_EN2
|
---|
| 1326 | fprintf(stderr, "REC2 <%lu>\n", msg_size);
|
---|
| 1327 | #endif
|
---|
| 1328 | /* protocol
|
---|
| 1329 | */
|
---|
| 1330 | /* memcpy(&header[3], &message[8], 4); */
|
---|
| 1331 |
|
---|
| 1332 | /* payload
|
---|
| 1333 | */
|
---|
| 1334 | msg_ret = SH_ALLOC(msg_size+1);
|
---|
| 1335 | if (msg_size > 0)
|
---|
| 1336 | {
|
---|
| 1337 | memcpy(msg_ret, &message[16], msg_size);
|
---|
| 1338 | }
|
---|
| 1339 | msg_ret[msg_size] = '\0';
|
---|
| 1340 | #ifdef DEBUG_EN2
|
---|
| 1341 | fprintf(stderr, "REC3 <%lu>\n", msg_size);
|
---|
| 1342 | #endif
|
---|
| 1343 | SH_FREE(message);
|
---|
| 1344 |
|
---|
| 1345 | return msg_ret;
|
---|
| 1346 | }
|
---|
| 1347 | #endif
|
---|
| 1348 |
|
---|
| 1349 | int sh_tools_hash_add(char * key, char * buf, int buflen)
|
---|
| 1350 | {
|
---|
| 1351 | char * theSig;
|
---|
[133] | 1352 | char sigbuf[KEYBUF_SIZE];
|
---|
[1] | 1353 |
|
---|
| 1354 | SL_ENTER(_("sh_tools_hash_add"));
|
---|
| 1355 |
|
---|
[133] | 1356 | theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
|
---|
[1] | 1357 | sl_strlcat(buf, theSig, buflen + KEY_LEN + 1);
|
---|
| 1358 |
|
---|
| 1359 | SL_RETURN((0), _("sh_tools_hash_add"));
|
---|
| 1360 | }
|
---|
| 1361 |
|
---|
| 1362 |
|
---|
| 1363 | /* return 0 (== FALSE) if no match, else 1 (== TRUE)
|
---|
| 1364 | */
|
---|
| 1365 | int sh_tools_hash_vfy(char * key, char * buf, int buflen)
|
---|
| 1366 | {
|
---|
| 1367 | char hash[KEY_LEN+1];
|
---|
| 1368 | register int i;
|
---|
| 1369 | char * theSig;
|
---|
[133] | 1370 | char sigbuf[KEYBUF_SIZE];
|
---|
[1] | 1371 |
|
---|
| 1372 | SL_ENTER(_("sh_tools_hash_vfy"));
|
---|
| 1373 |
|
---|
[133] | 1374 | theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
|
---|
[1] | 1375 | sl_strlcpy(hash, theSig, KEY_LEN+1);
|
---|
| 1376 |
|
---|
| 1377 | for (i = 0; i < KEY_LEN; ++i)
|
---|
| 1378 | {
|
---|
| 1379 | if (buf[buflen + i] != hash[i])
|
---|
| 1380 | SL_RETURN((0), _("sh_tools_hash_vfy"));
|
---|
| 1381 | }
|
---|
| 1382 |
|
---|
| 1383 | SL_RETURN((1), _("sh_tools_hash_vfy"));
|
---|
| 1384 | }
|
---|
| 1385 |
|
---|
| 1386 | /* ------------------------------------------ */
|
---|
| 1387 |
|
---|
| 1388 | #if defined (SH_WITH_SERVER)
|
---|
| 1389 |
|
---|
| 1390 | /* add a checksum to a buffer; put checksum in front
|
---|
| 1391 | */
|
---|
| 1392 | char * hash_me (char * key, char * buf, int buflen)
|
---|
| 1393 | {
|
---|
| 1394 | char hash[KEY_LEN+1];
|
---|
[34] | 1395 | char * temp = NULL;
|
---|
[1] | 1396 | register int i;
|
---|
| 1397 | int total = 0;
|
---|
| 1398 | char * theSig;
|
---|
[133] | 1399 | char sigbuf[KEYBUF_SIZE];
|
---|
[1] | 1400 |
|
---|
| 1401 |
|
---|
| 1402 | SL_ENTER(_("hash_me"));
|
---|
| 1403 |
|
---|
| 1404 | #ifdef DEBUG_EN2
|
---|
| 1405 | fprintf(stderr, "hash_me <%s> <%d>\n",
|
---|
| 1406 | (key == NULL) ? "NULL" : key, buflen);
|
---|
| 1407 | #endif
|
---|
| 1408 | /* key = H(NSRV,NCLT,SK)
|
---|
| 1409 | */
|
---|
| 1410 | ASSERT_RET((key != NULL), _("key != NULL"), (NULL));
|
---|
| 1411 | ASSERT_RET((buflen >= 0), _("buflen >= 0"), (NULL));
|
---|
| 1412 |
|
---|
[133] | 1413 | theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
|
---|
[1] | 1414 | sl_strlcpy(hash, theSig, KEY_LEN+1);
|
---|
| 1415 |
|
---|
[34] | 1416 | if (sl_ok_adds(buflen, KEY_LEN))
|
---|
| 1417 | {
|
---|
| 1418 | total = KEY_LEN + buflen;
|
---|
| 1419 | temp = SH_ALLOC (total);
|
---|
[1] | 1420 |
|
---|
[34] | 1421 | for (i = 0; i < KEY_LEN; ++i)
|
---|
| 1422 | temp[i] = hash[i];
|
---|
[1] | 1423 |
|
---|
[34] | 1424 | for (i = 0; i < buflen; ++i)
|
---|
| 1425 | temp[i+KEY_LEN] = buf[i];
|
---|
| 1426 | }
|
---|
| 1427 | else
|
---|
| 1428 | {
|
---|
| 1429 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
| 1430 | _("integer overflow"),
|
---|
| 1431 | _("hash_me"));
|
---|
| 1432 | temp = sh_util_strdup(buf);
|
---|
| 1433 | }
|
---|
[1] | 1434 | SL_RETURN(temp, _("hash_me"));
|
---|
| 1435 | }
|
---|
| 1436 | #endif
|
---|
| 1437 |
|
---|
[27] | 1438 | #if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
[1] | 1439 |
|
---|
| 1440 | /* verify the checksum of a buffer; checksum comes first
|
---|
| 1441 | */
|
---|
| 1442 | int hash_check(char * key,
|
---|
| 1443 | char * buf, int buflen)
|
---|
| 1444 | {
|
---|
| 1445 | char hash[KEY_LEN+1];
|
---|
| 1446 | register int i;
|
---|
| 1447 | char * theSig;
|
---|
[133] | 1448 | char sigbuf[KEYBUF_SIZE];
|
---|
[1] | 1449 |
|
---|
| 1450 | SL_ENTER(_("hash_check"));
|
---|
| 1451 |
|
---|
| 1452 | #ifdef DEBUG_EN2
|
---|
| 1453 | fprintf(stderr, "hash_check <%s> <%d>\n",
|
---|
| 1454 | (key == NULL) ? "NULL" : key, buflen);
|
---|
| 1455 | #endif
|
---|
[133] | 1456 | theSig = sh_util_siggen (key, &buf[KEY_LEN], buflen-KEY_LEN,
|
---|
| 1457 | sigbuf, sizeof(sigbuf));
|
---|
[1] | 1458 | sl_strlcpy(hash, theSig, KEY_LEN+1);
|
---|
| 1459 |
|
---|
| 1460 | for (i = 0; i < KEY_LEN; ++i)
|
---|
| 1461 | {
|
---|
| 1462 | if (buf[i] != hash[i])
|
---|
| 1463 | SL_RETURN((-1), _("hash_check"));
|
---|
| 1464 | }
|
---|
| 1465 | SL_RETURN((0), _("hash_check"));
|
---|
| 1466 | }
|
---|
| 1467 |
|
---|
| 1468 | #endif
|
---|
| 1469 |
|
---|
| 1470 | #if defined (SH_WITH_SERVER)
|
---|
| 1471 |
|
---|
| 1472 | char * get_client_conf_file (char * peer, unsigned long * length)
|
---|
| 1473 | {
|
---|
| 1474 | char * ret;
|
---|
[34] | 1475 | int status;
|
---|
[1] | 1476 | struct stat buf;
|
---|
| 1477 | char * base;
|
---|
[34] | 1478 | size_t size;
|
---|
[1] | 1479 |
|
---|
| 1480 | SL_ENTER(_("get_client_conf_file"));
|
---|
| 1481 |
|
---|
[34] | 1482 | base = sh_util_strdup(DEFAULT_DATAROOT);
|
---|
[1] | 1483 |
|
---|
[34] | 1484 | size = sl_strlen(base);
|
---|
| 1485 | if (sl_ok_adds(size, sl_strlen(peer)))
|
---|
| 1486 | size += sl_strlen(peer);
|
---|
| 1487 | if (sl_ok_adds(size, 6))
|
---|
| 1488 | size += 6;
|
---|
[1] | 1489 |
|
---|
| 1490 | ret = SH_ALLOC(size);
|
---|
| 1491 | sl_strlcpy(ret, base, size);
|
---|
| 1492 | sl_strlcat(ret, _("/rc."), size);
|
---|
| 1493 | sl_strlcat(ret, peer, size);
|
---|
| 1494 |
|
---|
| 1495 | status = retry_stat (FIL__, __LINE__, ret, &buf);
|
---|
| 1496 |
|
---|
| 1497 | if (status == 0)
|
---|
| 1498 | goto lab_end;
|
---|
| 1499 | else
|
---|
| 1500 | sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
|
---|
| 1501 | (long) sh.effective.uid, ret);
|
---|
| 1502 |
|
---|
| 1503 | sl_strlcpy(ret, base, size);
|
---|
| 1504 | sl_strlcat(ret, "/rc", size);
|
---|
| 1505 |
|
---|
| 1506 | status = retry_stat (FIL__, __LINE__, ret, &buf);
|
---|
| 1507 |
|
---|
| 1508 | if (status == 0)
|
---|
| 1509 | goto lab_end;
|
---|
| 1510 | else
|
---|
| 1511 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_ACCESS,
|
---|
| 1512 | (long) sh.effective.uid, ret);
|
---|
| 1513 |
|
---|
| 1514 | SH_FREE(base);
|
---|
[34] | 1515 | SH_FREE(ret);
|
---|
[1] | 1516 | *length=0;
|
---|
| 1517 | SL_RETURN(NULL, _("get_client_conf_file"));
|
---|
| 1518 |
|
---|
| 1519 | lab_end:
|
---|
| 1520 | if (buf.st_size > 0x7fffffff)
|
---|
| 1521 | {
|
---|
| 1522 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
| 1523 | _("File too large"), _("get_client_conf_file"));
|
---|
| 1524 | SH_FREE(base);
|
---|
| 1525 | SL_RETURN(NULL, _("get_client_conf_file"));
|
---|
| 1526 | }
|
---|
| 1527 | *length = (unsigned long) buf.st_size;
|
---|
| 1528 | SH_FREE(base);
|
---|
| 1529 | SL_RETURN(ret, _("get_client_conf_file"));
|
---|
| 1530 | }
|
---|
| 1531 |
|
---|
| 1532 | char * get_client_data_file (char * peer, unsigned long * length)
|
---|
| 1533 | {
|
---|
| 1534 | char * ret;
|
---|
[34] | 1535 | int status;
|
---|
[1] | 1536 | struct stat buf;
|
---|
| 1537 |
|
---|
| 1538 | char * base;
|
---|
[34] | 1539 | size_t size;
|
---|
[1] | 1540 |
|
---|
| 1541 | SL_ENTER(_("get_client_data_file"));
|
---|
| 1542 |
|
---|
[34] | 1543 | base = sh_util_strdup(DEFAULT_DATAROOT);
|
---|
[1] | 1544 |
|
---|
[34] | 1545 | size = sl_strlen(base);
|
---|
| 1546 | if (sl_ok_adds(size, sl_strlen(peer)))
|
---|
| 1547 | size += sl_strlen(peer);
|
---|
| 1548 | if (sl_ok_adds(size, 8))
|
---|
| 1549 | size += 8;
|
---|
[1] | 1550 |
|
---|
| 1551 | ret = SH_ALLOC(size);
|
---|
| 1552 | sl_strlcpy(ret, base, size);
|
---|
| 1553 | sl_strlcat(ret, _("/file."), size);
|
---|
| 1554 | sl_strlcat(ret, peer, size);
|
---|
| 1555 |
|
---|
| 1556 | status = retry_stat (FIL__, __LINE__, ret, &buf);
|
---|
| 1557 |
|
---|
| 1558 | if (status == 0)
|
---|
| 1559 | goto lab1_end;
|
---|
| 1560 | else
|
---|
| 1561 | sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
|
---|
| 1562 | (long) sh.effective.uid, ret);
|
---|
| 1563 |
|
---|
| 1564 |
|
---|
| 1565 | sl_strlcpy(ret, base, size);
|
---|
| 1566 | sl_strlcat(ret, _("/file"), size);
|
---|
| 1567 |
|
---|
| 1568 | status = retry_stat (FIL__, __LINE__, ret, &buf);
|
---|
| 1569 |
|
---|
| 1570 | if (status == 0)
|
---|
| 1571 | goto lab1_end;
|
---|
| 1572 | else
|
---|
| 1573 | sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
|
---|
| 1574 | (long) sh.effective.uid, ret);
|
---|
| 1575 |
|
---|
| 1576 |
|
---|
| 1577 | *length = 0;
|
---|
| 1578 | SH_FREE(base);
|
---|
[34] | 1579 | SH_FREE(ret);
|
---|
[1] | 1580 | SL_RETURN(NULL, _("get_client_data_file"));
|
---|
| 1581 |
|
---|
| 1582 | lab1_end:
|
---|
| 1583 | if (buf.st_size > 0x7fffffff)
|
---|
| 1584 | {
|
---|
| 1585 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
| 1586 | _("File too large"), _("get_client_data_file"));
|
---|
| 1587 | SH_FREE(base);
|
---|
| 1588 | SL_RETURN(NULL, _("get_client_data_file"));
|
---|
| 1589 | }
|
---|
| 1590 | *length = (unsigned long) buf.st_size;
|
---|
| 1591 | SH_FREE(base);
|
---|
| 1592 | SL_RETURN(ret, _("get_client_data_file"));
|
---|
| 1593 |
|
---|
| 1594 | }
|
---|
| 1595 | #endif
|
---|
| 1596 |
|
---|
[27] | 1597 | #if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER) || defined(SH_STEALTH) || defined(WITH_GPG) || defined(WITH_PGP)
|
---|
[1] | 1598 |
|
---|
| 1599 | /* --------- secure temporary file ------------ */
|
---|
| 1600 |
|
---|
| 1601 | SL_TICKET open_tmp ()
|
---|
| 1602 | {
|
---|
| 1603 | SL_TICKET fd;
|
---|
| 1604 | UINT32 ticks;
|
---|
| 1605 | char * file;
|
---|
| 1606 | struct stat buf;
|
---|
| 1607 | int error;
|
---|
| 1608 | int status = BAD;
|
---|
| 1609 | char * my_tmp_dir;
|
---|
[133] | 1610 | char hashbuf[KEYBUF_SIZE];
|
---|
[1] | 1611 |
|
---|
| 1612 | SL_ENTER(_("open_tmp"));
|
---|
| 1613 |
|
---|
| 1614 | #if defined(SH_TMPDIR)
|
---|
[34] | 1615 | my_tmp_dir = sh_util_strdup(SH_TMPDIR);
|
---|
[1] | 1616 | #else
|
---|
| 1617 | #if defined(SH_WITH_SERVER)
|
---|
[34] | 1618 | my_tmp_dir = sh_util_strdup(DEFAULT_LOGDIR);
|
---|
[1] | 1619 | #else
|
---|
[34] | 1620 | my_tmp_dir = sh_util_strdup(sh.effective.home);
|
---|
[1] | 1621 | #endif
|
---|
| 1622 | #endif
|
---|
| 1623 |
|
---|
| 1624 | if (0 != tf_trust_check (my_tmp_dir, SL_YESPRIV))
|
---|
| 1625 | {
|
---|
| 1626 | dlog(1, FIL__, __LINE__,
|
---|
| 1627 | _("The directory for temporary files: %s is untrusted, i.e. an\nuntrusted user owns or can write to some directory in the path.\n"),
|
---|
| 1628 | my_tmp_dir);
|
---|
| 1629 | sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_TRUST,
|
---|
| 1630 | (long) sh.effective.uid,
|
---|
| 1631 | my_tmp_dir);
|
---|
| 1632 | SH_FREE(my_tmp_dir);
|
---|
| 1633 | aud_exit (FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 1634 | }
|
---|
| 1635 |
|
---|
| 1636 | do {
|
---|
| 1637 |
|
---|
| 1638 | /* create random filename in effective users home directory
|
---|
| 1639 | */
|
---|
[137] | 1640 | ticks = taus_get ();
|
---|
[1] | 1641 | if (my_tmp_dir[0] == '/' && my_tmp_dir[1] == '\0')
|
---|
| 1642 | file = sh_util_strconcat (my_tmp_dir,
|
---|
[133] | 1643 | sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4,
|
---|
| 1644 | hashbuf, sizeof(hashbuf)),
|
---|
[1] | 1645 | NULL);
|
---|
| 1646 | else
|
---|
| 1647 | file = sh_util_strconcat (my_tmp_dir,
|
---|
| 1648 | "/",
|
---|
[133] | 1649 | sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4,
|
---|
| 1650 | hashbuf, sizeof(hashbuf)),
|
---|
[1] | 1651 | NULL);
|
---|
| 1652 |
|
---|
| 1653 | /* check whether it already exists (paranoia)
|
---|
| 1654 | */
|
---|
| 1655 | errno = 0;
|
---|
| 1656 | status = retry_lstat(FIL__, __LINE__, file, &buf);
|
---|
| 1657 | error = errno;
|
---|
| 1658 |
|
---|
| 1659 | if ( (status < 0) && (error == ENOENT) ) /* file does not exist */
|
---|
| 1660 | status = GOOD;
|
---|
| 1661 | else if (status < 0) /* unexpected error condition */
|
---|
| 1662 | {
|
---|
| 1663 | SH_FREE (file);
|
---|
| 1664 | SH_FREE(my_tmp_dir);
|
---|
| 1665 | TPT(( 0, FIL__, __LINE__, _("msg=<Unexpected error %d>\n"), error));
|
---|
| 1666 | SL_RETURN((-1), _("open_tmp"));
|
---|
| 1667 | }
|
---|
| 1668 | else /* file exists */
|
---|
| 1669 | {
|
---|
| 1670 | status = BAD;
|
---|
| 1671 | TPT(( 0, FIL__, __LINE__, _("msg=<Temporary file exists already>\n")));
|
---|
| 1672 | }
|
---|
| 1673 |
|
---|
| 1674 | if (status == GOOD)
|
---|
| 1675 | {
|
---|
| 1676 | if (0 == tf_trust_check (file, SL_YESPRIV))
|
---|
| 1677 | status = GOOD;
|
---|
| 1678 | else
|
---|
| 1679 | {
|
---|
| 1680 | status = BAD;
|
---|
| 1681 | TPT(( 0, FIL__, __LINE__, _("msg=<Temporary file untrusted>\n")));
|
---|
| 1682 | }
|
---|
| 1683 | }
|
---|
| 1684 |
|
---|
| 1685 | if (status == BAD)
|
---|
| 1686 | SH_FREE (file);
|
---|
| 1687 |
|
---|
| 1688 | } while (status == BAD);
|
---|
| 1689 |
|
---|
| 1690 | fd = sl_open_safe_rdwr (file, SL_YESPRIV);
|
---|
| 1691 | if (SL_ISERROR(fd))
|
---|
| 1692 | {
|
---|
| 1693 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, fd, MSG_E_SUBGEN,
|
---|
| 1694 | _("Error opening temporary file"), _("open_tmp"));
|
---|
| 1695 | TPT(( 0, FIL__, __LINE__, _("msg=<Error %d temporary file %s>\n"),
|
---|
| 1696 | fd, file));
|
---|
| 1697 | }
|
---|
| 1698 |
|
---|
| 1699 |
|
---|
| 1700 | SH_FREE (file);
|
---|
| 1701 | SH_FREE(my_tmp_dir);
|
---|
| 1702 |
|
---|
| 1703 | if (!SL_ISERROR(fd)) {
|
---|
| 1704 | sl_unlink(fd);
|
---|
| 1705 | }
|
---|
| 1706 |
|
---|
| 1707 | if (!SL_ISERROR(fd))
|
---|
| 1708 | SL_RETURN((fd), _("open_tmp"));
|
---|
| 1709 | else
|
---|
| 1710 | SL_RETURN((-1), _("open_tmp"));
|
---|
| 1711 | }
|
---|
| 1712 |
|
---|
| 1713 |
|
---|
| 1714 | int close_tmp (SL_TICKET fd)
|
---|
| 1715 | {
|
---|
| 1716 | SL_ENTER(_("close_tmp"));
|
---|
| 1717 |
|
---|
| 1718 | if (SL_ISERROR(sl_close (fd)))
|
---|
| 1719 | SL_RETURN((-1), _("close_tmp"));
|
---|
| 1720 | SL_RETURN((0), _("close_tmp"));
|
---|
| 1721 | }
|
---|
| 1722 |
|
---|
| 1723 | int rewind_tmp (SL_TICKET fd)
|
---|
| 1724 | {
|
---|
| 1725 | SL_ENTER(_("rewind_tmp"));
|
---|
| 1726 |
|
---|
| 1727 | if (SL_ISERROR(sl_rewind (fd)))
|
---|
| 1728 | SL_RETURN((-1), _("rewind_tmp"));
|
---|
| 1729 | SL_RETURN((0), _("rewind_tmp"));
|
---|
| 1730 | }
|
---|
| 1731 | #endif
|
---|