/***************************************************************************/ /* Module: $Id: main.c,v 1.1 1998/05/01 15:12:11 maf Exp $ /* Description: main for arsystem email gateway responder /* Author: maf /* Notes: /***************************************************************************/ /* $Log: main.c,v $ Revision 1.1 1998/05/01 15:12:11 maf mail to remedy gateway PR: Reviewed by: Submitted by: Obtained from: */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "main.h" int debug; int got_sigpipe; int sig_pipe(); char *my_env[] = { "PATH=/bin:/usr/bin:/usr/ucb:", "SHELL=/bin/sh", 0 }; #define FROM_ADDR "dispatch@tsc.ohio-state.edu (8-help Response)" #define MSG_FILE "/usr/local/ar/armaild/msgfile" main(argc, argv) int argc; char **argv; { ARControlStruct ar_control; ARStatusList ar_status; extern int optind, opterr, errno; extern char *optarg; int x, ret, err, fd, y; char *subject, *msg, *from, *reply_to, csum[17]; char *msg_file, *reply_file, *reply_body; unsigned char md5_sum[16]; struct stat statbuf; int send_reply; AREntryIdType entry_id; char *addr, *entry_id2; char new_subject[120], *c, new_from[120]; /* init AR structs */ bzero(&ar_control, sizeof ar_control); strcpy(ar_control.server, "action.net.ohio-state.edu"); strcpy(ar_control.password, "XXX"); strcpy(ar_control.user, "TSC Dispatch"); subject = msg = from = reply_to = reply_file = reply_body = (char*)0L; csum[0] = 0; fd = -1; send_reply = 0; /* catch sigpipe */ signal(SIGPIPE, sig_pipe); /* assume the worst */ err = 1; /* process command line args */ while ((x = getopt(argc, argv, "b:d:f:m:r:s:?hS")) != -1) switch (x) { case 'b': reply_file = optarg; break; case 'd': /* debug */ debug = atoi(optarg); break; case 'f': /* from */ from = optarg; if (from[0] == ' ') ++from; break; case 'm': /* message file */ msg_file = optarg; break; case 'r': /* reply-to */ reply_to = optarg; if (reply_to[0] == ' ') ++reply_to; break; case 's': /* subject */ subject = optarg; if (subject[0] == ' ') ++subject; break; case 'S': /* send reply */ send_reply = 1; break; case '?': case 'h': /* help */ usage(); exit (0); default: fprintf(stderr, "No such option.\n"); usage(); exit (1); break; } /* end switch */ /* sanity checks */ if (!subject) { fprintf(stderr, "-s required\n"); exit (1); } if (!msg_file) { fprintf(stderr, "-m required\n"); exit (1); } /* /* start up ARSystem API */ if ((ret=ARInitialization(&ar_status)) != AR_RETURN_OK) { if (ret != AR_RETURN_BAD_STATUS) ar_print_status(&ar_status); if (ret > AR_RETURN_GOOD) { fprintf(stderr, "ARInitialization() failed\n"); goto out_main; } } /* free memory allocated to status */ FreeARStatusList(&ar_status, (ARBoolean)FALSE); /* load the message file into memory */ if (msg_file) { if ((fd = open(msg_file, O_RDONLY, 0)) == -1) { fprintf(stderr, "open(%s): %s\n", msg_file, strerror(errno)); goto out_main; } if (fstat(fd, &statbuf) == -1) { fprintf(stderr, "fstat(): %s\n", strerror(errno)); goto out_main; } /* 16K is all we'll take */ if (statbuf.st_size > 16384) statbuf.st_size = 16384; /* allocate memory for msg */ if (!(msg = (char*)malloc((int)statbuf.st_size+1))) { fprintf(stderr, "malloc() failure.\n"); goto out_main; } if (read(fd, msg, (int)statbuf.st_size) == -1) { fprintf(stderr, "read(): %s\n", strerror(errno)); goto out_main; } close (fd); msg[statbuf.st_size] = 0; /* /* calculate checksum */ md5_calc(&md5_sum, msg, (int)statbuf.st_size); /* convert 48 bits of checksum to ascii */ sprintf(csum, "%2.2X%2.2X%2.2X%2.2X", (int)md5_sum[0], (int)md5_sum[1], (int)md5_sum[2], (int)md5_sum[3]); } /* load the reply body */ if (reply_file) { if ((fd = open(reply_file, O_RDONLY, 0)) == -1) { fprintf(stderr, "open(%s): %s\n", reply_file, strerror(errno)); goto out_main; } if (fstat(fd, &statbuf) == -1) { fprintf(stderr, "fstat(): %s\n", strerror(errno)); goto out_main; } /* 16K is all we'll take */ if (statbuf.st_size > 16384) statbuf.st_size = 16384; /* allocate memory for reply body */ if (!(reply_body = (char*)malloc((int)statbuf.st_size+1))) { fprintf(stderr, "malloc() failure.\n"); goto out_main; } if (read(fd, reply_body, (int)statbuf.st_size) == -1) { fprintf(stderr, "read(): %s\n", strerror(errno)); goto out_main; } close (fd); reply_body[statbuf.st_size] = 0; } /* convert the from line to all lowercase */ for (x = 0; (x < 100) && from[x]; ++x) { if (isascii((int)from[x])) { if (isupper((int)from[x])) new_from[x] = tolower((int)from[x]); else new_from[x] = from[x]; } else new_from[x] = from[x]; } new_from[x] = 0; /* /* submit to remedy */ if (submit_request(&ar_control, msg, new_from, reply_to, subject, &csum, &entry_id)) { fprintf(stderr, "submit_request() failed\n"); goto out_main; } /* /* send e-mail reply */ if (send_reply) { if ((from && from[0] && strcmp(from, "none")) || (reply_to && reply_to[0] && strcmp(reply_to, "none"))) { if (reply_to && reply_to[0] && strcmp(reply_to, "none")) addr = reply_to; else addr = from; new_subject[0] = 'R'; new_subject[1] = 'e'; new_subject[2] = ':'; new_subject[3] = ' '; /* copy up to 80 bytes of the original subject */ for (x = 0; (x < 80) && subject[x]; ++x) new_subject[x+4] = subject[x]; new_subject[x+4] = ' '; new_subject[x+5] = '('; new_subject[x+6] = 'E'; /* skip the leading zero's on the entry id */ entry_id2 = (char*)&entry_id; while (*entry_id2 && (*entry_id2 == '0')) ++entry_id2; /* tack the entry ID on the subject */ c = entry_id2; for (y = 0; (y < 15) && *c; ++c, ++y) new_subject[x+7+y] = *c; new_subject[x+7+y] = ')'; new_subject[x+7+y+1] = 0; if (deliver_mail(FROM_ADDR, addr, new_subject, "OSU Technology Support Center", entry_id2, csum, msg, MSG_FILE, reply_body)) { fprintf(stderr, "deliver_mail() failed\n"); goto out_main; } } } err = 0; /* good */ out_main: /* end ARSystem API */ if ((ret = ARTermination(&ar_status)) != AR_RETURN_OK) { if (ret != AR_RETURN_BAD_STATUS) ar_print_status(&ar_status); if (ret > AR_RETURN_GOOD) fprintf(stderr, "ARTermination() failed\n"); } /* free memory allocated to status */ FreeARStatusList(&ar_status, (ARBoolean)FALSE); /* close fd */ if (fd != -1) close (fd); /* free mem */ if (msg) free (msg); if (reply_body) free (reply_body); return err; } /* main */ ar_print_status(status) ARStatusList *status; { unsigned int x; for (x = 0; x < status->numItems; ++x) { fprintf(stderr, "%u:%lu: %s\n", status->statusList[x].messageType, status->statusList[x].messageNum, status->statusList[x].messageText); } } /* ar_print_status */ usage(){ fprintf(stderr, "ar-mail-in usage:\n\n"); fprintf(stderr, " -b file use file as the reply body\n"); fprintf(stderr, " -d # set debug level #\n"); fprintf(stderr, " -f name set the from address to name\n"); fprintf(stderr, " -m file use file as the original message\n"); fprintf(stderr, " -r name set the reply-to address to name\n"); fprintf(stderr, " -S send an e-mail reply (defaults to off)\n"); fprintf(stderr, " -h|? this message\n\n"); }