tcp_service_legacy.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/service.c
  18. * @brief functions related to starting services
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_protocols.h"
  24. #include "gnunet_constants.h"
  25. #include "gnunet_resolver_service.h"
  26. #if HAVE_MALLINFO
  27. #include <malloc.h>
  28. #include "gauger.h"
  29. #endif
  30. /* ******************* access control ******************** */
  31. /**
  32. * Check if the given IP address is in the list of IP addresses.
  33. *
  34. * @param list a list of networks
  35. * @param add the IP to check (in network byte order)
  36. * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
  37. */
  38. static int
  39. check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list,
  40. const struct in_addr *add)
  41. {
  42. unsigned int i;
  43. if (NULL == list)
  44. return GNUNET_NO;
  45. i = 0;
  46. while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
  47. {
  48. if ((add->s_addr & list[i].netmask.s_addr) ==
  49. (list[i].network.s_addr & list[i].netmask.s_addr))
  50. return GNUNET_YES;
  51. i++;
  52. }
  53. return GNUNET_NO;
  54. }
  55. /**
  56. * Check if the given IP address is in the list of IP addresses.
  57. *
  58. * @param list a list of networks
  59. * @param ip the IP to check (in network byte order)
  60. * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
  61. */
  62. static int
  63. check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
  64. const struct in6_addr *ip)
  65. {
  66. unsigned int i;
  67. unsigned int j;
  68. struct in6_addr zero;
  69. if (NULL == list)
  70. return GNUNET_NO;
  71. memset (&zero, 0, sizeof(struct in6_addr));
  72. i = 0;
  73. NEXT:
  74. while (0 != memcmp (&zero, &list[i].network, sizeof(struct in6_addr)))
  75. {
  76. for (j = 0; j < sizeof(struct in6_addr) / sizeof(int); j++)
  77. if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
  78. (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
  79. {
  80. i++;
  81. goto NEXT;
  82. }
  83. return GNUNET_YES;
  84. }
  85. return GNUNET_NO;
  86. }
  87. /* ****************** service struct ****************** */
  88. /**
  89. * Context for "service_task".
  90. */
  91. struct LEGACY_SERVICE_Context
  92. {
  93. /**
  94. * Our configuration.
  95. */
  96. const struct GNUNET_CONFIGURATION_Handle *cfg;
  97. /**
  98. * Handle for the server.
  99. */
  100. struct GNUNET_SERVER_Handle *server;
  101. /**
  102. * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
  103. * listen sockets.
  104. */
  105. struct sockaddr **addrs;
  106. /**
  107. * Name of our service.
  108. */
  109. const char *service_name;
  110. /**
  111. * Main service-specific task to run.
  112. */
  113. LEGACY_SERVICE_Main task;
  114. /**
  115. * Closure for @e task.
  116. */
  117. void *task_cls;
  118. /**
  119. * IPv4 addresses that are not allowed to connect.
  120. */
  121. struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_denied;
  122. /**
  123. * IPv6 addresses that are not allowed to connect.
  124. */
  125. struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_denied;
  126. /**
  127. * IPv4 addresses that are allowed to connect (if not
  128. * set, all are allowed).
  129. */
  130. struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_allowed;
  131. /**
  132. * IPv6 addresses that are allowed to connect (if not
  133. * set, all are allowed).
  134. */
  135. struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_allowed;
  136. /**
  137. * My (default) message handlers. Adjusted copy
  138. * of "defhandlers".
  139. */
  140. struct GNUNET_SERVER_MessageHandler *my_handlers;
  141. /**
  142. * Array of the lengths of the entries in addrs.
  143. */
  144. socklen_t *addrlens;
  145. /**
  146. * NULL-terminated array of listen sockets we should take over.
  147. */
  148. struct GNUNET_NETWORK_Handle **lsocks;
  149. /**
  150. * Task ID of the shutdown task.
  151. */
  152. struct GNUNET_SCHEDULER_Task *shutdown_task;
  153. /**
  154. * Idle timeout for server.
  155. */
  156. struct GNUNET_TIME_Relative timeout;
  157. /**
  158. * Overall success/failure of the service start.
  159. */
  160. int ret;
  161. /**
  162. * If we are daemonizing, this FD is set to the
  163. * pipe to the parent. Send '.' if we started
  164. * ok, '!' if not. -1 if we are not daemonizing.
  165. */
  166. int ready_confirm_fd;
  167. /**
  168. * Do we close connections if we receive messages
  169. * for which we have no handler?
  170. */
  171. int require_found;
  172. /**
  173. * Do we require a matching UID for UNIX domain socket connections?
  174. * #GNUNET_NO means that the UID does not have to match (however,
  175. * @e match_gid may still impose other access control checks).
  176. */
  177. int match_uid;
  178. /**
  179. * Do we require a matching GID for UNIX domain socket connections?
  180. * Ignored if @e match_uid is #GNUNET_YES. Note that this is about
  181. * checking that the client's UID is in our group OR that the
  182. * client's GID is our GID. If both "match_gid" and @e match_uid are
  183. * #GNUNET_NO, all users on the local system have access.
  184. */
  185. int match_gid;
  186. /**
  187. * Our options.
  188. */
  189. enum LEGACY_SERVICE_Options options;
  190. };
  191. /* ****************** message handlers ****************** */
  192. /**
  193. * Send a 'TEST' message back to the client.
  194. *
  195. * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
  196. * @param size number of bytes available in 'buf'
  197. * @param buf where to copy the message
  198. * @return number of bytes written to 'buf'
  199. */
  200. static size_t
  201. write_test (void *cls, size_t size, void *buf)
  202. {
  203. struct GNUNET_SERVER_Client *client = cls;
  204. struct GNUNET_MessageHeader *msg;
  205. if (size < sizeof(struct GNUNET_MessageHeader))
  206. {
  207. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  208. return 0; /* client disconnected */
  209. }
  210. msg = (struct GNUNET_MessageHeader *) buf;
  211. msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
  212. msg->size = htons (sizeof(struct GNUNET_MessageHeader));
  213. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  214. return sizeof(struct GNUNET_MessageHeader);
  215. }
  216. /**
  217. * Handler for TEST message.
  218. *
  219. * @param cls closure (refers to service)
  220. * @param client identification of the client
  221. * @param message the actual message
  222. */
  223. static void
  224. handle_test (void *cls,
  225. struct GNUNET_SERVER_Client *client,
  226. const struct GNUNET_MessageHeader *message)
  227. {
  228. /* simply bounce message back to acknowledge */
  229. if (NULL ==
  230. GNUNET_SERVER_notify_transmit_ready (client,
  231. sizeof(struct GNUNET_MessageHeader),
  232. GNUNET_TIME_UNIT_FOREVER_REL,
  233. &write_test,
  234. client))
  235. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  236. }
  237. /**
  238. * Default handlers for all services. Will be copied and the
  239. * "callback_cls" fields will be replaced with the specific service
  240. * struct.
  241. */
  242. static const struct GNUNET_SERVER_MessageHandler defhandlers[] =
  243. { { &handle_test,
  244. NULL,
  245. GNUNET_MESSAGE_TYPE_TEST,
  246. sizeof(struct GNUNET_MessageHeader) },
  247. { NULL, NULL, 0, 0 } };
  248. /* ****************** service core routines ************** */
  249. /**
  250. * Check if access to the service is allowed from the given address.
  251. *
  252. * @param cls closure
  253. * @param uc credentials, if available, otherwise NULL
  254. * @param addr address
  255. * @param addrlen length of address
  256. * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
  257. * for unknown address family (will be denied).
  258. */
  259. static int
  260. check_access (void *cls,
  261. const struct GNUNET_CONNECTION_Credentials *uc,
  262. const struct sockaddr *addr,
  263. socklen_t addrlen)
  264. {
  265. struct LEGACY_SERVICE_Context *sctx = cls;
  266. const struct sockaddr_in *i4;
  267. const struct sockaddr_in6 *i6;
  268. int ret;
  269. switch (addr->sa_family)
  270. {
  271. case AF_INET:
  272. GNUNET_assert (addrlen == sizeof(struct sockaddr_in));
  273. i4 = (const struct sockaddr_in *) addr;
  274. ret = ((NULL == sctx->v4_allowed) ||
  275. (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
  276. ((NULL == sctx->v4_denied) ||
  277. (! check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
  278. break;
  279. case AF_INET6:
  280. GNUNET_assert (addrlen == sizeof(struct sockaddr_in6));
  281. i6 = (const struct sockaddr_in6 *) addr;
  282. ret = ((NULL == sctx->v6_allowed) ||
  283. (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
  284. ((NULL == sctx->v6_denied) ||
  285. (! check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
  286. break;
  287. case AF_UNIX:
  288. ret = GNUNET_OK; /* controlled using file-system ACL now */
  289. break;
  290. default:
  291. LOG (GNUNET_ERROR_TYPE_WARNING,
  292. _ ("Unknown address family %d\n"),
  293. addr->sa_family);
  294. return GNUNET_SYSERR;
  295. }
  296. if (GNUNET_OK != ret)
  297. {
  298. LOG (GNUNET_ERROR_TYPE_WARNING,
  299. _ ("Access from `%s' denied to service `%s'\n"),
  300. GNUNET_a2s (addr, addrlen),
  301. sctx->service_name);
  302. }
  303. return ret;
  304. }
  305. /**
  306. * Get the name of the file where we will
  307. * write the PID of the service.
  308. *
  309. * @param sctx service context
  310. * @return name of the file for the process ID
  311. */
  312. static char *
  313. get_pid_file_name (struct LEGACY_SERVICE_Context *sctx)
  314. {
  315. char *pif;
  316. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
  317. sctx->service_name,
  318. "PIDFILE",
  319. &pif))
  320. return NULL;
  321. return pif;
  322. }
  323. /**
  324. * Parse an IPv4 access control list.
  325. *
  326. * @param ret location where to write the ACL (set)
  327. * @param sctx service context to use to get the configuration
  328. * @param option name of the ACL option to parse
  329. * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
  330. * no ACL configured)
  331. */
  332. static int
  333. process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret,
  334. struct LEGACY_SERVICE_Context *sctx,
  335. const char *option)
  336. {
  337. char *opt;
  338. if (! GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
  339. {
  340. *ret = NULL;
  341. return GNUNET_OK;
  342. }
  343. GNUNET_break (GNUNET_OK ==
  344. GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
  345. sctx->service_name,
  346. option,
  347. &opt));
  348. if (NULL == (*ret = GNUNET_STRINGS_parse_ipv4_policy (opt)))
  349. {
  350. LOG (GNUNET_ERROR_TYPE_WARNING,
  351. _ ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
  352. opt,
  353. sctx->service_name,
  354. option);
  355. GNUNET_free (opt);
  356. return GNUNET_SYSERR;
  357. }
  358. GNUNET_free (opt);
  359. return GNUNET_OK;
  360. }
  361. /**
  362. * Parse an IPv6 access control list.
  363. *
  364. * @param ret location where to write the ACL (set)
  365. * @param sctx service context to use to get the configuration
  366. * @param option name of the ACL option to parse
  367. * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
  368. * no ACL configured)
  369. */
  370. static int
  371. process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret,
  372. struct LEGACY_SERVICE_Context *sctx,
  373. const char *option)
  374. {
  375. char *opt;
  376. if (! GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
  377. {
  378. *ret = NULL;
  379. return GNUNET_OK;
  380. }
  381. GNUNET_break (GNUNET_OK ==
  382. GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
  383. sctx->service_name,
  384. option,
  385. &opt));
  386. if (NULL == (*ret = GNUNET_STRINGS_parse_ipv6_policy (opt)))
  387. {
  388. LOG (GNUNET_ERROR_TYPE_WARNING,
  389. _ ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
  390. opt,
  391. sctx->service_name,
  392. option);
  393. GNUNET_free (opt);
  394. return GNUNET_SYSERR;
  395. }
  396. GNUNET_free (opt);
  397. return GNUNET_OK;
  398. }
  399. /**
  400. * Add the given UNIX domain path as an address to the
  401. * list (as the first entry).
  402. *
  403. * @param saddrs array to update
  404. * @param saddrlens where to store the address length
  405. * @param unixpath path to add
  406. * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
  407. * parameter is ignore on systems other than LINUX
  408. */
  409. static void
  410. add_unixpath (struct sockaddr **saddrs,
  411. socklen_t *saddrlens,
  412. const char *unixpath,
  413. int abstract)
  414. {
  415. #ifdef AF_UNIX
  416. struct sockaddr_un *un;
  417. un = GNUNET_new (struct sockaddr_un);
  418. un->sun_family = AF_UNIX;
  419. GNUNET_strlcpy (un->sun_path, unixpath, sizeof(un->sun_path));
  420. #ifdef __linux__
  421. if (GNUNET_YES == abstract)
  422. un->sun_path[0] = '\0';
  423. #endif
  424. #if HAVE_SOCKADDR_UN_SUN_LEN
  425. un->sun_len = (u_char) sizeof(struct sockaddr_un);
  426. #endif
  427. *saddrs = (struct sockaddr *) un;
  428. *saddrlens = sizeof(struct sockaddr_un);
  429. #else
  430. /* this function should never be called
  431. * unless AF_UNIX is defined! */
  432. GNUNET_assert (0);
  433. #endif
  434. }
  435. /**
  436. * Get the list of addresses that a server for the given service
  437. * should bind to.
  438. *
  439. * @param service_name name of the service
  440. * @param cfg configuration (which specifies the addresses)
  441. * @param addrs set (call by reference) to an array of pointers to the
  442. * addresses the server should bind to and listen on; the
  443. * array will be NULL-terminated (on success)
  444. * @param addr_lens set (call by reference) to an array of the lengths
  445. * of the respective `struct sockaddr` struct in the @a addrs
  446. * array (on success)
  447. * @return number of addresses found on success,
  448. * #GNUNET_SYSERR if the configuration
  449. * did not specify reasonable finding information or
  450. * if it specified a hostname that could not be resolved;
  451. * #GNUNET_NO if the number of addresses configured is
  452. * zero (in this case, `*addrs` and `*addr_lens` will be
  453. * set to NULL).
  454. */
  455. int
  456. LEGACY_SERVICE_get_server_addresses (
  457. const char *service_name,
  458. const struct GNUNET_CONFIGURATION_Handle *cfg,
  459. struct sockaddr ***addrs,
  460. socklen_t **addr_lens)
  461. {
  462. int disablev6;
  463. struct GNUNET_NETWORK_Handle *desc;
  464. unsigned long long port;
  465. char *unixpath;
  466. struct addrinfo hints;
  467. struct addrinfo *res;
  468. struct addrinfo *pos;
  469. struct addrinfo *next;
  470. unsigned int i;
  471. int resi;
  472. int ret;
  473. int abstract;
  474. struct sockaddr **saddrs;
  475. socklen_t *saddrlens;
  476. char *hostname;
  477. *addrs = NULL;
  478. *addr_lens = NULL;
  479. desc = NULL;
  480. if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
  481. {
  482. if (GNUNET_SYSERR ==
  483. (disablev6 = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  484. service_name,
  485. "DISABLEV6")))
  486. return GNUNET_SYSERR;
  487. }
  488. else
  489. disablev6 = GNUNET_NO;
  490. if (! disablev6)
  491. {
  492. /* probe IPv6 support */
  493. desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
  494. if (NULL == desc)
  495. {
  496. if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
  497. (EACCES == errno))
  498. {
  499. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
  500. return GNUNET_SYSERR;
  501. }
  502. LOG (GNUNET_ERROR_TYPE_INFO,
  503. _ (
  504. "Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
  505. service_name,
  506. strerror (errno));
  507. disablev6 = GNUNET_YES;
  508. }
  509. else
  510. {
  511. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
  512. desc = NULL;
  513. }
  514. }
  515. port = 0;
  516. if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
  517. {
  518. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
  519. service_name,
  520. "PORT",
  521. &port))
  522. {
  523. LOG (GNUNET_ERROR_TYPE_ERROR,
  524. _ ("Require valid port number for service `%s' in configuration!\n"),
  525. service_name);
  526. }
  527. if (port > 65535)
  528. {
  529. LOG (GNUNET_ERROR_TYPE_ERROR,
  530. _ ("Require valid port number for service `%s' in configuration!\n"),
  531. service_name);
  532. return GNUNET_SYSERR;
  533. }
  534. }
  535. if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
  536. {
  537. GNUNET_break (GNUNET_OK ==
  538. GNUNET_CONFIGURATION_get_value_string (cfg,
  539. service_name,
  540. "BINDTO",
  541. &hostname));
  542. }
  543. else
  544. hostname = NULL;
  545. unixpath = NULL;
  546. abstract = GNUNET_NO;
  547. #ifdef AF_UNIX
  548. if ((GNUNET_YES ==
  549. GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
  550. (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
  551. service_name,
  552. "UNIXPATH",
  553. &unixpath)) &&
  554. (0 < strlen (unixpath)))
  555. {
  556. /* probe UNIX support */
  557. struct sockaddr_un s_un;
  558. if (strlen (unixpath) >= sizeof(s_un.sun_path))
  559. {
  560. LOG (GNUNET_ERROR_TYPE_WARNING,
  561. _ ("UNIXPATH `%s' too long, maximum length is %llu\n"),
  562. unixpath,
  563. (unsigned long long) sizeof(s_un.sun_path));
  564. unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
  565. LOG (GNUNET_ERROR_TYPE_INFO, _ ("Using `%s' instead\n"), unixpath);
  566. }
  567. #ifdef __linux__
  568. abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  569. "TESTING",
  570. "USE_ABSTRACT_SOCKETS");
  571. if (GNUNET_SYSERR == abstract)
  572. abstract = GNUNET_NO;
  573. #endif
  574. if ((GNUNET_YES != abstract) &&
  575. (GNUNET_OK != GNUNET_DISK_directory_create_for_file (unixpath)))
  576. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir", unixpath);
  577. }
  578. if (NULL != unixpath)
  579. {
  580. desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
  581. if (NULL == desc)
  582. {
  583. if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
  584. (EACCES == errno))
  585. {
  586. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
  587. GNUNET_free_non_null (hostname);
  588. GNUNET_free (unixpath);
  589. return GNUNET_SYSERR;
  590. }
  591. LOG (GNUNET_ERROR_TYPE_INFO,
  592. _ (
  593. "Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
  594. service_name,
  595. strerror (errno));
  596. GNUNET_free (unixpath);
  597. unixpath = NULL;
  598. }
  599. else
  600. {
  601. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
  602. desc = NULL;
  603. }
  604. }
  605. #endif
  606. if ((0 == port) && (NULL == unixpath))
  607. {
  608. LOG (GNUNET_ERROR_TYPE_ERROR,
  609. _ (
  610. "Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
  611. service_name);
  612. GNUNET_free_non_null (hostname);
  613. return GNUNET_SYSERR;
  614. }
  615. if (0 == port)
  616. {
  617. saddrs = GNUNET_malloc (2 * sizeof(struct sockaddr *));
  618. saddrlens = GNUNET_malloc (2 * sizeof(socklen_t));
  619. add_unixpath (saddrs, saddrlens, unixpath, abstract);
  620. GNUNET_free_non_null (unixpath);
  621. GNUNET_free_non_null (hostname);
  622. *addrs = saddrs;
  623. *addr_lens = saddrlens;
  624. return 1;
  625. }
  626. if (NULL != hostname)
  627. {
  628. LOG (GNUNET_ERROR_TYPE_DEBUG,
  629. "Resolving `%s' since that is where `%s' will bind to.\n",
  630. hostname,
  631. service_name);
  632. memset (&hints, 0, sizeof(struct addrinfo));
  633. if (disablev6)
  634. hints.ai_family = AF_INET;
  635. hints.ai_protocol = IPPROTO_TCP;
  636. if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
  637. (NULL == res))
  638. {
  639. LOG (GNUNET_ERROR_TYPE_ERROR,
  640. _ ("Failed to resolve `%s': %s\n"),
  641. hostname,
  642. gai_strerror (ret));
  643. GNUNET_free (hostname);
  644. GNUNET_free_non_null (unixpath);
  645. return GNUNET_SYSERR;
  646. }
  647. next = res;
  648. i = 0;
  649. while (NULL != (pos = next))
  650. {
  651. next = pos->ai_next;
  652. if ((disablev6) && (pos->ai_family == AF_INET6))
  653. continue;
  654. i++;
  655. }
  656. if (0 == i)
  657. {
  658. LOG (GNUNET_ERROR_TYPE_ERROR,
  659. _ ("Failed to find %saddress for `%s'.\n"),
  660. disablev6 ? "IPv4 " : "",
  661. hostname);
  662. freeaddrinfo (res);
  663. GNUNET_free (hostname);
  664. GNUNET_free_non_null (unixpath);
  665. return GNUNET_SYSERR;
  666. }
  667. resi = i;
  668. if (NULL != unixpath)
  669. resi++;
  670. saddrs = GNUNET_malloc ((resi + 1) * sizeof(struct sockaddr *));
  671. saddrlens = GNUNET_malloc ((resi + 1) * sizeof(socklen_t));
  672. i = 0;
  673. if (NULL != unixpath)
  674. {
  675. add_unixpath (saddrs, saddrlens, unixpath, abstract);
  676. i++;
  677. }
  678. next = res;
  679. while (NULL != (pos = next))
  680. {
  681. next = pos->ai_next;
  682. if ((disablev6) && (AF_INET6 == pos->ai_family))
  683. continue;
  684. if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
  685. continue; /* not TCP */
  686. if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
  687. continue; /* huh? */
  688. LOG (GNUNET_ERROR_TYPE_DEBUG,
  689. "Service `%s' will bind to `%s'\n",
  690. service_name,
  691. GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
  692. if (AF_INET == pos->ai_family)
  693. {
  694. GNUNET_assert (sizeof(struct sockaddr_in) == pos->ai_addrlen);
  695. saddrlens[i] = pos->ai_addrlen;
  696. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  697. GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
  698. ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
  699. }
  700. else
  701. {
  702. GNUNET_assert (AF_INET6 == pos->ai_family);
  703. GNUNET_assert (sizeof(struct sockaddr_in6) == pos->ai_addrlen);
  704. saddrlens[i] = pos->ai_addrlen;
  705. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  706. GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
  707. ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
  708. }
  709. i++;
  710. }
  711. GNUNET_free (hostname);
  712. freeaddrinfo (res);
  713. resi = i;
  714. }
  715. else
  716. {
  717. /* will bind against everything, just set port */
  718. if (disablev6)
  719. {
  720. /* V4-only */
  721. resi = 1;
  722. if (NULL != unixpath)
  723. resi++;
  724. i = 0;
  725. saddrs = GNUNET_malloc ((resi + 1) * sizeof(struct sockaddr *));
  726. saddrlens = GNUNET_malloc ((resi + 1) * sizeof(socklen_t));
  727. if (NULL != unixpath)
  728. {
  729. add_unixpath (saddrs, saddrlens, unixpath, abstract);
  730. i++;
  731. }
  732. saddrlens[i] = sizeof(struct sockaddr_in);
  733. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  734. #if HAVE_SOCKADDR_IN_SIN_LEN
  735. ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
  736. #endif
  737. ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
  738. ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
  739. }
  740. else
  741. {
  742. /* dual stack */
  743. resi = 2;
  744. if (NULL != unixpath)
  745. resi++;
  746. saddrs = GNUNET_malloc ((resi + 1) * sizeof(struct sockaddr *));
  747. saddrlens = GNUNET_malloc ((resi + 1) * sizeof(socklen_t));
  748. i = 0;
  749. if (NULL != unixpath)
  750. {
  751. add_unixpath (saddrs, saddrlens, unixpath, abstract);
  752. i++;
  753. }
  754. saddrlens[i] = sizeof(struct sockaddr_in6);
  755. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  756. #if HAVE_SOCKADDR_IN_SIN_LEN
  757. ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
  758. #endif
  759. ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
  760. ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
  761. i++;
  762. saddrlens[i] = sizeof(struct sockaddr_in);
  763. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  764. #if HAVE_SOCKADDR_IN_SIN_LEN
  765. ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
  766. #endif
  767. ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
  768. ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
  769. }
  770. }
  771. GNUNET_free_non_null (unixpath);
  772. *addrs = saddrs;
  773. *addr_lens = saddrlens;
  774. return resi;
  775. }
  776. /**
  777. * Setup addr, addrlen, idle_timeout
  778. * based on configuration!
  779. *
  780. * Configuration may specify:
  781. * - PORT (where to bind to for TCP)
  782. * - UNIXPATH (where to bind to for UNIX domain sockets)
  783. * - TIMEOUT (after how many ms does an inactive service timeout);
  784. * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
  785. * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
  786. * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
  787. * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
  788. * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
  789. * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
  790. *
  791. * @param sctx service context to initialize
  792. * @return #GNUNET_OK if configuration succeeded
  793. */
  794. static int
  795. setup_service (struct LEGACY_SERVICE_Context *sctx)
  796. {
  797. struct GNUNET_TIME_Relative idleout;
  798. int tolerant;
  799. const char *nfds;
  800. unsigned int cnt;
  801. int flags;
  802. if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
  803. sctx->service_name,
  804. "TIMEOUT"))
  805. {
  806. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (sctx->cfg,
  807. sctx->service_name,
  808. "TIMEOUT",
  809. &idleout))
  810. {
  811. LOG (GNUNET_ERROR_TYPE_ERROR,
  812. _ ("Specified value for `%s' of service `%s' is invalid\n"),
  813. "TIMEOUT",
  814. sctx->service_name);
  815. return GNUNET_SYSERR;
  816. }
  817. sctx->timeout = idleout;
  818. }
  819. else
  820. sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
  821. if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
  822. sctx->service_name,
  823. "TOLERANT"))
  824. {
  825. if (GNUNET_SYSERR ==
  826. (tolerant = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
  827. sctx->service_name,
  828. "TOLERANT")))
  829. {
  830. LOG (GNUNET_ERROR_TYPE_ERROR,
  831. _ ("Specified value for `%s' of service `%s' is invalid\n"),
  832. "TOLERANT",
  833. sctx->service_name);
  834. return GNUNET_SYSERR;
  835. }
  836. }
  837. else
  838. tolerant = GNUNET_NO;
  839. errno = 0;
  840. if ((NULL != (nfds = getenv ("LISTEN_FDS"))) &&
  841. (1 == sscanf (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
  842. (cnt + 4 < FD_SETSIZE))
  843. {
  844. sctx->lsocks =
  845. GNUNET_malloc (sizeof(struct GNUNET_NETWORK_Handle *) * (cnt + 1));
  846. while (0 < cnt--)
  847. {
  848. flags = fcntl (3 + cnt, F_GETFD);
  849. if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
  850. (NULL ==
  851. (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
  852. {
  853. LOG (GNUNET_ERROR_TYPE_ERROR,
  854. _ (
  855. "Could not access pre-bound socket %u, will try to bind myself\n"),
  856. (unsigned int) 3 + cnt);
  857. cnt++;
  858. while (sctx->lsocks[cnt] != NULL)
  859. GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
  860. GNUNET_free (sctx->lsocks);
  861. sctx->lsocks = NULL;
  862. break;
  863. }
  864. }
  865. unsetenv ("LISTEN_FDS");
  866. }
  867. if ((NULL == sctx->lsocks) &&
  868. (GNUNET_SYSERR == LEGACY_SERVICE_get_server_addresses (sctx->service_name,
  869. sctx->cfg,
  870. &sctx->addrs,
  871. &sctx->addrlens)))
  872. return GNUNET_SYSERR;
  873. sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
  874. sctx->match_uid = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
  875. sctx->service_name,
  876. "UNIX_MATCH_UID");
  877. sctx->match_gid = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
  878. sctx->service_name,
  879. "UNIX_MATCH_GID");
  880. process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
  881. process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
  882. process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
  883. process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
  884. return GNUNET_OK;
  885. }
  886. /**
  887. * Get the name of the user that'll be used
  888. * to provide the service.
  889. *
  890. * @param sctx service context
  891. * @return value of the 'USERNAME' option
  892. */
  893. static char *
  894. get_user_name (struct LEGACY_SERVICE_Context *sctx)
  895. {
  896. char *un;
  897. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
  898. sctx->service_name,
  899. "USERNAME",
  900. &un))
  901. return NULL;
  902. return un;
  903. }
  904. /**
  905. * Write PID file.
  906. *
  907. * @param sctx service context
  908. * @param pid PID to write (should be equal to 'getpid()'
  909. * @return #GNUNET_OK on success (including no work to be done)
  910. */
  911. static int
  912. write_pid_file (struct LEGACY_SERVICE_Context *sctx, pid_t pid)
  913. {
  914. FILE *pidfd;
  915. char *pif;
  916. char *user;
  917. char *rdir;
  918. int len;
  919. if (NULL == (pif = get_pid_file_name (sctx)))
  920. return GNUNET_OK; /* no file desired */
  921. user = get_user_name (sctx);
  922. rdir = GNUNET_strdup (pif);
  923. len = strlen (rdir);
  924. while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
  925. len--;
  926. rdir[len] = '\0';
  927. if (0 != access (rdir, F_OK))
  928. {
  929. /* we get to create a directory -- and claim it
  930. * as ours! */
  931. (void) GNUNET_DISK_directory_create (rdir);
  932. if ((NULL != user) && (0 < strlen (user)))
  933. GNUNET_DISK_file_change_owner (rdir, user);
  934. }
  935. if (0 != access (rdir, W_OK | X_OK))
  936. {
  937. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
  938. GNUNET_free (rdir);
  939. GNUNET_free_non_null (user);
  940. GNUNET_free (pif);
  941. return GNUNET_SYSERR;
  942. }
  943. GNUNET_free (rdir);
  944. pidfd = fopen (pif, "w");
  945. if (NULL == pidfd)
  946. {
  947. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
  948. GNUNET_free (pif);
  949. GNUNET_free_non_null (user);
  950. return GNUNET_SYSERR;
  951. }
  952. if (0 > fprintf (pidfd, "%u", pid))
  953. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
  954. GNUNET_break (0 == fclose (pidfd));
  955. if ((NULL != user) && (0 < strlen (user)))
  956. GNUNET_DISK_file_change_owner (pif, user);
  957. GNUNET_free_non_null (user);
  958. GNUNET_free (pif);
  959. return GNUNET_OK;
  960. }
  961. /**
  962. * Task run during shutdown. Stops the server/service.
  963. *
  964. * @param cls the `struct LEGACY_SERVICE_Context`
  965. */
  966. static void
  967. shutdown_task (void *cls)
  968. {
  969. struct LEGACY_SERVICE_Context *service = cls;
  970. struct GNUNET_SERVER_Handle *server = service->server;
  971. service->shutdown_task = NULL;
  972. if (0 != (service->options & LEGACY_SERVICE_OPTION_SOFT_SHUTDOWN))
  973. GNUNET_SERVER_stop_listening (server);
  974. else
  975. GNUNET_SERVER_destroy (server);
  976. }
  977. /**
  978. * Initial task for the service.
  979. *
  980. * @param cls service context
  981. */
  982. static void
  983. service_task (void *cls)
  984. {
  985. struct LEGACY_SERVICE_Context *sctx = cls;
  986. unsigned int i;
  987. GNUNET_RESOLVER_connect (sctx->cfg);
  988. if (NULL != sctx->lsocks)
  989. sctx->server = GNUNET_SERVER_create_with_sockets (&check_access,
  990. sctx,
  991. sctx->lsocks,
  992. sctx->timeout,
  993. sctx->require_found);
  994. else
  995. sctx->server = GNUNET_SERVER_create (&check_access,
  996. sctx,
  997. sctx->addrs,
  998. sctx->addrlens,
  999. sctx->timeout,
  1000. sctx->require_found);
  1001. if (NULL == sctx->server)
  1002. {
  1003. if (NULL != sctx->addrs)
  1004. for (i = 0; NULL != sctx->addrs[i]; i++)
  1005. LOG (GNUNET_ERROR_TYPE_INFO,
  1006. _ ("Failed to start `%s' at `%s'\n"),
  1007. sctx->service_name,
  1008. GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
  1009. sctx->ret = GNUNET_SYSERR;
  1010. return;
  1011. }
  1012. if (NULL != sctx->addrs)
  1013. for (i = 0; NULL != sctx->addrs[i]; i++)
  1014. if ((AF_UNIX == sctx->addrs[i]->sa_family) &&
  1015. ('\0' != ((const struct sockaddr_un *) sctx->addrs[i])->sun_path[0]))
  1016. GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)
  1017. sctx->addrs[i])
  1018. ->sun_path,
  1019. sctx->match_uid,
  1020. sctx->match_gid);
  1021. if (0 == (sctx->options & LEGACY_SERVICE_OPTION_MANUAL_SHUTDOWN))
  1022. {
  1023. /* install a task that will kill the server
  1024. * process if the scheduler ever gets a shutdown signal */
  1025. sctx->shutdown_task = GNUNET_SCHEDULER_add_shutdown (&shutdown_task, sctx);
  1026. }
  1027. sctx->my_handlers = GNUNET_malloc (sizeof(defhandlers));
  1028. GNUNET_memcpy (sctx->my_handlers, defhandlers, sizeof(defhandlers));
  1029. i = 0;
  1030. while (NULL != sctx->my_handlers[i].callback)
  1031. sctx->my_handlers[i++].callback_cls = sctx;
  1032. GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
  1033. if (-1 != sctx->ready_confirm_fd)
  1034. {
  1035. GNUNET_break (1 == write (sctx->ready_confirm_fd, ".", 1));
  1036. GNUNET_break (0 == close (sctx->ready_confirm_fd));
  1037. sctx->ready_confirm_fd = -1;
  1038. write_pid_file (sctx, getpid ());
  1039. }
  1040. if (NULL != sctx->addrs)
  1041. {
  1042. i = 0;
  1043. while (NULL != sctx->addrs[i])
  1044. {
  1045. LOG (GNUNET_ERROR_TYPE_INFO,
  1046. _ ("Service `%s' runs at %s\n"),
  1047. sctx->service_name,
  1048. GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
  1049. i++;
  1050. }
  1051. }
  1052. sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
  1053. }
  1054. /**
  1055. * Detach from terminal.
  1056. *
  1057. * @param sctx service context
  1058. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  1059. */
  1060. static int
  1061. detach_terminal (struct LEGACY_SERVICE_Context *sctx)
  1062. {
  1063. pid_t pid;
  1064. int nullfd;
  1065. int filedes[2];
  1066. if (0 != pipe (filedes))
  1067. {
  1068. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
  1069. return GNUNET_SYSERR;
  1070. }
  1071. pid = fork ();
  1072. if (pid < 0)
  1073. {
  1074. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
  1075. return GNUNET_SYSERR;
  1076. }
  1077. if (0 != pid)
  1078. {
  1079. /* Parent */
  1080. char c;
  1081. GNUNET_break (0 == close (filedes[1]));
  1082. c = 'X';
  1083. if (1 != read (filedes[0], &c, sizeof(char)))
  1084. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
  1085. fflush (stdout);
  1086. switch (c)
  1087. {
  1088. case '.':
  1089. exit (0);
  1090. case 'I':
  1091. LOG (GNUNET_ERROR_TYPE_INFO,
  1092. _ ("Service process failed to initialize\n"));
  1093. break;
  1094. case 'S':
  1095. LOG (GNUNET_ERROR_TYPE_INFO,
  1096. _ ("Service process could not initialize server function\n"));
  1097. break;
  1098. case 'X':
  1099. LOG (GNUNET_ERROR_TYPE_INFO,
  1100. _ ("Service process failed to report status\n"));
  1101. break;
  1102. }
  1103. exit (1); /* child reported error */
  1104. }
  1105. GNUNET_break (0 == close (0));
  1106. GNUNET_break (0 == close (1));
  1107. GNUNET_break (0 == close (filedes[0]));
  1108. nullfd = open ("/dev/null", O_RDWR | O_APPEND);
  1109. if (nullfd < 0)
  1110. return GNUNET_SYSERR;
  1111. /* set stdin/stdout to /dev/null */
  1112. if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
  1113. {
  1114. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
  1115. (void) close (nullfd);
  1116. return GNUNET_SYSERR;
  1117. }
  1118. (void) close (nullfd);
  1119. /* Detach from controlling terminal */
  1120. pid = setsid ();
  1121. if (-1 == pid)
  1122. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
  1123. sctx->ready_confirm_fd = filedes[1];
  1124. return GNUNET_OK;
  1125. }
  1126. /**
  1127. * Set user ID.
  1128. *
  1129. * @param sctx service context
  1130. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  1131. */
  1132. static int
  1133. set_user_id (struct LEGACY_SERVICE_Context *sctx)
  1134. {
  1135. char *user;
  1136. if (NULL == (user = get_user_name (sctx)))
  1137. return GNUNET_OK; /* keep */
  1138. struct passwd *pws;
  1139. errno = 0;
  1140. pws = getpwnam (user);
  1141. if (NULL == pws)
  1142. {
  1143. LOG (GNUNET_ERROR_TYPE_ERROR,
  1144. _ ("Cannot obtain information about user `%s': %s\n"),
  1145. user,
  1146. errno == 0 ? _ ("No such user") : strerror (errno));
  1147. GNUNET_free (user);
  1148. return GNUNET_SYSERR;
  1149. }
  1150. if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
  1151. #if HAVE_INITGROUPS
  1152. (0 != initgroups (user, pws->pw_gid)) ||
  1153. #endif
  1154. (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
  1155. {
  1156. if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
  1157. (0 != setreuid (pws->pw_uid, pws->pw_uid)))
  1158. {
  1159. LOG (GNUNET_ERROR_TYPE_ERROR,
  1160. _ ("Cannot change user/group to `%s': %s\n"),
  1161. user,
  1162. strerror (errno));
  1163. GNUNET_free (user);
  1164. return GNUNET_SYSERR;
  1165. }
  1166. }
  1167. GNUNET_free (user);
  1168. return GNUNET_OK;
  1169. }
  1170. /**
  1171. * Delete the PID file that was created by our parent.
  1172. *
  1173. * @param sctx service context
  1174. */
  1175. static void
  1176. pid_file_delete (struct LEGACY_SERVICE_Context *sctx)
  1177. {
  1178. char *pif = get_pid_file_name (sctx);
  1179. if (NULL == pif)
  1180. return; /* no PID file */
  1181. if (0 != unlink (pif))
  1182. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
  1183. GNUNET_free (pif);
  1184. }
  1185. /**
  1186. * Run a standard GNUnet service startup sequence (initialize loggers
  1187. * and configuration, parse options).
  1188. *
  1189. * @param argc number of command line arguments
  1190. * @param argv command line arguments
  1191. * @param service_name our service name
  1192. * @param options service options
  1193. * @param task main task of the service
  1194. * @param task_cls closure for @a task
  1195. * @return #GNUNET_SYSERR on error, #GNUNET_OK
  1196. * if we shutdown nicely
  1197. */
  1198. int
  1199. LEGACY_SERVICE_run (int argc,
  1200. char *const *argv,
  1201. const char *service_name,
  1202. enum LEGACY_SERVICE_Options options,
  1203. LEGACY_SERVICE_Main task,
  1204. void *task_cls)
  1205. {
  1206. #define HANDLE_ERROR \
  1207. do \
  1208. { \
  1209. GNUNET_break (0); \
  1210. goto shutdown; \
  1211. } while (0)
  1212. int err;
  1213. int ret;
  1214. char *cfg_fn;
  1215. char *opt_cfg_fn;
  1216. char *loglev;
  1217. char *logfile;
  1218. int do_daemonize;
  1219. unsigned int i;
  1220. unsigned long long skew_offset;
  1221. unsigned long long skew_variance;
  1222. long long clock_offset;
  1223. struct LEGACY_SERVICE_Context sctx;
  1224. struct GNUNET_CONFIGURATION_Handle *cfg;
  1225. const char *xdg;
  1226. struct GNUNET_GETOPT_CommandLineOption service_options[] =
  1227. { GNUNET_GETOPT_option_cfgfile (&opt_cfg_fn),
  1228. GNUNET_GETOPT_option_flag ('d',
  1229. "daemonize",
  1230. gettext_noop (
  1231. "do daemonize (detach from terminal)"),
  1232. &do_daemonize),
  1233. GNUNET_GETOPT_option_help (NULL),
  1234. GNUNET_GETOPT_option_loglevel (&loglev),
  1235. GNUNET_GETOPT_option_logfile (&logfile),
  1236. GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
  1237. GNUNET_GETOPT_OPTION_END };
  1238. err = 1;
  1239. do_daemonize = 0;
  1240. logfile = NULL;
  1241. loglev = NULL;
  1242. opt_cfg_fn = NULL;
  1243. xdg = getenv ("XDG_CONFIG_HOME");
  1244. if (NULL != xdg)
  1245. GNUNET_asprintf (&cfg_fn,
  1246. "%s%s%s",
  1247. xdg,
  1248. DIR_SEPARATOR_STR,
  1249. GNUNET_OS_project_data_get ()->config_file);
  1250. else
  1251. cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
  1252. memset (&sctx, 0, sizeof(sctx));
  1253. sctx.options = options;
  1254. sctx.ready_confirm_fd = -1;
  1255. sctx.ret = GNUNET_OK;
  1256. sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
  1257. sctx.task = task;
  1258. sctx.task_cls = task_cls;
  1259. sctx.service_name = service_name;
  1260. sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
  1261. /* setup subsystems */
  1262. ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
  1263. if (GNUNET_SYSERR == ret)
  1264. goto shutdown;
  1265. if (GNUNET_NO == ret)
  1266. {
  1267. err = 0;
  1268. goto shutdown;
  1269. }
  1270. if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
  1271. HANDLE_ERROR;
  1272. if (NULL == opt_cfg_fn)
  1273. opt_cfg_fn = GNUNET_strdup (cfg_fn);
  1274. if (GNUNET_YES == GNUNET_DISK_file_test (opt_cfg_fn))
  1275. {
  1276. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, opt_cfg_fn))
  1277. {
  1278. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1279. _ ("Malformed configuration file `%s', exit ...\n"),
  1280. opt_cfg_fn);
  1281. goto shutdown;
  1282. }
  1283. }
  1284. else
  1285. {
  1286. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, NULL))
  1287. {
  1288. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1289. _ ("Malformed configuration, exit ...\n"));
  1290. goto shutdown;
  1291. }
  1292. if (0 != strcmp (opt_cfg_fn, cfg_fn))
  1293. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1294. _ ("Could not access configuration file `%s'\n"),
  1295. opt_cfg_fn);
  1296. }
  1297. if (GNUNET_OK != setup_service (&sctx))
  1298. goto shutdown;
  1299. if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
  1300. HANDLE_ERROR;
  1301. if (GNUNET_OK != set_user_id (&sctx))
  1302. goto shutdown;
  1303. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1304. "Service `%s' runs with configuration from `%s'\n",
  1305. service_name,
  1306. opt_cfg_fn);
  1307. if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (sctx.cfg,
  1308. "TESTING",
  1309. "SKEW_OFFSET",
  1310. &skew_offset)) &&
  1311. (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (sctx.cfg,
  1312. "TESTING",
  1313. "SKEW_VARIANCE",
  1314. &skew_variance)))
  1315. {
  1316. clock_offset = skew_offset - skew_variance;
  1317. GNUNET_TIME_set_offset (clock_offset);
  1318. LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
  1319. }
  1320. /* actually run service */
  1321. err = 0;
  1322. GNUNET_SCHEDULER_run (&service_task, &sctx);
  1323. /* shutdown */
  1324. if ((1 == do_daemonize) && (NULL != sctx.server))
  1325. pid_file_delete (&sctx);
  1326. GNUNET_free_non_null (sctx.my_handlers);
  1327. shutdown:
  1328. if (-1 != sctx.ready_confirm_fd)
  1329. {
  1330. if (1 != write (sctx.ready_confirm_fd, err ? "I" : "S", 1))
  1331. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
  1332. GNUNET_break (0 == close (sctx.ready_confirm_fd));
  1333. }
  1334. #if HAVE_MALLINFO
  1335. {
  1336. char *counter;
  1337. if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (sctx.cfg,
  1338. service_name,
  1339. "GAUGER_HEAP")) &&
  1340. (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (sctx.cfg,
  1341. service_name,
  1342. "GAUGER_HEAP",
  1343. &counter)))
  1344. {
  1345. struct mallinfo mi;
  1346. mi = mallinfo ();
  1347. GAUGER (service_name, counter, mi.usmblks, "blocks");
  1348. GNUNET_free (counter);
  1349. }
  1350. }
  1351. #endif
  1352. GNUNET_CONFIGURATION_destroy (cfg);
  1353. i = 0;
  1354. if (NULL != sctx.addrs)
  1355. while (NULL != sctx.addrs[i])
  1356. GNUNET_free_nz (sctx.addrs[i++]);
  1357. GNUNET_free_non_null (sctx.addrs);
  1358. GNUNET_free_non_null (sctx.addrlens);
  1359. GNUNET_free_non_null (logfile);
  1360. GNUNET_free_non_null (loglev);
  1361. GNUNET_free (cfg_fn);
  1362. GNUNET_free_non_null (opt_cfg_fn);
  1363. GNUNET_free_non_null (sctx.v4_denied);
  1364. GNUNET_free_non_null (sctx.v6_denied);
  1365. GNUNET_free_non_null (sctx.v4_allowed);
  1366. GNUNET_free_non_null (sctx.v6_allowed);
  1367. return err ? GNUNET_SYSERR : sctx.ret;
  1368. }
  1369. /**
  1370. * Run a service startup sequence within an existing
  1371. * initialized system.
  1372. *
  1373. * @param service_name our service name
  1374. * @param cfg configuration to use
  1375. * @param options service options
  1376. * @return NULL on error, service handle
  1377. */
  1378. struct LEGACY_SERVICE_Context *
  1379. LEGACY_SERVICE_start (const char *service_name,
  1380. const struct GNUNET_CONFIGURATION_Handle *cfg,
  1381. enum LEGACY_SERVICE_Options options)
  1382. {
  1383. int i;
  1384. struct LEGACY_SERVICE_Context *sctx;
  1385. sctx = GNUNET_new (struct LEGACY_SERVICE_Context);
  1386. sctx->ready_confirm_fd = -1; /* no daemonizing */
  1387. sctx->ret = GNUNET_OK;
  1388. sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
  1389. sctx->service_name = service_name;
  1390. sctx->cfg = cfg;
  1391. sctx->options = options;
  1392. /* setup subsystems */
  1393. if (GNUNET_OK != setup_service (sctx))
  1394. {
  1395. LEGACY_SERVICE_stop (sctx);
  1396. return NULL;
  1397. }
  1398. if (NULL != sctx->lsocks)
  1399. sctx->server = GNUNET_SERVER_create_with_sockets (&check_access,
  1400. sctx,
  1401. sctx->lsocks,
  1402. sctx->timeout,
  1403. sctx->require_found);
  1404. else
  1405. sctx->server = GNUNET_SERVER_create (&check_access,
  1406. sctx,
  1407. sctx->addrs,
  1408. sctx->addrlens,
  1409. sctx->timeout,
  1410. sctx->require_found);
  1411. if (NULL == sctx->server)
  1412. {
  1413. LEGACY_SERVICE_stop (sctx);
  1414. return NULL;
  1415. }
  1416. if (NULL != sctx->addrs)
  1417. for (i = 0; NULL != sctx->addrs[i]; i++)
  1418. if ((AF_UNIX == sctx->addrs[i]->sa_family) &&
  1419. ('\0' != ((const struct sockaddr_un *) sctx->addrs[i])->sun_path[0]))
  1420. GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)
  1421. sctx->addrs[i])
  1422. ->sun_path,
  1423. sctx->match_uid,
  1424. sctx->match_gid);
  1425. sctx->my_handlers = GNUNET_malloc (sizeof(defhandlers));
  1426. GNUNET_memcpy (sctx->my_handlers, defhandlers, sizeof(defhandlers));
  1427. i = 0;
  1428. while ((sctx->my_handlers[i].callback != NULL))
  1429. sctx->my_handlers[i++].callback_cls = sctx;
  1430. GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
  1431. return sctx;
  1432. }
  1433. /**
  1434. * Obtain the server used by a service. Note that the server must NOT
  1435. * be destroyed by the caller.
  1436. *
  1437. * @param ctx the service context returned from the start function
  1438. * @return handle to the server for this service, NULL if there is none
  1439. */
  1440. struct GNUNET_SERVER_Handle *
  1441. LEGACY_SERVICE_get_server (struct LEGACY_SERVICE_Context *ctx)
  1442. {
  1443. return ctx->server;
  1444. }
  1445. /**
  1446. * Get the NULL-terminated array of listen sockets for this service.
  1447. *
  1448. * @param ctx service context to query
  1449. * @return NULL if there are no listen sockets, otherwise NULL-terminated
  1450. * array of listen sockets.
  1451. */
  1452. struct GNUNET_NETWORK_Handle *const *
  1453. LEGACY_SERVICE_get_listen_sockets (struct LEGACY_SERVICE_Context *ctx)
  1454. {
  1455. return ctx->lsocks;
  1456. }
  1457. /**
  1458. * Stop a service that was started with "LEGACY_SERVICE_start".
  1459. *
  1460. * @param sctx the service context returned from the start function
  1461. */
  1462. void
  1463. LEGACY_SERVICE_stop (struct LEGACY_SERVICE_Context *sctx)
  1464. {
  1465. unsigned int i;
  1466. #if HAVE_MALLINFO
  1467. {
  1468. char *counter;
  1469. if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (sctx->cfg,
  1470. sctx->service_name,
  1471. "GAUGER_HEAP")) &&
  1472. (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
  1473. sctx->service_name,
  1474. "GAUGER_HEAP",
  1475. &counter)))
  1476. {
  1477. struct mallinfo mi;
  1478. mi = mallinfo ();
  1479. GAUGER (sctx->service_name, counter, mi.usmblks, "blocks");
  1480. GNUNET_free (counter);
  1481. }
  1482. }
  1483. #endif
  1484. if (NULL != sctx->shutdown_task)
  1485. {
  1486. GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
  1487. sctx->shutdown_task = NULL;
  1488. }
  1489. if (NULL != sctx->server)
  1490. GNUNET_SERVER_destroy (sctx->server);
  1491. GNUNET_free_non_null (sctx->my_handlers);
  1492. if (NULL != sctx->addrs)
  1493. {
  1494. i = 0;
  1495. while (NULL != sctx->addrs[i])
  1496. GNUNET_free_nz (sctx->addrs[i++]);
  1497. GNUNET_free (sctx->addrs);
  1498. }
  1499. GNUNET_free_non_null (sctx->addrlens);
  1500. GNUNET_free_non_null (sctx->v4_denied);
  1501. GNUNET_free_non_null (sctx->v6_denied);
  1502. GNUNET_free_non_null (sctx->v4_allowed);
  1503. GNUNET_free_non_null (sctx->v6_allowed);
  1504. GNUNET_free (sctx);
  1505. }
  1506. /* end of service.c */