@@ -2202,6 +2202,79 @@ function api_system_hostname() {
22022202 }
22032203}
22042204
2205+ function api_system_dns_modify () {
2206+ # VARIABLES
2207+ global $ err_lib , $ config , $ api_resp , $ client_params , $ client_id ;
2208+ $ read_only_action = false ; // Set whether this action requires read only access
2209+ $ req_privs = array ("page-all " , "page-system " ); // Array of privs allowed
2210+ $ http_method = $ _SERVER ['REQUEST_METHOD ' ]; // Save our HTTP method
2211+ $ dns_ent = [];
2212+ # RUN TIME
2213+ // Check that client is authenticated and authorized
2214+ if (api_authorized ($ req_privs , $ read_only_action )) {
2215+ // Check that our HTTP method is POST (UPDATE)
2216+ if ($ http_method === 'POST ' ) {
2217+ if (isset ($ client_params ['dnsserver ' ])) {
2218+ $ dns_servers = $ client_params ['dnsserver ' ];
2219+ // If value is not an array, convert it
2220+ if (!is_array ($ dns_servers )) {
2221+ $ dns_servers = array ($ dns_servers );
2222+ }
2223+ // Loop through our DNS servers and check that entry is valid
2224+ foreach ($ dns_servers as $ ds ) {
2225+ // Check if our DNS server is valid
2226+ if (!is_ipaddrv4 ($ ds ) and !is_ipaddrv6 ($ ds )) {
2227+ $ api_resp = array ("status " => "bad request " , "code " => 400 , "return " => 1007 );
2228+ $ api_resp ["message " ] = $ err_lib [$ api_resp ["return " ]];
2229+ return $ api_resp ;
2230+ }
2231+ }
2232+ // Add our system DNS values to config and track the change
2233+ $ config ["system " ]["dnsserver " ] = $ dns_servers ;
2234+ $ dns_ent ["dnsserver " ] = $ dns_servers ;
2235+ }
2236+ if ($ client_params ['dnsallowoverride ' ] === true ) {
2237+ $ config ["system " ]["dnsallowoverride " ] = "" ;
2238+ $ dns_ent ["dnsallowoverride " ] = $ client_params ['dnsallowoverride ' ];
2239+ } elseif ($ client_params ['dnsallowoverride ' ] === false ) {
2240+ $ dns_ent ["dnsallowoverride " ] = $ client_params ['dnsallowoverride ' ];
2241+ unset($ config ["system " ]["dnsallowoverride " ]);
2242+ }
2243+ if ($ client_params ['dnslocalhost ' ] === true ) {
2244+ $ config ["system " ]["dnslocalhost " ] = "" ;
2245+ $ dns_ent ["dnslocalhost " ] = $ client_params ['dnslocalhost ' ];
2246+ } elseif ($ client_params ['dnslocalhost ' ] === false ) {
2247+ $ dns_ent ["dnslocalhost " ] = $ client_params ['dnslocalhost ' ];
2248+ unset($ config ["system " ]["dnslocalhost " ]);
2249+ }
2250+ // Write our new hostname
2251+ $ _SESSION ["Username " ] = $ client_id ; // Save our CLIENT ID to session data for logging
2252+ $ change_note = " Modified system DNS servers via API " ; // Add a change note
2253+ write_config (sprintf (gettext ($ change_note ))); // Apply our configuration change
2254+ // Update a slew of backend services
2255+ system_resolvconf_generate ();
2256+ if (isset ($ config ['dnsmasq ' ]['enable ' ])) {
2257+ services_dnsmasq_configure ();
2258+ } elseif (isset ($ config ['unbound ' ]['enable ' ])) {
2259+ services_unbound_configure ();
2260+ }
2261+ send_event ("service reload dns " );
2262+ filter_configure ();
2263+ // Print our JSON response
2264+ $ api_resp = array ("status " => "ok " , "code " => 200 , "return " => 0 );
2265+ $ api_resp ["message " ] = "Successfully modified system DNS servers " ;
2266+ $ api_resp ["data " ] = $ dns_ent ;
2267+ return $ api_resp ;
2268+ } else {
2269+ $ api_resp = array ("status " => "bad request " , "code " => 400 , "return " => 2 );
2270+ $ api_resp ["message " ] = $ err_lib [$ api_resp ["return " ]];
2271+ return $ api_resp ;
2272+ }
2273+ } else {
2274+ return $ api_resp ;
2275+ }
2276+ }
2277+
22052278function api_system_hostname_modify () {
22062279 # VARIABLES
22072280 global $ err_lib , $ config , $ api_resp , $ client_params , $ client_id ;
@@ -2268,6 +2341,44 @@ function api_system_hostname_modify() {
22682341 }
22692342}
22702343
2344+ function api_system_dns () {
2345+ # VARIABLES
2346+ global $ err_lib , $ config , $ api_resp , $ client_params ;
2347+ $ read_only_action = true ; // Set whether this action requires read only access
2348+ $ req_privs = array ("page-all " , "page-system " ); // Allowed privs
2349+ $ http_method = $ _SERVER ['REQUEST_METHOD ' ]; // Save our HTTP method
2350+ $ dns_array = array (); // Init our return array
2351+ # RUN TIME
2352+ // Check that client is authenticated and authorized
2353+ if (api_authorized ($ req_privs , $ read_only_action )) {
2354+ // Check that our HTTP method is GET (READ)
2355+ if ($ http_method === 'GET ' ) {
2356+ // Check that we have a configuration
2357+ if (!empty ($ config ["system " ])) {
2358+ $ dns_keys = ["dnsserver " , "dnsallowoverride " , "dnslocalhost " ];
2359+ foreach ($ config ["system " ] as $ key => $ sv ) {
2360+ if (in_array ($ key , $ dns_keys )) {
2361+ $ dns_array [$ key ] = $ sv ;
2362+ }
2363+ }
2364+ }
2365+ if (isset ($ client_params ['search ' ])) {
2366+ $ search = $ client_params ['search ' ];
2367+ $ dns_array = api_extended_search ($ dns_array , $ search );
2368+ }
2369+ // Print our JSON response
2370+ $ api_resp = array ("status " => "ok " , "code " => 200 , "return " => 0 , "message " => "" , "data " => $ dns_array );
2371+ return $ api_resp ;
2372+ } else {
2373+ $ api_resp = array ("status " => "bad request " , "code " => 400 , "return " => 2 );
2374+ $ api_resp ["message " ] = $ err_lib [$ api_resp ["return " ]];
2375+ return $ api_resp ;
2376+ }
2377+ } else {
2378+ return $ api_resp ;
2379+ }
2380+ }
2381+
22712382function api_system_certificates () {
22722383 # VARIABLES
22732384 global $ err_lib , $ config , $ api_resp , $ client_params ;
0 commit comments