Mwrite provides the "response" function in the network.
It sends its standard input to "toaddress", either by opening
his terminal and writing on it, or by mailing it to him.
It is executed by a remote machine from netdameon.c, either for a
response to be sent back or an errormsg to be sent back.
It excutes mmail locally if it needs to.
mwrite toaddress ttystr lutmptime fmach fuser [command ltimeSent]
mwrite [-t toaddress] [-f fromaddress] [-x lutmptime]
[-c command] [-y ttystr] [-e ltimeSent] [-r rc]
fmach is a single letter.
ttystr is the full name, e.g. /dev/tty0
ltimeSent is number of secs since TIMEBASE in decimal.
lutmptime is the login time from utmp in OCTAL in the old protocol
and in decimal in the new protocol.
rc is the decimal return code (exit code>>8) of the command.
command and ltimeSent are optional.
There is duplication in this argument list.
See the note in mmail.c about this stuff.
Mwrite must be setuid bin or setuid root, to get in group 0,
on the CC machines, in order to write on the user's terminal.
Returns 0 if the writing on the terminal works.
Returns the return code of the mmail program if this is mailed.
Returns 101 if can't exec mail program.
Returns 102 if can't read the utmp file.
long lutmptime
, otime
, ltimeSent
, ltimeCur
, ltimeElap
;
char fromaddress
[BUFSIZ
], toaddress
[BUFSIZ
];
char ttynamestr
[BUFSIZ
], cmdstr
[BUFSIZ
], *stimeCur
, stimeSent
[20];
char src
[10], stemp
[BUFSIZ
];
strcpy(toaddress
,argv
[1]);
strcpy(ttynamestr
,argv
[2]);
sscanf(argv
[3],"%lo",&lutmptime
);
sprintf(fromaddress
,"%s:%s",longname(argv
[4][0]),argv
[5]);
if(argc
> 6)strcpy(cmdstr
,argv
[6]);
if(argc
> 7)strcpy(stimeSent
,argv
[7]);
for(i
= 1; i
< argc
; i
++){
strcpy(toaddress
,argv
[++i
]);
strcpy(ttynamestr
,argv
[++i
]);
lutmptime
= atol(argv
[++i
]);
strcpy(fromaddress
,argv
[++i
]);
strcpy(cmdstr
,argv
[++i
]);
strcpy(stimeSent
,argv
[++i
]);
/* it is important that this code ignore unknown flags
so that options can be added w/o recompiling */
ltimeSent
=atol(stimeSent
)+TIMEBASE
;
if(errno
!= 100 && ttynamestr
[0] && ttynamestr
[8] != 'x'){
file
= fopen("/etc/utmp","r");
while(fread(&utmpstr
,1,sizeof utmpstr
,file
) == sizeof utmpstr
){
if(utmpstr
.ut_tty
== ttynamestr
[8]){
if(strcmp(utmpstr
.ut_line
,ttynamestr
+5) == 0){
debug("lutmptime %lo otime %lo",lutmptime,otime);
if(otime
!= 0 && otime
== lutmptime
) {
file
= fopen(ttynamestr
,"w");
if(file
!= NULL
&& fstat(fileno(file
),&statbuf
) != -1
&& (statbuf
.st_mode
&02)){
if(src
[0] != 0)sprintf(stemp
,", R: %s",src
);
stimeCur
= ctime(<imeCur
);
stimeCur
[strlen(stimeCur
)-9] = 0;
"\r\nMessage from %s at %s ...\r\n",
ltimeElap
= ltimeCur
- ltimeSent
;
"(command: %s%s, sent %s, took %s)\r\n",
cmdstr
,stemp
,s
,comptime(ltimeElap
));
while(fgets(buf
,BUFSIZ
,stdin
) != NULL
){
fprintf(file
,"------\r\n");
/* well, couldn't write to him, so we'll mail to him on this mach */
/* mail to "toaddress", saying its from "fromaddress", about a command
"cmdstr", which was sent at time "stimeSent" */
sprintf(stimeSent
,"%ld",ltimeSent
);
mexecl(MMAILCMD
,"mmail", "-r",src
, "-c",cmdstr
,
"-e",stimeSent
,"-f",fromaddress
,
mexecl(MMAILCMD
,"mmail", "-c",cmdstr
, "-e",stimeSent
,
"-f",fromaddress
, "-t",toaddress
,"-z",0);
mexecl(MMAILCMD
,"mmail", "-f",fromaddress
, "-t",toaddress
,
signal(SIGCLK
,SIG_IGN
); /* alarm off */
longjmp(env
,0); /* ugh */