├── .gitignore ├── AUTHORS ├── README.md ├── COPYING └── bin └── cloudns_api.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Originally created by Toki Winter 2 | 3 | Maintained by: 4 | Wagner Sartori Junior 5 | Toki Winter 6 | 7 | Contributed to this project: 8 | Daniel Lo Nigro 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cloudns-api 2 | =========== 3 | 4 | [![License: GPL](https://img.shields.io/badge/License-GPL-blue.svg)](https://www.gnu.org/licenses/gpl.html) 5 | 6 | This script has been developed to provide a simple-to-use command line interface 7 | to the JSON API provided by ClouDNS (http://cloudns.net). 8 | 9 | Installation 10 | ------------ 11 | 12 | $ git clone https://github.com/tokiwinter/cloudns-api.git 13 | $ sudo cp cloudns-api/bin/cloudns_api.sh /usr/local/bin 14 | $ sudo chmod +x /usr/local/bin/cloudns_api.sh 15 | 16 | You should also install `jq` if you don't have it already. 17 | 18 | Usage 19 | ----- 20 | 21 | Usage: cloudns_api.sh [-dfhjs] command [options] 22 | -d run in debug mode (lots of verbose messages) 23 | -f force delrecord operations without confirmation 24 | -h display this help message 25 | -j return listrecords output in JSON format 26 | -s skip testing authentication prior to attempting API operations 27 | 28 | Commands: 29 | listzones - list zones under management 30 | addzone - add a new zone 31 | delzone - delete an existing zone 32 | checkzone - check that a zone is managed 33 | dumpzone - dump a zone in BIND zonefile format 34 | dumpallzones - dump all zones in BIND zonefile format 35 | zonestatus - check whether a zone is updated on all NS 36 | nsstatus - view a breakdown of zone update status by NS 37 | addmaster - add new master server in domain zone 38 | delmaster - delete master server by ID in domain zone 39 | listmaster - list master servers in the domain zone 40 | addrecord - add a new DNS record to a zone 41 | delrecord - delete a DNS record from a zone 42 | listrecords - list zones under management 43 | modify - modify an existing DNS record 44 | getsoa - get SOA record parameters for a zone 45 | setsoa - set SOA record parameters for a zone 46 | helper - call a helper function directly 47 | test - perform an authentication test 48 | 49 | Environment: 50 | Ensure that the following two environment variables are exported: 51 | CLOUDNS_API_ID - your ClouDNS API ID (auth-id) 52 | CLOUDNS_PASSWORD - your ClouDNS API password (auth-password) 53 | 54 | Environment 55 | ----------- 56 | 57 | You should ensure that you export the following environment variables correctly. Their 58 | existence is tested by the script. 59 | 60 | $ export CLOUDNS_API_ID= 61 | $ export CLOUDNS_PASSWORD= 62 | 63 | The script does not currently support sub-auth-id, etc. See Limitations at the end of 64 | this document. This does allow us to operate globally on your account. Ensure you 65 | run this script from a trusted host, `unset` your envionment variables when done, 66 | remove the assignments from your history (`history -d `), and have appropriate 67 | IP restrictions in place on your auth-id (configurable via the ClouDNS web UI). 68 | 69 | Using the script 70 | ---------------- 71 | 72 | ### General options ### 73 | 74 | The `-d` option will cause `cloudns_api.sh` to act verbosely and print debug messages as it goes. 75 | The `-s` option will cause `cloudns_api.sh` to skip testing authentication prior to each API call. 76 | 77 | ### test ### 78 | 79 | You can execute an authentication test, to check that your environment is correctly configured, as 80 | follows: 81 | 82 | $ cloudns_api.sh test 83 | Thu Feb 23 20:33:11 AEDT 2017: Login test successful 84 | 85 | ### listzones ### 86 | 87 | #### Description #### 88 | 89 | List the zones under your account, and their type. 90 | 91 | #### Usage ##### 92 | 93 | $ cloudns_api.sh listzones 94 | 95 | #### Example #### 96 | 97 | $ cloudns_api.sh listzones 98 | testzone1.com:master 99 | testzone2.com:master 100 | 101 | ### addzone ### 102 | 103 | #### Description #### 104 | 105 | Add a new zone to your account. 106 | 107 | #### Usage #### 108 | 109 | $ cloudns_api.sh addzone 110 | 111 | #### Example #### 112 | 113 | $ cloudns_api.sh addzone foo.com 114 | Thu Feb 23 19:43:12 AEDT 2017: New zone [foo.com] added 115 | 116 | ### delzone ### 117 | 118 | Delete a zone from your account. `delzone` treats you like an idiot, as deleting an 119 | entire zone is a big deal. You cannot force this operation (with `-f`). You will be 120 | asked to enter a string, exactly, as well as wait for a 5 second timeout (to give 121 | you time to hit CTRL-C) prior to the zone being removed forever. 122 | 123 | #### Usage #### 124 | 125 | $ cloudns_api.sh delzone 126 | 127 | #### Example #### 128 | 129 | $ cloudns_api.sh delzone foo.com 130 | Are you sure you want to delete zone [foo.com]? 131 | You must type I-AM-SURE, exactly: I-AM-SURE 132 | Okay. Waiting 5s prior to removal. CTRL-C now if unsure! 133 | Thu Feb 23 19:43:39 AEDT 2017: Zone [foo.com] deleted 134 | 135 | ### checkzone ### 136 | 137 | #### Description #### 138 | 139 | Check whether domains are managed under this account. 140 | 141 | #### Usage #### 142 | 143 | $ cloudns_api.sh checkzone [ ... ] 144 | 145 | #### Example #### 146 | 147 | $ cloudns_api.sh checkzone foo.com bar.com 148 | foo.com:present 149 | bar.com:absent 150 | 151 | ### dumpzone ### 152 | 153 | #### Description #### 154 | 155 | Dump an entire zone in BIND (RFC 1035) format, ideal for redirecting to a file. 156 | 157 | #### Usage #### 158 | 159 | $ cloudns_api.sh dumpzone 160 | 161 | #### Example #### 162 | 163 | $ cloudns_api.sh dumpzone foo.com 164 | $TTL 3600 165 | @ IN SOA ns1.cloudns.net. support.cloudns.net. 2017022309 7200 1800 1209600 3600 166 | @ 3600 IN NS ns1.cloudns.net. 167 | .... 168 | 169 | ### dumpallzones ### 170 | 171 | #### Description #### 172 | 173 | Similar to `dumpzone`, except it dumps **all** zones into a particular directory. 174 | This is useful for backing up your entire account. The output directory will be 175 | created if it does not already exist. 176 | 177 | #### Usage #### 178 | 179 | $ cloudns_api.sh dumpzone 180 | 181 | #### Example #### 182 | 183 | $ cloudns_api.sh dumpzone /var/backup/zones/ 184 | 185 | 186 | ### zonestatus ### 187 | 188 | #### Description #### 189 | 190 | Check whether a zone is updated on all nameservers. 191 | 192 | #### Usage #### 193 | 194 | $ cloudns_api.sh zonestatus [ ... ] 195 | 196 | #### Example #### 197 | 198 | $ cloudns_api.sh zonestatus foo.com bar.com tokitest.com 199 | foo.com:up-to-date 200 | bar.com:not-valid 201 | tokitest.com:out-of-date 202 | 203 | ### nsstatus ### 204 | 205 | #### Description #### 206 | 207 | View the zone update status per nameserver. 208 | 209 | #### Usage #### 210 | 211 | $ cloudns_api.sh nsstatus 212 | 213 | #### Example #### 214 | 215 | $ cloudns_api.sh nsstatus tokitest.com 216 | ns1.cloudns.net:true 217 | ns2.cloudns.net:false 218 | ns3.cloudns.net:true 219 | ns4.cloudns.net:true 220 | pns1.cloudns.net:true 221 | pns2.cloudns.net:true 222 | pns3.cloudns.net:true 223 | pns4.cloudns.net:true 224 | 225 | ### listmaster ### 226 | 227 | #### Description #### 228 | 229 | List master ips on slave zones. IDs are not shown by default, specify `showid=true` to 230 | display them. 231 | 232 | #### Usage ##### 233 | 234 | $ cloudns_api.sh listmaster [showid=] 235 | 236 | #### Example #### 237 | 238 | $ cloudns_api.sh listmaster example.com showid=true 239 | 1.2.3.4 ; id=567890 240 | 5.6.7.8 ; id=123456 241 | 242 | ### addmaster ### 243 | 244 | #### Description #### 245 | 246 | Add master ip to slave zones. 247 | 248 | #### Usage ##### 249 | 250 | $ cloudns_api.sh addmaster 251 | 252 | #### Example #### 253 | 254 | $ cloudns_api.sh addmaster example.com 1.2.3.4 255 | Wed Nov 8 16:50:40 CET 2017: Master IP was added successfully to zone [example.com] 256 | 257 | ### delmaster ### 258 | 259 | #### Description #### 260 | 261 | Remove master ip from slave zones. You will be asked to confirm the operation. You 262 | can avoid this, and force the removal, with `-f`. Obtain the record id using the `listmaster` 263 | command. 264 | 265 | #### Usage ##### 266 | 267 | $ cloudns_api.sh delmaster 268 | 269 | #### Example #### 270 | 271 | $ cloudns_api.sh delmaster example.com id=567890 272 | Are you sure you want to delete master with id [567890]? [y|n]: y 273 | Wed Nov 8 16:51:18 CET 2017: Master successfully deleted 274 | 275 | ### addrecord ### 276 | 277 | #### Description #### 278 | 279 | Add a new resource record to a zone. 280 | 281 | #### Usage #### 282 | 283 | Options required depend upon the record type being added. The key=value pairs can appear 284 | in any order. 285 | 286 | For A, AAAA, NS, CNAME, SPF, TXT: 287 | 288 | $ cloudns_api.sh addrecord type= host= record= ttl= 289 | 290 | SPF and TXT records are a special case. As their record data could include whitespace and 291 | other characters, you should create a single-line file containing the record data. See Examples 292 | below. 293 | 294 | For MX: 295 | 296 | $ cloudns_api.sh addrecord type=MX host= record= ttl= priority= 297 | 298 | For SRV: 299 | 300 | $ cloudns_api.sh addrecord type=SRV host= record= ttl= priority= weight= port= 301 | 302 | If you want an apex record, specify `host=@`. 303 | 304 | #### Example #### 305 | 306 | For non-SPF/TXT records, here a CNAME (some limited validation of values occurs in the script, 307 | and then the API does its own validation): 308 | 309 | $ cloudns_api.sh addrecord foo.com type=CNAME host=testing record=cnametarget.foo.com. ttl=60 310 | Thu Feb 23 20:01:20 AEDT 2017: Record successfully added with id [24135067] 311 | 312 | For SPF/TXT records, add your record data to a file, on a single line, without enclosing quotes. 313 | Reference that in the `record=` key=value pair: 314 | 315 | $ echo "This isn't text. Oh, it is actually" > /var/tmp/txt.txt 316 | $ cloudns_api.sh addrecord foo.com type=TXT host=testtext record=/var/tmp/txt.txt ttl=60 317 | Thu Feb 23 20:02:51 AEDT 2017: Record successfully added with id [24135071] 318 | $ rm -f /var/tmp/txt.txt 319 | 320 | ### delrecord ### 321 | 322 | #### Description #### 323 | 324 | Delete a resource record from a zone by id. You will be asked to confirm the operation. You 325 | can avoid this, and force the removal, with `-f`. Obtain the record id using the `listrecords` 326 | command. 327 | 328 | #### Usage #### 329 | 330 | $ cloudns_api.sh [-f] delrecord id= 331 | 332 | #### Example #### 333 | 334 | $ cloudns_api.sh delrecord foo.com id=24135071 335 | Are you sure you want to delete record with id [24135071]? [y|n]: y 336 | Thu Feb 23 20:05:46 AEDT 2017: Record successfully deleted 337 | 338 | ### modify ### 339 | 340 | #### Description #### 341 | 342 | Modify an existing resource record. You must specify at least one attribute to modify. You 343 | must know the record id (use the `listrecords` command to obtain this). The key=value pairs 344 | are the same as with `addrecord`. You cannot modify a record's `type`, so don't try that. 345 | If you want to change `record` for an SPF or TXT record, the same loading-from-file 346 | mechanism as with `addrecord` applies to `modify` too. 347 | 348 | You can modify as many attributes as you want in a single invocation. If you specify the 349 | same attribute twice, the latest specification will be used. The order of attribute specification 350 | does not matter. 351 | 352 | #### Usage #### 353 | 354 | $ cloudns_api.sh modify id= key= [key= ...] 355 | 356 | #### Example #### 357 | 358 | $ cloudns_api.sh modify foo.com id=24135067 ttl=3600 record=newcnametarget.foo.com. 359 | Thu Feb 23 20:11:24 AEDT 2017: Record successfully modified 360 | 361 | ### listrecords ### 362 | 363 | #### Description #### 364 | 365 | List records for a specified zone in either BIND (RFC 1035) format or JSON. If you want 366 | apex records, specify `host=@`. IDs are not shown by default, specify `showid=true` to 367 | display them, as a BIND-style comment. 368 | 369 | #### Usage #### 370 | 371 | $ cloudns_api.sh [-j] listrecords [host=] [type=] [showid=] 372 | 373 | #### Example #### 374 | 375 | $ cloudns_api.sh listrecords foo.com host=@ type=NS showid=true 376 | @ 3600 IN NS ns1.cloudns.net. ; id=24134077 377 | @ 3600 IN NS ns2.cloudns.net. ; id=24134078 378 | ... 379 | 380 | ### getsoa ### 381 | 382 | #### Description #### 383 | 384 | Display SOA record details for a specified zone. 385 | 386 | #### Usage #### 387 | 388 | $ cloudns_api.sh getsoa 389 | 390 | #### Example #### 391 | 392 | $ cloudns_api.sh getsoa foo.com 393 | serialNumber:2017022315 394 | primaryNS:ns1.cloudns.net 395 | adminMail:support@cloudns.net 396 | refresh:7200 397 | retry:1800 398 | expire:1209600 399 | defaultTTL:3600 400 | 401 | ### setsoa ### 402 | 403 | #### Description #### 404 | 405 | Modify the SOA record for a specified zone. You can modify one or more parameters in the same 406 | invocation. Order does not matter. 407 | 408 | #### Usage #### 409 | 410 | $ cloudns_api.sh setsoa key=value [key= ...] 411 | 412 | Valid keys are: 413 | 414 | primary-ns admin-mail refresh retry expire default-ttl 415 | 416 | #### Example #### 417 | 418 | $ cloudns_api.sh setsoa foo.com admin-mail=hostmaster@foo.com default-ttl=3600 419 | Thu Feb 23 20:20:49 AEDT 2017: default-ttl value same as existing 420 | Thu Feb 23 20:20:49 AEDT 2017: SOA for zone [foo.com] modified 421 | 422 | ### helper ### 423 | 424 | #### Description #### 425 | 426 | Execute a helper function directly. 427 | 428 | #### Usage #### 429 | 430 | $ cloudns_api.sh helper 431 | 432 | #### Example #### 433 | 434 | $ cloudns_api.sh helper get_available_ttls 435 | 60 300 900 1800 3600 21600 43200 86400 172800 259200 604800 1209600 2592000 436 | $ cloudns_api.sh helper get_record_types 437 | A AAAA MX CNAME TXT SPF NS SRV WR ALIAS RP SSHFP NAPTR 438 | 439 | Limitations 440 | ----------- 441 | 442 | Current limitations. I may further develop the functionality offered by the script if 443 | I receive enough interest. 444 | 445 | - does not support sub-auth-id 446 | - only supports forward zones 447 | - only supports creation/modification of SUPPORTED_RECORD_TYPES 448 | 449 | `SUPPORTED_RECORD_TYPES=( "A" "AAAA" "CNAME" "MX" "NS" "SPF" "SRV" "TXT" )` 450 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /bin/cloudns_api.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | # 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | # 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | 16 | API_URL="https://api.cloudns.net/dns" 17 | DEBUG=0 18 | FORCE=0 19 | JSON=0 20 | REMOVAL_WAIT=5 21 | SKIP_TESTS=0 22 | THISPROG=$( basename $0 ) 23 | 24 | ROWS_PER_PAGE=100 25 | SUPPORTED_RECORD_TYPES=( "A" "AAAA" "CNAME" "MX" "NS" "SPF" "SRV" "TXT" ) 26 | 27 | function print_error() { 28 | builtin echo "$( date ): Error: $@" >&2 29 | } 30 | 31 | function print_usage() { 32 | { 33 | builtin echo "Usage: ${THISPROG} [-dfhjs] command [options]" 34 | builtin echo " -d run in debug mode (lots of verbose messages)" 35 | builtin echo " -f force delrecord operations without confirmation" 36 | builtin echo " -h display this help message" 37 | builtin echo " -j return listrecords output in JSON format" 38 | builtin echo " -s skip testing authentication prior to attempting API operations" 39 | builtin echo "" 40 | builtin echo " Commands:" 41 | builtin echo " listzones - list zones under management" 42 | builtin echo " addzone - add a new zone" 43 | builtin echo " delzone - delete an existing zone" 44 | builtin echo " checkzone - check that a zone is managed" 45 | builtin echo " dumpzone - dump a zone in BIND zonefile format" 46 | builtin echo " dumpallzones - dump all zones in BIND zonefile format" 47 | builtin echo " zonestatus - check whether a zone is updated on all NS" 48 | builtin echo " nsstatus - view a breakdown of zone update status by NS" 49 | builtin echo " addmaster - add new master server in domain zone" 50 | builtin echo " delmaster - delete master server by ID in domain zone" 51 | builtin echo " listmaster - list master servers in the domain zone" 52 | builtin echo " addrecord - add a new DNS record to a zone" 53 | builtin echo " delrecord - delete a DNS record from a zone" 54 | builtin echo " listrecords - list records in a zone" 55 | builtin echo " modify - modify an existing DNS record" 56 | builtin echo " getsoa - get SOA record parameters for a zone" 57 | builtin echo " setsoa - set SOA record parameters for a zone" 58 | builtin echo " helper - call a helper function directly" 59 | builtin echo " test - perform an authentication test" 60 | builtin echo "" 61 | builtin echo " Environment:" 62 | builtin echo " Ensure that the following two environment variables are exported:" 63 | builtin echo " CLOUDNS_API_ID - your ClouDNS API ID (auth-id)" 64 | builtin echo " CLOUDNS_PASSWORD - your ClouDNS API password (auth-password)" 65 | } >&2 66 | } 67 | 68 | function check_jq() { 69 | 70 | if ! which jq >/dev/null 2>&1; then 71 | print_error "This program requires jq to be installed. Install it." 72 | exit 1 73 | fi 74 | } 75 | 76 | function print_timestamp() { 77 | builtin echo "$( date ): $@" 78 | } 79 | 80 | function print_debug() { 81 | (( DEBUG )) && builtin echo "$( date ): DEBUG: $@" 82 | } 83 | 84 | function process_arguments() { 85 | local COMMAND="$1" 86 | if [ -z "${COMMAND}" ]; then 87 | print_usage && exit 1 88 | fi 89 | case ${COMMAND} in 90 | "help" ) print_usage && exit 0 ;; 91 | "test" ) test_login ;; 92 | "listzones" ) list_zones ;; 93 | "addzone" ) shift 94 | add_zone "$@" ;; 95 | "delzone" ) shift 96 | delete_zone "$@" ;; 97 | "checkzone" ) shift 98 | check_zone "$@" ;; 99 | "dumpzone" ) shift 100 | dump_zone "$@" ;; 101 | "dumpallzones" ) shift 102 | dump_all_zones "$@" ;; 103 | "zonestatus" ) shift 104 | zone_status "$@" ;; 105 | "nsstatus" ) shift 106 | ns_status "$@" ;; 107 | "addmaster" ) shift 108 | add_master "$@" ;; 109 | "delmaster" ) shift 110 | delete_master "$@" ;; 111 | "listmaster" ) shift 112 | list_master "$@" ;; 113 | "addrecord" ) shift 114 | add_record "$@" ;; 115 | "delrecord" ) shift 116 | delete_record "$@" ;; 117 | "modify" ) shift 118 | modify_record "$@" ;; 119 | "listrecords" ) shift 120 | list_records "$@" ;; 121 | "getsoa" ) shift 122 | get_soa "$@" ;; 123 | "setsoa" ) shift 124 | set_soa "$@" ;; 125 | "helper" ) shift 126 | call_helper "$@" ;; 127 | * ) print_usage && exit 1 ;; 128 | esac 129 | } 130 | 131 | function check_environment_variables() { 132 | local ERROR_COUNT=0 133 | local REQUIRED_VARIABLES=( CLOUDNS_API_ID CLOUDNS_PASSWORD ) 134 | for REQUIRED_VARIABLE in ${REQUIRED_VARIABLES[@]}; do 135 | if $( builtin eval test -z \${${REQUIRED_VARIABLE}} ); then 136 | print_error "Environment variable \${${REQUIRED_VARIABLE}} unset" 137 | (( ERROR_COUNT = ERROR_COUNT + 1 )) 138 | fi 139 | done 140 | if [ "${ERROR_COUNT}" -gt "0" ]; then 141 | exit 1 142 | fi 143 | } 144 | 145 | function set_auth_post_data() { 146 | AUTH_POST_DATA="-d auth-id=${CLOUDNS_API_ID} -d auth-password=${CLOUDNS_PASSWORD}" 147 | } 148 | 149 | function test_api_url() { 150 | local HTTP_CODE=$( curl -4qs -o /dev/null -w '%{http_code}' ${API_URL}/login.json ) 151 | if [ "${HTTP_CODE}" != "200" ]; then 152 | print_error "Unable to reach ClouDNS API" && exit 1 153 | else 154 | print_debug "API availability check successful" 155 | fi 156 | } 157 | 158 | function do_login() { 159 | local STATUS=$( curl -4qs -X POST ${AUTH_POST_DATA} "${API_URL}/login.json" | jq -r '.status' ) 160 | case ${STATUS} in 161 | "Success" ) print_debug "Login successful" 162 | return 0 ;; 163 | * ) print_error "Login failed" 164 | exit 1 ;; 165 | esac 166 | } 167 | 168 | function do_tests() { 169 | if [ "${SKIP_TESTS}" -eq "0" ]; then 170 | test_api_url 171 | do_login 172 | else 173 | print_debug "-s passed - skipping tests" 174 | fi 175 | } 176 | 177 | function check_zone() { 178 | do_tests 179 | local ZONES="$@" 180 | if [ -z "${ZONES}" ]; then 181 | print_error "No zones passed to checkzone" && exit 1 182 | fi 183 | for ZONE in ${ZONES}; do 184 | print_debug "Checking zone [${ZONE}]" 185 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 186 | local OUTPUT=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/get-zone-info.json" ) 187 | if [ $( builtin echo "${OUTPUT}" | jq -r '.status' ) != "Failed" ]; then 188 | builtin echo "${ZONE}:present" 189 | else 190 | builtin echo "${ZONE}:absent" 191 | fi 192 | done 193 | } 194 | 195 | function ns_status() { 196 | do_tests 197 | if [ "$#" -ne "1" ]; then 198 | print_error "nsstatus expects exactly one argument" && exit 1 199 | fi 200 | local ZONE="$1" 201 | check_zone_managed ${ZONE} 202 | print_debug "Checking NS status for [${ZONE}]" 203 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 204 | local NS_STATUS=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/update-status.json" ) 205 | local STATUS=$( builtin echo "${NS_STATUS}" | jq -r '.status' 2>/dev/null ) 206 | if [ "${STATUS}" = "Failed" ]; then 207 | print_error "No such domain [${ZONE}]" && exit 1 208 | else 209 | builtin echo "${NS_STATUS}" | jq -r '.[] | .server + ":" + (.updated|tostring)' 210 | fi 211 | } 212 | 213 | function zone_status() { 214 | do_tests 215 | local ZONES="$@" 216 | if [ -z "${ZONES}" ]; then 217 | print_error "No zones passed to zonestatus" && exit 1 218 | fi 219 | for ZONE in ${ZONES}; do 220 | print_debug "Checking zone status for [${ZONE}]" 221 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 222 | local IS_UPDATED=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/is-updated.json" ) 223 | if [ "${IS_UPDATED}" = "true" ]; then 224 | builtin echo "${ZONE}:up-to-date" 225 | elif [ "${IS_UPDATED}" = "false" ]; then 226 | builtin echo "${ZONE}:out-of-date" 227 | else 228 | builtin echo "${ZONE}:not-valid" 229 | fi 230 | done 231 | } 232 | 233 | # we don't need to call do_tests in the get_* helper functions, as it 234 | # will have already been called in the calling function 235 | function get_page_count() { 236 | local POST_DATA="${AUTH_POST_DATA} -d rows-per-page=${ROWS_PER_PAGE}" 237 | local PAGE_COUNT=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/get-pages-count.json" ) 238 | local STATUS=$( builtin echo "${PAGE_COUNT}" | jq -r '.status' 2>/dev/null ) 239 | if [ "${STATUS}" = "Failed" ]; then 240 | print_error "API call to get-pages-count.json failed" && exit 1 241 | fi 242 | builtin echo "${PAGE_COUNT}" | grep -Eqs '^[[:digit:]]+' 243 | if [ "$?" -ne "0" ]; then 244 | print_error "Invalid response received from get-pages-count.json" && exit 1 245 | fi 246 | builtin echo "${PAGE_COUNT}" 247 | } 248 | 249 | function get_record_types() { 250 | local POST_DATA="${AUTH_POST_DATA} -d zone-type=domain" 251 | local RECORD_TYPES=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/get-available-record-types.json" ) 252 | local STATUS=$( builtin echo "${RECORD_TYPES}" | jq -r '.status' 2>/dev/null ) 253 | if [ "${STATUS}" = "Failed" ]; then 254 | print_error "API call to get-available-record-types.json failed" && exit 1 255 | fi 256 | # check for the existence of a common record type, e.g. CNAME 257 | local INDEX=$( builtin echo "${RECORD_TYPES}" | jq -r 'index("CNAME")' ) 258 | if [ "${INDEX}" = "null" ]; then 259 | print_error "RECORD_TYPES array does not contain an expected record type" 260 | exit 1 261 | fi 262 | RECORD_TYPES=$( builtin echo "${RECORD_TYPES}" | jq -r '.|join(" ")' ) 263 | builtin echo "${RECORD_TYPES}" 264 | } 265 | 266 | function get_available_ttls() { 267 | local POST_DATA="${AUTH_POST_DATA}" 268 | local TTLS=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/get-available-ttl.json" ) 269 | local STATUS=$( builtin echo "${TTLS}" | jq -r '.status' 2>/dev/null ) 270 | if [ "${STATUS}" = "Failed" ]; then 271 | print_error "API call to get-available-ttl.json failed" && exit 1 272 | fi 273 | TTLS=$( builtin echo "${TTLS}" | jq -r '[.[]|tostring]|join(" ")' ) 274 | builtin echo "${TTLS}" 275 | } 276 | 277 | function has_element() { 278 | local -n CHECK_ARRAY="$1" 279 | local CHECK_VALUE="$2" 280 | local VALUE 281 | for VALUE in ${CHECK_ARRAY[@]}; do 282 | if [ "${VALUE}" = "${CHECK_VALUE}" ]; then 283 | return 0 284 | fi 285 | done 286 | return 1 287 | } 288 | 289 | function list_records() { 290 | do_tests 291 | if [ "$#" -eq "0" ]; then 292 | print_error "listrecords expects at least one argument" && exit 1 293 | fi 294 | local ZONE="$1" 295 | if [[ "${ZONE}" =~ ^.*=.*$ ]]; then 296 | print_error "[${ZONE}] looks like a key=value pair, not a zone name" && exit 1 297 | fi 298 | check_zone_managed ${ZONE} 299 | shift 300 | if [ "$#" -gt "3" ]; then 301 | print_error "usage: ${THISPROG} listrecords [type=] [host=] [showid=]" 302 | exit 1 303 | fi 304 | if [ "$#" -gt "0" ]; then 305 | local -a VALID_KEYS=( "type" "host" "showid" ) 306 | local KV_PAIRS="$@" ERROR_COUNT=0 SHOW_ID="false" 307 | local KV_PAIR 308 | for KV_PAIR in ${KV_PAIRS}; do 309 | builtin echo "${KV_PAIR}" | grep -Eqs '^[a-z-]+=[^=]+$' 310 | if [ "$?" -ne "0" ]; then 311 | print_error "key-value pair [${KV_PAIR}] not in correct format" && exit 1 312 | fi 313 | local KEY=$( builtin echo "${KV_PAIR}" | cut -d = -f 1 ) 314 | local VALUE=$( builtin echo "${KV_PAIR}" | cut -d = -f 2 ) 315 | print_debug "Checking key-value pair: ${KEY}=${VALUE}" 316 | if ! has_element VALID_KEYS "${KEY}"; then 317 | print_error "${KEY} is not a valid key" 318 | (( ERROR_COUNT = ERROR_COUNT + 1 )) 319 | fi 320 | case ${KEY} in 321 | "type" ) local -a RECORD_TYPES=( $( get_record_types ) ) 322 | local TMPVAR="${VALUE^^}" 323 | if ! has_element RECORD_TYPES "${TMPVAR}"; then 324 | print_error "${VALUE} (${TMPVAR}) is not a valid record type" 325 | exit 1 326 | else 327 | local RECORD_TYPE="${VALUE}" 328 | fi 329 | ;; 330 | "host" ) local HOST_RECORD="${VALUE}" 331 | ;; 332 | "showid" ) case ${VALUE} in 333 | "true"|"false" ) SHOW_ID="${VALUE}" 334 | ;; 335 | * ) print_error "Invalid value for showid" 336 | exit 1 337 | ;; 338 | esac 339 | ;; 340 | esac 341 | unset KEY VALUE 342 | done 343 | fi 344 | [[ "${ERROR_COUNT}" -gt "0" ]] && exit 1 345 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 346 | if [ -n "${RECORD_TYPE}" ]; then 347 | POST_DATA="${POST_DATA} -d type=${RECORD_TYPE}" 348 | fi 349 | if [ -n "${HOST_RECORD}" -a "${HOST_RECORD}" != "@" ]; then 350 | POST_DATA="${POST_DATA} -d host=${HOST_RECORD}" 351 | fi 352 | print_debug "Fetching records for zone [${ZONE}] with type [${RECORD_TYPE:-not set}] and host [${HOST_RECORD:-not set}]" 353 | local RECORD_DATA=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/records.json" ) 354 | local RESULT_LENGTH=$( builtin echo "${RECORD_DATA}" | jq -r '.|length' ) 355 | if [ "${RESULT_LENGTH}" -eq "0" ]; then 356 | print_error "No matching records found" && exit 1 357 | else 358 | local STATUS=$( builtin echo "${RECORD_DATA}" | jq -r '.status' 2>/dev/null ) 359 | if [ "${STATUS}" = "Failed" ]; then 360 | local STATUS_DESC=$( builtin echo "${RECORD_DATA}" | jq -r '.statusDescription' ) 361 | print_error "Unable to get records for [${ZONE}]: ${STATUS_DESC}" && exit 1 362 | fi 363 | # output records in BIND format - if showid is true, then add the id as a BIND 364 | # style comment 365 | # 366 | # note: the CloudDNS records.json API endpoint has no way of filtering for apex 367 | # records, so we handle this by changing empty hosts to '@', then select-ing based 368 | # upon that 369 | if [ "${JSON}" -eq "1" ]; then 370 | if [ "${HOST_RECORD}" = "@" ]; then 371 | builtin echo "${RECORD_DATA}" | jq -r '.[] | select(.host == "")' 372 | else 373 | builtin echo "${RECORD_DATA}" | jq -r '.' 374 | fi 375 | exit 0 376 | fi 377 | if [ "${HOST_RECORD}" = "@" ]; then 378 | if [ "${SHOW_ID}" = "true" ]; then 379 | builtin echo "${RECORD_DATA}" | jq -r 'map(if .host == "" then . + {"host":"@"} else . end) | map(if .type == "NS" or .type == "MX" or .type == "CNAME" or .type == "SRV" then . + {"record": (.record + ".")} else . end) | map(if .type == "TXT" or .type == "SPF" then . + {"record": ("\"" + .record + "\"")} else . end) | .[] | select(.host == "@") | if .type == "MX" then (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + (.priority|tostring) + "\t" + .record + "\t; id=" + .id) elif .type == "SRV" then (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + (.priority|tostring) + "\t" + (.weight|tostring) + "\t" + (.port|tostring) + "\t" + .record + "\t; id=" + .id) else (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + .record + "\t; id=" + .id) end' 380 | else 381 | builtin echo "${RECORD_DATA}" | jq -r 'map(if .host == "" then . + {"host":"@"} else . end) | map(if .type == "NS" or .type == "MX" or .type == "CNAME" or .type == "SRV" then . + {"record": (.record + ".")} else . end) | map(if .type == "TXT" or .type == "SPF" then . + {"record": ("\"" + .record + "\"")} else . end) | .[] | select(.host == "@") | if .type == "MX" then (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + (.priority|tostring) + "\t" + .record) elif .type == "SRV" then (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + (.priority|tostring) + "\t" + (.weight|tostring) + "\t" + (.port|tostring) + "\t" + .record) else (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + .record) end' 382 | fi 383 | else 384 | if [ "${SHOW_ID}" = "true" ]; then 385 | builtin echo "${RECORD_DATA}" | jq -r 'map(if .host == "" then . + {"host":"@"} else . end) | map(if .type == "NS" or .type == "MX" or .type == "CNAME" or .type == "SRV" then . + {"record": (.record + ".")} else . end) | map(if .type == "TXT" or .type == "SPF" then . + {"record": ("\"" + .record + "\"")} else . end) | .[] | if .type == "MX" then (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + (.priority|tostring) + "\t" + .record + "\t; id=" + .id) elif .type == "SRV" then (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + (.priority|tostring) + "\t" + (.weight|tostring) + "\t" + (.port|tostring) + "\t" + .record + "\t; id=" + .id) else (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + .record + "\t; id=" + .id) end' 386 | else 387 | builtin echo "${RECORD_DATA}" | jq -r 'map(if .host == "" then . + {"host":"@"} else . end) | map(if .type == "NS" or .type == "MX" or .type == "CNAME" or .type == "SRV" then . + {"record": (.record + ".")} else . end) | map(if .type == "TXT" or .type == "SPF" then . + {"record": ("\"" + .record + "\"")} else . end) | .[] | if .type == "MX" then (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + (.priority|tostring) + "\t" + .record) elif .type == "SRV" then (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + (.priority|tostring) + "\t" + (.weight|tostring) + "\t" + (.port|tostring) + "\t" + .record) else (.host + "\t" + .ttl + "\tIN\t" + .type + "\t" + .record) end' 388 | fi 389 | fi 390 | fi 391 | } 392 | 393 | function get_soa() { 394 | do_tests 395 | if [ "$#" -ne "1" ]; then 396 | print_error "getsoa expects exactly one argument" && exit 1 397 | fi 398 | local ZONE="$1" 399 | check_zone_managed ${ZONE} 400 | print_debug "Retrieving SOA details for [${ZONE}]" 401 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 402 | local SOA_DATA=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/soa-details.json" ) 403 | local STATUS=$( builtin echo "${SOA_DATA}" | jq -r '.status' 2>/dev/null ) 404 | if [ "${STATUS}" = "Failed" ]; then 405 | local STATUS_DESC=$( builtin echo "${SOA_DATA}" | jq -r '.statusDescription' ) 406 | print_error "Unable to get SOA for [${ZONE}]: ${STATUS_DESC}" && exit 1 407 | fi 408 | builtin echo "${SOA_DATA}" | jq -r 'to_entries[] | .key + ":" + .value' 409 | } 410 | 411 | function set_soa() { 412 | do_tests 413 | if [ "$#" -lt "2" ]; then 414 | print_error "usage: ${THISPROG} setsoa key= [key= ... key=]" 415 | exit 1 416 | fi 417 | local ZONE="$1" 418 | if [[ "${ZONE}" =~ ^.*=.*$ ]]; then 419 | print_error "[${ZONE}] looks like a key=value pair, not a zone name" && exit 1 420 | fi 421 | check_zone_managed ${ZONE} 422 | print_debug "Modifying SOA record for zone [${ZONE}]" 423 | shift 424 | local -a VALID_KEYS=( "primary-ns" "admin-mail" "refresh" "retry" "expire" "default-ttl" ) 425 | local KV_PAIRS="$@" ERROR_COUNT=0 426 | local KV_PAIR 427 | for KV_PAIR in ${KV_PAIRS}; do 428 | builtin echo "${KV_PAIR}" | grep -Eqs '^[a-z-]+=[^=]+$' 429 | if [ "$?" -ne "0" ]; then 430 | print_error "key-value pair [${KV_PAIR}] not in correct format" && exit 1 431 | fi 432 | local KEY=$( builtin echo "${KV_PAIR}" | cut -d = -f 1 ) 433 | local VALUE=$( builtin echo "${KV_PAIR}" | cut -d = -f 2 ) 434 | print_debug "Checking key-value pair: ${KEY}=${VALUE}" 435 | if ! has_element VALID_KEYS "${KEY}"; then 436 | print_error "${KEY} is not a valid key" 437 | (( ERROR_COUNT = ERROR_COUNT + 1 )) 438 | fi 439 | unset KEY VALUE 440 | done 441 | unset KV_PAIR 442 | [[ "${ERROR_COUNT}" -gt "0" ]] && exit 1 443 | # modify-soa.json expects ALL parameters to be set. We will pre-populate via 444 | # a call to get_soa() 445 | local SOA_DATA=$( get_soa "${ZONE}" ) 446 | local PRIMARY_NS=$( builtin echo "${SOA_DATA}" | awk -F : '$1 == "primaryNS" { print $2 }' ) 447 | local ADMIN_MAIL=$( builtin echo "${SOA_DATA}" | awk -F : '$1 == "adminMail" { print $2 }' ) 448 | local REFRESH=$( builtin echo "${SOA_DATA}" | awk -F : '$1 == "refresh" { print $2 }' ) 449 | local RETRY=$( builtin echo "${SOA_DATA}" | awk -F : '$1 == "retry" { print $2 }' ) 450 | local EXPIRE=$( builtin echo "${SOA_DATA}" | awk -F : '$1 == "expire" { print $2 }' ) 451 | local DEFAULT_TTL=$( builtin echo "${SOA_DATA}" | awk -F : '$1 == "defaultTTL" { print $2 }' ) 452 | print_debug "Initial SOA paramters loaded via get_soa():" 453 | print_debug "--> PRIMARY_NS: ${PRIMARY_NS}" 454 | print_debug "--> ADMIN_MAIL: ${ADMIN_MAIL}" 455 | print_debug "--> REFRESH: ${REFRESH}" 456 | print_debug "--> RETRY: ${RETRY}" 457 | print_debug "--> EXPIRE: ${EXPIRE}" 458 | print_debug "--> DEFAULT_TTL: ${DEFAULT_TTL}" 459 | local CHANGED=0 460 | for KV_PAIR in ${KV_PAIRS}; do 461 | local KEY=$( builtin echo "${KV_PAIR}" | cut -d = -f 1 ) 462 | local VALUE=$( builtin echo "${KV_PAIR}" | cut -d = -f 2 ) 463 | # no default required in case as we've already checked against VALID_KEYS 464 | case ${KEY} in 465 | "primary-ns" ) validate_soa_value ns "${VALUE}" 466 | if [ "$?" -ne "0" ]; then 467 | print_error "Validation of primary-ns failed" && exit 1 468 | else 469 | if [ "${PRIMARY_NS}" = "${VALUE}" ]; then 470 | print_timestamp "primary-ns value same as existing" 471 | else 472 | PRIMARY_NS="${VALUE}" 473 | (( CHANGED = CHANGED + 1 )) 474 | fi 475 | fi 476 | ;; 477 | "admin-mail" ) validate_soa_value email "${VALUE}" 478 | if [ "$?" -ne "0" ]; then 479 | print_error "Validation of admin-mail failed" && exit 1 480 | else 481 | if [ "${ADMIN_MAIL}" = "${VALUE}" ]; then 482 | print_timestamp "admin-mail value same as existing" 483 | else 484 | ADMIN_MAIL="${VALUE}" 485 | (( CHANGED = CHANGED + 1 )) 486 | fi 487 | fi 488 | ;; 489 | "refresh" ) validate_soa_value refresh "${VALUE}" 490 | if [ "$?" -ne "0" ]; then 491 | print_error "Validation of refresh failed" && exit 1 492 | else 493 | if [ "${REFRESH}" = "${VALUE}" ]; then 494 | print_timestamp "refresh value same as existing" 495 | else 496 | REFRESH="${VALUE}" 497 | (( CHANGED = CHANGED + 1 )) 498 | fi 499 | fi 500 | ;; 501 | "retry" ) validate_soa_value retry "${VALUE}" 502 | if [ "$?" -ne "0" ]; then 503 | print_error "Validation of retry failed" && exit 1 504 | else 505 | if [ "${RETRY}" = "${VALUE}" ]; then 506 | print_timestamp "retry value same as existing" 507 | else 508 | RETRY="${VALUE}" 509 | (( CHANGED = CHANGED + 1 )) 510 | fi 511 | fi 512 | ;; 513 | "expire" ) validate_soa_value expire "${VALUE}" 514 | if [ "$?" -ne "0" ]; then 515 | print_error "Validation of expire failed" && exit 1 516 | else 517 | if [ "${EXPIRE}" = "${VALUE}" ]; then 518 | print_timestamp "expire value same as existing" 519 | else 520 | EXPIRE="${VALUE}" 521 | (( CHANGED = CHANGED + 1 )) 522 | fi 523 | fi 524 | ;; 525 | "default-ttl" ) validate_soa_value ttl "${VALUE}" 526 | if [ "$?" -ne "0" ]; then 527 | print_error "Validation of default-ttl failed" && exit 1 528 | else 529 | if [ "${DEFAULT_TTL}" = "${VALUE}" ]; then 530 | print_timestamp "default-ttl value same as existing" 531 | else 532 | DEFAULT_TTL="${VALUE}" 533 | (( CHANGED = CHANGED + 1 )) 534 | fi 535 | fi 536 | ;; 537 | esac 538 | done 539 | if [ "${CHANGED}" -eq "0" ]; then 540 | print_timestamp "Nothing has changed - no need to modify" && exit 0 541 | fi 542 | print_debug "SOA paramters loaded after modification:" 543 | print_debug "--> PRIMARY_NS: ${PRIMARY_NS}" 544 | print_debug "--> ADMIN_MAIL: ${ADMIN_MAIL}" 545 | print_debug "--> REFRESH: ${REFRESH}" 546 | print_debug "--> RETRY: ${RETRY}" 547 | print_debug "--> EXPIRE: ${EXPIRE}" 548 | print_debug "--> DEFAULT_TTL: ${DEFAULT_TTL}" 549 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 550 | POST_DATA="${POST_DATA} -d primary-ns=${PRIMARY_NS}" 551 | POST_DATA="${POST_DATA} -d admin-mail=${ADMIN_MAIL}" 552 | POST_DATA="${POST_DATA} -d refresh=${REFRESH}" 553 | POST_DATA="${POST_DATA} -d retry=${RETRY}" 554 | POST_DATA="${POST_DATA} -d expire=${EXPIRE}" 555 | POST_DATA="${POST_DATA} -d default-ttl=${DEFAULT_TTL}" 556 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/modify-soa.json" ) 557 | local STATUS=$( builtin echo "${RESPONSE}" | jq -r '.status' ) 558 | local STATUS_DESC=$( builtin echo "${RESPONSE}" | jq -r '.statusDescription' ) 559 | if [ "${STATUS}" = "Failed" ]; then 560 | print_error "Failed to modify SOA for zone [${ZONE}]: ${STATUS_DESC}" && exit 1 561 | elif [ "${STATUS}" = "Success" ]; then 562 | print_timestamp "SOA for zone [${ZONE}] modified" 563 | else 564 | print_error "Unexpected response while modifiying SOA for zone [${ZONE}]" && exit 1 565 | fi 566 | } 567 | 568 | function check_integer() { 569 | local VALUE="$1" 570 | local LOWER="$2" 571 | local UPPER="$3" 572 | builtin echo "${VALUE}" | grep -Eqs '^[[:digit:]]+$' || return 1 573 | if [ "${VALUE}" -ge "${LOWER}" -a "${VALUE}" -le "${UPPER}" ]; then 574 | return 0 575 | else 576 | return 1 577 | fi 578 | } 579 | 580 | function validate_soa_value() { 581 | # see https://www.cloudns.net/wiki/article/63/ for permissible integer values 582 | local TYPE="$1" 583 | local VALUE="$2" 584 | case ${TYPE} in 585 | "ns" ) # check for at least something.something 586 | builtin echo "${VALUE}" | grep -Eqs '^[a-z0-9-]+\.[a-z0-9-]+' 587 | return $? ;; 588 | "email" ) # check for at least something@something 589 | builtin echo "${VALUE}" | grep -Eqs '^[^@]+@[^@]+$' 590 | return $? ;; 591 | "refresh" ) check_integer ${VALUE} 1200 43200 592 | return $? ;; 593 | "retry" ) check_integer ${VALUE} 180 2419200 594 | return $? ;; 595 | "expire" ) check_integer ${VALUE} 1209600 2419200 596 | return $? ;; 597 | "ttl" ) check_integer ${VALUE} 60 2419200 598 | return $? ;; 599 | * ) return 0 ;; 600 | esac 601 | } 602 | 603 | function dump_zone() { 604 | do_tests 605 | if [ "$#" -ne "1" ]; then 606 | print_error "dumpzone expects exactly one argument" && exit 1 607 | fi 608 | local ZONE="$1" 609 | check_zone_managed ${ZONE} 610 | dump_zone_impl $ZONE 611 | } 612 | 613 | function dump_zone_impl() { 614 | local ZONE="$1" 615 | print_debug "Dumping BIND-format zone file for [${ZONE}]" 616 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 617 | local ZONE_DATA=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/records-export.json" ) 618 | local STATUS=$( builtin echo "${ZONE_DATA}" | jq -r '.status' ) 619 | if [ "${STATUS}" = "Success" ]; then 620 | builtin echo "${ZONE_DATA}" | jq -r '.zone' 621 | else 622 | print_error "Unable to get zone file for [${ZONE}]" && exit 1 623 | fi 624 | } 625 | 626 | function dump_all_zones() { 627 | do_tests 628 | if [ "$#" -ne "1" ]; then 629 | print_error "dumpallzones expects exactly one argument" && exit 1 630 | fi 631 | 632 | local OUTPUT_DIR="$1" 633 | if ! mkdir -p $OUTPUT_DIR >/dev/null 2>&1; then 634 | print_error "Unable to create output directory ${OUTPUT_DIR}" 635 | exit 1 636 | fi; 637 | 638 | for RAW_ZONE in `list_zones`; do 639 | # Only master zones can be exported 640 | local TYPE=$( builtin echo "${RAW_ZONE}" | cut -d : -f 2 ) 641 | if [ "${TYPE}" = "master" ]; then 642 | local ZONE=$( builtin echo "${RAW_ZONE}" | cut -d : -f 1 ) 643 | dump_zone_impl $ZONE > "$OUTPUT_DIR/$ZONE.conf" 644 | builtin echo "- ${ZONE}" 645 | fi; 646 | done; 647 | } 648 | 649 | function add_master() { 650 | do_tests 651 | if [ "$#" -ne "2" ]; then 652 | print_error "usage: ${THISPROG} addmaster " 653 | exit 1 654 | fi 655 | local ZONE="$1" 656 | local MASTERIP="$2" 657 | if ! check_ipv4_address ${MASTERIP}; then 658 | print_error "${MASTERIP} doesn't look like an IP" 659 | exit 1 660 | fi 661 | check_zone_managed ${ZONE} 662 | print_debug "Adding MASTER IP to slave zone [${ZONE}]" 663 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE} -d master-ip=${MASTERIP}" 664 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/add-master-server.json" ) 665 | local STATUS=$( builtin echo "${RESPONSE}" | jq -r '.status' ) 666 | local STATUS_DESC=$( builtin echo "${RESPONSE}" | jq -r '.statusDescription' ) 667 | if [ "${STATUS}" = "Failed" ]; then 668 | print_error "Failed to add master IP for zone [${ZONE}]: ${STATUS_DESC}" && exit 1 669 | elif [ "${STATUS}" = "Success" ]; then 670 | print_timestamp "Master IP was added successfully to zone [${ZONE}]" 671 | else 672 | print_error "Unexpected response while adding master IP for zone [${ZONE}]" && exit 1 673 | fi 674 | } 675 | 676 | function delete_master() { 677 | do_tests 678 | if [ "$#" -ne "2" ]; then 679 | print_error "usage: ${THISPROG} delmaster id=" 680 | exit 1 681 | fi 682 | local ZONE="$1" 683 | if [[ "${ZONE}" =~ ^.*=.*$ ]]; then 684 | print_error "[${ZONE}] looks like a key=value pair, not a zone name" && exit 1 685 | fi 686 | check_zone_managed ${ZONE} 687 | shift 688 | local ID_KV="$1" 689 | local ID_K=$( builtin echo "${ID_KV}" | cut -d = -f 1 ) 690 | local ID_V=$( builtin echo "${ID_KV}" | cut -d = -f 2 ) 691 | if [ "${ID_K}" != "id" ]; then 692 | print_error "id= key-value pair not specified" && exit 1 693 | fi 694 | if ! [[ "${ID_V}" =~ ^[0-9]+$ ]]; then 695 | print_error "id is not an integer" && exit 1 696 | fi 697 | local ID=${ID_V} 698 | unset ID_K ID_V ID_KV 699 | local MASTER_LIST=$( list_master ${ZONE} showid=true ) 700 | local TARGET_MASTER 701 | TARGET_MASTER=$( builtin echo "${MASTER_LIST}" | grep "^.*; id=${ID}$" ) 702 | if [ "$?" -ne "0" ]; then 703 | print_error "No master found with id [${ID}] in zone [${ZONE}]" 704 | exit 1 705 | fi 706 | unset MASTER_LIST 707 | TARGET_MASTER=$( builtin echo "${TARGET_MASTER}" | sed 's/; id=[0-9][0-9]*$//' | sed -r 's/[[:space:]]$//' ) 708 | print_debug "Deleting master [${TARGET_MASTER}]" 709 | (( ! FORCE )) && { 710 | local USER_RESPONSE 711 | builtin echo -n "Are you sure you want to delete master with id [${ID}]? [y|n]: " 712 | read USER_RESPONSE 713 | if [ "${USER_RESPONSE}" != "y" ]; then 714 | print_error "Aborting at user request" && exit 1 715 | fi 716 | } 717 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE} -d master-id=${ID}" 718 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/delete-master-server.json" ) 719 | local STATUS=$( builtin echo "${RESPONSE}" | jq -r '.status' ) 720 | local STATUS_DESC=$( builtin echo "${RESPONSE}" | jq -r '.statusDescription' ) 721 | if [ "${STATUS}" = "Failed" ]; then 722 | print_error "Failed to delete master: ${STATUS_DESC}" && exit 1 723 | elif [ "${STATUS}" = "Success" ]; then 724 | print_timestamp "Master successfully deleted" 725 | else 726 | print_error "Unexpected response while deleting master" && exit 1 727 | fi 728 | } 729 | 730 | function list_master() { 731 | do_tests 732 | if [ "$#" -ne "1" -a "$#" -ne "2" ]; then 733 | print_error "usage: ${THISPROG} listmaster [showid=]" 734 | exit 1 735 | fi 736 | local ZONE="$1" 737 | shift 738 | if [[ "${ZONE}" =~ ^.*=.*$ ]]; then 739 | print_error "[${ZONE}] looks like a key=value pair, not a zone name" && exit 1 740 | fi 741 | check_zone_managed ${ZONE} 742 | if [ "$#" -eq "1" ]; then 743 | local -a VALID_KEYS=( "showid" ) 744 | builtin echo "${1}" | grep -Eqs '^[a-z-]+=[^=]+$' 745 | if [ "$?" -ne "0" ]; then 746 | print_error "key-value pair [${1}] not in correct format" && exit 1 747 | fi 748 | local KEY=$( builtin echo "${1}" | cut -d = -f 1 ) 749 | local VALUE=$( builtin echo "${1}" | cut -d = -f 2 ) 750 | print_debug "Checking key-value pair: ${KEY}=${VALUE}" 751 | if ! has_element VALID_KEYS "${KEY}"; then 752 | print_error "${KEY} is not a valid key" 753 | fi 754 | case ${KEY} in 755 | "showid" ) case ${VALUE} in 756 | "true"|"false" ) SHOW_ID="${VALUE}" 757 | ;; 758 | * ) print_error "Invalid value for showid" 759 | exit 1 760 | ;; 761 | esac 762 | ;; 763 | esac 764 | fi 765 | print_debug "Processing listmaster on zone ${ZONE}" 766 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 767 | local OUTPUT=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/master-servers.json" | jq -r '.' ) 768 | if [ "${SHOW_ID}" = "true" ]; then 769 | builtin echo "${OUTPUT}" | jq -r 'to_entries|map("\(.value) ; id=\(.key)")|.[]' 770 | else 771 | builtin echo "${OUTPUT}" | jq -r '.[]' 772 | fi 773 | } 774 | 775 | function add_record() { 776 | do_tests 777 | if [ "$#" -lt "5" ]; then 778 | print_error "usage: ${THISPROG} addrecord type= host= record= ttl= [key= ... key=]" 779 | exit 1 780 | fi 781 | local ZONE="$1" 782 | if [[ "${ZONE}" =~ ^.*=.*$ ]]; then 783 | print_error "[${ZONE}] looks like a key=value pair, not a zone name" && exit 1 784 | fi 785 | check_zone_managed ${ZONE} 786 | shift 787 | local -a VALID_KEYS=( "type" "host" "record" "ttl" "priority" "weight" "port" ) 788 | local KV_PAIRS="$@" ERROR_COUNT=0 789 | local KV_PAIR 790 | for KV_PAIR in ${KV_PAIRS}; do 791 | builtin echo "${KV_PAIR}" | grep -Eqs '^[a-z-]+=.+$' 792 | if [ "$?" -ne "0" ]; then 793 | print_error "key-value pair [${KV_PAIR}] not in correct format" && exit 1 794 | fi 795 | local KEY=$( builtin echo "${KV_PAIR}" | cut -d = -f 1 ) 796 | local VALUE=$( builtin echo "${KV_PAIR}" | cut -d = -f 2 ) 797 | print_debug "Checking key-value pair: ${KEY}=${VALUE}" 798 | if ! has_element VALID_KEYS "${KEY}"; then 799 | print_error "${KEY} is not a valid key" 800 | (( ERROR_COUNT = ERROR_COUNT + 1 )) 801 | fi 802 | unset KEY VALUE 803 | done 804 | unset KV_PAIR 805 | local KV_PAIR RR_TYPE RR_HOST RR_RECORD RR_TTL RR_PRIORITY 806 | local RR_WEIGHT RR_PORT 807 | [[ "${ERROR_COUNT}" -gt "0" ]] && exit 1 808 | for KV_PAIR in ${KV_PAIRS}; do 809 | local KEY=$( builtin echo "${KV_PAIR}" | cut -d = -f 1 ) 810 | local VALUE=$( builtin echo "${KV_PAIR}" | cut -d = -f 2 ) 811 | case ${KEY} in 812 | "type" ) local TMPVAR="${VALUE^^}" 813 | if validate_rr_value type null "${TMPVAR}"; then 814 | RR_TYPE="${TMPVAR}" 815 | else 816 | print_error "Unsupported record type [${TMPVAR}]" 817 | exit 1 818 | fi 819 | ;; 820 | "host" ) if validate_rr_value host null "${VALUE}"; then 821 | RR_HOST="${VALUE}" 822 | else 823 | print_error "Incorrectly formatted host [${VALUE}]" 824 | exit 1 825 | fi 826 | ;; 827 | "record" ) # record validation happens at end of the for loop, as we 828 | # need RR_TYPE to be set, and params could be passed in any 829 | # order 830 | RR_RECORD="${VALUE}" 831 | ;; 832 | "ttl" ) if validate_rr_value ttl null "${VALUE}"; then 833 | RR_TTL="${VALUE}" 834 | else 835 | print_error "Invalid TTL [${VALUE}]" 836 | exit 1 837 | fi 838 | ;; 839 | "priority" ) if validate_rr_value priority null "${VALUE}"; then 840 | RR_PRIORITY="${VALUE}" 841 | else 842 | print_error "Invalid priority [${VALUE}]" 843 | exit 1 844 | fi 845 | ;; 846 | "weight" ) if validate_rr_value weight null "${VALUE}"; then 847 | RR_WEIGHT="${VALUE}" 848 | else 849 | print_error "Invalid weight [${VALUE}]" 850 | exit 1 851 | fi 852 | ;; 853 | "port" ) if validate_rr_value port null "${VALUE}"; then 854 | RR_PORT="${VALUE}" 855 | else 856 | print_error "Invalid port [${VALUE}]" 857 | exit 1 858 | fi 859 | ;; 860 | * ) print_error "${KEY} is an unknown key" && exit 1 861 | ;; 862 | esac 863 | done 864 | unset KV_PAIR 865 | if [ -z "${RR_TYPE}" ]; then print_error "type= not passed" && exit 1; fi 866 | if [ -z "${RR_HOST}" ]; then print_error "host= not passed" && exit 1; fi 867 | if [ -z "${RR_RECORD}" ]; then print_error "record= not passed" && exit 1; fi 868 | if [ -z "${RR_TTL}" ]; then print_error "ttl= not passed" && exit 1; fi 869 | if [ "${RR_TYPE}" = "TXT" -o "${RR_TYPE}" = "SPF" ]; then 870 | # in the case of TXT or SPF records, the record data will most likely 871 | # contain spaces, and all kinds of other characters. As this is a humble 872 | # shell script, parsing that as a key=value pair would be horrendous, if 873 | # not impossible. So, we load the record data from a file. 874 | print_debug "Attempting to load record data for ${RR_TYPE} record from [${RR_RECORD}]" 875 | if [ ! -f "${RR_RECORD}" ]; then 876 | print_error "Unable to load record data from [${RR_RECORD}]" && exit 1 877 | else 878 | if [ "$( wc -l ${RR_RECORD} | awk '{ print $1 }' )" -ne "1" ]; then 879 | print_error "Input file [${RR_RECORD}] has more than one line" && exit 1 880 | fi 881 | fi 882 | else 883 | if ! validate_rr_value record "${RR_TYPE}" "${RR_RECORD}"; then 884 | print_error "record validation failed" && exit 1 885 | fi 886 | fi 887 | if [ "${RR_TYPE}" = "MX" ]; then 888 | if [ -z "${RR_PRIORITY}" ]; then 889 | print_error "priority mandatory for MX records" && exit 1 890 | fi 891 | fi 892 | if [ "${RR_TYPE}" = "SRV" ]; then 893 | local ERROR_COUNT=0 894 | if [ -z "${RR_PRIORITY}" ]; then 895 | print_error "priority mandatory for SRV records" && exit 1 896 | (( ERROR_COUNT = ERROR_COUNT + 1 )) 897 | fi 898 | if [ -z "${RR_WEIGHT}" ]; then 899 | print_error "weight mandatory for SRV records" && exit 1 900 | (( ERROR_COUNT = ERROR_COUNT + 1 )) 901 | fi 902 | if [ -z "${RR_PORT}" ]; then 903 | print_error "port mandatory for SRV records" && exit 1 904 | (( ERROR_COUNT = ERROR_COUNT + 1 )) 905 | fi 906 | [[ "${ERROR_COUNT}" -gt "0" ]] && exit 1 907 | fi 908 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 909 | POST_DATA="${POST_DATA} -d record-type=${RR_TYPE}" 910 | POST_DATA="${POST_DATA} -d ttl=${RR_TTL}" 911 | POST_DATA="${POST_DATA} -d host=${RR_HOST}" 912 | if [ -n "${RR_PRIORITY}" ]; then 913 | if [ "${RR_TYPE}" = "MX" -o "${RR_TYPE}" = "SRV" ]; then 914 | POST_DATA="${POST_DATA} -d priority=${RR_PRIORITY}" 915 | else 916 | print_error "priority specified for type other than MX or SRV" 917 | exit 1 918 | fi 919 | fi 920 | if [ -n "${RR_WEIGHT}" ]; then 921 | if [ "${RR_TYPE}" = "SRV" ]; then 922 | POST_DATA="${POST_DATA} -d weight=${RR_WEIGHT}" 923 | else 924 | print_error "weight specified for type other than SRV" 925 | exit 1 926 | fi 927 | fi 928 | if [ -n "${RR_PORT}" ]; then 929 | if [ "${RR_TYPE}" = "SRV" ]; then 930 | POST_DATA="${POST_DATA} -d port=${RR_PORT}" 931 | else 932 | print_error "port specified for type other than SRV" 933 | exit 1 934 | fi 935 | fi 936 | if [ "${RR_TYPE}" = "TXT" -o "${RR_TYPE}" = "SPF" ]; then 937 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} --data-binary @<( builtin echo -ne "record=\"" | cat - ${RR_RECORD} <( builtin echo -ne "\"" ) | tr -d '\n' ) "${API_URL}/add-record.json" ) 938 | else 939 | POST_DATA="${POST_DATA} -d record=${RR_RECORD}" 940 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/add-record.json" ) 941 | fi 942 | local STATUS=$( builtin echo "${RESPONSE}" | jq -r '.status' ) 943 | local STATUS_DESC=$( builtin echo "${RESPONSE}" | jq -r '.statusDescription' ) 944 | if [ "${STATUS}" = "Failed" ]; then 945 | print_error "Failed to add record: ${STATUS_DESC}" && exit 1 946 | elif [ "${STATUS}" = "Success" ]; then 947 | local ID=$( builtin echo "${RESPONSE}" | jq -r '.data.id' ) 948 | print_timestamp "Record successfully added with id [${ID}]" 949 | else 950 | print_error "Unexpected response while adding record" && exit 1 951 | fi 952 | } 953 | 954 | function check_ipv4_address() { 955 | local IP="$1" 956 | local NUM_PARTS=$( builtin echo "${IP}" | awk -F . '{ print NF }' ) 957 | if [ "${NUM_PARTS}" -ne "4" ]; then 958 | return 1 959 | else 960 | for OCTET in $( builtin echo "${IP}" | tr '.' ' ' ); do 961 | check_integer "${OCTET}" 0 255 962 | if [ "$?" -ne "0" ]; then 963 | return 1 964 | fi 965 | done 966 | fi 967 | return 0 968 | } 969 | 970 | function check_ipv6_address() { 971 | local IP="$1" 972 | # adapted from https://gist.github.com/syzdek/6086792 973 | local RE_IPV6="([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|" # TEST: 1:2:3:4:5:6:7:8 974 | local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,7}:|" # TEST: 1:: 1:2:3:4:5:6:7:: 975 | local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|" # TEST: 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8 976 | local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|" # TEST: 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8 977 | local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|" # TEST: 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8 978 | local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|" # TEST: 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8 979 | local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|" # TEST: 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8 980 | local RE_IPV6="${RE_IPV6}[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|" # TEST: 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8 981 | local RE_IPV6="${RE_IPV6}:((:[0-9a-fA-F]{1,4}){1,7}|:)" # TEST: ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 :: 982 | builtin echo $IP | grep -Eqs "${RE_IPV6}" 983 | } 984 | 985 | function validate_rr_value() { 986 | local CHECK_TYPE="$1" 987 | local RECORD_TYPE="$2" 988 | shift 2 989 | local VALUE="$@" 990 | case ${CHECK_TYPE} in 991 | "type" ) # we need to check two things here: 992 | # 1) is our record type returned by get_record_types 993 | # 2) is our record type in SUPPORTED_RECORD_TYPES 994 | local -a RECORD_TYPES=( $( get_record_types ) ) 995 | if has_element RECORD_TYPES "${VALUE}"; then 996 | if has_element SUPPORTED_RECORD_TYPES "${VALUE}"; then 997 | return 0 998 | else 999 | return 1 1000 | fi 1001 | else 1002 | return 1 1003 | fi 1004 | ;; 1005 | "host" ) builtin echo "${VALUE}" | grep -Eqs '^[a-zA-Z0-9\._@-]+$' 1006 | return $? 1007 | ;; 1008 | "ttl" ) local -a AVAILABLE_TTLS=( $( get_available_ttls ) ) 1009 | if has_element AVAILABLE_TTLS "${VALUE}"; then 1010 | return 0 1011 | else 1012 | return 1 1013 | fi 1014 | ;; 1015 | "priority" ) check_integer "${VALUE}" 0 65535 1016 | return $? 1017 | ;; 1018 | "weight" ) check_integer "${VALUE}" 0 65535 1019 | return $? 1020 | ;; 1021 | "port" ) check_integer "${VALUE}" 0 65535 1022 | return $? 1023 | ;; 1024 | "record" ) case "${RECORD_TYPE}" in 1025 | "A" ) check_ipv4_address "${VALUE}" 1026 | return $? 1027 | ;; 1028 | "AAAA" ) check_ipv6_address "${VALUE}" 1029 | return $? 1030 | ;; 1031 | "CNAME" ) builtin echo "${VALUE}" | grep -Eqs '^[a-zA-Z0-9\.-]+$' 1032 | return $? 1033 | ;; 1034 | "MX" ) builtin echo "${VALUE}" | grep -Eqs '^[a-zA-Z0-9\.-]+$' 1035 | return $? 1036 | ;; 1037 | "NS" ) builtin echo "${VALUE}" | grep -Eqs '^[a-zA-Z0-9\.-]+$' 1038 | return $? 1039 | ;; 1040 | "SPF" ) return 0 1041 | ;; 1042 | "SRV" ) builtin echo "${VALUE}" | grep -Eqs '^[a-zA-Z0-9\.-]+$' 1043 | return $? 1044 | ;; 1045 | "TXT" ) return 0 1046 | ;; 1047 | * ) return 0 1048 | ;; 1049 | esac 1050 | ;; 1051 | * ) return 0 1052 | ;; 1053 | esac 1054 | } 1055 | 1056 | function delete_record() { 1057 | do_tests 1058 | if [ "$#" -ne "2" ]; then 1059 | print_error "usage: ${THISPROG} delrecord id=" 1060 | exit 1 1061 | fi 1062 | local ZONE="$1" 1063 | if [[ "${ZONE}" =~ ^.*=.*$ ]]; then 1064 | print_error "[${ZONE}] looks like a key=value pair, not a zone name" && exit 1 1065 | fi 1066 | check_zone_managed ${ZONE} 1067 | shift 1068 | local ID_KV="$1" 1069 | local ID_K=$( builtin echo "${ID_KV}" | cut -d = -f 1 ) 1070 | local ID_V=$( builtin echo "${ID_KV}" | cut -d = -f 2 ) 1071 | if [ "${ID_K}" != "id" ]; then 1072 | print_error "id= key-value pair not specified" && exit 1 1073 | fi 1074 | if ! [[ "${ID_V}" =~ ^[0-9]+$ ]]; then 1075 | print_error "id is not an integer" && exit 1 1076 | fi 1077 | local ID=${ID_V} 1078 | unset ID_K ID_V ID_KV 1079 | local RECORD_LIST=$( list_records ${ZONE} showid=true ) 1080 | local TARGET_RECORD 1081 | TARGET_RECORD=$( builtin echo "${RECORD_LIST}" | grep "^.*; id=${ID}$" ) 1082 | if [ "$?" -ne "0" ]; then 1083 | print_error "No record found with id [${ID}] in zone [${ZONE}]" 1084 | exit 1 1085 | fi 1086 | unset RECORD_LIST 1087 | TARGET_RECORD=$( builtin echo "${TARGET_RECORD}" | sed 's/; id=[0-9][0-9]*$//' | sed -r 's/[[:space:]]$//' ) 1088 | print_debug "Deleting record [${TARGET_RECORD}]" 1089 | (( ! FORCE )) && { 1090 | local USER_RESPONSE 1091 | builtin echo -n "Are you sure you want to delete record with id [${ID}]? [y|n]: " 1092 | read USER_RESPONSE 1093 | if [ "${USER_RESPONSE}" != "y" ]; then 1094 | print_error "Aborting at user request" && exit 1 1095 | fi 1096 | } 1097 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE} -d record-id=${ID}" 1098 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/delete-record.json" ) 1099 | local STATUS=$( builtin echo "${RESPONSE}" | jq -r '.status' ) 1100 | local STATUS_DESC=$( builtin echo "${RESPONSE}" | jq -r '.statusDescription' ) 1101 | if [ "${STATUS}" = "Failed" ]; then 1102 | print_error "Failed to delete record: ${STATUS_DESC}" && exit 1 1103 | elif [ "${STATUS}" = "Success" ]; then 1104 | print_timestamp "Record successfully deleted" 1105 | else 1106 | print_error "Unexpected response while deleting record" && exit 1 1107 | fi 1108 | } 1109 | 1110 | function modify_record() { 1111 | do_tests 1112 | if [ "$#" -lt "3" ]; then 1113 | print_error "usage: ${THISPROG} modify id= key= [key= ... key=]" 1114 | exit 1 1115 | fi 1116 | local ZONE="$1" 1117 | if [[ "${ZONE}" =~ ^.*=.*$ ]]; then 1118 | print_error "[${ZONE}] looks like a key=value pair, not a zone name" && exit 1 1119 | fi 1120 | check_zone_managed ${ZONE} 1121 | shift 1122 | local -a VALID_KEYS=( "id" "host" "record" "ttl" "priority" "weight" "port" ) 1123 | local KV_PAIRS="$@" ERROR_COUNT=0 1124 | local KV_PAIR 1125 | for KV_PAIR in ${KV_PAIRS}; do 1126 | builtin echo "${KV_PAIR}" | grep -Eqs '^[a-z-]+=.+$' 1127 | if [ "$?" -ne "0" ]; then 1128 | print_error "key-value pair [${KV_PAIR}] not in correct format" && exit 1 1129 | fi 1130 | local KEY=$( builtin echo "${KV_PAIR}" | cut -d = -f 1 ) 1131 | local VALUE=$( builtin echo "${KV_PAIR}" | cut -d = -f 2 ) 1132 | print_debug "Checking key-value pair: ${KEY}=${VALUE}" 1133 | if ! has_element VALID_KEYS "${KEY}"; then 1134 | print_error "${KEY} is not a valid key" 1135 | (( ERROR_COUNT = ERROR_COUNT + 1 )) 1136 | fi 1137 | unset KEY VALUE 1138 | done 1139 | unset KV_PAIR 1140 | [[ "${ERROR_COUNT}" -gt "0" ]] && exit 1 1141 | local RR_ID RR_HOST RR_RECORD RR_TTL RR_PRIORITY RR_WEIGHT RR_PORT 1142 | for KV_PAIR in ${KV_PAIRS}; do 1143 | local KEY=$( builtin echo "${KV_PAIR}" | cut -d = -f 1 ) 1144 | local VALUE=$( builtin echo "${KV_PAIR}" | cut -d = -f 2 ) 1145 | case ${KEY} in 1146 | "id" ) if check_integer "${VALUE}" 0 100000000000; then 1147 | RR_ID="${VALUE}" 1148 | else 1149 | print_error "id must be an integer value" && exit 1 1150 | fi 1151 | ;; 1152 | "host" ) if validate_rr_value host null "${VALUE}"; then 1153 | RR_HOST="${VALUE}" 1154 | else 1155 | print_error "Incorrectly formatted host [${VALUE}]" 1156 | exit 1 1157 | fi 1158 | ;; 1159 | "record" ) # record validation happens at end of the for loop, as we 1160 | # need RR_TYPE to be set, and params could be passed in any 1161 | # order 1162 | RR_RECORD="${VALUE}" 1163 | ;; 1164 | "ttl" ) if validate_rr_value ttl null "${VALUE}"; then 1165 | RR_TTL="${VALUE}" 1166 | else 1167 | print_error "Invalid TTL [${VALUE}]" 1168 | exit 1 1169 | fi 1170 | ;; 1171 | "priority" ) if validate_rr_value priority null "${VALUE}"; then 1172 | RR_PRIORITY="${VALUE}" 1173 | else 1174 | print_error "Invalid priority [${VALUE}]" 1175 | exit 1 1176 | fi 1177 | ;; 1178 | "weight" ) if validate_rr_value weight null "${VALUE}"; then 1179 | RR_WEIGHT="${VALUE}" 1180 | else 1181 | print_error "Invalid weight [${VALUE}]" 1182 | exit 1 1183 | fi 1184 | ;; 1185 | "port" ) if validate_rr_value port null "${VALUE}"; then 1186 | RR_PORT="${VALUE}" 1187 | else 1188 | print_error "Invalid port [${VALUE}]" 1189 | exit 1 1190 | fi 1191 | ;; 1192 | * ) print_error "${KEY} is an unknown key" && exit 1 1193 | ;; 1194 | esac 1195 | done 1196 | unset KV_PAIR 1197 | # at this point, we have a bunch of variables which may or may not have values 1198 | # depending upon the record type being modified, and the modifications being made 1199 | # we *do*, however, need RR_ID set, at least 1200 | if [ -z "${RR_ID}" ]; then print_error "id= not passed" && exit 1; fi 1201 | # first, we need to try to get a record with this id, to pre-populate some 1202 | # variables which we may overwrite with user supplied key=value pairs, if they 1203 | # are set. The ClouDNS API has no way of retrieving a record by id. So, we use 1204 | # list_records ${ZONE} showid=true and sift through the output. 1205 | local RECORD_LIST=$( list_records ${ZONE} showid=true ) 1206 | local TARGET_RECORD 1207 | TARGET_RECORD=$( builtin echo "${RECORD_LIST}" | grep "^.*; id=${RR_ID}$" ) 1208 | if [ "$?" -ne "0" ]; then 1209 | print_error "No record found with id [${RR_ID}] in zone [${ZONE}]" 1210 | exit 1 1211 | fi 1212 | unset RECORD_LIST 1213 | local GOT_TYPE=$( builtin echo "${TARGET_RECORD}" | awk '{ print $4 }' ) 1214 | # preload approriate variables depending on GOT_TYPE. All RRs will have 1215 | # host, ttl, record. MX will have priority. SRV will have priority, weight 1216 | # and port. SPF and TXT could have all kinds of nonsense in the record, but 1217 | # will be surrounded by double quotes. If we are modifying the record value 1218 | # for an SPF or TXT record, we will read the new value in from a file, as we 1219 | # do in add_record() 1220 | local GOT_HOST GOT_TTL GOT_RECORD GOT_PRIORITY GOT_WEIGHT GOT_PORT 1221 | if ! has_element SUPPORTED_RECORD_TYPES "${GOT_TYPE}"; then 1222 | print_error "Trying to modify a record of type [${GOT_TYPE}] is not supported" 1223 | exit 1 1224 | fi 1225 | TARGET_RECORD=$( builtin echo "${TARGET_RECORD}" | sed 's/; id=[0-9][0-9]*$//' | sed -r 's/[[:space:]]$//' ) 1226 | GOT_HOST=$( builtin echo "${TARGET_RECORD}" | awk '{ print $1 }' ) 1227 | GOT_TTL=$( builtin echo "${TARGET_RECORD}" | awk '{ print $2 }' ) 1228 | case ${GOT_TYPE} in 1229 | "MX" ) GOT_PRIORITY=$( builtin echo "${TARGET_RECORD}" | awk '{ print $5 }' ) 1230 | GOT_RECORD=$( builtin echo "${TARGET_RECORD}" | awk '{ print $NF }' ) 1231 | print_debug "got RR data: HOST=[${GOT_HOST}] TTL=[${GOT_TTL}] TYPE=[${GOT_TYPE}] PRIORITY=[${GOT_PRIORITY}] RECORD=[${GOT_RECORD}]" 1232 | ;; 1233 | "SPF"|"TXT" ) GOT_RECORD=$( builtin echo "${TARGET_RECORD}" | sed 's/^.*"\([^"]*\)"$/\1/' ) 1234 | print_debug "got RR data: HOST=[${GOT_HOST}] TTL=[${GOT_TTL}] TYPE=[${GOT_TYPE}] RECORD=[${GOT_RECORD}]" 1235 | ;; 1236 | "SRV" ) GOT_PRIORITY=$( builtin echo "${TARGET_RECORD}" | awk '{ print $5 }' ) 1237 | GOT_WEIGHT=$( builtin echo "${TARGET_RECORD}" | awk '{ print $6 }' ) 1238 | GOT_PORT=$( builtin echo "${TARGET_RECORD}" | awk '{ print $7 }' ) 1239 | GOT_RECORD=$( builtin echo "${TARGET_RECORD}" | awk '{ print $NF }' ) 1240 | print_debug "got RR data: HOST=[${GOT_HOST}] TTL=[${GOT_TTL}] TYPE=[${GOT_TYPE}] PRIORITY=[${GOT_PRIORITY}] WEIGHT=[${GOT_WEIGHT}] PORT=[${GOT_PORT}] RECORD=[${GOT_RECORD}]" 1241 | ;; 1242 | * ) GOT_RECORD=$( builtin echo "${TARGET_RECORD}" | awk '{ print $NF }' ) 1243 | print_debug "got RR data: HOST=[${GOT_HOST}] TTL=[${GOT_TTL}] TYPE=[${GOT_TYPE}] RECORD=[${GOT_RECORD}]" 1244 | ;; 1245 | esac 1246 | # so, we now have our required variables set, pre-populated from the existing record. 1247 | # now, if we are modifying an SPF or TXT record's record field, load in the value from 1248 | # the specified file 1249 | case ${GOT_TYPE} in 1250 | "SPF"|"TXT" ) if [ ! -f "${RR_RECORD}" ]; then 1251 | print_error "Unable to load record data from [${RR_RECORD}]" && exit 1 1252 | else 1253 | if [ "$( wc -l ${RR_RECORD} | awk '{ print $1 }' )" -ne "1" ]; then 1254 | print_error "Input file [${RR_RECORD}] has more than one line" && exit 1 1255 | else 1256 | RR_RECORD="$( cat ${RR_RECORD} )" 1257 | fi 1258 | fi 1259 | ;; 1260 | esac 1261 | local CHANGED=0 1262 | [[ -n "${RR_HOST}" ]] && { 1263 | [[ "${RR_HOST}" != "${GOT_HOST}" ]] && { 1264 | GOT_HOST="${RR_HOST}" 1265 | (( CHANGED = CHANGED + 1 )) 1266 | } || { 1267 | print_timestamp "host value same as existing" 1268 | } 1269 | } 1270 | [[ -n "${RR_TTL}" ]] && { 1271 | [[ "${RR_TTL}" != "${GOT_TTL}" ]] && { 1272 | GOT_TTL="${RR_TTL}" 1273 | (( CHANGED = CHANGED + 1 )) 1274 | } || { 1275 | print_timestamp "ttl value same as existing" 1276 | } 1277 | } 1278 | [[ -n "${RR_RECORD}" ]] && { 1279 | [[ "${RR_RECORD}" != "${GOT_RECORD}" ]] && { 1280 | GOT_RECORD="${RR_RECORD}" 1281 | (( CHANGED = CHANGED + 1 )) 1282 | } || { 1283 | print_timestamp "record value same as existing" 1284 | } 1285 | } 1286 | [[ -n "${RR_PRIORITY}" ]] && { 1287 | [[ "${RR_PRIORITY}" != "${GOT_PRIORITY}" ]] && { 1288 | GOT_PRIORITY="${RR_PRIORITY}" 1289 | (( CHANGED = CHANGED + 1 )) 1290 | } || { 1291 | print_timestamp "priority value same as existing" 1292 | } 1293 | } 1294 | [[ -n "${RR_WEIGHT}" ]] && { 1295 | [[ "${RR_WEIGHT}" != "${GOT_WEIGHT}" ]] && { 1296 | GOT_WEIGHT="${RR_WEIGHT}" 1297 | (( CHANGED = CHANGED + 1 )) 1298 | } || { 1299 | print_timestamp "weight value same as existing" 1300 | } 1301 | } 1302 | [[ -n "${RR_PORT}" ]] && { 1303 | [[ "${RR_PORT}" != "${GOT_PORT}" ]] && { 1304 | GOT_PORT="${RR_PORT}" 1305 | (( CHANGED = CHANGED + 1 )) 1306 | } || { 1307 | print_timestamp "port value same as existing" 1308 | } 1309 | } 1310 | [[ "${CHANGED}" -eq "0" ]] && { 1311 | print_timestamp "Nothing has changed - no need to modify" && exit 0 1312 | } 1313 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 1314 | POST_DATA="${POST_DATA} -d record-id=${RR_ID}" 1315 | POST_DATA="${POST_DATA} -d host=${GOT_HOST}" 1316 | POST_DATA="${POST_DATA} -d ttl=${GOT_TTL}" 1317 | if [ -n "${RR_PRIORITY}" ]; then 1318 | if [ "${GOT_TYPE}" != "MX" -a "${GOT_TYPE}" != "SRV" ]; then 1319 | print_error "priority specified for type other than MX or SRV" 1320 | exit 1 1321 | fi 1322 | fi 1323 | if [ -n "${RR_WEIGHT}" ]; then 1324 | if [ "${GOT_TYPE}" != "SRV" ]; then 1325 | print_error "weight specified for type other than SRV" 1326 | exit 1 1327 | fi 1328 | fi 1329 | if [ -n "${RR_PORT}" ]; then 1330 | if [ "${GOT_TYPE}" != "SRV" ]; then 1331 | print_error "port specified for type other than SRV" 1332 | exit 1 1333 | fi 1334 | fi 1335 | if [ "${GOT_TYPE}" = "MX" -o "${GOT_TYPE}" = "SRV" ]; then 1336 | POST_DATA="${POST_DATA} -d priority=${GOT_PRIORITY}" 1337 | fi 1338 | if [ "${GOT_TYPE}" = "SRV" ]; then 1339 | POST_DATA="${POST_DATA} -d weight=${GOT_WEIGHT} -d port=${GOT_PORT}" 1340 | fi 1341 | if [ "${GOT_TYPE}" = "TXT" -o "${GOT_TYPE}" = "SPF" ]; then 1342 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} --data-binary @<( builtin echo -ne "record=\"${GOT_RECORD}\"" ) "${API_URL}/mod-record.json" ) 1343 | else 1344 | POST_DATA="${POST_DATA} -d record=${GOT_RECORD}" 1345 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/mod-record.json" ) 1346 | fi 1347 | local STATUS=$( builtin echo "${RESPONSE}" | jq -r '.status' ) 1348 | local STATUS_DESC=$( builtin echo "${RESPONSE}" | jq -r '.statusDescription' ) 1349 | if [ "${STATUS}" = "Failed" ]; then 1350 | print_error "Failed to modify record: ${STATUS_DESC}" && exit 1 1351 | elif [ "${STATUS}" = "Success" ]; then 1352 | print_timestamp "Record successfully modified" 1353 | else 1354 | print_error "Unexpected response while modifying record" && exit 1 1355 | fi 1356 | } 1357 | 1358 | function add_zone() { 1359 | do_tests 1360 | if [ "$#" -ne "1" ]; then 1361 | print_error "addzone expects exactly one argument" && exit 1 1362 | fi 1363 | local ZONE="$1" 1364 | local ZONE_TYPE="master" # we only support master zones 1365 | print_debug "Adding new ${ZONE_TYPE} zone for [${ZONE}]" 1366 | local POST_DATA="${AUTH_POST_DATA} -d zone-type=${ZONE_TYPE} -d domain-name=${ZONE}" 1367 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/register.json" ) 1368 | local STATUS=$( builtin echo "${RESPONSE}" | jq -r '.status' ) 1369 | local STATUS_DESC=$( builtin echo "${RESPONSE}" | jq -r '.statusDescription' ) 1370 | if [ "${STATUS}" = "Failed" ]; then 1371 | print_error "Failed to add zone [${ZONE}]: ${STATUS_DESC}" && exit 1 1372 | elif [ "${STATUS}" = "Success" ]; then 1373 | print_timestamp "New zone [${ZONE}] added" 1374 | else 1375 | print_error "Unexpected response while adding zone [${ZONE}]" && exit 1 1376 | fi 1377 | } 1378 | 1379 | function delete_zone() { 1380 | do_tests 1381 | if [ "$#" -ne "1" ]; then 1382 | print_error "delzone expects exactly one argument" && exit 1 1383 | fi 1384 | local ZONE="$1" 1385 | check_zone_managed ${ZONE} 1386 | print_debug "Deleting zone [${ZONE}]" 1387 | builtin echo "Are you sure you want to delete zone [${ZONE}]?" 1388 | builtin echo -n "You must type I-AM-SURE, exactly: " 1389 | local RESPONSE="" 1390 | builtin read RESPONSE 1391 | if [ "${RESPONSE}" != "I-AM-SURE" ]; then 1392 | print_error "Aborting removal of zone [${ZONE}]" && exit 1 1393 | fi 1394 | builtin echo "Okay. Waiting ${REMOVAL_WAIT}s prior to removal. CTRL-C now if unsure!" 1395 | sleep ${REMOVAL_WAIT} 1396 | local POST_DATA="${AUTH_POST_DATA} -d domain-name=${ZONE}" 1397 | local RESPONSE=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/delete.json" ) 1398 | local STATUS=$( builtin echo "${RESPONSE}" | jq -r '.status' ) 1399 | local STATUS_DESC=$( builtin echo "${RESPONSE}" | jq -r '.statusDescription' ) 1400 | if [ "${STATUS}" = "Failed" ]; then 1401 | print_error "Unable to delete zone [${ZONE}]: ${STATUS_DESC}" && exit 1 1402 | elif [ "${STATUS}" = "Success" ]; then 1403 | print_timestamp "Zone [${ZONE}] deleted" 1404 | else 1405 | print_error "Unexpected response while deleting zone [${ZONE}]" && exit 1 1406 | fi 1407 | } 1408 | 1409 | function list_zones() { 1410 | do_tests 1411 | local PAGE_COUNT=$( get_page_count ) 1412 | local COUNTER=0 1413 | local POST_DATA="${AUTH_POST_DATA} -d page=0 -d rows-per-page=${ROWS_PER_PAGE}" 1414 | while [ "${COUNTER}" -lt "${PAGE_COUNT}" ]; do 1415 | print_debug "Processing listzones page=$(( ${COUNTER} + 1 )) with rows-per-page=${ROWS_PER_PAGE}" 1416 | POST_DATA=$( builtin echo "${POST_DATA}" |\ 1417 | sed "s/^\(.*-d page=\)[0-9][0-9]*\( .*\)$/\1$(( ${COUNTER} + 1 ))\2/" ) 1418 | local OUTPUT=$( curl -4qs -X POST ${POST_DATA} "${API_URL}/list-zones.json" | jq -r . ) 1419 | builtin echo "${OUTPUT}" | jq -r '.[] | .name + ":" + .type' 1420 | (( COUNTER = COUNTER + 1 )) 1421 | done 1422 | } 1423 | 1424 | function call_helper() { 1425 | do_tests 1426 | if [ "$#" -ne "1" ]; then 1427 | print_error "helper expects exactly one argument" && exit 1 1428 | fi 1429 | local HELPER_FUNCTION="$1" 1430 | print_debug "Calling helper function ${HELPER_FUNCTION}()" 1431 | case ${HELPER_FUNCTION} in 1432 | "get_available_ttls" ) get_available_ttls ;; 1433 | "get_record_types" ) get_record_types ;; 1434 | "get_page_count" ) get_page_count ;; 1435 | * ) print_error "No such helper function ${HELPER_FUNCTION}" 1436 | exit 1 ;; 1437 | esac 1438 | } 1439 | 1440 | function test_login() { 1441 | # don't check SKIP_TESTS here as this is the "test" command 1442 | test_api_url 1443 | do_login 1444 | if [ "$?" -eq "0" ]; then 1445 | print_timestamp "Login test successful" 1446 | else 1447 | print_error "Login test failed" 1448 | exit 1 1449 | fi 1450 | } 1451 | 1452 | function check_zone_managed() { 1453 | local ZONE=$1 1454 | local LISTED_ZONES 1455 | LISTED_ZONES=$( list_zones | grep -qs "^${ZONE}:" ) 1456 | if [ "$?" -ne "0" ]; then 1457 | print_error "Zone [${ZONE}] not under management" && exit 1 1458 | fi 1459 | } 1460 | 1461 | while getopts ":dfhjs" OPTION; do 1462 | case ${OPTION} in 1463 | "d") DEBUG=1 ;; 1464 | "f") FORCE=1 ;; 1465 | "h") print_usage && exit 0 ;; 1466 | "j") JSON=1 ;; 1467 | "s") SKIP_TESTS=1 ;; 1468 | * ) print_usage && exit 1 ;; 1469 | esac 1470 | done 1471 | 1472 | shift $(( ${OPTIND} - 1 )) 1473 | 1474 | if [ "$#" -eq "0" ]; then 1475 | print_usage && exit 1 1476 | fi 1477 | 1478 | check_jq 1479 | check_environment_variables 1480 | set_auth_post_data 1481 | process_arguments "$@" 1482 | 1483 | exit 0 1484 | --------------------------------------------------------------------------------