|
|
1.1 root 1: /*
2: * The code that basically handles the sending of
3: * an individual message in the mail system.
4: */
5:
6: #include "mail.h"
7:
8: #define SITENAMELEN 32 /* max length of sitename */
9:
10: extern char *strtok();
11: FILE *aliasfp;
12: char aliasbuf [BUFSIZ];
13: static char **tolist;
14: extern char BOBerrmsg[];
15:
16: extern char *temp;
17: extern int myuid; /* User-id of mail user */
18: extern int mygid; /* Group-id of mail user */
19: extern char cmdname[]; /* Command for x{en,de}code filter */
20: /* and for tail recursion to uumail */
21: /* and for editor recursion */
22: extern char *editname; /* name of editor */
23: extern char *askcc; /* Ask for CC: list? (YES/NO) */
24: extern int callmexmail; /* Xmail modifier present */
25: extern char wrerr[];
26: extern char myname[]; /* User name */
27: extern char myfullname[]; /* full user name */
28: extern int mflag; /* `You have mail.' message to recipient */
29: extern int verbflag; /* verbose flag */
30: extern char templ[]; /* Temp file name template */
31: extern char nosave[];
32: extern int callmermail;
33:
34: static int senderr;
35: struct tm *tp;
36: char toerr[] = "Cannot create temporary file\n";
37: char header[BUFSIZ]; /* Message header */
38: char boxname[256]; /* Destination mailbox */
39: char remotefrom [32]; /* "remote from uucp" */
40:
41: char nosend[] = "Can't send mail to '%s'\n";
42: char nopubk[] = "Can't send xmail to '%s'\n";
43:
44: /*
45: * Send the message found on
46: * the file pointer to the list
47: * of people (argv style) with
48: * a NULL pointer at the end.
49: * The message is copied to a temp-file
50: * from position `start' to `end' (or EOF).
51: */
52: send2(fp, users, start, end, asksubj)
53: FILE *fp;
54: register char **users;
55: fsize_t start, end;
56: int asksubj;
57: {
58: char **getcc();
59: FILE *xfp, *tfp, *sigfp;
60: time_t curtime;
61:
62: senderr = 0;
63: temp = templ;
64:
65:
66: /* open a temporary file that data from smail will be
67: * written to. Abort on failure or interrupt signal.
68: */
69:
70: if ((tfp = fopen(temp, "w+")) == NULL){
71: merr(toerr);
72: return(1);
73: }else{
74: chown(temp, myuid, mygid);
75:
76: if (intcheck()) {
77: fclose(tfp);
78: return(1);
79: }
80:
81: fseek(fp, start, 0);
82: end -= start;
83:
84: for(;;){
85: if (fgets(msgline, NLINE, fp) == NULL)
86: break;
87: fputs(msgline, tfp);
88: fflush(tfp);
89: if ( (end-=strlen(msgline)) <= 0 )
90: break;
91: }
92:
93: if (intcheck()) {
94: fclose(tfp);
95: return(1);
96: }
97:
98: /*
99: * if empty message, bug out.
100: */
101: if (ftell(tfp) == 0) {
102: fclose(tfp);
103: return(1);
104: }
105:
106: /*
107: * Now send the message.
108: */
109:
110: time(&curtime);
111: tp = localtime(&curtime);
112:
113: usend(users, tfp); /* temp file is closed in usend() */
114:
115:
116: unlink(temp); /* delete temp file */
117: temp = NULL;
118:
119: }
120:
121: return( senderr );
122: }
123:
124: usend(users, tfp)
125: char **users;
126: FILE *tfp;
127: {
128: FILE *xfp;
129: char *cp, *name;
130: char **ulist;
131: register struct passwd *pwp;
132: int lockstat; /* status of mlock */
133:
134:
135: for (ulist = users; (name=*ulist) != NULL; ulist++) {
136: rewind(tfp);
137:
138:
139: sprintf(boxname, "%s%s", SPOOLDIR, name);
140: if ((pwp = getpwnam(name)) == NULL) {
141: /* RFC 822 (Internet) addresses are case independent,
142: * so valid local addresses must should be lower case.
143: */
144: lcase(name);
145: sprintf(boxname, "%s%s", SPOOLDIR, name);
146: if ((pwp = getpwnam(name)) == NULL) {
147: mmsg(nosend, name);
148: logdump(nosend, name);
149: senderr = 1;
150: continue;
151: }
152: }
153:
154: lockstat = mlock(pwp->pw_uid); /* lock mailbox */
155:
156: if(lockstat == 0){ /* lock successful */
157: if ((xfp = fopen(boxname, "a")) == NULL) {
158: mmsg(nosend, name);
159: logdump(nosend, name);
160: senderr = 1;
161: munlock();
162: continue;
163: }else{
164:
165: chown(boxname, pwp->pw_uid, pwp->pw_gid);
166: chmod(boxname, 0600);
167: fprintf(xfp, "\1\1\1\1\n");
168: fflush(xfp);
169: mcopy(tfp, xfp, ftell(tfp), (fsize_t)MAXLONG, 0);
170: fprintf(xfp, "\n\1\1\1\1\n");
171: fflush(xfp);
172: sleep(1);
173: fclose(xfp);
174: munlock();
175: advise(name);
176: }
177: }else{ /* lock failed */
178: senderr = 1;
179: sprintf(BOBerrmsg,"{%d} mailbox already locked!\n",getpid());
180: logdump(BOBerrmsg);
181: continue;
182: }
183:
184: }
185:
186: if (senderr && callmermail) {
187: mmsg("error sending mail, text of message follows:\n\n");
188: mmsg("------------------------\n\n");
189: rewind(tfp);
190: mcopy(tfp, stdout, ftell(tfp), (fsize_t)MAXLONG, 0);
191: mmsg("\n------------------------\n\n");
192: }
193:
194: fclose(tfp);
195: }
196:
197: xsend(users, tfp) char **users; FILE *tfp;
198: {
199: register char **ulist;
200: register char *cp;
201: register struct passwd *pwp;
202: FILE *xfp;
203:
204: for (ulist = users; *ulist!=NULL; ulist++) {
205: rewind(tfp);
206: sprintf(boxname, "%s%s", SPOOLDIR, *ulist);
207: sprintf(cmdname, "xencode %s >> %s", *ulist, boxname);
208: if (index(*ulist, '!') != NULL
209: || (pwp = getpwnam(*ulist)) == NULL) {
210: mmsg(nosend, *ulist);
211: continue;
212: }
213: if (xaccess(*ulist) == 0) {
214: mmsg(nopubk, *ulist);
215: continue;
216: }
217: mlock(pwp->pw_uid);
218: if ((xfp = fopen(boxname, "a")) == NULL) {
219: mmsg(nosend, *ulist);
220: munlock();
221: continue;
222: }
223: chown(boxname, pwp->pw_uid, pwp->pw_gid);
224: chmod(boxname, 066);
225: fprintf(xfp, "From xmail %s %s\n", cp,
226: tzname[tp->tm_isdst ? 1 : 0]);
227: fclose(xfp);
228: if ((xfp = popen(cmdname, "w")) == NULL) {
229: mmsg("Can't pipe to xencode\n");
230: continue;
231: }
232: if (fwrite(header, strlen(header), 1, xfp) != 1
233: || mcopy(tfp, xfp, (fsize_t)0, (fsize_t)MAXLONG, 0)) {
234: merr(wrerr, cmdname);
235: }
236: pclose(xfp);
237: munlock();
238: }
239: fclose(tfp);
240: }
241:
242: /*
243: * If the `-m' option is specified, advise
244: * the recipient of the presence of mail.
245: */
246: advise(recipient)
247: char *recipient;
248: {
249: register FILE *fp;
250: register FILE *tfp;
251: struct utmp ut;
252: char tty[30];
253: struct stat sb;
254:
255: if (!mflag)
256: return;
257: if ((fp = fopen("/etc/utmp", "r")) == NULL)
258: return;
259: while (fread(&ut, sizeof ut, 1, fp) == 1)
260: if (strncmp(ut.ut_name, recipient, DIRSIZ) == 0) {
261: sprintf(tty, "/dev/%s", ut.ut_line);
262: if (stat(tty, &sb)<0 || (sb.st_mode&S_IEXEC)==0)
263: continue;
264: if ((tfp = fopen(tty, "w")) != NULL) {
265: fprintf(tfp, "\7%s: you have mail.\n", myname);
266: fclose(tfp);
267: }
268: }
269: fclose(fp);
270: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.