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