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.

record.inc.php 70KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. <?php
  2. /* Poweradmin, a friendly web-based admin tool for PowerDNS.
  3. * See <http://www.poweradmin.org> for more details.
  4. *
  5. * Copyright 2007-2009 Rejo Zenger <rejo@zenger.nl>
  6. * Copyright 2010-2017 Poweradmin Development Team
  7. * <http://www.poweradmin.org/credits.html>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. require_once('templates.inc.php');
  23. /**
  24. * DNS record functions
  25. *
  26. * @package Poweradmin
  27. * @copyright 2007-2010 Rejo Zenger <rejo@zenger.nl>
  28. * @copyright 2010-2017 Poweradmin Development Team
  29. * @license http://opensource.org/licenses/GPL-3.0 GPL
  30. */
  31. /** Check if Zone ID exists
  32. *
  33. * @param int $zid Zone ID
  34. *
  35. * @return boolean|int Domain count or false on failure
  36. */
  37. function zone_id_exists($zid) {
  38. global $db;
  39. $query = "SELECT COUNT(id) FROM domains WHERE id = " . $db->quote($zid, 'integer');
  40. $count = $db->queryOne($query);
  41. if (PEAR::isError($count)) {
  42. error($count->getMessage());
  43. return false;
  44. }
  45. return $count;
  46. }
  47. /** Get Zone ID from Record ID
  48. *
  49. * @param int $rid Record ID
  50. *
  51. * @return int Zone ID
  52. */
  53. function get_zone_id_from_record_id($rid) {
  54. global $db;
  55. $query = "SELECT domain_id FROM records WHERE id = " . $db->quote($rid, 'integer');
  56. $zid = $db->queryOne($query);
  57. return $zid;
  58. }
  59. /** Count Zone Records for Zone ID
  60. *
  61. * @param int $zone_id Zone ID
  62. *
  63. * @return int Record count
  64. */
  65. function count_zone_records($zone_id) {
  66. global $db;
  67. $sqlq = "SELECT COUNT(id) FROM records WHERE domain_id = " . $db->quote($zone_id, 'integer') . " AND type IS NOT NULL";
  68. $record_count = $db->queryOne($sqlq);
  69. return $record_count;
  70. }
  71. /** Get SOA record content for Zone ID
  72. *
  73. * @param int $zone_id Zone ID
  74. *
  75. * @return string SOA content
  76. */
  77. function get_soa_record($zone_id) {
  78. global $db;
  79. $sqlq = "SELECT content FROM records WHERE type = " . $db->quote('SOA', 'text') . " AND domain_id = " . $db->quote($zone_id, 'integer');
  80. $result = $db->queryOne($sqlq);
  81. return $result;
  82. }
  83. /** Get SOA Serial Number
  84. *
  85. * @param string $soa_rec SOA record content
  86. *
  87. * @return string SOA serial
  88. */
  89. function get_soa_serial($soa_rec) {
  90. $soa = explode(" ", $soa_rec);
  91. return $soa[2];
  92. }
  93. /** Get Next Date
  94. *
  95. * @param string $curr_date Current date in YYYYMMDD format
  96. *
  97. * @return string Date +1 day
  98. */
  99. function get_next_date($curr_date) {
  100. $next_date = date('Ymd', strtotime('+1 day', strtotime($curr_date)));
  101. return $next_date;
  102. }
  103. /** Get Next Serial
  104. *
  105. * Zone transfer to zone slave(s) will occur only if the serial number
  106. * of the SOA RR is arithmetically greater that the previous one
  107. * (as defined by RFC-1982).
  108. *
  109. * The serial should be updated, unless:
  110. *
  111. * - the serial is set to "0", see http://doc.powerdns.com/types.html#id482176
  112. *
  113. * - set a fresh serial ONLY if the existing serial is lower than the current date
  114. *
  115. * - update date in serial if it reaches limit of revisions for today or do you
  116. * think that ritual suicide is better in such case?
  117. *
  118. * "This works unless you will require to make more than 99 changes until the new
  119. * date is reached - in which case perhaps ritual suicide is the best option."
  120. * http://www.zytrax.com/books/dns/ch9/serial.html
  121. *
  122. * @param string $curr_serial Current Serial No
  123. * @param string $today Optional date for "today"
  124. *
  125. * @return string Next serial number
  126. */
  127. function get_next_serial($curr_serial, $today = '') {
  128. // Autoserial
  129. if ($curr_serial == 0) {
  130. return 0;
  131. }
  132. // Serial number could be a not date based
  133. if ($curr_serial < 1979999999) {
  134. return $curr_serial+1;
  135. }
  136. // Reset the serial number, Bind was written in the early 1980s
  137. if ($curr_serial == 1979999999) {
  138. return 1;
  139. }
  140. if ($today == '') {
  141. set_timezone();
  142. $today = date('Ymd');
  143. }
  144. $revision = (int) substr($curr_serial, -2);
  145. $ser_date = substr($curr_serial, 0, 8);
  146. if ($curr_serial == '0') {
  147. $serial = $curr_serial;
  148. } elseif ($curr_serial == $today . '99') {
  149. $serial = get_next_date($today) . '00';
  150. } else {
  151. if (strcmp($today, $ser_date) === 0) {
  152. // Current serial starts with date of today, so we need to update the revision only.
  153. ++$revision;
  154. } elseif (strncmp($today, $curr_serial, 8) === -1) {
  155. // Reuse existing serial date if it's in the future
  156. $today = substr($curr_serial, 0, 8);
  157. // Get next date if revision reaches maximum per day (99) limit otherwise increment the counter
  158. if ($revision == 99) {
  159. $today = get_next_date($today);
  160. $revision = "00";
  161. } else {
  162. ++$revision;
  163. }
  164. } else {
  165. // Current serial did not start of today, so it's either an older
  166. // serial, therefore set a fresh serial
  167. $revision = "00";
  168. }
  169. // Create new serial out of existing/updated date and revision
  170. $serial = $today . str_pad($revision, 2, "0", STR_PAD_LEFT);
  171. }
  172. return $serial;
  173. }
  174. /** Update SOA record
  175. *
  176. * @param int $domain_id Domain ID
  177. * @param string $content SOA content to set
  178. *
  179. * @return boolean true if success
  180. */
  181. function update_soa_record($domain_id, $content) {
  182. global $db;
  183. $sqlq = "UPDATE records SET content = " . $db->quote($content, 'text') . " WHERE domain_id = " . $db->quote($domain_id, 'integer') . " AND type = " . $db->quote('SOA', 'text');
  184. $response = $db->query($sqlq);
  185. if (PEAR::isError($response)) {
  186. error($response->getMessage());
  187. return false;
  188. }
  189. return true;
  190. }
  191. /** Set SOA serial in SOA content
  192. *
  193. * @param string $soa_rec SOA record content
  194. * @param string $serial New serial number
  195. *
  196. * @return string Updated SOA record
  197. */
  198. function set_soa_serial($soa_rec, $serial) {
  199. // Split content of current SOA record into an array.
  200. $soa = explode(" ", $soa_rec);
  201. $soa[2] = $serial;
  202. // Build new SOA record content
  203. $soa_rec = join(" ", $soa);
  204. chop($soa_rec);
  205. return $soa_rec;
  206. }
  207. /** Return SOA record
  208. *
  209. * Returns SOA record with incremented serial number
  210. *
  211. * @param int $soa_rec Current SOA record
  212. *
  213. * @return boolean true if success
  214. */
  215. function get_updated_soa_record($soa_rec) {
  216. $curr_serial = get_soa_serial($soa_rec);
  217. $new_serial = get_next_serial($curr_serial);
  218. if ($curr_serial != $new_serial) {
  219. return set_soa_serial($soa_rec, $new_serial);
  220. }
  221. return set_soa_serial($soa_rec, $curr_serial);
  222. }
  223. /** Update SOA serial
  224. *
  225. * Increments SOA serial to next possible number
  226. *
  227. * @param int $domain_id Domain ID
  228. *
  229. * @return boolean true if success
  230. */
  231. function update_soa_serial($domain_id) {
  232. $soa_rec = get_soa_record($domain_id);
  233. if ($soa_rec == NULL) {
  234. return false;
  235. }
  236. $curr_serial = get_soa_serial($soa_rec);
  237. $new_serial = get_next_serial($curr_serial);
  238. if ($curr_serial != $new_serial) {
  239. $soa_rec = set_soa_serial($soa_rec, $new_serial);
  240. return update_soa_record($domain_id, $soa_rec);
  241. }
  242. return true;
  243. }
  244. /** Get Zone comment
  245. *
  246. * @param int $zone_id Zone ID
  247. *
  248. * @return string Zone Comment
  249. */
  250. function get_zone_comment($zone_id) {
  251. global $db;
  252. $query = "SELECT comment FROM zones WHERE domain_id = " . $db->quote($zone_id, 'integer');
  253. $comment = $db->queryOne($query);
  254. if ($comment == "0") {
  255. $comment = '';
  256. }
  257. return $comment;
  258. }
  259. /** Edit the zone comment
  260. *
  261. * This function validates it if correct it inserts it into the database.
  262. *
  263. * @param int $zone_id Zone ID
  264. * @param string $comment Comment to set
  265. *
  266. * @return boolean true on success
  267. */
  268. function edit_zone_comment($zone_id, $comment) {
  269. if (do_hook('verify_permission', 'zone_content_edit_others')) {
  270. $perm_content_edit = "all";
  271. } elseif (do_hook('verify_permission', 'zone_content_edit_own')) {
  272. $perm_content_edit = "own";
  273. } elseif (do_hook('verify_permission', 'zone_content_edit_own_as_client')) {
  274. $perm_content_edit = "own_as_client";
  275. } else {
  276. $perm_content_edit = "none";
  277. }
  278. $user_is_zone_owner = do_hook('verify_user_is_owner_zoneid' , $zone_id );
  279. $zone_type = get_domain_type($zone_id);
  280. if ($zone_type == "SLAVE" || $perm_content_edit == "none" || (($perm_content_edit == "own" || $perm_content_edit == "own_as_client") && $user_is_zone_owner == "0")) {
  281. error(ERR_PERM_EDIT_COMMENT);
  282. return false;
  283. } else {
  284. global $db;
  285. $query = "SELECT COUNT(*) FROM zones WHERE domain_id=" . $db->quote($zone_id, 'integer');
  286. $count = $db->queryOne($query);
  287. if ($count > 0) {
  288. $query = "UPDATE zones
  289. SET comment=" . $db->quote($comment, 'text') . "
  290. WHERE domain_id=" . $db->quote($zone_id, 'integer');
  291. $result = $db->query($query);
  292. if (PEAR::isError($result)) {
  293. error($result->getMessage());
  294. return false;
  295. }
  296. } else {
  297. $query = "INSERT INTO zones (domain_id, owner, comment)
  298. VALUES(" . $db->quote($zone_id, 'integer') . ",1," . $db->quote($comment, 'text') . ")";
  299. $result = $db->query($query);
  300. if (PEAR::isError($result)) {
  301. error($result->getMessage());
  302. return false;
  303. }
  304. }
  305. }
  306. return true;
  307. }
  308. /** Edit a record
  309. *
  310. * This function validates it if correct it inserts it into the database.
  311. *
  312. * @param mixed[] $record Record structure to update
  313. *
  314. * @return boolean true if successful
  315. */
  316. function edit_record($record) {
  317. if (do_hook('verify_permission', 'zone_content_edit_others')) {
  318. $perm_content_edit = "all";
  319. } elseif (do_hook('verify_permission', 'zone_content_edit_own')) {
  320. $perm_content_edit = "own";
  321. } elseif (do_hook('verify_permission', 'zone_content_edit_own_as_client')) {
  322. $perm_content_edit = "own_as_client";
  323. } else {
  324. $perm_content_edit = "none";
  325. }
  326. $user_is_zone_owner = do_hook('verify_user_is_owner_zoneid', $record['zid']);
  327. $zone_type = get_domain_type($record['zid']);
  328. if($record['type'] == 'SOA' && $perm_content_edit == "own_as_client"){
  329. error(ERR_PERM_EDIT_RECORD_SOA);
  330. return false;
  331. }
  332. if($record['type'] == 'NS' && $perm_content_edit == "own_as_client"){
  333. error(ERR_PERM_EDIT_RECORD_NS);
  334. return false;
  335. }
  336. if ($zone_type == "SLAVE" || $perm_content_edit == "none" || (($perm_content_edit == "own" || $perm_content_edit == "own_as_client") && $user_is_zone_owner == "0")) {
  337. error(ERR_PERM_EDIT_RECORD);
  338. return false;
  339. } else {
  340. global $db;
  341. if (validate_input($record['rid'], $record['zid'], $record['type'], $record['content'], $record['name'], $record['prio'], $record['ttl'])) {
  342. $name = strtolower($record['name']); // powerdns only searches for lower case records
  343. $query = "UPDATE records
  344. SET name=" . $db->quote($name, 'text') . ",
  345. type=" . $db->quote($record['type'], 'text') . ",
  346. content=" . $db->quote($record['content'], 'text') . ",
  347. ttl=" . $db->quote($record['ttl'], 'integer') . ",
  348. prio=" . $db->quote($record['prio'], 'integer') . ",
  349. change_date=" . $db->quote(time(), 'integer') . "
  350. WHERE id=" . $db->quote($record['rid'], 'integer');
  351. $result = $db->query($query);
  352. if (PEAR::isError($result)) {
  353. error($result->getMessage());
  354. return false;
  355. }
  356. return true;
  357. }
  358. return false;
  359. }
  360. }
  361. /** Add a record
  362. *
  363. * This function validates it if correct it inserts it into the database.
  364. *
  365. * @param int $zone_id Zone ID
  366. * @param string $name Name part of record
  367. * @param string $type Type of record
  368. * @param string $content Content of record
  369. * @param int $ttl Time-To-Live of record
  370. * @param int $prio Priority of record
  371. *
  372. * @return boolean true if successful
  373. */
  374. function add_record($zone_id, $name, $type, $content, $ttl, $prio) {
  375. global $db;
  376. global $pdnssec_use;
  377. if (do_hook('verify_permission', 'zone_content_edit_others')) {
  378. $perm_content_edit = "all";
  379. } elseif (do_hook('verify_permission', 'zone_content_edit_own')) {
  380. $perm_content_edit = "own";
  381. } elseif (do_hook('verify_permission', 'zone_content_edit_own_as_client')) {
  382. $perm_content_edit = "own_as_client";
  383. } else {
  384. $perm_content_edit = "none";
  385. }
  386. $user_is_zone_owner = do_hook('verify_user_is_owner_zoneid' , $zone_id );
  387. $zone_type = get_domain_type($zone_id);
  388. if ($zone_type == "SLAVE" || $perm_content_edit == "none" || (($perm_content_edit == "own" || $perm_content_edit == "own_as_client") && $user_is_zone_owner == "0")) {
  389. error(ERR_PERM_ADD_RECORD);
  390. return false;
  391. } else {
  392. $response = $db->beginTransaction();
  393. if (validate_input(-1, $zone_id, $type, $content, $name, $prio, $ttl)) {
  394. $change = time();
  395. $name = strtolower($name); // powerdns only searches for lower case records
  396. $query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES ("
  397. . $db->quote($zone_id, 'integer') . ","
  398. . $db->quote($name, 'text') . ","
  399. . $db->quote($type, 'text') . ","
  400. . $db->quote($content, 'text') . ","
  401. . $db->quote($ttl, 'integer') . ","
  402. . $db->quote($prio, 'integer') . ","
  403. . $db->quote($change, 'integer') . ")";
  404. $response = $db->exec($query);
  405. if (PEAR::isError($response)) {
  406. error($response->getMessage());
  407. $response = $db->rollback();
  408. return false;
  409. } else {
  410. $response = $db->commit();
  411. if ($type != 'SOA') {
  412. update_soa_serial($zone_id);
  413. }
  414. if ($pdnssec_use) {
  415. dnssec_rectify_zone($zone_id);
  416. }
  417. return true;
  418. }
  419. } else {
  420. return false;
  421. }
  422. }
  423. }
  424. /** Add Supermaster
  425. *
  426. * Add a trusted supermaster to the global supermasters table
  427. *
  428. * @param string $master_ip Supermaster IP address
  429. * @param string $ns_name Hostname of supermasterfound in NS records for domain
  430. * @param string $account Account name used for tracking
  431. *
  432. * @return boolean true on success
  433. */
  434. function add_supermaster($master_ip, $ns_name, $account) {
  435. global $db;
  436. if (!is_valid_ipv4($master_ip) && !is_valid_ipv6($master_ip)) {
  437. error(ERR_DNS_IP);
  438. return false;
  439. }
  440. if (!is_valid_hostname_fqdn($ns_name, 0)) {
  441. error(ERR_DNS_HOSTNAME);
  442. return false;
  443. }
  444. if (!validate_account($account)) {
  445. error(sprintf(ERR_INV_ARGC, "add_supermaster", "given account name is invalid (alpha chars only)"));
  446. return false;
  447. }
  448. if (supermaster_ip_name_exists($master_ip, $ns_name)) {
  449. error(ERR_SM_EXISTS);
  450. return false;
  451. } else {
  452. $db->query("INSERT INTO supermasters VALUES (" . $db->quote($master_ip, 'text') . ", " . $db->quote($ns_name, 'text') . ", " . $db->quote($account, 'text') . ")");
  453. return true;
  454. }
  455. }
  456. /** Delete Supermaster
  457. *
  458. * Delete a supermaster from the global supermasters table
  459. *
  460. * @param string $master_ip Supermaster IP address
  461. * @param string $ns_name Hostname of supermaster
  462. *
  463. * @return boolean true on success
  464. */
  465. function delete_supermaster($master_ip, $ns_name) {
  466. global $db;
  467. if (is_valid_ipv4($master_ip) || is_valid_ipv6($master_ip) || is_valid_hostname_fqdn($ns_name, 0)) {
  468. $db->query("DELETE FROM supermasters WHERE ip = " . $db->quote($master_ip, 'text') .
  469. " AND nameserver = " . $db->quote($ns_name, 'text'));
  470. return true;
  471. } else {
  472. error(sprintf(ERR_INV_ARGC, "delete_supermaster", "No or no valid ipv4 or ipv6 address given."));
  473. }
  474. }
  475. /** Get Supermaster Info from IP
  476. *
  477. * Retrieve supermaster details from supermaster IP address
  478. *
  479. * @param string $master_ip Supermaster IP address
  480. *
  481. * @return mixed[] array of supermaster details
  482. */
  483. function get_supermaster_info_from_ip($master_ip) {
  484. global $db;
  485. if (is_valid_ipv4($master_ip) || is_valid_ipv6($master_ip)) {
  486. $result = $db->queryRow("SELECT ip,nameserver,account FROM supermasters WHERE ip = " . $db->quote($master_ip, 'text'));
  487. $ret = array(
  488. "master_ip" => $result["ip"],
  489. "ns_name" => $result["nameserver"],
  490. "account" => $result["account"]
  491. );
  492. return $ret;
  493. } else {
  494. error(sprintf(ERR_INV_ARGC, "get_supermaster_info_from_ip", "No or no valid ipv4 or ipv6 address given."));
  495. }
  496. }
  497. /** Get record details from Record ID
  498. *
  499. * @param $rid Record ID
  500. *
  501. * @return mixed[] array of record details [rid,zid,name,type,content,ttl,prio,change_date]
  502. */
  503. function get_record_details_from_record_id($rid) {
  504. global $db;
  505. $query = "SELECT id AS rid, domain_id AS zid, name, type, content, ttl, prio, change_date FROM records WHERE id = " . $db->quote($rid, 'integer');
  506. $response = $db->query($query);
  507. if (PEAR::isError($response)) {
  508. error($response->getMessage());
  509. return false;
  510. }
  511. $return = $response->fetchRow();
  512. return $return;
  513. }
  514. /** Delete a record by a given record id
  515. *
  516. * @param int $rid Record ID
  517. *
  518. * @return boolean true on success
  519. */
  520. function delete_record($rid) {
  521. global $db;
  522. if (do_hook('verify_permission' , 'zone_content_edit_others' )) {
  523. $perm_content_edit = "all";
  524. } elseif (do_hook('verify_permission' , 'zone_content_edit_own' )) {
  525. $perm_content_edit = "own";
  526. } else {
  527. $perm_content_edit = "none";
  528. }
  529. // Determine ID of zone first.
  530. $record = get_record_details_from_record_id($rid);
  531. $user_is_zone_owner = do_hook('verify_user_is_owner_zoneid' , $record['zid'] );
  532. if ($perm_content_edit == "all" || (($perm_content_edit == "own" || $perm_content_edit == "own_as_client") && $user_is_zone_owner == "1" )) {
  533. if ($record['type'] == "SOA") {
  534. error(_('You are trying to delete the SOA record. You are not allowed to remove it, unless you remove the entire zone.'));
  535. } else {
  536. $query = "DELETE FROM records WHERE id = " . $db->quote($rid, 'integer');
  537. $response = $db->query($query);
  538. if (PEAR::isError($response)) {
  539. error($response->getMessage());
  540. return false;
  541. }
  542. return true;
  543. }
  544. } else {
  545. error(ERR_PERM_DEL_RECORD);
  546. return false;
  547. }
  548. }
  549. /** Delete record reference to zone template
  550. *
  551. * @param int $rid Record ID
  552. *
  553. * @return boolean true on success
  554. */
  555. function delete_record_zone_templ($rid) {
  556. global $db;
  557. $query = "DELETE FROM records_zone_templ WHERE record_id = " . $db->quote($rid, 'integer');
  558. $response = $db->query($query);
  559. if (PEAR::isError($response)) {
  560. error($response->getMessage());
  561. return false;
  562. }
  563. return true;
  564. }
  565. /**
  566. * Add a domain to the database
  567. *
  568. * A domain is name obligatory, so is an owner.
  569. * return values: true when succesful.
  570. *
  571. * Empty means templates dont have to be applied.
  572. *
  573. * This functions eats a template and by that it inserts various records.
  574. * first we start checking if something in an arpa record
  575. * remember to request nextID's from the database to be able to insert record.
  576. * if anything is invalid the function will error
  577. *
  578. * @param string $domain A domain name
  579. * @param int $owner Owner ID for domain
  580. * @param string $type Type of domain ['NATIVE','MASTER','SLAVE']
  581. * @param string $slave_master Master server hostname for domain
  582. * @param int|string $zone_template ID of zone template ['none' or int]
  583. *
  584. * @return boolean true on success
  585. */
  586. function add_domain($domain, $owner, $type, $slave_master, $zone_template) {
  587. if (do_hook('verify_permission' , 'zone_master_add' )) {
  588. $zone_master_add = "1";
  589. }
  590. if (do_hook('verify_permission' , 'zone_slave_add' )) {
  591. $zone_slave_add = "1";
  592. }
  593. // TODO: make sure only one is possible if only one is enabled
  594. if ($zone_master_add == "1" || $zone_slave_add == "1") {
  595. global $db;
  596. global $dns_ns1;
  597. global $dns_hostmaster;
  598. global $dns_ttl;
  599. global $db_type;
  600. if (($domain && $owner && $zone_template) ||
  601. (preg_match('/in-addr.arpa/i', $domain) && $owner && $zone_template) ||
  602. $type == "SLAVE" && $domain && $owner && $slave_master) {
  603. $response = $db->query("INSERT INTO domains (name, type) VALUES (" . $db->quote($domain, 'text') . ", " . $db->quote($type, 'text') . ")");
  604. if (PEAR::isError($response)) {
  605. error($response->getMessage());
  606. return false;
  607. }
  608. if ($db_type == 'pgsql') {
  609. $domain_id = $db->lastInsertId('domains_id_seq');
  610. } else {
  611. $domain_id = $db->lastInsertId();
  612. }
  613. if (PEAR::isError($domain_id)) {
  614. error($domain_id->getMessage());
  615. return false;
  616. }
  617. $response = $db->query("INSERT INTO zones (domain_id, owner, zone_templ_id) VALUES (" . $db->quote($domain_id, 'integer') . ", " . $db->quote($owner, 'integer') . ", " . $db->quote(($zone_template == "none") ? 0 : $zone_template, 'integer') . ")");
  618. if (PEAR::isError($response)) {
  619. error($response->getMessage());
  620. return false;
  621. }
  622. if ($type == "SLAVE") {
  623. $response = $db->query("UPDATE domains SET master = " . $db->quote($slave_master, 'text') . " WHERE id = " . $db->quote($domain_id, 'integer'));
  624. if (PEAR::isError($response)) {
  625. error($response->getMessage());
  626. return false;
  627. }
  628. return true;
  629. } else {
  630. $now = time();
  631. if ($zone_template == "none" && $domain_id) {
  632. $ns1 = $dns_ns1;
  633. $hm = $dns_hostmaster;
  634. $ttl = $dns_ttl;
  635. set_timezone();
  636. $serial = date("Ymd");
  637. $serial .= "00";
  638. $query = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES ("
  639. . $db->quote($domain_id, 'integer') . ","
  640. . $db->quote($domain, 'text') . ","
  641. . $db->quote($ns1 . ' ' . $hm . ' ' . $serial . ' 28800 7200 604800 86400', 'text') . ","
  642. . $db->quote('SOA', 'text') . ","
  643. . $db->quote($ttl, 'integer') . ","
  644. . $db->quote(0, 'integer') . ","
  645. . $db->quote($now, 'integer') . ")";
  646. $response = $db->query($query);
  647. if (PEAR::isError($response)) {
  648. error($response->getMessage());
  649. return false;
  650. }
  651. return true;
  652. } elseif ($domain_id && is_numeric($zone_template)) {
  653. global $dns_ttl;
  654. $templ_records = get_zone_templ_records($zone_template);
  655. if ($templ_records != -1) {
  656. foreach ($templ_records as $r) {
  657. if ((preg_match('/in-addr.arpa/i', $domain) && ($r["type"] == "NS" || $r["type"] == "SOA")) || (!preg_match('/in-addr.arpa/i', $domain))) {
  658. $name = parse_template_value($r["name"], $domain);
  659. $type = $r["type"];
  660. $content = parse_template_value($r["content"], $domain);
  661. $ttl = $r["ttl"];
  662. $prio = intval($r["prio"]);
  663. if (!$ttl) {
  664. $ttl = $dns_ttl;
  665. }
  666. $query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES ("
  667. . $db->quote($domain_id, 'integer') . ","
  668. . $db->quote($name, 'text') . ","
  669. . $db->quote($type, 'text') . ","
  670. . $db->quote($content, 'text') . ","
  671. . $db->quote($ttl, 'integer') . ","
  672. . $db->quote($prio, 'integer') . ","
  673. . $db->quote($now, 'integer') . ")";
  674. $response = $db->query($query);
  675. if (PEAR::isError($response)) {
  676. error($response->getMessage());
  677. return false;
  678. }
  679. if ($db_type == 'pgsql') {
  680. $record_id = $db->lastInsertId('records_id_seq');
  681. } else {
  682. $record_id = $db->lastInsertId();
  683. }
  684. if (PEAR::isError($record_id)) {
  685. error($record_id->getMessage());
  686. return false;
  687. }
  688. $query = "INSERT INTO records_zone_templ (domain_id, record_id, zone_templ_id) VALUES ("
  689. . $db->quote($domain_id, 'integer') . ","
  690. . $db->quote($record_id, 'integer') . ","
  691. . $db->quote($r['zone_templ_id'], 'integer') . ")";
  692. $response = $db->query($query);
  693. if (PEAR::isError($response)) {
  694. error($response->getMessage());
  695. return false;
  696. }
  697. }
  698. }
  699. }
  700. return true;
  701. } else {
  702. error(sprintf(ERR_INV_ARGC, "add_domain", "could not create zone"));
  703. }
  704. }
  705. } else {
  706. error(sprintf(ERR_INV_ARG, "add_domain"));
  707. }
  708. } else {
  709. error(ERR_PERM_ADD_ZONE_MASTER);
  710. return false;
  711. }
  712. }
  713. /** Deletes a domain by a given id
  714. *
  715. * Function always succeeds. If the field is not found in the database, thats what we want anyway.
  716. *
  717. * @param int $id Zone ID
  718. *
  719. * @return boolean true on success
  720. */
  721. function delete_domain($id) {
  722. global $db;
  723. if (do_hook('verify_permission' , 'zone_content_edit_others' )) {
  724. $perm_edit = "all";
  725. } elseif (do_hook('verify_permission' , 'zone_content_edit_own' )) {
  726. $perm_edit = "own";
  727. } else {
  728. $perm_edit = "none";
  729. }
  730. $user_is_zone_owner = do_hook('verify_user_is_owner_zoneid' , $id );
  731. if ($perm_edit == "all" || ( $perm_edit == "own" && $user_is_zone_owner == "1")) {
  732. if (is_numeric($id)) {
  733. $db->query("DELETE FROM zones WHERE domain_id=" . $db->quote($id, 'integer'));
  734. $db->query("DELETE FROM records WHERE domain_id=" . $db->quote($id, 'integer'));
  735. $db->query("DELETE FROM records_zone_templ WHERE domain_id=" . $db->quote($id, 'integer'));
  736. $db->query("DELETE FROM domains WHERE id=" . $db->quote($id, 'integer'));
  737. return true;
  738. } else {
  739. error(sprintf(ERR_INV_ARGC, "delete_domain", "id must be a number"));
  740. return false;
  741. }
  742. } else {
  743. error(ERR_PERM_DEL_ZONE);
  744. }
  745. }
  746. /** Record ID to Domain ID
  747. *
  748. * Gets the id of the domain by a given record id
  749. *
  750. * @param int $id Record ID
  751. * @return int Domain ID of record
  752. */
  753. function recid_to_domid($id) {
  754. global $db;
  755. if (is_numeric($id)) {
  756. $result = $db->query("SELECT domain_id FROM records WHERE id=" . $db->quote($id, 'integer'));
  757. $r = $result->fetchRow();
  758. return $r["domain_id"];
  759. } else {
  760. error(sprintf(ERR_INV_ARGC, "recid_to_domid", "id must be a number"));
  761. }
  762. }
  763. /** Change owner of a domain
  764. *
  765. * @param int $zone_id Zone ID
  766. * @param int $user_id User ID
  767. *
  768. * @return boolean true when succesful
  769. */
  770. function add_owner_to_zone($zone_id, $user_id) {
  771. global $db;
  772. if ((do_hook('verify_permission' , 'zone_meta_edit_others' )) || (do_hook('verify_permission' , 'zone_meta_edit_own' )) && do_hook('verify_user_is_owner_zoneid' , $_GET["id"] )) {
  773. // User is allowed to make change to meta data of this zone.
  774. if (is_numeric($zone_id) && is_numeric($user_id) && do_hook('is_valid_user' , $user_id )) {
  775. if ($db->queryOne("SELECT COUNT(id) FROM zones WHERE owner=" . $db->quote($user_id, 'integer') . " AND domain_id=" . $db->quote($zone_id, 'integer')) == 0) {
  776. $zone_templ_id = get_zone_template($zone_id);
  777. if ($zone_templ_id == NULL)
  778. $zone_templ_id = 0;
  779. $db->query("INSERT INTO zones (domain_id, owner, zone_templ_id) VALUES("
  780. . $db->quote($zone_id, 'integer') . ", "
  781. . $db->quote($user_id, 'integer') . ", "
  782. . $db->quote($zone_templ_id, 'integer') . ")"
  783. );
  784. }
  785. return true;
  786. } else {
  787. error(sprintf(ERR_INV_ARGC, "add_owner_to_zone", "$zone_id / $user_id"));
  788. }
  789. } else {
  790. return false;
  791. }
  792. }
  793. /** Delete owner from zone
  794. *
  795. * @param int $zone_id Zone ID
  796. * @param int $user_id User ID
  797. *
  798. * @return boolean true on success
  799. */
  800. function delete_owner_from_zone($zone_id, $user_id) {
  801. global $db;
  802. if ((do_hook('verify_permission' , 'zone_meta_edit_others' )) || (do_hook('verify_permission' , 'zone_meta_edit_own' )) && do_hook('verify_user_is_owner_zoneid' , $_GET["id"] )) {
  803. // User is allowed to make change to meta data of this zone.
  804. if (is_numeric($zone_id) && is_numeric($user_id) && do_hook('is_valid_user' , $user_id )) {
  805. // TODO: Next if() required, why not just execute DELETE query?
  806. if ($db->queryOne("SELECT COUNT(id) FROM zones WHERE owner=" . $db->quote($user_id, 'integer') . " AND domain_id=" . $db->quote($zone_id, 'integer')) != 0) {
  807. $db->query("DELETE FROM zones WHERE owner=" . $db->quote($user_id, 'integer') . " AND domain_id=" . $db->quote($zone_id, 'integer'));
  808. }
  809. return true;
  810. } else {
  811. error(sprintf(ERR_INV_ARGC, "delete_owner_from_zone", "$zone_id / $user_id"));
  812. }
  813. } else {
  814. return false;
  815. }
  816. }
  817. /** Retrieve all supported dns record types
  818. *
  819. * This function might be deprecated.
  820. *
  821. * @return string[] array of types
  822. */
  823. function get_record_types() {
  824. global $rtypes;
  825. return $rtypes;
  826. }
  827. /** Retrieve all records by a given type and domain id
  828. *
  829. * Example get all records that are of type A from domain id 1
  830. *
  831. * <code>
  832. * get_records_by_type_from_domid('A', 1)
  833. * </code>
  834. *
  835. * @param string $type Record type
  836. * @param int $recid Record ID
  837. *
  838. * @return object a DB class result object
  839. */
  840. function get_records_by_type_from_domid($type, $recid) {
  841. global $rtypes;
  842. global $db;
  843. // Does this type exist?
  844. if (!in_array(strtoupper($type), $rtypes)) {
  845. error(sprintf(ERR_INV_ARGC, "get_records_from_type", "this is not a supported record"));
  846. }
  847. // Get the domain id.
  848. $domid = recid_to_domid($recid);
  849. $result = $db->query("select id, type from records where domain_id=" . $db->quote($recid, 'integer') . " and type=" . $db->quote($type, 'text'));
  850. return $result;
  851. }
  852. /** Get Record Type for Record ID
  853. *
  854. * Retrieves the type of a record from a given id.
  855. *
  856. * @param int $id Record ID
  857. * @return string Record type (one of the records types in $rtypes assumable).
  858. */
  859. function get_recordtype_from_id($id) {
  860. global $db;
  861. if (is_numeric($id)) {
  862. $result = $db->query("SELECT type FROM records WHERE id=" . $db->quote($id, 'integer'));
  863. $r = $result->fetchRow();
  864. return $r["type"];
  865. } else {
  866. error(sprintf(ERR_INV_ARG, "get_recordtype_from_id"));
  867. }
  868. }
  869. /** Get Name from Record ID
  870. *
  871. * Retrieves the name (e.g. bla.test.com) of a record by a given id.
  872. *
  873. * @param int $id Record ID
  874. * @return string Name part of record
  875. */
  876. function get_name_from_record_id($id) {
  877. global $db;
  878. if (is_numeric($id)) {
  879. $result = $db->query("SELECT name FROM records WHERE id=" . $db->quote($id, 'integer'));
  880. $r = $result->fetchRow();
  881. return $r["name"];
  882. } else {
  883. error(sprintf(ERR_INV_ARG, "get_name_from_record_id"));
  884. }
  885. }
  886. /** Get Zone Name from Zone ID
  887. *
  888. * @param int $zid Zone ID
  889. *
  890. * @return string Domain name
  891. */
  892. function get_zone_name_from_id($zid) {
  893. global $db;
  894. if (is_numeric($zid)) {
  895. $result = $db->queryRow("SELECT name FROM domains WHERE id=" . $db->quote($zid, 'integer'));
  896. if ($result) {
  897. return $result["name"];
  898. } else {
  899. error(sprintf("Zone does not exist."));
  900. return false;
  901. }
  902. } else {
  903. error(sprintf(ERR_INV_ARGC, "get_zone_name_from_id", "Not a valid domainid: $zid"));
  904. }
  905. }
  906. /** Get zone id from name
  907. *
  908. * @param string $zname Zone name
  909. * @return int Zone ID
  910. */
  911. function get_zone_id_from_name($zname) {
  912. global $db;
  913. if (!empty($zname)) {
  914. $result = $db->queryRow("SELECT id FROM domains WHERE name=" . $db->quote($zname, 'text'));
  915. if ($result) {
  916. return $result["id"];
  917. } else {
  918. error(sprintf("Zone does not exist."));
  919. return false;
  920. }
  921. } else {
  922. error(sprintf(ERR_INV_ARGC, "get_zone_id_from_name", "Not a valid domainname: $zname"));
  923. }
  924. }
  925. /** Get Zone details from Zone ID
  926. *
  927. * @param int $zid Zone ID
  928. * @return mixed[] array of zone details [type,name,master_ip,record_count]
  929. */
  930. function get_zone_info_from_id($zid) {
  931. if (do_hook('verify_permission' , 'zone_content_view_others' )) {
  932. $perm_view = "all";
  933. } elseif (do_hook('verify_permission' , 'zone_content_view_own' )) {
  934. $perm_view = "own";
  935. } else {
  936. $perm_view = "none";
  937. }
  938. if ($perm_view == "none") {
  939. error(ERR_PERM_VIEW_ZONE);
  940. } else {
  941. global $db;
  942. $query = "SELECT domains.type AS type,
  943. domains.name AS name,
  944. domains.master AS master_ip,
  945. count(records.domain_id) AS record_count
  946. FROM domains LEFT OUTER JOIN records ON domains.id = records.domain_id
  947. WHERE domains.id = " . $db->quote($zid, 'integer') . "
  948. GROUP BY domains.id, domains.type, domains.name, domains.master";
  949. $result = $db->queryRow($query);
  950. $return = array(
  951. "name" => $result['name'],
  952. "type" => $result['type'],
  953. "master_ip" => $result['master_ip'],
  954. "record_count" => $result['record_count']
  955. );
  956. return $return;
  957. }
  958. }
  959. /** Convert IPv6 Address to PTR
  960. *
  961. * @param string $ip IPv6 Address
  962. * @return string PTR form of address
  963. */
  964. function convert_ipv6addr_to_ptrrec($ip) {
  965. // rev-patch
  966. // taken from: http://stackoverflow.com/questions/6619682/convert-ipv6-to-nibble-format-for-ptr-records
  967. // PHP (>= 5.1.0, or 5.3+ on Windows), use the inet_pton
  968. // $ip = '2001:db8::567:89ab';
  969. $addr = inet_pton($ip);
  970. $unpack = unpack('H*hex', $addr);
  971. $hex = $unpack['hex'];
  972. $arpa = implode('.', array_reverse(str_split($hex))) . '.ip6.arpa';
  973. return $arpa;
  974. }
  975. /** Get Best Matching in-addr.arpa Zone ID from Domain Name
  976. *
  977. * @param string $domain Domain name
  978. *
  979. * @return int Zone ID
  980. */
  981. function get_best_matching_zone_id_from_name($domain) {
  982. // rev-patch
  983. // tring to find the correct zone
  984. // %ip6.arpa and %in-addr.arpa is looked for
  985. global $db;
  986. $match = 72; // the longest ip6.arpa has a length of 72
  987. $found_domain_id = -1;
  988. // get all reverse-zones
  989. $query = "SELECT name, id FROM domains
  990. WHERE name like " . $db->quote('%.arpa', 'text') . "
  991. ORDER BY length(name) DESC";
  992. $response = $db->query($query);
  993. if (PEAR::isError($response)) {
  994. error($response->getMessage());
  995. return false;
  996. }
  997. if ($response) {
  998. while ($r = $response->fetchRow()) {
  999. $pos = stripos($domain, $r["name"]);
  1000. if ($pos !== false) {
  1001. // one possible searched $domain is found
  1002. if ($pos < $match) {
  1003. $match = $pos;
  1004. $found_domain_id = $r["id"];
  1005. }
  1006. }
  1007. }
  1008. } else {
  1009. return -1;
  1010. }
  1011. return $found_domain_id;
  1012. }
  1013. /** Check if Domain Exists
  1014. *
  1015. * Check if a domain is already existing.
  1016. *
  1017. * @param string $domain Domain name
  1018. * @return boolean true if existing, false if it doesnt exist.
  1019. */
  1020. function domain_exists($domain) {
  1021. global $db;
  1022. if (is_valid_hostname_fqdn($domain, 0)) {
  1023. $result = $db->queryRow("SELECT id FROM domains WHERE name=" . $db->quote($domain, 'text'));
  1024. return ($result ? true : false);
  1025. } else {
  1026. error(ERR_DOMAIN_INVALID);
  1027. }
  1028. }
  1029. /** Get All Supermasters
  1030. *
  1031. * Gets an array of arrays of supermaster details
  1032. *
  1033. * @return array[] supermasters detail [master_ip,ns_name,account]s
  1034. */
  1035. function get_supermasters() {
  1036. global $db;
  1037. $result = $db->query("SELECT ip, nameserver, account FROM supermasters");
  1038. if (PEAR::isError($result)) {
  1039. error($result->getMessage());
  1040. return false;
  1041. }
  1042. $ret = array();
  1043. while ($r = $result->fetchRow()) {
  1044. $ret[] = array(
  1045. "master_ip" => $r["ip"],
  1046. "ns_name" => $r["nameserver"],
  1047. "account" => $r["account"],
  1048. );
  1049. }
  1050. return (sizeof($ret) == 0 ? -1 : $ret);
  1051. }
  1052. /** Check if Supermaster IP address exists
  1053. *
  1054. * @param string $master_ip Supermaster IP
  1055. *
  1056. * @return boolean true if exists, otherwise false
  1057. */
  1058. function supermaster_exists($master_ip) {
  1059. global $db;
  1060. if (is_valid_ipv4($master_ip, false) || is_valid_ipv6($master_ip)) {
  1061. $result = $db->queryOne("SELECT ip FROM supermasters WHERE ip = " . $db->quote($master_ip, 'text'));
  1062. return ($result ? true : false);
  1063. } else {
  1064. error(sprintf(ERR_INV_ARGC, "supermaster_exists", "No or no valid IPv4 or IPv6 address given."));
  1065. }
  1066. }
  1067. /** Check if Supermaster IP Address and NS Name combo exists
  1068. *
  1069. * @param string $master_ip Supermaster IP Address
  1070. * @param string $ns_name Supermaster NS Name
  1071. *
  1072. * @return boolean true if exists, false otherwise
  1073. */
  1074. function supermaster_ip_name_exists($master_ip, $ns_name) {
  1075. global $db;
  1076. if ((is_valid_ipv4($master_ip) || is_valid_ipv6($master_ip)) && is_valid_hostname_fqdn($ns_name, 0)) {
  1077. $result = $db->queryOne("SELECT ip FROM supermasters WHERE ip = " . $db->quote($master_ip, 'text') .
  1078. " AND nameserver = " . $db->quote($ns_name, 'text'));
  1079. return ($result ? true : false);
  1080. } else {
  1081. error(sprintf(ERR_INV_ARGC, "supermaster_exists", "No or no valid IPv4 or IPv6 address given."));
  1082. }
  1083. }
  1084. /** Get Zones
  1085. *
  1086. * @param string $perm View Zone Permissions ['own','all','none']
  1087. * @param int $userid Requesting User ID
  1088. * @param string $letterstart Starting letters to match [default='all']
  1089. * @param int $rowstart Start from row in set [default=0]
  1090. * @param int $rowamount Max number of rows to fetch for this query when not 'all' [default=999999]
  1091. * @param string $sortby Column to sort results by [default='name']
  1092. *
  1093. * @return boolean|mixed[] false or array of zone details [id,name,type,count_records]
  1094. */
  1095. function get_zones($perm, $userid = 0, $letterstart = 'all', $rowstart = 0, $rowamount = 999999, $sortby = 'name') {
  1096. global $db;
  1097. global $db_type;
  1098. global $sql_regexp;
  1099. global $pdnssec_use;
  1100. if ($letterstart == '_') {
  1101. $letterstart = '\_';
  1102. }
  1103. $sql_add = '';
  1104. if ($perm != "own" && $perm != "all") {
  1105. error(ERR_PERM_VIEW_ZONE);
  1106. return false;
  1107. } else {
  1108. if ($perm == "own") {
  1109. $sql_add = " AND zones.domain_id = domains.id
  1110. AND zones.owner = " . $db->quote($userid, 'integer');
  1111. }
  1112. if ($letterstart != 'all' && $letterstart != 1) {
  1113. $sql_add .=" AND substring(domains.name,1,1) = " . $db->quote($letterstart, 'text') . " ";
  1114. } elseif ($letterstart == 1) {
  1115. $sql_add .=" AND substring(domains.name,1,1) " . $sql_regexp . " '^[[:digit:]]'";
  1116. }
  1117. }
  1118. if ($sortby == 'owner') {
  1119. $sortby = 'users.username';
  1120. } elseif ($sortby != 'count_records') {
  1121. $sortby = 'domains.' . $sortby;
  1122. }
  1123. $natural_sort = 'domains.name';
  1124. if ($db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'sqlite' || $db_type == 'sqlite3') {
  1125. $natural_sort = 'domains.name+0<>0 DESC, domains.name+0, domains.name';
  1126. }
  1127. $sql_sortby = ($sortby == 'domains.name' ? $natural_sort : $sortby . ', ' . $natural_sort);
  1128. $sqlq = "SELECT domains.id,
  1129. domains.name,
  1130. domains.type,
  1131. COUNT(records.id) AS count_records,
  1132. users.fullname
  1133. " . ($pdnssec_use ? ",COUNT(cryptokeys.id) > 0 OR COUNT(domainmetadata.id) > 0 AS secured" : "") . "
  1134. FROM domains
  1135. LEFT JOIN zones ON domains.id=zones.domain_id
  1136. LEFT JOIN records ON records.domain_id=domains.id AND records.type IS NOT NULL
  1137. LEFT JOIN users ON users.id=zones.owner
  1138. ";
  1139. if ($pdnssec_use) {
  1140. $sqlq .= " LEFT JOIN cryptokeys ON domains.id = cryptokeys.domain_id AND cryptokeys.active
  1141. LEFT JOIN domainmetadata ON domains.id = domainmetadata.domain_id AND domainmetadata.kind = 'PRESIGNED'
  1142. ";
  1143. }
  1144. $sqlq .= "
  1145. WHERE 1=1" . $sql_add . "
  1146. GROUP BY domains.name, domains.id, domains.type, users.fullname
  1147. ORDER BY " . $sql_sortby;
  1148. if ($letterstart != 'all') {
  1149. $db->setLimit($rowamount, $rowstart);
  1150. }
  1151. $result = $db->query($sqlq);
  1152. $ret = array();
  1153. while ($r = $result->fetchRow()) {
  1154. //fixme: name is not guaranteed to be unique with round-robin record sets
  1155. $ret[$r["name"]] = array(
  1156. "id" => $r["id"],
  1157. "name" => $r["name"],
  1158. "type" => $r["type"],
  1159. "count_records" => $r["count_records"],
  1160. "owner" => $r["fullname"],
  1161. );
  1162. if ($pdnssec_use) {
  1163. $ret[$r["name"]]["secured"] = $r["secured"];
  1164. }
  1165. }
  1166. return $ret;
  1167. }
  1168. // TODO: letterstart limitation and userid permission limitiation should be applied at the same time?
  1169. // fixme: letterstart 'all' forbids searching for domains that actually start with 'all'
  1170. /** Get Count of Zones
  1171. *
  1172. * @param string $perm 'all', 'own' uses session 'userid'
  1173. * @param string $letterstart Starting letters to match [default='all']
  1174. *
  1175. * @return int Count of zones matched
  1176. */
  1177. function zone_count_ng($perm, $letterstart = 'all') {
  1178. global $db;
  1179. global $sql_regexp;
  1180. $fromTable = 'domains';
  1181. $sql_add = '';
  1182. if ($perm != "own" && $perm != "all") {
  1183. $zone_count = "0";
  1184. } else {
  1185. if ($perm == "own") {
  1186. $sql_add = " AND zones.domain_id = domains.id
  1187. AND zones.owner = " . $db->quote($_SESSION['userid'], 'integer');
  1188. $fromTable .= ',zones';
  1189. }
  1190. if ($letterstart != 'all' && $letterstart != 1) {
  1191. $sql_add .=" AND domains.name LIKE " . $db->quote($letterstart . "%", 'text') . " ";
  1192. } elseif ($letterstart == 1) {
  1193. $sql_add .=" AND substring(domains.name,1,1) " . $sql_regexp . " '^[[:digit:]]'";
  1194. }
  1195. # XXX: do we really need this distinct directive as it's unsupported in sqlite)
  1196. # $sqlq = "SELECT COUNT(distinct domains.id) AS count_zones
  1197. $sqlq = "SELECT COUNT(domains.id) AS count_zones
  1198. FROM " . $fromTable . " WHERE 1=1
  1199. " . $sql_add;
  1200. $zone_count = $db->queryOne($sqlq);
  1201. }
  1202. return $zone_count;
  1203. }
  1204. /** Get Zone Count for Owner User ID
  1205. *
  1206. * @param int $uid User ID
  1207. *
  1208. * @return int Count of Zones matched
  1209. */
  1210. function zone_count_for_uid($uid) {
  1211. global $db;
  1212. $query = "SELECT COUNT(domain_id)
  1213. FROM zones
  1214. WHERE owner = " . $db->quote($uid, 'integer') . "
  1215. ORDER BY domain_id";
  1216. $zone_count = $db->queryOne($query);
  1217. return $zone_count;
  1218. }
  1219. /** Get a Record from an Record ID
  1220. *
  1221. * Retrieve all fields of the record and send it back to the function caller.
  1222. *
  1223. * @param int $id Record ID
  1224. * @return int|mixed[] array of record detail, or -1 if nothing found
  1225. */
  1226. function get_record_from_id($id) {
  1227. global $db;
  1228. if (is_numeric($id)) {
  1229. $result = $db->queryRow("SELECT id, domain_id, name, type, content, ttl, prio, change_date FROM records WHERE id=" . $db->quote($id, 'integer') . " AND type IS NOT NULL");
  1230. if ($result) {
  1231. if ($result["type"] == "" || $result["content"] == "") {
  1232. return -1;
  1233. }
  1234. $ret = array(
  1235. "id" => $result["id"],
  1236. "domain_id" => $result["domain_id"],
  1237. "name" => $result["name"],
  1238. "type" => $result["type"],
  1239. "content" => $result["content"],
  1240. "ttl" => $result["ttl"],
  1241. "prio" => $result["prio"],
  1242. "change_date" => $result["change_date"]
  1243. );
  1244. return $ret;
  1245. } else {
  1246. return -1;
  1247. }
  1248. } else {
  1249. error(sprintf(ERR_INV_ARG, "get_record_from_id"));
  1250. }
  1251. }
  1252. /** Get all records from a domain id.
  1253. *
  1254. * Retrieve all fields of the records and send it back to the function caller.
  1255. *
  1256. * @param int $id Domain ID
  1257. * @param int $rowstart Starting row [default=0]
  1258. * @param int $rowamount Number of rows to return in this query [default=999999]
  1259. * @param string $sortby Column to sort by [default='name']
  1260. *
  1261. * @return int|mixed[] array of record detail, or -1 if nothing found
  1262. */
  1263. function get_records_from_domain_id($id, $rowstart = 0, $rowamount = 999999, $sortby = 'name') {
  1264. global $db;
  1265. global $db_type;
  1266. $result = array();
  1267. if (is_numeric($id)) {
  1268. if ((isset($_SESSION[$id . "_ispartial"])) && ($_SESSION[$id . "_ispartial"] == 1)) {
  1269. $db->setLimit($rowamount, $rowstart);
  1270. $result = $db->query("SELECT records.id, domains.id, records.name, records.type, records.content, records.ttl, records.prio, records.change_date
  1271. FROM record_owners,domains,records
  1272. WHERE record_owners.user_id = " . $db->quote($_SESSION["userid"], 'integer') . "
  1273. AND record_owners.record_id = records.id
  1274. AND records.domain_id = " . $db->quote($id, 'integer') . "
  1275. GROUP BY records.id, domains.id, records.name, records.type, records.content, records.ttl, records.prio, records.change_date
  1276. ORDER BY type = 'SOA' DESC, type = 'NS' DESC, records." . $sortby);
  1277. if ($result) {
  1278. while ($r = $result->fetchRow()) {
  1279. $ret[] = array(
  1280. "id" => $r["id"],
  1281. "domain_id" => $r["domain_id"],
  1282. "name" => $r["name"],
  1283. "type" => $r["type"],
  1284. "content" => $r["content"],
  1285. "ttl" => $r["ttl"],
  1286. "prio" => $r["prio"],
  1287. "change_date" => $r["change_date"]
  1288. );
  1289. }
  1290. $result = $ret;
  1291. } else {
  1292. return -1;
  1293. }
  1294. } else {
  1295. $db->setLimit($rowamount, $rowstart);
  1296. $natural_sort = 'records.name';
  1297. if ($db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'sqlite' || $db_type == 'sqlite3') {
  1298. $natural_sort = 'records.name+0<>0 DESC, records.name+0, records.name';
  1299. }
  1300. $sql_sortby = ($sortby == 'name' ? $natural_sort : $sortby . ', ' . $natural_sort);
  1301. $result = $db->query("SELECT id, domain_id, name, type, content, ttl, prio, change_date
  1302. FROM records
  1303. WHERE domain_id=" . $db->quote($id, 'integer') . " AND type IS NOT NULL
  1304. ORDER BY type = 'SOA' DESC, type = 'NS' DESC," . $sql_sortby);
  1305. $ret = array();
  1306. if ($result) {
  1307. while ($r = $result->fetchRow()) {
  1308. $ret[] = array(
  1309. "id" => $r["id"],
  1310. "domain_id" => $r["domain_id"],
  1311. "name" => $r["name"],
  1312. "type" => $r["type"],
  1313. "content" => $r["content"],
  1314. "ttl" => $r["ttl"],
  1315. "prio" => $r["prio"],
  1316. "change_date" => $r["change_date"]
  1317. );
  1318. }
  1319. $result = $ret;
  1320. } else {
  1321. return -1;
  1322. }
  1323. $result = order_domain_results($result, $sortby);
  1324. return $result;
  1325. }
  1326. } else {
  1327. error(sprintf(ERR_INV_ARG, "get_records_from_domain_id"));
  1328. }
  1329. }
  1330. /** Sort Domain Records intelligently
  1331. *
  1332. * @param string[] $domains Array of domains
  1333. * @param string $sortby Column to sort by [default='name','type','content','prio','ttl']
  1334. *
  1335. * @return mixed[] array of records detail
  1336. */
  1337. function order_domain_results($domains, $sortby) {
  1338. $results = array();
  1339. $soa = array();
  1340. $ns = array();
  1341. foreach ($domains as $key => $domain) {
  1342. switch ($domain['type']) {
  1343. case 'SOA':
  1344. $soa[] = $domain;
  1345. unset($domains[$key]);
  1346. break;
  1347. case 'NS':
  1348. $ns[] = $domain;
  1349. unset($domains[$key]);
  1350. break;
  1351. default:
  1352. continue;
  1353. }
  1354. }
  1355. switch ($sortby) {
  1356. case 'name':
  1357. usort($domains, 'sort_domain_results_by_name');
  1358. break;
  1359. case 'type':
  1360. usort($domains, 'sort_domain_results_by_type');
  1361. break;
  1362. case 'content':
  1363. usort($domains, 'sort_domain_results_by_content');
  1364. break;
  1365. case 'prio':
  1366. usort($domains, 'sort_domain_results_by_prio');
  1367. break;
  1368. case 'ttl':
  1369. usort($domains, 'sort_domain_results_by_ttl');
  1370. break;
  1371. default:
  1372. usort($domains, 'sort_domain_results_by_name');
  1373. break;
  1374. }
  1375. $results = array_merge($soa, $ns);
  1376. $results = array_merge($results, $domains);
  1377. return $results;
  1378. }
  1379. /** Sort records by name
  1380. *
  1381. * @param mixed[] $a A
  1382. * @param mixed[] $b B
  1383. *
  1384. * @return mixed[] result of strnatcmp
  1385. */
  1386. function sort_domain_results_by_name($a, $b) {
  1387. return strnatcmp($a['name'], $b['name']);
  1388. }
  1389. /** Sort records by type
  1390. *
  1391. * @param mixed[] $a A
  1392. * @param mixed[] $b B
  1393. *
  1394. * @return mixed[] result of strnatcmp
  1395. */
  1396. function sort_domain_results_by_type($a, $b) {
  1397. if ($a['type'] != $b['type']) {
  1398. return strnatcmp($a['type'], $b['type']);
  1399. } else {
  1400. return strnatcmp($a['name'], $b['name']);
  1401. }
  1402. }
  1403. /** Sort records by content
  1404. *
  1405. * @param mixed[] $a A
  1406. * @param mixed[] $b B
  1407. *
  1408. * @return mixed[] result of strnatcmp
  1409. */
  1410. function sort_domain_results_by_content($a, $b) {
  1411. if ($a['content'] != $b['content']) {
  1412. return strnatcmp($a['content'], $b['content']);
  1413. } else {
  1414. return strnatcmp($a['name'], $b['name']);
  1415. }
  1416. }
  1417. /** Sort records by prio
  1418. *
  1419. * @param mixed[] $a A
  1420. * @param mixed[] $b B
  1421. *
  1422. * @return mixed[] result of strnatcmp
  1423. */
  1424. function sort_domain_results_by_prio($a, $b) {
  1425. if ($a['prio'] != $b['prio']) {
  1426. return strnatcmp($a['prio'], $b['prio']);
  1427. } else {
  1428. return strnatcmp($a['name'], $b['name']);
  1429. }
  1430. }
  1431. /** Sort records by TTL
  1432. *
  1433. * @param mixed[] $a A
  1434. * @param mixed[] $b B
  1435. *
  1436. * @return mixed[] result of strnatcmp
  1437. */
  1438. function sort_domain_results_by_ttl($a, $b) {
  1439. if ($a['ttl'] != $b['ttl']) {
  1440. return strnatcmp($a['ttl'], $b['ttl']);
  1441. } else {
  1442. return strnatcmp($a['name'], $b['name']);
  1443. }
  1444. }
  1445. /** Get list of owners for Domain ID
  1446. *
  1447. * @param int $id Domain ID
  1448. *
  1449. * @return mixed[] array of owners [id,fullename]
  1450. */
  1451. function get_users_from_domain_id($id) {
  1452. global $db;
  1453. $owners = array();
  1454. $sqlq = "SELECT owner FROM zones WHERE domain_id =" . $db->quote($id, 'integer');
  1455. $id_owners = $db->query($sqlq);
  1456. if ($id_owners) {
  1457. while ($r = $id_owners->fetchRow()) {
  1458. $fullname = $db->queryOne("SELECT fullname FROM users WHERE id=" . $r['owner']);
  1459. $owners[] = array(
  1460. "id" => $r['owner'],
  1461. "fullname" => $fullname
  1462. );
  1463. }
  1464. } else {
  1465. return -1;
  1466. }
  1467. return $owners;
  1468. }
  1469. /**
  1470. * Search for Zones and/or Records
  1471. *
  1472. * @param array $parameters Array with parameters which configures function
  1473. * @param string $permission_view User permitted to view 'all' or 'own' zones
  1474. * @param string $sort_zones_by Column to sort zone results
  1475. * @param string $sort_records_by Column to sort record results
  1476. * @return array|bool
  1477. */
  1478. function search_zone_and_record($parameters, $permission_view, $sort_zones_by, $sort_records_by) {
  1479. global $db;
  1480. $return = array('zones' => array(), 'records' => array());
  1481. if ($parameters['reverse']) {
  1482. if (filter_var($parameters['query'], FILTER_FLAG_IPV4)) {
  1483. $reverse_search_string = implode('.', array_reverse(explode('.', $parameters['query'])));
  1484. } elseif (filter_var($parameters['query'], FILTER_FLAG_IPV6)) {
  1485. $reverse_search_string = unpack('H*hex', inet_pton($parameters['query']));
  1486. $reverse_search_string = implode('.', array_reverse(str_split($reverse_search_string['hex'])));
  1487. } else {
  1488. $parameters['reverse'] = false;
  1489. $reverse_search_string = '';
  1490. }
  1491. $reverse_search_string = $db->quote('%' . $reverse_search_string . '%', 'text');
  1492. }
  1493. $search_string = ($parameters['wildcard'] ? '%' : '') . trim($parameters['query']) . ($parameters['wildcard'] ? '%' : '');
  1494. if ($parameters['zones']) {
  1495. $zonesQuery = '
  1496. SELECT
  1497. domains.id,
  1498. domains.name,
  1499. domains.type,
  1500. z.id as zone_id,
  1501. z.domain_id,
  1502. z.owner,
  1503. u.id as user_id,
  1504. u.fullname,
  1505. record_count.count_records
  1506. FROM
  1507. domains
  1508. LEFT JOIN zones z on domains.id = z.domain_id
  1509. LEFT JOIN users u on z.owner = u.id
  1510. LEFT JOIN (SELECT COUNT(domain_id) AS count_records, domain_id FROM records WHERE type IS NOT NULL GROUP BY domain_id) record_count ON record_count.domain_id=domains.id
  1511. WHERE
  1512. (domains.name LIKE ' . $db->quote($search_string, 'text') .
  1513. ($parameters['reverse'] ? ' OR domains.name LIKE ' . $reverse_search_string : '') . ') ' .
  1514. ($permission_view == 'own' ? ' AND z.owner = ' . $db->quote($_SESSION['userid'], 'integer') : '') .
  1515. ' ORDER BY ' . $sort_zones_by;
  1516. $zonesResponse = $db->query($zonesQuery);
  1517. if (PEAR::isError($zonesResponse)) {
  1518. error($zonesResponse->getMessage());
  1519. return false;
  1520. }
  1521. while ($zone = $zonesResponse->fetchRow()) {
  1522. $return['zones'][] = $zone;
  1523. }
  1524. }
  1525. if ($parameters['records']) {
  1526. $recordsQuery = '
  1527. SELECT
  1528. records.id,
  1529. records.domain_id,
  1530. records.name,
  1531. records.type,
  1532. records.content,
  1533. records.ttl,
  1534. records.prio,
  1535. z.id as zone_id,
  1536. z.owner,
  1537. u.id as user_id,
  1538. u.fullname
  1539. FROM
  1540. records
  1541. LEFT JOIN zones z on records.domain_id = z.domain_id
  1542. LEFT JOIN users u on z.owner = u.id
  1543. WHERE
  1544. (records.name LIKE ' . $db->quote($search_string, 'text') . ' OR records.content LIKE ' . $db->quote($search_string, 'text') .
  1545. ($parameters['reverse'] ? ' OR records.name LIKE ' . $reverse_search_string . ' OR records.content LIKE ' . $reverse_search_string : '') . ')' .
  1546. ($permission_view == 'own' ? 'AND z.owner = ' . $db->quote($_SESSION['userid'], 'integer') : '') .
  1547. ' ORDER BY ' . $sort_records_by;
  1548. $recordsResponse = $db->query($recordsQuery);
  1549. if (PEAR::isError($recordsResponse)) {
  1550. error($recordsResponse->getMessage());
  1551. return false;
  1552. }
  1553. while ($record = $recordsResponse->fetchRow()) {
  1554. $return['records'][] = $record;
  1555. }
  1556. }
  1557. return $return;
  1558. }
  1559. /** Get Domain Type for Domain ID
  1560. *
  1561. * @param int $id Domain ID
  1562. *
  1563. * @return string Domain Type [NATIVE,MASTER,SLAVE]
  1564. */
  1565. function get_domain_type($id) {
  1566. global $db;
  1567. if (is_numeric($id)) {
  1568. $type = $db->queryOne("SELECT type FROM domains WHERE id = " . $db->quote($id, 'integer'));
  1569. if ($type == "") {
  1570. $type = "NATIVE";
  1571. }
  1572. return $type;
  1573. } else {
  1574. error(sprintf(ERR_INV_ARG, "get_record_from_id", "no or no valid zoneid given"));
  1575. }
  1576. }
  1577. /** Get Slave Domain's Master
  1578. *
  1579. * @param int $id Domain ID
  1580. *
  1581. * @return string Master server
  1582. */
  1583. function get_domain_slave_master($id) {
  1584. global $db;
  1585. if (is_numeric($id)) {
  1586. $slave_master = $db->queryOne("SELECT master FROM domains WHERE type = 'SLAVE' and id = " . $db->quote($id, 'integer'));
  1587. return $slave_master;
  1588. } else {
  1589. error(sprintf(ERR_INV_ARG, "get_domain_slave_master", "no or no valid zoneid given"));
  1590. }
  1591. }
  1592. /** Change Zone Type
  1593. *
  1594. * @param string $type New Zone Type [NATIVE,MASTER,SLAVE]
  1595. * @param int $id Zone ID
  1596. *
  1597. * @return null
  1598. */
  1599. function change_zone_type($type, $id) {
  1600. global $db;
  1601. $add = '';
  1602. if (is_numeric($id)) {
  1603. // It is not really neccesary to clear the field that contains the IP address
  1604. // of the master if the type changes from slave to something else. PowerDNS will
  1605. // ignore the field if the type isn't something else then slave. But then again,
  1606. // it's much clearer this way.
  1607. if ($type != "SLAVE") {
  1608. $add = ", master=" . $db->quote('', 'text');
  1609. }
  1610. $result = $db->query("UPDATE domains SET type = " . $db->quote($type, 'text') . $add . " WHERE id = " . $db->quote($id, 'integer'));
  1611. } else {
  1612. error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
  1613. }
  1614. }
  1615. /** Change Slave Zone's Master IP Address
  1616. *
  1617. * @param int $zone_id Zone ID
  1618. * @param string $ip_slave_master Master IP Address
  1619. *
  1620. * @return null
  1621. */
  1622. function change_zone_slave_master($zone_id, $ip_slave_master) {
  1623. global $db;
  1624. if (is_numeric($zone_id)) {
  1625. if (are_multipe_valid_ips($ip_slave_master)) {
  1626. $result = $db->query("UPDATE domains SET master = " . $db->quote($ip_slave_master, 'text') . " WHERE id = " . $db->quote($zone_id, 'integer'));
  1627. } else {
  1628. error(sprintf(ERR_INV_ARGC, "change_domain_ip_slave_master", "This is not a valid IPv4 or IPv6 address: $ip_slave_master"));
  1629. }
  1630. } else {
  1631. error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
  1632. }
  1633. }
  1634. /** Get Serial for Zone ID
  1635. *
  1636. * @param int $zid Zone ID
  1637. *
  1638. * @return boolean|string Serial Number or false if not found
  1639. */
  1640. function get_serial_by_zid($zid) {
  1641. global $db;
  1642. if (is_numeric($zid)) {
  1643. $query = "SELECT content FROM records where TYPE = " . $db->quote('SOA', 'text') . " and domain_id = " . $db->quote($zid, 'integer');
  1644. $rr_soa = $db->queryOne($query);
  1645. if (PEAR::isError($rr_soa)) {
  1646. error($rr_soa->getMessage());
  1647. return false;
  1648. }
  1649. $rr_soa_fields = explode(" ", $rr_soa);
  1650. } else {
  1651. error(sprintf(ERR_INV_ARGC, "get_serial_by_zid", "id must be a number"));
  1652. return false;
  1653. }
  1654. return $rr_soa_fields[2];
  1655. }
  1656. /** Validate Account is valid string
  1657. *
  1658. * @param string $account Account name alphanumeric and ._-
  1659. *
  1660. * @return boolean true is valid, false otherwise
  1661. */
  1662. function validate_account($account) {
  1663. if (preg_match("/^[A-Z0-9._-]+$/i", $account)) {
  1664. return true;
  1665. } else {
  1666. return false;
  1667. }
  1668. }
  1669. /** Get Zone Template ID for Zone ID
  1670. *
  1671. * @param int $zone_id Zone ID
  1672. *
  1673. * @return int Zone Template ID
  1674. */
  1675. function get_zone_template($zone_id) {
  1676. global $db;
  1677. $query = "SELECT zone_templ_id FROM zones WHERE domain_id = " . $db->quote($zone_id, 'integer');
  1678. $zone_templ_id = $db->queryOne($query);
  1679. return $zone_templ_id;
  1680. }
  1681. /** Update Zone Templatea ID for Zone ID
  1682. *
  1683. * @param int $zone_id Zone ID
  1684. * @param int $new_zone_template_id New Zone Template ID
  1685. *
  1686. * @return boolean true on success, false otherwise
  1687. */
  1688. function update_zone_template($zone_id, $new_zone_template_id) {
  1689. global $db;
  1690. $query = "UPDATE zones
  1691. SET zone_templ_id = " . $db->quote($new_zone_template_id, 'integer') . "
  1692. WHERE id = " . $db->quote($zone_id, 'integer');
  1693. $response = $db->query($query);
  1694. if (PEAR::isError($response)) {
  1695. error($response->getMessage());
  1696. return false;
  1697. }
  1698. return true;
  1699. }
  1700. /** Update All Zone Records for Zone ID with Zone Template
  1701. *
  1702. * @param int $zone_id Zone ID to update
  1703. * @param int $zone_template_id Zone Template ID to use for update
  1704. *
  1705. * @return null
  1706. */
  1707. function update_zone_records($zone_id, $zone_template_id) {
  1708. global $db;
  1709. global $dns_ttl;
  1710. global $db_type;
  1711. if (do_hook('verify_permission' , 'zone_content_edit_others' )) {
  1712. $perm_edit = "all";
  1713. } elseif (do_hook('verify_permission' , 'zone_content_edit_own' )) {
  1714. $perm_edit = "own";
  1715. } else {
  1716. $perm_edit = "none";
  1717. }
  1718. $user_is_zone_owner = do_hook('verify_user_is_owner_zoneid' , $zone_id );
  1719. if (do_hook('verify_permission' , 'zone_master_add' )) {
  1720. $zone_master_add = "1";
  1721. }
  1722. if (do_hook('verify_permission' , 'zone_slave_add' )) {
  1723. $zone_slave_add = "1";
  1724. }
  1725. $soa_rec = get_soa_record($zone_id);
  1726. $response = $db->beginTransaction();
  1727. if (0 != $zone_template_id) {
  1728. if ($perm_edit == "all" || ( $perm_edit == "own" && $user_is_zone_owner == "1")) {
  1729. if (is_numeric($zone_id)) {
  1730. $db->exec("DELETE FROM records WHERE id IN (SELECT record_id FROM records_zone_templ WHERE "
  1731. . "domain_id = " . $db->quote($zone_id, 'integer') . ")");
  1732. $db->exec("DELETE FROM records_zone_templ WHERE domain_id = " . $db->quote($zone_id, 'integer'));
  1733. } else {
  1734. error(sprintf(ERR_INV_ARGC, "delete_domain", "id must be a number"));
  1735. }
  1736. } else {
  1737. error(ERR_PERM_DEL_ZONE);
  1738. }
  1739. if ($zone_master_add == "1" || $zone_slave_add == "1") {
  1740. $domain = get_zone_name_from_id($zone_id);
  1741. $now = time();
  1742. $templ_records = get_zone_templ_records($zone_template_id);
  1743. if ($templ_records == -1) {
  1744. return;
  1745. }
  1746. foreach ($templ_records as $r) {
  1747. //fixme: appears to be a bug and regex match should occur against $domain
  1748. if ((preg_match('/in-addr.arpa/i', $zone_id) && ($r["type"] == "NS" || $r["type"] == "SOA")) || (!preg_match('/in-addr.arpa/i', $zone_id))) {
  1749. $name = parse_template_value($r["name"], $domain);
  1750. $type = $r["type"];
  1751. if ($type == "SOA") {
  1752. $db->exec("DELETE FROM records WHERE domain_id = " . $db->quote($zone_id, 'integer') . " AND type = 'SOA'");
  1753. $content = get_updated_soa_record($soa_rec);
  1754. } else {
  1755. $content = parse_template_value($r["content"], $domain);
  1756. }
  1757. $ttl = $r["ttl"];
  1758. $prio = intval($r["prio"]);
  1759. if (!$ttl) {
  1760. $ttl = $dns_ttl;
  1761. }
  1762. $query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES ("
  1763. . $db->quote($zone_id, 'integer') . ","
  1764. . $db->quote($name, 'text') . ","
  1765. . $db->quote($type, 'text') . ","
  1766. . $db->quote($content, 'text') . ","
  1767. . $db->quote($ttl, 'integer') . ","
  1768. . $db->quote($prio, 'integer') . ","
  1769. . $db->quote($now, 'integer') . ")";
  1770. $response = $db->exec($query);
  1771. if ($db_type == 'pgsql') {
  1772. $record_id = $db->lastInsertId('records_id_seq');
  1773. } else {
  1774. $record_id = $db->lastInsertId();
  1775. }
  1776. $query = "INSERT INTO records_zone_templ (domain_id, record_id, zone_templ_id) VALUES ("
  1777. . $db->quote($zone_id, 'integer') . ","
  1778. . $db->quote($record_id, 'integer') . ","
  1779. . $db->quote($zone_template_id, 'integer') . ")";
  1780. $response = $db->query($query);
  1781. }
  1782. }
  1783. }
  1784. }
  1785. $query = "UPDATE zones
  1786. SET zone_templ_id = " . $db->quote($zone_template_id, 'integer') . "
  1787. WHERE domain_id = " . $db->quote($zone_id, 'integer');
  1788. $response = $db->exec($query);
  1789. if (PEAR::isError($response)) {
  1790. $response = $db->rollback();
  1791. } else {
  1792. $response = $db->commit();
  1793. }
  1794. }
  1795. /** Delete array of domains
  1796. *
  1797. * Deletes a domain by a given id.
  1798. * Function always succeeds. If the field is not found in the database, thats what we want anyway.
  1799. *
  1800. * @param int[] $domains Array of Domain IDs to delete
  1801. *
  1802. * @return boolean true on success, false otherwise
  1803. */
  1804. function delete_domains($domains) {
  1805. global $db;
  1806. global $pdnssec_use;
  1807. $error = false;
  1808. $return = false;
  1809. $response = $db->beginTransaction();
  1810. foreach ($domains as $id) {
  1811. if (do_hook('verify_permission' , 'zone_content_edit_others' )) {
  1812. $perm_edit = "all";
  1813. } elseif (do_hook('verify_permission' , 'zone_content_edit_own' )) {
  1814. $perm_edit = "own";
  1815. } else {
  1816. $perm_edit = "none";
  1817. }
  1818. $user_is_zone_owner = do_hook('verify_user_is_owner_zoneid' , $id );
  1819. if ($perm_edit == "all" || ( $perm_edit == "own" && $user_is_zone_owner == "1")) {
  1820. if (is_numeric($id)) {
  1821. $zone_type = get_domain_type($id);
  1822. if ($pdnssec_use && $zone_type == 'MASTER') {
  1823. $zone_name = get_zone_name_from_id($id);
  1824. dnssec_unsecure_zone($zone_name);
  1825. }
  1826. $db->exec("DELETE FROM zones WHERE domain_id=" . $db->quote($id, 'integer'));
  1827. $db->exec("DELETE FROM records WHERE domain_id=" . $db->quote($id, 'integer'));
  1828. $db->query("DELETE FROM records_zone_templ WHERE domain_id=" . $db->quote($id, 'integer'));
  1829. $db->exec("DELETE FROM domains WHERE id=" . $db->quote($id, 'integer'));
  1830. } else {
  1831. error(sprintf(ERR_INV_ARGC, "delete_domains", "id must be a number"));
  1832. $error = true;
  1833. }
  1834. } else {
  1835. error(ERR_PERM_DEL_ZONE);
  1836. $error = true;
  1837. }
  1838. }
  1839. if (PEAR::isError($response)) {
  1840. $response = $db->rollback();
  1841. $commit = false;
  1842. } else {
  1843. $response = $db->commit();
  1844. $commit = true;
  1845. }
  1846. if (true == $commit && false == $error) {
  1847. $return = true;
  1848. }
  1849. return $return;
  1850. }
  1851. /** Check if record exists
  1852. *
  1853. * @param string $name Record name
  1854. *
  1855. * @return boolean true on success, false on failure
  1856. */
  1857. function record_name_exists($name) {
  1858. global $db;
  1859. $query = "SELECT COUNT(id) FROM records WHERE name = " . $db->quote($name, 'text');
  1860. $count = $db->queryOne($query);
  1861. return ($count == "1" ? true : false);
  1862. }
  1863. /** Return domain level for given name
  1864. *
  1865. * @param string $name Zone name
  1866. *
  1867. * @return int domain level
  1868. */
  1869. function get_domain_level($name) {
  1870. return substr_count($name, '.') + 1;
  1871. }
  1872. /** Return domain second level domain for given name
  1873. *
  1874. * @param string $name Zone name
  1875. *
  1876. * @return string 2nd level domain name
  1877. */
  1878. function get_second_level_domain($name) {
  1879. $domain_parts = explode('.', $name);
  1880. $domain_parts = array_reverse($domain_parts);
  1881. return $domain_parts[1] . '.' . $domain_parts[0];
  1882. }
  1883. /** Get zone list which use templates
  1884. *
  1885. * @param resource $db DB link
  1886. *
  1887. * @return mixed[] Array with domain and template ids
  1888. */
  1889. function get_zones_with_templates($db) {
  1890. $query = "SELECT id, domain_id, zone_templ_id FROM zones WHERE zone_templ_id <> 0";
  1891. $result = $db->query($query);
  1892. $zones = array();
  1893. while ($zone = $result->fetchRow()) {
  1894. $zones[]=$zone;
  1895. }
  1896. return $zones;
  1897. }
  1898. /** Get records by domain id
  1899. *
  1900. *
  1901. */
  1902. function get_records_by_domain_id($db, $domain_id) {
  1903. $query = "SELECT id, name, type, content FROM records WHERE domain_id = " . $db->quote($domain_id, 'integer');
  1904. $result = $db->query($query);
  1905. $records = array();
  1906. while ($zone_records = $result->fetchRow()) {
  1907. $records[]=$zone_records;
  1908. }
  1909. return $records;
  1910. }
  1911. /** Set timezone (required for PHP5)
  1912. *
  1913. * Set timezone to configured tz or UTC it not set
  1914. *
  1915. * @return null
  1916. */
  1917. function set_timezone() {
  1918. global $timezone;
  1919. if (function_exists('date_default_timezone_set')) {
  1920. if (isset($timezone)) {
  1921. date_default_timezone_set($timezone);
  1922. } else if (!ini_get('date.timezone')) {
  1923. date_default_timezone_set('UTC');
  1924. }
  1925. }
  1926. }