%% options copyright owner = Dirk Krause copyright year = 2012-xxxx license = bsd %% module #include "dk3all.h" #include "itadmin.h" $!trace-include /** Keywords used by the module. */ char const * const itadmin_hosts_c8_kw[] = { $!string-table # # 0: File name "hosts" # hosts # # 1: File open mode "w" # w # # 2: File name "hosts.ldif" # hosts.ldif # # 3: Line start for DN # dn: cn= # # 4: ipHostNumber in DN line # +ipHostNumber= # # 5: Final string in DN line before base # ,ou=Hosts, # # 6: objectClass: ipHost # objectClass: ipHost # # 7: objectClass: device # objectClass: device # # 8: objectClass: top # objectClass: top # # 9: cn # cn: # # 10: ipHostNumber # ipHostNumber: # # 11: First hosts line. # 127.0.0.1 localhost loopback # # 12: First hosts.ldif line. # dn: cn=localhost+ipHostNumber=127.0.0.1 # # 13: # cn: localhost # # 14: # cn: loopback # # 15: # ipHostNumber: 127.0.0.1 $!end }; int itadmin_hosts_output(itadmin_job *job) { FILE *fipo; /* Output file. */ itadmin_network *cnw; /* Current network. */ itadmin_network *pnw; /* Previous network. */ itadmin_host *host; /* Current host to process. */ char const *al; /* Current alias to process. */ int dnw; /* Flag: Different network. */ int any = 0; /* Flag: Any host found. */ int back = 0; $? "+ itadmin_output_hosts" fipo = dk3sf_c8_fopen_app( itadmin_hosts_c8_kw[0], itadmin_hosts_c8_kw[1], job->app ); if(fipo) { if(!(job->hlh)) { fputs(itadmin_hosts_c8_kw[11], fipo); fputc('\n', fipo); fputc('\n', fipo); } dk3sto_it_reset(job->i_hoi); pnw = cnw = NULL; while(NULL != (host = (itadmin_host *)dk3sto_it_next(job->i_hoi))) { if((host->ipv4) && (host->sn)) { dnw = 0; cnw = (itadmin_network *)dk3sto_it_find_like( job->i_nw, (void *)(&(host->ipv4)), 1 ); if(pnw) { if(cnw) { if(cnw->ip4 != pnw->ip4) { dnw = 1; } } else { dnw = 1; } } else { if(cnw) { dnw = 1; } } pnw = cnw; if(dnw) { fputc('\n', fipo); } itadmin_tool_write_ip(fipo, host->ipv4); fputc('\t', fipo); fputs(host->sn, fipo); if((host->fnf) && (host->dnsd)) { fputc('.', fipo); fputs(host->dnsd, fipo); fputc(' ', fipo); fputs(host->sn, fipo); } if(host->i_al) { dk3sto_it_reset(host->i_al); while(NULL != (al = (char const *)dk3sto_it_next(host->i_al))) { fputc(' ', fipo); fputs(al, fipo); } } fputc('\n', fipo); any = 1; } } if(any) { back = 1; } #if DK3_CHAR_SIZE == 1 if(!dk3sf_fclose_fn_app(fipo, itadmin_hosts_c8_kw[0], job->app)) { back = 0; } #else if(!dk3sf_fclose_fn_app(fipo, NULL, job->app)) { back = 0; } #endif } else { $? "! fopen" } $? "- itadmin_output_hosts %d", back return back; } int itadmin_hosts_ldif_output(itadmin_job *job) { char bu[ITADMIN_CONFIG_LINE_SIZE]; itadmin_host *host; FILE *fipo; char const *al; int ie; int any = 0; int back = 0; $? "+ itadmin_hosts_ldif_output" ie = dk3app_get_encoding(job->app); if(dk3str_to_c8u_app(bu,sizeof(bu),job->ldapb,ie,job->app)) { fipo = dk3sf_c8_fopen_app( itadmin_hosts_c8_kw[2], itadmin_hosts_c8_kw[1], job->app ); if(fipo) { if(!(job->hlh)) { fputs(itadmin_hosts_c8_kw[12], fipo); fputs(itadmin_hosts_c8_kw[5], fipo); fputs(bu, fipo); fputc('\n', fipo); /* objectClass: ipHost */ fputs(itadmin_hosts_c8_kw[6], fipo); fputc('\n', fipo); /* objectClass: device */ fputs(itadmin_hosts_c8_kw[7], fipo); fputc('\n', fipo); /* objectClass: top */ fputs(itadmin_hosts_c8_kw[8], fipo); fputc('\n', fipo); /* cn: localhost */ fputs(itadmin_hosts_c8_kw[13], fipo); fputc('\n', fipo); /* cn: loopback */ fputs(itadmin_hosts_c8_kw[14], fipo); fputc('\n', fipo); /* ipHostNumber: 127.0.0.1 */ fputs(itadmin_hosts_c8_kw[15], fipo); fputc('\n', fipo); fputc('\n', fipo); } dk3sto_it_reset(job->i_hoi); while(NULL != (host = (itadmin_host *)dk3sto_it_next(job->i_hoi))) { if((host->ipv4) && (host->sn)) { any = 1; /* DN */ fputs(itadmin_hosts_c8_kw[3], fipo); fputs(host->sn, fipo); if((host->fnf) && (host->dnsd)) { fputc('.', fipo); fputs(host->dnsd, fipo); } fputs(itadmin_hosts_c8_kw[4], fipo); itadmin_tool_write_ip(fipo, host->ipv4); fputs(itadmin_hosts_c8_kw[5], fipo); fputs(bu, fipo); fputc('\n', fipo); /* objectClass: ipHost */ fputs(itadmin_hosts_c8_kw[6], fipo); fputc('\n', fipo); /* objectClass: device */ fputs(itadmin_hosts_c8_kw[7], fipo); fputc('\n', fipo); /* objectClass: top */ fputs(itadmin_hosts_c8_kw[8], fipo); fputc('\n', fipo); /* cn: */ if((host->dnsd) && (host->fnf)) { fputs(itadmin_hosts_c8_kw[9], fipo); fputs(host->sn, fipo); fputc('.', fipo); fputs(host->dnsd, fipo); fputc('\n', fipo); } fputs(itadmin_hosts_c8_kw[9], fipo); fputs(host->sn, fipo); fputc('\n', fipo); if(host->i_al) { dk3sto_it_reset(host->i_al); while(NULL != (al = (char const *)dk3sto_it_next(host->i_al))) { fputs(itadmin_hosts_c8_kw[9], fipo); fputs(al, fipo); fputc('\n', fipo); } } /* ipHostNumber: */ fputs(itadmin_hosts_c8_kw[10], fipo); itadmin_tool_write_ip(fipo, host->ipv4); fputc('\n', fipo); fputc('\n', fipo); } } if(any) { back = 1; } #if DK3_CHAR_SIZE == 1 if(!dk3sf_fclose_fn_app(fipo, itadmin_hosts_c8_kw[2], job->app)) { back = 0; } #else if(!dk3sf_fclose_fn_app(fipo, NULL, job->app)) { back = 0; } #endif } else { $? "! fopen" } } else { $? "! conversion to UTF-8 failed" } $? "- itadmin_hosts_ldif_output %d", back return back; }