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.

446 lines
14 KiB

  1. diff --git a/configure.ac b/configure.ac
  2. index 94f950c76..e3bda0599 100644
  3. --- a/configure.ac
  4. +++ b/configure.ac
  5. @@ -783,6 +783,7 @@ AC_CHECK_FUNCS(strdup \
  6. strcasestr \
  7. memrchr \
  8. localtime_r \
  9. + getprotobynumber_r \
  10. gmtime_r \
  11. strnlen \
  12. strtok_r)
  13. diff --git a/lib/compat/CMakeLists.txt b/lib/compat/CMakeLists.txt
  14. index 4fa05d7e0..95fcb0e0d 100644
  15. --- a/lib/compat/CMakeLists.txt
  16. +++ b/lib/compat/CMakeLists.txt
  17. @@ -10,7 +10,8 @@ set(COMPAT_HEADERS
  18. compat/openssl_support.h
  19. compat/pcre.h
  20. compat/getent.h
  21. - compat/getent-bb.h
  22. + compat/getent-sun.h
  23. + compat/getent-generic.h
  24. PARENT_SCOPE)
  25. set(COMPAT_SOURCES
  26. @@ -24,7 +25,8 @@ set(COMPAT_SOURCES
  27. compat/strnlen.c
  28. compat/time.c
  29. compat/openssl_support.c
  30. - compat/getent.c
  31. + compat/getent-sun.c
  32. + compat/getent-generic.c
  33. PARENT_SCOPE)
  34. add_test_subdirectory(tests)
  35. diff --git a/lib/compat/Makefile.am b/lib/compat/Makefile.am
  36. index e5c1f4e56..8d5010558 100644
  37. --- a/lib/compat/Makefile.am
  38. +++ b/lib/compat/Makefile.am
  39. @@ -13,9 +13,10 @@ compatinclude_HEADERS = \
  40. lib/compat/string.h \
  41. lib/compat/time.h \
  42. lib/compat/openssl_support.h \
  43. - lib/compat/pcre.h \
  44. - lib/compat/getent.h \
  45. - lib/compat/getent-bb.h
  46. + lib/compat/pcre.h \
  47. + lib/compat/getent.h \
  48. + lib/compat/getent-sun.h \
  49. + lib/compat/getent-generic.h
  50. compat_sources = \
  51. lib/compat/getutent.c \
  52. @@ -28,6 +29,7 @@ compat_sources = \
  53. lib/compat/strnlen.c \
  54. lib/compat/time.c \
  55. lib/compat/openssl_support.c \
  56. - lib/compat/getent.c
  57. + lib/compat/getent-sun.c \
  58. + lib/compat/getent-generic.c
  59. include lib/compat/tests/Makefile.am
  60. diff --git a/lib/compat/getent-generic.c b/lib/compat/getent-generic.c
  61. new file mode 100644
  62. index 000000000..f75d1cc0a
  63. --- /dev/null
  64. +++ b/lib/compat/getent-generic.c
  65. @@ -0,0 +1,150 @@
  66. +/*
  67. + * Copyright (c) 2017 Balabit
  68. + *
  69. + * This library is free software; you can redistribute it and/or
  70. + * modify it under the terms of the GNU Lesser General Public
  71. + * License as published by the Free Software Foundation; either
  72. + * version 2.1 of the License, or (at your option) any later version.
  73. + *
  74. + * This library is distributed in the hope that it will be useful,
  75. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  76. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  77. + * Lesser General Public License for more details.
  78. + *
  79. + * You should have received a copy of the GNU Lesser General Public
  80. + * License along with this library; if not, write to the Free Software
  81. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  82. + *
  83. + * As an additional exemption you are allowed to compile & link against the
  84. + * OpenSSL libraries as published by the OpenSSL project. See the file
  85. + * COPYING for details.
  86. + *
  87. + */
  88. +
  89. +#include "compat/getent-generic.h"
  90. +
  91. +#ifndef SYSLOG_NG_HAVE_GETPROTOBYNUMBER_R
  92. +
  93. +#include <glib.h>
  94. +#include <errno.h>
  95. +
  96. +G_LOCK_DEFINE_STATIC(getproto);
  97. +
  98. +/* this code does not support proto aliases, as we wouldn't be using
  99. + * them anyway. Should we ever want to support it, we would need to
  100. + * suballocate @buf and store all of the aliases in the same character
  101. + * array.
  102. + */
  103. +static void
  104. +_extract_protoent_fields(struct protoent *dst, struct protoent *src, char *buf, size_t buflen)
  105. +{
  106. + g_strlcpy(buf, src->p_name, buflen);
  107. + dst->p_name = buf;
  108. + dst->p_aliases = NULL;
  109. + dst->p_proto = src->p_proto;
  110. +}
  111. +
  112. +int
  113. +_compat_generic__getprotobynumber_r(int proto,
  114. + struct protoent *result_buf, char *buf,
  115. + size_t buflen, struct protoent **result)
  116. +{
  117. + struct protoent *pe;
  118. +
  119. + G_LOCK(getproto);
  120. + pe = getprotobynumber(proto);
  121. +
  122. + if (pe)
  123. + {
  124. + _extract_protoent_fields(result_buf, pe, buf, buflen);
  125. + *result = result_buf;
  126. + errno = 0;
  127. + }
  128. +
  129. + G_UNLOCK(getproto);
  130. + return errno;
  131. +}
  132. +
  133. +int
  134. +_compat_generic__getprotobyname_r(const char *name,
  135. + struct protoent *result_buf, char *buf,
  136. + size_t buflen, struct protoent **result)
  137. +{
  138. + struct protoent *pe;
  139. +
  140. + G_LOCK(getproto);
  141. + pe = getprotobyname(name);
  142. +
  143. + if (pe)
  144. + {
  145. + _extract_protoent_fields(result_buf, pe, buf, buflen);
  146. + *result = result_buf;
  147. + errno = 0;
  148. + }
  149. +
  150. + G_UNLOCK(getproto);
  151. + return errno;
  152. +}
  153. +
  154. +G_LOCK_DEFINE_STATIC(getserv);
  155. +
  156. +/* this code does not support service aliases or using the s_proto field, as
  157. + * we wouldn't be using them anyway. Should we ever want to support it, we
  158. + * would need to suballocate @buf and store all of the aliases in the same
  159. + * character array.
  160. + */
  161. +static void
  162. +_extract_servent_fields(struct servent *dst, struct servent *src, char *buf, size_t buflen)
  163. +{
  164. + g_strlcpy(buf, src->s_name, buflen);
  165. + dst->s_name = buf;
  166. + dst->s_aliases = NULL;
  167. + dst->s_port = src->s_port;
  168. + /* we don't support s_proto */
  169. + dst->s_proto = NULL;
  170. +}
  171. +
  172. +
  173. +int
  174. +_compat_generic__getservbyport_r(int port, const char *proto,
  175. + struct servent *result_buf, char *buf,
  176. + size_t buflen, struct servent **result)
  177. +{
  178. + struct servent *se;
  179. +
  180. + G_LOCK(getserv);
  181. + se = getservbyport(port, proto);
  182. +
  183. + if (se)
  184. + {
  185. + _extract_servent_fields(result_buf, se, buf, buflen);
  186. + *result = result_buf;
  187. + errno = 0;
  188. + }
  189. +
  190. + G_UNLOCK(getserv);
  191. + return errno;
  192. +}
  193. +
  194. +int
  195. +_compat_generic__getservbyname_r(const char *name, const char *proto,
  196. + struct servent *result_buf, char *buf,
  197. + size_t buflen, struct servent **result)
  198. +{
  199. + struct servent *se;
  200. +
  201. + G_LOCK(getserv);
  202. + se = getservbyname(name, proto);
  203. +
  204. + if (se)
  205. + {
  206. + _extract_servent_fields(result_buf, se, buf, buflen);
  207. + *result = result_buf;
  208. + errno = 0;
  209. + }
  210. +
  211. + G_UNLOCK(getserv);
  212. + return errno;
  213. +}
  214. +
  215. +#endif
  216. diff --git a/lib/compat/getent-generic.h b/lib/compat/getent-generic.h
  217. new file mode 100644
  218. index 000000000..cc95a2646
  219. --- /dev/null
  220. +++ b/lib/compat/getent-generic.h
  221. @@ -0,0 +1,54 @@
  222. +/*
  223. + * Copyright (c) 2017 Balabit
  224. + *
  225. + * This library is free software; you can redistribute it and/or
  226. + * modify it under the terms of the GNU Lesser General Public
  227. + * License as published by the Free Software Foundation; either
  228. + * version 2.1 of the License, or (at your option) any later version.
  229. + *
  230. + * This library is distributed in the hope that it will be useful,
  231. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  232. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  233. + * Lesser General Public License for more details.
  234. + *
  235. + * You should have received a copy of the GNU Lesser General Public
  236. + * License along with this library; if not, write to the Free Software
  237. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  238. + *
  239. + * As an additional exemption you are allowed to compile & link against the
  240. + * OpenSSL libraries as published by the OpenSSL project. See the file
  241. + * COPYING for details.
  242. + *
  243. + */
  244. +
  245. +#ifndef COMPAT_GETENT_GENERIC_H_INCLUDED
  246. +#define COMPAT_GETENT_GENERIC_H_INCLUDED
  247. +
  248. +#include "compat/compat.h"
  249. +
  250. +#ifndef SYSLOG_NG_HAVE_GETPROTOBYNUMBER_R
  251. +
  252. +#include <sys/types.h>
  253. +#include <grp.h>
  254. +#include <pwd.h>
  255. +#include <netdb.h>
  256. +
  257. +int _compat_generic__getprotobynumber_r(int proto,
  258. + struct protoent *result_buf, char *buf,
  259. + size_t buflen, struct protoent **result);
  260. +
  261. +int _compat_generic__getprotobyname_r(const char *name,
  262. + struct protoent *result_buf, char *buf,
  263. + size_t buflen, struct protoent **result);
  264. +
  265. +int _compat_generic__getservbyport_r(int port, const char *proto,
  266. + struct servent *result_buf, char *buf,
  267. + size_t buflen, struct servent **result);
  268. +
  269. +int _compat_generic__getservbyname_r(const char *name, const char *proto,
  270. + struct servent *result_buf, char *buf,
  271. + size_t buflen, struct servent **result);
  272. +
  273. +#endif
  274. +
  275. +#endif
  276. diff --git a/lib/compat/getent.c b/lib/compat/getent-sun.c
  277. similarity index 63%
  278. rename from lib/compat/getent.c
  279. rename to lib/compat/getent-sun.c
  280. index bb9b5b431..dce676f2f 100644
  281. --- a/lib/compat/getent.c
  282. +++ b/lib/compat/getent-sun.c
  283. @@ -21,40 +21,45 @@
  284. *
  285. */
  286. -#if defined(sun) || defined(__sun)
  287. +#include "compat/getent-sun.h"
  288. -#include "compat/getent-bb.h"
  289. +#if defined(sun) || defined(__sun)
  290. #include <errno.h>
  291. -int bb__getprotobynumber_r(int proto,
  292. - struct protoent *result_buf, char *buf,
  293. - size_t buflen, struct protoent **result)
  294. +int
  295. +_compat_sun__getprotobynumber_r(int proto,
  296. + struct protoent *result_buf, char *buf,
  297. + size_t buflen, struct protoent **result)
  298. {
  299. *result = getprotobynumber_r(proto, result_buf, buf, buflen);
  300. return (*result ? NULL : errno);
  301. }
  302. -int bb__getprotobyname_r(const char *name,
  303. - struct protoent *result_buf, char *buf,
  304. - size_t buflen, struct protoent **result)
  305. +int
  306. +_compat_sun__getprotobyname_r(const char *name,
  307. + struct protoent *result_buf, char *buf,
  308. + size_t buflen, struct protoent **result)
  309. {
  310. *result = getprotobyname_r(name, result_buf, buf, buflen);
  311. return (*result ? NULL : errno);
  312. }
  313. -int bb__getservbyport_r(int port, const char *proto,
  314. - struct servent *result_buf, char *buf,
  315. - size_t buflen, struct servent **result)
  316. +int
  317. +_compat_sun__getservbyport_r(int port, const char *proto,
  318. + struct servent *result_buf, char *buf,
  319. + size_t buflen, struct servent **result)
  320. {
  321. *result = getservbyport_r(port, proto, result_buf, buf, buflen);
  322. return (*result ? NULL : errno);
  323. }
  324. -int bb__getservbyname_r(const char *name, const char *proto,
  325. - struct servent *result_buf, char *buf,
  326. - size_t buflen, struct servent **result)
  327. +int
  328. +_compat_sun__getservbyname_r(const char *name, const char *proto,
  329. + struct servent *result_buf, char *buf,
  330. + size_t buflen, struct servent **result)
  331. {
  332. *result = getservbyname_r(name, proto, result_buf, buf, buflen);
  333. return (*result ? NULL : errno);
  334. }
  335. +
  336. #endif
  337. diff --git a/lib/compat/getent-bb.h b/lib/compat/getent-sun.h
  338. similarity index 53%
  339. rename from lib/compat/getent-bb.h
  340. rename to lib/compat/getent-sun.h
  341. index 15aa2f5e5..fc1eccd2c 100644
  342. --- a/lib/compat/getent-bb.h
  343. +++ b/lib/compat/getent-sun.h
  344. @@ -21,8 +21,10 @@
  345. *
  346. */
  347. -#ifndef GETENT_BB_H_INCLUDED
  348. -#define GETENT_BB_H_INCLUDED
  349. +#ifndef COMPAT_GETENT_SUN_H_INCLUDED
  350. +#define COMPAT_GETENT_SUN_H_INCLUDED
  351. +
  352. +#include "compat/compat.h"
  353. #if defined(sun) || defined(__sun)
  354. @@ -31,21 +33,21 @@
  355. #include <pwd.h>
  356. #include <netdb.h>
  357. -int bb__getprotobynumber_r(int proto,
  358. - struct protoent *result_buf, char *buf,
  359. - size_t buflen, struct protoent **result);
  360. +int _compat_sun__getprotobynumber_r(int proto,
  361. + struct protoent *result_buf, char *buf,
  362. + size_t buflen, struct protoent **result);
  363. -int bb__getprotobyname_r(const char *name,
  364. - struct protoent *result_buf, char *buf,
  365. - size_t buflen, struct protoent **result);
  366. +int _compat_sun__getprotobyname_r(const char *name,
  367. + struct protoent *result_buf, char *buf,
  368. + size_t buflen, struct protoent **result);
  369. -int bb__getservbyport_r(int port, const char *proto,
  370. - struct servent *result_buf, char *buf,
  371. - size_t buflen, struct servent **result);
  372. +int _compat_sun__getservbyport_r(int port, const char *proto,
  373. + struct servent *result_buf, char *buf,
  374. + size_t buflen, struct servent **result);
  375. -int bb__getservbyname_r(const char *name, const char *proto,
  376. - struct servent *result_buf, char *buf,
  377. - size_t buflen, struct servent **result);
  378. +int _compat_sun__getservbyname_r(const char *name, const char *proto,
  379. + struct servent *result_buf, char *buf,
  380. + size_t buflen, struct servent **result);
  381. #endif
  382. diff --git a/lib/compat/getent.h b/lib/compat/getent.h
  383. index 09a9f73d6..01c3deb6d 100644
  384. --- a/lib/compat/getent.h
  385. +++ b/lib/compat/getent.h
  386. @@ -21,22 +21,28 @@
  387. *
  388. */
  389. -#ifndef GETENT_COMPAT_H_INCLUDED
  390. -#define GETENT_COMPAT_H_INCLUDED
  391. +#ifndef COMPAT_GETENT_H_INCLUDED
  392. +#define COMPAT_GETENT_H_INCLUDED
  393. -#include <sys/types.h>
  394. -#include <grp.h>
  395. -#include <pwd.h>
  396. -#include <netdb.h>
  397. +#include "compat/compat.h"
  398. -#if defined(sun) || defined(__sun)
  399. +#ifndef SYSLOG_NG_HAVE_GETPROTOBYNUMBER_R
  400. -#define getprotobynumber_r bb__getprotobynumber_r
  401. -#define getprotobyname_r bb__getprotobyname_r
  402. -#define getservbyport_r bb__getservbyport_r
  403. -#define getservbyname_r bb__getservbyname_r
  404. +#define getprotobynumber_r _compat_generic__getprotobynumber_r
  405. +#define getprotobyname_r _compat_generic__getprotobyname_r
  406. +#define getservbyport_r _compat_generic__getservbyport_r
  407. +#define getservbyname_r _compat_generic__getservbyname_r
  408. -#include "getent-bb.h"
  409. +#include "getent-generic.h"
  410. -#endif // Solaris
  411. +#elif defined(sun) || defined(__sun)
  412. +
  413. +#define getprotobynumber_r _compat_sun__getprotobynumber_r
  414. +#define getprotobyname_r _compat_sun__getprotobyname_r
  415. +#define getservbyport_r _compat_sun__getservbyport_r
  416. +#define getservbyname_r _compat_sun__getservbyname_r
  417. +
  418. +#include "getent-sun.h"
  419. +
  420. +#endif
  421. #endif