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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  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, *body;
  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. body = buf;
  1078. bufput(&rhc->hash, sizeof(u_int));
  1079. bufput(&rhc->flags, sizeof(char));
  1080. bufput(&rhc->timestamp, sizeof(time_t));
  1081. tot_pack_sz -= RH_CACHE_BODY_PACK_SZ(0);
  1082. tot_pack_sz -= snsd_pack_all_services(buf, tot_pack_sz,
  1083. rhc->service);
  1084. /* host -> network order */
  1085. ints_host_to_network(buf, rh_cache_pkt_body_iinfo);
  1086. }
  1087. }
  1088. return pack;
  1089. }
  1090. /*
  1091. * unpack_rh_cache
  1092. *
  1093. * Unpacks a packed resolved hnames cache linked list and returns its head.
  1094. * `counter' is set to the number of struct in the llist.
  1095. *
  1096. * On error 0 is returned and `*counter' is set to -1.
  1097. *
  1098. * Note: `pack' will be modified during the unpacking.
  1099. */
  1100. rh_cache *
  1101. unpack_rh_cache(char *pack, size_t pack_sz, int *counter)
  1102. {
  1103. struct rh_cache_pkt_hdr *hdr;
  1104. rh_cache *rhc = 0, *rhc_head = 0;
  1105. char *buf;
  1106. size_t unpacked_sz = 0;
  1107. int i = 0;
  1108. hdr = (struct rh_cache_pkt_hdr *) pack;
  1109. ints_network_to_host(hdr, rh_cache_pkt_hdr_iinfo);
  1110. *counter = 0;
  1111. if (hdr->tot_caches > ANDNA_MAX_RHC_HNAMES)
  1112. ERROR_FINISH(*counter, -1, finish);
  1113. *counter = 0;
  1114. if (hdr->tot_caches) {
  1115. buf = pack + sizeof(struct rh_cache_pkt_hdr);
  1116. unpacked_sz = sizeof(struct rh_cache_pkt_hdr);
  1117. for (i = 0; i < hdr->tot_caches; i++) {
  1118. unpacked_sz += RH_CACHE_BODY_PACK_SZ(0);
  1119. if (unpacked_sz > pack_sz)
  1120. ERROR_FINISH(*counter, -1, finish);
  1121. ints_network_to_host(buf, rh_cache_pkt_body_iinfo);
  1122. rhc = xzalloc(sizeof(rh_cache));
  1123. bufget(&rhc->hash, sizeof(u_int));
  1124. bufget(&rhc->flags, sizeof(char));
  1125. bufget(&rhc->timestamp, sizeof(time_t));
  1126. rhc->service = snsd_unpack_all_service(buf, pack_sz,
  1127. &unpacked_sz, 0);
  1128. clist_add(&rhc_head, counter, rhc);
  1129. }
  1130. }
  1131. finish:
  1132. return rhc_head;
  1133. }
  1134. /*
  1135. *
  1136. * * * * Save/Load functions * * *
  1137. *
  1138. */
  1139. /*
  1140. * save_lcl_keyring: saves a local cache keyring in the specified `file'.
  1141. */
  1142. int
  1143. save_lcl_keyring(lcl_cache_keyring * keyring, char *file)
  1144. {
  1145. FILE *fd;
  1146. size_t pack_sz;
  1147. char *pack;
  1148. /*Pack! */
  1149. pack = pack_lcl_keyring(keyring, &pack_sz);
  1150. if (!pack_sz || !pack)
  1151. return 0;
  1152. if ((fd = fopen(file, "w")) == NULL) {
  1153. error("Cannot save the lcl_keyring in %s: %s", file,
  1154. strerror(errno));
  1155. return -1;
  1156. }
  1157. /*Write! */
  1158. fwrite(pack, pack_sz, 1, fd);
  1159. xfree(pack);
  1160. fclose(fd);
  1161. return 0;
  1162. }
  1163. /*
  1164. * load_lcl_keyring
  1165. *
  1166. * loads from `file' a local cache keyring and restores in it the RSA keys.
  1167. *
  1168. * On error -1 is returned.
  1169. */
  1170. int
  1171. load_lcl_keyring(lcl_cache_keyring * keyring, char *file)
  1172. {
  1173. FILE *fd;
  1174. char *pack = 0;
  1175. size_t pack_sz;
  1176. int ret = 0;
  1177. if (!(fd = fopen(file, "r"))) {
  1178. error("Cannot load the lcl_keyring from %s: %s", file,
  1179. strerror(errno));
  1180. return -1;
  1181. }
  1182. fseek(fd, 0, SEEK_END);
  1183. pack_sz = ftell(fd);
  1184. rewind(fd);
  1185. pack = xmalloc(pack_sz);
  1186. if (!fread(pack, pack_sz, 1, fd))
  1187. ERROR_FINISH(ret, -1, finish);
  1188. ret = unpack_lcl_keyring(keyring, pack, pack_sz);
  1189. finish:
  1190. if (pack)
  1191. xfree(pack);
  1192. fclose(fd);
  1193. if (ret < 0)
  1194. debug(DBG_NORMAL, "Malformed or empty lcl_keyring file. "
  1195. "Aborting load_lcl_keyring().");
  1196. return ret;
  1197. }
  1198. /*
  1199. * save_lcl_cache: saves a local cache linked list in the specified `file'.
  1200. */
  1201. int
  1202. save_lcl_cache(lcl_cache * lcl, char *file)
  1203. {
  1204. FILE *fd;
  1205. size_t pack_sz;
  1206. char *pack;
  1207. /*Pack! */
  1208. pack = pack_lcl_cache(lcl, &pack_sz);
  1209. if (!pack_sz || !pack)
  1210. return 0;
  1211. if ((fd = fopen(file, "w")) == NULL) {
  1212. error("Cannot save the lcl_cache in %s: %s", file,
  1213. strerror(errno));
  1214. return -1;
  1215. }
  1216. /*Write! */
  1217. fwrite(pack, pack_sz, 1, fd);
  1218. xfree(pack);
  1219. fclose(fd);
  1220. return 0;
  1221. }
  1222. /*
  1223. * load_lcl_cache: loads from `file' a local cache list and returns the head
  1224. * of the newly allocated llist. In `counter' it is stored the number of
  1225. * structs of the llist.
  1226. * On error 0 is returned.
  1227. */
  1228. lcl_cache *
  1229. load_lcl_cache(char *file, int *counter)
  1230. {
  1231. lcl_cache *lcl = 0;
  1232. FILE *fd;
  1233. char *pack = 0;
  1234. size_t pack_sz;
  1235. if (!(fd = fopen(file, "r"))) {
  1236. error("Cannot load the lcl_cache from %s: %s", file,
  1237. strerror(errno));
  1238. return 0;
  1239. }
  1240. fseek(fd, 0, SEEK_END);
  1241. pack_sz = ftell(fd);
  1242. rewind(fd);
  1243. pack = xmalloc(pack_sz);
  1244. if (!fread(pack, pack_sz, 1, fd))
  1245. goto finish;
  1246. lcl = unpack_lcl_cache(pack, pack_sz, counter);
  1247. finish:
  1248. if (pack)
  1249. xfree(pack);
  1250. fclose(fd);
  1251. if (!lcl && counter < 0)
  1252. error("Malformed lcl_cache file (%s)"
  1253. "Aborting load_lcl_cache().", file);
  1254. return lcl;
  1255. }
  1256. /*
  1257. * save_andna_cache: saves an andna cache linked list in the `file' specified
  1258. */
  1259. int
  1260. save_andna_cache(andna_cache * acache, char *file)
  1261. {
  1262. FILE *fd;
  1263. size_t pack_sz;
  1264. char *pack;
  1265. /*Pack! */
  1266. pack = pack_andna_cache(acache, &pack_sz, ACACHE_PACK_FILE);
  1267. if (!pack_sz || !pack)
  1268. return 0;
  1269. if ((fd = fopen(file, "w")) == NULL) {
  1270. error("Cannot save the andna_cache in %s: %s", file,
  1271. strerror(errno));
  1272. return -1;
  1273. }
  1274. /*Write! */
  1275. fwrite(pack, pack_sz, 1, fd);
  1276. xfree(pack);
  1277. fclose(fd);
  1278. return 0;
  1279. }
  1280. /*
  1281. * load_andna_cache: loads from `file' an andna cache list and returns the head
  1282. * of the newly allocated llist. In `counter' it is stored the number of
  1283. * list's structs.
  1284. * On error 0 is returned.
  1285. */
  1286. andna_cache *
  1287. load_andna_cache(char *file, int *counter)
  1288. {
  1289. andna_cache *acache = 0;
  1290. FILE *fd;
  1291. char *pack = 0;
  1292. size_t pack_sz;
  1293. if ((fd = fopen(file, "r")) == NULL) {
  1294. error("Cannot load the andna_cache from %s: %s", file,
  1295. strerror(errno));
  1296. return 0;
  1297. }
  1298. fseek(fd, 0, SEEK_END);
  1299. pack_sz = ftell(fd);
  1300. rewind(fd);
  1301. pack = xmalloc(pack_sz);
  1302. if (!fread(pack, pack_sz, 1, fd))
  1303. goto finish;
  1304. acache = unpack_andna_cache(pack, pack_sz, counter, ACACHE_PACK_FILE);
  1305. finish:
  1306. if (pack)
  1307. xfree(pack);
  1308. fclose(fd);
  1309. if (!acache && counter < 0)
  1310. error("Malformed andna_cache file."
  1311. " Aborting load_andna_cache().");
  1312. else if (!acache)
  1313. debug(DBG_NORMAL, "Empty andna_cache file.");
  1314. return acache;
  1315. }
  1316. /*
  1317. * save_counter_c: saves a counter cache linked list in the `file' specified
  1318. */
  1319. int
  1320. save_counter_c(counter_c * countercache, char *file)
  1321. {
  1322. FILE *fd;
  1323. size_t pack_sz;
  1324. char *pack;
  1325. /*Pack! */
  1326. pack = pack_counter_cache(countercache, &pack_sz);
  1327. if (!pack_sz || !pack)
  1328. return 0;
  1329. if ((fd = fopen(file, "w")) == NULL) {
  1330. error("Cannot save the counter_c in %s: %s", file,
  1331. strerror(errno));
  1332. return -1;
  1333. }
  1334. /*Write! */
  1335. fwrite(pack, pack_sz, 1, fd);
  1336. xfree(pack);
  1337. fclose(fd);
  1338. return 0;
  1339. }
  1340. /*
  1341. * load_counter_c: loads from `file' a counter cache list and returns the head
  1342. * of the newly allocated llist. In `counter' it is stored the number of
  1343. * list's structs.
  1344. * On error 0 is returned.
  1345. */
  1346. counter_c *
  1347. load_counter_c(char *file, int *counter)
  1348. {
  1349. counter_c *countercache = 0;
  1350. FILE *fd;
  1351. char *pack = 0;
  1352. size_t pack_sz;
  1353. if ((fd = fopen(file, "r")) == NULL) {
  1354. error("Cannot load the counter_c from %s: %s", file,
  1355. strerror(errno));
  1356. return 0;
  1357. }
  1358. fseek(fd, 0, SEEK_END);
  1359. pack_sz = ftell(fd);
  1360. rewind(fd);
  1361. pack = xmalloc(pack_sz);
  1362. if (!fread(pack, pack_sz, 1, fd))
  1363. goto finish;
  1364. countercache = unpack_counter_cache(pack, pack_sz, counter);
  1365. finish:
  1366. if (pack)
  1367. xfree(pack);
  1368. fclose(fd);
  1369. if (!countercache && counter < 0)
  1370. debug(DBG_NORMAL, "Malformed counter_c file (%s). "
  1371. "Aborting load_counter_c().", file);
  1372. return countercache;
  1373. }
  1374. /*
  1375. * save_rh_cache: saves the resolved hnames cache linked list `rh' in the
  1376. * `file' specified.
  1377. */
  1378. int
  1379. save_rh_cache(rh_cache * rh, char *file)
  1380. {
  1381. FILE *fd = 0;
  1382. size_t pack_sz;
  1383. char *pack;
  1384. /*Pack! */
  1385. pack = pack_rh_cache(rh, &pack_sz);
  1386. if (!pack_sz || !pack)
  1387. return 0;
  1388. if (!(fd = fopen(file, "w"))) {
  1389. error("Cannot save the rh_cache in %s: %s", file, strerror(errno));
  1390. return -1;
  1391. }
  1392. /*Write! */
  1393. fwrite(pack, pack_sz, 1, fd);
  1394. xfree(pack);
  1395. fclose(fd);
  1396. return 0;
  1397. }
  1398. /*
  1399. * load_rh_cache: loads from `file' a resolved hnames cache list and returns
  1400. * the head of the newly allocated llist. In `counter' it is stored the number
  1401. * of structs of the llist.
  1402. * On error 0 is returned.
  1403. */
  1404. rh_cache *
  1405. load_rh_cache(char *file, int *counter)
  1406. {
  1407. rh_cache *rh = 0;
  1408. FILE *fd;
  1409. char *pack = 0;
  1410. size_t pack_sz;
  1411. if ((fd = fopen(file, "r")) == NULL) {
  1412. error("Cannot load the rh_cache from %s: %s", file,
  1413. strerror(errno));
  1414. return 0;
  1415. }
  1416. fseek(fd, 0, SEEK_END);
  1417. pack_sz = ftell(fd);
  1418. rewind(fd);
  1419. pack = xmalloc(pack_sz);
  1420. if (!fread(pack, pack_sz, 1, fd))
  1421. goto finish;
  1422. rh = unpack_rh_cache(pack, pack_sz, counter);
  1423. finish:
  1424. if (pack)
  1425. xfree(pack);
  1426. fclose(fd);
  1427. if (!rh && counter < 0)
  1428. error("Malformed rh_cache file (%s). "
  1429. "Aborting load_rh_cache().", file);
  1430. return rh;
  1431. }
  1432. /*
  1433. * load_hostnames
  1434. *
  1435. * It reads the `file' specified and reads each line in it.
  1436. * The strings read are the hostnames that will be registered in andna.
  1437. * Only ANDNA_MAX_HOSTNAMES lines are read. Each line can be maximum of
  1438. * ANDNA_MAX_HNAME_LEN character long.
  1439. *
  1440. * This function updates automagically the old local cache that is pointed by
  1441. * `*old_alcl_head'. The hostnames that are no more present in the loaded
  1442. * `file' are discarded from the local cache.
  1443. * Since a new local cache is allocated and the old is destroyed, the new
  1444. * pointer to it is written in `*old_alcl_head'.
  1445. *
  1446. * The `old_alcl_counter' is updated too.
  1447. *
  1448. * This function shall be used each time the `file' changes.
  1449. *
  1450. * On error -1 is returned, otherwise 0 shall be the sacred value.
  1451. */
  1452. int
  1453. load_hostnames(char *file, lcl_cache ** old_alcl_head,
  1454. int *old_alcl_counter)
  1455. {
  1456. FILE *fd;
  1457. char buf[ANDNA_MAX_HNAME_LEN + 1];
  1458. size_t slen;
  1459. time_t cur_t, diff;
  1460. int i = 0;
  1461. lcl_cache *alcl, *old_alcl, *new_alcl_head = 0;
  1462. int new_alcl_counter = 0;
  1463. if ((fd = fopen(file, "r")) == NULL) {
  1464. error("Cannot load any hostnames from %s: %s", file,
  1465. strerror(errno));
  1466. return -1;
  1467. }
  1468. cur_t = time(0);
  1469. while (!feof(fd) && i < ANDNA_MAX_HOSTNAMES) {
  1470. setzero(buf, ANDNA_MAX_HNAME_LEN + 1);
  1471. fgets(buf, ANDNA_MAX_HNAME_LEN, fd);
  1472. if (feof(fd))
  1473. break;
  1474. if ((*buf) == '#' || (*buf) == '\n' || !(*buf)) {
  1475. /* Strip off the comment lines */
  1476. continue;
  1477. } else {
  1478. slen = strlen(buf);
  1479. if (buf[slen - 1] == '\n') {
  1480. /* Don't include the newline in the string */
  1481. buf[slen - 1] = '\0';
  1482. slen = strlen(buf);
  1483. }
  1484. /* Add the hname in the new local cache */
  1485. alcl = lcl_cache_new(buf);
  1486. clist_add(&new_alcl_head, &new_alcl_counter, alcl);
  1487. /*
  1488. * If there is an equal entry in the old lcl_cache and
  1489. * it isn't expired, copy the old data in the new
  1490. * struct.
  1491. */
  1492. old_alcl = lcl_cache_find_hname(*old_alcl_head,
  1493. alcl->hostname);
  1494. if (old_alcl) {
  1495. diff = cur_t - old_alcl->timestamp;
  1496. if (diff < ANDNA_EXPIRATION_TIME) {
  1497. alcl->timestamp = old_alcl->timestamp;
  1498. alcl->hname_updates = old_alcl->hname_updates;
  1499. }
  1500. }
  1501. i++;
  1502. }
  1503. }
  1504. /* Remove completely the old lcl_cache */
  1505. lcl_cache_destroy(*old_alcl_head, old_alcl_counter);
  1506. /* Update the pointers */
  1507. *old_alcl_head = new_alcl_head;
  1508. *old_alcl_counter = new_alcl_counter;
  1509. fclose(fd);
  1510. return 0;
  1511. }
  1512. /*
  1513. * load_snsd
  1514. *
  1515. * It loads the SNSD records to be registered from the given `file'.
  1516. * In the file there shall be one record per line, up to SNSD_MAX_RECORDS-1#
  1517. * records.
  1518. *
  1519. * Each line has to be written in the following format:
  1520. * hostname:snsd_hostname:service:priority:weight[:pub_key_file]
  1521. * or
  1522. * hostname:snsd_ip:service:priority:weight[:pub_key_file]
  1523. *
  1524. * The old records present in `alcl_head' will be deleted and substituted by
  1525. * the loaded ones.
  1526. *
  1527. * On error -1 is returned.
  1528. * If a syntax error is encountered in the file -2 is returned.
  1529. */
  1530. int
  1531. load_snsd(char *file, lcl_cache * alcl_head)
  1532. {
  1533. #define MAX_SNSD_LINE_SZ (ANDNA_MAX_HNAME_LEN*4)
  1534. FILE *fd;
  1535. size_t slen;
  1536. int line = 0, fields, e, service, nodes, ret = 0, err;
  1537. char buf[MAX_SNSD_LINE_SZ + 1], **records;
  1538. u_char proto, abort = 0;
  1539. lcl_cache *alcl;
  1540. snsd_service *sns;
  1541. snsd_prio *snp;
  1542. snsd_node *snd, snsd_node;
  1543. inet_prefix ip;
  1544. /* Delete all the old snsd records */
  1545. alcl = alcl_head;
  1546. list_for(alcl)
  1547. if (alcl->service)
  1548. snsd_service_llist_del(&alcl->service);
  1549. if ((fd = fopen(file, "r")) == NULL) {
  1550. error("Cannot open the snsd_nodes file from %s: %s",
  1551. file, strerror(errno));
  1552. return -1;
  1553. }
  1554. line = 1;
  1555. while (!feof(fd) && line <= SNSD_MAX_RECORDS - 1) {
  1556. setzero(buf, MAX_SNSD_LINE_SZ + 1);
  1557. fgets(buf, MAX_SNSD_LINE_SZ, fd);
  1558. if (feof(fd))
  1559. break;
  1560. if ((*buf) == '#' || (*buf) == '\n' || !(*buf)) {
  1561. /* Strip off the comment lines */
  1562. line++;
  1563. continue;
  1564. } else {
  1565. slen = strlen(buf);
  1566. if (buf[slen - 1] == '\n') {
  1567. /* Don't include the newline in the string */
  1568. buf[slen - 1] = '\0';
  1569. slen = strlen(buf);
  1570. }
  1571. records = split_string(buf, ":", &fields, MAX_SNSD_FIELDS,
  1572. ANDNA_MAX_HNAME_LEN * 2);
  1573. if (fields < MIN_SNSD_FIELDS) {
  1574. error("%s: Syntax error in line %d.\n"
  1575. " The correct syntax is:\n"
  1576. " \thostname:snsd_hostname:service:"
  1577. "priority:weight[:pub_key_file]\n"
  1578. " or\n"
  1579. " \thostname:snsd_ip:service:"
  1580. "priority:weight[:pub_key_file]", file, line);
  1581. ERROR_FINISH(abort, 1, skip_line);
  1582. }
  1583. /*
  1584. * hostname
  1585. */
  1586. alcl = lcl_cache_find_hname(alcl_head, records[0]);
  1587. if (!alcl) {
  1588. error("%s: line %d: The hostname \"%s\" doesn't"
  1589. " exist in your local cache.\n"
  1590. " Register it in the `andna_hostnames' file",
  1591. file, line, records[0]);
  1592. ERROR_FINISH(abort, 1, skip_line);
  1593. }
  1594. /*
  1595. * snsd record
  1596. */
  1597. if (str_to_inet(records[1], &ip) >= 0) {
  1598. inet_copy_ipdata_raw((u_int*)snsd_node.record, &ip);
  1599. snsd_node.flags = SNSD_NODE_IP;
  1600. } else {
  1601. hash_md5((u_char *) records[1], strlen(records[1]),
  1602. (u_char *) snsd_node.record);
  1603. snsd_node.flags = SNSD_NODE_HNAME;
  1604. }
  1605. if (!strncmp(records[0], records[1],
  1606. ANDNA_MAX_HNAME_LEN) && !strcmp(records[2], "0"))
  1607. snsd_node.flags = SNSD_NODE_MAIN_IP | SNSD_NODE_IP;
  1608. /***
  1609. * Parse service and protocol
  1610. */
  1611. err = str_to_snsd_service(records[2], &service, &proto);
  1612. if (err == -1)
  1613. error("%s: error in line %d: \"%s\""
  1614. " isn't a valid protocol\n", file, line, records[2]);
  1615. else if (err == -2)
  1616. error("%s: error in line %d: \"%s\""
  1617. " isn't a valid service\n", file, line, records[2]);
  1618. if (err < 0)
  1619. ERROR_FINISH(abort, 1, skip_line);
  1620. /**/
  1621. /* Store service and protocol */
  1622. sns = snsd_add_service(&alcl->service, service, proto);
  1623. /* priority */
  1624. snp = snsd_add_prio(&sns->prio, atoi(records[3]));
  1625. nodes = snsd_count_prio_nodes(sns->prio);
  1626. if (nodes >= SNSD_MAX_REC_SERV - 1) {
  1627. error("%s: The maximum number of records for"
  1628. " the service \"%s\" has been reached.\n"
  1629. " The maximum is %d records per service",
  1630. file, service, SNSD_MAX_REC_SERV);
  1631. ERROR_FINISH(abort, 1, skip_line);
  1632. }
  1633. /* node and weight */
  1634. snd = snsd_add_node(&snp->node, &alcl->snsd_counter,
  1635. SNSD_MAX_RECORDS - 1, snsd_node.record);
  1636. snd->weight = SNSD_WEIGHT(atoi(records[4]));
  1637. snd->flags |= snsd_node.flags;
  1638. /* pub_key_file
  1639. * TODO:
  1640. * if(fields >= 6)
  1641. * snd->pubkey=load_pubkey(records[5])
  1642. */
  1643. skip_line:
  1644. for (e = 0; e < fields; e++)
  1645. xfree(records[e]);
  1646. if (abort)
  1647. ERROR_FINISH(ret, -2, finish);
  1648. }
  1649. line++;
  1650. }
  1651. finish:
  1652. fclose(fd);
  1653. return ret;
  1654. }
  1655. /*
  1656. *
  1657. * * * * Modify /etc/resolv.conf * * *
  1658. *
  1659. */
  1660. /*
  1661. * add_resolv_conf: It opens `file' and write in the first line `hname' moving
  1662. * down the previous lines. The old `file' is backupped in `file'.bak.
  1663. * Example: add_resolv_conf("nameserver 127.0.0.1", "/etc/resolv.conf").
  1664. * Use del_resolv_conf to restore `file' with its backup.
  1665. * On error -1 is returned.
  1666. */
  1667. int
  1668. add_resolv_conf(char *hname, char *file)
  1669. {
  1670. FILE *fin = 0, /* `file' */
  1671. *fin_bak = 0, /* `file'.bak */
  1672. *fout = 0, /* The replaced `file' */
  1673. *fout_back = 0; /* The backup of `file' */
  1674. char *buf = 0, *p, *file_bk = 0;
  1675. size_t buf_sz;
  1676. int ret = 0;
  1677. /*
  1678. * Open and read `file'
  1679. */
  1680. if (!(fin = fopen(file, "r"))) {
  1681. error("add_resolv_conf: cannot load %s: %s", file,
  1682. strerror(errno));
  1683. ERROR_FINISH(ret, -1, finish);
  1684. }
  1685. /* Prepare the name of the backup file */
  1686. file_bk = xmalloc(strlen(file) + strlen(".bak") + 1);
  1687. *file_bk = 0;
  1688. strcpy(file_bk, file);
  1689. strcat(file_bk, ".bak");
  1690. reread_fin:
  1691. fseek(fin, 0, SEEK_END);
  1692. buf_sz = ftell(fin);
  1693. rewind(fin);
  1694. buf = xmalloc(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. }