biff stuff wrong
[unix-history] / usr / src / old / berknet / mwrite.c
CommitLineData
a8de32da
KM
1static char sccsid[] = "@(#)mwrite.c 4.1 (Berkeley) %G%";
2
3/* sccs id variable */
4static char *mwrite_sid = "@(#)mwrite.c 1.2";
5/*
6
7 Mwrite provides the "response" function in the network.
8 It sends its standard input to "toaddress", either by opening
9 his terminal and writing on it, or by mailing it to him.
10 It is executed by a remote machine from netdameon.c, either for a
11 response to be sent back or an errormsg to be sent back.
12 It excutes mmail locally if it needs to.
13
14Archaic Usage:
15 mwrite toaddress ttystr lutmptime fmach fuser [command ltimeSent]
16
17New Usage:
18 mwrite [-t toaddress] [-f fromaddress] [-x lutmptime]
19 [-c command] [-y ttystr] [-e ltimeSent] [-r rc]
20
21 fmach is a single letter.
22 ttystr is the full name, e.g. /dev/tty0
23 ltimeSent is number of secs since TIMEBASE in decimal.
24 lutmptime is the login time from utmp in OCTAL in the old protocol
25 and in decimal in the new protocol.
26 rc is the decimal return code (exit code>>8) of the command.
27 command and ltimeSent are optional.
28
29 There is duplication in this argument list.
30 See the note in mmail.c about this stuff.
31
32 Mwrite must be setuid bin or setuid root, to get in group 0,
33 on the CC machines, in order to write on the user's terminal.
34
35 Exit Codes:
36 Returns 0 if the writing on the terminal works.
37 Returns the return code of the mmail program if this is mailed.
38*/
39# include "defs.h"
40jmp_buf env;
41main(argc,argv)
42 char **argv; {
43 long lutmptime, otime, ltimeSent, ltimeCur, ltimeElap;
44 int alarmint();
45 FILE *file;
46 int i;
47 struct utmp *putmp;
48 char buf[BUFSIZ],*s;
49 char fromaddress[BUFSIZ], toaddress[BUFSIZ];
50 char ttynamestr[BUFSIZ], cmdstr[BUFSIZ], *stimeCur, stimeSent[20];
51 char src[10], stemp[BUFSIZ];
52 struct stat statbuf;
53
54 debugflg = DBV;
55 argv[argc] = 0;
56 otime = 0;
57 src[0] = 0;
58 errno = 0;
59
60 /* NO LONGER NEEDED */
61 strcpy(toaddress,argv[1]);
62 strcpy(ttynamestr,argv[2]);
63 sscanf(argv[3],"%lo",&lutmptime);
64 sprintf(fromaddress,"%s:%s",longname(argv[4][0]),argv[5]);
65 if(argc > 6)strcpy(cmdstr,argv[6]);
66 else cmdstr[0] = 0;
67 if(argc > 7)strcpy(stimeSent,argv[7]);
68 else stimeSent[0] = 0;
69
70 /* parse arguments */
71 for(i = 1; i < argc; i++){
72 if(argv[i][0] == '-')
73 switch(argv[i][1]){
74 case 't':
75 strcpy(toaddress,argv[++i]);
76 break;
77 case 'y':
78 strcpy(ttynamestr,argv[++i]);
79 break;
80 case 'x':
81 lutmptime = atol(argv[++i]);
82 break;
83 case 'f':
84 strcpy(fromaddress,argv[++i]);
85 break;
86 case 'c':
87 strcpy(cmdstr,argv[++i]);
88 break;
89 case 'e':
90 strcpy(stimeSent,argv[++i]);
91 break;
92 case 'r':
93 strcpy(src,argv[++i]);
94 break;
95 }
96 /* it is important that this code ignore unknown flags
97 so that options can be added w/o recompiling */
98 }
99
100 ltimeSent=atol(stimeSent)+TIMEBASE;
101
102 setjmp(env);
103 alarm(0);
104 signal(SIGALRM,alarmint);
105 if(errno != 100 && ttynamestr[0] && ttynamestr[8] != 'x'){
106 alarm(100);
107 putmp = getutmp(ttynamestr);
108 if(putmp != NULL) otime = putmp->ut_time;
109 /*
110 debug("lutmptime %lo otime %lo",lutmptime,otime);
111 */
112 if(otime != 0 && otime == lutmptime) {
113 file = fopen(ttynamestr,"w");
114 if(file != NULL && fstat(fileno(file),&statbuf) != -1
115 && (statbuf.st_mode&02)){
116 alarm(0);
117 if(src[0] != 0)sprintf(stemp,", R: %s",src);
118 else stemp[0] = 0;
119 ltimeCur = gettime();
120 stimeCur = ctime(&ltimeCur);
121 stimeCur += 11;
122 stimeCur[strlen(stimeCur)-9] = 0;
123 fprintf(file,
124 "\r\nMessage from %s at %s ...\r\n",
125 fromaddress, stimeCur);
126 if(cmdstr[0] != 0){
127 s = ctime(&ltimeSent);
128 s[strlen(s)-6] = 0;
129 ltimeElap = ltimeCur - ltimeSent;
130 fprintf(file,
131 "(command: %s%s, sent %s, took %s)\r\n",
132 cmdstr,stemp,s,comptime(ltimeElap));
133 }
134 while(fgets(buf,BUFSIZ,stdin) != NULL){
135 fputs(buf,file);
136 fputc('\r',file);
137 }
138 fprintf(file,"------\r\n");
139 exit(EX_OK);
140 }
141 }
142 }
143
144 /* well, couldn't write to him, so we'll mail to him on this mach */
145 /* mail to "toaddress", saying its from "fromaddress", about a command
146 "cmdstr", which was sent at time "stimeSent" */
147
148 alarm(0);
149 sprintf(stimeSent,"%ld",ltimeSent);
150 if(cmdstr[0] != 0){
151 if(src[0] != 0)
152 mexecl(MMAILCMD,"mmail", "-r",src, "-c",cmdstr,
153 "-e",stimeSent,"-f",fromaddress,
154 "-t",toaddress,"-z",0);
155 else
156 mexecl(MMAILCMD,"mmail", "-c",cmdstr, "-e",stimeSent,
157 "-f",fromaddress, "-t",toaddress,"-z",0);
158 }
159 else
160 mexecl(MMAILCMD,"mmail", "-f",fromaddress, "-t",toaddress,
161 "-z", 0);
162 perror(MMAILCMD);
163 exit(EX_UNAVAILABLE);
164}
165alarmint(){
166 errno = 100;
167 signal(SIGALRM,SIG_IGN); /* alarm off */
168 longjmp(env,0); /* ugh */
169 }