You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

388 lines
12 KiB

  1. From eb7461e4ceab14020b3a129d826e4ee86f9da8c6 Mon Sep 17 00:00:00 2001
  2. From: Nicholas Marriott <nicholas.marriott@gmail.com>
  3. Date: Tue, 26 Jun 2018 12:19:25 +0100
  4. Subject: [PATCH] Compat fixes from Rosen Penev.
  5. ---
  6. Makefile.am | 6 +
  7. compat/base64.c | 317 ++++++++++++++++++++++++++++++++++++++++++++++++
  8. fdm.h | 18 +++
  9. 3 files changed, 343 insertions(+), 2 deletions(-)
  10. create mode 100644 compat/base64.c
  11. diff --git a/Makefile.am b/Makefile.am
  12. index a4ebbf3..fb25d33 100644
  13. --- a/Makefile.am
  14. +++ b/Makefile.am
  15. @@ -110,6 +110,9 @@ dist_fdm_SOURCES = \
  16. lex.c
  17. nodist_fdm_SOURCES =
  18. +if NO_B64_NTOP
  19. +nodist_fdm_SOURCES += compat/base64.c
  20. +endif
  21. if NO_STRLCAT
  22. nodist_fdm_SOURCES += compat/strlcat.c
  23. endif
  24. diff --git a/compat/base64.c b/compat/base64.c
  25. new file mode 100644
  26. index 0000000..4e44d6a
  27. --- /dev/null
  28. +++ b/compat/base64.c
  29. @@ -0,0 +1,317 @@
  30. +/* $OpenBSD: base64.c,v 1.8 2015/01/16 16:48:51 deraadt Exp $ */
  31. +
  32. +/*
  33. + * Copyright (c) 1996 by Internet Software Consortium.
  34. + *
  35. + * Permission to use, copy, modify, and distribute this software for any
  36. + * purpose with or without fee is hereby granted, provided that the above
  37. + * copyright notice and this permission notice appear in all copies.
  38. + *
  39. + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  40. + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  41. + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  42. + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  43. + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  44. + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  45. + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  46. + * SOFTWARE.
  47. + */
  48. +
  49. +/*
  50. + * Portions Copyright (c) 1995 by International Business Machines, Inc.
  51. + *
  52. + * International Business Machines, Inc. (hereinafter called IBM) grants
  53. + * permission under its copyrights to use, copy, modify, and distribute this
  54. + * Software with or without fee, provided that the above copyright notice and
  55. + * all paragraphs of this notice appear in all copies, and that the name of IBM
  56. + * not be used in connection with the marketing of any product incorporating
  57. + * the Software or modifications thereof, without specific, written prior
  58. + * permission.
  59. + *
  60. + * To the extent it has a right to do so, IBM grants an immunity from suit
  61. + * under its patents, if any, for the use, sale or manufacture of products to
  62. + * the extent that such products are used for performing Domain Name System
  63. + * dynamic updates in TCP/IP networks by means of the Software. No immunity is
  64. + * granted for any product per se or for any other function of any product.
  65. + *
  66. + * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
  67. + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  68. + * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
  69. + * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
  70. + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
  71. + * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
  72. + */
  73. +
  74. +#include <sys/types.h>
  75. +#include <sys/socket.h>
  76. +#include <netinet/in.h>
  77. +#include <arpa/inet.h>
  78. +#include <arpa/nameser.h>
  79. +
  80. +#include <ctype.h>
  81. +#include <resolv.h>
  82. +#include <stdio.h>
  83. +
  84. +#include <stdlib.h>
  85. +#include <string.h>
  86. +
  87. +#include "fdm.h"
  88. +
  89. +static const char Base64[] =
  90. + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  91. +static const char Pad64 = '=';
  92. +
  93. +/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
  94. + The following encoding technique is taken from RFC 1521 by Borenstein
  95. + and Freed. It is reproduced here in a slightly edited form for
  96. + convenience.
  97. +
  98. + A 65-character subset of US-ASCII is used, enabling 6 bits to be
  99. + represented per printable character. (The extra 65th character, "=",
  100. + is used to signify a special processing function.)
  101. +
  102. + The encoding process represents 24-bit groups of input bits as output
  103. + strings of 4 encoded characters. Proceeding from left to right, a
  104. + 24-bit input group is formed by concatenating 3 8-bit input groups.
  105. + These 24 bits are then treated as 4 concatenated 6-bit groups, each
  106. + of which is translated into a single digit in the base64 alphabet.
  107. +
  108. + Each 6-bit group is used as an index into an array of 64 printable
  109. + characters. The character referenced by the index is placed in the
  110. + output string.
  111. +
  112. + Table 1: The Base64 Alphabet
  113. +
  114. + Value Encoding Value Encoding Value Encoding Value Encoding
  115. + 0 A 17 R 34 i 51 z
  116. + 1 B 18 S 35 j 52 0
  117. + 2 C 19 T 36 k 53 1
  118. + 3 D 20 U 37 l 54 2
  119. + 4 E 21 V 38 m 55 3
  120. + 5 F 22 W 39 n 56 4
  121. + 6 G 23 X 40 o 57 5
  122. + 7 H 24 Y 41 p 58 6
  123. + 8 I 25 Z 42 q 59 7
  124. + 9 J 26 a 43 r 60 8
  125. + 10 K 27 b 44 s 61 9
  126. + 11 L 28 c 45 t 62 +
  127. + 12 M 29 d 46 u 63 /
  128. + 13 N 30 e 47 v
  129. + 14 O 31 f 48 w (pad) =
  130. + 15 P 32 g 49 x
  131. + 16 Q 33 h 50 y
  132. +
  133. + Special processing is performed if fewer than 24 bits are available
  134. + at the end of the data being encoded. A full encoding quantum is
  135. + always completed at the end of a quantity. When fewer than 24 input
  136. + bits are available in an input group, zero bits are added (on the
  137. + right) to form an integral number of 6-bit groups. Padding at the
  138. + end of the data is performed using the '=' character.
  139. +
  140. + Since all base64 input is an integral number of octets, only the
  141. + -------------------------------------------------
  142. + following cases can arise:
  143. +
  144. + (1) the final quantum of encoding input is an integral
  145. + multiple of 24 bits; here, the final unit of encoded
  146. + output will be an integral multiple of 4 characters
  147. + with no "=" padding,
  148. + (2) the final quantum of encoding input is exactly 8 bits;
  149. + here, the final unit of encoded output will be two
  150. + characters followed by two "=" padding characters, or
  151. + (3) the final quantum of encoding input is exactly 16 bits;
  152. + here, the final unit of encoded output will be three
  153. + characters followed by one "=" padding character.
  154. + */
  155. +
  156. +int
  157. +b64_ntop(src, srclength, target, targsize)
  158. + u_char const *src;
  159. + size_t srclength;
  160. + char *target;
  161. + size_t targsize;
  162. +{
  163. + size_t datalength = 0;
  164. + u_char input[3];
  165. + u_char output[4];
  166. + int i;
  167. +
  168. + while (2 < srclength) {
  169. + input[0] = *src++;
  170. + input[1] = *src++;
  171. + input[2] = *src++;
  172. + srclength -= 3;
  173. +
  174. + output[0] = input[0] >> 2;
  175. + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
  176. + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
  177. + output[3] = input[2] & 0x3f;
  178. +
  179. + if (datalength + 4 > targsize)
  180. + return (-1);
  181. + target[datalength++] = Base64[output[0]];
  182. + target[datalength++] = Base64[output[1]];
  183. + target[datalength++] = Base64[output[2]];
  184. + target[datalength++] = Base64[output[3]];
  185. + }
  186. +
  187. + /* Now we worry about padding. */
  188. + if (0 != srclength) {
  189. + /* Get what's left. */
  190. + input[0] = input[1] = input[2] = '\0';
  191. + for (i = 0; i < srclength; i++)
  192. + input[i] = *src++;
  193. +
  194. + output[0] = input[0] >> 2;
  195. + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
  196. + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
  197. +
  198. + if (datalength + 4 > targsize)
  199. + return (-1);
  200. + target[datalength++] = Base64[output[0]];
  201. + target[datalength++] = Base64[output[1]];
  202. + if (srclength == 1)
  203. + target[datalength++] = Pad64;
  204. + else
  205. + target[datalength++] = Base64[output[2]];
  206. + target[datalength++] = Pad64;
  207. + }
  208. + if (datalength >= targsize)
  209. + return (-1);
  210. + target[datalength] = '\0'; /* Returned value doesn't count \0. */
  211. + return (datalength);
  212. +}
  213. +
  214. +/* skips all whitespace anywhere.
  215. + converts characters, four at a time, starting at (or after)
  216. + src from base - 64 numbers into three 8 bit bytes in the target area.
  217. + it returns the number of data bytes stored at the target, or -1 on error.
  218. + */
  219. +
  220. +int
  221. +b64_pton(src, target, targsize)
  222. + char const *src;
  223. + u_char *target;
  224. + size_t targsize;
  225. +{
  226. + int tarindex, state, ch;
  227. + u_char nextbyte;
  228. + char *pos;
  229. +
  230. + state = 0;
  231. + tarindex = 0;
  232. +
  233. + while ((ch = (unsigned char)*src++) != '\0') {
  234. + if (isspace(ch)) /* Skip whitespace anywhere. */
  235. + continue;
  236. +
  237. + if (ch == Pad64)
  238. + break;
  239. +
  240. + pos = strchr(Base64, ch);
  241. + if (pos == 0) /* A non-base64 character. */
  242. + return (-1);
  243. +
  244. + switch (state) {
  245. + case 0:
  246. + if (target) {
  247. + if (tarindex >= targsize)
  248. + return (-1);
  249. + target[tarindex] = (pos - Base64) << 2;
  250. + }
  251. + state = 1;
  252. + break;
  253. + case 1:
  254. + if (target) {
  255. + if (tarindex >= targsize)
  256. + return (-1);
  257. + target[tarindex] |= (pos - Base64) >> 4;
  258. + nextbyte = ((pos - Base64) & 0x0f) << 4;
  259. + if (tarindex + 1 < targsize)
  260. + target[tarindex+1] = nextbyte;
  261. + else if (nextbyte)
  262. + return (-1);
  263. + }
  264. + tarindex++;
  265. + state = 2;
  266. + break;
  267. + case 2:
  268. + if (target) {
  269. + if (tarindex >= targsize)
  270. + return (-1);
  271. + target[tarindex] |= (pos - Base64) >> 2;
  272. + nextbyte = ((pos - Base64) & 0x03) << 6;
  273. + if (tarindex + 1 < targsize)
  274. + target[tarindex+1] = nextbyte;
  275. + else if (nextbyte)
  276. + return (-1);
  277. + }
  278. + tarindex++;
  279. + state = 3;
  280. + break;
  281. + case 3:
  282. + if (target) {
  283. + if (tarindex >= targsize)
  284. + return (-1);
  285. + target[tarindex] |= (pos - Base64);
  286. + }
  287. + tarindex++;
  288. + state = 0;
  289. + break;
  290. + }
  291. + }
  292. +
  293. + /*
  294. + * We are done decoding Base-64 chars. Let's see if we ended
  295. + * on a byte boundary, and/or with erroneous trailing characters.
  296. + */
  297. +
  298. + if (ch == Pad64) { /* We got a pad char. */
  299. + ch = (unsigned char)*src++; /* Skip it, get next. */
  300. + switch (state) {
  301. + case 0: /* Invalid = in first position */
  302. + case 1: /* Invalid = in second position */
  303. + return (-1);
  304. +
  305. + case 2: /* Valid, means one byte of info */
  306. + /* Skip any number of spaces. */
  307. + for (; ch != '\0'; ch = (unsigned char)*src++)
  308. + if (!isspace(ch))
  309. + break;
  310. + /* Make sure there is another trailing = sign. */
  311. + if (ch != Pad64)
  312. + return (-1);
  313. + ch = (unsigned char)*src++; /* Skip the = */
  314. + /* Fall through to "single trailing =" case. */
  315. + /* FALLTHROUGH */
  316. +
  317. + case 3: /* Valid, means two bytes of info */
  318. + /*
  319. + * We know this char is an =. Is there anything but
  320. + * whitespace after it?
  321. + */
  322. + for (; ch != '\0'; ch = (unsigned char)*src++)
  323. + if (!isspace(ch))
  324. + return (-1);
  325. +
  326. + /*
  327. + * Now make sure for cases 2 and 3 that the "extra"
  328. + * bits that slopped past the last full byte were
  329. + * zeros. If we don't check them, they become a
  330. + * subliminal channel.
  331. + */
  332. + if (target && tarindex < targsize &&
  333. + target[tarindex] != 0)
  334. + return (-1);
  335. + }
  336. + } else {
  337. + /*
  338. + * We ended by seeing the end of the string. Make sure we
  339. + * have no partial bytes lying around.
  340. + */
  341. + if (state != 0)
  342. + return (-1);
  343. + }
  344. +
  345. + return (tarindex);
  346. +}
  347. diff --git a/fdm.h b/fdm.h
  348. index 5de799c..abc52e0 100644
  349. --- a/fdm.h
  350. +++ b/fdm.h
  351. @@ -84,6 +84,18 @@ extern char *__progname;
  352. #define INFTIM -1
  353. #endif
  354. +#ifndef WAIT_ANY
  355. +#define WAIT_ANY -1
  356. +#endif
  357. +
  358. +#ifndef GLOB_BRACE
  359. +#define GLOB_BRACE 0
  360. +#endif
  361. +
  362. +#ifndef ACCESSPERMS
  363. +#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO)
  364. +#endif
  365. +
  366. #ifndef __dead
  367. #define __dead __attribute__ ((__noreturn__))
  368. #endif
  369. @@ -707,6 +719,12 @@ size_t strlcpy(char *, const char *, size_t);
  370. size_t strlcat(char *, const char *, size_t);
  371. #endif
  372. +#ifndef HAVE_B64_NTOP
  373. +/* base64.c */
  374. +int b64_ntop(src, srclength, target, targsize);
  375. +int b64_pton(src, target, targsize);
  376. +#endif
  377. +
  378. /* shm.c */
  379. char *shm_path(struct shm *);
  380. void *shm_create(struct shm *, size_t);
  381. --
  382. 2.17.1