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 |
|
---|
80 | #define SH_REAL_SET
|
---|
81 |
|
---|
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"
|
---|
90 | #include "sh_pthread.h"
|
---|
91 | #include "sh_ipvx.h"
|
---|
92 |
|
---|
93 | #undef FIL__
|
---|
94 | #define FIL__ _("sh_tools.c")
|
---|
95 |
|
---|
96 | static int tools_debug = 0;
|
---|
97 |
|
---|
98 | #ifdef SH_ENCRYPT
|
---|
99 | #include "rijndael-api-fst.h"
|
---|
100 | char * errorExplain (int err_num, char * buffer, size_t len)
|
---|
101 | {
|
---|
102 | char * p;
|
---|
103 |
|
---|
104 | if (err_num == BAD_KEY_DIR)
|
---|
105 | p = (_("Key direction is invalid"));
|
---|
106 | else if (err_num == BAD_KEY_MAT)
|
---|
107 | p = (_("Key material not of correct length"));
|
---|
108 | else if (err_num == BAD_KEY_INSTANCE)
|
---|
109 | p = (_("Key passed is not valid"));
|
---|
110 | else if (err_num == BAD_CIPHER_MODE)
|
---|
111 | p = (_("Params struct passed to cipherInit invalid"));
|
---|
112 | else if (err_num == BAD_CIPHER_STATE)
|
---|
113 | p = (_("Cipher in wrong state"));
|
---|
114 | else if (err_num == BAD_BLOCK_LENGTH)
|
---|
115 | p = (_("Bad block length"));
|
---|
116 | else if (err_num == BAD_CIPHER_INSTANCE)
|
---|
117 | p = (_("Bad cipher instance"));
|
---|
118 | else if (err_num == BAD_DATA)
|
---|
119 | p = (_("Data contents are invalid"));
|
---|
120 | else
|
---|
121 | p = (_("Unknown error"));
|
---|
122 | sl_strlcpy (buffer, p, len);
|
---|
123 | return buffer;
|
---|
124 | }
|
---|
125 |
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | /* --- check for an interface ---
|
---|
129 | */
|
---|
130 | int sh_tools_iface_is_present(char *str)
|
---|
131 | {
|
---|
132 | #if defined(USE_IPVX)
|
---|
133 | struct addrinfo *ai;
|
---|
134 | struct addrinfo hints;
|
---|
135 | int res;
|
---|
136 |
|
---|
137 | memset (&hints, '\0', sizeof (hints));
|
---|
138 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
|
---|
139 | hints.ai_socktype = SOCK_STREAM;
|
---|
140 | res = getaddrinfo (str, _("2543"), &hints, &ai);
|
---|
141 |
|
---|
142 | if (res == 0)
|
---|
143 | {
|
---|
144 | struct addrinfo *p = ai;
|
---|
145 | while (p != NULL)
|
---|
146 | {
|
---|
147 | int fd = socket (p->ai_family, p->ai_socktype,
|
---|
148 | p->ai_protocol);
|
---|
149 |
|
---|
150 | if (fd < 0)
|
---|
151 | {
|
---|
152 | freeaddrinfo (ai);
|
---|
153 | return 0;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (bind (fd, p->ai_addr, p->ai_addrlen) != 0)
|
---|
157 | {
|
---|
158 | /* bind() fails for access reasons, iface exists
|
---|
159 | */
|
---|
160 | if (errno == EACCES || errno == EADDRINUSE)
|
---|
161 | {
|
---|
162 | sl_close_fd (FIL__, __LINE__, fd);
|
---|
163 | freeaddrinfo (ai);
|
---|
164 | return 1;
|
---|
165 | }
|
---|
166 |
|
---|
167 | sl_close_fd (FIL__, __LINE__, fd);
|
---|
168 | freeaddrinfo (ai);
|
---|
169 | return 0;
|
---|
170 | }
|
---|
171 |
|
---|
172 | sl_close_fd (FIL__, __LINE__, fd);
|
---|
173 | freeaddrinfo (ai);
|
---|
174 | return 1;
|
---|
175 | /* p = p->ai_next; */
|
---|
176 | }
|
---|
177 | }
|
---|
178 | #else
|
---|
179 | struct sockaddr_in sin;
|
---|
180 | int sd;
|
---|
181 |
|
---|
182 | memset(&sin, '\0', sizeof(sin));
|
---|
183 | sin.sin_family = AF_INET;
|
---|
184 | if (inet_aton(str, &(sin.sin_addr)))
|
---|
185 | {
|
---|
186 | sin.sin_port = htons(2543);
|
---|
187 |
|
---|
188 | if (-1 == (sd = socket(AF_INET, SOCK_STREAM, 0)))
|
---|
189 | {
|
---|
190 | return 0;
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (-1 == bind(sd, (struct sockaddr *)&sin, sizeof(sin)))
|
---|
194 | {
|
---|
195 | int retval = 0;
|
---|
196 |
|
---|
197 | /* bind() fails for access reasons, iface exists
|
---|
198 | */
|
---|
199 | if (errno == EACCES || errno == EADDRINUSE)
|
---|
200 | retval = 1;
|
---|
201 | sl_close_fd (FIL__, __LINE__, sd);
|
---|
202 | return retval;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* bind() succeeds, iface exists
|
---|
206 | */
|
---|
207 | sl_close_fd(FIL__, __LINE__, sd);
|
---|
208 | return 1;
|
---|
209 | }
|
---|
210 | #endif
|
---|
211 | return 0;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* --- recode all \blah escapes to qp (quoted printable) '=XX' format, and
|
---|
215 | * also code all remaining unprintable chars ---
|
---|
216 | */
|
---|
217 | #define SH_PUT_4(p, a, b, c) (p)[0] = (a); (p)[1] = (b); (p)[2] = (c);
|
---|
218 |
|
---|
219 | char * sh_tools_safe_name (const char * instr, int flag)
|
---|
220 | {
|
---|
221 | unsigned char c, d;
|
---|
222 | const char * p;
|
---|
223 | char tmp[4];
|
---|
224 | char * outstr;
|
---|
225 | size_t len = 1;
|
---|
226 | int i = 0;
|
---|
227 | unsigned char val_octal = '\0';
|
---|
228 | static char ctable[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
---|
229 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
---|
230 |
|
---|
231 | SL_ENTER(_("sh_tools_safe_name"));
|
---|
232 |
|
---|
233 | if (instr)
|
---|
234 | {
|
---|
235 | len = strlen(instr);
|
---|
236 | if (sl_ok_muls (3, len) && sl_ok_adds ((3*len), 4))
|
---|
237 | {
|
---|
238 | len = (3 * len) + 4;
|
---|
239 | p = instr;
|
---|
240 | }
|
---|
241 | else
|
---|
242 | {
|
---|
243 | len = 1;
|
---|
244 | p = NULL;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | else
|
---|
248 | {
|
---|
249 | p = NULL;
|
---|
250 | }
|
---|
251 |
|
---|
252 | outstr = SH_ALLOC(len);
|
---|
253 |
|
---|
254 | outstr[0] = '\0';
|
---|
255 | tmp[3] = '\0';
|
---|
256 |
|
---|
257 | #if !defined(SH_USE_XML)
|
---|
258 | (void) flag; /* fix compiler warning */
|
---|
259 | #endif
|
---|
260 |
|
---|
261 | if (!p)
|
---|
262 | goto end;
|
---|
263 |
|
---|
264 | while (*p)
|
---|
265 | {
|
---|
266 | c = *p;
|
---|
267 |
|
---|
268 | if (*p == '\n')
|
---|
269 | {
|
---|
270 | outstr[i] = ' '; ++i; ++p;
|
---|
271 | continue;
|
---|
272 | }
|
---|
273 |
|
---|
274 | #ifdef SH_USE_XML
|
---|
275 | if (flag == 1)
|
---|
276 | {
|
---|
277 | if ((*p) == '"')
|
---|
278 | {
|
---|
279 | SH_PUT_4(&outstr[i], '=', '2', '2');
|
---|
280 | i+=3; ++p;
|
---|
281 | continue;
|
---|
282 | }
|
---|
283 | else if ((*p) == '&')
|
---|
284 | {
|
---|
285 | SH_PUT_4(&outstr[i], '=', '2', '6');
|
---|
286 | i+=3; ++p;
|
---|
287 | continue;
|
---|
288 | }
|
---|
289 | else if ((*p) == '<')
|
---|
290 | { /* left angle */
|
---|
291 | SH_PUT_4(&outstr[i], '=', '3', 'c');
|
---|
292 | i+=3; ++p;
|
---|
293 | continue;
|
---|
294 | }
|
---|
295 | else if ((*p) == '>')
|
---|
296 | { /* right angle */
|
---|
297 | SH_PUT_4(&outstr[i], '=', '3', 'e');
|
---|
298 | i+=3; ++p;
|
---|
299 | continue;
|
---|
300 | }
|
---|
301 | }
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | if ( (*p) != '\\' && (*p) != '&' && (*p) != '=' && (*p) != '\'')
|
---|
305 | {
|
---|
306 | outstr[i] = *p; ++i;
|
---|
307 | ++p;
|
---|
308 |
|
---|
309 | if (c < 32 || c > 126)
|
---|
310 | {
|
---|
311 | --i;
|
---|
312 | d = c % 16; c = c / 16;
|
---|
313 | outstr[i] = '='; ++i;
|
---|
314 | outstr[i] = ctable[c]; ++i;
|
---|
315 | outstr[i] = ctable[d]; ++i;
|
---|
316 | }
|
---|
317 |
|
---|
318 | continue;
|
---|
319 | }
|
---|
320 | else if ((*p) == '\'')
|
---|
321 | {
|
---|
322 | SH_PUT_4(&outstr[i], '=', '2', '7');
|
---|
323 | i+=3; ++p;
|
---|
324 | }
|
---|
325 | else if (*p == '=')
|
---|
326 | {
|
---|
327 | if (p[1] != '"' && p[1] != '<')
|
---|
328 | {
|
---|
329 | SH_PUT_4(&outstr[i], '=', '3', 'd');
|
---|
330 | i+=3; ++p;
|
---|
331 | }
|
---|
332 | else
|
---|
333 | { outstr[i] = *p; ++i; ++p; }
|
---|
334 | }
|
---|
335 | else if (*p == '\\')
|
---|
336 | {
|
---|
337 | ++p;
|
---|
338 | if (!p)
|
---|
339 | break;
|
---|
340 | if (!(*p))
|
---|
341 | break;
|
---|
342 |
|
---|
343 |
|
---|
344 |
|
---|
345 | switch (*p) {
|
---|
346 | case '\\':
|
---|
347 | SH_PUT_4(&outstr[i], '=', '5', 'c');
|
---|
348 | i+=3; ++p;
|
---|
349 | break;
|
---|
350 | case 'n':
|
---|
351 | SH_PUT_4(&outstr[i], '=', '0', 'a');
|
---|
352 | i+=3; ++p;
|
---|
353 | break;
|
---|
354 | case 'b':
|
---|
355 | SH_PUT_4(&outstr[i], '=', '0', '8');
|
---|
356 | i+=3; ++p;
|
---|
357 | break;
|
---|
358 | case 'r':
|
---|
359 | SH_PUT_4(&outstr[i], '=', '0', 'd');
|
---|
360 | i+=3; ++p;
|
---|
361 | break;
|
---|
362 | case 't':
|
---|
363 | SH_PUT_4(&outstr[i], '=', '0', '9');
|
---|
364 | i+=3; ++p;
|
---|
365 | break;
|
---|
366 | case 'v':
|
---|
367 | SH_PUT_4(&outstr[i], '=', '0', 'b');
|
---|
368 | i+=3; ++p;
|
---|
369 | break;
|
---|
370 | case 'f':
|
---|
371 | SH_PUT_4(&outstr[i], '=', '0', 'c');
|
---|
372 | i+=3; ++p;
|
---|
373 | break;
|
---|
374 | case '\'':
|
---|
375 | SH_PUT_4(&outstr[i], '=', '2', '7');
|
---|
376 | i+=3; ++p;
|
---|
377 | break;
|
---|
378 | case '"': /* also encode quoted '"' */
|
---|
379 | SH_PUT_4(&outstr[i], '=', '2', '2');
|
---|
380 | i+=3; ++p;
|
---|
381 | break;
|
---|
382 | case ' ':
|
---|
383 | SH_PUT_4(&outstr[i], '=', '2', '0');
|
---|
384 | i+=3; ++p;
|
---|
385 | break;
|
---|
386 | default:
|
---|
387 | if (strlen(p) < 3) /* certainly not an octal number, skip */
|
---|
388 | {
|
---|
389 | p += strlen(p);
|
---|
390 | }
|
---|
391 | else
|
---|
392 | {
|
---|
393 | tmp[0] = p[0]; tmp[1] = p[1]; tmp[2] = p[2];
|
---|
394 | val_octal = (unsigned char) strtoul(tmp, (char **)NULL, 8);
|
---|
395 | if (val_octal != '\0') {
|
---|
396 | c = val_octal;
|
---|
397 | d = c % 16; c = c / 16;
|
---|
398 | outstr[i] = '='; ++i;
|
---|
399 | outstr[i] = ctable[c]; ++i;
|
---|
400 | outstr[i] = ctable[d]; ++i;
|
---|
401 | }
|
---|
402 | p += 3;
|
---|
403 | }
|
---|
404 | }
|
---|
405 | }
|
---|
406 | else if (*p == '&')
|
---|
407 | {
|
---|
408 | ++p;
|
---|
409 | if (!p || !(*p))
|
---|
410 | {
|
---|
411 | outstr[i] = '&'; ++i;
|
---|
412 | break;
|
---|
413 | }
|
---|
414 |
|
---|
415 | if (p[0] == 'a' && p[1] == 'm' && p[2] == 'p' && p[3] == ';')
|
---|
416 | {
|
---|
417 | SH_PUT_4(&outstr[i], '=', '2', '6');
|
---|
418 | i+=3; p += 4;
|
---|
419 | }
|
---|
420 | else if (p[0] == 'q' && p[1] == 'u' && p[2] == 'o' && p[3] == 't' &&
|
---|
421 | p[4] == ';')
|
---|
422 | {
|
---|
423 | SH_PUT_4(&outstr[i], '=', '2', '2');
|
---|
424 | i+=3; p += 5;
|
---|
425 | }
|
---|
426 | else if (p[0] == 'l' && p[1] == 't' && p[2] == ';')
|
---|
427 | {
|
---|
428 | SH_PUT_4(&outstr[i], '=', '3', 'c');
|
---|
429 | i+=3; p += 3;
|
---|
430 | }
|
---|
431 | else if (p[0] == 'g' && p[1] == 't' && p[2] == ';')
|
---|
432 | {
|
---|
433 | SH_PUT_4(&outstr[i], '=', '3', 'e');
|
---|
434 | i+=3; p += 3;
|
---|
435 | }
|
---|
436 | else /* conserve the '&' */
|
---|
437 | {
|
---|
438 | outstr[i] = '&'; ++i;
|
---|
439 | }
|
---|
440 | }
|
---|
441 | else
|
---|
442 | {
|
---|
443 | outstr[i] = *p; ++i;
|
---|
444 | ++p;
|
---|
445 | }
|
---|
446 | } /* while (p && *p) */
|
---|
447 |
|
---|
448 | end:
|
---|
449 |
|
---|
450 | outstr[i] = '\0';
|
---|
451 | SL_RETURN( outstr, _("sh_tools_safe_name"));
|
---|
452 | }
|
---|
453 |
|
---|
454 |
|
---|
455 | /* extern int h_errno; */
|
---|
456 |
|
---|
457 | char * sh_tools_errmessage (int tellme, char * errbuf, size_t len)
|
---|
458 | {
|
---|
459 | char * p = NULL;
|
---|
460 | #ifdef HOST_NOT_FOUND
|
---|
461 | if (tellme == HOST_NOT_FOUND)
|
---|
462 | p = _("The specified host is unknown: ");
|
---|
463 | #endif
|
---|
464 | #ifdef NO_ADDRESS
|
---|
465 | if (tellme == NO_ADDRESS)
|
---|
466 | p = _("The requested name is valid but does not have an IP address: ");
|
---|
467 | #endif
|
---|
468 | #ifdef NO_RECOVERY
|
---|
469 | if (tellme == NO_RECOVERY)
|
---|
470 | p = _("A non-recoverable name server error occurred: ");
|
---|
471 | #endif
|
---|
472 | #ifdef TRY_AGAIN
|
---|
473 | if (tellme == TRY_AGAIN)
|
---|
474 | p = _("A temporary error occurred on an authoritative name server. The specified host is unknown: ");
|
---|
475 | #endif
|
---|
476 | if (!p) p = _("Unknown error");
|
---|
477 | sl_strlcpy(errbuf, p, len);
|
---|
478 | return errbuf;
|
---|
479 | }
|
---|
480 |
|
---|
481 | #if defined (SH_WITH_SERVER)
|
---|
482 |
|
---|
483 | int get_open_max ()
|
---|
484 | {
|
---|
485 | int value;
|
---|
486 |
|
---|
487 | #ifdef _SC_OPEN_MAX
|
---|
488 | value = sysconf (_SC_OPEN_MAX);
|
---|
489 | #else
|
---|
490 | #ifdef OPEN_MAX
|
---|
491 | value = OPEN_MAX;
|
---|
492 | #else
|
---|
493 | value = _POSIX_OPEN_MAX;
|
---|
494 | #endif
|
---|
495 | #endif
|
---|
496 |
|
---|
497 | if (value < 0)
|
---|
498 | value = 8; /* POSIX lower limit */
|
---|
499 |
|
---|
500 | if (value > 4096)
|
---|
501 | value = 4096;
|
---|
502 |
|
---|
503 | return value;
|
---|
504 | }
|
---|
505 |
|
---|
506 | #endif
|
---|
507 |
|
---|
508 | typedef struct _sin_cache {
|
---|
509 | char * address;
|
---|
510 | struct sh_sockaddr saddr;
|
---|
511 | struct _sin_cache * next;
|
---|
512 | } sin_cache;
|
---|
513 |
|
---|
514 | static sin_cache * conn_cache = NULL;
|
---|
515 | static int cached_addr = 0;
|
---|
516 |
|
---|
517 | void delete_cache()
|
---|
518 | {
|
---|
519 | sin_cache * check_cache = conn_cache;
|
---|
520 | sin_cache * old_entry;
|
---|
521 |
|
---|
522 | SL_ENTER(_("delete_cache"));
|
---|
523 |
|
---|
524 | while (check_cache != NULL)
|
---|
525 | {
|
---|
526 | old_entry = check_cache;
|
---|
527 | check_cache = check_cache->next;
|
---|
528 | SH_FREE(old_entry->address);
|
---|
529 | SH_FREE(old_entry);
|
---|
530 | }
|
---|
531 |
|
---|
532 | cached_addr = 0;
|
---|
533 |
|
---|
534 | conn_cache = NULL;
|
---|
535 | SL_RET0(_("delete_cache"));
|
---|
536 | }
|
---|
537 |
|
---|
538 | int DoReverseLookup = S_TRUE;
|
---|
539 |
|
---|
540 | int set_reverse_lookup (const char * c)
|
---|
541 | {
|
---|
542 | return sh_util_flagval(c, &DoReverseLookup);
|
---|
543 | }
|
---|
544 |
|
---|
545 | #if !defined(USE_IPVX)
|
---|
546 | int connect_port (char * address, int port,
|
---|
547 | char * ecall, int * errnum, char * errmsg, int errsiz)
|
---|
548 | {
|
---|
549 | struct in_addr haddr; /* host address from numeric */
|
---|
550 | /* host details returned by the DNS */
|
---|
551 | struct hostent *host_entry = NULL;
|
---|
552 | struct sockaddr_in sinr; /* socket to the remote host */
|
---|
553 |
|
---|
554 | char * host_name;
|
---|
555 |
|
---|
556 | volatile int fd = (-1);
|
---|
557 | int status;
|
---|
558 | volatile int fail = 0;
|
---|
559 | int cached = 0;
|
---|
560 |
|
---|
561 | int retval;
|
---|
562 | char errbuf[SH_ERRBUF_SIZE];
|
---|
563 |
|
---|
564 | sin_cache * check_cache = conn_cache;
|
---|
565 |
|
---|
566 | SL_ENTER(_("connect_port"));
|
---|
567 |
|
---|
568 | if (tools_debug)
|
---|
569 | fprintf(stderr, _("-00- <%s> <%d> no IPv6 support\n"), address, port);
|
---|
570 |
|
---|
571 | if (errsiz > 0) errmsg[0] = '\0';
|
---|
572 |
|
---|
573 | /* paranoia -- should not happen
|
---|
574 | */
|
---|
575 | if (cached_addr > 128)
|
---|
576 | delete_cache();
|
---|
577 |
|
---|
578 | if (check_cache != NULL)
|
---|
579 | {
|
---|
580 | while (check_cache && check_cache->address)
|
---|
581 | {
|
---|
582 | if (tools_debug)
|
---|
583 | fprintf(stderr, _("-01- <%s> <%s>\n"),
|
---|
584 | address, check_cache->address);
|
---|
585 |
|
---|
586 | if ( 0 == sl_strncmp(check_cache->address,
|
---|
587 | address, sl_strlen(address)) )
|
---|
588 | {
|
---|
589 | memcpy (&sinr, &((check_cache->saddr).sin), sizeof(struct sockaddr_in));
|
---|
590 | sinr.sin_family = AF_INET;
|
---|
591 | sinr.sin_port = htons (port);
|
---|
592 | cached = 1;
|
---|
593 | break;
|
---|
594 | }
|
---|
595 | if (tools_debug)
|
---|
596 | {
|
---|
597 | char eaddr[SH_IP_BUF];
|
---|
598 | sl_strlcpy(eaddr,
|
---|
599 | inet_ntoa(*(struct in_addr *) &(sinr.sin_addr)),
|
---|
600 | sizeof(eaddr));
|
---|
601 | fprintf(stderr, _("-02- <AF_INET> <%s> <%d> <%d>\n"),
|
---|
602 | eaddr,
|
---|
603 | port, cached);
|
---|
604 | }
|
---|
605 | if (check_cache->next)
|
---|
606 | check_cache = check_cache->next;
|
---|
607 | else
|
---|
608 | check_cache = NULL;
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | /* only use gethostbyname() if neccessary
|
---|
613 | */
|
---|
614 | if (cached == 0)
|
---|
615 | {
|
---|
616 | if (tools_debug)
|
---|
617 | fputs(_("-03- not cached\n"), stderr);
|
---|
618 | #ifdef HAVE_INET_ATON
|
---|
619 | if (0 == inet_aton(address, &haddr))
|
---|
620 | #else
|
---|
621 | if ((unsigned long)-1 == (haddr.s_addr = inet_addr(address)))
|
---|
622 | #endif
|
---|
623 | {
|
---|
624 | SH_MUTEX_LOCK(mutex_resolv);
|
---|
625 |
|
---|
626 | host_name = NULL;
|
---|
627 |
|
---|
628 | host_entry = sh_gethostbyname(address);
|
---|
629 |
|
---|
630 | if (host_entry == NULL || host_entry->h_addr == NULL)
|
---|
631 | {
|
---|
632 | sl_strlcpy(ecall, _("gethostbyname"), SH_MINIBUF);
|
---|
633 | #ifndef NO_H_ERRNO
|
---|
634 | *errnum = h_errno;
|
---|
635 | #else
|
---|
636 | *errnum = 666;
|
---|
637 | #endif
|
---|
638 | (void) sh_tools_errmessage (*errnum, errmsg, errsiz);
|
---|
639 | sl_strlcat(errmsg, address, errsiz);
|
---|
640 | fail = (-1);
|
---|
641 | }
|
---|
642 | else
|
---|
643 | {
|
---|
644 | sinr.sin_family = AF_INET;
|
---|
645 | sinr.sin_port = htons (port);
|
---|
646 | sinr.sin_addr = *(struct in_addr *) host_entry->h_addr;
|
---|
647 |
|
---|
648 | if (tools_debug)
|
---|
649 | fprintf(stderr,
|
---|
650 | _("-04- <%s> <%s> hostent->h_name %s <%s> hostent->h_addr\n"),
|
---|
651 | address,
|
---|
652 | (host_entry->h_name == NULL) ? _("NULL") : host_entry->h_name,
|
---|
653 | (host_entry->h_addrtype == AF_INET) ? _("AF_INET") : _("AF_INET6"),
|
---|
654 | inet_ntoa(*(struct in_addr *) &(sinr.sin_addr)));
|
---|
655 |
|
---|
656 | /* reverse DNS lookup
|
---|
657 | */
|
---|
658 | if (DoReverseLookup == S_TRUE)
|
---|
659 | {
|
---|
660 | if (host_entry->h_name == NULL)
|
---|
661 | {
|
---|
662 | host_name = SH_ALLOC(1);
|
---|
663 | host_name[0] = '\0';
|
---|
664 | }
|
---|
665 | else
|
---|
666 | {
|
---|
667 | host_name = sh_util_strdup(host_entry->h_name);
|
---|
668 | }
|
---|
669 |
|
---|
670 | host_entry = sh_gethostbyaddr ((char *) &sinr.sin_addr,
|
---|
671 | sizeof(struct in_addr),
|
---|
672 | AF_INET);
|
---|
673 | if (host_entry == NULL || host_entry->h_name == NULL)
|
---|
674 | {
|
---|
675 | sl_strlcpy(ecall, _("gethostbyaddr"), SH_MINIBUF);
|
---|
676 | #ifndef NO_H_ERRNO
|
---|
677 | *errnum = h_errno;
|
---|
678 | #else
|
---|
679 | *errnum = 666;
|
---|
680 | #endif
|
---|
681 | (void) sh_tools_errmessage (*errnum, errmsg, errsiz);
|
---|
682 | sl_strlcat(errmsg,
|
---|
683 | inet_ntoa (*(struct in_addr *) &(sinr.sin_addr)),
|
---|
684 | errsiz);
|
---|
685 | fail = (-1);
|
---|
686 | }
|
---|
687 | else
|
---|
688 | {
|
---|
689 | *errnum = 0;
|
---|
690 | if (sl_strlen(host_entry->h_name) == 0 ||
|
---|
691 | (*errnum = sl_strcasecmp(host_name,host_entry->h_name)) != 0)
|
---|
692 | {
|
---|
693 | if (*errnum)
|
---|
694 | sl_strlcpy(ecall, _("strcmp"), SH_MINIBUF);
|
---|
695 | else
|
---|
696 | sl_strlcpy(ecall, _("strlen"), SH_MINIBUF);
|
---|
697 | sl_strlcpy(errmsg, _("Reverse lookup failed: "),
|
---|
698 | errsiz);
|
---|
699 | sl_strlcat(errmsg, address, errsiz);
|
---|
700 | sl_strlcat(errmsg, _(" vs "), errsiz);
|
---|
701 | sl_strlcat(errmsg,
|
---|
702 | inet_ntoa (*(struct in_addr *) &(sinr.sin_addr)),
|
---|
703 | errsiz);
|
---|
704 | fail = -1;
|
---|
705 | }
|
---|
706 | }
|
---|
707 | }
|
---|
708 | }
|
---|
709 | SH_MUTEX_UNLOCK(mutex_resolv);
|
---|
710 | if (host_name) SH_FREE(host_name);
|
---|
711 | }
|
---|
712 |
|
---|
713 | else /* address was numeric */
|
---|
714 | {
|
---|
715 | sinr.sin_family = AF_INET;
|
---|
716 | sinr.sin_port = htons (port);
|
---|
717 | sinr.sin_addr = haddr;
|
---|
718 |
|
---|
719 | if (tools_debug)
|
---|
720 | fprintf(stderr,
|
---|
721 | _("-04- <%s> is_numeric AF_INET <%s> \n"),
|
---|
722 | address,
|
---|
723 | inet_ntoa(*(struct in_addr *) &(sinr.sin_addr)));
|
---|
724 | }
|
---|
725 |
|
---|
726 |
|
---|
727 | if (fail != -1)
|
---|
728 | {
|
---|
729 | /* put it into the cache
|
---|
730 | */
|
---|
731 | check_cache = SH_ALLOC(sizeof(sin_cache));
|
---|
732 | check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
|
---|
733 | sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
|
---|
734 |
|
---|
735 | sh_ipvx_save(&(check_cache->saddr), AF_INET, (struct sockaddr *) &sinr);
|
---|
736 |
|
---|
737 | ++cached_addr;
|
---|
738 |
|
---|
739 | if (conn_cache)
|
---|
740 | {
|
---|
741 | if (conn_cache->next)
|
---|
742 | check_cache->next = conn_cache->next;
|
---|
743 | else
|
---|
744 | check_cache->next = NULL;
|
---|
745 | conn_cache->next = check_cache;
|
---|
746 | }
|
---|
747 | else
|
---|
748 | {
|
---|
749 | check_cache->next = NULL;
|
---|
750 | conn_cache = check_cache;
|
---|
751 | }
|
---|
752 | }
|
---|
753 | }
|
---|
754 |
|
---|
755 |
|
---|
756 | if (fail != (-1))
|
---|
757 | {
|
---|
758 | fd = socket(AF_INET, SOCK_STREAM, 0);
|
---|
759 | if (fd < 0) {
|
---|
760 | fail = (-1);
|
---|
761 | status = errno;
|
---|
762 | sl_strlcpy(ecall, _("socket"), SH_MINIBUF);
|
---|
763 | *errnum = status;
|
---|
764 | sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
|
---|
765 | sl_strlcat(errmsg, _(", address "), errsiz);
|
---|
766 | sl_strlcat(errmsg, address, errsiz);
|
---|
767 | }
|
---|
768 | }
|
---|
769 |
|
---|
770 | if (fail != (-1)) {
|
---|
771 |
|
---|
772 | if ( retry_connect(FIL__, __LINE__, fd,
|
---|
773 | (struct sockaddr *) &sinr, sizeof(sinr)) < 0)
|
---|
774 | {
|
---|
775 | status = errno;
|
---|
776 | sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
|
---|
777 | *errnum = status;
|
---|
778 | sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
|
---|
779 | sl_strlcat(errmsg,
|
---|
780 | (sinr.sin_family == AF_INET) ? _(", AF_INET ") : _(", AF_INET6 "),
|
---|
781 | errsiz);
|
---|
782 | sl_strlcat(errmsg, _(", address "), errsiz);
|
---|
783 | sl_strlcat(errmsg, address, errsiz);
|
---|
784 | sl_close_fd(FIL__, __LINE__, fd);
|
---|
785 | fail = (-1);
|
---|
786 | }
|
---|
787 | }
|
---|
788 |
|
---|
789 | retval = (fail < 0) ? (-1) : fd;
|
---|
790 | SL_RETURN(retval, _("connect_port"));
|
---|
791 | }
|
---|
792 | #else
|
---|
793 | int connect_port (char * address, int port,
|
---|
794 | char * ecall, int * errnum, char * errmsg, int errsiz)
|
---|
795 | {
|
---|
796 | struct sockaddr_in *sin;
|
---|
797 | struct sockaddr_in6 *sin6;
|
---|
798 | struct sh_sockaddr ss;
|
---|
799 | sin_cache * check_cache = conn_cache;
|
---|
800 | int cached = 0;
|
---|
801 | int fail = 0;
|
---|
802 | int fd = -1;
|
---|
803 | int status = 0;
|
---|
804 |
|
---|
805 | int retval;
|
---|
806 | char errbuf[SH_ERRBUF_SIZE];
|
---|
807 |
|
---|
808 | SL_ENTER(_("connect_port"));
|
---|
809 |
|
---|
810 | /* paranoia -- should not happen
|
---|
811 | */
|
---|
812 | if (cached_addr > 128)
|
---|
813 | delete_cache();
|
---|
814 |
|
---|
815 | if (tools_debug)
|
---|
816 | fprintf(stderr, _("-00- <%s> <%d>\n"), address, port);
|
---|
817 |
|
---|
818 | if (check_cache != NULL)
|
---|
819 | {
|
---|
820 | while (check_cache && check_cache->address)
|
---|
821 | {
|
---|
822 | if (tools_debug)
|
---|
823 | fprintf(stderr, _("-01- <%s> <%s>\n"),
|
---|
824 | address, check_cache->address);
|
---|
825 |
|
---|
826 | if ( 0 == sl_strcmp(check_cache->address, address) )
|
---|
827 | {
|
---|
828 | memcpy (&ss, &(check_cache->saddr), sizeof(struct sh_sockaddr));
|
---|
829 | switch (ss.ss_family)
|
---|
830 | {
|
---|
831 | case AF_INET:
|
---|
832 | sin = &(ss.sin);
|
---|
833 | sin->sin_port = htons (port);
|
---|
834 | cached = 1;
|
---|
835 | break;
|
---|
836 | case AF_INET6:
|
---|
837 | sin6 = &(ss.sin6);
|
---|
838 | sin6->sin6_port = htons (port);
|
---|
839 | cached = 1;
|
---|
840 | break;
|
---|
841 | default:
|
---|
842 | break;
|
---|
843 | }
|
---|
844 | if (tools_debug)
|
---|
845 | {
|
---|
846 | char eaddr[SH_IP_BUF];
|
---|
847 | sh_ipvx_ntoa(eaddr, sizeof(eaddr), &ss);
|
---|
848 | fprintf(stderr, _("-02- <%s> <%s> <%d> <%d>\n"),
|
---|
849 | (ss.ss_family == AF_INET) ? _("AF_INET") : _("AF_INET6"),
|
---|
850 | eaddr,
|
---|
851 | port, cached);
|
---|
852 | }
|
---|
853 | break;
|
---|
854 | }
|
---|
855 | if (check_cache->next)
|
---|
856 | check_cache = check_cache->next;
|
---|
857 | else
|
---|
858 | check_cache = NULL;
|
---|
859 | }
|
---|
860 | }
|
---|
861 |
|
---|
862 | if (cached != 0)
|
---|
863 | {
|
---|
864 | if (tools_debug)
|
---|
865 | fputs(_("-03- cached\n"), stderr);
|
---|
866 | fd = socket(ss.ss_family, SOCK_STREAM, 0);
|
---|
867 | if (fd < 0)
|
---|
868 | {
|
---|
869 | status = errno;
|
---|
870 | fail = (-1);
|
---|
871 | sl_strlcpy(ecall, _("socket"), SH_MINIBUF);
|
---|
872 | *errnum = status;
|
---|
873 | sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
|
---|
874 | sl_strlcat(errmsg, _(", address "), errsiz);
|
---|
875 | sl_strlcat(errmsg, address, errsiz);
|
---|
876 | }
|
---|
877 |
|
---|
878 |
|
---|
879 | if (fail != (-1))
|
---|
880 | {
|
---|
881 | int addrlen = SH_SS_LEN(ss);
|
---|
882 |
|
---|
883 | if ( retry_connect(FIL__, __LINE__, fd,
|
---|
884 | sh_ipvx_sockaddr_cast(&ss), addrlen) < 0)
|
---|
885 | {
|
---|
886 | status = errno;
|
---|
887 | sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
|
---|
888 | *errnum = status;
|
---|
889 | sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
|
---|
890 | sl_strlcat(errmsg, _(", address "), errsiz);
|
---|
891 | sl_strlcat(errmsg, address, errsiz);
|
---|
892 | sl_close_fd(FIL__, __LINE__, fd);
|
---|
893 | fail = (-1);
|
---|
894 | }
|
---|
895 | }
|
---|
896 |
|
---|
897 | if (fail != 0)
|
---|
898 | {
|
---|
899 | delete_cache();
|
---|
900 | cached = 0;
|
---|
901 | }
|
---|
902 | }
|
---|
903 |
|
---|
904 | if (cached == 0)
|
---|
905 | {
|
---|
906 | int res;
|
---|
907 | char sport[32];
|
---|
908 | struct addrinfo *ai;
|
---|
909 | struct addrinfo hints;
|
---|
910 |
|
---|
911 | if (tools_debug)
|
---|
912 | fputs(_("-03- not cached\n"), stderr);
|
---|
913 |
|
---|
914 | memset (&hints, '\0', sizeof (hints));
|
---|
915 | hints.ai_flags = AI_ADDRCONFIG;
|
---|
916 | #if defined(AI_CANONNAME)
|
---|
917 | hints.ai_flags |= AI_CANONNAME;
|
---|
918 | #endif
|
---|
919 | hints.ai_family = AF_UNSPEC;
|
---|
920 | hints.ai_socktype = SOCK_STREAM;
|
---|
921 | sl_snprintf(sport, sizeof(sport), "%d", port);
|
---|
922 |
|
---|
923 | res = getaddrinfo (address, sport, &hints, &ai);
|
---|
924 | if (res != 0)
|
---|
925 | {
|
---|
926 | fail = (-1);
|
---|
927 | status = errno;
|
---|
928 | sl_strlcpy(ecall, _("getaddrinfo"), SH_MINIBUF);
|
---|
929 | *errnum = status;
|
---|
930 | sl_strlcpy(errmsg, gai_strerror (res), errsiz);
|
---|
931 | sl_strlcat(errmsg, _(", address "), errsiz);
|
---|
932 | sl_strlcat(errmsg, address, errsiz);
|
---|
933 | }
|
---|
934 |
|
---|
935 | if (fail != (-1) && (DoReverseLookup == S_TRUE) && !sh_ipvx_is_numeric(address))
|
---|
936 | {
|
---|
937 | struct addrinfo *p = ai;
|
---|
938 | int success = 0;
|
---|
939 | char hostname[SH_BUFSIZE];
|
---|
940 | const char * canonical;
|
---|
941 |
|
---|
942 |
|
---|
943 | #if defined(AI_CANONNAME)
|
---|
944 | if (ai->ai_canonname && strlen(ai->ai_canonname) > 0)
|
---|
945 | {
|
---|
946 | canonical = ai->ai_canonname;
|
---|
947 | if (tools_debug)
|
---|
948 | fprintf(stderr, _("-04- <%s> <%s> ai->ai_canonname\n"),
|
---|
949 | address, canonical);
|
---|
950 | }
|
---|
951 | else
|
---|
952 | {
|
---|
953 | canonical = address;
|
---|
954 | if (tools_debug)
|
---|
955 | fprintf(stderr, _("-04- <%s> <%s> defined ai_canonname\n"),
|
---|
956 | address, canonical);
|
---|
957 | }
|
---|
958 | #else
|
---|
959 | canonical = address;
|
---|
960 | if (tools_debug)
|
---|
961 | fprintf(stderr, _("-04- <%s> <%s> not defined ai_canonname\n"),
|
---|
962 | address, canonical);
|
---|
963 | #endif
|
---|
964 |
|
---|
965 | while (p != NULL)
|
---|
966 | {
|
---|
967 | int e = getnameinfo (p->ai_addr, p->ai_addrlen,
|
---|
968 | hostname, sizeof(hostname),
|
---|
969 | NULL, 0, NI_NAMEREQD);
|
---|
970 |
|
---|
971 | if (e == 0)
|
---|
972 | {
|
---|
973 | if (tools_debug)
|
---|
974 | {
|
---|
975 | fprintf(stderr, _("-05- <%s> <%s> <%s>\n"),
|
---|
976 | (p->ai_family == AF_INET) ? _("AF_INET") : _("AF_INET6"),
|
---|
977 | sh_ipvx_print_sockaddr (p->ai_addr, p->ai_family),
|
---|
978 | hostname);
|
---|
979 | }
|
---|
980 |
|
---|
981 | if (sl_strcasecmp(hostname, canonical) == 0)
|
---|
982 | {
|
---|
983 | if (tools_debug)
|
---|
984 | fprintf(stderr, _("-06- <%s> <%s> match\n"),
|
---|
985 | hostname, canonical);
|
---|
986 | success = 1;
|
---|
987 | break;
|
---|
988 | }
|
---|
989 |
|
---|
990 | }
|
---|
991 |
|
---|
992 | p = p->ai_next;
|
---|
993 | }
|
---|
994 |
|
---|
995 | if (success == 0)
|
---|
996 | {
|
---|
997 | sl_strlcpy(ecall, _("strcmp"), SH_MINIBUF);
|
---|
998 | sl_strlcpy(errmsg, _("Reverse lookup failed: "),
|
---|
999 | errsiz);
|
---|
1000 | sl_strlcat(errmsg, address, errsiz);
|
---|
1001 | fail = -1;
|
---|
1002 | freeaddrinfo (ai);
|
---|
1003 | }
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | if (fail != (-1))
|
---|
1007 | {
|
---|
1008 | struct addrinfo *p = ai;
|
---|
1009 |
|
---|
1010 | while (p != NULL)
|
---|
1011 | {
|
---|
1012 | if ( (SOCK_STREAM == p->ai_socktype) &&
|
---|
1013 | ((p->ai_family == AF_INET) || (p->ai_family == AF_INET6)) )
|
---|
1014 | {
|
---|
1015 |
|
---|
1016 | fd = socket(p->ai_family, SOCK_STREAM, 0);
|
---|
1017 |
|
---|
1018 | if (fd != (-1))
|
---|
1019 | {
|
---|
1020 | if (retry_connect(FIL__, __LINE__, fd,
|
---|
1021 | p->ai_addr, p->ai_addrlen) >= 0)
|
---|
1022 | {
|
---|
1023 | /* put it into the cache
|
---|
1024 | */
|
---|
1025 | check_cache = SH_ALLOC(sizeof(sin_cache));
|
---|
1026 | check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
|
---|
1027 | sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
|
---|
1028 |
|
---|
1029 | sh_ipvx_save(&(check_cache->saddr), p->ai_family, p->ai_addr);
|
---|
1030 |
|
---|
1031 | ++cached_addr;
|
---|
1032 |
|
---|
1033 | if (conn_cache)
|
---|
1034 | {
|
---|
1035 | if (conn_cache->next)
|
---|
1036 | check_cache->next = conn_cache->next;
|
---|
1037 | else
|
---|
1038 | check_cache->next = NULL;
|
---|
1039 | conn_cache->next = check_cache;
|
---|
1040 | }
|
---|
1041 | else
|
---|
1042 | {
|
---|
1043 | check_cache->next = NULL;
|
---|
1044 | conn_cache = check_cache;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | freeaddrinfo (ai);
|
---|
1048 | goto end;
|
---|
1049 | }
|
---|
1050 | status = errno;
|
---|
1051 | sl_close_fd(FIL__, __LINE__, fd);
|
---|
1052 | }
|
---|
1053 | else
|
---|
1054 | {
|
---|
1055 | status = errno;
|
---|
1056 | }
|
---|
1057 | }
|
---|
1058 | p = p->ai_next;
|
---|
1059 | }
|
---|
1060 | fail = (-1);
|
---|
1061 | freeaddrinfo (ai);
|
---|
1062 |
|
---|
1063 | sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
|
---|
1064 | *errnum = status;
|
---|
1065 | sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
|
---|
1066 | sl_strlcat(errmsg, _(", address "), errsiz);
|
---|
1067 | sl_strlcat(errmsg, address, errsiz);
|
---|
1068 | }
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | end:
|
---|
1072 | retval = (fail < 0) ? (-1) : fd;
|
---|
1073 | SL_RETURN(retval, _("connect_port"));
|
---|
1074 |
|
---|
1075 | }
|
---|
1076 | #endif
|
---|
1077 |
|
---|
1078 | int connect_port_2 (char * address1, char * address2, int port,
|
---|
1079 | char * ecall, int * errnum, char * errmsg, int errsiz)
|
---|
1080 | {
|
---|
1081 | int retval = (-1);
|
---|
1082 |
|
---|
1083 | SL_ENTER(_("connect_port_2"));
|
---|
1084 |
|
---|
1085 | errmsg[0] = '\0';
|
---|
1086 | *errnum = 0;
|
---|
1087 |
|
---|
1088 | if (address1 != NULL && address1[0] != '\0')
|
---|
1089 | retval = connect_port (address1, port,
|
---|
1090 | ecall, errnum,
|
---|
1091 | errmsg, errsiz);
|
---|
1092 |
|
---|
1093 | if (retval < 0 && address2 != NULL && address2[0] != '\0')
|
---|
1094 | {
|
---|
1095 | /* can't use sh_error_handle here, as this would cause an infinite
|
---|
1096 | * loop if called from sh_unix_time
|
---|
1097 | */
|
---|
1098 | TPT(( 0, FIL__, __LINE__, _("msg=<Using alternative server %s.>\n"),
|
---|
1099 | address2));
|
---|
1100 | retval = connect_port (address2, port,
|
---|
1101 | ecall, errnum,
|
---|
1102 | errmsg, errsiz);
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | if ((retval < 0) &&
|
---|
1106 | (address1 == NULL || address1[0] == '\0') &&
|
---|
1107 | (address1 == NULL || address1[0] == '\0'))
|
---|
1108 | {
|
---|
1109 | sl_strlcpy(ecall, _("connect_port_2"), SH_MINIBUF);
|
---|
1110 | sl_strlcpy(errmsg, _("No server address known"), errsiz);
|
---|
1111 | }
|
---|
1112 | SL_RETURN(retval, _("connect_port_2"));
|
---|
1113 | /* return retval; */
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | #if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
1117 | static
|
---|
1118 | int sh_write_select(int type, int sockfd,
|
---|
1119 | char *buf, int nbytes,
|
---|
1120 | int * w_error, int timeout)
|
---|
1121 | {
|
---|
1122 | int countbytes, count;
|
---|
1123 | fd_set fds;
|
---|
1124 | struct timeval tv;
|
---|
1125 | int select_now;
|
---|
1126 | int num_sel;
|
---|
1127 |
|
---|
1128 | char errbuf[SH_ERRBUF_SIZE];
|
---|
1129 |
|
---|
1130 | SL_ENTER(_("sh_write_select"));
|
---|
1131 |
|
---|
1132 | FD_ZERO(&fds);
|
---|
1133 | FD_SET(sockfd, &fds);
|
---|
1134 |
|
---|
1135 | countbytes = 0;
|
---|
1136 | tv.tv_sec = 1;
|
---|
1137 | tv.tv_usec = 0;
|
---|
1138 | select_now = 0;
|
---|
1139 |
|
---|
1140 | *w_error = 0;
|
---|
1141 |
|
---|
1142 | while ( countbytes < nbytes ) {
|
---|
1143 |
|
---|
1144 | FD_ZERO(&fds);
|
---|
1145 | FD_SET(sockfd, &fds);
|
---|
1146 |
|
---|
1147 | if (type == SH_DO_WRITE)
|
---|
1148 | {
|
---|
1149 | if ( (num_sel = select (sockfd+1, NULL, &fds, NULL, &tv)) == -1)
|
---|
1150 | {
|
---|
1151 | if (sig_raised == 1)
|
---|
1152 | {
|
---|
1153 | sig_raised = 2;
|
---|
1154 | continue;
|
---|
1155 | }
|
---|
1156 | if ( errno == EINTR || errno == EINPROGRESS ) /* try again */
|
---|
1157 | continue;
|
---|
1158 | *w_error = errno;
|
---|
1159 |
|
---|
1160 | sh_error_message(*w_error, errbuf, sizeof(errbuf));
|
---|
1161 | sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
1162 | errbuf,
|
---|
1163 | _("sh_write_select (ws)") );
|
---|
1164 | TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"), errbuf ));
|
---|
1165 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
1166 | }
|
---|
1167 | }
|
---|
1168 | else
|
---|
1169 | {
|
---|
1170 | if ( (num_sel = select (sockfd+1, &fds, NULL, NULL, &tv)) == -1)
|
---|
1171 | {
|
---|
1172 | if (sig_raised == 1)
|
---|
1173 | {
|
---|
1174 | sig_raised = 2;
|
---|
1175 | continue;
|
---|
1176 | }
|
---|
1177 | if ( errno == EINTR || errno == EINPROGRESS ) /* try again */
|
---|
1178 | continue;
|
---|
1179 | *w_error = errno;
|
---|
1180 |
|
---|
1181 | sh_error_message(*w_error, errbuf, sizeof(errbuf));
|
---|
1182 | sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
1183 | errbuf,
|
---|
1184 | _("sh_write_select (rs)") );
|
---|
1185 | TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"), errbuf ));
|
---|
1186 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
1187 | }
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | /* on Linux, timeout is modified to reflect the amount of
|
---|
1191 | * time not slept
|
---|
1192 | */
|
---|
1193 | tv.tv_sec = 1;
|
---|
1194 | tv.tv_usec = 0;
|
---|
1195 |
|
---|
1196 |
|
---|
1197 | /* let's not hang on forever
|
---|
1198 | */
|
---|
1199 | if (num_sel == 0)
|
---|
1200 | {
|
---|
1201 | ++select_now; /* timeout */
|
---|
1202 | if ( select_now > timeout ) /* 5 minutes */
|
---|
1203 | {
|
---|
1204 | #ifdef ETIMEDOUT
|
---|
1205 | *w_error = ETIMEDOUT;
|
---|
1206 | #else
|
---|
1207 | *w_error = 0;
|
---|
1208 | #endif
|
---|
1209 |
|
---|
1210 | TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
|
---|
1211 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
1212 | }
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | if ( FD_ISSET (sockfd, &fds) )
|
---|
1216 | {
|
---|
1217 | if (type == SH_DO_WRITE)
|
---|
1218 | count = write (sockfd, buf, nbytes-countbytes);
|
---|
1219 | else
|
---|
1220 | count = read (sockfd, buf, nbytes-countbytes);
|
---|
1221 |
|
---|
1222 | if (count > 0)
|
---|
1223 | {
|
---|
1224 | countbytes += count;
|
---|
1225 | buf += count; /* move buffer pointer forward */
|
---|
1226 | if (countbytes < nbytes) FD_SET( sockfd, &fds );
|
---|
1227 | }
|
---|
1228 | else if (count < 0 && errno == EINTR)
|
---|
1229 | {
|
---|
1230 | FD_SET( sockfd, &fds );
|
---|
1231 | }
|
---|
1232 | else if (count < 0)
|
---|
1233 | {
|
---|
1234 | *w_error = errno;
|
---|
1235 |
|
---|
1236 | sh_error_message(*w_error, errbuf, sizeof(errbuf));
|
---|
1237 | sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
1238 | errbuf,
|
---|
1239 | (type == SH_DO_WRITE) ?
|
---|
1240 | _("sh_write_select (w)") : _("sh_write_select (r)"));
|
---|
1241 | TPT(( 0, FIL__, __LINE__, _("msg=<count < 0>\n")));
|
---|
1242 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
1243 | }
|
---|
1244 | else /* count == 0 */
|
---|
1245 | {
|
---|
1246 | *w_error = errno;
|
---|
1247 |
|
---|
1248 | TPT(( 0, FIL__, __LINE__, _("msg=<count == 0>\n")));
|
---|
1249 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
1250 | }
|
---|
1251 | }
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | *w_error = 0;
|
---|
1255 |
|
---|
1256 | TPT(( 0, FIL__, __LINE__, _("msg=<count = %d>\n"), countbytes));
|
---|
1257 | SL_RETURN( countbytes, _("sh_write_select"));
|
---|
1258 | }
|
---|
1259 | #endif
|
---|
1260 |
|
---|
1261 | #if defined (SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
1262 | unsigned long write_port (int sockfd, char *buf, unsigned long nbytes,
|
---|
1263 | int * w_error, int timeout)
|
---|
1264 | {
|
---|
1265 | unsigned long bytes;
|
---|
1266 |
|
---|
1267 | SL_ENTER(_("write_port"));
|
---|
1268 |
|
---|
1269 | bytes = sh_write_select(SH_DO_WRITE, sockfd, buf, nbytes, w_error, timeout);
|
---|
1270 | if (*w_error != 0)
|
---|
1271 | {
|
---|
1272 | char errbuf[SH_ERRBUF_SIZE];
|
---|
1273 | sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP,
|
---|
1274 | sh_error_message (*w_error, errbuf, sizeof(errbuf)),
|
---|
1275 | (long) sockfd, _("write_port"));
|
---|
1276 | }
|
---|
1277 | SL_RETURN( bytes, _("write_port"));
|
---|
1278 | }
|
---|
1279 | #endif
|
---|
1280 |
|
---|
1281 | #if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
1282 |
|
---|
1283 | unsigned long read_port (int sockfd, char *buf, unsigned long nbytes,
|
---|
1284 | int * w_error, int timeout)
|
---|
1285 | {
|
---|
1286 | unsigned long bytes;
|
---|
1287 |
|
---|
1288 | SL_ENTER(_("read_port"));
|
---|
1289 |
|
---|
1290 | bytes = sh_write_select(SH_DO_READ, sockfd, buf, nbytes, w_error, timeout);
|
---|
1291 | if (*w_error != 0)
|
---|
1292 | {
|
---|
1293 | char errbuf[SH_ERRBUF_SIZE];
|
---|
1294 | sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP,
|
---|
1295 | sh_error_message (*w_error, errbuf, sizeof(errbuf)),
|
---|
1296 | (long) sockfd, _("read_port"));
|
---|
1297 | }
|
---|
1298 | SL_RETURN( bytes, _("read_port"));
|
---|
1299 | }
|
---|
1300 | #endif
|
---|
1301 |
|
---|
1302 | #if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
1303 |
|
---|
1304 | int check_request_nerr (char * have, char * need)
|
---|
1305 | {
|
---|
1306 | SL_ENTER(_("check_request_nerr"));
|
---|
1307 | ASSERT_RET((have != NULL && need != NULL),
|
---|
1308 | _("have != NULL && need != NULL"), (-1))
|
---|
1309 |
|
---|
1310 | if ( (have[0] == need[0]) && (have[1] == need[1]) &&
|
---|
1311 | (have[2] == need[2]) && (have[3] == need[3]))
|
---|
1312 | SL_RETURN(0, _("check_request_nerr"));
|
---|
1313 | SL_RETURN((-1), _("check_request_nerr"));
|
---|
1314 | }
|
---|
1315 | #endif
|
---|
1316 |
|
---|
1317 | #if defined (SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
1318 |
|
---|
1319 | int check_request (char * have, char * need)
|
---|
1320 | {
|
---|
1321 | char first[21], second[5];
|
---|
1322 | int i;
|
---|
1323 |
|
---|
1324 | SL_ENTER(_("check_request"));
|
---|
1325 | i = check_request_nerr (have, need);
|
---|
1326 |
|
---|
1327 | if (i == 0)
|
---|
1328 | SL_RETURN(0, _("check_request"));
|
---|
1329 |
|
---|
1330 | for (i = 0; i < 4; ++i)
|
---|
1331 | {
|
---|
1332 | second[i] = need[i];
|
---|
1333 | sprintf(&first[i*4], _("%c%03o"), /* known to fit */
|
---|
1334 | '\\', (unsigned char) have[i]);
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | first[20] = '\0'; second[4] = '\0';
|
---|
1338 |
|
---|
1339 | sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_NETST,
|
---|
1340 | second, first);
|
---|
1341 | SL_RETURN((-1), _("check_request"));
|
---|
1342 | }
|
---|
1343 | #endif
|
---|
1344 |
|
---|
1345 | #if defined (SH_WITH_SERVER)
|
---|
1346 |
|
---|
1347 | int check_request_s (char * have, char * need, char * clt)
|
---|
1348 | {
|
---|
1349 | char first[21], second[5];
|
---|
1350 | int i;
|
---|
1351 |
|
---|
1352 | SL_ENTER(_("check_request_s"));
|
---|
1353 | i = check_request_nerr (have, need);
|
---|
1354 |
|
---|
1355 | if (i == 0)
|
---|
1356 | SL_RETURN( (0), _("check_request_s"));
|
---|
1357 |
|
---|
1358 | for (i = 0; i < 4; ++i)
|
---|
1359 | {
|
---|
1360 | second[i] = need[i];
|
---|
1361 | sprintf(&first[i*4], _("%c%03o"), /* known to fit */
|
---|
1362 | '\\', (unsigned char) have[i]);
|
---|
1363 | }
|
---|
1364 | first[20] = '\0'; second[4] = '\0';
|
---|
1365 | sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_NETST1,
|
---|
1366 | second, first, clt);
|
---|
1367 | SL_RETURN( (-1), _("check_request_s"));
|
---|
1368 | }
|
---|
1369 | #endif
|
---|
1370 |
|
---|
1371 | #if defined (SH_WITH_CLIENT) || defined (SH_WITH_SERVER)
|
---|
1372 |
|
---|
1373 | void get_header (unsigned char * head, unsigned long * bytes, char * u)
|
---|
1374 | {
|
---|
1375 | SL_ENTER(_("get_header"));
|
---|
1376 |
|
---|
1377 | *bytes =
|
---|
1378 | (256 * (unsigned int)head[1] + (unsigned int)head[2]);
|
---|
1379 |
|
---|
1380 | if (u != NULL)
|
---|
1381 | {
|
---|
1382 | u[0] = head[3];
|
---|
1383 | u[1] = head[4];
|
---|
1384 | u[2] = head[5];
|
---|
1385 | u[3] = head[6];
|
---|
1386 | u[4] = '\0';
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | SL_RET0(_("get_header"));
|
---|
1390 | }
|
---|
1391 | #endif
|
---|
1392 |
|
---|
1393 | #if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
1394 |
|
---|
1395 | #ifdef SH_ENCRYPT_2
|
---|
1396 | #define TRANS_BYTES 65120
|
---|
1397 | #else
|
---|
1398 | #define TRANS_BYTES 65280
|
---|
1399 | #endif
|
---|
1400 |
|
---|
1401 | void put_header (unsigned char * head, int protocol,
|
---|
1402 | unsigned long * length, char * u)
|
---|
1403 | {
|
---|
1404 |
|
---|
1405 | /* static long transfer_limit = (8 * SH_BUFSIZE); V0.8 */
|
---|
1406 | static unsigned long transfer_limit = TRANS_BYTES + 6 + KEY_LEN;
|
---|
1407 |
|
---|
1408 | SL_ENTER(_("put_header"));
|
---|
1409 |
|
---|
1410 | head[0] = protocol;
|
---|
1411 |
|
---|
1412 | ASSERT((*length < transfer_limit), _("*length < transfer_limit"))
|
---|
1413 |
|
---|
1414 | if (*length > transfer_limit)
|
---|
1415 | *length = transfer_limit;
|
---|
1416 |
|
---|
1417 | head[1] = (unsigned int)(*length/256);
|
---|
1418 | head[2] = (unsigned int)(*length-256 * head[1]);
|
---|
1419 | if (u == NULL)
|
---|
1420 | {
|
---|
1421 | head[3] = 0x01;
|
---|
1422 | head[4] = 0x01;
|
---|
1423 | head[5] = 0x01;
|
---|
1424 | head[6] = 0x01;
|
---|
1425 | }
|
---|
1426 | else
|
---|
1427 | {
|
---|
1428 | head[3] = u[0];
|
---|
1429 | head[4] = u[1];
|
---|
1430 | head[5] = u[2];
|
---|
1431 | head[6] = u[3];
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | SL_RET0(_("put_header"));
|
---|
1435 | }
|
---|
1436 | #endif
|
---|
1437 |
|
---|
1438 | /* ------------------------------------------
|
---|
1439 | *
|
---|
1440 | * version 2 client/server protocol
|
---|
1441 | *
|
---|
1442 | * ------------------------------------------
|
---|
1443 | *
|
---|
1444 | * header : flag size[2]
|
---|
1445 | *
|
---|
1446 | * payload: random_pad[8] protocol[4] size[4] payload[payload_size] padding
|
---|
1447 | *
|
---|
1448 | * full_size <= 8192; payload_size <= 8176 (511*16); msg_size <= 8128 (508*16)
|
---|
1449 | * (msg_size = payload_size - key_len = payload_size - 48)
|
---|
1450 | */
|
---|
1451 |
|
---|
1452 | /*
|
---|
1453 | * only SH_V2_FULLSIZE is used, and only once
|
---|
1454 | */
|
---|
1455 | #if 0
|
---|
1456 | #ifdef SH_WITH_SERVER
|
---|
1457 | #define SH_V2_FULLSIZE 240
|
---|
1458 | #define SH_V2_PAYLOAD 224
|
---|
1459 | #define SH_V2_MESSAGE 176
|
---|
1460 | #else
|
---|
1461 | #define SH_V2_FULLSIZE 1024
|
---|
1462 | #define SH_V2_PAYLOAD 1008
|
---|
1463 | #define SH_V2_MESSAGE 960
|
---|
1464 | #endif
|
---|
1465 | #endif
|
---|
1466 | #define SH_V2_FULLSIZE 1024
|
---|
1467 |
|
---|
1468 | #ifdef SH_ENCRYPT
|
---|
1469 | #include "rijndael-api-fst.h"
|
---|
1470 | #endif
|
---|
1471 |
|
---|
1472 | void sh_tools_show_header (unsigned char * head, char sign)
|
---|
1473 | {
|
---|
1474 | #define SH_IS_ASCII(c) (((c) & ~0x7f) == 0)
|
---|
1475 |
|
---|
1476 |
|
---|
1477 | int msg_size = (256 * (unsigned int)head[1] + (unsigned int)head[2]);
|
---|
1478 | char code[32];
|
---|
1479 | char * p = &code[0];
|
---|
1480 |
|
---|
1481 | memset (code, ' ', 32); /* space */
|
---|
1482 |
|
---|
1483 | if ((head[0] & SH_PROTO_SRP) != 0) { p[0]='S';p[1]='R';p[2]='P';}
|
---|
1484 | p += 4;
|
---|
1485 | if ((head[0] & SH_PROTO_MSG) != 0) { p[0]='M';p[1]='S';p[2]='G';}
|
---|
1486 | p += 4;
|
---|
1487 | if ((head[0] & SH_PROTO_BIG) != 0) { p[0]='B';p[1]='I';p[2]='G';}
|
---|
1488 | p += 4;
|
---|
1489 | if ((head[0] & SH_PROTO_END) != 0) { p[0]='E';p[1]='N';p[2]='D';}
|
---|
1490 | p += 4;
|
---|
1491 | if ((head[0] & SH_PROTO_ENC) != 0) { p[0]='E';p[1]='N';p[2]='C';}
|
---|
1492 | p += 4;
|
---|
1493 | if ((head[0] & SH_PROTO_EN2) != 0) { p[0]='E';p[1]='N';p[2]='2';}
|
---|
1494 | code[23] = '\0';
|
---|
1495 |
|
---|
1496 | if (SH_IS_ASCII(head[3]) && isalpha(head[3]) &&
|
---|
1497 | SH_IS_ASCII(head[4]) && isalpha(head[4]) &&
|
---|
1498 | SH_IS_ASCII(head[5]) && isalpha(head[5]) &&
|
---|
1499 | SH_IS_ASCII(head[6]) && isalpha(head[6])) {
|
---|
1500 | fprintf(stderr, _("%c %3o %s %5d %c %c %c %c\n"), sign,
|
---|
1501 | head[0], code, msg_size, head[3], head[4], head[5], head[6]);
|
---|
1502 | } else {
|
---|
1503 | fprintf(stderr, _("%c %3o %s %5d %2X %2X %2X %2X\n"), sign,
|
---|
1504 | head[0], code, msg_size, head[3], head[4], head[5], head[6]);
|
---|
1505 | }
|
---|
1506 | return;
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | #ifdef SH_ENCRYPT
|
---|
1510 | /*
|
---|
1511 | * #define DEBUG_EN2
|
---|
1512 | *
|
---|
1513 | * ingest version 1 7-byte header and payload, return version2 header/payload
|
---|
1514 | * last 4 bytes of outgoing header are set to dummy value
|
---|
1515 | */
|
---|
1516 | char * sh_tools_makePack (unsigned char * header,
|
---|
1517 | char * payload, unsigned long payload_size,
|
---|
1518 | keyInstance * keyInstE)
|
---|
1519 | {
|
---|
1520 | UINT32 rpad[3];
|
---|
1521 | unsigned char head[16];
|
---|
1522 | double epad;
|
---|
1523 | unsigned long i_epad = 0;
|
---|
1524 | unsigned long i_blk = payload_size / 16;
|
---|
1525 | unsigned long i_blkmax = SH_V2_FULLSIZE / 16;
|
---|
1526 | unsigned long pads = 0;
|
---|
1527 | size_t full_size;
|
---|
1528 | char * full_ret;
|
---|
1529 |
|
---|
1530 | char * p;
|
---|
1531 | RIJ_BYTE inBlock[B_SIZ];
|
---|
1532 | RIJ_BYTE outBlock[B_SIZ];
|
---|
1533 | int j;
|
---|
1534 | cipherInstance cipherInst;
|
---|
1535 | int err_num;
|
---|
1536 | int blkfac;
|
---|
1537 | int oflow = 0;
|
---|
1538 |
|
---|
1539 | /*
|
---|
1540 | SL_REQUIRE (i_blk*16 == payload_size, _("payload_size % 16 != 0"));
|
---|
1541 | */
|
---|
1542 | if ((i_blk * 16) != payload_size) ++i_blk;
|
---|
1543 | #ifdef DEBUG_EN2
|
---|
1544 | fprintf(stderr, "SEND <%d> blocks <%d>\n", payload_size, i_blk);
|
---|
1545 | #endif
|
---|
1546 | /* random_pad
|
---|
1547 | */
|
---|
1548 | rpad[1] = taus_get ();
|
---|
1549 | memcpy (head, &rpad[1], 4);
|
---|
1550 | rpad[0] = taus_get ();
|
---|
1551 | memcpy (&head[4], &rpad[0], 4);
|
---|
1552 | rpad[2] = taus_get ();
|
---|
1553 | memcpy (&head[8], &rpad[2], 4);
|
---|
1554 |
|
---|
1555 | /* protocol
|
---|
1556 | */
|
---|
1557 | /* memcpy (&head[8], &header[3], 4); */
|
---|
1558 |
|
---|
1559 | /* size (payload)
|
---|
1560 | */
|
---|
1561 | head[12] = header[1];
|
---|
1562 | head[13] = header[2];
|
---|
1563 | head[14] = '\0';
|
---|
1564 | head[15] = '\0';
|
---|
1565 |
|
---|
1566 | if (i_blk < i_blkmax)
|
---|
1567 | {
|
---|
1568 | pads = i_blkmax - i_blk;
|
---|
1569 | /* memcpy((char *) &rpad[2], &head[12], 4); */
|
---|
1570 | epad = taus_get_double (&rpad);
|
---|
1571 | #ifdef DEBUG_EN2
|
---|
1572 | fprintf(stderr, "PAD1 <%d> <%f>\n", pads, epad);
|
---|
1573 | #endif
|
---|
1574 | i_epad = (unsigned long) (pads * epad);
|
---|
1575 | #ifdef DEBUG_EN2
|
---|
1576 | fprintf(stderr, "PAD2 <%d> <%d>\n", i_epad, (i_epad*16));
|
---|
1577 | #endif
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | full_size = 16; /* head */
|
---|
1581 | if (sl_ok_muls(i_blk, 16) && sl_ok_adds(full_size, (i_blk*16)))
|
---|
1582 | full_size = full_size + (i_blk*16); /* payload */
|
---|
1583 | else
|
---|
1584 | oflow = 1;
|
---|
1585 | if (sl_ok_adds(full_size, (i_epad*16)))
|
---|
1586 | full_size = full_size + (i_epad*16); /* pad */
|
---|
1587 | else
|
---|
1588 | i_epad = 0;
|
---|
1589 |
|
---|
1590 | if (oflow)
|
---|
1591 | {
|
---|
1592 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
1593 | _("integer overflow"),
|
---|
1594 | _("sh_tools_makePack"));
|
---|
1595 | }
|
---|
1596 |
|
---|
1597 | full_ret = SH_ALLOC(full_size);
|
---|
1598 | memcpy(full_ret, head, 16);
|
---|
1599 | if (payload != NULL && !oflow)
|
---|
1600 | {
|
---|
1601 | memcpy(&full_ret[16], payload, payload_size);
|
---|
1602 | }
|
---|
1603 | if ((i_blk*16) > payload_size && !oflow)
|
---|
1604 | {
|
---|
1605 | #ifdef DEBUG_EN2
|
---|
1606 | fprintf(stderr, "SEN2 <%d>\n", (i_blk*16) - payload_size);
|
---|
1607 | #endif
|
---|
1608 | memset(&full_ret[16+payload_size], '\0', (i_blk*16) - payload_size);
|
---|
1609 | payload_size = i_blk * 16;
|
---|
1610 | }
|
---|
1611 | memset(&full_ret[16+payload_size], '\0', i_epad*16);
|
---|
1612 | #ifdef DEBUG_EN2
|
---|
1613 | fprintf(stderr, "SEN3 <%d> <%d>\n", full_size, i_epad*16);
|
---|
1614 | #endif
|
---|
1615 |
|
---|
1616 | /* rewrite header
|
---|
1617 | */
|
---|
1618 | header[1] = (unsigned int)(full_size/256);
|
---|
1619 | header[2] = (unsigned int)(full_size - (256 * header[1]));
|
---|
1620 | /* don't erase protocol from header
|
---|
1621 | memset(&header[3], '\0', 4);
|
---|
1622 | */
|
---|
1623 | p = full_ret; blkfac = full_size / 16;
|
---|
1624 |
|
---|
1625 | err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
|
---|
1626 |
|
---|
1627 | if (err_num < 0)
|
---|
1628 | {
|
---|
1629 | char expbuf[SH_ERRBUF_SIZE];
|
---|
1630 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
1631 | errorExplain(err_num, expbuf, sizeof(expbuf)),
|
---|
1632 | _("sh_tools_makePack: cipherInit"));
|
---|
1633 | }
|
---|
1634 | for (j = 0; j < blkfac; ++j)
|
---|
1635 | {
|
---|
1636 | memcpy(inBlock, p, B_SIZ);
|
---|
1637 | err_num = blockEncrypt(&cipherInst, keyInstE,
|
---|
1638 | inBlock, 128 * BNUM, outBlock);
|
---|
1639 | if (err_num < 0)
|
---|
1640 | {
|
---|
1641 | char expbuf[SH_ERRBUF_SIZE];
|
---|
1642 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
1643 | errorExplain(err_num, expbuf, sizeof(expbuf)),
|
---|
1644 | _("sh_tools_makePack: blockEncrypt"));
|
---|
1645 | }
|
---|
1646 | memcpy(p, outBlock, B_SIZ);
|
---|
1647 | p += B_SIZ;
|
---|
1648 | }
|
---|
1649 |
|
---|
1650 | return full_ret;
|
---|
1651 | }
|
---|
1652 |
|
---|
1653 | /* write a 7-byte header and return payload as expected by version 1
|
---|
1654 | * last 4 bytes of incoming header are dummy
|
---|
1655 | */
|
---|
1656 | char * sh_tools_revertPack (unsigned char * header, char * message,
|
---|
1657 | keyInstance * keyInstD,
|
---|
1658 | unsigned long message_size)
|
---|
1659 | {
|
---|
1660 | unsigned long msg_size;
|
---|
1661 | char * msg_ret;
|
---|
1662 |
|
---|
1663 | char * p;
|
---|
1664 | RIJ_BYTE inBlock[B_SIZ];
|
---|
1665 | RIJ_BYTE outBlock[B_SIZ];
|
---|
1666 | int j;
|
---|
1667 | cipherInstance cipherInst;
|
---|
1668 | int err_num;
|
---|
1669 | int blkfac;
|
---|
1670 | char expbuf[SH_ERRBUF_SIZE];
|
---|
1671 |
|
---|
1672 | msg_size = (256 * (unsigned int)header[1] + (unsigned int)header[2]);
|
---|
1673 | #ifdef DEBUG_EN2
|
---|
1674 | fprintf(stderr, "RECV <%lu>\n", msg_size);
|
---|
1675 | #endif
|
---|
1676 | if (msg_size > message_size) {
|
---|
1677 | msg_size = message_size;
|
---|
1678 | #ifdef DEBUG_EN2
|
---|
1679 | fprintf(stderr, "RECV TRUNC1 <%lu>\n", msg_size);
|
---|
1680 | #endif
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | p = message; blkfac = msg_size / 16;
|
---|
1684 |
|
---|
1685 | err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
|
---|
1686 |
|
---|
1687 | if (err_num < 0)
|
---|
1688 | {
|
---|
1689 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
1690 | errorExplain(err_num, expbuf, sizeof(expbuf)),
|
---|
1691 | _("sh_tools_revertPack: cipherInit"));
|
---|
1692 | }
|
---|
1693 | for (j = 0; j < blkfac; ++j)
|
---|
1694 | {
|
---|
1695 | memcpy(inBlock, p, B_SIZ);
|
---|
1696 | err_num = blockDecrypt(&cipherInst, keyInstD,
|
---|
1697 | inBlock, 128 * BNUM, outBlock);
|
---|
1698 | if (err_num < 0)
|
---|
1699 | {
|
---|
1700 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
1701 | errorExplain(err_num, expbuf, sizeof(expbuf)),
|
---|
1702 | _("sh_tools_revertPack: blockDecrypt"));
|
---|
1703 | }
|
---|
1704 | memcpy(p, outBlock, B_SIZ);
|
---|
1705 | p += B_SIZ;
|
---|
1706 | }
|
---|
1707 |
|
---|
1708 | /* rewrite size in header
|
---|
1709 | */
|
---|
1710 | header[1] = message[12];
|
---|
1711 | header[2] = message[13];
|
---|
1712 | msg_size = (256 * (unsigned int)header[1] + (unsigned int)header[2]);
|
---|
1713 |
|
---|
1714 | if (msg_size > (message_size-16))
|
---|
1715 | {
|
---|
1716 | msg_size = message_size-16;
|
---|
1717 | header[1] = (unsigned int)(msg_size/256);
|
---|
1718 | header[2] = (unsigned int)(msg_size - (256 * header[1]));
|
---|
1719 | #ifdef DEBUG_EN2
|
---|
1720 | fprintf(stderr, "RECV TRUNC2 <%lu>\n", msg_size);
|
---|
1721 | #endif
|
---|
1722 | }
|
---|
1723 | #ifdef DEBUG_EN2
|
---|
1724 | fprintf(stderr, "REC2 <%lu>\n", msg_size);
|
---|
1725 | #endif
|
---|
1726 | /* protocol
|
---|
1727 | */
|
---|
1728 | /* memcpy(&header[3], &message[8], 4); */
|
---|
1729 |
|
---|
1730 | /* payload
|
---|
1731 | */
|
---|
1732 | msg_ret = SH_ALLOC(msg_size+1);
|
---|
1733 | if (msg_size > 0)
|
---|
1734 | {
|
---|
1735 | memcpy(msg_ret, &message[16], msg_size);
|
---|
1736 | }
|
---|
1737 | msg_ret[msg_size] = '\0';
|
---|
1738 | #ifdef DEBUG_EN2
|
---|
1739 | fprintf(stderr, "REC3 <%lu>\n", msg_size);
|
---|
1740 | #endif
|
---|
1741 | SH_FREE(message);
|
---|
1742 |
|
---|
1743 | return msg_ret;
|
---|
1744 | }
|
---|
1745 | #endif
|
---|
1746 |
|
---|
1747 | int sh_tools_hash_add(char * key, char * buf, int buflen)
|
---|
1748 | {
|
---|
1749 | char * theSig;
|
---|
1750 | char sigbuf[KEYBUF_SIZE];
|
---|
1751 |
|
---|
1752 | SL_ENTER(_("sh_tools_hash_add"));
|
---|
1753 |
|
---|
1754 | theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
|
---|
1755 | sl_strlcat(buf, theSig, buflen + KEY_LEN + 1);
|
---|
1756 |
|
---|
1757 | SL_RETURN((0), _("sh_tools_hash_add"));
|
---|
1758 | }
|
---|
1759 |
|
---|
1760 |
|
---|
1761 | /* return 0 (== FALSE) if no match, else 1 (== TRUE)
|
---|
1762 | */
|
---|
1763 | int sh_tools_hash_vfy(char * key, char * buf, int buflen)
|
---|
1764 | {
|
---|
1765 | char hash[KEY_LEN+1];
|
---|
1766 | register int i;
|
---|
1767 | char * theSig;
|
---|
1768 | char sigbuf[KEYBUF_SIZE];
|
---|
1769 |
|
---|
1770 | SL_ENTER(_("sh_tools_hash_vfy"));
|
---|
1771 |
|
---|
1772 | theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
|
---|
1773 | sl_strlcpy(hash, theSig, KEY_LEN+1);
|
---|
1774 |
|
---|
1775 | for (i = 0; i < KEY_LEN; ++i)
|
---|
1776 | {
|
---|
1777 | if (buf[buflen + i] != hash[i])
|
---|
1778 | SL_RETURN((0), _("sh_tools_hash_vfy"));
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 | SL_RETURN((1), _("sh_tools_hash_vfy"));
|
---|
1782 | }
|
---|
1783 |
|
---|
1784 | /* ------------------------------------------ */
|
---|
1785 |
|
---|
1786 | #if defined (SH_WITH_SERVER)
|
---|
1787 |
|
---|
1788 | /* add a checksum to a buffer; put checksum in front
|
---|
1789 | */
|
---|
1790 | char * hash_me (char * key, char * buf, int buflen)
|
---|
1791 | {
|
---|
1792 | char hash[KEY_LEN+1];
|
---|
1793 | char * temp = NULL;
|
---|
1794 | register int i;
|
---|
1795 | int total = 0;
|
---|
1796 | char * theSig;
|
---|
1797 | char sigbuf[KEYBUF_SIZE];
|
---|
1798 |
|
---|
1799 |
|
---|
1800 | SL_ENTER(_("hash_me"));
|
---|
1801 |
|
---|
1802 | #ifdef DEBUG_EN2
|
---|
1803 | fprintf(stderr, "hash_me <%s> <%d>\n",
|
---|
1804 | (key == NULL) ? "NULL" : key, buflen);
|
---|
1805 | #endif
|
---|
1806 | /* key = H(NSRV,NCLT,SK)
|
---|
1807 | */
|
---|
1808 | ASSERT_RET((key != NULL), _("key != NULL"), (NULL));
|
---|
1809 | ASSERT_RET((buflen >= 0), _("buflen >= 0"), (NULL));
|
---|
1810 |
|
---|
1811 | theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
|
---|
1812 | sl_strlcpy(hash, theSig, KEY_LEN+1);
|
---|
1813 |
|
---|
1814 | if (sl_ok_adds(buflen, KEY_LEN))
|
---|
1815 | {
|
---|
1816 | total = KEY_LEN + buflen;
|
---|
1817 | temp = SH_ALLOC (total);
|
---|
1818 |
|
---|
1819 | for (i = 0; i < KEY_LEN; ++i)
|
---|
1820 | temp[i] = hash[i];
|
---|
1821 |
|
---|
1822 | for (i = 0; i < buflen; ++i)
|
---|
1823 | temp[i+KEY_LEN] = buf[i];
|
---|
1824 | }
|
---|
1825 | else
|
---|
1826 | {
|
---|
1827 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
1828 | _("integer overflow"),
|
---|
1829 | _("hash_me"));
|
---|
1830 | temp = sh_util_strdup(buf);
|
---|
1831 | }
|
---|
1832 | SL_RETURN(temp, _("hash_me"));
|
---|
1833 | }
|
---|
1834 | #endif
|
---|
1835 |
|
---|
1836 | #if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
|
---|
1837 |
|
---|
1838 | /* verify the checksum of a buffer; checksum comes first
|
---|
1839 | */
|
---|
1840 | int hash_check(char * key,
|
---|
1841 | char * buf, int buflen)
|
---|
1842 | {
|
---|
1843 | char hash[KEY_LEN+1];
|
---|
1844 | register int i;
|
---|
1845 | char * theSig;
|
---|
1846 | char sigbuf[KEYBUF_SIZE];
|
---|
1847 |
|
---|
1848 | SL_ENTER(_("hash_check"));
|
---|
1849 |
|
---|
1850 | #ifdef DEBUG_EN2
|
---|
1851 | fprintf(stderr, "hash_check <%s> <%d>\n",
|
---|
1852 | (key == NULL) ? "NULL" : key, buflen);
|
---|
1853 | #endif
|
---|
1854 | theSig = sh_util_siggen (key, &buf[KEY_LEN], buflen-KEY_LEN,
|
---|
1855 | sigbuf, sizeof(sigbuf));
|
---|
1856 | sl_strlcpy(hash, theSig, KEY_LEN+1);
|
---|
1857 |
|
---|
1858 | for (i = 0; i < KEY_LEN; ++i)
|
---|
1859 | {
|
---|
1860 | if (buf[i] != hash[i])
|
---|
1861 | SL_RETURN((-1), _("hash_check"));
|
---|
1862 | }
|
---|
1863 | SL_RETURN((0), _("hash_check"));
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | #endif
|
---|
1867 |
|
---|
1868 | #if defined (SH_WITH_SERVER)
|
---|
1869 |
|
---|
1870 | char * get_client_conf_file (char * peer, unsigned long * length)
|
---|
1871 | {
|
---|
1872 | char * ret;
|
---|
1873 | int status;
|
---|
1874 | struct stat buf;
|
---|
1875 | char * base;
|
---|
1876 | size_t size;
|
---|
1877 |
|
---|
1878 | SL_ENTER(_("get_client_conf_file"));
|
---|
1879 |
|
---|
1880 | base = sh_util_strdup(DEFAULT_DATAROOT);
|
---|
1881 |
|
---|
1882 | size = sl_strlen(base);
|
---|
1883 | if (sl_ok_adds(size, sl_strlen(peer)))
|
---|
1884 | size += sl_strlen(peer);
|
---|
1885 | if (sl_ok_adds(size, 6))
|
---|
1886 | size += 6;
|
---|
1887 |
|
---|
1888 | ret = SH_ALLOC(size);
|
---|
1889 | sl_strlcpy(ret, base, size);
|
---|
1890 | sl_strlcat(ret, _("/rc."), size);
|
---|
1891 | sl_strlcat(ret, peer, size);
|
---|
1892 |
|
---|
1893 | status = retry_stat (FIL__, __LINE__, ret, &buf);
|
---|
1894 |
|
---|
1895 | if (status == 0)
|
---|
1896 | goto lab_end;
|
---|
1897 | else
|
---|
1898 | sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
|
---|
1899 | (long) sh.effective.uid, ret);
|
---|
1900 |
|
---|
1901 | sl_strlcpy(ret, base, size);
|
---|
1902 | sl_strlcat(ret, "/rc", size);
|
---|
1903 |
|
---|
1904 | status = retry_stat (FIL__, __LINE__, ret, &buf);
|
---|
1905 |
|
---|
1906 | if (status == 0)
|
---|
1907 | goto lab_end;
|
---|
1908 | else
|
---|
1909 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_ACCESS,
|
---|
1910 | (long) sh.effective.uid, ret);
|
---|
1911 |
|
---|
1912 | SH_FREE(base);
|
---|
1913 | SH_FREE(ret);
|
---|
1914 | *length=0;
|
---|
1915 | SL_RETURN(NULL, _("get_client_conf_file"));
|
---|
1916 |
|
---|
1917 | lab_end:
|
---|
1918 | if (buf.st_size > 0x7fffffff)
|
---|
1919 | {
|
---|
1920 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
1921 | _("File too large"), _("get_client_conf_file"));
|
---|
1922 | SH_FREE(base);
|
---|
1923 | SL_RETURN(NULL, _("get_client_conf_file"));
|
---|
1924 | }
|
---|
1925 | *length = (unsigned long) buf.st_size;
|
---|
1926 | SH_FREE(base);
|
---|
1927 | SL_RETURN(ret, _("get_client_conf_file"));
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 | char * get_client_data_file (char * peer, unsigned long * length)
|
---|
1931 | {
|
---|
1932 | char * ret;
|
---|
1933 | int status;
|
---|
1934 | struct stat buf;
|
---|
1935 |
|
---|
1936 | char * base;
|
---|
1937 | size_t size;
|
---|
1938 |
|
---|
1939 | SL_ENTER(_("get_client_data_file"));
|
---|
1940 |
|
---|
1941 | base = sh_util_strdup(DEFAULT_DATAROOT);
|
---|
1942 |
|
---|
1943 | size = sl_strlen(base);
|
---|
1944 | if (sl_ok_adds(size, sl_strlen(peer)))
|
---|
1945 | size += sl_strlen(peer);
|
---|
1946 | if (sl_ok_adds(size, 8))
|
---|
1947 | size += 8;
|
---|
1948 |
|
---|
1949 | ret = SH_ALLOC(size);
|
---|
1950 | sl_strlcpy(ret, base, size);
|
---|
1951 | sl_strlcat(ret, _("/file."), size);
|
---|
1952 | sl_strlcat(ret, peer, size);
|
---|
1953 |
|
---|
1954 | status = retry_stat (FIL__, __LINE__, ret, &buf);
|
---|
1955 |
|
---|
1956 | if (status == 0)
|
---|
1957 | goto lab1_end;
|
---|
1958 | else
|
---|
1959 | sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
|
---|
1960 | (long) sh.effective.uid, ret);
|
---|
1961 |
|
---|
1962 |
|
---|
1963 | sl_strlcpy(ret, base, size);
|
---|
1964 | sl_strlcat(ret, _("/file"), size);
|
---|
1965 |
|
---|
1966 | status = retry_stat (FIL__, __LINE__, ret, &buf);
|
---|
1967 |
|
---|
1968 | if (status == 0)
|
---|
1969 | goto lab1_end;
|
---|
1970 | else
|
---|
1971 | sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
|
---|
1972 | (long) sh.effective.uid, ret);
|
---|
1973 |
|
---|
1974 |
|
---|
1975 | *length = 0;
|
---|
1976 | SH_FREE(base);
|
---|
1977 | SH_FREE(ret);
|
---|
1978 | SL_RETURN(NULL, _("get_client_data_file"));
|
---|
1979 |
|
---|
1980 | lab1_end:
|
---|
1981 | if (buf.st_size > 0x7fffffff)
|
---|
1982 | {
|
---|
1983 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
1984 | _("File too large"), _("get_client_data_file"));
|
---|
1985 | SH_FREE(base);
|
---|
1986 | SL_RETURN(NULL, _("get_client_data_file"));
|
---|
1987 | }
|
---|
1988 | *length = (unsigned long) buf.st_size;
|
---|
1989 | SH_FREE(base);
|
---|
1990 | SL_RETURN(ret, _("get_client_data_file"));
|
---|
1991 |
|
---|
1992 | }
|
---|
1993 | #endif
|
---|
1994 |
|
---|
1995 | #if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER) || defined(SH_STEALTH) || defined(WITH_GPG) || defined(WITH_PGP)
|
---|
1996 |
|
---|
1997 | /* --------- secure temporary file ------------ */
|
---|
1998 |
|
---|
1999 | SL_TICKET open_tmp ()
|
---|
2000 | {
|
---|
2001 | SL_TICKET fd;
|
---|
2002 | UINT32 ticks;
|
---|
2003 | char * file;
|
---|
2004 | struct stat buf;
|
---|
2005 | int error;
|
---|
2006 | int status = BAD;
|
---|
2007 | char * my_tmp_dir;
|
---|
2008 | char hashbuf[KEYBUF_SIZE];
|
---|
2009 |
|
---|
2010 | SL_ENTER(_("open_tmp"));
|
---|
2011 |
|
---|
2012 | #if defined(SH_TMPDIR)
|
---|
2013 | my_tmp_dir = sh_util_strdup(SH_TMPDIR);
|
---|
2014 | #else
|
---|
2015 | #if defined(SH_WITH_SERVER)
|
---|
2016 | my_tmp_dir = sh_util_strdup(DEFAULT_LOGDIR);
|
---|
2017 | #else
|
---|
2018 | my_tmp_dir = sh_util_strdup(sh.effective.home);
|
---|
2019 | #endif
|
---|
2020 | #endif
|
---|
2021 |
|
---|
2022 | if (0 != tf_trust_check (my_tmp_dir, SL_YESPRIV))
|
---|
2023 | {
|
---|
2024 | dlog(1, FIL__, __LINE__,
|
---|
2025 | _("The directory for temporary files: %s is untrusted, i.e. an\nuntrusted user owns or can write to some directory in the path.\n"),
|
---|
2026 | my_tmp_dir);
|
---|
2027 | sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_TRUST,
|
---|
2028 | (long) sh.effective.uid,
|
---|
2029 | my_tmp_dir);
|
---|
2030 | SH_FREE(my_tmp_dir);
|
---|
2031 | aud_exit (FIL__, __LINE__, EXIT_FAILURE);
|
---|
2032 | }
|
---|
2033 |
|
---|
2034 | do {
|
---|
2035 |
|
---|
2036 | /* create random filename in effective users home directory
|
---|
2037 | */
|
---|
2038 | ticks = taus_get ();
|
---|
2039 | if (my_tmp_dir[0] == '/' && my_tmp_dir[1] == '\0')
|
---|
2040 | file = sh_util_strconcat (my_tmp_dir,
|
---|
2041 | sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4,
|
---|
2042 | hashbuf, sizeof(hashbuf)),
|
---|
2043 | NULL);
|
---|
2044 | else
|
---|
2045 | file = sh_util_strconcat (my_tmp_dir,
|
---|
2046 | "/",
|
---|
2047 | sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4,
|
---|
2048 | hashbuf, sizeof(hashbuf)),
|
---|
2049 | NULL);
|
---|
2050 |
|
---|
2051 | /* check whether it already exists (paranoia)
|
---|
2052 | */
|
---|
2053 | errno = 0;
|
---|
2054 | status = retry_lstat(FIL__, __LINE__, file, &buf);
|
---|
2055 | error = errno;
|
---|
2056 |
|
---|
2057 | if ( (status < 0) && (error == ENOENT) ) /* file does not exist */
|
---|
2058 | status = GOOD;
|
---|
2059 | else if (status < 0) /* unexpected error condition */
|
---|
2060 | {
|
---|
2061 | SH_FREE (file);
|
---|
2062 | SH_FREE(my_tmp_dir);
|
---|
2063 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
2064 | _("Error (lstat) while opening temporary file"), _("open_tmp"));
|
---|
2065 | TPT(( 0, FIL__, __LINE__, _("msg=<Unexpected error %d>\n"), error));
|
---|
2066 | SL_RETURN((-1), _("open_tmp"));
|
---|
2067 | }
|
---|
2068 | else /* file exists */
|
---|
2069 | {
|
---|
2070 | status = BAD;
|
---|
2071 | TPT(( 0, FIL__, __LINE__, _("msg=<Temporary file exists already>\n")));
|
---|
2072 | }
|
---|
2073 |
|
---|
2074 | if (status == GOOD)
|
---|
2075 | {
|
---|
2076 | if (0 == tf_trust_check (file, SL_YESPRIV))
|
---|
2077 | status = GOOD;
|
---|
2078 | else
|
---|
2079 | {
|
---|
2080 | status = BAD;
|
---|
2081 | TPT(( 0, FIL__, __LINE__, _("msg=<Temporary file untrusted>\n")));
|
---|
2082 | }
|
---|
2083 | }
|
---|
2084 |
|
---|
2085 | if (status == BAD)
|
---|
2086 | SH_FREE (file);
|
---|
2087 |
|
---|
2088 | } while (status == BAD);
|
---|
2089 |
|
---|
2090 | fd = sl_open_safe_rdwr (FIL__, __LINE__, file, SL_YESPRIV);
|
---|
2091 | if (SL_ISERROR(fd))
|
---|
2092 | {
|
---|
2093 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, fd, MSG_E_SUBGEN,
|
---|
2094 | _("Error opening temporary file"), _("open_tmp"));
|
---|
2095 | TPT(( 0, FIL__, __LINE__, _("msg=<Error %d temporary file %s>\n"),
|
---|
2096 | fd, file));
|
---|
2097 | }
|
---|
2098 |
|
---|
2099 |
|
---|
2100 | SH_FREE (file);
|
---|
2101 | SH_FREE(my_tmp_dir);
|
---|
2102 |
|
---|
2103 | if (!SL_ISERROR(fd)) {
|
---|
2104 | sl_unlink(fd);
|
---|
2105 | }
|
---|
2106 |
|
---|
2107 | if (!SL_ISERROR(fd))
|
---|
2108 | SL_RETURN((fd), _("open_tmp"));
|
---|
2109 | else
|
---|
2110 | SL_RETURN((-1), _("open_tmp"));
|
---|
2111 | }
|
---|
2112 |
|
---|
2113 |
|
---|
2114 | int close_tmp (SL_TICKET fd)
|
---|
2115 | {
|
---|
2116 | SL_ENTER(_("close_tmp"));
|
---|
2117 |
|
---|
2118 | if (SL_ISERROR(sl_close (fd)))
|
---|
2119 | SL_RETURN((-1), _("close_tmp"));
|
---|
2120 | SL_RETURN((0), _("close_tmp"));
|
---|
2121 | }
|
---|
2122 |
|
---|
2123 | int rewind_tmp (SL_TICKET fd)
|
---|
2124 | {
|
---|
2125 | SL_ENTER(_("rewind_tmp"));
|
---|
2126 |
|
---|
2127 | if (SL_ISERROR(sl_rewind (fd)))
|
---|
2128 | SL_RETURN((-1), _("rewind_tmp"));
|
---|
2129 | SL_RETURN((0), _("rewind_tmp"));
|
---|
2130 | }
|
---|
2131 | #endif
|
---|
2132 |
|
---|
2133 | /********************************************************
|
---|
2134 | * Search rotated logfile
|
---|
2135 | */
|
---|
2136 | #include <unistd.h>
|
---|
2137 | #include <libgen.h>
|
---|
2138 | #include <dirent.h>
|
---|
2139 |
|
---|
2140 | char * sh_rotated_log_search(const char * path, struct stat * buf)
|
---|
2141 | {
|
---|
2142 |
|
---|
2143 | size_t size;
|
---|
2144 | int i;
|
---|
2145 | char * searchpath;
|
---|
2146 | struct stat sbuf;
|
---|
2147 | DIR * dp;
|
---|
2148 | char * dname;
|
---|
2149 | char * bname;
|
---|
2150 |
|
---|
2151 | dname = sh_util_dirname(path);
|
---|
2152 | bname = sh_util_basename(path);
|
---|
2153 |
|
---|
2154 | size = strlen(dname) + strlen(bname) + 4;
|
---|
2155 | searchpath = SH_ALLOC(size);
|
---|
2156 |
|
---|
2157 | for (i = 0; i < 2; ++i)
|
---|
2158 | {
|
---|
2159 | snprintf(searchpath, size, "%s/%s.%1d", dname, bname, i);
|
---|
2160 | if (0 == stat(searchpath, &sbuf) && sbuf.st_ino == buf->st_ino)
|
---|
2161 | {
|
---|
2162 | SH_FREE(dname);
|
---|
2163 | SH_FREE(bname);
|
---|
2164 | return searchpath;
|
---|
2165 | }
|
---|
2166 | }
|
---|
2167 |
|
---|
2168 | SH_FREE(searchpath);
|
---|
2169 |
|
---|
2170 | if (NULL != (dp = opendir(dname)))
|
---|
2171 | {
|
---|
2172 | struct dirent * de;
|
---|
2173 |
|
---|
2174 | while (NULL != (de = readdir(dp)))
|
---|
2175 | {
|
---|
2176 | if (0 == strcmp(de->d_name, ".") || 0 == strcmp(de->d_name, ".."))
|
---|
2177 | continue;
|
---|
2178 |
|
---|
2179 | size = strlen(dname) + strlen(de->d_name) + 2;
|
---|
2180 | searchpath = SH_ALLOC(size);
|
---|
2181 | snprintf(searchpath, size, "%s/%s", dname, de->d_name);
|
---|
2182 |
|
---|
2183 | if (0 == stat(searchpath, &sbuf) && sbuf.st_ino == buf->st_ino)
|
---|
2184 | {
|
---|
2185 | SH_FREE(dname);
|
---|
2186 | SH_FREE(bname);
|
---|
2187 | closedir(dp);
|
---|
2188 | return searchpath;
|
---|
2189 | }
|
---|
2190 |
|
---|
2191 | SH_FREE(searchpath);
|
---|
2192 | }
|
---|
2193 | closedir(dp);
|
---|
2194 | }
|
---|
2195 |
|
---|
2196 | SH_FREE(dname);
|
---|
2197 | SH_FREE(bname);
|
---|
2198 |
|
---|
2199 | return NULL;
|
---|
2200 | }
|
---|
2201 |
|
---|