Debian Codegraph @

agent/mibgroup/mibII/mta_sendmail.c

raw
1 /*
2 * MTA-MIB implementation for sendmail - mibII/mta_sendmail.c
3 * Christoph Mammitzsch <[email protected]>
4 *
5 * todo: put queue directory into description?
6 *
7 * 13.02.2002:
8 * - support sendmail 8.12 queue groups
9 *
10 *
11 * 05.04.2000:
12 *
13 * - supports sendmail 8.10.0 statistics files now
14 * - function read_option has been removed
15 *
16 * 12.04.2000:
17 *
18 * - renamed configuration tokens:
19 * sendmail config -> sendmail_config
20 * sendmail stats -> sendmail_stats
21 * sendmail queue -> sendmail_queue
22 * sendmail index -> sendmail_index
23 * sendmail statcachetime -> sendmail_stats_t
24 * sendmail dircacetime -> sendmail_queue_t
25 *
26 * - now using snmpd_register_config_handler instead of config_parse_dot_conf
27 *
28 * 15.04.2000:
29 *
30 * - introduced new function print_error
31 * - changed open_sendmailst and read_sendmailcf to use the new function
32 * - changed calls to open_sendmailst and read_sendmailcf
33 * - added some error handling to calls to chdir(), close() and closedir()
34 *
35 */
36
37
38 /** "include files" */
39 #ifdef __lint
40 # define NETSNMP_NO_DEBUGGING 1 /* keeps lint from complaining about the DEBUGMSG* macros */
41 #endif
42
43 #include <net-snmp/net-snmp-config.h>
44
45 #include <net-snmp/net-snmp-includes.h>
46 #include <net-snmp/agent/net-snmp-agent-includes.h>
47
48 #include "mta_sendmail.h"
49
50 #include <sys/types.h>
51
52 #include <stdio.h>
53
54 #include <ctype.h>
55
56 #ifdef HAVE_STRING_H
57 # include <string.h>
58 #else
59 # include <strings.h>
60 #endif
61
62 #ifdef HAVE_STDLIB_H
63 #include <stdlib.h>
64 #endif
65
66 #ifdef HAVE_UNISTD_H
67 # include <unistd.h>
68 #endif
69
70 #ifdef HAVE_FCNTL_H
71 # include <fcntl.h>
72 #endif
73
74 #ifdef HAVE_DIRENT_H
75 #include <dirent.h>
76 #else
77 # define dirent direct
78 # ifdef HAVE_SYS_NDIR_H
79 # include <sys/ndir.h>
80 # endif
81 # ifdef HAVE_SYS_DIR_H
82 # include <sys/dir.h>
83 # endif
84 # ifdef HAVE_NDIR_H
85 # include <ndir.h>
86 # endif
87 #endif
88
89 #ifdef HAVE_SYS_STAT_H
90 # include <sys/stat.h>
91 #endif
92
93 #ifdef TIME_WITH_SYS_TIME
94 # include <sys/time.h>
95 # include <time.h>
96 #else
97 # ifdef HAVE_SYS_TIME_H
98 # include <sys/time.h>
99 # else
100 # include <time.h>
101 # endif
102 #endif
103
104 #include <errno.h>
105 #include <stdarg.h>
106
107 /**/
108 /** "macros and variables for registering the OID tree" */
109 /*
110 * prefix for all OIDs
111 */
112
113 static FindVarMethod var_mtaEntry;
114 static FindVarMethod var_mtaGroupEntry;
115
116 static oid mta_variables_oid[] = { 1, 3, 6, 1, 2, 1, 28 };
117
118 /*
119 * bits that indicate what's needed to compute the value
120 */
121 #define NEEDS_STATS (1 << 6)
122 #define NEEDS_DIR (1 << 7)
123 #define NEEDS (NEEDS_STATS | NEEDS_DIR)
124
125 /*
126 * symbolic names for the magic values
127 */
128 enum {
129 MTARECEIVEDMESSAGES = 3 | NEEDS_STATS,
130 MTASTOREDMESSAGES = 4 | NEEDS_DIR,
131 MTATRANSMITTEDMESSAGES = 5 | NEEDS_STATS,
132 MTARECEIVEDVOLUME = 6 | NEEDS_STATS,
133 MTASTOREDVOLUME = 7 | NEEDS_DIR,
134 MTATRANSMITTEDVOLUME = 8 | NEEDS_STATS,
135 MTAGROUPSTOREDMESSAGES = 17 | NEEDS_DIR,
136 MTAGROUPSTOREDVOLUME = 18 | NEEDS_DIR,
137 MTAGROUPRECEIVEDMESSAGES = 19 | NEEDS_STATS,
138 MTAGROUPREJECTEDMESSAGES = 20 | NEEDS_STATS,
139 MTAGROUPTRANSMITTEDMESSAGES = 22 | NEEDS_STATS,
140 MTAGROUPRECEIVEDVOLUME = 23 | NEEDS_STATS,
141 MTAGROUPTRANSMITTEDVOLUME = 25 | NEEDS_STATS,
142 MTAGROUPNAME = 43,
143 MTAGROUPHIERARCHY = 49
144 };
145
146 /*
147 * structure that tells the agent, which function returns what values
148 */
149 static struct variable3 mta_variables[] = {
150 {MTARECEIVEDMESSAGES, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
151 var_mtaEntry, 3, {1, 1, 1}},
152 {MTASTOREDMESSAGES, ASN_GAUGE, NETSNMP_OLDAPI_RONLY,
153 var_mtaEntry, 3, {1, 1, 2}},
154 {MTATRANSMITTEDMESSAGES, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
155 var_mtaEntry, 3, {1, 1, 3}},
156 {MTARECEIVEDVOLUME, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
157 var_mtaEntry, 3, {1, 1, 4}},
158 {MTASTOREDVOLUME, ASN_GAUGE, NETSNMP_OLDAPI_RONLY,
159 var_mtaEntry, 3, {1, 1, 5}},
160 {MTATRANSMITTEDVOLUME, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
161 var_mtaEntry, 3, {1, 1, 6}},
162
163 {MTAGROUPRECEIVEDMESSAGES, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
164 var_mtaGroupEntry, 3, {2, 1, 2}},
165 {MTAGROUPREJECTEDMESSAGES, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
166 var_mtaGroupEntry, 3, {2, 1, 3}},
167 {MTAGROUPSTOREDMESSAGES, ASN_GAUGE, NETSNMP_OLDAPI_RONLY,
168 var_mtaGroupEntry, 3, {2, 1, 4}},
169 {MTAGROUPTRANSMITTEDMESSAGES, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
170 var_mtaGroupEntry, 3, {2, 1, 5}},
171 {MTAGROUPRECEIVEDVOLUME, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
172 var_mtaGroupEntry, 3, {2, 1, 6}},
173 {MTAGROUPSTOREDVOLUME, ASN_GAUGE, NETSNMP_OLDAPI_RONLY,
174 var_mtaGroupEntry, 3, {2, 1, 7}},
175 {MTAGROUPTRANSMITTEDVOLUME, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
176 var_mtaGroupEntry, 3, {2, 1, 8}},
177 {MTAGROUPNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
178 var_mtaGroupEntry, 3, {2, 1, 25}},
179 {MTAGROUPHIERARCHY, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
180 var_mtaGroupEntry, 3, {2, 1, 31}}
181 };
182 /**/
183 /** "other macros and structures" */
184 /*
185 * for boolean values
186 */
187 #ifndef FALSE
188 #define FALSE 0
189 #endif
190 #ifndef TRUE
191 #define TRUE 1
192 #endif
193 #ifndef BOOL
194 #define BOOL short
195 #endif
196 /*
197 * important constants
198 */
199 #define FILENAMELEN 200 /* maximum length for filenames */
200 #define MAXMAILERS 25 /* maximum number of mailers (copied from the sendmail sources) */
201 #define MAXQUEUEGROUPS 50 /* maximum # of queue groups (copied from sendmail) */
202 #define MNAMELEN 20 /* maximum length of mailernames (copied from the sendmail sources) */
203 #define STAT_VERSION_8_9 2 /* version of sendmail V8.9.x statistics files (copied from the sendmail sources) */
204 #define STAT_VERSION_8_10 3 /* version of sendmail V8.10.x statistics files (copied from the sendmail sources) */
205 #define STAT_VERSION_8_12_QUAR 4 /* version of sendmail V8.12.x statistics files using -D_FFR_QUARANTINE (commercial and edge-living opensource*/
206 #define STAT_MAGIC 0x1B1DE /* magic value to identify statistics files from sendmail V8.9.x or higher (copied from the sendmail sources) */
207 /*
208 * structure of sendmail.st file from sendmail V8.10.x (copied from the sendmail sources)
209 */
210
211 struct statisticsV8_12_QUAR {
212 int stat_magic; /* magic number */
213 int stat_version; /* stat file version */
214 time_t stat_itime; /* file initialization time */
215 short stat_size; /* size of this structure */
216 long stat_cf; /* # from connections */
217 long stat_ct; /* # to connections */
218 long stat_cr; /* # rejected connections */
219 long stat_nf[MAXMAILERS]; /* # msgs from each mailer */
220 long stat_bf[MAXMAILERS]; /* kbytes from each mailer */
221 long stat_nt[MAXMAILERS]; /* # msgs to each mailer */
222 long stat_bt[MAXMAILERS]; /* kbytes to each mailer */
223 long stat_nr[MAXMAILERS]; /* # rejects by each mailer */
224 long stat_nd[MAXMAILERS]; /* # discards by each mailer */
225 long stat_nq[MAXMAILERS]; /* # quarantines by each mailer*/
226 };
227
228 struct statisticsV8_10 {
229 int stat_magic; /* magic number */
230 int stat_version; /* stat file version */
231 time_t stat_itime; /* file initialization time */
232 short stat_size; /* size of this structure */
233 long stat_cf; /* # from connections */
234 long stat_ct; /* # to connections */
235 long stat_cr; /* # rejected connections */
236 long stat_nf[MAXMAILERS]; /* # msgs from each mailer */
237 long stat_bf[MAXMAILERS]; /* kbytes from each mailer */
238 long stat_nt[MAXMAILERS]; /* # msgs to each mailer */
239 long stat_bt[MAXMAILERS]; /* kbytes to each mailer */
240 long stat_nr[MAXMAILERS]; /* # rejects by each mailer */
241 long stat_nd[MAXMAILERS]; /* # discards by each mailer */
242
243 };
244
245 /*
246 * structure of sendmail.st file from sendmail V8.9.x (copied from the sendmail sources)
247 */
248 struct statisticsV8_9 {
249 int stat_magic; /* magic number */
250 int stat_version; /* stat file version */
251 time_t stat_itime; /* file initialization time */
252 short stat_size; /* size of this structure */
253 long stat_nf[MAXMAILERS]; /* # msgs from each mailer */
254 long stat_bf[MAXMAILERS]; /* kbytes from each mailer */
255 long stat_nt[MAXMAILERS]; /* # msgs to each mailer */
256 long stat_bt[MAXMAILERS]; /* kbytes to each mailer */
257 long stat_nr[MAXMAILERS]; /* # rejects by each mailer */
258 long stat_nd[MAXMAILERS]; /* # discards by each mailer */
259 };
260
261 /*
262 * structure of sendmail.st file from sendmail V8.8.x (copied from the sendmail sources)
263 */
264 struct statisticsV8_8 {
265 time_t stat_itime; /* file initialization time */
266 short stat_size; /* size of this structure */
267 long stat_nf[MAXMAILERS]; /* # msgs from each mailer */
268 long stat_bf[MAXMAILERS]; /* kbytes from each mailer */
269 long stat_nt[MAXMAILERS]; /* # msgs to each mailer */
270 long stat_bt[MAXMAILERS]; /* kbytes to each mailer */
271 };
272 /**/
273 /*
274 * queue groups (strictly a sendmail 8.12+ thing
275 */
276 struct QDir {
277 char *dir;
278 struct QDir *next;
279 };
280
281 struct QGrp {
282 char *name; /* name of queuegroup */
283
284 time_t last; /* last time we counted */
285 int count; /* # of files */
286 int size; /* size of files */
287
288 struct QDir *dirs; /* directories in queue group */
289 };
290
291 /** "static variables" */
292
293 /*
294 * a list of all the queue groups, NULL terminated
295 */
296 static struct QGrp qgrps[MAXQUEUEGROUPS];
297 static int nqgrps = 0;
298
299 static char sendmailst_fn[FILENAMELEN + 1]; /* name of statistics file */
300 static int sendmailst_fh = -1; /* filehandle for statistics file */
301 static char sendmailcf_fn[FILENAMELEN + 1]; /* name of sendmails config file */
302 static char mailernames[MAXMAILERS][MNAMELEN + 1]; /* array of mailer names */
303 static int mailers = MAXMAILERS; /* number of mailer names in array */
304
305 static long *stat_nf; /* pointer to stat_nf array within the statistics structure */
306 static long *stat_bf; /* pointer to stat_bf array within the statistics structure */
307 static long *stat_nt; /* pointer to stat_nt array within the statistics structure */
308 static long *stat_bt; /* pointer to stat_bt array within the statistics structure */
309 static long *stat_nr; /* pointer to stat_nr array within the statistics structure,
310 * only valid for statistics files from sendmail >=V8.9.0 */
311 static long *stat_nd; /* pointer to stat_nd array within the statistics structure,
312 * only valid for statistics files from sendmail >=V8.9.0 */
313 static int stats_size; /* size of statistics structure */
314 static long stats[sizeof(struct statisticsV8_12_QUAR) / sizeof(long) + 1]; /* buffer for statistics structure */
315 static time_t lastreadstats; /* time stats file has been read */
316 static long applindex = 1; /* ApplIndex value for OIDs */
317 static long stat_cache_time = 5; /* time (in seconds) to wait before reading stats file again */
318 static long dir_cache_time = 10; /* time (in seconds) to wait before scanning queue directoy again */
319
320 /**/
321 /** static void print_error(int priority, BOOL config, BOOL config_only, char *function, char *format, ...)
322 *
323 * Description:
324 *
325 * Called to print errors. It uses the config_perror or the snmp_log function
326 * depending on whether the config parameter is TRUE or FALSE.
327 *
328 * Parameters:
329 *
330 * priority: priority to be used when calling the snmp_log function
331 *
332 * config: indicates whether this function has been called during the
333 * configuration process or not. If set to TRUE, the function
334 * config_perror will be used to report the error.
335 *
336 * config_only: if set to TRUE, the error will only be printed when function
337 * has been called during the configuration process.
338 *
339 * function: name of the calling function. Used when printing via snmp_log.
340 *
341 * format: format string for the error message
342 *
343 * ...: additional parameters to insert into the error message string
344 *
345 */
346 static void
347 print_error(int priority, BOOL config, BOOL config_only,
348 const char *function, const char *format, ...)
349 {
350 va_list ap;
351 char buffer[2 * FILENAMELEN + 200]; /* I know, that's not perfectly safe, but since I don't use more
352 * than two filenames in one error message, that should be enough */
353
354 va_start(ap, format);
355 vsnprintf(buffer, sizeof(buffer), format, ap);
356
357 if (config) {
358 config_perror(buffer);
359 } else if (!config_only) {
360 snmp_log(priority, "%s: %s\n", function, buffer);
361 }
362 va_end(ap);
363 }
364
365 /**/
366 /** static void open_sendmailst(BOOL config)
367 *
368 * Description:
369 *
370 * Closes old sendmail.st file, then tries to open the new sendmail.st file
371 * and guess it's version. If it succeeds, it initializes the stat_*
372 * pointers and the stats_size variable.
373 *
374 * Parameters:
375 *
376 * config: TRUE if function has been called during the configuration process
377 *
378 * Returns:
379 *
380 * nothing
381 *
382 */
383 static void
384 open_sendmailst(BOOL config)
385 {
386 int filelen;
387
388 if (sendmailst_fh != -1) {
389 while (close(sendmailst_fh) == -1 && errno == EINTR) {
390 /*
391 * do nothing
392 */
393 }
394 }
395
396 sendmailst_fh = open(sendmailst_fn, O_RDONLY);
397
398 if (sendmailst_fh == -1) {
399 print_error(LOG_ERR, config, TRUE,
400 "mibII/mta_sendmail.c:open_sendmailst",
401 "could not open file \"%s\"", sendmailst_fn);
402 return;
403 }
404
405 filelen = read(sendmailst_fh, (void *) &stats, sizeof stats);
406
407 if (((struct statisticsV8_10 *) stats)->stat_magic == STAT_MAGIC) {
408
409 if (((struct statisticsV8_12_QUAR *) stats)->stat_version ==
410 STAT_VERSION_8_12_QUAR
411 && ((struct statisticsV8_12_QUAR *) stats)->stat_size ==
412 sizeof(struct statisticsV8_12_QUAR)
413 && filelen == sizeof(struct statisticsV8_12_QUAR)) {
414 DEBUGMSGTL(("mibII/mta_sendmail.c:open_sendmailst",
415 "looks like file \"%s\" has been created by sendmail V8.10.0 or newer\n",
416 sendmailst_fn));
417 stat_nf = (((struct statisticsV8_12_QUAR *) stats)->stat_nf);
418 stat_bf = (((struct statisticsV8_12_QUAR *) stats)->stat_bf);
419 stat_nt = (((struct statisticsV8_12_QUAR *) stats)->stat_nt);
420 stat_bt = (((struct statisticsV8_12_QUAR *) stats)->stat_bt);
421 stat_nr = (((struct statisticsV8_12_QUAR *) stats)->stat_nr);
422 stat_nd = (((struct statisticsV8_12_QUAR *) stats)->stat_nd);
423 stats_size = sizeof(struct statisticsV8_12_QUAR);
424 } else
425
426 if (((struct statisticsV8_10 *) stats)->stat_version ==
427 STAT_VERSION_8_10
428 && ((struct statisticsV8_10 *) stats)->stat_size ==
429 sizeof(struct statisticsV8_10)
430 && filelen == sizeof(struct statisticsV8_10)) {
431 DEBUGMSGTL(("mibII/mta_sendmail.c:open_sendmailst",
432 "looks like file \"%s\" has been created by sendmail V8.10.0 or newer\n",
433 sendmailst_fn));
434 stat_nf = (((struct statisticsV8_10 *) stats)->stat_nf);
435 stat_bf = (((struct statisticsV8_10 *) stats)->stat_bf);
436 stat_nt = (((struct statisticsV8_10 *) stats)->stat_nt);
437 stat_bt = (((struct statisticsV8_10 *) stats)->stat_bt);
438 stat_nr = (((struct statisticsV8_10 *) stats)->stat_nr);
439 stat_nd = (((struct statisticsV8_10 *) stats)->stat_nd);
440 stats_size = sizeof(struct statisticsV8_10);
441
442 } else if (((struct statisticsV8_9 *) stats)->stat_version ==
443 STAT_VERSION_8_9
444 && ((struct statisticsV8_9 *) stats)->stat_size ==
445 sizeof(struct statisticsV8_9)
446 && filelen == sizeof(struct statisticsV8_9)) {
447 DEBUGMSGTL(("mibII/mta_sendmail.c:open_sendmailst",
448 "looks like file \"%s\" has been created by sendmail V8.9.x\n",
449 sendmailst_fn));
450 stat_nf = (((struct statisticsV8_9 *) stats)->stat_nf);
451 stat_bf = (((struct statisticsV8_9 *) stats)->stat_bf);
452 stat_nt = (((struct statisticsV8_9 *) stats)->stat_nt);
453 stat_bt = (((struct statisticsV8_9 *) stats)->stat_bt);
454 stat_nr = (((struct statisticsV8_9 *) stats)->stat_nr);
455 stat_nd = (((struct statisticsV8_9 *) stats)->stat_nd);
456 stats_size = sizeof(struct statisticsV8_9);
457 } else {
458 print_error(LOG_WARNING, config, FALSE,
459 "mibII/mta_sendmail.c:open_sendmailst",
460 "could not guess version of statistics file \"%s\"",
461 sendmailst_fn);
462 while (close(sendmailst_fh) == -1 && errno == EINTR) {
463 /*
464 * do nothing
465 */
466 }
467 sendmailst_fh = -1;
468 }
469 } else {
470 if (((struct statisticsV8_8 *) stats)->stat_size ==
471 sizeof(struct statisticsV8_8)
472 && filelen == sizeof(struct statisticsV8_8)) {
473 DEBUGMSGTL(("mibII/mta_sendmail.c:open_sendmailst",
474 "looks like file \"%s\" has been created by sendmail V8.8.x\n",
475 sendmailst_fn));
476 stat_nf = (((struct statisticsV8_8 *) stats)->stat_nf);
477 stat_bf = (((struct statisticsV8_8 *) stats)->stat_bf);
478 stat_nt = (((struct statisticsV8_8 *) stats)->stat_nt);
479 stat_bt = (((struct statisticsV8_8 *) stats)->stat_bt);
480 stat_nr = (long *) NULL;
481 stat_nd = (long *) NULL;
482 stats_size = sizeof(struct statisticsV8_8);
483 } else {
484 print_error(LOG_WARNING, config, FALSE,
485 "mibII/mta_sendmail.c:open_sendmailst",
486 "could not guess version of statistics file \"%s\"",
487 sendmailst_fn);
488 while (close(sendmailst_fh) == -1 && errno == EINTR) {
489 /*
490 * do nothing
491 */
492 }
493 sendmailst_fh = -1;
494 }
495 }
496 }
497
498 /**/ static void
499 count_queuegroup(struct QGrp *qg)
500 {
501 struct QDir *d;
502 char cwd[SNMP_MAXPATH];
503 time_t current_time = time(NULL);
504
505 if (current_time <= (qg->last + dir_cache_time)) {
506 return;
507 }
508
509 if (getcwd(cwd, sizeof cwd) == NULL) {
510 snmp_log(LOG_ERR,
511 "mibII/mta_sendmail.c:count_queuegroup: could not get current working directory\n");
512 return;
513 }
514
515 qg->count = 0;
516 qg->size = 0;
517
518 for (d = qg->dirs; d != NULL; d = d->next) {
519 DIR *dp;
520 struct dirent *dirp;
521 struct stat filestat;
522
523 if (chdir(d->dir) != 0)
524 continue;
525 dp = opendir(".");
526 if (dp == NULL)
527 continue;
528 while ((dirp = readdir(dp)) != NULL) {
529 if (dirp->d_name[0] == 'd' && dirp->d_name[1] == 'f') {
530 if (stat(dirp->d_name, &filestat) == 0) {
531 qg->size += (filestat.st_size + 999) / 1000;
532 }
533 } else if (dirp->d_name[0] == 'q' && dirp->d_name[1] == 'f') {
534 qg->count++;
535 }
536 }
537 closedir(dp);
538 }
539
540 qg->last = current_time;
541
542 NETSNMP_IGNORE_RESULT(chdir(cwd));
543 }
544
545 /** static void add_queuegroup(const char *name, const char *path)
546 *
547 * Description:
548 *
549 * Adds a queuegroup of 'name' with root path 'path' to the static
550 * list of queue groups. if 'path' ends in a *, we expand it out to
551 * all matching subdirs. also look for 'qf' subdirectories.
552 *
553 * Parameters:
554 *
555 * qgname: name of the queuegroup discovered
556 * path: path of queuegroup discovered
557 */
558 static void
559 add_queuegroup(const char *name, char *path)
560 {
561 char parentdir[FILENAMELEN];
562 char *p;
563 struct QDir *new = NULL;
564 struct QDir *subdir = NULL;
565 DIR *dp;
566 struct dirent *dirp;
567
568 if (nqgrps == MAXQUEUEGROUPS) {
569 /*
570 * xxx error
571 */
572 return;
573 }
574
575 if (strlen(path) > FILENAMELEN - 10) {
576 /*
577 * xxx error
578 */
579 return;
580 }
581
582 p = path + strlen(path) - 1;
583 if (*p == '*') { /* multiple queue dirs */
584 /*
585 * remove *
586 */
587 *p = '\0';
588
589 strlcpy(parentdir, path, sizeof(parentdir));
590 /*
591 * remove last directory component from parentdir
592 */
593 for (p = parentdir + strlen(parentdir) - 1; p >= parentdir; p--) {
594 if (*p == '/') {
595 *p = '\0';
596 break;
597 }
598 }
599
600 if (p < parentdir) {
601 /*
602 * no trailing / ?!?
603 */
604
605 /*
606 * xxx error
607 */
608 return;
609 }
610 p++;
611
612 /*
613 * p is now the prefix we need to match
614 */
615 if ((dp = opendir(parentdir)) == NULL) {
616 /*
617 * xxx can't open parentdir
618 */
619 return;
620 }
621
622 while ((dirp = readdir(dp)) != NULL) {
623 if (!strncmp(dirp->d_name, p, strlen(p)) &&
624 dirp->d_name[0] != '.') {
625 /*
626 * match, add it to the list
627 */
628
629 /*
630 * single queue directory
631 */
632 if ((subdir = calloc(1, sizeof(*subdir))) != NULL &&
633 asprintf(&subdir->dir, "%s/%s", parentdir, dirp->d_name) >=
634 0) {
635 subdir->next = new;
636 new = subdir;
637 } else {
638 free(subdir);
639 }
640 }
641 }
642
643 closedir(dp);
644 } else {
645 /*
646 * single queue directory
647 */
648 new = malloc(sizeof(*new));
649 new->dir = strdup(path);
650 new->next = NULL;
651 }
652
653 /*
654 * check 'new' for /qf directories
655 */
656 for (subdir = new; subdir != NULL; subdir = subdir->next) {
657 char *qf = NULL;
658
659 if (asprintf(&qf, "%s/qf", subdir->dir) >= 0 &&
660 (dp = opendir(qf)) != NULL) {
661 /*
662 * it exists !
663 */
664 free(subdir->dir);
665 subdir->dir = qf;
666 qf = NULL;
667 closedir(dp);
668 }
669 free(qf);
670 }
671
672 /*
673 * we now have the list of directories in 'new'; create the queuegroup
674 * object
675 */
676 qgrps[nqgrps].name = strdup(name);
677 qgrps[nqgrps].last = 0;
678 qgrps[nqgrps].count = 0;
679 qgrps[nqgrps].size = 0;
680 qgrps[nqgrps].dirs = new;
681
682 nqgrps++;
683 }
684
685 /** static BOOL read_sendmailcf(BOOL config)
686 *
687 * Description:
688 *
689 * Tries to open the file named in sendmailcf_fn and to get the names of
690 * the mailers, the status file and the mailqueue directories.
691 *
692 * Parameters:
693 *
694 * config: TRUE if function has been called during the configuration process
695 *
696 * Returns:
697 *
698 * TRUE : config file has been successfully opened
699 *
700 * FALSE : could not open config file
701 *
702 */
703
704 static BOOL
705 read_sendmailcf(BOOL config)
706 {
707 FILE *sendmailcf_fp;
708 char line[500];
709 char *filename;
710 char *qgname, *p;
711 int linenr;
712 int linelen;
713 int found_sendmailst = FALSE;
714 int i;
715
716
717 sendmailcf_fp = fopen(sendmailcf_fn, "r");
718 if (sendmailcf_fp == NULL) {
719 print_error(LOG_ERR, config, TRUE,
720 "mibII/mta_sendmail.c:read_sendmailcf",
721 "could not open file \"%s\"", sendmailcf_fn);
722 return FALSE;
723 }
724
725 /*
726 * initializes the standard mailers, which aren't necessarily mentioned in the sendmail.cf file
727 */
728 strcpy(mailernames[0], "prog");
729 strcpy(mailernames[1], "*file*");
730 strcpy(mailernames[2], "*include*");
731 mailers = 3;
732
733 /*
734 * reset queuegroups
735 */
736
737 linenr = 1;
738 while (fgets(line, sizeof line, sendmailcf_fp) != NULL) {
739 linelen = strlen(line);
740
741 if (line[linelen - 1] != '\n') {
742 print_error(LOG_WARNING, config, FALSE,
743 "mibII/mta_sendmail.c:read_sendmailcf",
744 "line %d in config file \"%s is too long\n",
745 linenr, sendmailcf_fn);
746 while (fgets(line, sizeof line, sendmailcf_fp) != NULL && line[strlen(line) - 1] != '\n') { /* skip rest of the line */
747 /*
748 * nothing to do
749 */
750 }
751 linenr++;
752 continue;
753 }
754
755 line[--linelen] = '\0';
756
757 switch (line[0]) {
758
759 case 'M':
760
761 if (mailers < MAXMAILERS) {
762 for (i = 1;
763 line[i] != ',' && !isspace(line[i] & 0xFF) && line[i] != '\0'
764 && i <= MNAMELEN; i++) {
765 mailernames[mailers][i - 1] = line[i];
766 }
767 mailernames[mailers][i - 1] = '\0';
768
769 DEBUGMSGTL(("mibII/mta_sendmail.c:read_sendmailcf",
770 "found mailer \"%s\"\n",
771 mailernames[mailers]));
772
773 for (i = 0;
774 i < mailers
775 && strcmp(mailernames[mailers], mailernames[i]) != 0;
776 i++) {
777 /*
778 * nothing to do
779 */
780 }
781
782 if (i == mailers) {
783 mailers++;
784 } else {
785 if (i < 3) {
786 DEBUGMSGTL(("mibII/mta_sendmail.c:read_sendmailcf",
787 "mailer \"%s\" already existed, but since it's one of the predefined mailers, that's probably nothing to worry about\n",
788 mailernames[mailers]));
789 } else {
790 DEBUGMSGTL(("mibII/mta_sendmail.c:read_sendmailcf",
791 "mailer \"%s\" already existed\n",
792 mailernames[mailers]));
793 }
794 mailernames[mailers][0] = '\0';
795 }
796 } else {
797 print_error(LOG_WARNING, config, FALSE,
798 "mibII/mta_sendmail.c:read_sendmailcf",
799 "found too many mailers in config file \"%s\"",
800 sendmailcf_fn);
801 }
802
803
804 break;
805
806 case 'O':
807 switch (line[1]) {
808 case ' ':
809 /*
810 * long option
811 */
812 if (strncasecmp(line + 2, "StatusFile", 10) == 0) {
813 filename = line + 12;
814 } else if (strncasecmp(line + 2, "QueueDirectory", 14) ==
815 0) {
816 filename = line + 16;
817 } else {
818 /*
819 * not an option we care about
820 */
821 break;
822 }
823
824 /*
825 * make sure it's the end of the option
826 */
827 if (*filename != ' ' && *filename != '=')
828 break;
829
830 /*
831 * skip WS
832 */
833 while (*filename == ' ')
834 filename++;
835
836 /*
837 * must be O <option> = <file>
838 */
839 if (*filename++ != '=') {
840 print_error(LOG_WARNING, config, FALSE,
841 "mibII/mta_sendmail.c:read_sendmailcf",
842 "line %d in config file \"%s\" ist missing an '='",
843 linenr, sendmailcf_fn);
844 break;
845 }
846
847 /*
848 * skip WS
849 */
850 while (*filename == ' ')
851 filename++;
852
853 if (strlen(filename) > FILENAMELEN) {
854 print_error(LOG_WARNING, config, FALSE,
855 "mibII/mta_sendmail.c:read_sendmailcf",
856 "line %d config file \"%s\" contains a filename that's too long",
857 linenr, sendmailcf_fn);
858 break;
859 }
860
861 if (strncasecmp(line + 2, "StatusFile", 10) == 0) {
862 strlcpy(sendmailst_fn, filename, sizeof(sendmailst_fn));
863 found_sendmailst = TRUE;
864 DEBUGMSGTL(("mibII/mta_sendmail.c:read_sendmailcf",
865 "found statatistics file \"%s\"\n",
866 sendmailst_fn));
867 } else if (strncasecmp(line + 2, "QueueDirectory", 14) ==
868 0) {
869 add_queuegroup("mqueue", filename);
870 } else {
871 print_error(LOG_CRIT, config, FALSE,
872 "mibII/mta_sendmail.c:read_sendmailcf",
873 "This shouldn't happen.");
874 abort();
875 }
876 break;
877
878 case 'S':
879 if (strlen(line + 2) > FILENAMELEN) {
880 print_error(LOG_WARNING, config, FALSE,
881 "mibII/mta_sendmail.c:read_sendmailcf",
882 "line %d config file \"%s\" contains a filename that's too long",
883 linenr, sendmailcf_fn);
884 break;
885 }
886 strlcpy(sendmailst_fn, line + 2, sizeof(sendmailst_fn));
887 found_sendmailst = TRUE;
888 DEBUGMSGTL(("mibII/mta_sendmail.c:read_sendmailcf",
889 "found statatistics file \"%s\"\n",
890 sendmailst_fn));
891 break;
892
893 case 'Q':
894 if (strlen(line + 2) > FILENAMELEN) {
895 print_error(LOG_WARNING, config, FALSE,
896 "mibII/mta_sendmail.c:read_sendmailcf",
897 "line %d config file \"%s\" contains a filename that's too long",
898 linenr, sendmailcf_fn);
899 break;
900 }
901
902 add_queuegroup("mqueue", line + 2);
903 break;
904 }
905 break;
906
907 case 'Q':
908 /*
909 * found a queue group
910 */
911 p = qgname = line + 1;
912 while (*p && *p != ',') {
913 p++;
914 }
915 if (*p == '\0') {
916 print_error(LOG_WARNING, config, FALSE,
917 "mibII/mta_sendmail.c:read_sendmailcf",
918 "line %d config file \"%s\" contains a weird queuegroup",
919 linenr, sendmailcf_fn);
920 break;
921 }
922
923 /*
924 * look for the directory
925 */
926 filename = NULL;
927 *p++ = '\0';
928 while (*p != '\0') {
929 /*
930 * skip WS
931 */
932 while (*p && *p == ' ')
933 p++;
934
935 if (*p == 'P') { /* found path */
936 while (*p && *p != '=')
937 p++;
938 if (*p++ != '=') {
939 print_error(LOG_WARNING, config, FALSE,
940 "mibII/mta_sendmail.c:read_sendmailcf",
941 "line %d config file \"%s\" contains a weird queuegroup",
942 linenr, sendmailcf_fn);
943 break;
944 }
945 filename = p;
946
947 /*
948 * find next ',', turn into \0
949 */
950 while (*p && *p != ',')
951 p++;
952 *p = '\0';
953 }
954
955 /*
956 * skip to one past the next ,
957 */
958 while (*p && *p != ',') {
959 p++;
960 }
961 if (*p)
962 p++;
963 }
964
965 /*
966 * we found a directory
967 */
968 if (filename) {
969 add_queuegroup(qgname, filename);
970 } else {
971 print_error(LOG_WARNING, config, FALSE,
972 "mibII/mta_sendmail.c:read_sendmailcf",
973 "line %d config file \"%s\" contains a weird queuegroup: no directory",
974 linenr, sendmailcf_fn);
975 }
976
977 break;
978 }
979
980 linenr++;
981 }
982
983 fclose(sendmailcf_fp);
984
985 for (i = mailers; i < MAXMAILERS; i++) {
986 mailernames[i][0] = '\0';
987 }
988
989 if (found_sendmailst) {
990 open_sendmailst(config);
991 }
992
993 return TRUE;
994 }
995
996 /**/
997 /** static void mta_sendmail_parse_config(const char* token, char *line)
998 *
999 * Description:
1000 *
1001 * Called by the agent for each configuration line that belongs to this module.
1002 * The possible tokens are:
1003 *
1004 * sendmail_config - filename of the sendmail configutarion file
1005 * sendmail_stats - filename of the sendmail statistics file
1006 * sendmail_queue - name of the sendmail mailqueue directory
1007 * sendmail_index - the ApplIndex to use for the table
1008 * sendmail_stats_t - the time (in seconds) to cache statistics
1009 * sendmail_queue_t - the time (in seconds) to cache the directory scanning results
1010 *
1011 * For "sendmail_config", "sendmail_stats" and "sendmail_queue", the copy_nword
1012 * function is used to copy the filename.
1013 *
1014 * Parameters:
1015 *
1016 * token: first word of the line
1017 *
1018 * line: rest of the line
1019 *
1020 * Returns:
1021 *
1022 * nothing
1023 *
1024 */
1025 static void
1026 mta_sendmail_parse_config(const char *token, char *line)
1027 {
1028 if (strlen(line) > FILENAMELEN) { /* Might give some false alarm, but better to be safe than sorry */
1029 config_perror("line too long");
1030 return;
1031 }
1032
1033 if (strcasecmp(token, "sendmail_stats") == 0) {
1034 while (isspace(*line & 0xFF)) {
1035 line++;
1036 }
1037 copy_nword(line, sendmailst_fn, sizeof(sendmailst_fn));
1038
1039 open_sendmailst(TRUE);
1040
1041 if (sendmailst_fh == -1) {
1042 netsnmp_config_error("couldn't open file \"%s\"", sendmailst_fn);
1043 return;
1044 }
1045
1046 DEBUGMSGTL(("mibII/mta_sendmail.c:mta_sendmail_parse_config",
1047 "opened statistics file \"%s\"\n", sendmailst_fn));
1048 return;
1049 } else if (strcasecmp(token, "sendmail_config") == 0) {
1050 while (isspace(*line & 0xFF)) {
1051 line++;
1052 }
1053 copy_nword(line, sendmailcf_fn, sizeof(sendmailcf_fn));
1054
1055 read_sendmailcf(TRUE);
1056
1057 DEBUGMSGTL(("mibII/mta_sendmail.c:mta_sendmail_parse_config",
1058 "read config file \"%s\"\n", sendmailcf_fn));
1059 return;
1060 } else if (strcasecmp(token, "sendmail_queue") == 0) {
1061 while (isspace(*line & 0xFF)) {
1062 line++;
1063 }
1064 add_queuegroup("mqueue", line);
1065
1066 return;
1067 } else if (strcasecmp(token, "sendmail_index") == 0) {
1068 while (isspace(*line & 0xFF)) {
1069 line++;
1070 }
1071 applindex = atol(line);
1072 if (applindex < 1) {
1073 config_perror("invalid index number");
1074 applindex = 1;
1075 }
1076 } else if (strcasecmp(token, "sendmail_stats_t") == 0) {
1077 while (isspace(*line & 0xFF)) {
1078 line++;
1079 }
1080 stat_cache_time = atol(line);
1081 if (stat_cache_time < 1) {
1082 config_perror("invalid cache time");
1083 applindex = 5;
1084 }
1085 } else if (strcasecmp(token, "sendmail_queue_t") == 0) {
1086 while (isspace(*line & 0xFF)) {
1087 line++;
1088 }
1089 dir_cache_time = atol(line);
1090 if (dir_cache_time < 1) {
1091 config_perror("invalid cache time");
1092 applindex = 10;
1093 }
1094 } else {
1095 config_perror
1096 ("mibII/mta_sendmail.c says: What should I do with that token? Did you ./configure the agent properly?");
1097 }
1098
1099 return;
1100 }
1101
1102 /**/
1103 /** void init_mta_sendmail(void)
1104 *
1105 * Description:
1106 *
1107 * Called by the agent to initialize the module. The function will register
1108 * the OID tree and the config handler and try some default values for the
1109 * sendmail.cf and sendmail.st files and for the mailqueue directory.
1110 *
1111 * Parameters:
1112 *
1113 * none
1114 *
1115 * Returns:
1116 *
1117 * nothing
1118 *
1119 */
1120 void
1121 init_mta_sendmail(void)
1122 {
1123 REGISTER_MIB("mibII/mta_sendmail", mta_variables, variable3,
1124 mta_variables_oid);
1125
1126 snmpd_register_config_handler("sendmail_config",
1127 mta_sendmail_parse_config, NULL, "file");
1128 snmpd_register_config_handler("sendmail_stats",
1129 mta_sendmail_parse_config, NULL, "file");
1130 snmpd_register_config_handler("sendmail_queue",
1131 mta_sendmail_parse_config, NULL,
1132 "directory");
1133 snmpd_register_config_handler("sendmail_index",
1134 mta_sendmail_parse_config, NULL,
1135 "integer");
1136 snmpd_register_config_handler("sendmail_stats_t",
1137 mta_sendmail_parse_config, NULL,
1138 "cachetime/sec");
1139 snmpd_register_config_handler("sendmail_queue_t",
1140 mta_sendmail_parse_config, NULL,
1141 "cachetime/sec");
1142
1143 strcpy(sendmailcf_fn, "/etc/mail/sendmail.cf");
1144 if (read_sendmailcf(FALSE) == FALSE) {
1145 strcpy(sendmailcf_fn, "/etc/sendmail.cf");
1146 read_sendmailcf(FALSE);
1147 }
1148
1149 if (sendmailst_fh == -1) {
1150 strcpy(sendmailst_fn, "/etc/mail/statistics");
1151 open_sendmailst(FALSE);
1152 if (sendmailst_fh == -1) {
1153 strcpy(sendmailst_fn, "/etc/mail/sendmail.st");
1154 open_sendmailst(FALSE);
1155 }
1156 }
1157
1158 }
1159
1160 /**/
1161 /** unsigned char *var_mtaEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method)
1162 *
1163 * Description:
1164 *
1165 * Called by the agent in order to get the values for the mtaTable.
1166 *
1167 * Parameters:
1168 *
1169 * see agent documentation
1170 *
1171 * Returns:
1172 *
1173 * see agent documentation
1174 *
1175 */
1176 unsigned char *
1177 var_mtaEntry(struct variable *vp,
1178 oid * name,
1179 size_t * length,
1180 int exact, size_t * var_len, WriteMethod ** write_method)
1181 {
1182
1183
1184 static long long_ret;
1185 auto int i;
1186 auto int result;
1187 auto time_t current_time;
1188 int global_count = 0;
1189 int global_size = 0;
1190
1191 if (exact) {
1192 if (*length != vp->namelen + 1) {
1193 return NULL;
1194 }
1195 result =
1196 snmp_oid_compare(name, *length - 1, vp->name, vp->namelen);
1197 if (result != 0 || name[*length - 1] != applindex) {
1198 return NULL;
1199 }
1200 } else {
1201 if (*length <= vp->namelen) {
1202 result = -1;
1203 } else {
1204 result =
1205 snmp_oid_compare(name, *length - 1, vp->name, vp->namelen);
1206 }
1207 if (result > 0) {
1208 return NULL;
1209 }
1210 if (result == 0 && name[*length - 1] >= applindex) {
1211 return NULL;
1212 }
1213 if (result < 0) {
1214 memcpy(name, vp->name, (int) vp->namelen * (int) sizeof *name);
1215 *length = vp->namelen + 1;
1216 }
1217 name[vp->namelen] = applindex;
1218 }
1219
1220 *write_method = (WriteMethod *) NULL;
1221 *var_len = sizeof(long); /* default to 'long' results */
1222
1223 if (vp->magic & NEEDS_STATS) {
1224 if (sendmailst_fh == -1)
1225 return NULL;
1226 current_time = time(NULL);
1227 if (current_time == (time_t) - 1
1228 || current_time > lastreadstats + stat_cache_time) {
1229 if (lseek(sendmailst_fh, 0, SEEK_SET) == -1) {
1230 snmp_log(LOG_ERR,
1231 "mibII/mta_sendmail.c:var_mtaEntry: could not rewind to the beginning of file \"%s\"\n",
1232 sendmailst_fn);
1233 return NULL;
1234 }
1235 if (read(sendmailst_fh, (void *) &stats, stats_size) !=
1236 stats_size) {
1237 snmp_log(LOG_ERR,
1238 "mibII/mta_sendmail.c:var_mtaEntry: could not read from statistics file \"%s\"\n",
1239 sendmailst_fn);
1240 return NULL;
1241 }
1242 if (current_time != (time_t) - 1) {
1243 lastreadstats = current_time;
1244 }
1245 }
1246 }
1247
1248 if (vp->magic & NEEDS_DIR) {
1249 global_count = 0;
1250 global_size = 0;
1251 /*
1252 * count all queue group messages
1253 */
1254 for (i = 0; i < nqgrps; i++) {
1255 count_queuegroup(&qgrps[i]);
1256 global_count += qgrps[i].count;
1257 global_size += qgrps[i].size;
1258 }
1259 }
1260
1261 switch (vp->magic) {
1262
1263 case MTARECEIVEDMESSAGES:
1264
1265 long_ret = 0;
1266 for (i = 0; i < MAXMAILERS; i++) {
1267 long_ret += stat_nf[i];
1268 }
1269 return (unsigned char *) &long_ret;
1270
1271 case MTASTOREDMESSAGES:
1272
1273 long_ret = global_count;
1274 return (unsigned char *) &long_ret;
1275
1276 case MTATRANSMITTEDMESSAGES:
1277
1278 long_ret = 0;
1279 for (i = 0; i < MAXMAILERS; i++) {
1280 long_ret += stat_nt[i];
1281 }
1282 return (unsigned char *) &long_ret;
1283
1284 case MTARECEIVEDVOLUME:
1285
1286 long_ret = 0;
1287 for (i = 0; i < MAXMAILERS; i++) {
1288 long_ret += stat_bf[i];
1289 }
1290 return (unsigned char *) &long_ret;
1291
1292 case MTASTOREDVOLUME:
1293
1294 long_ret = global_size;
1295 return (unsigned char *) &long_ret;
1296
1297 case MTATRANSMITTEDVOLUME:
1298
1299 long_ret = 0;
1300 for (i = 0; i < MAXMAILERS; i++) {
1301 long_ret += stat_bt[i];
1302 }
1303 return (unsigned char *) &long_ret;
1304
1305 default:
1306 snmp_log(LOG_ERR,
1307 "mibII/mta_sendmail.c:mtaEntry: unknown magic value\n");
1308 }
1309 return NULL;
1310 }
1311
1312 /**/
1313 /** unsigned char *var_mtaGroupEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method)
1314 *
1315 * Description:
1316 *
1317 * Called by the agent in order to get the values for the mtaGroupTable.
1318 *
1319 * Parameters:
1320 *
1321 * see agent documentation
1322 *
1323 * Returns:
1324 *
1325 * see agent documentation
1326 *
1327 */
1328 unsigned char *
1329 var_mtaGroupEntry(struct variable *vp,
1330 oid * name,
1331 size_t * length,
1332 int exact, size_t * var_len, WriteMethod ** write_method)
1333 {
1334 static long long_ret;
1335 auto long row;
1336 auto int result;
1337 auto time_t current_time;
1338
1339
1340 if (exact) {
1341 if (*length != vp->namelen + 2) {
1342 return NULL;
1343 }
1344 result =
1345 snmp_oid_compare(name, *length - 2, vp->name, vp->namelen);
1346 if (result != 0 || name[*length - 2] != applindex
1347 || name[*length - 1] <= 0
1348 || name[*length - 1] > mailers + nqgrps) {
1349 return NULL;
1350 }
1351 } else {
1352 if (*length < vp->namelen) {
1353 result = -1;
1354 } else {
1355 result =
1356 snmp_oid_compare(name, vp->namelen, vp->name, vp->namelen);
1357 }
1358
1359 if (result > 0) {
1360 /*
1361 * OID prefix too large
1362 */
1363 return NULL;
1364 }
1365
1366 if (result == 0) {
1367 /*
1368 * OID prefix matches exactly,...
1369 */
1370 if (*length > vp->namelen && name[vp->namelen] > applindex) {
1371 /*
1372 * ... but ApplIndex too large
1373 */
1374 return NULL;
1375 }
1376 if (*length > vp->namelen && name[vp->namelen] == applindex) {
1377 /*
1378 * ... ApplIndex ok,...
1379 */
1380 if (*length > vp->namelen + 1
1381 && name[vp->namelen + 1] >= 1) {
1382 if (name[vp->namelen + 1] >= mailers + nqgrps) {
1383 /*
1384 * ... but mailernr too large
1385 */
1386 return NULL;
1387 } else {
1388 name[vp->namelen + 1]++;
1389 }
1390 } else {
1391 name[vp->namelen + 1] = 1;
1392 }
1393 } else {
1394 name[vp->namelen] = applindex;
1395 name[vp->namelen + 1] = 1;
1396 }
1397 } else { /* OID prefix too small */
1398 memcpy(name, vp->name, (int) vp->namelen * (int) sizeof *name);
1399 name[vp->namelen] = applindex;
1400 name[vp->namelen + 1] = 1;
1401 }
1402 *length = vp->namelen + 2;
1403 }
1404
1405 *write_method = NULL;
1406 *var_len = sizeof(long); /* default to 'long' results */
1407
1408 if (vp->magic & NEEDS_STATS) {
1409 if (sendmailst_fh == -1)
1410 return NULL;
1411 current_time = time(NULL);
1412 if (current_time == (time_t) - 1 ||
1413 current_time > lastreadstats + stat_cache_time) {
1414 if (lseek(sendmailst_fh, 0, SEEK_SET) == -1) {
1415 snmp_log(LOG_ERR,
1416 "mibII/mta_sendmail.c:var_mtaGroupEntry: could not rewind to beginning of file \"%s\"\n",
1417 sendmailst_fn);
1418 return NULL;
1419 }
1420 if (read(sendmailst_fh, (void *) &stats, stats_size) !=
1421 stats_size) {
1422 snmp_log(LOG_ERR,
1423 "mibII/mta_sendmail.c:var_mtaGroupEntry: could not read statistics file \"%s\"\n",
1424 sendmailst_fn);
1425 return NULL;
1426 }
1427 if (current_time != (time_t) - 1) {
1428 lastreadstats = current_time;
1429 }
1430 }
1431 }
1432
1433 row = name[*length - 1] - 1;
1434
1435 /*
1436 * if this is a mailer but we're asking for queue-group only info,
1437 * bump there
1438 */
1439 if (!exact && row < mailers && (vp->magic == MTAGROUPSTOREDMESSAGES ||
1440 vp->magic == MTAGROUPSTOREDVOLUME)) {
1441 row = mailers;
1442 name[*length - 1] = row + 1;
1443 }
1444
1445 if (row < mailers) {
1446 switch (vp->magic) {
1447 case MTAGROUPRECEIVEDMESSAGES:
1448 long_ret = (long) stat_nf[row];
1449 return (unsigned char *) &long_ret;
1450
1451 case MTAGROUPREJECTEDMESSAGES:
1452 if (stat_nr != NULL && stat_nd != NULL) {
1453 long_ret = (long) (stat_nr[row] + stat_nd[row]); /* Number of rejected plus number of discarded messages */
1454 return (unsigned char *) &long_ret;
1455 } else {
1456 return NULL;
1457 }
1458
1459 case MTAGROUPTRANSMITTEDMESSAGES:
1460 long_ret = (long) stat_nt[row];
1461 return (unsigned char *) &long_ret;
1462
1463 case MTAGROUPRECEIVEDVOLUME:
1464 long_ret = (long) stat_bf[row];
1465 return (unsigned char *) &long_ret;
1466
1467 case MTAGROUPTRANSMITTEDVOLUME:
1468 long_ret = (long) stat_bt[row];
1469 return (unsigned char *) &long_ret;
1470
1471 case MTAGROUPNAME:
1472 *var_len = strlen(mailernames[row]);
1473 return (unsigned char *) (*var_len >
1474 0 ? mailernames[row] : NULL);
1475
1476 case MTAGROUPHIERARCHY:
1477 long_ret = (long) -1;
1478 return (unsigned char *) &long_ret;
1479
1480 default:
1481 return NULL;
1482 }
1483 } else {
1484 /*
1485 * this is the queue group part of the table
1486 */
1487 row -= mailers;
1488 switch (vp->magic) {
1489 case MTAGROUPSTOREDMESSAGES:
1490 count_queuegroup(&qgrps[row]);
1491 long_ret = (long) qgrps[row].count;
1492 return (unsigned char *) &long_ret;
1493
1494 case MTAGROUPSTOREDVOLUME:
1495 count_queuegroup(&qgrps[row]);
1496 long_ret = (long) qgrps[row].size;
1497 return (unsigned char *) &long_ret;
1498
1499 case MTAGROUPNAME:
1500 *var_len = strlen(qgrps[row].name);
1501 return (unsigned char *) (*var_len >
1502 0 ? qgrps[row].name : NULL);
1503
1504 case MTAGROUPHIERARCHY:
1505 long_ret = (long) -2;
1506 return (unsigned char *) &long_ret;
1507
1508 default:
1509 return NULL;
1510 }
1511
1512 }
1513 return NULL;
1514 }
1515
1516 /**/