| 154 | | |
| 155 | | // |
| 156 | | // Major file section |
| 157 | | // Importing DNS data functions |
| 158 | | // |
| 159 | | |
| 160 | | void ProcessRRLine(const char *cLine,char *cZoneName,const unsigned uZone, |
| 161 | | const unsigned uCustId,const unsigned uNSSet, |
| 162 | | const unsigned uCreatedBy,const char *cComment) |
| 163 | | { |
| 164 | | char cName[100]={""}; |
| 165 | | char cNamePlus[200]={""}; |
| 166 | | char cParam1[256]={""}; |
| 167 | | char cParam2[256]={""}; |
| 168 | | char cParam3[256]={""}; |
| 169 | | char cParam4[256]={""}; |
| 170 | | char cType[256]={""}; |
| 171 | | static char cPrevZoneName[100]={""}; |
| 172 | | static char cPrevcName[100]={""}; |
| 173 | | unsigned uRRType=0; |
| 174 | | static unsigned uPrevTTL=0;//Has to be reset every new cZoneName |
| 175 | | unsigned uTTL=0; |
| 176 | | static char cPrevOrigin[100]={""}; |
| 177 | | char *cp; |
| 178 | | char cResourceImportTable[256]="tResourceImport"; |
| 179 | | |
| 180 | | GetConfiguration("cResourceImportTable",cResourceImportTable,0); |
| 181 | | |
| 182 | | |
| 183 | | if(strcmp(cZoneName,cPrevZoneName)) |
| 184 | | { |
| 185 | | //printf("New zone %s\n",cZoneName); |
| 186 | | sprintf(cPrevZoneName,"%.99s",cZoneName); |
| 187 | | uPrevTTL=0; |
| 188 | | cPrevOrigin[0]=0; |
| 189 | | cPrevcName[0]=0; |
| 190 | | } |
| 191 | | |
| 192 | | if((cp=strchr(cLine,';'))) |
| 193 | | { |
| 194 | | *cp='\n'; |
| 195 | | *(cp+1)=0; |
| 196 | | } |
| 197 | | |
| 198 | | |
| 199 | | //debug only |
| 200 | | //printf("<u>%s</u>\n",cLine); |
| 201 | | |
| 202 | | if(cLine[0]=='\t') |
| 203 | | { |
| 204 | | sscanf(cLine,"%100s %255s %255s %255s %255s\n", |
| 205 | | cType,cParam1,cParam2,cParam3,cParam4); |
| 206 | | if(cPrevcName[0]) |
| 207 | | sprintf(cName,"%.99s",cPrevcName); |
| 208 | | else |
| 209 | | strcpy(cName,"\t"); |
| 210 | | } |
| 211 | | else if(cLine[0]==' ') |
| 212 | | { |
| 213 | | sscanf(cLine,"%99s %100s %255s %255s %255s %255s\n", |
| 214 | | cName,cType,cParam1,cParam2,cParam3,cParam4); |
| 215 | | sprintf(cName,"@"); |
| 216 | | sprintf(cPrevcName,"%.99s",cName); |
| 217 | | } |
| 218 | | else if(cLine[0]=='@') |
| 219 | | { |
| 220 | | sscanf(cLine,"@ %100s %255s %255s %255s %255s\n", |
| 221 | | cType,cParam1,cParam2,cParam3,cParam4); |
| 222 | | if(cPrevcName[0]) |
| 223 | | sprintf(cName,"%.99s",cPrevcName); |
| 224 | | else |
| 225 | | strcpy(cName,"\t"); |
| 226 | | } |
| 227 | | else |
| 228 | | { |
| 229 | | sscanf(cLine,"%99s %100s %255s %255s %255s %255s\n", |
| 230 | | cName,cType,cParam1,cParam2,cParam3,cParam4); |
| 231 | | sprintf(cPrevcName,"%.99s",cName); |
| 232 | | } |
| 233 | | |
| 234 | | if(!cLine[0] || cLine[0]=='\n') |
| 235 | | return; |
| 236 | | |
| 237 | | |
| 238 | | if(cName[0]!='$') |
| 239 | | { |
| 240 | | //Shift left on inline TTL NOT $TTL directive |
| 241 | | sscanf(cType,"%u",&uTTL); |
| 242 | | if(uTTL>1 && uTTL<800000) |
| 243 | | { |
| 244 | | strcpy(cType,cParam1); |
| 245 | | strcpy(cParam1,cParam2); |
| 246 | | strcpy(cParam2,cParam3); |
| 247 | | strcpy(cParam3,cParam4); |
| 248 | | } |
| 249 | | } |
| 250 | | |
| 251 | | //Shift left on IN |
| 252 | | if(!strcasecmp(cType,"IN")) |
| 253 | | { |
| 254 | | strcpy(cType,cParam1); |
| 255 | | strcpy(cParam1,cParam2); |
| 256 | | strcpy(cParam2,cParam3); |
| 257 | | strcpy(cParam3,cParam4); |
| 258 | | } |
| 259 | | |
| 260 | | |
| 261 | | //Check for recognized data and verify needed params |
| 262 | | if(!strcasecmp(cType,"A")) |
| 263 | | { |
| 264 | | uRRType=1; |
| 265 | | if(!cParam1[0] || cParam2[0]) |
| 266 | | { |
| 267 | | fprintf(stdout,"ProcessRRLine() Error %s: Incorrect A format: %s\n", |
| 268 | | cZoneName,cLine); |
| 269 | | return; |
| 270 | | } |
| 271 | | if(!strcmp(IPNumber(cParam1),"0.0.0.0")) |
| 272 | | { |
| 273 | | printf("Incorrect A IP number format: %s\n",cLine); |
| 274 | | return; |
| 275 | | } |
| 276 | | } |
| 277 | | else if(!strcasecmp(cType,"CNAME")) |
| 278 | | { |
| 279 | | char cZone[254]; |
| 280 | | char cName[254]; |
| 281 | | uRRType=5; |
| 282 | | if(!cParam1[0] || cParam2[0]) |
| 283 | | { |
| 284 | | fprintf(stdout,"ProcessRRLine() Error %s: Incorrect CNAME format: %s\n", |
| 285 | | cZoneName,cLine); |
| 286 | | return; |
| 287 | | } |
| 288 | | if(cParam1[strlen(cParam1)-1]!='.') |
| 289 | | { |
| 290 | | printf("Warning: Incorrect FQDN missing period: %s\n",cParam1); |
| 291 | | if(cPrevOrigin[0]) |
| 292 | | { |
| 293 | | sprintf(gcQuery,"%.255s.%.99s",cParam1,cPrevOrigin); |
| 294 | | sprintf(cParam1,"%.255s",gcQuery); |
| 295 | | printf("Fixed: CNAME RHS fixed from $ORIGIN: %s\n",cParam1); |
| 296 | | } |
| 297 | | else |
| 298 | | { |
| 299 | | sprintf(gcQuery,"%.255s.%.99s.",cParam1,cZoneName); |
| 300 | | sprintf(cParam1,"%.255s",gcQuery); |
| 301 | | printf("Fixed: CNAME RHS fixed from cZoneName: %s\n",cParam1); |
| 302 | | } |
| 303 | | } |
| 304 | | sprintf(cZone,"%s.",FQDomainName(cZoneName)); |
| 305 | | strcpy(cName,FQDomainName(cParam1)); |
| 306 | | if(strcmp(cName+strlen(cName)-strlen(cZone),cZone)) |
| 307 | | printf("Warning: CNAME RHS should probably end with zone: %s\n",cLine); |
| 308 | | } |
| 309 | | else if(!strcasecmp(cType,"NS")) |
| 310 | | { |
| 311 | | MYSQL_RES *res; |
| 312 | | char cNS[100]; |
| 313 | | |
| 314 | | uRRType=2; |
| 315 | | if(!cParam1[0] || cParam2[0]) |
| 316 | | { |
| 317 | | fprintf(stdout,"ProcessRRLine() Error %s: Incorrect NS format: %s\n", |
| 318 | | cZoneName,cLine); |
| 319 | | return; |
| 320 | | } |
| 321 | | if(cParam1[strlen(cParam1)-1]!='.') |
| 322 | | { |
| 323 | | fprintf(stdout,"Warning %s: Incorrect FQDN missing period: %s\n", |
| 324 | | cZoneName,cLine); |
| 325 | | if(cPrevOrigin[0]) |
| 326 | | { |
| 327 | | sprintf(gcQuery,"%.255s.%.99s",cParam1,cPrevOrigin); |
| 328 | | sprintf(cParam1,"%.255s",gcQuery); |
| 329 | | printf("Fixed: NS RHS fixed from $ORIGIN: %s\n",cParam1); |
| 330 | | } |
| 331 | | else |
| 332 | | { |
| 333 | | sprintf(gcQuery,"%.255s.%.99s.",cParam1,cZoneName); |
| 334 | | sprintf(cParam1,"%.255s",gcQuery); |
| 335 | | printf("Fixed: NS RHS fixed from cZoneName: %s\n",cParam1); |
| 336 | | } |
| 337 | | } |
| 338 | | |
| 339 | | //Get rid of last period for check. |
| 340 | | strcpy(cNS,cParam1); |
| 341 | | cNS[strlen(cNS)-1]=0; |
| 342 | | sprintf(gcQuery,"SELECT uNS FROM tNS WHERE uNSSet=%u AND cFQDN LIKE '%s' ", |
| 343 | | uNSSet,cNS); |
| 344 | | mysql_query(&gMysql,gcQuery); |
| 345 | | if(mysql_errno(&gMysql)) |
| 346 | | { |
| 347 | | fprintf(stdout,"ProcessRRLine() Error %s: %s\n",cZoneName,mysql_error(&gMysql)); |
| 348 | | return; |
| 349 | | } |
| 350 | | res=mysql_store_result(&gMysql); |
| 351 | | if(mysql_num_rows(res)) |
| 352 | | { |
| 353 | | fprintf(stdout,"ProcessRRLine() warning %s:NS RR Ignored. NS belongs to current uNSSet=%u\n", |
| 354 | | cZoneName,uNSSet); |
| 355 | | return; |
| 356 | | } |
| 357 | | mysql_free_result(res); |
| 358 | | } |
| 359 | | else if(!strcasecmp(cType,"MX")) |
| 360 | | { |
| 361 | | unsigned uMX=0; |
| 362 | | uRRType=3; |
| 363 | | if(!cParam1[0] || !cParam2[0] ) |
| 364 | | { |
| 365 | | fprintf(stdout,"ProcessRRLine() Error %s: Missing MX param: %s\n", |
| 366 | | cZoneName,cLine); |
| 367 | | return; |
| 368 | | } |
| 369 | | sscanf(cParam1,"%u",&uMX); |
| 370 | | if(uMX<1 || uMX>99999) |
| 371 | | { |
| 372 | | fprintf(stdout,"ProcessRRLine() Error %s: Incorrect MX format: %s\n", |
| 373 | | cZoneName,cLine); |
| 374 | | return; |
| 375 | | } |
| 376 | | if(cParam2[strlen(cParam2)-1]!='.') |
| 377 | | { |
| 378 | | fprintf(stdout,"Warning %s: Incorrect FQDN missing period: %s\n", |
| 379 | | cZoneName,cLine); |
| 380 | | if(cPrevOrigin[0]) |
| 381 | | { |
| 382 | | sprintf(gcQuery,"%.255s.%.99s",cParam2,cPrevOrigin); |
| 383 | | sprintf(cParam2,"%.255s",gcQuery); |
| 384 | | printf("Fixed: MX RHS fixed from $ORIGIN: %s\n",cParam2); |
| 385 | | } |
| 386 | | else |
| 387 | | { |
| 388 | | sprintf(gcQuery,"%.255s.%.99s.",cParam2,cZoneName); |
| 389 | | sprintf(cParam2,"%.255s",gcQuery); |
| 390 | | printf("Fixed: MX RHS fixed from cZoneName: %s\n",cParam2); |
| 391 | | } |
| 392 | | } |
| 393 | | } |
| 394 | | else if(!strcasecmp(cType,"PTR")) |
| 395 | | { |
| 396 | | unsigned uFirstDigit=0; |
| 397 | | |
| 398 | | uRRType=7; |
| 399 | | if(!cParam1[0] || cParam2[0]) |
| 400 | | { |
| 401 | | fprintf(stdout,"ProcessRRLine() Error %s: Incorrect PTR format: %s\n", |
| 402 | | cZoneName,cLine); |
| 403 | | return; |
| 404 | | } |
| 405 | | sscanf(cName,"%u.%*s",&uFirstDigit); |
| 406 | | if(!uFirstDigit) |
| 407 | | { |
| 408 | | //Check this rule again |
| 409 | | fprintf(stdout,"ProcessRRLine() Error %s: Incorrect PTR LHS should start with a non zero digit: %s\n", |
| 410 | | cZoneName,cLine); |
| 411 | | return; |
| 412 | | } |
| 413 | | |
| 414 | | } |
| 415 | | else if(!strcasecmp(cType,"TXT")) |
| 416 | | { |
| 417 | | uRRType=6; |
| 418 | | if((cp=strchr(cLine,'"'))) |
| 419 | | sprintf(cParam1,"%.255s",cp); |
| 420 | | //Adjust for cr/lf also |
| 421 | | if(!cParam1[0] || cParam1[0]!='\"' || (cParam1[strlen(cParam1)-2]!='\"' && |
| 422 | | cParam1[strlen(cParam1)-1]!='\"') ) |
| 423 | | { |
| 424 | | fprintf(stdout,"ProcessRRLine() Error %s: Incorrect TXT format: %s\n", |
| 425 | | cZoneName,cParam1); |
| 426 | | return; |
| 427 | | } |
| 428 | | //debug only |
| 429 | | printf("TXT: %s\n",cParam1); |
| 430 | | } |
| 431 | | |
| 432 | | //Special cases that should keep a static var for next line |
| 433 | | else if(!strcasecmp(cName,"$TTL")) |
| 434 | | { |
| 435 | | if(cType[0]) |
| 436 | | { |
| 437 | | sscanf(cType,"%u",&uPrevTTL); |
| 438 | | //debug only |
| 439 | | printf("$TTL changed: %u (%s %s)\n", |
| 440 | | uPrevTTL,cType,cParam1); |
| 441 | | return; |
| 442 | | } |
| 443 | | } |
| 444 | | else if(!strcasecmp(cName,"$ORIGIN")) |
| 445 | | { |
| 446 | | if(cType[0] && strcmp(cType,".") && cType[strlen(cType)-1]=='.') |
| 447 | | sprintf(cPrevOrigin,"%.99s",cType); |
| 448 | | { |
| 449 | | cPrevcName[0]=0;//Reset? |
| 450 | | if(!strncasecmp(cPrevOrigin,cZoneName,strlen(cPrevOrigin)-1)) |
| 451 | | cPrevOrigin[0]=0; |
| 452 | | else |
| 453 | | //debug only |
| 454 | | printf("$ORIGIN Changed: %s\n", |
| 455 | | cPrevOrigin); |
| 456 | | return; |
| 457 | | } |
| 458 | | } |
| 459 | | |
| 460 | | //Unrecognized lines |
| 461 | | //Current missing features -we know about and need: hinfo ignored |
| 462 | | else if(1 && cLine[0] && cLine[0]!='\n') |
| 463 | | { |
| 464 | | fprintf(stdout,"ProcessRRLine() Error %s: RR Not recognized: %s %s\n",cZoneName,cLine,cType); |
| 465 | | return; |
| 466 | | } |
| 467 | | |
| 468 | | if(!uTTL && uPrevTTL) uTTL=uPrevTTL; |
| 469 | | if(cPrevOrigin[0]) |
| 470 | | sprintf(cNamePlus,"%.99s.%.99s",cName,cPrevOrigin); |
| 471 | | else |
| 472 | | sprintf(cNamePlus,"%.99s",cName); |
| 473 | | if(uRRType==6)//TXT special case |
| 474 | | sprintf(gcQuery,"INSERT INTO %s SET uZone=%u,cName='%s',uTTL=%u,uRRType=%u,cParam1='%s'," |
| 475 | | "cComment='%s',uOwner=%u,uCreatedBy=%u,uCreatedDate=UNIX_TIMESTAMP(NOW())" |
| 476 | | ,cResourceImportTable |
| 477 | | ,uZone |
| 478 | | ,FQDomainName(cNamePlus) |
| 479 | | ,uTTL |
| 480 | | ,uRRType |
| 481 | | ,cParam1 |
| 482 | | ,cComment |
| 483 | | ,uCustId |
| 484 | | ,uCreatedBy); |
| 485 | | else |
| 486 | | sprintf(gcQuery,"INSERT INTO %s SET uZone=%u,cName='%s',uTTL=%u,uRRType=%u,cParam1='%s'," |
| 487 | | "cParam2='%s',cComment='%s',uOwner=%u,uCreatedBy=%u,uCreatedDate=UNIX_TIMESTAMP(NOW())" |
| 488 | | ,cResourceImportTable |
| 489 | | ,uZone |
| 490 | | ,FQDomainName(cNamePlus) |
| 491 | | ,uTTL |
| 492 | | ,uRRType |
| 493 | | ,FQDomainName(cParam1) |
| 494 | | ,FQDomainName(cParam2) |
| 495 | | ,cComment |
| 496 | | ,uCustId |
| 497 | | ,uCreatedBy); |
| 498 | | mysql_query(&gMysql,gcQuery); |
| 499 | | if(mysql_errno(&gMysql)) |
| 500 | | fprintf(stdout,"ProcessRRLine() Error %s: %s\n",cZoneName,mysql_error(&gMysql)); |
| 501 | | |
| 502 | | return; |
| 503 | | |
| 504 | | }//void ProcessRRLine() |
| 505 | | |
| 506 | | |
| 507 | | void Import(void) |
| 508 | | { |
| 509 | | struct dirent **namelist; |
| 510 | | structSOA *importSOA; |
| 511 | | register int n,i; |
| 512 | | char cHostMaster[256]="hostmaster.isp.net"; |
| 513 | | char cuView[256]="2"; |
| 514 | | char cuNSSet[256]="0"; |
| 515 | | char cuOwner[256]="1"; |
| 516 | | char cZoneImportTable[256]="tZoneImport"; |
| 517 | | |
| 518 | | printf("ImportZones() Start\n"); |
| 519 | | |
| 520 | | GetConfiguration("cHostMaster",cHostMaster,0); |
| 521 | | GetConfiguration("cuView",cuView,0); |
| 522 | | GetConfiguration("cZoneImportTable",cZoneImportTable,0); |
| 523 | | GetConfiguration("cuNSSet",cuNSSet,0); |
| 524 | | GetConfiguration("cuOwner",cuOwner,0); |
| 525 | | |
| 526 | | printf("Importing master zones from /usr/local/idns/import directory.\n" |
| 527 | | "File name must be same as zone name.\nOnly A, CNAME, MX, PTR and" |
| 528 | | " NS records supported.\nSOA ttl values must be seconds.\n" |
| 529 | | "You can also set cHostMaster, cuNSSet, cuView, cuOwner and cZoneImportTable in\n" |
| 530 | | "tConfiguration. Current defaults:\n" |
| 531 | | "cHostMaster=%s\n" |
| 532 | | "cuNSSet=%s (if set to zero will try to determine from SOA or fallback to uNSSet=1)\n" |
| 533 | | "cuView=%s\n" |
| 534 | | "cuOwner=%s\n" |
| 535 | | "cZoneImportTable=%s\n\n",cHostMaster,cuNSSet,cuView,cuOwner,cZoneImportTable); |
| 536 | | printf("Confirm <enter any key>, <ctrl-c> to abort\n"); |
| 537 | | getchar(); |
| 538 | | |
| 539 | | n=scandir("/usr/local/idns/import",&namelist,0,0); |
| 540 | | |
| 541 | | if(n<0) |
| 542 | | { |
| 543 | | fprintf(stdout,"scandir() error: Does /usr/local/idns/import exist?\n"); |
| 544 | | return; |
| 545 | | } |
| 546 | | else if(n==2) |
| 547 | | { |
| 548 | | fprintf(stdout,"No files found.\n"); |
| 549 | | return; |
| 550 | | } |
| 551 | | |
| 552 | | for(i=0;i<n;i++) |
| 553 | | { |
| 554 | | |
| 555 | | //Added some end of list test hack a long time ago. Remove? |
| 556 | | if(namelist[i]->d_name[0]=='.' || |
| 557 | | strstr(namelist[i]->d_name+strlen(namelist[i]->d_name)-5, |
| 558 | | ".done")) |
| 559 | | { |
| 560 | | ; |
| 561 | | } |
| 562 | | else |
| 563 | | { |
| 564 | | FILE *fp; |
| 565 | | |
| 566 | | sprintf(gcQuery,"/usr/local/idns/import/%.100s", |
| 567 | | namelist[i]->d_name); |
| 568 | | if(!(fp=fopen(gcQuery,"r"))) |
| 569 | | { |
| 570 | | fprintf(stdout,"Error: Could not open: %s\n",gcQuery); |
| 571 | | return; |
| 572 | | } |
| 573 | | |
| 574 | | //Start processing |
| 575 | | printf("%.100s\n\n",namelist[i]->d_name); |
| 576 | | |
| 577 | | //Major import component |
| 578 | | importSOA=ProcessSOA(fp); |
| 579 | | |
| 580 | | //Add more rule based checking of TTLs |
| 581 | | if( importSOA->uTTL && |
| 582 | | importSOA->uSerial && |
| 583 | | importSOA->uRefresh && |
| 584 | | importSOA->uRetry && |
| 585 | | importSOA->uExp && |
| 586 | | importSOA->uNegTTL && |
| 587 | | importSOA->cHostmaster[0] ) |
| 588 | | { |
| 589 | | //All zones will belong to the default #1 NS |
| 590 | | unsigned uZone,uOwner=1,uNSSet=0; |
| 591 | | |
| 592 | | sscanf(cuOwner,"%u",&uOwner); |
| 593 | | sscanf(cuNSSet,"%u",&uNSSet); |
| 594 | | |
| 595 | | //Import zone |
| 596 | | importSOA->cNameServer[strlen(importSOA->cNameServer)-1]=0; |
| 597 | | importSOA->cHostmaster[strlen(importSOA->cHostmaster)-1]=0; |
| 598 | | //debug only |
| 599 | | //printf("uTTL=%u\n",importSOA->uTTL); |
| 600 | | //printf("uSerial=%u\n",importSOA->uSerial); |
| 601 | | //printf("uRefresh=%u\n",importSOA->uRefresh); |
| 602 | | //printf("uRetry=%u\n",importSOA->uRetry); |
| 603 | | //printf("uExp=%u\n",importSOA->uExp); |
| 604 | | //printf("uNegTTL=%u\n",importSOA->uNegTTL); |
| 605 | | //printf("cHostmaster=%s\n",importSOA->cHostmaster); |
| 606 | | //printf("cNameServer=%s\n",importSOA->cNameServer); |
| 607 | | |
| 608 | | //Try to use an existing tNSSet |
| 609 | | if(uNSSet==0) |
| 610 | | { |
| 611 | | uNSSet=uGetNSSet(importSOA->cNameServer); |
| 612 | | |
| 613 | | if(uNSSet==0) |
| 614 | | uNSSet=1;//First tNSSet |
| 615 | | else |
| 616 | | printf("Using uNSSet=%u determined via:%s\n", |
| 617 | | uNSSet,importSOA->cNameServer); |
| 618 | | } |
| 619 | | else |
| 620 | | { |
| 621 | | uNSSet=1;//First tNSSet |
| 622 | | } |
| 623 | | |
| 624 | | |
| 625 | | //uZoneTTL is the NegTTL |
| 626 | | sprintf(gcQuery,"INSERT INTO %s SET cZone='%s',uNSSet=%u,uSerial=%u," |
| 627 | | "uExpire=%u,uRefresh=%u,uTTL=%u,uRetry=%u,uZoneTTL=%u,uMailServers=0," |
| 628 | | "cMainAddress='0.0.0.0',uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())," |
| 629 | | "uModBy=0,uModDate=0,cHostmaster='%s IMPORTED',uView=%.2s" |
| 630 | | ,cZoneImportTable |
| 631 | | ,FQDomainName(namelist[i]->d_name) |
| 632 | | ,uNSSet |
| 633 | | ,importSOA->uSerial |
| 634 | | ,importSOA->uExp |
| 635 | | ,importSOA->uRefresh |
| 636 | | ,importSOA->uTTL |
| 637 | | ,importSOA->uRetry |
| 638 | | ,importSOA->uNegTTL |
| 639 | | ,uOwner |
| 640 | | ,cHostMaster |
| 641 | | ,cuView); |
| 642 | | //,importSOA->cHostmaster); |
| 643 | | |
| 644 | | mysql_query(&gMysql,gcQuery); |
| 645 | | if(mysql_errno(&gMysql)) |
| 646 | | { |
| 647 | | fprintf(stdout,"Error %s: %s\n", |
| 648 | | namelist[i]->d_name,mysql_error(&gMysql)); |
| 649 | | if(!strncmp("Duplicate entry",mysql_error(&gMysql),15)) |
| 650 | | { |
| 651 | | MYSQL_RES *res4; |
| 652 | | MYSQL_ROW field4; |
| 653 | | |
| 654 | | |
| 655 | | uZone=0; |
| 656 | | uOwner=0; |
| 657 | | sprintf(gcQuery, |
| 658 | | "SELECT uZone,uOwner FROM %s WHERE cZone='%s'", |
| 659 | | cZoneImportTable, |
| 660 | | FQDomainName(namelist[i]->d_name)); |
| 661 | | mysql_query(&gMysql,gcQuery); |
| 662 | | res4=mysql_store_result(&gMysql); |
| 663 | | if((field4=mysql_fetch_row(res4))) |
| 664 | | { |
| 665 | | sscanf(field4[0],"%u",&uZone); |
| 666 | | sscanf(field4[1],"%u",&uOwner); |
| 667 | | } |
| 668 | | mysql_free_result(res4); |
| 669 | | if(uZone) |
| 670 | | { |
| 671 | | fprintf(stdout,"Warning %s: Adding RR to a zone" |
| 672 | | " that already exists!\n",namelist[i]->d_name); |
| 673 | | goto AddToExisting; |
| 674 | | } |
| 675 | | } |
| 676 | | |
| 677 | | continue; |
| 678 | | } |
| 679 | | uZone=mysql_insert_id(&gMysql); |
| 680 | | |
| 681 | | AddToExisting: |
| 682 | | if(!uOwner) uOwner=1; |
| 683 | | //Process rest of file for RRs |
| 684 | | //printf("Importing RRs\n"); |
| 685 | | while(fgets(gcQuery,254,fp)!=NULL) |
| 686 | | { |
| 687 | | //skip empty lines |
| 688 | | if(gcQuery[0]!='\n') |
| 689 | | ProcessRRLine(gcQuery, |
| 690 | | FQDomainName(namelist[i]->d_name), |
| 691 | | uZone,uOwner,uNSSet,1," IMPORTED"); |
| 692 | | } |
| 693 | | } |
| 694 | | else |
| 695 | | { |
| 696 | | fprintf(stdout,"Error %s:\nCould not process SOA correctly\n", |
| 697 | | namelist[i]->d_name); |
| 698 | | printf("uTTL=%u\n",importSOA->uTTL); |
| 699 | | printf("uSerial=%u\n",importSOA->uSerial); |
| 700 | | printf("uRefresh=%u\n",importSOA->uRefresh); |
| 701 | | printf("uRetry=%u\n",importSOA->uRetry); |
| 702 | | printf("uExp=%u\n",importSOA->uExp); |
| 703 | | printf("uNegTTL=%u\n",importSOA->uNegTTL); |
| 704 | | printf("cHostmaster=%s\n",importSOA->cHostmaster); |
| 705 | | printf("cNameServer=%s\n",importSOA->cNameServer); |
| 706 | | return; |
| 707 | | } |
| 708 | | fclose(fp); |
| 709 | | |
| 710 | | }//If valid file for importing |
| 711 | | |
| 712 | | }//for each file in import dir |
| 713 | | printf("\nImportZones() Done\n"); |
| 714 | | |
| 715 | | }//void Import(void) |
| 716 | | |
| 717 | | |
| 718 | | void ImportSORRs(void) |
| 719 | | { |
| 720 | | // |
| 721 | | //This function import the zone RRs for secondary service only zones |
| 722 | | //into the iDNS database. |
| 723 | | //These records will be read only, which will be limited at interface level. |
| 724 | | //The code here is heavily based on the code of the Import() function above. |
| 725 | | // |
| 726 | | //Steps: |
| 727 | | //1. Query the database to find out the secondary service only zones |
| 728 | | //2. Once we have the zone name, we build the zone file path using: |
| 729 | | // iDNSRootFolder/named.d/slave/[external|internal]/(cZone 1st char)/cZone |
| 730 | | //3. We open the file |
| 731 | | //4. Must remove all existing RRs at the zone, we are updating! |
| 732 | | //5. We run a modified version of the loop in the Import() function that imports the zone RRs by |
| 733 | | //calling ProcessRRLine() |
| 734 | | |
| 735 | | MYSQL_RES *res; |
| 736 | | MYSQL_ROW field; |
| 737 | | char cFileName[512]={""}; |
| 738 | | char *cView=""; |
| 739 | | FILE *fp; |
| 740 | | unsigned uZone=0; |
| 741 | | unsigned uOwner=0; |
| 742 | | unsigned uNSSet=0; |
| 743 | | structSOA *importSOA; |
| 744 | | |
| 745 | | |
| 746 | | printf("ImportSORRs() Start\n"); |
| 747 | | |
| 748 | | //1. Query the database to find out the secondary service only zones |
| 749 | | sprintf(gcQuery,"SELECT cZone,uView,uZone,uOwner,uNSSet FROM tZone WHERE uSecondaryOnly=1 ORDER BY uZone"); |
| 750 | | mysql_query(&gMysql,gcQuery); |
| 751 | | if(mysql_errno(&gMysql)) |
| 752 | | { |
| 753 | | fprintf(stderr,"Error: %s\n",mysql_error(&gMysql)); |
| 754 | | exit(1); |
| 755 | | } |
| 756 | | |
| 757 | | res=mysql_store_result(&gMysql); |
| 758 | | |
| 759 | | while((field=mysql_fetch_row(res))) |
| 760 | | { |
| 761 | | //2. Once we have the zone name, we build the zone file path |
| 762 | | if(!strcmp(field[1],"1")) |
| 763 | | cView="internal"; |
| 764 | | else |
| 765 | | cView="external"; |
| 766 | | |
| 767 | | sprintf(cFileName,"/usr/local/idns/named.d/slave/%s/%c/%s", |
| 768 | | cView |
| 769 | | ,field[0][0] |
| 770 | | ,field[0] |
| 771 | | ); |
| 772 | | //3. We open the file |
| 773 | | if((fp=fopen(cFileName,"r"))==NULL) |
| 774 | | { |
| 775 | | fprintf(stderr,"Warning: could not open zonefile for zone %s, path was %s\n", |
| 776 | | field[0],cFileName); |
| 777 | | continue; |
| 778 | | } |
| 779 | | |
| 780 | | //4. Must remove all existing RRs at the zone, we are updating! |
| 781 | | |
| 782 | | sscanf(field[2],"%u",&uZone); |
| 783 | | sscanf(field[3],"%u",&uOwner); |
| 784 | | sscanf(field[4],"%u",&uNSSet); |
| 785 | | |
| 786 | | sprintf(gcQuery,"DELETE FROM tResource WHERE uZone='%u'",uZone); |
| 787 | | mysql_query(&gMysql,gcQuery); |
| 788 | | if(mysql_errno(&gMysql)) |
| 789 | | { |
| 790 | | fprintf(stderr,"Error: %s\n",mysql_error(&gMysql)); |
| 791 | | exit(1); |
| 792 | | } |
| 793 | | |
| 794 | | //5. We run a modified version of the loop in the Import() function |
| 795 | | importSOA=ProcessSOA(fp); //Unused, just to move file pointer to 'after soa position' |
| 796 | | |
| 797 | | while(fgets(gcQuery,254,fp)!=NULL) |
| 798 | | { |
| 799 | | //skip empty lines |
| 800 | | if(gcQuery[0]!='\n') |
| 801 | | ProcessSORRLine(gcQuery,field[0],uZone,uOwner,uNSSet,1," SecOnly IMPORTED"); |
| 802 | | } |
| 803 | | }//while((field=mysql_fetch_row(res))) |
| 804 | | |
| 805 | | printf("ImportSORRs() End\n"); |
| 806 | | |
| 807 | | }//void ImportSORRs(void) |
| 808 | | |
| 809 | | |
| 810 | | void ProcessSORRLine(const char *cLine,char *cZoneName,const unsigned uZone, |
| 811 | | const unsigned uCustId,const unsigned uNSSet, |
| 812 | | const unsigned uCreatedBy,const char *cComment) |
| 813 | | { |
| 814 | | char cName[100]={""}; |
| 815 | | char cNamePlus[200]={""}; |
| 816 | | char cParam1[256]={""}; |
| 817 | | char cParam2[256]={""}; |
| 818 | | char cParam3[256]={""}; |
| 819 | | char cParam4[256]={""}; |
| 820 | | char cType[256]={""}; |
| 821 | | static char cPrevZoneName[100]={""}; |
| 822 | | static char cPrevcName[100]={""}; |
| 823 | | unsigned uRRType=0; |
| 824 | | static unsigned uPrevTTL=0;//Has to be reset every new cZoneName |
| 825 | | unsigned uTTL=0; |
| 826 | | static char cPrevOrigin[100]={""}; |
| 827 | | char *cp; |
| 828 | | |
| 829 | | |
| 830 | | if(strcmp(cZoneName,cPrevZoneName)) |
| 831 | | { |
| 832 | | //printf("New zone %s\n",cZoneName); |
| 833 | | sprintf(cPrevZoneName,"%.99s",cZoneName); |
| 834 | | uPrevTTL=0; |
| 835 | | cPrevOrigin[0]=0; |
| 836 | | cPrevcName[0]=0; |
| 837 | | } |
| 838 | | |
| 839 | | if((cp=strchr(cLine,';'))) |
| 840 | | { |
| 841 | | *cp='\n'; |
| 842 | | *(cp+1)=0; |
| 843 | | } |
| 844 | | |
| 845 | | |
| 846 | | //debug only |
| 847 | | //printf("<u>%s</u>\n",cLine); |
| 848 | | |
| 849 | | if(cLine[0]=='\t') |
| 850 | | { |
| 851 | | sscanf(cLine,"%100s %255s %255s %255s %255s\n", |
| 852 | | cType,cParam1,cParam2,cParam3,cParam4); |
| 853 | | if(cPrevcName[0]) |
| 854 | | sprintf(cName,"%.99s",cPrevcName); |
| 855 | | else |
| 856 | | strcpy(cName,"\t"); |
| 857 | | } |
| 858 | | else if(cLine[0]=='@') |
| 859 | | { |
| 860 | | sscanf(cLine,"@ %100s %255s %255s %255s %255s\n", |
| 861 | | cType,cParam1,cParam2,cParam3,cParam4); |
| 862 | | if(cPrevcName[0]) |
| 863 | | sprintf(cName,"%.99s",cPrevcName); |
| 864 | | else |
| 865 | | strcpy(cName,"\t"); |
| 866 | | } |
| 867 | | else |
| 868 | | { |
| 869 | | sscanf(cLine,"%99s %100s %255s %255s %255s %255s\n", |
| 870 | | cName,cType,cParam1,cParam2,cParam3,cParam4); |
| 871 | | sprintf(cPrevcName,"%.99s",cName); |
| 872 | | } |
| 873 | | |
| 874 | | if(!cLine[0] || cLine[0]==';') |
| 875 | | return; |
| 876 | | |
| 877 | | if(cName[0]!='$') |
| 878 | | { |
| 879 | | //Shift left on inline TTL NOT $TTL directive |
| 880 | | sscanf(cType,"%u",&uTTL); |
| 881 | | if(uTTL>1 && uTTL<800000) |
| 882 | | { |
| 883 | | strcpy(cType,cParam1); |
| 884 | | strcpy(cParam1,cParam2); |
| 885 | | strcpy(cParam2,cParam3); |
| 886 | | strcpy(cParam3,cParam4); |
| 887 | | } |
| 888 | | } |
| 889 | | |
| 890 | | //Shift left on IN |
| 891 | | if(!strcasecmp(cType,"IN")) |
| 892 | | { |
| 893 | | strcpy(cType,cParam1); |
| 894 | | strcpy(cParam1,cParam2); |
| 895 | | strcpy(cParam2,cParam3); |
| 896 | | strcpy(cParam3,cParam4); |
| 897 | | } |
| 898 | | |
| 899 | | //Check for recognized data and verify needed params |
| 900 | | if(!strcasecmp(cType,"A")) |
| 901 | | { |
| 902 | | uRRType=1; |
| 903 | | if(!cParam1[0] || cParam2[0]) |
| 904 | | { |
| 905 | | fprintf(stdout,"Error %s: Incorrect A format: %s\n", |
| 906 | | cZoneName,cLine); |
| 907 | | return; |
| 908 | | } |
| 909 | | if(!strcmp(IPNumber(cParam1),"0.0.0.0")) |
| 910 | | { |
| 911 | | printf("Incorrect A IP number format: %s\n",cLine); |
| 912 | | return; |
| 913 | | } |
| 914 | | } |
| 915 | | else if(!strcasecmp(cType,"CNAME")) |
| 916 | | { |
| 917 | | char cZone[254]; |
| 918 | | char cName[254]; |
| 919 | | uRRType=5; |
| 920 | | if(!cParam1[0] || cParam2[0]) |
| 921 | | { |
| 922 | | fprintf(stdout,"Error %s: Incorrect CNAME format: %s\n", |
| 923 | | cZoneName,cLine); |
| 924 | | return; |
| 925 | | } |
| 926 | | if(cParam1[strlen(cParam1)-1]!='.') |
| 927 | | { |
| 928 | | printf("Warning: Incorrect FQDN missing period: %s\n",cParam1); |
| 929 | | if(cPrevOrigin[0]) |
| 930 | | { |
| 931 | | sprintf(gcQuery,"%.255s.%.99s",cParam1,cPrevOrigin); |
| 932 | | sprintf(cParam1,"%.255s",gcQuery); |
| 933 | | printf("Fixed: CNAME RHS fixed from $ORIGIN: %s\n",cParam1); |
| 934 | | } |
| 935 | | else |
| 936 | | { |
| 937 | | sprintf(gcQuery,"%.255s.%.99s.",cParam1,cZoneName); |
| 938 | | sprintf(cParam1,"%.255s",gcQuery); |
| 939 | | printf("Fixed: CNAME RHS fixed from cZoneName: %s\n",cParam1); |
| 940 | | } |
| 941 | | } |
| 942 | | sprintf(cZone,"%s.",FQDomainName(cZoneName)); |
| 943 | | strcpy(cName,FQDomainName(cParam1)); |
| 944 | | if(strcmp(cName+strlen(cName)-strlen(cZone),cZone)) |
| 945 | | printf("Warning: CNAME RHS should probably end with zone: %s\n",cLine); |
| 946 | | } |
| 947 | | else if(!strcasecmp(cType,"NS")) |
| 948 | | { |
| 949 | | MYSQL_RES *res; |
| 950 | | char cNS[100]; |
| 951 | | |
| 952 | | uRRType=2; |
| 953 | | if(!cParam1[0] || cParam2[0]) |
| 954 | | { |
| 955 | | fprintf(stdout,"Error %s: Incorrect NS format: %s\n", |
| 956 | | cZoneName,cLine); |
| 957 | | return; |
| 958 | | } |
| 959 | | if(cParam1[strlen(cParam1)-1]!='.') |
| 960 | | { |
| 961 | | fprintf(stdout,"Warning %s: Incorrect FQDN missing period: %s\n", |
| 962 | | cZoneName,cLine); |
| 963 | | if(cPrevOrigin[0]) |
| 964 | | { |
| 965 | | sprintf(gcQuery,"%.255s.%.99s",cParam1,cPrevOrigin); |
| 966 | | sprintf(cParam1,"%.255s",gcQuery); |
| 967 | | printf("Fixed: NS RHS fixed from $ORIGIN: %s\n",cParam1); |
| 968 | | } |
| 969 | | else |
| 970 | | { |
| 971 | | sprintf(gcQuery,"%.255s.%.99s.",cParam1,cZoneName); |
| 972 | | sprintf(cParam1,"%.255s",gcQuery); |
| 973 | | printf("Fixed: NS RHS fixed from cZoneName: %s\n",cParam1); |
| 974 | | } |
| 975 | | } |
| 976 | | |
| 977 | | //Get rid of last period for check. |
| 978 | | strcpy(cNS,cParam1); |
| 979 | | cNS[strlen(cNS)-1]=0; |
| 980 | | sprintf(gcQuery,"SELECT uNS FROM tNS WHERE uNSSet=%u AND cFQDN LIKE '%s' ", |
| 981 | | uNSSet,cNS); |
| 982 | | mysql_query(&gMysql,gcQuery); |
| 983 | | mysql_query(&gMysql,gcQuery); |
| 984 | | if(mysql_errno(&gMysql)) |
| 985 | | { |
| 986 | | fprintf(stdout,"Error %s: %s\n",cZoneName,mysql_error(&gMysql)); |
| 987 | | return; |
| 988 | | } |
| 989 | | res=mysql_store_result(&gMysql); |
| 990 | | if(mysql_num_rows(res)) |
| 991 | | //Silent return |
| 992 | | return; |
| 993 | | mysql_free_result(res); |
| 994 | | } |
| 995 | | else if(!strcasecmp(cType,"MX")) |
| 996 | | { |
| 997 | | unsigned uMX=0; |
| 998 | | uRRType=3; |
| 999 | | if(!cParam1[0] || !cParam2[0] ) |
| 1000 | | { |
| 1001 | | fprintf(stdout,"Error %s: Missing MX param: %s\n", |
| 1002 | | cZoneName,cLine); |
| 1003 | | return; |
| 1004 | | } |
| 1005 | | sscanf(cParam1,"%u",&uMX); |
| 1006 | | |
| 1007 | | if(uMX>99999) |
| 1008 | | { |
| 1009 | | fprintf(stdout,"Error %s: Incorrect MX format: %s\n", |
| 1010 | | cZoneName,cLine); |
| 1011 | | return; |
| 1012 | | } |
| 1013 | | if(cParam2[strlen(cParam2)-1]!='.') |
| 1014 | | { |
| 1015 | | fprintf(stdout,"Warning %s: Incorrect FQDN missing period: %s\n", |
| 1016 | | cZoneName,cLine); |
| 1017 | | if(cPrevOrigin[0]) |
| 1018 | | { |
| 1019 | | sprintf(gcQuery,"%.255s.%.99s",cParam2,cPrevOrigin); |
| 1020 | | sprintf(cParam2,"%.255s",gcQuery); |
| 1021 | | printf("Fixed: MX RHS fixed from $ORIGIN: %s\n",cParam2); |
| 1022 | | } |
| 1023 | | else |
| 1024 | | { |
| 1025 | | sprintf(gcQuery,"%.255s.%.99s.",cParam2,cZoneName); |
| 1026 | | sprintf(cParam2,"%.255s",gcQuery); |
| 1027 | | printf("Fixed: MX RHS fixed from cZoneName: %s\n",cParam2); |
| 1028 | | } |
| 1029 | | } |
| 1030 | | } |
| 1031 | | else if(!strcasecmp(cType,"PTR")) |
| 1032 | | { |
| 1033 | | unsigned uFirstDigit=0; |
| 1034 | | |
| 1035 | | uRRType=7; |
| 1036 | | if(!cParam1[0] || cParam2[0]) |
| 1037 | | { |
| 1038 | | fprintf(stdout,"Error %s: Incorrect PTR format: %s\n", |
| 1039 | | cZoneName,cLine); |
| 1040 | | return; |
| 1041 | | } |
| 1042 | | sscanf(cName,"%u.%*s",&uFirstDigit); |
| 1043 | | if(!uFirstDigit) |
| 1044 | | { |
| 1045 | | //Check this rule again |
| 1046 | | fprintf(stdout,"Error %s: Incorrect PTR LHS should start with" |
| 1047 | | " a non zero digit: %s\n", |
| 1048 | | cZoneName,cLine); |
| 1049 | | return; |
| 1050 | | } |
| 1051 | | |
| 1052 | | } |
| 1053 | | else if(!strcasecmp(cType,"TXT")) |
| 1054 | | { |
| 1055 | | uRRType=6; |
| 1056 | | if((cp=strchr(cLine,'"'))) |
| 1057 | | sprintf(cParam1,"%.255s",cp); |
| 1058 | | //Adjust for cr/lf also |
| 1059 | | if(!cParam1[0] || cParam1[0]!='\"' || (cParam1[strlen(cParam1)-2]!='\"' && |
| 1060 | | cParam1[strlen(cParam1)-1]!='\"') ) |
| 1061 | | { |
| 1062 | | fprintf(stdout,"Error %s: Incorrect TXT format: %s\n", |
| 1063 | | cZoneName,cParam1); |
| 1064 | | return; |
| 1065 | | } |
| 1066 | | //debug only |
| 1067 | | printf("TXT: %s\n",cParam1); |
| 1068 | | } |
| 1069 | | |
| 1070 | | //Special cases that should keep a static var for next line |
| 1071 | | else if(!strcasecmp(cName,"$TTL")) |
| 1072 | | { |
| 1073 | | if(cType[0]) |
| 1074 | | { |
| 1075 | | sscanf(cType,"%u",&uPrevTTL); |
| 1076 | | //debug only |
| 1077 | | //printf("$TTL changed: %u (%s %s)\n", |
| 1078 | | // uPrevTTL,cType,cParam1); |
| 1079 | | return; |
| 1080 | | } |
| 1081 | | } |
| 1082 | | else if(!strcasecmp(cName,"$ORIGIN")) |
| 1083 | | { |
| 1084 | | if(cType[0] && strcmp(cType,".") && cType[strlen(cType)-1]=='.') |
| 1085 | | sprintf(cPrevOrigin,"%.99s",cType); |
| 1086 | | { |
| 1087 | | cPrevcName[0]=0;//Reset? |
| 1088 | | if(!strncasecmp(cPrevOrigin,cZoneName,strlen(cPrevOrigin)-1)) |
| 1089 | | cPrevOrigin[0]=0; |
| 1090 | | else |
| 1091 | | //debug only |
| 1092 | | //printf("$ORIGIN Changed: %s\n", |
| 1093 | | // cPrevOrigin); |
| 1094 | | return; |
| 1095 | | } |
| 1096 | | } |
| 1097 | | |
| 1098 | | //Unrecognized lines |
| 1099 | | //Current missing features -we know about and need: hinfo ignored |
| 1100 | | else if(1) |
| 1101 | | { |
| 1102 | | fprintf(stdout,"Error %s: RR Not recognized: %s %s\n",cZoneName,cLine,cType); |
| 1103 | | return; |
| 1104 | | } |
| 1105 | | |
| 1106 | | if(!uTTL && uPrevTTL) uTTL=uPrevTTL; |
| 1107 | | if(cPrevOrigin[0]) |
| 1108 | | sprintf(cNamePlus,"%.99s.%.99s",cName,cPrevOrigin); |
| 1109 | | else |
| 1110 | | sprintf(cNamePlus,"%.99s",cName); |
| 1111 | | |
| 1112 | | if(!uRRType) return; //NOP if not valid RR is found |
| 1113 | | if(uRRType==6)//TXT special case |
| 1114 | | sprintf(gcQuery,"INSERT INTO tResource SET uZone=%u,cName='%s',uTTL=%u," |
| 1115 | | "uRRType=%u,cParam1='%s',cComment='%s',uOwner=%u,uCreatedBy=%u," |
| 1116 | | "uCreatedDate=UNIX_TIMESTAMP(NOW())", |
| 1117 | | uZone |
| 1118 | | ,FQDomainName(cNamePlus) |
| 1119 | | ,uTTL |
| 1120 | | ,uRRType |
| 1121 | | ,cParam1 |
| 1122 | | ,cComment |
| 1123 | | ,uCustId |
| 1124 | | ,uCreatedBy); |
| 1125 | | else |
| 1126 | | sprintf(gcQuery,"INSERT INTO tResource SET uZone=%u,cName='%s',uTTL=%u," |
| 1127 | | "uRRType=%u,cParam1='%s',cParam2='%s',cComment='%s',uOwner=%u," |
| 1128 | | "uCreatedBy=%u,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
| 1129 | | uZone |
| 1130 | | ,FQDomainName(cNamePlus) |
| 1131 | | ,uTTL |
| 1132 | | ,uRRType |
| 1133 | | ,FQDomainName(cParam1) |
| 1134 | | ,FQDomainName(cParam2) |
| 1135 | | ,cComment |
| 1136 | | ,uCustId |
| 1137 | | ,uCreatedBy); |
| 1138 | | mysql_query(&gMysql,gcQuery); |
| 1139 | | //printf("%s\n",gcQuery); |
| 1140 | | if(mysql_errno(&gMysql)) |
| 1141 | | fprintf(stdout,"Error %s: %s\n",cZoneName,mysql_error(&gMysql)); |
| 1142 | | |
| 1143 | | return; |
| 1144 | | |
| 1145 | | }//void ProcessSORRLine() |
| 1146 | | |
| 1147 | | |
| 1148 | | void DropImportedZones(void) |
| 1149 | | { |
| 1150 | | char cZoneImportTable[256]="tZoneImport"; |
| 1151 | | char cResourceImportTable[256]="tResourceImport"; |
| 1152 | | |
| 1153 | | GetConfiguration("cZoneImportTable",cZoneImportTable,0); |
| 1154 | | GetConfiguration("cResourceImportTable",cResourceImportTable,0); |
| 1155 | | |
| 1156 | | printf("DropImportedZones() Start\n"); |
| 1157 | | |
| 1158 | | sprintf(gcQuery,"DELETE FROM %s WHERE cHostmaster LIKE '%% IMPORTED'",cZoneImportTable); |
| 1159 | | mysql_query(&gMysql,gcQuery); |
| 1160 | | if(mysql_errno(&gMysql)) |
| 1161 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 1162 | | |
| 1163 | | sprintf(gcQuery,"DELETE FROM %s WHERE cComment LIKE '%% IMPORTED'",cResourceImportTable); |
| 1164 | | mysql_query(&gMysql,gcQuery); |
| 1165 | | if(mysql_errno(&gMysql)) |
| 1166 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 1167 | | |
| 1168 | | printf("DropImportedZones() Done\n"); |
| 1169 | | |
| 1170 | | }//DropImportedZones() |
| 1171 | | |
| 1172 | | |
| 1173 | | structSOA *ProcessSOA(FILE *fp) |
| 1174 | | { |
| 1175 | | static structSOA importSOA; |
| 1176 | | register unsigned uIn=0; |
| 1177 | | register unsigned uParamCount=0; |
| 1178 | | char *cp; |
| 1179 | | |
| 1180 | | importSOA.uSerial=0; |
| 1181 | | importSOA.uTTL=86400; |
| 1182 | | importSOA.uRefresh=0; |
| 1183 | | importSOA.uRetry=0; |
| 1184 | | importSOA.uExp=0; |
| 1185 | | importSOA.uNegTTL=0; |
| 1186 | | importSOA.cHostmaster[0]=0; |
| 1187 | | importSOA.cNameServer[0]=0; |
| 1188 | | |
| 1189 | | while(fgets(gcQuery,1024,fp)!=NULL) |
| 1190 | | { |
| 1191 | | if((cp=strchr(gcQuery,';'))) |
| 1192 | | { |
| 1193 | | *cp='\n'; |
| 1194 | | *(cp+1)=0; |
| 1195 | | } |
| 1196 | | //debug only |
| 1197 | | //printf("%s",gcQuery); |
| 1198 | | |
| 1199 | | if(strstr(gcQuery,"(")) |
| 1200 | | { |
| 1201 | | if((cp=strstr(gcQuery,"SOA")) || (cp=strstr(gcQuery,"soa"))) |
| 1202 | | sscanf(cp+4,"%s %s",importSOA.cNameServer, |
| 1203 | | importSOA.cHostmaster); |
| 1204 | | uIn=1; |
| 1205 | | continue; |
| 1206 | | } |
| 1207 | | else if(strstr(gcQuery,")")) |
| 1208 | | { |
| 1209 | | if(uIn && uParamCount==4) |
| 1210 | | sscanf(gcQuery,"%u",&importSOA.uNegTTL); |
| 1211 | | return(&importSOA); |
| 1212 | | } |
| 1213 | | else if(strstr(gcQuery,"$TTL")) |
| 1214 | | sscanf(gcQuery,"$TTL %u",&importSOA.uTTL); |
| 1215 | | else if((cp=strstr(gcQuery,"SOA")) || (cp=strstr(gcQuery,"soa"))) |
| 1216 | | sscanf(cp+4,"%s %s",importSOA.cNameServer, |
| 1217 | | importSOA.cHostmaster); |
| 1218 | | |
| 1219 | | if(uIn) |
| 1220 | | { |
| 1221 | | if(!uParamCount) |
| 1222 | | sscanf(gcQuery,"%u",&importSOA.uSerial); |
| 1223 | | else if(uParamCount==1) |
| 1224 | | sscanf(gcQuery,"%u",&importSOA.uRefresh); |
| 1225 | | else if(uParamCount==2) |
| 1226 | | sscanf(gcQuery,"%u",&importSOA.uRetry); |
| 1227 | | else if(uParamCount==3) |
| 1228 | | sscanf(gcQuery,"%u",&importSOA.uExp); |
| 1229 | | else if(uParamCount==4) |
| 1230 | | sscanf(gcQuery,"%u",&importSOA.uNegTTL); |
| 1231 | | uParamCount++; |
| 1232 | | } |
| 1233 | | |
| 1234 | | if(uParamCount>5) |
| 1235 | | return(&importSOA); |
| 1236 | | } |
| 1237 | | |
| 1238 | | return(&importSOA); |
| 1239 | | |
| 1240 | | }//structSOA *ProcessSOA(FILE *fp) |
| 1241 | | |
| 1242 | | |
| 1243 | | // |
| 1244 | | // Major file section |
| 1245 | | // Non import/migration mainfunc.h CLI functions and aux functions |
| 1246 | | // |
| 2710 | | //Import tClient records that will be owners of tClient user records |
| 2711 | | void ImportCompanies(void) |
| 2712 | | { |
| 2713 | | FILE *fp; |
| 2714 | | char cQuery[256]; |
| 2715 | | unsigned uClient; |
| 2716 | | char *cp; |
| 2717 | | |
| 2718 | | printf("ImportCompanies() Start\n"); |
| 2719 | | |
| 2720 | | if(!(fp=fopen("/usr/local/idns/csv/companycode.csv","r"))) |
| 2721 | | { |
| 2722 | | fprintf(stdout,"Error: Could not open: /usr/local/idns/csv/companycode.csv\n"); |
| 2723 | | return; |
| 2724 | | } |
| 2725 | | |
| 2726 | | //Start processing. Unix files only! |
| 2727 | | while(fgets(gcQuery,254,fp)!=NULL) |
| 2728 | | { |
| 2729 | | uClient=0;//tClient PK |
| 2730 | | |
| 2731 | | //skip empty lines and comments |
| 2732 | | if(gcQuery[0]=='\n' || gcQuery[0]=='#') |
| 2733 | | continue; |
| 2734 | | gcQuery[strlen(gcQuery)-1]=0; |
| 2735 | | |
| 2736 | | |
| 2737 | | //Quick and dirty parsing input must be in good shape! |
| 2738 | | //Parse uClient |
| 2739 | | if((cp=strchr(gcQuery,','))) |
| 2740 | | { |
| 2741 | | *cp=0; |
| 2742 | | sscanf(gcQuery,"%u",&uClient); |
| 2743 | | |
| 2744 | | sprintf(cQuery,"INSERT INTO tClient SET uClient=%u,cLabel='%.99s'," |
| 2745 | | "cCode='Organization',cInfo='ImportCompanies() IMPORTED'," |
| 2746 | | "uOwner=1,uCreatedBy=1," |
| 2747 | | "uCreatedDate=UNIX_TIMESTAMP(NOW())",uClient,cp+1); |
| 2748 | | //debug only |
| 2749 | | //printf("%s\n",cQuery); |
| 2750 | | mysql_query(&gMysql,cQuery); |
| 2751 | | if(mysql_errno(&gMysql)) |
| 2752 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 2753 | | } |
| 2754 | | else |
| 2755 | | printf("Skipping invalid line: %s\n",gcQuery); |
| 2756 | | } |
| 2757 | | |
| 2758 | | if(fp) fclose(fp); |
| 2759 | | |
| 2760 | | printf("ImportCompanies() End\n"); |
| 2761 | | |
| 2762 | | }//void ImportCompanies(void) |
| 2763 | | |
| 2764 | | |
| 2765 | | void DropCompanies(void) |
| 2766 | | { |
| 2767 | | printf("DropCompanies() Start\n"); |
| 2768 | | |
| 2769 | | sprintf(gcQuery,"DELETE FROM tClient WHERE cInfo LIKE '%%ImportCompanies() IMPORTED'"); |
| 2770 | | mysql_query(&gMysql,gcQuery); |
| 2771 | | if(mysql_errno(&gMysql)) |
| 2772 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 2773 | | |
| 2774 | | printf("DropCompanies() End\n"); |
| 2775 | | |
| 2776 | | }//void DropCompanies(void) |
| 2777 | | |
| 2778 | | |
| 2779 | | //Import tClient records that will be owned by an existing tClient company record |
| 2780 | | void ImportUsers(void) |
| 2781 | | { |
| 2782 | | MYSQL_RES *res; |
| 2783 | | MYSQL_ROW field; |
| 2784 | | FILE *fp; |
| 2785 | | char cQuery[256]; |
| 2786 | | char cCompany[256]; |
| 2787 | | char cFirst[100]; |
| 2788 | | char cLast[100]; |
| 2789 | | char cEmail[100]; |
| 2790 | | char cLogin[100]; |
| 2791 | | char cPasswd[100]; |
| 2792 | | char *cp,*cp2; |
| 2793 | | unsigned uCompany,uUser; |
| 2794 | | |
| 2795 | | printf("ImportUsers() Start\n"); |
| 2796 | | |
| 2797 | | if(!(fp=fopen("/usr/local/idns/csv/companyuser.csv","r"))) |
| 2798 | | { |
| 2799 | | fprintf(stdout,"Error: Could not open: /usr/local/idns/csv/companyuser.csv\n"); |
| 2800 | | return; |
| 2801 | | } |
| 2802 | | |
| 2803 | | //Start processing. Unix files only! |
| 2804 | | while(fgets(gcQuery,254,fp)!=NULL) |
| 2805 | | { |
| 2806 | | uCompany=0; |
| 2807 | | uUser=0; |
| 2808 | | cFirst[0]=0; |
| 2809 | | cLast[0]=0; |
| 2810 | | cEmail[0]=0; |
| 2811 | | cLogin[0]=0; |
| 2812 | | cPasswd[0]=0; |
| 2813 | | cCompany[0]=0; |
| 2814 | | |
| 2815 | | //skip empty lines and comments |
| 2816 | | if(gcQuery[0]=='\n' || gcQuery[0]=='#') |
| 2817 | | continue; |
| 2818 | | gcQuery[strlen(gcQuery)-1]=0; |
| 2819 | | |
| 2820 | | //debug only |
| 2821 | | //printf("%s\n",gcQuery); |
| 2822 | | |
| 2823 | | //Quick and dirty parsing input must be in good shape! |
| 2824 | | //Parse uCompany |
| 2825 | | if((cp=strchr(gcQuery,','))) |
| 2826 | | { |
| 2827 | | *cp=0; |
| 2828 | | sscanf(gcQuery,"%u",&uCompany); |
| 2829 | | *cp=','; |
| 2830 | | } |
| 2831 | | //debug only |
| 2832 | | //printf("uCompany=%u\n",uCompany); |
| 2833 | | |
| 2834 | | //Parse uUser |
| 2835 | | if((cp2=strchr(cp+1,','))) |
| 2836 | | { |
| 2837 | | *cp2=0; |
| 2838 | | sscanf(cp+1,"%u",&uUser); |
| 2839 | | *cp2=','; |
| 2840 | | } |
| 2841 | | //debug only |
| 2842 | | //printf("uUser=%u\n",uUser); |
| 2843 | | |
| 2844 | | //Parse cFirst |
| 2845 | | if((cp=strchr(cp2+1,','))) |
| 2846 | | { |
| 2847 | | *cp=0; |
| 2848 | | sprintf(cFirst,"%.99s",cp2+1); |
| 2849 | | *cp=','; |
| 2850 | | } |
| 2851 | | //debug only |
| 2852 | | //printf("cFirst=%s\n",cFirst); |
| 2853 | | |
| 2854 | | //Parse cLast |
| 2855 | | if((cp2=strchr(cp+1,','))) |
| 2856 | | { |
| 2857 | | *cp2=0; |
| 2858 | | sprintf(cLast,"%.99s",cp+1); |
| 2859 | | *cp2=','; |
| 2860 | | } |
| 2861 | | //debug only |
| 2862 | | //printf("cLast=%s\n",cLast); |
| 2863 | | |
| 2864 | | //Parse cEmail |
| 2865 | | if((cp=strchr(cp2+1,','))) |
| 2866 | | { |
| 2867 | | *cp=0; |
| 2868 | | sprintf(cEmail,"%.99s",cp2+1); |
| 2869 | | *cp=','; |
| 2870 | | } |
| 2871 | | //debug only |
| 2872 | | //printf("cEmail=%s\n",cEmail); |
| 2873 | | |
| 2874 | | //Parse cLogin |
| 2875 | | if((cp2=strchr(cp+1,','))) |
| 2876 | | { |
| 2877 | | *cp2=0; |
| 2878 | | sprintf(cLogin,"%.99s",cp+1); |
| 2879 | | *cp2=','; |
| 2880 | | } |
| 2881 | | //debug only |
| 2882 | | //printf("cLogin=%s\n",cLogin); |
| 2883 | | |
| 2884 | | //Parse cPasswd |
| 2885 | | sprintf(cPasswd,"%.99s",cp2+1); |
| 2886 | | //debug only |
| 2887 | | //printf("cPasswd=%s\n",cPasswd); |
| 2888 | | |
| 2889 | | |
| 2890 | | //Sanity checks |
| 2891 | | if(!uCompany || !uUser || !cFirst[0] || !cLast[0] || !cLogin[0]) |
| 2892 | | printf("Warning %s may be incorrectly formatted\n", |
| 2893 | | gcQuery); |
| 2894 | | |
| 2895 | | sprintf(cQuery,"SELECT cLabel FROM tClient WHERE uOwner=1 AND uClient=%u AND" |
| 2896 | | " cInfo LIKE '%%ImportCompanies() IMPORTED'",uCompany); |
| 2897 | | mysql_query(&gMysql,cQuery); |
| 2898 | | if(mysql_errno(&gMysql)) |
| 2899 | | { |
| 2900 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 2901 | | exit(2); |
| 2902 | | } |
| 2903 | | res=mysql_store_result(&gMysql); |
| 2904 | | if((field=mysql_fetch_row(res))) |
| 2905 | | sprintf(cCompany,"%.255s",field[0]); |
| 2906 | | mysql_free_result(res); |
| 2907 | | |
| 2908 | | if(cCompany[0]) |
| 2909 | | { |
| 2910 | | //Company same as user, fix company |
| 2911 | | sprintf(cQuery,"%.99s %.99s",cFirst,cLast); |
| 2912 | | if(!strcmp(cCompany,cQuery)) |
| 2913 | | { |
| 2914 | | sprintf(gcQuery,"UPDATE tClient SET cLabel='%.250s Co.',uModBy=1," |
| 2915 | | "uModDate=UNIX_TIMESTAMP(NOW()) WHERE uClient=%u",cCompany,uCompany); |
| 2916 | | mysql_query(&gMysql,gcQuery); |
| 2917 | | if(mysql_errno(&gMysql)) |
| 2918 | | { |
| 2919 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 2920 | | exit(2); |
| 2921 | | } |
| 2922 | | } |
| 2923 | | sprintf(gcQuery,"INSERT INTO tClient SET cLabel='%.99s %.99s',cInfo='ImportUsers() IMPORTED'," |
| 2924 | | "uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW()),cEmail='%s'," |
| 2925 | | "uClient=%u",TextAreaSave(cFirst),TextAreaSave(cLast),uCompany,cEmail,uUser); |
| 2926 | | mysql_query(&gMysql,gcQuery); |
| 2927 | | if(mysql_errno(&gMysql)) |
| 2928 | | { |
| 2929 | | fprintf(stdout,"Warning: %s\n",mysql_error(&gMysql)); |
| 2930 | | continue; |
| 2931 | | } |
| 2932 | | |
| 2933 | | //tAuthorize issues |
| 2934 | | if(!cLogin[0]) |
| 2935 | | sprintf(cLogin,"%.4s%.4s",cFirst,cLast); |
| 2936 | | if(!cPasswd[0]) |
| 2937 | | sprintf(cPasswd,"%.3sH56%.3s",cFirst,cLast); |
| 2938 | | EncryptPasswdWithSalt(cPasswd,".."); |
| 2939 | | sprintf(gcQuery,"INSERT INTO tAuthorize SET cLabel='%.32s',uPerm=6,uCertClient=%u,cPasswd='%s'," |
| 2940 | | "uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW()),cIPMask='0.0.0.0/0'", |
| 2941 | | cLogin,uUser,cPasswd,uCompany); |
| 2942 | | mysql_query(&gMysql,gcQuery); |
| 2943 | | if(mysql_errno(&gMysql)) |
| 2944 | | fprintf(stdout,"Warning: %s\n",mysql_error(&gMysql)); |
| 2945 | | } |
| 2946 | | else |
| 2947 | | { |
| 2948 | | printf("Error: %s company not found\n",cCompany); |
| 2949 | | } |
| 2950 | | |
| 2951 | | } |
| 2952 | | |
| 2953 | | if(fp) fclose(fp); |
| 2954 | | |
| 2955 | | |
| 2956 | | printf("ImportUsers() End\n"); |
| 2957 | | |
| 2958 | | }//void ImportUsers(void) |
| 2959 | | |
| 2960 | | |
| 2961 | | void DropUsers(void) |
| 2962 | | { |
| 2963 | | MYSQL_RES *res; |
| 2964 | | MYSQL_ROW field; |
| 2965 | | char cQuery[256]; |
| 2966 | | unsigned uClient; |
| 2967 | | |
| 2968 | | printf("DropUsers() Start\n"); |
| 2969 | | |
| 2970 | | sprintf(cQuery,"SELECT uClient FROM tClient WHERE uOwner!=1 AND cInfo LIKE '%%ImportUsers() IMPORTED'"); |
| 2971 | | mysql_query(&gMysql,cQuery); |
| 2972 | | if(mysql_errno(&gMysql)) |
| 2973 | | { |
| 2974 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 2975 | | exit(2); |
| 2976 | | } |
| 2977 | | res=mysql_store_result(&gMysql); |
| 2978 | | while((field=mysql_fetch_row(res))) |
| 2979 | | { |
| 2980 | | uClient=0; |
| 2981 | | sscanf(field[0],"%u",&uClient); |
| 2982 | | |
| 2983 | | sprintf(gcQuery,"DELETE FROM tAuthorize WHERE uCertClient=%u",uClient); |
| 2984 | | mysql_query(&gMysql,gcQuery); |
| 2985 | | if(mysql_errno(&gMysql)) |
| 2986 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 2987 | | } |
| 2988 | | mysql_free_result(res); |
| 2989 | | |
| 2990 | | sprintf(cQuery,"DELETE FROM tClient WHERE uOwner!=1 AND cInfo LIKE '%%ImportUsers() IMPORTED'"); |
| 2991 | | mysql_query(&gMysql,cQuery); |
| 2992 | | if(mysql_errno(&gMysql)) |
| 2993 | | { |
| 2994 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 2995 | | exit(2); |
| 2996 | | } |
| 2997 | | |
| 2998 | | printf("DropUsers() End\n"); |
| 2999 | | |
| 3000 | | }//void DropUsers(void) |
| 3001 | | |
| 3002 | | |
| 3003 | | void ImportBlocks(void) |
| 3004 | | { |
| 3005 | | MYSQL_RES *res; |
| 3006 | | MYSQL_ROW field; |
| 3007 | | FILE *fp; |
| 3008 | | char cQuery[256]; |
| 3009 | | char cCompany[256]; |
| 3010 | | char cBlock[100]; |
| 3011 | | char *cp; |
| 3012 | | unsigned uClient; |
| 3013 | | |
| 3014 | | printf("ImportBlocks() Start\n"); |
| 3015 | | |
| 3016 | | if(!(fp=fopen("/usr/local/idns/csv/companyblock.csv","r"))) |
| 3017 | | { |
| 3018 | | fprintf(stdout,"Error: Could not open: /usr/local/idns/csv/companyblock.csv\n"); |
| 3019 | | return; |
| 3020 | | } |
| 3021 | | |
| 3022 | | //Start processing. Unix files only! |
| 3023 | | while(fgets(gcQuery,254,fp)!=NULL) |
| 3024 | | { |
| 3025 | | cBlock[0]=0; |
| 3026 | | cCompany[0]=0; |
| 3027 | | uClient=0; |
| 3028 | | |
| 3029 | | //skip empty lines and comments |
| 3030 | | if(gcQuery[0]=='\n' || gcQuery[0]=='#') |
| 3031 | | continue; |
| 3032 | | gcQuery[strlen(gcQuery)-1]=0; |
| 3033 | | |
| 3034 | | //debug only |
| 3035 | | //printf("%s\n",gcQuery); |
| 3036 | | |
| 3037 | | //Quick and dirty parsing input must be in good shape! |
| 3038 | | //Parse Company uClient |
| 3039 | | if((cp=strchr(gcQuery,','))) |
| 3040 | | { |
| 3041 | | *cp=0; |
| 3042 | | sscanf(gcQuery,"%u",&uClient); |
| 3043 | | *cp=','; |
| 3044 | | } |
| 3045 | | //debug only |
| 3046 | | //printf("uClient=%u\n",uClient); |
| 3047 | | |
| 3048 | | //Parse cBlock |
| 3049 | | sprintf(cBlock,"%.99s",cp+1); |
| 3050 | | //printf("cBlock=%s\n",cBlock); |
| 3051 | | |
| 3052 | | //Sanity checks |
| 3053 | | if(!uClient || !cBlock[0] ) |
| 3054 | | printf("Warning %s may be incorrectly formatted\n", |
| 3055 | | gcQuery); |
| 3056 | | |
| 3057 | | sprintf(cQuery,"SELECT cLabel FROM tClient WHERE uOwner=1 AND cInfo LIKE '%%ImportCompanies()" |
| 3058 | | " IMPORTED' AND uClient=%u",uClient); |
| 3059 | | mysql_query(&gMysql,cQuery); |
| 3060 | | if(mysql_errno(&gMysql)) |
| 3061 | | { |
| 3062 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3063 | | exit(2); |
| 3064 | | } |
| 3065 | | res=mysql_store_result(&gMysql); |
| 3066 | | if((field=mysql_fetch_row(res))) |
| 3067 | | sprintf(cCompany,"%.255s",field[0]); |
| 3068 | | mysql_free_result(res); |
| 3069 | | |
| 3070 | | //debug only |
| 3071 | | //printf("cCompany=%s\n",cCompany); |
| 3072 | | if(cCompany[0]) |
| 3073 | | { |
| 3074 | | sprintf(cQuery,"INSERT INTO tBlock SET cLabel='%.32s',cComment='ImportBlocks() IMPORTED'," |
| 3075 | | "uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())",cBlock,uClient); |
| 3076 | | mysql_query(&gMysql,cQuery); |
| 3077 | | if(mysql_errno(&gMysql)) |
| 3078 | | { |
| 3079 | | fprintf(stdout,"Warning: %s\n",mysql_error(&gMysql)); |
| 3080 | | } |
| 3081 | | } |
| 3082 | | else |
| 3083 | | { |
| 3084 | | printf("Error: %s company not found\n",cCompany); |
| 3085 | | } |
| 3086 | | |
| 3087 | | } |
| 3088 | | |
| 3089 | | if(fp) fclose(fp); |
| 3090 | | printf("ImportBlocks() End\n"); |
| 3091 | | |
| 3092 | | }//void ImportBlocks(void) |
| 3093 | | |
| 3094 | | |
| 3095 | | void DropBlocks(void) |
| 3096 | | { |
| 3097 | | printf("DropBlocks() Start\n"); |
| 3098 | | |
| 3099 | | sprintf(gcQuery,"DELETE FROM tBlock WHERE cComment LIKE '%%ImportBlocks() IMPORTED'"); |
| 3100 | | mysql_query(&gMysql,gcQuery); |
| 3101 | | if(mysql_errno(&gMysql)) |
| 3102 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3103 | | |
| 3104 | | printf("DropBlocks() End\n"); |
| 3105 | | |
| 3106 | | }//void DropBlocks(void) |
| 3107 | | |
| 3108 | | |
| 3109 | | void AssociateCompaniesZones(void) |
| 3110 | | { |
| 3111 | | MYSQL_RES *res; |
| 3112 | | MYSQL_ROW field; |
| 3113 | | FILE *fp; |
| 3114 | | char cQuery[256]; |
| 3115 | | char cCompany[256]; |
| 3116 | | char cZone[100]; |
| 3117 | | char cIP[20]; |
| 3118 | | char *cp; |
| 3119 | | unsigned uClient; |
| 3120 | | unsigned uA; |
| 3121 | | unsigned uB; |
| 3122 | | unsigned uC; |
| 3123 | | unsigned uD; |
| 3124 | | unsigned uResource; |
| 3125 | | |
| 3126 | | printf("AssociateCompaniesZones() Start\n"); |
| 3127 | | |
| 3128 | | if(!(fp=fopen("/usr/local/idns/csv/companyzone.csv","r"))) |
| 3129 | | { |
| 3130 | | fprintf(stdout,"Error: Could not open: /usr/local/idns/csv/companyzone.csv\n"); |
| 3131 | | return; |
| 3132 | | } |
| 3133 | | |
| 3134 | | //Start processing. Unix files only! |
| 3135 | | while(fgets(gcQuery,254,fp)!=NULL) |
| 3136 | | { |
| 3137 | | cCompany[0]=0; |
| 3138 | | cZone[0]=0; |
| 3139 | | uClient=0; |
| 3140 | | |
| 3141 | | //skip empty lines and comments |
| 3142 | | if(gcQuery[0]=='\n' || gcQuery[0]=='#') |
| 3143 | | continue; |
| 3144 | | gcQuery[strlen(gcQuery)-1]=0; |
| 3145 | | |
| 3146 | | //debug only |
| 3147 | | //printf("%s\n",gcQuery); |
| 3148 | | |
| 3149 | | //Quick and dirty parsing input must be in good shape! |
| 3150 | | //Parse cCompany |
| 3151 | | if((cp=strchr(gcQuery,','))) |
| 3152 | | { |
| 3153 | | *cp=0; |
| 3154 | | sscanf(gcQuery,"%u",&uClient); |
| 3155 | | *cp=','; |
| 3156 | | } |
| 3157 | | |
| 3158 | | sprintf(cZone,"%.99s",cp+1); |
| 3159 | | //printf("cZone=%s\n",cZone); |
| 3160 | | |
| 3161 | | //Sanity checks |
| 3162 | | if(!uClient || !cZone[0] ) |
| 3163 | | printf("Warning %s may be incorrectly formatted\n", |
| 3164 | | gcQuery); |
| 3165 | | |
| 3166 | | sprintf(cQuery,"SELECT uClient FROM tClient WHERE uOwner=1 AND cInfo" |
| 3167 | | " LIKE '%%ImportCompanies() IMPORTED' AND uClient=%u",uClient); |
| 3168 | | mysql_query(&gMysql,cQuery); |
| 3169 | | if(mysql_errno(&gMysql)) |
| 3170 | | { |
| 3171 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3172 | | exit(2); |
| 3173 | | } |
| 3174 | | res=mysql_store_result(&gMysql); |
| 3175 | | if((field=mysql_fetch_row(res))) |
| 3176 | | sprintf(cCompany,"%.255s",field[0]); |
| 3177 | | mysql_free_result(res); |
| 3178 | | |
| 3179 | | if(cCompany[0]) |
| 3180 | | { |
| 3181 | | sprintf(cQuery,"UPDATE tZone SET uOwner=%u,uModBy=1,uModDate=UNIX_TIMESTAMP(NOW())" |
| 3182 | | " WHERE cZone='%s'",uClient,cZone); |
| 3183 | | mysql_query(&gMysql,cQuery); |
| 3184 | | if(mysql_errno(&gMysql)) |
| 3185 | | { |
| 3186 | | fprintf(stdout,"Warning: %s\n",mysql_error(&gMysql)); |
| 3187 | | } |
| 3188 | | if(mysql_affected_rows(&gMysql)<1) |
| 3189 | | { |
| 3190 | | fprintf(stdout,"Warning: %s not updated.\n",cZone); |
| 3191 | | continue;//Skip RRs |
| 3192 | | } |
| 3193 | | sprintf(cQuery,"UPDATE tResource,tZone SET tResource.uOwner=%u,tResource.uModBy=1," |
| 3194 | | "tResource.uModDate=UNIX_TIMESTAMP(NOW()) WHERE" |
| 3195 | | " tZone.uZone=tResource.uZone AND tZone.cZone='%s'",uClient,cZone); |
| 3196 | | mysql_query(&gMysql,cQuery); |
| 3197 | | if(mysql_errno(&gMysql)) |
| 3198 | | { |
| 3199 | | fprintf(stdout,"Warning: %s\n",mysql_error(&gMysql)); |
| 3200 | | } |
| 3201 | | } |
| 3202 | | else |
| 3203 | | { |
| 3204 | | printf("Error: %s company not found\n",cCompany); |
| 3205 | | } |
| 3206 | | |
| 3207 | | } |
| 3208 | | if(fp) fclose(fp); |
| 3209 | | |
| 3210 | | //In this section we use block ownership to change PTR RRs ownership |
| 3211 | | //Overlapping blocks should not be allowed to exist in tBlock |
| 3212 | | //so the first block that contains the IP is the block we use. |
| 3213 | | //This simplistic algo only works for class C in-addr.arpa zones |
| 3214 | | sprintf(cQuery,"SELECT tResource.uResource,tResource.cName,tZone.cZone FROM tResource,tZone WHERE" |
| 3215 | | " tResource.uZone=tZone.uZone AND tResource.uRRType=7"); |
| 3216 | | mysql_query(&gMysql,cQuery); |
| 3217 | | if(mysql_errno(&gMysql)) |
| 3218 | | { |
| 3219 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3220 | | exit(2); |
| 3221 | | } |
| 3222 | | res=mysql_store_result(&gMysql); |
| 3223 | | while((field=mysql_fetch_row(res))) |
| 3224 | | { |
| 3225 | | uA=255; |
| 3226 | | uB=255; |
| 3227 | | uC=255; |
| 3228 | | uD=255; |
| 3229 | | |
| 3230 | | sscanf(field[0],"%u",&uResource); |
| 3231 | | |
| 3232 | | //Try to reconstruct the IP |
| 3233 | | sscanf(field[1],"%u",&uD); |
| 3234 | | sscanf(field[2],"%u.%u.%u.in-addr.arpa",&uC,&uB,&uA); |
| 3235 | | sprintf(cIP,"%u.%u.%u.%u\n",uA,uB,uC,uD); |
| 3236 | | if(uA>0 && uA<255 && uB<255 && uC<255 && uD<255) |
| 3237 | | { |
| 3238 | | MYSQL_RES *res2; |
| 3239 | | MYSQL_ROW field2; |
| 3240 | | |
| 3241 | | //This next step is Big-O n. i.e. very slow... |
| 3242 | | sprintf(cQuery,"SELECT cLabel,uOwner FROM tBlock"); |
| 3243 | | mysql_query(&gMysql,cQuery); |
| 3244 | | if(mysql_errno(&gMysql)) |
| 3245 | | { |
| 3246 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3247 | | exit(2); |
| 3248 | | } |
| 3249 | | res2=mysql_store_result(&gMysql); |
| 3250 | | while((field2=mysql_fetch_row(res2))) |
| 3251 | | { |
| 3252 | | if(uIpv4InCIDR4(cIP,field2[0])==1) |
| 3253 | | { |
| 3254 | | sscanf(field2[1],"%u",&uClient); |
| 3255 | | sprintf(cQuery,"UPDATE tResource SET uOwner=%u WHERE uResource=%u", |
| 3256 | | uClient,uResource); |
| 3257 | | mysql_query(&gMysql,cQuery); |
| 3258 | | if(mysql_errno(&gMysql)) |
| 3259 | | { |
| 3260 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3261 | | exit(2); |
| 3262 | | } |
| 3263 | | //debug only |
| 3264 | | //printf("%s is in %s\n",cIP,field2[0]); |
| 3265 | | break; |
| 3266 | | } |
| 3267 | | } |
| 3268 | | mysql_free_result(res2); |
| 3269 | | } |
| 3270 | | else |
| 3271 | | { |
| 3272 | | printf("Warning: %s seems incorrect. Skiping.\n",cIP); |
| 3273 | | } |
| 3274 | | } |
| 3275 | | mysql_free_result(res); |
| 3276 | | |
| 3277 | | printf("AssociateCompaniesZones() End\n"); |
| 3278 | | |
| 3279 | | }//void AssociateCompaniesZones(void) |
| 3280 | | |
| 3332 | | |
| 3333 | | |
| 3334 | | //Import tRegistrar records |
| 3335 | | void ImportRegistrars(void) |
| 3336 | | { |
| 3337 | | FILE *fp; |
| 3338 | | char cQuery[256]; |
| 3339 | | |
| 3340 | | printf("ImportRegistrars() Start\n"); |
| 3341 | | |
| 3342 | | if(!(fp=fopen("/usr/local/idns/csv/registrar.txt","r"))) |
| 3343 | | { |
| 3344 | | fprintf(stdout,"Error: Could not open: /usr/local/idns/csv/registrar.txt\n"); |
| 3345 | | return; |
| 3346 | | } |
| 3347 | | |
| 3348 | | //Start processing. Unix files only! |
| 3349 | | while(fgets(gcQuery,254,fp)!=NULL) |
| 3350 | | { |
| 3351 | | //skip empty lines NOT comments #1 registery in the world :) |
| 3352 | | if(gcQuery[0]=='\n') |
| 3353 | | continue; |
| 3354 | | gcQuery[strlen(gcQuery)-1]=0; |
| 3355 | | |
| 3356 | | sprintf(cQuery,"INSERT INTO tRegistrar SET cLabel='%.99s',uOwner=1,uCreatedBy=1," |
| 3357 | | "uCreatedDate=UNIX_TIMESTAMP(NOW())",gcQuery); |
| 3358 | | //debug only |
| 3359 | | //printf("%s\n",cQuery); |
| 3360 | | mysql_query(&gMysql,cQuery); |
| 3361 | | if(mysql_errno(&gMysql)) |
| 3362 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3363 | | } |
| 3364 | | |
| 3365 | | if(fp) fclose(fp); |
| 3366 | | |
| 3367 | | printf("ImportRegistrars() End\n"); |
| 3368 | | |
| 3369 | | }//void ImportRegistrars(void) |
| 3370 | | |
| 3371 | | |
| 3372 | | void DropRegistrars(void) |
| 3373 | | { |
| 3374 | | printf("DropRegistrars() Start\n"); |
| 3375 | | |
| 3376 | | sprintf(gcQuery,"TRUNCATE tRegistrar"); |
| 3377 | | mysql_query(&gMysql,gcQuery); |
| 3378 | | if(mysql_errno(&gMysql)) |
| 3379 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3380 | | |
| 3381 | | printf("DropRegistrars() End\n"); |
| 3382 | | |
| 3383 | | }//void DropRegistrars(void) |
| 3384 | | |
| 3385 | | |
| 3386 | | void AssociateRegistrarsZones(void) |
| 3387 | | { |
| 3388 | | MYSQL_RES *res; |
| 3389 | | MYSQL_ROW field; |
| 3390 | | FILE *fp; |
| 3391 | | char cQuery[256]; |
| 3392 | | char cRegistrar[256]; |
| 3393 | | unsigned uRegistrar; |
| 3394 | | char cZone[100]; |
| 3395 | | char *cp; |
| 3396 | | |
| 3397 | | printf("AssociateRegistrarsZones() Start\n"); |
| 3398 | | |
| 3399 | | if(!(fp=fopen("/usr/local/idns/csv/registrarzone.csv","r"))) |
| 3400 | | { |
| 3401 | | fprintf(stdout,"Error: Could not open: /usr/local/idns/csv/registrarzone.csv\n"); |
| 3402 | | return; |
| 3403 | | } |
| 3404 | | |
| 3405 | | //Start processing. Unix files only! |
| 3406 | | while(fgets(gcQuery,254,fp)!=NULL) |
| 3407 | | { |
| 3408 | | cRegistrar[0]=0; |
| 3409 | | cZone[0]=0; |
| 3410 | | uRegistrar=0; |
| 3411 | | |
| 3412 | | if(gcQuery[0]=='\n' || gcQuery[0]=='#') |
| 3413 | | continue; |
| 3414 | | gcQuery[strlen(gcQuery)-1]=0; |
| 3415 | | |
| 3416 | | //Sample lines |
| 3417 | | //442subs.co.uk,Nominet |
| 3418 | | //442subsusa.com,Network Solutions |
| 3419 | | |
| 3420 | | //Quick and dirty parsing input must be in good shape! |
| 3421 | | //Parse cZone |
| 3422 | | if((cp=strchr(gcQuery,','))) |
| 3423 | | { |
| 3424 | | *cp=0; |
| 3425 | | sprintf(cZone,"%.99s",gcQuery); |
| 3426 | | *cp=','; |
| 3427 | | } |
| 3428 | | //debug only |
| 3429 | | //printf("cZone=%s\n",cZone); |
| 3430 | | |
| 3431 | | sprintf(cRegistrar,"%.99s",cp+1); |
| 3432 | | //debug only |
| 3433 | | //printf("cRegistrar=%s\n",cRegistrar); |
| 3434 | | |
| 3435 | | //Sanity checks |
| 3436 | | if(!cRegistrar[0] || !cZone[0] ) |
| 3437 | | { |
| 3438 | | fprintf(stderr,"Error: %s may be incorrectly formatted\n", |
| 3439 | | gcQuery); |
| 3440 | | continue; |
| 3441 | | } |
| 3442 | | |
| 3443 | | sprintf(cQuery,"SELECT uRegistrar FROM tRegistrar WHERE cLabel='%s'",cRegistrar); |
| 3444 | | mysql_query(&gMysql,cQuery); |
| 3445 | | if(mysql_errno(&gMysql)) |
| 3446 | | { |
| 3447 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3448 | | exit(2); |
| 3449 | | } |
| 3450 | | res=mysql_store_result(&gMysql); |
| 3451 | | if((field=mysql_fetch_row(res))) |
| 3452 | | sscanf(field[0],"%u",&uRegistrar); |
| 3453 | | mysql_free_result(res); |
| 3454 | | |
| 3455 | | if(uRegistrar) |
| 3456 | | { |
| 3457 | | sprintf(cQuery,"UPDATE tZone SET uRegistrar=%u,uModBy=1,uModDate=UNIX_TIMESTAMP(NOW())" |
| 3458 | | " WHERE cZone='%s'",uRegistrar,cZone); |
| 3459 | | mysql_query(&gMysql,cQuery); |
| 3460 | | if(mysql_errno(&gMysql)) |
| 3461 | | { |
| 3462 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3463 | | } |
| 3464 | | if(mysql_affected_rows(&gMysql)<1) |
| 3465 | | { |
| 3466 | | fprintf(stdout,"Warning: %s not updated.\n",cZone); |
| 3467 | | } |
| 3468 | | } |
| 3469 | | else |
| 3470 | | { |
| 3471 | | printf("Warning: %s registrar not found\n",cRegistrar); |
| 3472 | | } |
| 3473 | | |
| 3474 | | } |
| 3475 | | if(fp) fclose(fp); |
| 3476 | | |
| 3477 | | }//void AssociateRegistrarsZones(void) |
| 3478 | | |
| 3479 | | |
| 3480 | | void MassZoneUpdate(void) |
| 3481 | | { |
| 3482 | | FILE *fp; |
| 3483 | | char cQuery[512]; |
| 3484 | | char cZone[256]; |
| 3485 | | unsigned uNSSet; |
| 3486 | | MYSQL_RES *res; |
| 3487 | | MYSQL_ROW field; |
| 3488 | | |
| 3489 | | printf("MassZoneUpdate() Start\n"); |
| 3490 | | |
| 3491 | | if(!(fp=fopen("/usr/local/idns/csv/zones2update.txt","r"))) |
| 3492 | | { |
| 3493 | | fprintf(stdout,"Error: Could not open: /usr/local/idns/csv/zones2update.txt\n"); |
| 3494 | | return; |
| 3495 | | } |
| 3496 | | |
| 3497 | | //Start processing. Unix files only! |
| 3498 | | while(fgets(cZone,254,fp)!=NULL) |
| 3499 | | { |
| 3500 | | if(cZone[0]=='\n' || cZone[0]=='#') |
| 3501 | | continue; |
| 3502 | | cZone[strlen(cZone)-1]=0; |
| 3503 | | |
| 3504 | | uNSSet=0; |
| 3505 | | sprintf(cQuery,"SELECT uNSSet FROM tZone WHERE cZone='%.255s'",cZone); |
| 3506 | | mysql_query(&gMysql,cQuery); |
| 3507 | | if(mysql_errno(&gMysql)) |
| 3508 | | { |
| 3509 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3510 | | exit(2); |
| 3511 | | } |
| 3512 | | res=mysql_store_result(&gMysql); |
| 3513 | | if((field=mysql_fetch_row(res))) |
| 3514 | | sscanf(field[0],"%u",&uNSSet); |
| 3515 | | mysql_free_result(res); |
| 3516 | | if(!uNSSet) |
| 3517 | | { |
| 3518 | | fprintf(stdout,"Warning: %s not found.\n",cZone); |
| 3519 | | continue; |
| 3520 | | } |
| 3521 | | |
| 3522 | | //debug only |
| 3523 | | //printf("%s\n",cQuery); |
| 3524 | | //break; |
| 3525 | | |
| 3526 | | //1-. Update zone serial number |
| 3527 | | sprintf(cQuery,"UPDATE tZone SET uSerial=uSerial+1,uModBy=1,uModDate=UNIX_TIMESTAMP(NOW())" |
| 3528 | | " WHERE cZone='%.255s'",cZone); |
| 3529 | | mysql_query(&gMysql,cQuery); |
| 3530 | | if(mysql_errno(&gMysql)) |
| 3531 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3532 | | if(mysql_affected_rows(&gMysql)<1) |
| 3533 | | { |
| 3534 | | fprintf(stdout,"Warning: %s not updated.\n",cZone); |
| 3535 | | continue; |
| 3536 | | } |
| 3537 | | |
| 3538 | | //2-. Submit zone mod job only if we have a NS and the update worked |
| 3539 | | //external view only |
| 3540 | | SubmitJob("Modify",uNSSet,cZone,0,0); |
| 3541 | | } |
| 3542 | | |
| 3543 | | if(fp) fclose(fp); |
| 3544 | | |
| 3545 | | printf("MassZoneUpdate() End\n"); |
| 3546 | | |
| 3547 | | }//void MassZoneUpdate(void) |
| 3548 | | |
| 3549 | | |
| 3550 | | void MassZoneNSUpdate(char *cLabel) |
| 3551 | | { |
| 3552 | | FILE *fp; |
| 3553 | | char cQuery[512]; |
| 3554 | | char cZone[256]; |
| 3555 | | char cMasterIPs[256]={""}; |
| 3556 | | unsigned uNSSet=0; |
| 3557 | | MYSQL_RES *res; |
| 3558 | | MYSQL_ROW field; |
| 3559 | | |
| 3560 | | printf("MassZoneNSUpdate() Start\n"); |
| 3561 | | |
| 3562 | | if(!cLabel[0]) |
| 3563 | | { |
| 3564 | | fprintf(stderr,"Error: No tNameServer.cLabel provided. Aborting.\n"); |
| 3565 | | exit(1); |
| 3566 | | } |
| 3567 | | |
| 3568 | | if(!(fp=fopen("/usr/local/idns/csv/zones2NSupdate.txt","r"))) |
| 3569 | | { |
| 3570 | | fprintf(stdout,"Error: Could not open: /usr/local/idns/csv/zones2NSupdate.txt\n"); |
| 3571 | | return; |
| 3572 | | } |
| 3573 | | //3 == MASTER EXTERNAL |
| 3574 | | sprintf(cQuery,"SELECT uNSSet,cMasterIPs FROM tNS,tNSSet WHERE tNS.uNSSet=tNSSet.uNSSet AND" |
| 3575 | | " tNSSet.cLabel='%.32s' AND tNS.uNSType=3",cLabel); |
| 3576 | | mysql_query(&gMysql,cQuery); |
| 3577 | | if(mysql_errno(&gMysql)) |
| 3578 | | { |
| 3579 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3580 | | exit(2); |
| 3581 | | } |
| 3582 | | res=mysql_store_result(&gMysql); |
| 3583 | | if((field=mysql_fetch_row(res))) |
| 3584 | | { |
| 3585 | | sscanf(field[0],"%u",&uNSSet); |
| 3586 | | sprintf(cMasterIPs,"%.255s",field[1]); |
| 3587 | | } |
| 3588 | | mysql_free_result(res); |
| 3589 | | if(!uNSSet) |
| 3590 | | { |
| 3591 | | fprintf(stderr,"Error: %s not found in tNameServer with a MASTER EXTERNAL. Aborting.\n",cLabel); |
| 3592 | | exit(3); |
| 3593 | | } |
| 3594 | | if(!cMasterIPs[0]) |
| 3595 | | { |
| 3596 | | fprintf(stderr,"Error: No tNameServer.cMasterIPs for %s. Aborting.\n",cLabel); |
| 3597 | | exit(4); |
| 3598 | | } |
| 3599 | | |
| 3600 | | |
| 3601 | | //Start processing. Unix files only! |
| 3602 | | while(fgets(cZone,254,fp)!=NULL) |
| 3603 | | { |
| 3604 | | if(cZone[0]=='\n' || cZone[0]=='#') |
| 3605 | | continue; |
| 3606 | | cZone[strlen(cZone)-1]=0; |
| 3607 | | |
| 3608 | | |
| 3609 | | //1-. Update zone serial number, uNSSet, cMasterIPs OLD FORK |
| 3610 | | //1-. Update zone serial number, uNSSet NEW GPL FORK |
| 3611 | | sprintf(cQuery,"UPDATE tZone SET uNSSet=%u,uSerial=uSerial+1,uModBy=1,uModDate=UNIX_TIMESTAMP(NOW())" |
| 3612 | | " WHERE cZone='%.255s'",uNSSet,cZone); |
| 3613 | | |
| 3614 | | //debug only |
| 3615 | | //printf("%s\n",cQuery); |
| 3616 | | //continue; |
| 3617 | | |
| 3618 | | |
| 3619 | | mysql_query(&gMysql,cQuery); |
| 3620 | | if(mysql_errno(&gMysql)) |
| 3621 | | { |
| 3622 | | fprintf(stdout,"Error: %s\n",mysql_error(&gMysql)); |
| 3623 | | continue; |
| 3624 | | } |
| 3625 | | if(mysql_affected_rows(&gMysql)<1) |
| 3626 | | { |
| 3627 | | fprintf(stdout,"Warning: %s not updated.\n",cZone); |
| 3628 | | continue; |
| 3629 | | } |
| 3630 | | |
| 3631 | | //2-. Submit zone mod job only if we have a NS and the update worked |
| 3632 | | //Modify New means new NS = new slave.zones file |
| 3633 | | SubmitJob("Modify New",uNSSet,cZone,0,0); |
| 3634 | | } |
| 3635 | | |
| 3636 | | if(fp) fclose(fp); |
| 3637 | | |
| 3638 | | printf("MassZoneNSUpdate() End\n"); |
| 3639 | | |
| 3640 | | }//void MassZoneNSUpdate(char *cLabel) |