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.

andna_cache.c 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * --
  19. * andna_cache.c:
  20. * Functions to manipulate all the andna's caches.
  21. */
  22. #include "includes.h"
  23. #include "crypto.h"
  24. #include "andna_cache.h"
  25. #include "snsd_cache.h"
  26. #include "common.h"
  27. #include "hash.h"
  28. int net_family;
  29. void
  30. andna_caches_init(int family)
  31. {
  32. net_family = family;
  33. setzero(&lcl_keyring, sizeof(lcl_keyring));
  34. andna_lcl = (lcl_cache *) clist_init(&lcl_counter);
  35. andna_c = (andna_cache *) clist_init(&andna_c_counter);
  36. andna_counter_c = (counter_c *) clist_init(&cc_counter);
  37. andna_rhc = (rh_cache *) clist_init(&rhc_counter);
  38. }
  39. /*
  40. * andna_32bit_hash
  41. *
  42. * It returns the 32bit hash of the md5 hash of the `hname' string.
  43. */
  44. u_int
  45. andna_32bit_hash(char *hname)
  46. {
  47. u_char hashm5[ANDNA_HASH_SZ];
  48. hash_md5((u_char *) hname, strlen(hname), hashm5);
  49. return fnv_32_buf(hashm5, ANDNA_HASH_SZ, FNV1_32_INIT);
  50. }
  51. /*
  52. *
  53. * * * * Local Cache functions * * *
  54. *
  55. */
  56. /*
  57. * lcl_new_keyring
  58. *
  59. * It generates a new keyring.
  60. */
  61. void
  62. lcl_new_keyring(lcl_cache_keyring * keyring)
  63. {
  64. setzero(keyring, sizeof(lcl_cache_keyring));
  65. loginfo("Generating a new ANDNA keyring");
  66. /* Generate the new key pair for the first time */
  67. keyring->priv_rsa = genrsa(ANDNA_PRIVKEY_BITS, &keyring->pubkey,
  68. &keyring->pkey_len, &keyring->privkey,
  69. &keyring->skey_len);
  70. }
  71. /*
  72. * lcl_destroy_keyring
  73. *
  74. * destroys accurately the keyring ^_^
  75. */
  76. void
  77. lcl_destroy_keyring(lcl_cache_keyring * keyring)
  78. {
  79. if (keyring->priv_rsa)
  80. RSA_free(keyring->priv_rsa);
  81. if (keyring->pubkey)
  82. xfree(keyring->pubkey);
  83. if (keyring->privkey)
  84. xfree(keyring->privkey);
  85. setzero(keyring, sizeof(lcl_cache_keyring));
  86. }
  87. /*
  88. * lcl_cache_new: builds a new lcl_cache generating a new rsa key pair and
  89. * setting the hostname in the struct
  90. */
  91. lcl_cache *
  92. lcl_cache_new(char *hname)
  93. {
  94. lcl_cache *alcl;
  95. alcl = (lcl_cache *) xzalloc(sizeof(lcl_cache));
  96. alcl->hostname = xstrdup(hname);
  97. alcl->hash = andna_32bit_hash(hname);
  98. return alcl;
  99. }
  100. void
  101. lcl_cache_free(lcl_cache * alcl)
  102. {
  103. if (alcl->hostname)
  104. xfree(alcl->hostname);
  105. alcl->snsd_counter = 0;
  106. if (alcl->service)
  107. snsd_service_llist_del(&alcl->service);
  108. }
  109. void
  110. lcl_cache_destroy(lcl_cache * head, int *counter)
  111. {
  112. lcl_cache *alcl = head, *next;
  113. if (!alcl || !lcl_counter)
  114. return;
  115. list_safe_for(alcl, next) {
  116. lcl_cache_free(alcl);
  117. xfree(alcl);
  118. }
  119. *counter = 0;
  120. }
  121. lcl_cache *
  122. lcl_cache_find_hname(lcl_cache * alcl, char *hname)
  123. {
  124. u_int hash;
  125. if (!alcl || !lcl_counter)
  126. return 0;
  127. hash = andna_32bit_hash(hname);
  128. list_for(alcl)
  129. if (alcl->hash == hash && alcl->hostname &&
  130. !strncmp(alcl->hostname, hname, ANDNA_MAX_HNAME_LEN))
  131. return alcl;
  132. return 0;
  133. }
  134. lcl_cache *
  135. lcl_cache_find_hash(lcl_cache * alcl, u_int hash)
  136. {
  137. if (!alcl || !lcl_counter)
  138. return 0;
  139. list_for(alcl)
  140. if (alcl->hash == hash && alcl->hostname)
  141. return alcl;
  142. return 0;
  143. }
  144. int
  145. is_lcl_hname_registered(lcl_cache * alcl)
  146. {
  147. return alcl->timestamp;
  148. }
  149. /*
  150. * lcl_get_registered_hnames
  151. *
  152. * It returns a duplicated lcl_cache of `alcl', which contains only
  153. * hostnames already registered.
  154. * Note that the structs present in the returned cache are in a different
  155. * mallocated space, so you should free them.
  156. */
  157. lcl_cache *
  158. lcl_get_registered_hnames(lcl_cache * alcl)
  159. {
  160. lcl_cache *lcl;
  161. lcl = list_copy_some(alcl, is_lcl_hname_registered);
  162. list_for(lcl) {
  163. lcl->hostname = xstrdup(lcl->hostname);
  164. lcl->service = snsd_service_llist_copy(lcl->service,
  165. SNSD_ALL_SERVICE, 0);
  166. }
  167. return lcl;
  168. }
  169. /*
  170. *
  171. * * * * Andna Cache functions * * *
  172. *
  173. */
  174. andna_cache_queue *
  175. ac_queue_findpubk(andna_cache * ac, char *pubk)
  176. {
  177. andna_cache_queue *acq = ac->acq;
  178. if (!acq)
  179. return 0;
  180. list_for(acq)
  181. if (!memcmp(acq->pubkey, pubk, ANDNA_PKEY_LEN))
  182. return acq;
  183. return 0;
  184. }
  185. /*
  186. * ac_queue_add
  187. *
  188. * adds a new entry in the andna cache queue, which is `ac'->acq.
  189. * The elements in the new `ac'->acq are updated.
  190. * If an `ac'->acq struct with an `ac'->acq->pubkey equal to `pubkey' already
  191. * exists, then only the timestamp and the IP will be updated.
  192. *
  193. * It returns the pointer to the acq struct. If it isn't possible to add a new
  194. * entry in the queue, 0 will be returned.
  195. *
  196. * Remember to update the acq->timestamp value after this call.
  197. */
  198. andna_cache_queue *
  199. ac_queue_add(andna_cache * ac, char *pubkey)
  200. {
  201. andna_cache_queue *acq;
  202. /*
  203. * This call is not necessary because it's already done by
  204. * andna_cache_del_expired().
  205. * * ac_queue_del_expired(ac); * *
  206. */
  207. if (!(acq = ac_queue_findpubk(ac, pubkey))) {
  208. if (ac->queue_counter >= ANDNA_MAX_QUEUE || ac->flags & ANDNA_FULL)
  209. return 0;
  210. acq = xzalloc(sizeof(andna_cache_queue));
  211. memcpy(acq->pubkey, pubkey, ANDNA_PKEY_LEN);
  212. clist_append(&ac->acq, 0, &ac->queue_counter, acq);
  213. }
  214. if (ac->queue_counter >= ANDNA_MAX_QUEUE)
  215. ac->flags |= ANDNA_FULL;
  216. return acq;
  217. }
  218. void
  219. ac_queue_del(andna_cache * ac, andna_cache_queue * acq)
  220. {
  221. acq->snsd_counter = 0;
  222. if (acq->service)
  223. snsd_service_llist_del(&acq->service);
  224. clist_del(&ac->acq, &ac->queue_counter, acq);
  225. ac->flags &= ~ANDNA_FULL;
  226. }
  227. /*
  228. * ac_queue_del_expired: removes the expired entries from the
  229. * andna_cache_queue `ac'->acq.
  230. */
  231. void
  232. ac_queue_del_expired(andna_cache * ac)
  233. {
  234. andna_cache_queue *acq, *next;
  235. time_t cur_t;
  236. if (!ac || !ac->acq)
  237. return;
  238. cur_t = time(0);
  239. acq = ac->acq;
  240. list_safe_for(acq, next)
  241. if (cur_t - acq->timestamp > ANDNA_EXPIRATION_TIME)
  242. ac_queue_del(ac, acq);
  243. }
  244. /*
  245. * ac_queue_destroy: destroys an andna_cache_queue
  246. */
  247. void
  248. ac_queue_destroy(andna_cache * ac)
  249. {
  250. andna_cache_queue *acq, *next;
  251. if (!ac || !ac->acq)
  252. return;
  253. acq = ac->acq;
  254. list_safe_for(acq, next)
  255. ac_queue_del(ac, acq);
  256. }
  257. andna_cache *
  258. andna_cache_findhash(int hash[MAX_IP_INT])
  259. {
  260. andna_cache *ac = andna_c;
  261. if (!andna_c_counter)
  262. return 0;
  263. list_for(ac)
  264. if (!memcmp(ac->hash, hash, ANDNA_HASH_SZ))
  265. return ac;
  266. return 0;
  267. }
  268. /*
  269. * andna_cache_gethash
  270. *
  271. * It searches an andna_cache entry which has the same hash of `hash'.
  272. * If it found but this entry is expired, it is deleted from the cache and 0 is
  273. * returned.
  274. * If it isn't found 0 is returned, otherwise a pointer to the entry is
  275. * returned.
  276. */
  277. andna_cache *
  278. andna_cache_gethash(int hash[MAX_IP_INT])
  279. {
  280. andna_cache *ac;
  281. ac = andna_cache_findhash(hash);
  282. if (ac && andna_cache_del_ifexpired(ac))
  283. return 0;
  284. return ac;
  285. }
  286. andna_cache *
  287. andna_cache_addhash(int hash[MAX_IP_INT])
  288. {
  289. andna_cache *ac;
  290. andna_cache_del_expired();
  291. if (!(ac = andna_cache_findhash(hash))) {
  292. ac = xzalloc(sizeof(andna_cache));
  293. memcpy(ac->hash, hash, ANDNA_HASH_SZ);
  294. clist_add(&andna_c, &andna_c_counter, ac);
  295. }
  296. return ac;
  297. }
  298. /*
  299. * andna_cache_del_ifexpired
  300. *
  301. * If `ac' is expired, it deletes it and returns 1; otherwise 0 is returned.
  302. */
  303. int
  304. andna_cache_del_ifexpired(andna_cache * ac)
  305. {
  306. ac_queue_del_expired(ac);
  307. if (!ac->queue_counter) {
  308. clist_del(&andna_c, &andna_c_counter, ac);
  309. return 1;
  310. }
  311. return 0;
  312. }
  313. void
  314. andna_cache_del_expired(void)
  315. {
  316. andna_cache *ac = andna_c, *next;
  317. if (!andna_c_counter)
  318. return;
  319. list_safe_for(ac, next)
  320. andna_cache_del_ifexpired(ac);
  321. }
  322. /*
  323. * andna_cache_destroy
  324. *
  325. * destroys the andna_c llist
  326. */
  327. void
  328. andna_cache_destroy(void)
  329. {
  330. andna_cache *ac = andna_c, *next;
  331. if (!andna_c_counter)
  332. return;
  333. list_safe_for(ac, next) {
  334. ac_queue_destroy(ac);
  335. clist_del(&andna_c, &andna_c_counter, ac);
  336. }
  337. }
  338. /*
  339. *
  340. * * * * Counter Cache functions * * *
  341. *
  342. */
  343. /*
  344. * Remeber to update the cch->timestamp value after this call.
  345. */
  346. counter_c_hashes *
  347. cc_hashes_add(counter_c * cc, int hash[MAX_IP_INT])
  348. {
  349. counter_c_hashes *cch;
  350. /* The purge is already done in counter_c_del_expired(), so it is not
  351. * necessary to call it here.
  352. * * cc_hashes_del_expired(cc); * *
  353. */
  354. if (!(cch = cc_findhash(cc, hash))) {
  355. if (cc->hashes >= ANDNA_MAX_HOSTNAMES || cc->flags & ANDNA_FULL)
  356. return 0;
  357. cch = xzalloc(sizeof(counter_c_hashes));
  358. memcpy(cch->hash, hash, ANDNA_HASH_SZ);
  359. clist_add(&cc->cch, &cc->hashes, cch);
  360. }
  361. if (cc->hashes >= ANDNA_MAX_HOSTNAMES)
  362. cc->flags |= ANDNA_FULL;
  363. return cch;
  364. }
  365. void
  366. cc_hashes_del(counter_c * cc, counter_c_hashes * cch)
  367. {
  368. clist_del(&cc->cch, &cc->hashes, cch);
  369. cc->flags &= ~ANDNA_FULL;
  370. }
  371. void
  372. cc_hashes_del_expired(counter_c * cc)
  373. {
  374. counter_c_hashes *cch, *next;
  375. time_t cur_t;
  376. if (!cc || !cc->cch || !cc->hashes)
  377. return;
  378. cur_t = time(0);
  379. cch = cc->cch;
  380. list_safe_for(cch, next)
  381. if (cur_t - cch->timestamp > ANDNA_EXPIRATION_TIME)
  382. cc_hashes_del(cc, cch);
  383. }
  384. void
  385. cc_hashes_destroy(counter_c * cc)
  386. {
  387. counter_c_hashes *cch, *next;
  388. if (!cc || !cc->cch || !cc->hashes)
  389. return;
  390. cch = cc->cch;
  391. list_safe_for(cch, next)
  392. cc_hashes_del(cc, cch);
  393. }
  394. counter_c_hashes *
  395. cc_findhash(counter_c * cc, int hash[MAX_IP_INT])
  396. {
  397. counter_c_hashes *cch = cc->cch;
  398. if (!cc->hashes || !cch)
  399. return 0;
  400. list_for(cch)
  401. if (!memcmp(cch->hash, hash, ANDNA_HASH_SZ))
  402. return cch;
  403. return 0;
  404. }
  405. counter_c *
  406. counter_c_findpubk(char *pubk)
  407. {
  408. counter_c *cc = andna_counter_c;
  409. if (!cc_counter || !cc)
  410. return 0;
  411. list_for(cc)
  412. if (!memcmp(&cc->pubkey, pubk, ANDNA_PKEY_LEN))
  413. return cc;
  414. return 0;
  415. }
  416. counter_c *
  417. counter_c_add(inet_prefix * rip, char *pubkey)
  418. {
  419. counter_c *cc;
  420. counter_c_del_expired();
  421. if (!(cc = counter_c_findpubk(pubkey))) {
  422. cc = xzalloc(sizeof(counter_c));
  423. memcpy(cc->pubkey, pubkey, ANDNA_PKEY_LEN);
  424. clist_add(&andna_counter_c, &cc_counter, cc);
  425. }
  426. return cc;
  427. }
  428. void
  429. counter_c_del_expired(void)
  430. {
  431. counter_c *cc = andna_counter_c, *next;
  432. if (!cc)
  433. return;
  434. list_safe_for(cc, next) {
  435. cc_hashes_del_expired(cc);
  436. if (!cc->hashes)
  437. clist_del(&andna_counter_c, &cc_counter, cc);
  438. }
  439. }
  440. /*
  441. * counter_c_destroy
  442. *
  443. * destroy the andna_counter_c llist
  444. */
  445. void
  446. counter_c_destroy(void)
  447. {
  448. counter_c *cc = andna_counter_c, *next;
  449. if (!cc)
  450. return;
  451. list_safe_for(cc, next) {
  452. cc_hashes_destroy(cc);
  453. clist_del(&andna_counter_c, &cc_counter, cc);
  454. }
  455. }
  456. /*
  457. *
  458. * * * * Resolved hostnames cache functions * * *
  459. *
  460. */
  461. rh_cache *
  462. rh_cache_new_hash(u_int hash, time_t timestamp)
  463. {
  464. rh_cache *rhc;
  465. rhc = xzalloc(sizeof(rh_cache));
  466. rhc->hash = hash;
  467. rhc->timestamp = timestamp;
  468. return rhc;
  469. }
  470. rh_cache *
  471. rh_cache_new(char *hname, time_t timestamp)
  472. {
  473. return rh_cache_new_hash(andna_32bit_hash(hname), timestamp);
  474. }
  475. /*
  476. * rh_cache_add_hash
  477. *
  478. * It searches a struct in the rh_cache which has the hash value equal to
  479. * `hash'.
  480. * If it isn't found a new one is added. In both cases the pointer to the
  481. * struct will be returned.
  482. *
  483. * On error 0 is returned.
  484. */
  485. rh_cache *
  486. rh_cache_add_hash(u_int hash, time_t timestamp)
  487. {
  488. rh_cache *rhc;
  489. if (!(rhc = rh_cache_find_hash(hash))) {
  490. if (rhc_counter >= ANDNA_MAX_HOSTNAMES) {
  491. /* Delete the expired hnames and see if there's empty
  492. * space */
  493. rh_cache_del_expired();
  494. if (rhc_counter >= ANDNA_MAX_HOSTNAMES) {
  495. /* Delete the oldest struct in cache */
  496. rhc = (rh_cache *)list_last(andna_rhc);
  497. clist_del(&andna_rhc, &rhc_counter, rhc);
  498. }
  499. }
  500. rhc = rh_cache_new_hash(hash, timestamp);
  501. clist_add(&andna_rhc, &rhc_counter, rhc);
  502. }
  503. rhc->timestamp = timestamp;
  504. return rhc;
  505. }
  506. /*
  507. * rh_cache_add
  508. *
  509. * It searches a struct in the rh_cache which is associated to `hname'.
  510. * If it isn't found a new one is added. In both cases the pointer to the
  511. * struct will be returned.
  512. *
  513. * On error 0 is returned.
  514. */
  515. rh_cache *
  516. rh_cache_add(char *hname, time_t timestamp)
  517. {
  518. return rh_cache_add_hash(andna_32bit_hash(hname), timestamp);
  519. }
  520. rh_cache *
  521. rh_cache_find_hash(u_int hash)
  522. {
  523. rh_cache *rhc = andna_rhc, *next;
  524. time_t cur_t;
  525. if (!rhc || !rhc_counter)
  526. return 0;
  527. cur_t = time(0);
  528. list_safe_for(rhc, next)
  529. if (rhc->hash == hash) {
  530. if (cur_t - rhc->timestamp > ANDNA_EXPIRATION_TIME) {
  531. /* This hostname expired, delete it from the
  532. * cache */
  533. rh_cache_del(rhc);
  534. continue;
  535. } else
  536. /* Each time we find a hname in the rh_cache,
  537. * we move it on top of the llist. */
  538. andna_rhc = list_moveontop(andna_rhc, rhc);
  539. return rhc;
  540. }
  541. return 0;
  542. }
  543. rh_cache *
  544. rh_cache_find_hname(char *hname)
  545. {
  546. u_int hash;
  547. hash = andna_32bit_hash(hname);
  548. return rh_cache_find_hash(hash);
  549. }
  550. void
  551. rh_cache_del(rh_cache * rhc)
  552. {
  553. rhc->snsd_counter = 0;
  554. if (rhc->service)
  555. snsd_service_llist_del(&rhc->service);
  556. clist_del(&andna_rhc, &rhc_counter, rhc);
  557. }
  558. void
  559. rh_cache_del_expired(void)
  560. {
  561. rh_cache *rhc = andna_rhc, *next;
  562. time_t cur_t;
  563. if (!rhc || !rhc_counter)
  564. return;
  565. cur_t = time(0);
  566. list_safe_for(rhc, next)
  567. if (cur_t - rhc->timestamp > ANDNA_EXPIRATION_TIME)
  568. rh_cache_del(rhc);
  569. }
  570. void
  571. rh_cache_flush(void)
  572. {
  573. rh_cache *rhc = andna_rhc, *next;
  574. list_safe_for(rhc, next)
  575. rh_cache_del(rhc);
  576. }
  577. /*
  578. *
  579. * * * * Pack/Unpack functions * * *
  580. *
  581. */
  582. char *
  583. pack_lcl_keyring(lcl_cache_keyring * keyring, size_t * pack_sz)
  584. {
  585. struct lcl_keyring_pkt_hdr key_hdr;
  586. size_t sz;
  587. char *pack, *buf;
  588. key_hdr.skey_len = keyring->skey_len;
  589. key_hdr.pkey_len = keyring->pkey_len;
  590. sz = LCL_KEYRING_HDR_PACK_SZ(&key_hdr);
  591. pack = buf = xmalloc(sz);
  592. bufput(&key_hdr, sizeof(struct lcl_keyring_pkt_hdr));
  593. ints_host_to_network(pack, lcl_keyring_pkt_hdr_iinfo);
  594. bufput(keyring->privkey, keyring->skey_len);
  595. bufput(keyring->pubkey, keyring->pkey_len);
  596. *pack_sz = sz;
  597. return pack;
  598. }
  599. /*
  600. * unpack_lcl_keyring: unpacks a lcl keyring. On error it returns -1.
  601. * In `keyring' it restores the packed keys.
  602. */
  603. int
  604. unpack_lcl_keyring(lcl_cache_keyring * keyring, char *pack, size_t pack_sz)
  605. {
  606. struct lcl_keyring_pkt_hdr *hdr;
  607. char *buf;
  608. u_char *pk;
  609. hdr = (struct lcl_keyring_pkt_hdr *) pack;
  610. ints_network_to_host(hdr, lcl_keyring_pkt_hdr_iinfo);
  611. /*
  612. * Restore the keyring
  613. */
  614. keyring->skey_len = hdr->skey_len;
  615. keyring->pkey_len = hdr->pkey_len;
  616. if (keyring->skey_len > ANDNA_SKEY_MAX_LEN) {
  617. error(ERROR_MSG "Invalid keyring header", ERROR_FUNC);
  618. return -1;
  619. }
  620. keyring->privkey = xmalloc(hdr->skey_len);
  621. keyring->pubkey = xmalloc(hdr->pkey_len);
  622. /* extract the private key */
  623. buf = pack + sizeof(struct lcl_keyring_pkt_hdr);
  624. bufget(keyring->privkey, hdr->skey_len);
  625. /* public key */
  626. bufget(keyring->pubkey, hdr->pkey_len);
  627. pk = keyring->privkey;
  628. if (!(keyring->priv_rsa = get_rsa_priv((const u_char **) &pk,
  629. keyring->skey_len))) {
  630. error(ERROR_MSG "Cannot unpack the priv key from the"
  631. " lcl_pack: %s", ERROR_POS, ssl_strerr());
  632. return -1;
  633. }
  634. return 0;
  635. }
  636. /*
  637. * pack_lcl_cache
  638. *
  639. * packs the entire local cache linked list that starts with the head
  640. * `local_cache'. The size of the pack is stored in `pack_sz'.
  641. * The pointer to the newly allocated pack is returned.
  642. * Note that the pack is in network byte order.
  643. */
  644. char *
  645. pack_lcl_cache(lcl_cache * local_cache, size_t * pack_sz)
  646. {
  647. struct lcl_cache_pkt_hdr lcl_hdr;
  648. lcl_cache *alcl = local_cache;
  649. size_t sz = 0, slen;
  650. char *pack, *buf, *body;
  651. lcl_hdr.tot_caches = 0;
  652. sz = LCL_CACHE_HDR_PACK_SZ;
  653. /* Calculate the final pack size */
  654. list_for(alcl) {
  655. sz += LCL_CACHE_BODY_PACK_SZ(strlen(alcl->hostname) + 1);
  656. lcl_hdr.tot_caches++;
  657. }
  658. pack = buf = xmalloc(sz);
  659. bufput(&lcl_hdr, sizeof(struct lcl_cache_pkt_hdr));
  660. ints_host_to_network(pack, lcl_cache_pkt_hdr_iinfo);
  661. *pack_sz = 0;
  662. if (lcl_hdr.tot_caches) {
  663. alcl = local_cache;
  664. list_for(alcl) {
  665. body = buf;
  666. bufput(&alcl->hname_updates, sizeof(u_short));
  667. bufput(&alcl->timestamp, sizeof(time_t));
  668. slen = strlen(alcl->hostname) + 1;
  669. bufput(alcl->hostname, slen);
  670. ints_host_to_network(body, lcl_cache_pkt_body_iinfo);
  671. }
  672. }
  673. *pack_sz = sz;
  674. return pack;
  675. }
  676. /*
  677. * unpack_lcl_cache
  678. *
  679. * Unpacks a packed local cache linked list and returns its head.
  680. * `counter' is set to the number of struct in the llist.
  681. *
  682. * On error 0 is returned and `*counter' is set to -1.
  683. *
  684. * Note: `pack' is modified during the unpacking.
  685. */
  686. lcl_cache *
  687. unpack_lcl_cache(char *pack, size_t pack_sz, int *counter)
  688. {
  689. struct lcl_cache_pkt_hdr *hdr;
  690. lcl_cache *alcl, *alcl_head = 0;
  691. char *buf;
  692. size_t slen, unpacked_sz;
  693. int i = 0;
  694. hdr = (struct lcl_cache_pkt_hdr *) pack;
  695. buf = pack + sizeof(struct lcl_cache_pkt_hdr);
  696. unpacked_sz = sizeof(struct lcl_cache_pkt_hdr);
  697. ints_network_to_host(hdr, lcl_cache_pkt_hdr_iinfo);
  698. *counter = 0;
  699. if (hdr->tot_caches > ANDNA_MAX_HOSTNAMES)
  700. ERROR_FINISH(*counter, -1, finish);
  701. *counter = 0;
  702. if (hdr->tot_caches) {
  703. for (i = 0; i < hdr->tot_caches; i++) {
  704. unpacked_sz += LCL_CACHE_BODY_PACK_SZ(0);
  705. if (unpacked_sz > pack_sz)
  706. ERROR_FINISH(*counter, -1, finish);
  707. slen = strlen(buf + sizeof(u_short) + sizeof(time_t)) + 1;
  708. if (slen > ANDNA_MAX_HNAME_LEN ||
  709. (unpacked_sz += slen) > pack_sz)
  710. ERROR_FINISH(*counter, -1, finish);
  711. ints_network_to_host(buf, lcl_cache_pkt_body_iinfo);
  712. alcl = xzalloc(sizeof(lcl_cache));
  713. bufget(&alcl->hname_updates, sizeof(u_short));
  714. bufget(&alcl->timestamp, sizeof(time_t));
  715. alcl->hostname = xstrdup(buf);
  716. alcl->hash = andna_32bit_hash(alcl->hostname);
  717. buf += slen;
  718. clist_add(&alcl_head, counter, alcl);
  719. }
  720. }
  721. finish:
  722. return alcl_head;
  723. }
  724. /*
  725. * pack_andna_cache_queue
  726. *
  727. * It packs an andna_cache_queue struct. The package is stored in `pack' which
  728. * has `tot_pack_sz' allocated bytes.
  729. * `acq' is the struct which will be packed.
  730. * `pack_type' is equal to ACACHE_PACK_FILE or ACACHE_PACK_PKT, it specify if
  731. * the package will be stored in a file or will be sent over a network.
  732. *
  733. * The number of bytes written in `pack' is returned.
  734. */
  735. int
  736. pack_andna_cache_queue(char *pack, size_t tot_pack_sz,
  737. andna_cache_queue * acq, int pack_type)
  738. {
  739. char *buf = pack;
  740. u_int t;
  741. int pack_sz = 0;
  742. if (pack_type == ACACHE_PACK_PKT)
  743. t = time(0) - acq->timestamp;
  744. else
  745. t = acq->timestamp;
  746. bufput(&t, sizeof(uint32_t));
  747. bufput(&acq->hname_updates, sizeof(u_short));
  748. bufput(&acq->pubkey, ANDNA_PKEY_LEN);
  749. bufput(&acq->snsd_counter, sizeof(u_short));
  750. pack_sz += ACQ_BODY_PACK_SZ;
  751. ints_host_to_network(pack, acq_body_iinfo);
  752. pack_sz += snsd_pack_all_services(buf, tot_pack_sz, acq->service);
  753. return pack_sz;
  754. }
  755. /*
  756. * pack_single_andna_cache
  757. *
  758. * It packs an andna_cache struct. The package is stored in `pack' which
  759. * has `tot_pack_sz' allocated bytes.
  760. * `ac' is the struct which will be packed.
  761. * `pack_type' is equal to ACACHE_PACK_FILE or ACACHE_PACK_PKT, it specify if
  762. * the package will be stored in a file or will be sent over a network.
  763. *
  764. * The number of bytes written in `pack' is returned.
  765. */
  766. int
  767. pack_single_andna_cache(char *pack, size_t tot_pack_sz,
  768. andna_cache * ac, int pack_type)
  769. {
  770. andna_cache_queue *acq;
  771. char *buf = pack;
  772. int pack_sz = 0;
  773. size_t psz;
  774. bufput(ac->hash, ANDNA_HASH_SZ);
  775. bufput(&ac->flags, sizeof(char));
  776. bufput(&ac->queue_counter, sizeof(u_short));
  777. pack_sz += ACACHE_BODY_PACK_SZ;
  778. ints_host_to_network(pack, andna_cache_body_iinfo);
  779. acq = ac->acq;
  780. list_for(acq) {
  781. psz = pack_andna_cache_queue(buf, tot_pack_sz, acq, pack_type);
  782. buf += psz;
  783. pack_sz += psz;
  784. tot_pack_sz -= psz;
  785. }
  786. return pack_sz;
  787. }
  788. /*
  789. * pack_andna_cache
  790. *
  791. * It packs the entire andna cache linked list that starts with
  792. * the head `acache'.
  793. * The size of the pack is stored in `pack_sz'.
  794. * `pack_type' specifies if the package will be saved in a file or sent over
  795. * the net, it is equal to ACACHE_PACK_FILE or to ACACHE_PACK_PKT.
  796. *
  797. * The pointer to the newly allocated pack is returned.
  798. * The pack is written in network order.
  799. */
  800. char *
  801. pack_andna_cache(andna_cache * acache, size_t * pack_sz, int pack_type)
  802. {
  803. struct andna_cache_pkt_hdr hdr;
  804. andna_cache *ac = acache;
  805. andna_cache_queue *acq;
  806. char *pack, *buf;
  807. size_t sz, free_sz, acq_sz, service_sz, psz;
  808. /* Calculate the pack size */
  809. ac = acache;
  810. hdr.tot_caches = 0;
  811. sz = sizeof(struct andna_cache_pkt_hdr);
  812. list_for(ac) {
  813. acq = ac->acq;
  814. acq_sz = 0;
  815. list_for(acq) {
  816. service_sz = SNSD_SERVICE_LLIST_PACK_SZ(acq->service);
  817. acq_sz = ACQ_PACK_SZ(service_sz);
  818. }
  819. sz += ACACHE_PACK_SZ(acq_sz);
  820. hdr.tot_caches++;
  821. }
  822. free_sz = sz;
  823. buf = pack = xmalloc(sz);
  824. /* Write the header of the package */
  825. bufput(&hdr, sizeof(struct andna_cache_pkt_hdr));
  826. free_sz -= sizeof(struct andna_cache_pkt_hdr);
  827. ints_host_to_network(pack, andna_cache_pkt_hdr_iinfo);
  828. if (!hdr.tot_caches)
  829. goto finish;
  830. /* Pack the rest of the andna_cache */
  831. ac = acache;
  832. list_for(ac) {
  833. psz = pack_single_andna_cache(buf, free_sz, ac, pack_type);
  834. buf += psz;
  835. free_sz -= psz;
  836. }
  837. finish:
  838. *pack_sz = sz;
  839. return pack;
  840. }
  841. /*
  842. * unpack_acq_llist
  843. *
  844. * ac->queue_counter must contain the number of acq structs contained in the
  845. * package.
  846. *
  847. * `*unpacked_sz' is incremented by the number of unpacked bytes.
  848. *
  849. * `pack_type' specifies if the package will be saved in a file or sent over
  850. * the net, it is equal to ACACHE_PACK_FILE or to ACACHE_PACK_PKT.
  851. */
  852. andna_cache_queue *
  853. unpack_acq_llist(char *pack, size_t pack_sz, size_t * unpacked_sz,
  854. andna_cache * ac, int pack_type)
  855. {
  856. andna_cache_queue *acq = 0;
  857. int e, tmp_counter = 0;
  858. u_short snsd_counter;
  859. time_t cur_t;
  860. char *buf;
  861. cur_t = time(0);
  862. buf = pack;
  863. for (e = 0; e < ac->queue_counter; e++) {
  864. acq = xzalloc(sizeof(andna_cache_queue));
  865. ints_network_to_host(buf, acq_body_iinfo);
  866. bufget(&acq->timestamp, sizeof(uint32_t));
  867. if (pack_type == ACACHE_PACK_PKT)
  868. acq->timestamp = cur_t - acq->timestamp;
  869. bufget(&acq->hname_updates, sizeof(u_short));
  870. bufget(&acq->pubkey, ANDNA_PKEY_LEN);
  871. bufget(&acq->snsd_counter, sizeof(u_short));
  872. pack_sz -= ACACHE_BODY_PACK_SZ;
  873. (*unpacked_sz) += ACACHE_BODY_PACK_SZ;
  874. acq->service = snsd_unpack_all_service(buf, pack_sz, unpacked_sz,
  875. &snsd_counter);
  876. if (acq->snsd_counter != snsd_counter) {
  877. debug(DBG_SOFT, ERROR_MSG "unpack_acq:"
  878. "snsd_counter (%h) != snsd_counter (%h)",
  879. ERROR_POS, acq->snsd_counter, snsd_counter);
  880. xfree(acq);
  881. list_destroy(ac->acq);
  882. return 0;
  883. }
  884. clist_add(&ac->acq, &tmp_counter, acq);
  885. }
  886. return ac->acq;
  887. }
  888. /*
  889. * unpack_andna_cache
  890. *
  891. * Unpacks a packed andna cache linked list and returns the
  892. * its head.
  893. * `counter' is set to the number of struct in the llist.
  894. * `pack_type' specifies if the package will be saved in a file or sent over
  895. * the net, it is equal to ACACHE_PACK_FILE or to ACACHE_PACK_PKT.
  896. *
  897. * On error 0 is returned and `*counter' is set to -1.
  898. * Warning: `pack' will be modified during the unpacking.
  899. */
  900. andna_cache *
  901. unpack_andna_cache(char *pack, size_t pack_sz, int *counter, int pack_type)
  902. {
  903. struct andna_cache_pkt_hdr *hdr;
  904. andna_cache *ac, *ac_head = 0;
  905. char *buf;
  906. size_t sz = 0;
  907. int i, err = 0;
  908. size_t unpacked_sz = 0;
  909. hdr = (struct andna_cache_pkt_hdr *) pack;
  910. ints_network_to_host(hdr, andna_cache_pkt_hdr_iinfo);
  911. *counter = 0;
  912. if (!hdr->tot_caches)
  913. ERROR_FINISH(err, 1, finish);
  914. buf = pack + sizeof(struct andna_cache_pkt_hdr);
  915. sz = sizeof(struct andna_cache_pkt_hdr);
  916. for (i = 0; i < hdr->tot_caches; i++) {
  917. sz += ACACHE_BODY_PACK_SZ;
  918. if (sz > pack_sz)
  919. ERROR_FINISH(err, 1, finish); /* overflow */
  920. ac = xzalloc(sizeof(andna_cache));
  921. ints_network_to_host(buf, andna_cache_body_iinfo);
  922. bufget(ac->hash, ANDNA_HASH_SZ);
  923. bufget(&ac->flags, sizeof(char));
  924. bufget(&ac->queue_counter, sizeof(u_short));
  925. sz += ACQ_PACK_SZ(0) * ac->queue_counter;
  926. if (sz > pack_sz)
  927. ERROR_FINISH(err, 1, finish); /* overflow */
  928. unpacked_sz += ACACHE_BODY_PACK_SZ;
  929. ac->acq =
  930. unpack_acq_llist(buf, pack_sz - unpacked_sz, &unpacked_sz, ac,
  931. pack_type);
  932. clist_add(&ac_head, counter, ac);
  933. }
  934. finish:
  935. if (err)
  936. *counter = -1;
  937. return ac_head;
  938. }
  939. /*
  940. * pack_counter_cache: packs the entire counter cache linked list that starts
  941. * with the head `counter'. The size of the pack is stored in `pack_sz'.
  942. * The pointer to the newly allocated pack is returned.
  943. * The pack will be in network order.
  944. */
  945. char *
  946. pack_counter_cache(counter_c * countercache, size_t * pack_sz)
  947. {
  948. struct counter_c_pkt_hdr hdr;
  949. counter_c *cc = countercache;
  950. counter_c_hashes *cch;
  951. char *pack, *buf, *p;
  952. size_t sz;
  953. time_t cur_t;
  954. uint32_t t;
  955. /* Calculate the pack size */
  956. hdr.tot_caches = 0;
  957. sz = sizeof(struct counter_c_pkt_hdr);
  958. list_for(cc) {
  959. sz += COUNTER_CACHE_PACK_SZ(cc->hashes);
  960. hdr.tot_caches++;
  961. }
  962. pack = xmalloc(sz);
  963. memcpy(pack, &hdr, sizeof(struct counter_c_pkt_hdr));
  964. ints_host_to_network(pack, counter_c_pkt_hdr_iinfo);
  965. if (hdr.tot_caches) {
  966. cur_t = time(0);
  967. buf = pack + sizeof(struct counter_c_pkt_hdr);
  968. cc = countercache;
  969. list_for(cc) {
  970. p = buf;
  971. bufput(cc->pubkey, ANDNA_PKEY_LEN);
  972. bufput(&cc->flags, sizeof(char));
  973. bufput(&cc->hashes, sizeof(u_short));
  974. ints_host_to_network(p, counter_c_body_iinfo);
  975. cch = cc->cch;
  976. list_for(cch) {
  977. p = buf;
  978. t = cur_t - cch->timestamp;
  979. bufput(&t, sizeof(uint32_t));
  980. bufput(&cch->hname_updates, sizeof(u_short));
  981. bufput(cch->hash, ANDNA_HASH_SZ);
  982. ints_host_to_network(p, counter_c_hashes_body_iinfo);
  983. }
  984. }
  985. }
  986. *pack_sz = sz;
  987. return pack;
  988. }
  989. /*
  990. * unpack_counter_cache
  991. *
  992. * Unpacks a packed counter cache linked list and returns the its head.
  993. * `counter' is set to the number of struct in the llist.
  994. *
  995. * On error 0 is returned and `*counter' is set to -1.
  996. *
  997. * Note: `pack' will be modified during the unpacking.
  998. */
  999. counter_c *
  1000. unpack_counter_cache(char *pack, size_t pack_sz, int *counter)
  1001. {
  1002. struct counter_c_pkt_hdr *hdr;
  1003. counter_c *cc, *cc_head = 0;
  1004. counter_c_hashes *cch;
  1005. char *buf;
  1006. size_t sz;
  1007. int i, e, fake_int = 0;
  1008. time_t cur_t;
  1009. hdr = (struct counter_c_pkt_hdr *) pack;
  1010. ints_network_to_host(hdr, counter_c_pkt_hdr_iinfo);
  1011. *counter = 0;
  1012. if (hdr->tot_caches) {
  1013. cur_t = time(0);
  1014. buf = pack + sizeof(struct counter_c_pkt_hdr);
  1015. sz = sizeof(struct counter_c_pkt_hdr);
  1016. for (i = 0; i < hdr->tot_caches; i++) {
  1017. sz += COUNTER_CACHE_BODY_PACK_SZ;
  1018. if (sz > pack_sz)
  1019. /* We don't want to overflow */
  1020. ERROR_FINISH(*counter, -1, finish);
  1021. cc = xzalloc(sizeof(counter_c));
  1022. ints_network_to_host(buf, counter_c_body_iinfo);
  1023. bufget(cc->pubkey, ANDNA_PKEY_LEN);
  1024. bufget(&cc->flags, sizeof(char));
  1025. bufget(&cc->hashes, sizeof(u_short));
  1026. sz += COUNTER_CACHE_HASHES_PACK_SZ * cc->hashes;
  1027. if (sz > pack_sz)
  1028. /* bleah */
  1029. ERROR_FINISH(*counter, -1, finish);
  1030. for (e = 0; e < cc->hashes; e++) {
  1031. cch = xzalloc(sizeof(counter_c_hashes));
  1032. ints_network_to_host(buf, counter_c_hashes_body_iinfo);
  1033. cch->timestamp = 0;
  1034. bufget(&cch->timestamp, sizeof(uint32_t));
  1035. cch->timestamp = cur_t - cch->timestamp;
  1036. bufget(&cch->hname_updates, sizeof(u_short));
  1037. bufget(cch->hash, ANDNA_HASH_SZ);
  1038. clist_add(&cc->cch, &fake_int, cch);
  1039. }
  1040. clist_add(&cc_head, counter, cc);
  1041. }
  1042. }
  1043. finish:
  1044. return cc_head;
  1045. }
  1046. /*
  1047. * pack_rh_cache
  1048. *
  1049. * It packs the entire resolved hnames cache linked list that starts
  1050. * with the head `rhcache'. The size of the pack is stored in `pack_sz'.
  1051. * The pointer to the newly allocated pack is returned.
  1052. * The pack will be in network order.
  1053. */
  1054. char *
  1055. pack_rh_cache(rh_cache * rhcache, size_t * pack_sz)
  1056. {
  1057. struct rh_cache_pkt_hdr rh_hdr;
  1058. rh_cache *rhc = rhcache;
  1059. size_t tot_pack_sz = 0, service_sz;
  1060. char *pack, *buf;
  1061. rh_hdr.tot_caches = 0;
  1062. tot_pack_sz = sizeof(struct rh_cache_pkt_hdr);
  1063. /* Calculate the final pack size */
  1064. list_for(rhc) {
  1065. service_sz = SNSD_SERVICE_LLIST_PACK_SZ(rhc->service);
  1066. tot_pack_sz += RH_CACHE_BODY_PACK_SZ(service_sz);
  1067. rh_hdr.tot_caches++;
  1068. }
  1069. *pack_sz = tot_pack_sz;
  1070. buf = pack = xmalloc(tot_pack_sz);
  1071. bufput(&rh_hdr, sizeof(struct rh_cache_pkt_hdr));
  1072. tot_pack_sz -= sizeof(struct rh_cache_pkt_hdr);
  1073. ints_host_to_network(pack, rh_cache_pkt_hdr_iinfo);
  1074. if (rh_hdr.tot_caches) {
  1075. rhc = rhcache;
  1076. list_for(rhc) {
  1077. bufput(&rhc->hash, sizeof(u_int));
  1078. bufput(&rhc->flags, sizeof(char));
  1079. bufput(&rhc->timestamp, sizeof(time_t));
  1080. tot_pack_sz -= RH_CACHE_BODY_PACK_SZ(0);
  1081. tot_pack_sz -= snsd_pack_all_services(buf, tot_pack_sz,
  1082. rhc->service);
  1083. /* host -> network order */
  1084. ints_host_to_network(buf, rh_cache_pkt_body_iinfo);
  1085. }
  1086. }
  1087. return pack;
  1088. }
  1089. /*
  1090. * unpack_rh_cache
  1091. *
  1092. * Unpacks a packed resolved hnames cache linked list and returns its head.
  1093. * `counter' is set to the number of struct in the llist.
  1094. *
  1095. * On error 0 is returned and `*counter' is set to -1.
  1096. *
  1097. * Note: `pack' will be modified during the unpacking.
  1098. */
  1099. rh_cache *
  1100. unpack_rh_cache(char *pack, size_t pack_sz, int *counter)
  1101. {
  1102. struct rh_cache_pkt_hdr *hdr;
  1103. rh_cache *rhc = 0, *rhc_head = 0;
  1104. char *buf;
  1105. size_t unpacked_sz = 0;
  1106. int i = 0;
  1107. hdr = (struct rh_cache_pkt_hdr *) pack;
  1108. ints_network_to_host(hdr, rh_cache_pkt_hdr_iinfo);
  1109. *counter = 0;
  1110. if (hdr->tot_caches > ANDNA_MAX_RHC_HNAMES)
  1111. ERROR_FINISH(*counter, -1, finish);
  1112. *counter = 0;
  1113. if (hdr->tot_caches) {
  1114. buf = pack + sizeof(struct rh_cache_pkt_hdr);
  1115. unpacked_sz = sizeof(struct rh_cache_pkt_hdr);
  1116. for (i = 0; i < hdr->tot_caches; i++) {
  1117. unpacked_sz += RH_CACHE_BODY_PACK_SZ(0);
  1118. if (unpacked_sz > pack_sz)
  1119. ERROR_FINISH(*counter, -1, finish);
  1120. ints_network_to_host(buf, rh_cache_pkt_body_iinfo);
  1121. rhc = xzalloc(sizeof(rh_cache));
  1122. bufget(&rhc->hash, sizeof(u_int));
  1123. bufget(&rhc->flags, sizeof(char));
  1124. bufget(&rhc->timestamp, sizeof(time_t));
  1125. rhc->service = snsd_unpack_all_service(buf, pack_sz,
  1126. &unpacked_sz, 0);
  1127. clist_add(&rhc_head, counter, rhc);
  1128. }
  1129. }
  1130. finish:
  1131. return rhc_head;
  1132. }
  1133. /*
  1134. *
  1135. * * * * Save/Load functions * * *
  1136. *
  1137. */
  1138. /*
  1139. * save_lcl_keyring: saves a local cache keyring in the specified `file'.
  1140. */
  1141. int
  1142. save_lcl_keyring(lcl_cache_keyring * keyring, char *file)
  1143. {
  1144. FILE *fd;
  1145. size_t pack_sz;
  1146. char *pack;
  1147. /*Pack! */
  1148. pack = pack_lcl_keyring(keyring, &pack_sz);
  1149. if (!pack_sz || !pack)
  1150. return 0;
  1151. if ((fd = fopen(file, "w")) == NULL) {
  1152. error("Cannot save the lcl_keyring in %s: %s", file,
  1153. strerror(errno));
  1154. return -1;
  1155. }
  1156. /*Write! */
  1157. fwrite(pack, pack_sz, 1, fd);
  1158. xfree(pack);
  1159. fclose(fd);
  1160. return 0;
  1161. }
  1162. /*
  1163. * load_lcl_keyring
  1164. *
  1165. * loads from `file' a local cache keyring and restores in it the RSA keys.
  1166. *
  1167. * On error -1 is returned.
  1168. */
  1169. int
  1170. load_lcl_keyring(lcl_cache_keyring * keyring, char *file)
  1171. {
  1172. FILE *fd;
  1173. char *pack = 0;
  1174. size_t pack_sz;
  1175. int ret = 0;
  1176. if (!(fd = fopen(file, "r"))) {
  1177. error("Cannot load the lcl_keyring from %s: %s", file,
  1178. strerror(errno));
  1179. return -1;
  1180. }
  1181. fseek(fd, 0, SEEK_END);
  1182. pack_sz = ftell(fd);
  1183. rewind(fd);
  1184. pack = xmalloc(pack_sz);
  1185. if (!fread(pack, pack_sz, 1, fd))
  1186. ERROR_FINISH(ret, -1, finish);
  1187. ret = unpack_lcl_keyring(keyring, pack, pack_sz);
  1188. finish:
  1189. if (pack)
  1190. xfree(pack);
  1191. fclose(fd);
  1192. if (ret < 0)
  1193. debug(DBG_NORMAL, "Malformed or empty lcl_keyring file. "
  1194. "Aborting load_lcl_keyring().");
  1195. return ret;
  1196. }
  1197. /*
  1198. * save_lcl_cache: saves a local cache linked list in the specified `file'.
  1199. */
  1200. int
  1201. save_lcl_cache(lcl_cache * lcl, char *file)
  1202. {
  1203. FILE *fd;
  1204. size_t pack_sz;
  1205. char *pack;
  1206. /*Pack! */
  1207. pack = pack_lcl_cache(lcl, &pack_sz);
  1208. if (!pack_sz || !pack)
  1209. return 0;
  1210. if ((fd = fopen(file, "w")) == NULL) {
  1211. error("Cannot save the lcl_cache in %s: %s", file,
  1212. strerror(errno));
  1213. return -1;
  1214. }
  1215. /*Write! */
  1216. fwrite(pack, pack_sz, 1, fd);
  1217. xfree(pack);
  1218. fclose(fd);
  1219. return 0;
  1220. }
  1221. /*
  1222. * load_lcl_cache: loads from `file' a local cache list and returns the head
  1223. * of the newly allocated llist. In `counter' it is stored the number of
  1224. * structs of the llist.
  1225. * On error 0 is returned.
  1226. */
  1227. lcl_cache *
  1228. load_lcl_cache(char *file, int *counter)
  1229. {
  1230. lcl_cache *lcl = 0;
  1231. FILE *fd;
  1232. char *pack = 0;
  1233. size_t pack_sz;
  1234. if (!(fd = fopen(file, "r"))) {
  1235. error("Cannot load the lcl_cache from %s: %s", file,
  1236. strerror(errno));
  1237. return 0;
  1238. }
  1239. fseek(fd, 0, SEEK_END);
  1240. pack_sz = ftell(fd);
  1241. rewind(fd);
  1242. pack = xmalloc(pack_sz);
  1243. if (!fread(pack, pack_sz, 1, fd))
  1244. goto finish;
  1245. lcl = unpack_lcl_cache(pack, pack_sz, counter);
  1246. finish:
  1247. if (pack)
  1248. xfree(pack);
  1249. fclose(fd);
  1250. if (!lcl && counter < 0)
  1251. error("Malformed lcl_cache file (%s)"
  1252. "Aborting load_lcl_cache().", file);
  1253. return lcl;
  1254. }
  1255. /*
  1256. * save_andna_cache: saves an andna cache linked list in the `file' specified
  1257. */
  1258. int
  1259. save_andna_cache(andna_cache * acache, char *file)
  1260. {
  1261. FILE *fd;
  1262. size_t pack_sz;
  1263. char *pack;
  1264. /*Pack! */
  1265. pack = pack_andna_cache(acache, &pack_sz, ACACHE_PACK_FILE);
  1266. if (!pack_sz || !pack)
  1267. return 0;
  1268. if ((fd = fopen(file, "w")) == NULL) {
  1269. error("Cannot save the andna_cache in %s: %s", file,
  1270. strerror(errno));
  1271. return -1;
  1272. }
  1273. /*Write! */
  1274. fwrite(pack, pack_sz, 1, fd);
  1275. xfree(pack);
  1276. fclose(fd);
  1277. return 0;
  1278. }
  1279. /*
  1280. * load_andna_cache: loads from `file' an andna cache list and returns the head
  1281. * of the newly allocated llist. In `counter' it is stored the number of
  1282. * list's structs.
  1283. * On error 0 is returned.
  1284. */
  1285. andna_cache *
  1286. load_andna_cache(char *file, int *counter)
  1287. {
  1288. andna_cache *acache = 0;
  1289. FILE *fd;
  1290. char *pack = 0;
  1291. size_t pack_sz;
  1292. if ((fd = fopen(file, "r")) == NULL) {
  1293. error("Cannot load the andna_cache from %s: %s", file,
  1294. strerror(errno));
  1295. return 0;
  1296. }
  1297. fseek(fd, 0, SEEK_END);
  1298. pack_sz = ftell(fd);
  1299. rewind(fd);
  1300. pack = xmalloc(pack_sz);
  1301. if (!fread(pack, pack_sz, 1, fd))
  1302. goto finish;
  1303. acache = unpack_andna_cache(pack, pack_sz, counter, ACACHE_PACK_FILE);
  1304. finish:
  1305. if (pack)
  1306. xfree(pack);
  1307. fclose(fd);
  1308. if (!acache && counter < 0)
  1309. error("Malformed andna_cache file."
  1310. " Aborting load_andna_cache().");
  1311. else if (!acache)
  1312. debug(DBG_NORMAL, "Empty andna_cache file.");
  1313. return acache;
  1314. }
  1315. /*
  1316. * save_counter_c: saves a counter cache linked list in the `file' specified
  1317. */
  1318. int
  1319. save_counter_c(counter_c * countercache, char *file)
  1320. {
  1321. FILE *fd;
  1322. size_t pack_sz;
  1323. char *pack;
  1324. /*Pack! */
  1325. pack = pack_counter_cache(countercache, &pack_sz);
  1326. if (!pack_sz || !pack)
  1327. return 0;
  1328. if ((fd = fopen(file, "w")) == NULL) {
  1329. error("Cannot save the counter_c in %s: %s", file,
  1330. strerror(errno));
  1331. return -1;
  1332. }
  1333. /*Write! */
  1334. fwrite(pack, pack_sz, 1, fd);
  1335. xfree(pack);
  1336. fclose(fd);
  1337. return 0;
  1338. }
  1339. /*
  1340. * load_counter_c: loads from `file' a counter cache list and returns the head
  1341. * of the newly allocated llist. In `counter' it is stored the number of
  1342. * list's structs.
  1343. * On error 0 is returned.
  1344. */
  1345. counter_c *
  1346. load_counter_c(char *file, int *counter)
  1347. {
  1348. counter_c *countercache = 0;
  1349. FILE *fd;
  1350. char *pack = 0;
  1351. size_t pack_sz;
  1352. if ((fd = fopen(file, "r")) == NULL) {
  1353. error("Cannot load the counter_c from %s: %s", file,
  1354. strerror(errno));
  1355. return 0;
  1356. }
  1357. fseek(fd, 0, SEEK_END);
  1358. pack_sz = ftell(fd);
  1359. rewind(fd);
  1360. pack = xmalloc(pack_sz);
  1361. if (!fread(pack, pack_sz, 1, fd))
  1362. goto finish;
  1363. countercache = unpack_counter_cache(pack, pack_sz, counter);
  1364. finish:
  1365. if (pack)
  1366. xfree(pack);
  1367. fclose(fd);
  1368. if (!countercache && counter < 0)
  1369. debug(DBG_NORMAL, "Malformed counter_c file (%s). "
  1370. "Aborting load_counter_c().", file);
  1371. return countercache;
  1372. }
  1373. /*
  1374. * save_rh_cache: saves the resolved hnames cache linked list `rh' in the
  1375. * `file' specified.
  1376. */
  1377. int
  1378. save_rh_cache(rh_cache * rh, char *file)
  1379. {
  1380. FILE *fd = 0;
  1381. size_t pack_sz;
  1382. char *pack;
  1383. /*Pack! */
  1384. pack = pack_rh_cache(rh, &pack_sz);
  1385. if (!pack_sz || !pack)
  1386. return 0;
  1387. if (!(fd = fopen(file, "w"))) {
  1388. error("Cannot save the rh_cache in %s: %s", file, strerror(errno));
  1389. return -1;
  1390. }
  1391. /*Write! */
  1392. fwrite(pack, pack_sz, 1, fd);
  1393. xfree(pack);
  1394. fclose(fd);
  1395. return 0;
  1396. }
  1397. /*
  1398. * load_rh_cache: loads from `file' a resolved hnames cache list and returns
  1399. * the head of the newly allocated llist. In `counter' it is stored the number
  1400. * of structs of the llist.
  1401. * On error 0 is returned.
  1402. */
  1403. rh_cache *
  1404. load_rh_cache(char *file, int *counter)
  1405. {
  1406. rh_cache *rh = 0;
  1407. FILE *fd;
  1408. char *pack = 0;
  1409. size_t pack_sz;
  1410. if ((fd = fopen(file, "r")) == NULL) {
  1411. error("Cannot load the rh_cache from %s: %s", file,
  1412. strerror(errno));
  1413. return 0;
  1414. }
  1415. fseek(fd, 0, SEEK_END);
  1416. pack_sz = ftell(fd);
  1417. rewind(fd);
  1418. pack = xmalloc(pack_sz);
  1419. if (!fread(pack, pack_sz, 1, fd))
  1420. goto finish;
  1421. rh = unpack_rh_cache(pack, pack_sz, counter);
  1422. finish:
  1423. if (pack)
  1424. xfree(pack);
  1425. fclose(fd);
  1426. if (!rh && counter < 0)
  1427. error("Malformed rh_cache file (%s). "
  1428. "Aborting load_rh_cache().", file);
  1429. return rh;
  1430. }
  1431. /*
  1432. * load_hostnames
  1433. *
  1434. * It reads the `file' specified and reads each line in it.
  1435. * The strings read are the hostnames that will be registered in andna.
  1436. * Only ANDNA_MAX_HOSTNAMES lines are read. Each line can be maximum of
  1437. * ANDNA_MAX_HNAME_LEN character long.
  1438. *
  1439. * This function updates automagically the old local cache that is pointed by
  1440. * `*old_alcl_head'. The hostnames that are no more present in the loaded
  1441. * `file' are discarded from the local cache.
  1442. * Since a new local cache is allocated and the old is destroyed, the new
  1443. * pointer to it is written in `*old_alcl_head'.
  1444. *
  1445. * The `old_alcl_counter' is updated too.
  1446. *
  1447. * This function shall be used each time the `file' changes.
  1448. *
  1449. * On error -1 is returned, otherwise 0 shall be the sacred value.
  1450. */
  1451. int
  1452. load_hostnames(char *file, lcl_cache ** old_alcl_head,
  1453. int *old_alcl_counter)
  1454. {
  1455. FILE *fd;
  1456. char buf[ANDNA_MAX_HNAME_LEN + 1];
  1457. size_t slen;
  1458. time_t cur_t, diff;
  1459. int i = 0;
  1460. lcl_cache *alcl, *old_alcl, *new_alcl_head = 0;
  1461. int new_alcl_counter = 0;
  1462. if ((fd = fopen(file, "r")) == NULL) {
  1463. error("Cannot load any hostnames from %s: %s", file,
  1464. strerror(errno));
  1465. return -1;
  1466. }
  1467. cur_t = time(0);
  1468. while (!feof(fd) && i < ANDNA_MAX_HOSTNAMES) {
  1469. setzero(buf, ANDNA_MAX_HNAME_LEN + 1);
  1470. fgets(buf, ANDNA_MAX_HNAME_LEN, fd);
  1471. if (feof(fd))
  1472. break;
  1473. if ((*buf) == '#' || (*buf) == '\n' || !(*buf)) {
  1474. /* Strip off the comment lines */
  1475. continue;
  1476. } else {
  1477. slen = strlen(buf);
  1478. if (buf[slen - 1] == '\n') {
  1479. /* Don't include the newline in the string */
  1480. buf[slen - 1] = '\0';
  1481. slen = strlen(buf);
  1482. }
  1483. /* Add the hname in the new local cache */
  1484. alcl = lcl_cache_new(buf);
  1485. clist_add(&new_alcl_head, &new_alcl_counter, alcl);
  1486. /*
  1487. * If there is an equal entry in the old lcl_cache and
  1488. * it isn't expired, copy the old data in the new
  1489. * struct.
  1490. */
  1491. old_alcl = lcl_cache_find_hname(*old_alcl_head,
  1492. alcl->hostname);
  1493. if (old_alcl) {
  1494. diff = cur_t - old_alcl->timestamp;
  1495. if (diff < ANDNA_EXPIRATION_TIME) {
  1496. alcl->timestamp = old_alcl->timestamp;
  1497. alcl->hname_updates = old_alcl->hname_updates;
  1498. }
  1499. }
  1500. i++;
  1501. }
  1502. }
  1503. /* Remove completely the old lcl_cache */
  1504. lcl_cache_destroy(*old_alcl_head, old_alcl_counter);
  1505. /* Update the pointers */
  1506. *old_alcl_head = new_alcl_head;
  1507. *old_alcl_counter = new_alcl_counter;
  1508. fclose(fd);
  1509. return 0;
  1510. }
  1511. /*
  1512. * load_snsd
  1513. *
  1514. * It loads the SNSD records to be registered from the given `file'.
  1515. * In the file there shall be one record per line, up to SNSD_MAX_RECORDS-1#
  1516. * records.
  1517. *
  1518. * Each line has to be written in the following format:
  1519. * hostname:snsd_hostname:service:priority:weight[:pub_key_file]
  1520. * or
  1521. * hostname:snsd_ip:service:priority:weight[:pub_key_file]
  1522. *
  1523. * The old records present in `alcl_head' will be deleted and substituted by
  1524. * the loaded ones.
  1525. *
  1526. * On error -1 is returned.
  1527. * If a syntax error is encountered in the file -2 is returned.
  1528. */
  1529. int
  1530. load_snsd(char *file, lcl_cache * alcl_head)
  1531. {
  1532. #define MAX_SNSD_LINE_SZ (ANDNA_MAX_HNAME_LEN*4)
  1533. FILE *fd;
  1534. size_t slen;
  1535. int line = 0, fields, e, service, nodes, ret = 0, err;
  1536. char buf[MAX_SNSD_LINE_SZ + 1], **records;
  1537. u_char proto, abort = 0;
  1538. lcl_cache *alcl;
  1539. snsd_service *sns;
  1540. snsd_prio *snp;
  1541. snsd_node *snd, snsd_node;
  1542. inet_prefix ip;
  1543. /* Delete all the old snsd records */
  1544. alcl = alcl_head;
  1545. list_for(alcl)
  1546. if (alcl->service)
  1547. snsd_service_llist_del(&alcl->service);
  1548. if ((fd = fopen(file, "r")) == NULL) {
  1549. error("Cannot open the snsd_nodes file from %s: %s",
  1550. file, strerror(errno));
  1551. return -1;
  1552. }
  1553. line = 1;
  1554. while (!feof(fd) && line <= SNSD_MAX_RECORDS - 1) {
  1555. setzero(buf, MAX_SNSD_LINE_SZ + 1);
  1556. fgets(buf, MAX_SNSD_LINE_SZ, fd);
  1557. if (feof(fd))
  1558. break;
  1559. if ((*buf) == '#' || (*buf) == '\n' || !(*buf)) {
  1560. /* Strip off the comment lines */
  1561. line++;
  1562. continue;
  1563. } else {
  1564. slen = strlen(buf);
  1565. if (buf[slen - 1] == '\n') {
  1566. /* Don't include the newline in the string */
  1567. buf[slen - 1] = '\0';
  1568. slen = strlen(buf);
  1569. }
  1570. records = split_string(buf, ":", &fields, MAX_SNSD_FIELDS,
  1571. ANDNA_MAX_HNAME_LEN * 2);
  1572. if (fields < MIN_SNSD_FIELDS) {
  1573. error("%s: Syntax error in line %d.\n"
  1574. " The correct syntax is:\n"
  1575. " \thostname:snsd_hostname:service:"
  1576. "priority:weight[:pub_key_file]\n"
  1577. " or\n"
  1578. " \thostname:snsd_ip:service:"
  1579. "priority:weight[:pub_key_file]", file, line);
  1580. ERROR_FINISH(abort, 1, skip_line);
  1581. }
  1582. /*
  1583. * hostname
  1584. */
  1585. alcl = lcl_cache_find_hname(alcl_head, records[0]);
  1586. if (!alcl) {
  1587. error("%s: line %d: The hostname \"%s\" doesn't"
  1588. " exist in your local cache.\n"
  1589. " Register it in the `andna_hostnames' file",
  1590. file, line, records[0]);
  1591. ERROR_FINISH(abort, 1, skip_line);
  1592. }
  1593. /*
  1594. * snsd record
  1595. */
  1596. if (str_to_inet(records[1], &ip) >= 0) {
  1597. inet_copy_ipdata_raw((u_int*)snsd_node.record, &ip);
  1598. snsd_node.flags = SNSD_NODE_IP;
  1599. } else {
  1600. hash_md5((u_char *) records[1], strlen(records[1]),
  1601. (u_char *) snsd_node.record);
  1602. snsd_node.flags = SNSD_NODE_HNAME;
  1603. }
  1604. if (!strncmp(records[0], records[1],
  1605. ANDNA_MAX_HNAME_LEN) && !strcmp(records[2], "0"))
  1606. snsd_node.flags = SNSD_NODE_MAIN_IP | SNSD_NODE_IP;
  1607. /***
  1608. * Parse service and protocol
  1609. */
  1610. err = str_to_snsd_service(records[2], &service, &proto);
  1611. if (err == -1)
  1612. error("%s: error in line %d: \"%s\""
  1613. " isn't a valid protocol\n", file, line, records[2]);
  1614. else if (err == -2)
  1615. error("%s: error in line %d: \"%s\""
  1616. " isn't a valid service\n", file, line, records[2]);
  1617. if (err < 0)
  1618. ERROR_FINISH(abort, 1, skip_line);
  1619. /**/
  1620. /* Store service and protocol */
  1621. sns = snsd_add_service(&alcl->service, service, proto);
  1622. /* priority */
  1623. snp = snsd_add_prio(&sns->prio, atoi(records[3]));
  1624. nodes = snsd_count_prio_nodes(sns->prio);
  1625. if (nodes >= SNSD_MAX_REC_SERV - 1) {
  1626. error("%s: The maximum number of records for"
  1627. " the service \"%s\" has been reached.\n"
  1628. " The maximum is %d records per service",
  1629. file, service, SNSD_MAX_REC_SERV);
  1630. ERROR_FINISH(abort, 1, skip_line);
  1631. }
  1632. /* node and weight */
  1633. snd = snsd_add_node(&snp->node, &alcl->snsd_counter,
  1634. SNSD_MAX_RECORDS - 1, snsd_node.record);
  1635. snd->weight = SNSD_WEIGHT(atoi(records[4]));
  1636. snd->flags |= snsd_node.flags;
  1637. /* pub_key_file
  1638. * TODO:
  1639. * if(fields >= 6)
  1640. * snd->pubkey=load_pubkey(records[5])
  1641. */
  1642. skip_line:
  1643. for (e = 0; e < fields; e++)
  1644. xfree(records[e]);
  1645. if (abort)
  1646. ERROR_FINISH(ret, -2, finish);
  1647. }
  1648. line++;
  1649. }
  1650. finish:
  1651. fclose(fd);
  1652. return ret;
  1653. }
  1654. /*
  1655. *
  1656. * * * * Modify /etc/resolv.conf * * *
  1657. *
  1658. */
  1659. /*
  1660. * add_resolv_conf: It opens `file' and write in the first line `hname' moving
  1661. * down the previous lines. The old `file' is backupped in `file'.bak.
  1662. * Example: add_resolv_conf("nameserver 127.0.0.1", "/etc/resolv.conf").
  1663. * Use del_resolv_conf to restore `file' with its backup.
  1664. * On error -1 is returned.
  1665. */
  1666. int
  1667. add_resolv_conf(char *hname, char *file)
  1668. {
  1669. FILE *fin = 0, /* `file' */
  1670. *fin_bak = 0, /* `file'.bak */
  1671. *fout = 0, /* The replaced `file' */
  1672. *fout_back = 0; /* The backup of `file' */
  1673. char *buf = 0, *p, *file_bk = 0;
  1674. size_t buf_sz;
  1675. int ret = 0;
  1676. /*
  1677. * Open and read `file'
  1678. */
  1679. if (!(fin = fopen(file, "r"))) {
  1680. error("add_resolv_conf: cannot load %s: %s", file,
  1681. strerror(errno));
  1682. ERROR_FINISH(ret, -1, finish);
  1683. }
  1684. /* Prepare the name of the backup file */
  1685. file_bk = xmalloc(strlen(file) + strlen(".bak") + 1);
  1686. *file_bk = 0;
  1687. strcpy(file_bk, file);
  1688. strcat(file_bk, ".bak");
  1689. reread_fin:
  1690. fseek(fin, 0, SEEK_END);
  1691. buf_sz = ftell(fin)+1;
  1692. rewind(fin);
  1693. buf = xmalloc(buf_sz);
  1694. memset(buf, 0, buf_sz);
  1695. if (!fread(buf, buf_sz, 1, fin)) {
  1696. error("add_resolv_conf: it wasn't possible to read the %s file",
  1697. file);
  1698. ERROR_FINISH(ret, -1, finish);
  1699. }
  1700. /*
  1701. * If there is already the `hname' string in the first line, try to
  1702. * read `file'.bak, if it doesn't exist do nothing.
  1703. */
  1704. if (buf_sz - 1 >= strlen(hname) && !strncmp(buf, hname, strlen(hname))) {
  1705. if (fin == fin_bak) {
  1706. /*
  1707. * We've already read `fin_bak', and it has
  1708. * the `hname' string in its first line too. Stop it.
  1709. */
  1710. goto finish;
  1711. }
  1712. debug(DBG_NORMAL, "add_resolv_conf: Reading %s instead", file_bk);
  1713. if (!(fin_bak = fopen(file_bk, "r")))
  1714. goto finish;
  1715. fclose(fin);
  1716. fin = fin_bak;
  1717. goto reread_fin;
  1718. }
  1719. /*
  1720. * Backup `file' in `file'.bak
  1721. */
  1722. if (!(fout_back = fopen(file_bk, "w"))) {
  1723. error
  1724. ("add_resolv_conf: cannot create a backup copy of %s in %s: %s",
  1725. file, file_bk, strerror(errno));
  1726. ERROR_FINISH(ret, -1, finish);
  1727. }
  1728. fwrite(buf, buf_sz, 1, fout_back);
  1729. /*
  1730. * Delete `file'
  1731. */
  1732. fclose(fin);
  1733. fin = 0;
  1734. unlink(file);
  1735. /*
  1736. * Add as a first line `hname' in `file'
  1737. */
  1738. if (!(fout = fopen(file, "w"))) {
  1739. error("add_resolv_conf: cannot reopen %s to overwrite it: %s",
  1740. file, strerror(errno));
  1741. ERROR_FINISH(ret, -1, finish);
  1742. }
  1743. fprintf(fout, "%s\n", hname);
  1744. p = buf;
  1745. while (*p) {
  1746. if (*p != '#')
  1747. fprintf(fout, "#");
  1748. while (*p) {
  1749. fprintf(fout, "%c", *p);
  1750. if (*p == '\n')
  1751. break;
  1752. p++;
  1753. }
  1754. if (!*p)
  1755. break;
  1756. p++;
  1757. }
  1758. /*fwrite(buf, buf_sz, 1, fout); */
  1759. finish:
  1760. if (buf)
  1761. xfree(buf);
  1762. if (file_bk)
  1763. xfree(file_bk);
  1764. if (fin)
  1765. fclose(fin);
  1766. if (fout)
  1767. fclose(fout);
  1768. if (fout_back)
  1769. fclose(fout_back);
  1770. return ret;
  1771. }
  1772. /*
  1773. * del_resolv_conf
  1774. *
  1775. * restores the old `file' modified by add_resolv_conf() by
  1776. * copying `file'.bak over `file'. If the `hname' string is present in
  1777. * `file'.bak it won't be written in `file'.
  1778. * On error it returns -1.
  1779. */
  1780. int
  1781. del_resolv_conf(char *hname, char *file)
  1782. {
  1783. FILE *fin = 0, *fout = 0;
  1784. char *buf = 0, *file_bk = 0, tmp_buf[128 + 1];
  1785. size_t buf_sz;
  1786. int ret = 0;
  1787. /*
  1788. * Open and read `file'.bak
  1789. */
  1790. file_bk = xmalloc(strlen(file) + strlen(".bak") + 1);
  1791. *file_bk = 0;
  1792. strcpy(file_bk, file);
  1793. strcat(file_bk, ".bak");
  1794. if (!(fin = fopen(file_bk, "r"))) {
  1795. /*error("del_resolv_conf: cannot load %s: %s", file_bk, strerror(errno)); */
  1796. ERROR_FINISH(ret, -1, finish);
  1797. }
  1798. fseek(fin, 0, SEEK_END);
  1799. buf_sz = ftell(fin);
  1800. rewind(fin);
  1801. if (!buf_sz) {
  1802. /* `file_bk' is empty, delete it */
  1803. unlink(file_bk);
  1804. ERROR_FINISH(ret, -1, finish);
  1805. }
  1806. buf = xzalloc(buf_sz);
  1807. while (fgets(tmp_buf, 128, fin)) {
  1808. /* Skip the line which is equal to `hname' */
  1809. if (!strncmp(tmp_buf, hname, strlen(hname)))
  1810. continue;
  1811. strcat(buf, tmp_buf);
  1812. }
  1813. /*
  1814. * Delete `file'
  1815. */
  1816. unlink(file);
  1817. /*
  1818. * Copy `file'.bak in `file'
  1819. */
  1820. if (!(fout = fopen(file, "w"))) {
  1821. error("del_resolv_conf: cannot copy %s in %s: %s", file_bk,
  1822. file, strerror(errno));
  1823. ERROR_FINISH(ret, -1, finish);
  1824. }
  1825. fprintf(fout, "%s", buf);
  1826. /*
  1827. * delete `file'.bak
  1828. */
  1829. fclose(fin);
  1830. fin = 0;
  1831. unlink(file_bk);
  1832. finish:
  1833. if (buf)
  1834. xfree(buf);
  1835. if (file_bk)
  1836. xfree(file_bk);
  1837. if (fin)
  1838. fclose(fin);
  1839. if (fout)
  1840. fclose(fout);
  1841. return ret;
  1842. }