## bin/dbupdate/dbupdate.cc

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. error_init
2. main

       1 #include "dbupdate.h"
       2 #include "erroutines.h"
       3
       4 int tracing = 0;
       5
       6 void error_init(int argc, char ** argv) {
         /* [<][>][^][v][top][bottom][index][help] */
       7   er_path_t erlogstr;
       8
       9   ER_init(argc, argv);
      10
      11   erlogstr.fdes = stderr;
      12   erlogstr.asp  = 0;
      13   erlogstr.sev  = ER_SEV_W;
      14   erlogstr.mode = ER_M_SEVCHAR | ER_M_TEXTLONG;
      15
      16   ER_setpath(& erlogstr);
      17
      18 } /* error_init() */
      19
      20
      21
      22
      23 void main(int argc, char **argv, char **envp){
         /* [<][>][^][v][top][bottom][index][help] */
      24   //schema.initialize();
      25   //init_and_set_options(argc, argv, envp);
      26
      27   int count = 0;
      28
      29
      30   char *line;
      31   char *input_file_name = NULL;
      32   FILE * input_file;
      33   char *object = NULL;
      34   int object_count = 0;
      35   GSList *list_of_objects = NULL, *list_of_objects2 = NULL;
      36   GSList *next = NULL;
      37   GHashTable* AUTO_NIC_hdl_hash;
      38   credentials_struct credentials;
      39   int reading_from_mail = 0;
      40
      41   pid_t pid;
      42   char * mail_command_line, * ack_file_name;
      43   FILE * ack_file;
      44   int result = 0;
      45
      46   extern char *optarg;
      47   extern int optind;
      48
      49
      50   object = NULL;
      51   credentials.password_list = NULL;
      52   credentials.from = NULL;
      53   int ch;
      54   char * to_address = NULL;
      55
      56   AUTO_NIC_hdl_hash = g_hash_table_new(g_str_hash, g_str_equal);
      57   error_init(argc, argv);
      58
      59   line = (char *)malloc(1024);
      60
      61   while ((ch = getopt(argc, argv, "Mtf:")) != -1){
      62           switch(ch) {
      63           case 'M':
      64                   reading_from_mail = 1;
      65                   break;
      66           case 'f':
      67                   input_file_name = strdup(optarg);
      68                   break;
      69           case 't':
      70                   tracing = 1;
      71                   break;
      72           case '?':
      73           default:
      74                   printf("Unknown option\n");exit(1);
      75           }
      76   }
      77
      78   /* if input_file_name is given in the command line, open and read it,
      79      otherwise, read from stdin */
      80   if(input_file_name == NULL){
      81     input_file = stdin;
      82   }else{
      83     if((input_file = fopen(input_file_name, "r")) == NULL){
      84        printf("Couldn't open the file %s: %s\n", input_file_name, strerror(errno));
      85        exit(1);
      86     }
      87   }
      88
      89   while(fgets(line, 1024, input_file) != NULL){
      90     /* first, if it is a pasword, save it, but do not regard it as an attrib */
      91     if(strstr(line, "password:") == line){
      92       cout << "This is a password" << endl;
      93       credentials.password_list = g_slist_append(credentials.password_list,
      94                                     g_strstrip(strdup(line + strlen("password:"))));
      95       continue;
      96     }
      97     /* if the length of the line read is 1, then this is an empty line */
      98     if(strlen(line) == 1){
      99        //cout << endl;
     100        if(object != NULL){
     101          //cout << "The object was" << endl << object << endl;
     102          list_of_objects = g_slist_append(list_of_objects, object);
     103          object = NULL;
     104        }
     105     }else{
     106       /* if the line contains only the EOL char */
     107       if(object == NULL && strlen(line) != 1){
     108         object = (char *)malloc(strlen(line));
     109         object = strdup(line);
     110       }
     111       else{
     112         object = (char *)realloc(object, strlen(object) + strlen(line));
     113         object = strcat(object, line);
     114       }
     115     }
     116
     117   }
     118
     119   /* now, if at the very and of the input file there wasn't an
     120      empty line, we have to add the remaining object in the 'object'
     121      variable */
     122   if(object != NULL){
     123      //cout << "The object was" << endl << object << endl;
     124      list_of_objects = g_slist_append(list_of_objects, object);
     125      object = NULL;
     126   }
     127
     128   /* initialize the parser */
     129   schema.initialize();
     130
     131   pid = getpid();
     132
     133   ack_file_name = (char *)malloc(strlen(ACK_FILE_PREFIX) + 128);
     134   sprintf(ack_file_name, "%s.%i", ACK_FILE_PREFIX, pid);
     135
     136   if((ack_file = fopen(ack_file_name, "w")) == NULL){
     137     perror("Couldn't open ack file");
     138     exit(1);
     139   }
     140
     141
     142
     143
     144   if(tracing) {
     145      printf("TRACING: Will process the objects in the list\n");
     146   }
     147   next = list_of_objects;
     148   object_count = 0;
     149   for( next = list_of_objects; next != NULL ; next = g_slist_next(next) ){
     150     object_count++;
     151     /* if this is the first 'object' and we are getting an update from
     152        an e-mail message, then this is the mail header. */
     153     if(object_count == 1 ){
     154       if(reading_from_mail){
     155         printf("TRACING: Will process the mail header\n");
     156         process_mail_header(&credentials , (char *)next->data);
     157         to_address = find_to_address(credentials.from);
     158         /* Send a primitive ack */
     159         fprintf(ack_file, "To: ");
     160         fprintf(ack_file, to_address);
     161         fprintf(ack_file, "\nFrom: bit-bucket@ripe.net\nSubject: ack \nReply-To: bit-bucket@ripe.net\n\nAcknowledgement message from database software\n");
     162         if(credentials.from != NULL){
     163           fprintf(ack_file, "\n[%s]\n", credentials.from);
     164         }
     165         fclose(ack_file);
     166
     167         continue;
     168       }else{
     169         fprintf(ack_file, "This is the ack file. The update is not a mail update\n");
     170         fclose(ack_file);
     171       }
     172     }
     173
     174     if(tracing) {
     175       cout << "TRACING: Got an object from the list" << endl;
     176       cout << (char *)next->data << endl;
     177     }
     178
     179     if(has_ref_to_AUTO_nic_hdl((char *)next->data)){/* defer the processing */
     180       if(tracing) {
     181         printf("TRACING: this object has a ref to an AUTO NIC hdl\n");
     182       }
     183       list_of_objects2 = g_slist_append(list_of_objects2, strdup((char *)next->data));
     184     }else{
     185       result = 0;
     186       result = process_object((char *)next->data, credentials, AUTO_NIC_hdl_hash, ack_file_name);
     187     }
     188   }
     189
     190   if(tracing) {
     191     printf("TRACING: list_of_objects2 has %d entries\n", g_slist_length(list_of_objects2));
     192   }
     193
     194   if(tracing) {
     195     printf("TRACING: will start to process the second list\n");
     196   }
     197
     198   for( next = list_of_objects2; next != NULL ; next = g_slist_next(next) ){
     199     if(tracing) {
     200       printf("TRACING: Will process object: %s\n", (char *)next->data);
     201     }
     202     result = process_object((char *)next->data, credentials, AUTO_NIC_hdl_hash, ack_file_name);
     203   }
     204
     205
     206   if(reading_from_mail && to_address != NULL){
     207     mail_command_line = (char *)malloc(strlen(MAIL_CMD) + strlen(ack_file_name) + 128);
     208
     209     sprintf(mail_command_line, "%s %s < %s", MAIL_CMD, to_address, ack_file_name);
     210
     211     system(mail_command_line);
     212   }
     213
     214
     215   if(tracing) {
     216     printf("TRACING: END\n");
     217   }
     218
     219
     220 }

/* [<][>][^][v][top][bottom][index][help] */