| 1 | # Base class for a Bind8 zone record | ||
| 2 | # | ||
| 3 | # Copyright Karthik Krishnamurthy <karthik.k@extremix.net> | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Unix::Conf::Bind8::DB::Record - Base class which contains | ||
| 8 | methods which are inherited by the derived classes residing | ||
| 9 | under Unix::Conf::Bind8::DB. | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Unix::Conf::Bind8; | ||
| 14 | |||
| 15 | my ($conf, $db, $rec, $ret); | ||
| 16 | |||
| 17 | # get a db object from a valid Unix::Conf::Bind8::Conf object | ||
| 18 | $db = $conf->get_db ('extremix.net') | ||
| 19 | or $die ("couldn't get db object for `extremix.net'"); | ||
| 20 | |||
| 21 | # create a new A record. All new_* methods are similar except | ||
| 22 | # for new_mx (), where an extra argument MXPREF has to be | ||
| 23 | # provided. | ||
| 24 | $rec = $db->new_a ( | ||
| 25 | LABEL => 'www', | ||
| 26 | TTL => '1d', | ||
| 27 | RDATA => '192.168.1.1', | ||
| 28 | ) or $rec->die ("couldn't create A record for www.extremix.net"); | ||
| 29 | |||
| 30 | $rec = $db->get_a ('www', '192.168.1.1') | ||
| 31 | or $rec->die ("couldn't get record object for www.extremix.net A 192.168.1.1"); | ||
| 32 | |||
| 33 | # get attribute values | ||
| 34 | printf ("Class for record `www.extremix.net' A 192.168.1.1 is $ret\n") | ||
| 35 | if ($ret = $rec->class ()); | ||
| 36 | |||
| 37 | # set attribute values | ||
| 38 | $ret = $rec->ttl ('1w') or $ret->die ("couldn't set ttl"); | ||
| 39 | $ret = $rec->rdata ('192.168.2.1') or $ret->die ("couldn't set rdata"); | ||
| 40 | |||
| 41 | # delete record | ||
| 42 | $ret = $rec->delete () or $ret->die ("couldn't delete"); | ||
| 43 | |||
| 44 | =over 4 | ||
| 45 | |||
| 46 | =head1 METHODS | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | package Unix::Conf::Bind8::DB::Record; | ||
| 51 | |||
| 52 | use strict; | ||
| 53 | use warnings; | ||
| 54 | use Unix::Conf; | ||
| 55 | use Unix::Conf::Bind8::DB; | ||
| 56 | use Unix::Conf::Bind8::DB::Lib; | ||
| 57 | |||
| 58 | =item new () | ||
| 59 | |||
| 60 | Arguments | ||
| 61 | LABEL => 'string', | ||
| 62 | CLASS => 'string', # 'IN'|'HS'|'CHAOS' | ||
| 63 | TTL => 'string'|number, | ||
| 64 | RTYPE => 'string', # 'A'|'NS'|'MX'|'SOA'|'CNAME'|'PTR' | ||
| 65 | RDATA => data | ||
| 66 | PARENT => reference, # to the DB object datastructure | ||
| 67 | |||
| 68 | Class constructor. | ||
| 69 | Creates a new Unix::Conf::Bind8::DB::* object and returns it | ||
| 70 | if successful, an Err object otherwise. Do not use this constructor | ||
| 71 | directly. Use the Unix::Conf::Bind8::DB::new_* equivalent instead. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | # Arguments | ||
| 76 | # LABEL | ||
| 77 | # RTYPE | ||
| 78 | # RDATA | ||
| 79 | # PARENT | ||
| 80 | # CLASS | ||
| 81 | # TTL | ||
| 82 | sub new | ||
| 83 | { | ||
| 84 | my $class = shift (); | ||
| 85 | my %args = ( @_ ); | ||
| 86 | my $new = bless ({}, $class); | ||
| 87 | my $ret; | ||
| 88 | |||
| 89 | return (Unix::Conf->_err ('new', "LABEL not specified")) | ||
| 90 | unless (defined ($args{LABEL})); | ||
| 91 | return (Unix::Conf->_err ('new', "RTYPE not specified")) | ||
| 92 | unless (defined ($args{RTYPE})); | ||
| 93 | return (Unix::Conf->_err ('new', "RDATA not specified")) | ||
| 94 | unless (defined ($args{RDATA})); | ||
| 95 | return (Unix::Conf->_err ('new', "PARENT not specified")) | ||
| 96 | unless (defined ($args{PARENT})); | ||
| 97 | |||
| 98 | $new->_parent ($args{PARENT}); | ||
| 99 | $ret = $new->class ($args{CLASS}) or return ($ret) | ||
| 100 | if (defined ($args{CLASS})); | ||
| 101 | $ret = $new->ttl ($args{TTL}) or return ($ret) | ||
| 102 | if (defined ($args{TTL})); | ||
| 103 | |||
| 104 | $ret = $new->rtype ($args{RTYPE}) or return ($ret); | ||
| 105 | $ret = $new->rdata ($args{RDATA}) or return ($ret); | ||
| 106 | $ret = $new->label ($args{LABEL}) or return ($ret); | ||
| 107 | |||
| 108 | return ($new); | ||
| 109 | } | ||
| 110 | |||
| 111 | =item label () | ||
| 112 | |||
| 113 | Arguments | ||
| 114 | 'label' | ||
| 115 | |||
| 116 | Object method. | ||
| 117 | Get/set the record's label. If an argument is passed, the invocant's | ||
| 118 | label is set and true returned, on success, an Err object otherwise. | ||
| 119 | If no argument is passed the invocant's label is returned. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub label | ||
| 124 | { | ||
| 125 | my ($self, $label) = @_; | ||
| 126 | my $ret; | ||
| 127 | |||
| 128 | if (defined ($label)) { | ||
| 129 | # the object is stored in a complicated datastructure keyed | ||
| 130 | # on the label among other things. so we need to delete the | ||
| 131 | # old key and store the object at the new key | ||
| 132 | if (defined ($self->{LABEL})) { | ||
| 133 | $ret = Unix::Conf::Bind8::DB::_delete_object ($self) or return ($ret); | ||
| 134 | } | ||
| 135 | $self->{LABEL} = $label; | ||
| 136 | $ret = Unix::Conf::Bind8::DB::_insert_object ($self) or return ($ret); | ||
| 137 | $self->dirty (1); | ||
| 138 | return (1); | ||
| 139 | } | ||
| 140 | |||
| 141 | return ( | ||
| 142 | defined ($self->{LABEL}) ? $self->{LABEL} : | ||
| 143 | Unix::Conf->_err ('label', "LABEL not defined") | ||
| 144 | ); | ||
| 145 | } | ||
| 146 | |||
| 147 | =item class () | ||
| 148 | |||
| 149 | Arguments | ||
| 150 | 'class' | ||
| 151 | |||
| 152 | Object method. | ||
| 153 | Get/set the record's class. If an argument is passed, the invocant's | ||
| 154 | class is set and true returned, on success, an Err object otherwise. | ||
| 155 | If no argument is passed the invocant's class is returned, if defined, | ||
| 156 | an Err object otherwise. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub class | ||
| 161 | { | ||
| 162 | my ($self, $class) = @_; | ||
| 163 | |||
| 164 | if (defined ($class)) { | ||
| 165 | return (Unix::Conf->_err ('class', "illegal class `$class'")) | ||
| 166 | if ($class !~ /^(in|hs|chaos)$/i); | ||
| 167 | return (Unix::Conf->_err ('class', "class `$class' not the same as DB class `$self->{PARENT}{CLASS}'")) | ||
| 168 | if ($class ne $self->{PARENT}{CLASS}); | ||
| 169 | $self->{CLASS} = $class; | ||
| 170 | $self->dirty (1); | ||
| 171 | return (1); | ||
| 172 | } | ||
| 173 | return ( | ||
| 174 | defined ($self->{CLASS}) ? $self->{CLASS} : | ||
| 175 | Unix::Conf->_err ('class', "class not defined") | ||
| 176 | ) | ||
| 177 | } | ||
| 178 | |||
| 179 | =item rtype () | ||
| 180 | |||
| 181 | Arguments | ||
| 182 | 'rtype' | ||
| 183 | |||
| 184 | Object method. | ||
| 185 | Get/set the record's rtype. If an argument is passed, the invocant's | ||
| 186 | rtype is set and true returned, on success, an Err object otherwise. | ||
| 187 | If no argument is passed the invocant's rtype is returned. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | # Do not allow change in RTYPE once defined | ||
| 192 | sub rtype | ||
| 193 | { | ||
| 194 | my ($self, $rtype) = @_; | ||
| 195 | |||
| 196 | if (defined ($rtype)) { | ||
| 197 | return (Unix::Conf->_err ('rtype', "RTYPE already defined")) | ||
| 198 | if (defined ($self->{RTYPE})); | ||
| 199 | $self->{RTYPE} = $rtype; | ||
| 200 | return (1); | ||
| 201 | } | ||
| 202 | return ($self->{RTYPE}); | ||
| 203 | } | ||
| 204 | |||
| 205 | =item ttl () | ||
| 206 | |||
| 207 | Arguments | ||
| 208 | ttl | ||
| 209 | |||
| 210 | Object method. | ||
| 211 | Get/set the record's ttl. If an argument is passed, the invocant's | ||
| 212 | ttl is set and true returned, on success, an Err object otherwise. | ||
| 213 | If no argument is passed the invocant's ttl is returned, if defined, | ||
| 214 | an Err object otherwise. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub ttl | ||
| 219 | { | ||
| 220 | my ($self, $ttl) = @_; | ||
| 221 | |||
| 222 | if (defined ($ttl)) { | ||
| 223 | return (Unix::Conf->_err ('ttl', "illegal ttl `$ttl'")) | ||
| 224 | unless (__is_validttl ($ttl)); | ||
| 225 | $self->{TTL} = $ttl; | ||
| 226 | $self->dirty (1); | ||
| 227 | return (1); | ||
| 228 | } | ||
| 229 | return ( | ||
| 230 | defined ($self->{TTL}) ? $self->{TTL} : | ||
| 231 | Unix::Conf->_err ('ttl', "TTL not defined") | ||
| 232 | ) | ||
| 233 | } | ||
| 234 | |||
| 235 | =item rdata () | ||
| 236 | |||
| 237 | Arguments | ||
| 238 | data | ||
| 239 | |||
| 240 | Object method. | ||
| 241 | Get/set the record's rdata. If an argument is passed, the invocant's | ||
| 242 | rdata is set and true returned, on success, an Err object otherwise. | ||
| 243 | If no argument is passed the invocant's rdata is returned. | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | sub rdata | ||
| 248 | { | ||
| 249 | my ($self, $rdata) = @_; | ||
| 250 | |||
| 251 | if (defined ($rdata)) { | ||
| 252 | # the object is stored in a complicated datastructure keyed | ||
| 253 | # on the rdata among other things. so we need to delete the | ||
| 254 | # old key and store the object at the new key | ||
| 255 | if (defined ($self->{RDATA})) { | ||
| 256 | my $ret; | ||
| 257 | $ret = Unix::Conf::Bind8::DB::_delete_object ($self) | ||
| 258 | or return ($ret); | ||
| 259 | # change rdata now before storing in new location as it is | ||
| 260 | # depenedant on the rdata. | ||
| 261 | $self->{RDATA} = $rdata; | ||
| 262 | return (Unix::Conf::Bind8::DB::_insert_object ($self)); | ||
| 263 | $self->dirty (1); | ||
| 264 | } | ||
| 265 | |||
| 266 | $self->{RDATA} = $rdata; | ||
| 267 | $self->dirty (1); | ||
| 268 | return (1); | ||
| 269 | } | ||
| 270 | return ( | ||
| 271 | defined ($self->{RDATA}) ? $self->{RDATA} : | ||
| 272 | Unix::Conf->_err ('rdata', "RDATA not defined") | ||
| 273 | ); | ||
| 274 | } | ||
| 275 | |||
| 276 | =item dirty () | ||
| 277 | |||
| 278 | Arguments | ||
| 279 | dirty # numeric | ||
| 280 | |||
| 281 | Object method. | ||
| 282 | Get/set the dirty attribute. If an argument is passed, sets it | ||
| 283 | as the value of the dirty attribute, and returns true on success, | ||
| 284 | an Err object otherwise. If no argument is passed, returns the | ||
| 285 | value of the attribute. | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | # we set the dirty flag in the containing object using the PARENT member. | ||
| 290 | sub dirty | ||
| 291 | { | ||
| 292 | my ($self, $dirty) = @_; | ||
| 293 | |||
| 294 | if (defined ($dirty)) { | ||
| 295 | $self->{PARENT}{DIRTY} = $dirty; | ||
| 296 | return (1); | ||
| 297 | } | ||
| 298 | return ($self->{PARENT}{DIRTY}); | ||
| 299 | } | ||
| 300 | |||
| 301 | =item delete () | ||
| 302 | |||
| 303 | Object method. | ||
| 304 | Deletes the invocant record and returns true on success, | ||
| 305 | an Err object otherwise. | ||
| 306 | |||
| 307 | =cut | ||
| 308 | |||
| 309 | sub delete | ||
| 310 | { | ||
| 311 | return (Unix::Conf::Bind8::DB::_delete_object ($_[0])); | ||
| 312 | $_[0]->dirty (1); | ||
| 313 | } | ||
| 314 | |||
| 315 | # this is used to set the memeber PARENT which points to the hash in | ||
| 316 | # which we are contained. This helps us set the dirty flag in case | ||
| 317 | # we are modified. | ||
| 318 | sub _parent | ||
| 319 | { | ||
| 320 | my ($self, $parent) = @_; | ||
| 321 | |||
| 322 | if (defined ($parent)) { | ||
| 323 | # Don't allow changing value once defined. | ||
| 324 | return (Unix::Conf->_err ('parent', "PARENT already defined")) | ||
| 325 | if (defined ($self->{PARENT})); | ||
| 326 | $self->{PARENT} = $parent; | ||
| 327 | return (1); | ||
| 328 | } | ||
| 329 | return ( | ||
| 330 | defined ($self->{PARENT}) ? $self->{PARENT} : | ||
| 331 | Unix::Conf->_err ('parent', "PARENT not defined") | ||
| 332 | ); | ||
| 333 | } | ||
| 334 | |||
| 335 | 1; | ||
| 336 | __END__ |