modules/nt/notification.cc
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- NT_ntfy_filename_generate
- NT_forwd_filename_generate
- NT_crossntfy_filename_generate
- NT_add_to_ntfy_hash
- NT_add_to_frwd_hash
- NT_add_to_ntfy_hash_list
- NT_add_to_frwd_hash_list
- NT_add_to_ntfy
- NT_add_to_ntfy_list
- NT_send_ntfy
- NT_log_ntfy
- NT_delete_ntfy
- nt_gfunc_send
- NT_send_ntfy_list
- nt_gfunc_log
- NT_log_ntfy_list
- nt_gfunc_delete
- NT_delete_ntfy_list
- NT_gather_ntfy_addresses
- NT_gather_frwd_addresses
- NT_write_all_ntfs
- NT_write_all_frwds
1 /***************************************
2 $Revision: 1.7 $
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
245 buf = (char *)malloc(1024);
246 if(( notif_file = fopen(filename, "r")) == NULL){
247 fprintf(stderr, "Can't open notification file, [%s]\n", filename);
248 return;
249 }
250
251 if(( log_file = fopen(logfilename, "a")) == NULL){
252 fprintf(stderr, "Can't open log file, %s\n", logfilename);
253 return;
254 }
255
256 /* must put real time here */
257 fprintf(log_file, ">>> time: NOTIF <<<\n\n");
258
259
260 while((buf=fgets(buf, 1023, notif_file)) > 0){
261 fprintf(log_file, "%s", buf);
262 }
263
264 fclose(notif_file);
265 fclose(log_file);
266
267 }
268
269
270 /* Deletes the temporary notification file. */
271 void NT_delete_ntfy( const char * filename){
/* [<][>][^][v][top][bottom][index][help] */
272
273 unlink(filename);
274
275 }
276
277
278 /* The function required for NT_send_ntfy_list */
279 void nt_gfunc_send(gpointer key, gpointer value, gpointer user_data){
/* [<][>][^][v][top][bottom][index][help] */
280 NT_send_ntfy((char *)value, (char *)key, (char *)user_data);
281 }
282
283
284
285 /* Sends the notification messages whose temp files are stored in filehash. */
286 void NT_send_ntfy_list( GHashTable * filehash, char * mailercommand){
/* [<][>][^][v][top][bottom][index][help] */
287
288 g_hash_table_foreach( filehash, (GHFunc)nt_gfunc_send, mailercommand);
289
290 }
291
292
293
294
295 /* The function required for NT_log_ntfy_list */
296 void nt_gfunc_log(gpointer key, gpointer value, gpointer user_data){
/* [<][>][^][v][top][bottom][index][help] */
297 NT_log_ntfy((char *)value, (char *)user_data);
298 }
299
300
301
302
303 /* Logs the notification whose temp files are in filehash to log_file. */
304 void NT_log_ntfy_list( GHashTable * filehash, char * log_file){
/* [<][>][^][v][top][bottom][index][help] */
305
306 g_hash_table_foreach( filehash, (GHFunc)nt_gfunc_log, log_file);
307
308 }
309
310
311
312 /* The function required for NT_delete_ntfy_list */
313 void nt_gfunc_delete(gpointer key, gpointer value, gpointer user_data){
/* [<][>][^][v][top][bottom][index][help] */
314 NT_delete_ntfy((char *)value);
315 }
316
317
318
319 /* Deletes the temporary notification messages in the filehash. Empties and frees
320 the hash too. */
321 void NT_delete_ntfy_list( GHashTable * filehash){
/* [<][>][^][v][top][bottom][index][help] */
322
323 g_hash_table_foreach(filehash, (GHFunc)nt_gfunc_delete, NULL);
324 g_hash_table_destroy(filehash);
325
326 }
327
328
329 /* Gathers e-mail boxes to which we will send normal notification messages. It
330 takes old and new objects, looks up maintainers and less specific inetnums/domains/routes
331 when necessary, finds the addresses (in mnt-nfy and notify attributes) and returns
332 a list of them. */
333 GSList * NT_gather_ntfy_addresses( char * old_object, char * new_object){
/* [<][>][^][v][top][bottom][index][help] */
334 GSList *temp = NULL;
335 GSList * mntners = NULL;
336
337 if(old_object != NULL && new_object != NULL){/* it was an update */
338 temp = get_attr_list(old_object, "notify");
339 mntners = get_mntners(old_object);
340 temp = g_slist_concat(temp, get_mntnfy_vector(mntners));
341 }else if(old_object == NULL && new_object != NULL){/* it was a creation */
342 temp = get_attr_list(new_object, "notify");
343 mntners = get_mntners(new_object);
344 temp = g_slist_concat(temp, get_mntnfy_vector(mntners));
345 }else if(old_object != NULL && new_object == NULL){/* it was a deletion */
346 temp = get_attr_list(old_object, "notify");
347 mntners = get_mntners(old_object);
348 temp = g_slist_concat(temp, get_mntnfy_vector(mntners));
349 }
350 return temp;
351 }
352
353
354
355 /* Gathers e-mail boxes to which we will forward messages (or rather, objects). It
356 an object, looks up maintainers, finds the addresses (in upd-to attributes) and returns
357 a list of them. */
358 GSList * NT_gather_frwd_addresses(char * object){
/* [<][>][^][v][top][bottom][index][help] */
359 GSList *temp = NULL;
360 GSList * mntners = NULL;
361
362 mntners = get_mntners(object);
363 temp = get_updto_vector(mntners);
364 return temp;
365 }
366
367
368
369
370
371
372 /* Gets old and new versions of the object, and creates temporary notification
373 files when necessary, and then writes appropriate strings into those
374 temporary files. */
375 void NT_write_all_ntfs(char * old_object, char * new_object, /*const char * notif_log,
/* [<][>][^][v][top][bottom][index][help] */
376 const char * forw_log, const char * cross_log,*/ const char * tempdir,
377 GHashTable * ntfy_hash, GHashTable * forwd_hash, GHashTable * cross_hash,
378 const char * from_address){
379
380 GSList * e_mail_list = NULL;
381
382
383 if(tracing){
384 printf("TRACING: NT_write_all_ntfs\n");
385 }
386 if(old_object != NULL && new_object != NULL){/* it was an update */
387 e_mail_list = NT_gather_ntfy_addresses(old_object, new_object);
388 NT_add_to_ntfy_hash_list(ntfy_hash, e_mail_list);
389 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "---\nPREVIOUS OBJECT:\n\n");
390 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, old_object);
391 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "\n\nREPLACED BY:\n\n");
392 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, new_object);
393 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "\n");
394 }else if(old_object == NULL && new_object != NULL){/* it was a creation */
395 e_mail_list = NT_gather_ntfy_addresses(old_object, new_object);
396 NT_add_to_ntfy_hash_list(ntfy_hash, e_mail_list);
397 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "---\nOBJECT BELOW CREATED:\n\n");
398 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, new_object);
399 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "\n");
400 }else if(old_object != NULL && new_object == NULL){/* it was a deletion */
401 e_mail_list = NT_gather_ntfy_addresses(old_object, new_object);
402 NT_add_to_ntfy_hash_list(ntfy_hash, e_mail_list);
403 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "---\nOBJECT BELOW DELETED:\n\n");
404 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, old_object);
405 NT_add_to_ntfy_list(e_mail_list, ntfy_hash, "\n");
406 }
407 }
408
409
410
411
412
413 /* Gets old and new versions of the object, and creates temporary notification
414 files when necessary, and then writes appropriate strings into those
415 temporary files. */
416 void NT_write_all_frwds(char * old_object, char * new_object, /*const char * notif_log,
/* [<][>][^][v][top][bottom][index][help] */
417 const char * forw_log, const char * cross_log,*/ const char * tempdir,
418 GHashTable * ntfy_hash, GHashTable * forwd_hash, GHashTable * cross_hash,
419 const char * from_address){
420
421 GSList * e_mail_list = NULL;
422
423
424 if(tracing){
425 printf("TRACING: NT_write_all_frwds\n");
426 }
427 if(old_object != NULL && new_object != NULL){/* it was an update */
428 e_mail_list = NT_gather_frwd_addresses(old_object);
429 NT_add_to_frwd_hash_list(forwd_hash, e_mail_list);
430 NT_add_to_ntfy_list(e_mail_list, forwd_hash, "----\nUPDATE REQUESTED FOR:\n\n");
431 //NT_add_to_ntfy_list(e_mail_list, forwd_hash, old_object);
432 //NT_add_to_ntfy_list(e_mail_list, forwd_hash, "\n\n");
433 NT_add_to_ntfy_list(e_mail_list, forwd_hash, new_object);
434 }else if(old_object == NULL && new_object != NULL){/* it was a creation */
435 e_mail_list = NT_gather_frwd_addresses(new_object);
436 NT_add_to_frwd_hash_list(forwd_hash, e_mail_list);
437 NT_add_to_ntfy_list(e_mail_list, forwd_hash, "----\nADDITION REQUESTED FOR:\n\n");
438 NT_add_to_ntfy_list(e_mail_list, forwd_hash, new_object);
439 }else if(old_object != NULL && new_object == NULL){/* it was a deletion */
440 e_mail_list = NT_gather_frwd_addresses(old_object);
441 NT_add_to_frwd_hash_list(forwd_hash, e_mail_list);
442 NT_add_to_ntfy_list(e_mail_list, forwd_hash, "----\nDELETION REQUESTED FOR:\n\n");
443 NT_add_to_ntfy_list(e_mail_list, forwd_hash, old_object);
444 }
445 }
446