|
|
1.1 root 1: # include "defs.h"
2: /*
3: Mmail is a berkeley network internal command.
4: It is executed locally by the mwrite command,
5: and from a remote machine by the sendberkmail command.
6: Its purpose is to send mail to a user on this
7: machine using the system mail program.
8:
9: Archaic Usage:
10:
11: mmail [-commandsent -timesent] fromuser frommach touser
12:
13: Correct Usage:
14: mmail [-c commandsent] [-e timesent] [-f fromaddress] [-t toaddress]
15: [-h hopcnt] [-r rc] [-z]
16:
17: The mwrite command uses all the options.
18: The sendberkmail command does not use the commandsend, timesent and rc
19: options.
20: Timesent is time in seconds since 1901 in decimal, as returned by time().
21: Frommach is a multi-character name, not a single letter.
22: Rc is the return code (exit code>>8) of the command.
23:
24: Assumptions about the system mail command:
25: 1. We assume there is an optional argument "-r" which can be added to mail.
26: Mail argument format (two choices):
27:
28: mail -r fromaddress toaddress
29:
30: which becomes mail from "fromaddress" instead of "network".
31:
32: 2. We assume that mail accepts the "-h hopcnt" flag, and passes it thru
33: unchanged to the sendberkmail program. The hopcnt is incremented everytime
34: it passes thru mmail, so inifinite mail forwarding is detected.
35: Since both the from and to addresses cycle, it there is infinite looping
36: we simply mail to root to that effect and throw away the mail.
37:
38:
39: If this argument scheme looks flakey it is because I screwed up
40: in the argument design. With the network now up to 10 machines,
41: I can't add another parameter to the internal commands of the network
42: like mmail and mwrite. If I had used labeled parms instead of
43: positional parms, I would be able to add more options/info
44: without having to recompile all code...
45:
46: exit codes:
47: normally returns the exit code from the mail program
48:
49: */
50: main(argc,argv)
51: char **argv; {
52: int n, ret, i, hopcnt = 0, pid;
53: char *sargv[20], *cmdstr=NULL, buf[BUFSIZ], *timestr,
54: fromaddress[BUFSIZ];
55: char toaddress[BUFSIZ], src[20], snFrom[BUFSIZ], snto[BUFSIZ],
56: mchFrom, mchto, stemp[BUFSIZ], fisresponse = 0;
57: long timesent = TIMEBASE, el;
58: FILE *fdm;
59:
60: debugflg = DBV;
61: src[0] = 0;
62:
63: /* parse old format positional parms */
64: if(argv[1][0] == '-'){
65: cmdstr = argv[1] + 1;
66: timesent = atol(argv[2] + 1);
67: sprintf(fromaddress,"%s:%s",argv[4],argv[3]);
68: strcpy(toaddress,argv[5]);
69: }
70: else {
71: sprintf(fromaddress,"%s:%s",argv[2],argv[1]);
72: strcpy(toaddress,argv[3]);
73: }
74: argv[argc] = 0;
75:
76: /* parse labeled parameters */
77: /* prob because of -cmd in arg1 and arg2 */
78: for(i = 1; i < argc; i++){
79: if(argv[i][0] == '-' && argv[i][2] == 0)
80: switch(argv[i][1]){
81: case 'f':
82: strcpy(fromaddress,argv[++i]);
83: break;
84: case 'c':
85: cmdstr = argv[++i];
86: break;
87: case 'e':
88: timesent = atol(argv[++i]);
89: break;
90: case 't':
91: strcpy(toaddress,argv[++i]);
92: break;
93: case 'h':
94: hopcnt = atoi(argv[++i]);
95: break;
96: case 'r':
97: strcpy(src,argv[++i]);
98: break;
99: case 'z':
100: fisresponse++;
101: break;
102: /* it is important there be no error if an unknown
103: flag is encountered */
104: }
105: }
106: mchFrom = MchSFromAddr(snFrom,fromaddress);
107:
108: /* compute time send */
109: timestr = ctime(×ent);
110: timestr[strlen(timestr) - 6] = 0;
111: el = gettime() - timesent;
112:
113: /* check the hopcnt */
114: hopcnt++;
115: if(hopcnt > MAXHOPS)hopcnterr(toaddress, hopcnt);
116:
117: /* analyze the dest, if local, strip off mach name, otherwise ok */
118: mchto = MchSFromAddr(snto,toaddress);
119: if(mchto == local)strcpy(toaddress,snto);
120:
121: /* it is important to realize that mmail is executed
122: either as root, network, or the USER!
123: So the -r option must be accepted (and possibly ignored)
124: by the mail program if the user is a reandom user.
125: */
126: /* now we fork off a mail command. if fisresponse, then
127: we are "cautious" and don't use mail forwarders */
128:
129: fdm = mailopen(toaddress, fromaddress, fisresponse, hopcnt);
130: if(cmdstr != NULL){
131: if(src[0] != 0)sprintf(stemp,", R: %s", src);
132: else stemp[0] = 0;
133: fprintf(fdm,"Subject: \"%s\"%s, sent %s, took %s\n",
134: cmdstr,stemp,timestr,comptime(el));
135: }
136: while((n = fread(buf,1,BUFSIZ,stdin)) > 0)
137: fwrite(buf,1,n,fdm);
138: ret = mailclose(fdm);
139: ret >>= 8;
140: if(ret != 0)
141: fprintf(stderr,
142: "Non-zero return code (%d) from the mail program.\n",ret);
143: exit(ret);
144: }
145: /*
146: hopcnterr()
147:
148: there appears to be infinite mail forwarding -
149: as detected by the hop count. Mail to root and give up.
150: Both the from and to addresses are cycling, so mail
151: can't be sent there.
152: */
153: hopcnterr(toaddress,hopcnt)
154: char *toaddress;
155: int hopcnt;
156: {
157: char cmdstr[BUFSIZ];
158: int rcode;
159: sprintf(cmdstr,"echo infinite mail loop for %s hops %d | mail root",
160: toaddress,hopcnt);
161: rcode = system(cmdstr);
162: exit(EX_OSERR);
163: /*UNREACHED*/
164: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.