modules/nt/notification.cc

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

FUNCTIONS

This source file includes following functions.
  1. NT_ntfy_filename_generate
  2. NT_forwd_filename_generate
  3. NT_crossntfy_filename_generate
  4. NT_add_to_ntfy_hash
  5. NT_add_to_frwd_hash
  6. NT_add_to_ntfy_hash_list
  7. NT_add_to_frwd_hash_list
  8. NT_add_to_ntfy
  9. NT_add_to_ntfy_list
  10. NT_send_ntfy
  11. NT_log_ntfy
  12. NT_delete_ntfy
  13. nt_gfunc_send
  14. NT_send_ntfy_list
  15. nt_gfunc_log
  16. NT_log_ntfy_list
  17. nt_gfunc_delete
  18. NT_delete_ntfy_list
  19. NT_gather_ntfy_addresses
  20. NT_gather_frwd_addresses
  21. NT_write_all_ntfs
  22. NT_write_all_frwds

   1 /***************************************
   2   $Revision: 1.8 $
   3 
   4   NT (Notifications) module
   5 
   6   Status: NOT REVIEWED, NOT TESTED
   7 
   8   Author(s):       Engin Gunduz
   9 
  10   ******************/ /******************
  11   Modification History:
  12         engin (06/07/2000) Created.
  13   ******************/ /******************
  14   Copyright (c) 2000                              RIPE NCC
  15  
  16   All Rights Reserved
  17   
  18   Permission to use, copy, modify, and distribute this software and its
  19   documentation for any purpose and without fee is hereby granted,
  20   provided that the above copyright notice appear in all copies and that
  21   both that copyright notice and this permission notice appear in
  22   supporting documentation, and that the name of the author not be
  23   used in advertising or publicity pertaining to distribution of the
  24   software without specific, written prior permission.
  25   
  26   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  27   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  28   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  29   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  30   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  31   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32  ***************************************/
  33 
  34 
  35 
  36 
  37 
  38 
  39 #include "notification.h"
  40 
  41 
  42 /*  Generates a unique file name and returns the full path of the filename 
  43     for storing notification message.  Creates the file at the same time. 
  44     May use PID or time or both to ensure uniqueness.  */
  45       
  46 char * NT_ntfy_filename_generate( const char * tmpdir, const char * e_mail){
     /* [<][>][^][v][top][bottom][index][help] */
  47 
  48    FILE * ntfy_file;
  49    char * name;
  50      
  51    /* allocate space for name.  32 should be enough for PID */
  52    name = (char*)malloc(strlen(tmpdir) + strlen(e_mail) + strlen("notify") +32 ); 
  53    
  54    sprintf(name, "%s/%s-%s.%i", tmpdir, "notify", e_mail, getpid());
  55 
  56    /* create the file */
  57    if(( ntfy_file = fopen(name, "w")) == NULL){
  58      fprintf(stderr, "Can't open notification file, %s", name);
  59    }
  60 
  61    fprintf(ntfy_file, "To: %s\nFrom: %s\nSubject: Notification of RIPE Database changes\nReply-To: %s\n\n%s\n", e_mail, humailbox, humailbox, notitxt);
  62    if(reading_from_mail){
  63      fprintf(ntfy_file, "%s\n\n", notimailtxt);
  64    }
  65    /* close it */
  66    fclose(ntfy_file);
  67     
  68    return name;
  69       
  70 }
  71 
  72 
  73 
  74 
  75 
  76 /* Generates a unique file name and returns the full path of the filename 
  77    for storing forwarded message. Creates the file at the same time.  */ 
  78 char * NT_forwd_filename_generate( const char * tmpdir, const char * e_mail){
     /* [<][>][^][v][top][bottom][index][help] */
  79 
  80    FILE * forwd_file;
  81    char * name;
  82       
  83    /* allocate space for name.  32 should be enough for PID */
  84    name = (char*)malloc(strlen(tmpdir) + strlen(e_mail) + strlen("forwd") +32 ); 
  85    
  86    sprintf(name, "%s/%s-%s.%i", tmpdir, "forwd", e_mail, getpid());
  87    //printf("DEBUG: NT_forwd_filename_generate: will generate %s\n", name);
  88    /* create the file */
  89    if(( forwd_file = fopen(name, "w")) == NULL){
  90      fprintf(stderr, "Can't open forward file, %s", name);
  91    }
  92 
  93    fprintf(forwd_file, "To: %s\nFrom: %s\nSubject:  Requested RIPE database object changes \nReply-To: %s\n\n%s\n", e_mail, humailbox, humailbox, fwtxt);
  94    if(reading_from_mail){
  95      fprintf(forwd_file, "\n%s\n", fwmailtxt);
  96    }
  97 
  98    /* close it */
  99    fclose(forwd_file);
 100     
 101    return name;
 102 
 103 }
 104 
 105 
 106 
 107 
 108 
 109 /* Generates a unique file name and returns the full path of the filename for 
 110    storing notification message. Creates the file at the same time.  */
 111 char * NT_crossntfy_filename_generate( const char * tmpdir, const char * e_mail){
     /* [<][>][^][v][top][bottom][index][help] */
 112    FILE * cross_file;
 113    char * name;
 114       
 115    /* allocate space for name.  32 should be enough for PID */
 116    name = (char*)malloc(strlen(tmpdir) + strlen(e_mail) + strlen("cross") +32 ); 
 117    
 118    sprintf(name, "%s/%s-%s.%i", tmpdir, "cross", e_mail, getpid());
 119 
 120    /* create the file */
 121    if(( cross_file = fopen(name, "w")) == NULL){
 122      fprintf(stderr, "Can't open cross file, %s", name);
 123    }
 124 
 125    /* close it */
 126    fclose(cross_file);
 127     
 128    return name;
 129 
 130 }
 131 
 132 
 133 
 134 
 135 
 136 /* Adds the e-mail to the notify hash, generating appropriate temp files */
 137 void NT_add_to_ntfy_hash(GHashTable * ntfy_hash, char * e_mail){
     /* [<][>][^][v][top][bottom][index][help] */
 138 
 139   if(g_hash_table_lookup(ntfy_hash ,e_mail) == NULL){/* there is no such entry, so create it */
 140     g_hash_table_insert(ntfy_hash, strdup(e_mail), NT_ntfy_filename_generate(tmpdir, e_mail));
 141   }
 142     
 143 }
 144 
 145 
 146 /* Adds the e-mail to the forw hash, generating appropriate temp files */
 147 void NT_add_to_frwd_hash(GHashTable * frwd_hash, char * e_mail){
     /* [<][>][^][v][top][bottom][index][help] */
 148 
 149   if(g_hash_table_lookup(frwd_hash ,e_mail) == NULL){/* there is no such entry, so create it */
 150     g_hash_table_insert(frwd_hash, strdup(e_mail), NT_forwd_filename_generate(tmpdir, e_mail));
 151   }
 152     
 153 }
 154 
 155 
 156 
 157 /* Adds the e-mails in a linked list to the hash */
 158 void NT_add_to_ntfy_hash_list(GHashTable * ntfy_hash, GSList * e_mail_list){
     /* [<][>][^][v][top][bottom][index][help] */
 159 
 160    GSList * temp = NULL;
 161 
 162    for(temp = e_mail_list; temp != NULL; temp = g_slist_next(temp)){
 163      NT_add_to_ntfy_hash(ntfy_hash, (char *)temp->data);
 164    }
 165   
 166 }
 167 
 168 
 169 
 170 
 171 /* Adds the e-mails in a linked list to the hash */
 172 void NT_add_to_frwd_hash_list(GHashTable * frwd_hash, GSList * e_mail_list){
     /* [<][>][^][v][top][bottom][index][help] */
 173 
 174    GSList * temp = NULL;
 175 
 176    for(temp = e_mail_list; temp != NULL; temp = g_slist_next(temp)){
 177      NT_add_to_frwd_hash(frwd_hash, (char *)temp->data);
 178    }
 179   
 180 }
 181 
 182 
 183 
 184 /* Appends the argument strings to the file.  */
 185 void NT_add_to_ntfy( char * filename, char * fmt, ... ){
     /* [<][>][^][v][top][bottom][index][help] */
 186   va_list ap;  /* points to each unnamed arg in turn */
 187   FILE * ack_file;
 188  
 189   if(tracing){
 190     printf("TRACING: NT_add_to_ntfy\n"); 
 191   }
 192   if(( ack_file = fopen(filename, "a")) == NULL){
 193     fprintf(stderr, "Can't open notification file, %s\n", filename);
 194   }
 195     
 196   va_start(ap, fmt);
 197   vfprintf(ack_file, fmt, ap);
 198 
 199   va_end(ap); /* clean up */
 200   fclose(ack_file);
 201 }
 202 
 203 
 204 
 205 
 206 
 207 /* Appends the argument string to the temp notif files in the list */
 208 void NT_add_to_ntfy_list(GSList * list, GHashTable * hash, char * arg){
     /* [<][>][^][v][top][bottom][index][help] */
 209   
 210   GSList * temp = NULL;
 211 
 212   for(temp = list; temp != NULL; temp = g_slist_next(temp)){
 213     NT_add_to_ntfy((char *)g_hash_table_lookup(hash, ((char *)temp->data)), arg);
 214   }
 215 }
 216 
 217 
 218 
 219 
 220 
 221 
 222 
 223 
 224 /* Sends the notification message which is stored in the temporary filefilename.  */
 225 void NT_send_ntfy( const char * filename, const char * to_address, const char * mailercommand){
     /* [<][>][^][v][top][bottom][index][help] */
 226 
 227     char * mail_command_line = NULL;
 228 
 229     if(to_address != NULL){
 230       mail_command_line = (char *)malloc(strlen(mailercommand) + strlen(filename) + 128);
 231       sprintf(mail_command_line, "%s %s < %s", mailercommand, to_address, filename);
 232       system(mail_command_line);
 233     }
 234 
 235 }
 236 
 237 
 238 
 239 /* Adds the notification message which is in the filename into log_file. */
 240 void NT_log_ntfy( const char * filename, const char * logfilename){
     /* [<][>][^][v][top][bottom][index][help] */
 241 
 242   FILE * notif_file, * log_file;
 243   char * buf;
 244   time_t cur_time;
 245   char * time_str;
 246 
 247   buf = (char *)malloc(1024);
 248   if(( notif_file = fopen(filename, "r")) == NULL){
 249     fprintf(stderr, "Can't open notification file, [%s]\n", filename);
 250     return;
 251   }
 252 
 253   if(( log_file = fopen(logfilename, "a")) == NULL){
 254     fprintf(stderr, "Can't open log file, %s\n", logfilename);
 255     return;
 256   }
 257 
 258   /* get time */
 259   cur_time = time(NULL);
 260   time_str = strdup(ctime(&cur_time));
 261   /* cut the '\n' at the end */
 262   time_str[strlen(time_str) - 1] = '\0';
 263 
 264   fprintf(log_file, ">>> time: %s NOTIF <<<\n\n", time_str);
 265 
 266 
 267   while((buf=fgets(buf, 1023, notif_file)) > 0){
 268     fprintf(log_file, "%s", buf);
 269   }
 270 
 271   fclose(notif_file);
 272   fclose(log_file);
 273 
 274 }
 275 
 276 
 277 /* Deletes the temporary notification file. */ 
 278 void NT_delete_ntfy( const char * filename){
     /* [<][>][^][v][top][bottom][index][help] */
 279 
 280    unlink(filename);
 281    
 282 }
 283 
 284 
 285 /* The function required for NT_send_ntfy_list */
 286 void  nt_gfunc_send(gpointer key, gpointer value, gpointer user_data){
     /* [<][>][^][v][top][bottom][index][help] */
 287   NT_send_ntfy((char *)value, (char *)key, (char *)user_data);
 288 }
 289 
 290 
 291        
 292 /* Sends the notification messages whose temp files are stored in filehash. */              
 293 void NT_send_ntfy_list( GHashTable * filehash, char * mailercommand){
     /* [<][>][^][v][top][bottom][index][help] */
 294 
 295   g_hash_table_foreach( filehash, (GHFunc)nt_gfunc_send, mailercommand);
 296   
 297 }
 298 
 299 
 300 
 301 
 302 /* The function required for NT_log_ntfy_list */
 303 void  nt_gfunc_log(gpointer key, gpointer value, gpointer user_data){
     /* [<][>][^][v][top][bottom][index][help] */
 304   NT_log_ntfy((char *)value, (char *)user_data);
 305 }
 306 
 307 
 308 
 309 
 310 /* Logs the notification whose temp files are in filehash to log_file. */
 311 void NT_log_ntfy_list( GHashTable * filehash, char * log_file){
     /* [<][>][^][v][top][bottom][index][help] */
 312 
 313    g_hash_table_foreach( filehash, (GHFunc)nt_gfunc_log, log_file);
 314   
 315 }
 316 
 317 
 318 
 319 /* The function required for NT_delete_ntfy_list */
 320 void  nt_gfunc_delete(gpointer key, gpointer value, gpointer user_data){
     /* [<][>][^][v][top][bottom][index][help] */
 321   NT_delete_ntfy((char *)value);
 322 }
 323 
 324 
 325 
 326 /* Deletes the temporary notification messages in the filehash. Empties and frees 
 327    the hash too.  */
 328 void NT_delete_ntfy_list( GHashTable * filehash){
     /* [<][>][^][v][top][bottom][index][help] */
 329 
 330   g_hash_table_foreach(filehash, (GHFunc)nt_gfunc_delete, NULL);
 331   g_hash_table_destroy(filehash);
 332   
 333 }
 334 
 335 
 336 /* Gathers e-mail boxes to which we will send normal notification messages. It 
 337    takes old and new objects, looks up maintainers and less specific inetnums/domains/routes 
 338    when necessary, finds the addresses (in mnt-nfy and notify attributes) and returns 
 339    a list of them. */
 340 GSList * NT_gather_ntfy_addresses( char * old_object, char * new_object){
     /* [<][>][^][v][top][bottom][index][help] */
 341   GSList *temp = NULL;
 342   GSList * mntners = NULL; 
 343 
 344   if(old_object != NULL && new_object != NULL){/* it was an update */
 345     temp = get_attr_list(old_object, "notify");
 346     mntners = get_mntners(old_object);
 347     temp = g_slist_concat(temp, get_mntnfy_vector(mntners));
 348   }else if(old_object == NULL && new_object != NULL){/* it was a creation */
 349     temp = get_attr_list(new_object, "notify");
 350     mntners = get_mntners(new_object);
 351     temp = g_slist_concat(temp, get_mntnfy_vector(mntners));
 352   }else if(old_object != NULL && new_object == NULL){/* it was a deletion */
 353     temp = get_attr_list(old_object, "notify");
 354     mntners = get_mntners(old_object);
 355     temp = g_slist_concat(temp, get_mntnfy_vector(mntners));
 356   }
 357   return temp;
 358 }
 359 
 360 
 361 
 362 /* Gathers e-mail boxes to which we will forward messages (or rather, objects). It 
 363    an object, looks up maintainers, finds the addresses (in upd-to attributes) and returns 
 364    a list of them. */
 365 GSList * NT_gather_frwd_addresses(char * object){
     /* [<][>][^][v][top][bottom][index][help] */
 366   GSList *temp = NULL;
 367   GSList * mntners = NULL; 
 368 
 369   mntners = get_mntners(object);
 370   temp = get_updto_vector(mntners);
 371   return temp;
 372 }
 373 
 374 
 375 
 376 
 377 
 378 
 379 /* Gets old and new versions of the object, and creates temporary notification
 380    files when necessary, and then writes appropriate strings into those
 381    temporary files. */
 382 void NT_write_all_ntfs(char * old_object, char * new_object, /*const char * notif_log,
     /* [<][>][^][v][top][bottom][index][help] */
 383                        const char * forw_log,    const char * cross_log,*/ const char * tempdir,
 384                        GHashTable * ntfy_hash,  GHashTable * forwd_hash, GHashTable * cross_hash, 
 385                        const char * from_address){ 
 386 
 387    GSList * e_mail_list = NULL;
 388 
 389 
 390    if(tracing){
 391      printf("TRACING: NT_write_all_ntfs\n");
 392    }
 393    if(old_object != NULL && new_object != NULL){/* it was an update */
 394      e_mail_list = NT_gather_ntfy_addresses(old_object, new_object);
 395      NT_add_to_ntfy_hash_list(ntfy_hash, e_mail_list); 
 396      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "---\nPREVIOUS OBJECT:\n\n");
 397      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, old_object);
 398      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "\n\nREPLACED BY:\n\n");
 399      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, new_object);
 400      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "\n");
 401    }else if(old_object == NULL && new_object != NULL){/* it was a creation */
 402      e_mail_list = NT_gather_ntfy_addresses(old_object, new_object);
 403      NT_add_to_ntfy_hash_list(ntfy_hash, e_mail_list); 
 404      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "---\nOBJECT BELOW CREATED:\n\n");
 405      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, new_object);
 406      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "\n");
 407    }else if(old_object != NULL && new_object == NULL){/* it was a deletion */
 408      e_mail_list = NT_gather_ntfy_addresses(old_object, new_object);
 409      NT_add_to_ntfy_hash_list(ntfy_hash, e_mail_list); 
 410      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "---\nOBJECT BELOW DELETED:\n\n");
 411      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, old_object);
 412      NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "\n");
 413    }
 414 }
 415 
 416 
 417 
 418 
 419 
 420 /* Gets old and new versions of the object, and creates temporary notification
 421    files when necessary, and then writes appropriate strings into those
 422    temporary files. */
 423 void NT_write_all_frwds(char * old_object, char * new_object, /*const char * notif_log,
     /* [<][>][^][v][top][bottom][index][help] */
 424                        const char * forw_log,    const char * cross_log,*/ const char * tempdir,
 425                        GHashTable * ntfy_hash,  GHashTable * forwd_hash, GHashTable * cross_hash, 
 426                        const char * from_address){ 
 427 
 428    GSList * e_mail_list = NULL;
 429 
 430 
 431    if(tracing){
 432      printf("TRACING: NT_write_all_frwds\n");
 433    }
 434    if(old_object != NULL && new_object != NULL){/* it was an update */
 435      e_mail_list = NT_gather_frwd_addresses(old_object);
 436      NT_add_to_frwd_hash_list(forwd_hash, e_mail_list);
 437      NT_add_to_ntfy_list(e_mail_list, forwd_hash, "----\nUPDATE REQUESTED FOR:\n\n");
 438      //NT_add_to_ntfy_list(e_mail_list, forwd_hash, old_object);
 439      //NT_add_to_ntfy_list(e_mail_list, forwd_hash, "\n\n");
 440      NT_add_to_ntfy_list(e_mail_list, forwd_hash, new_object);
 441    }else if(old_object == NULL && new_object != NULL){/* it was a creation */
 442      e_mail_list = NT_gather_frwd_addresses(new_object);
 443      NT_add_to_frwd_hash_list(forwd_hash, e_mail_list);
 444      NT_add_to_ntfy_list(e_mail_list, forwd_hash, "----\nADDITION REQUESTED FOR:\n\n");
 445      NT_add_to_ntfy_list(e_mail_list, forwd_hash, new_object);
 446    }else if(old_object != NULL && new_object == NULL){/* it was a deletion */
 447      e_mail_list = NT_gather_frwd_addresses(old_object);
 448      NT_add_to_frwd_hash_list(forwd_hash, e_mail_list);
 449      NT_add_to_ntfy_list(e_mail_list, forwd_hash, "----\nDELETION REQUESTED FOR:\n\n");
 450      NT_add_to_ntfy_list(e_mail_list, forwd_hash, old_object);
 451    }
 452 }
 453 

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