xref: /csrg-svn/old/berknet/netmail.c (revision 8196)
1*8196Smckusick static char sccsid[] = "@(#)netmail.c	4.1	(Berkeley)	09/12/82";
2*8196Smckusick 
3*8196Smckusick /* sccs id variable */
4*8196Smckusick static char *netmail_sid = "@(#)netmail.c	1.2";
5*8196Smckusick /*
6*8196Smckusick 
7*8196Smckusick 
8*8196Smckusick  netmail [-c] [-l ...] [-p ...] [-f] [-n] [-q] ([mach] | [mach:username])
9*8196Smckusick 
10*8196Smckusick    Read mail on remote machine "mach"
11*8196Smckusick    Sends a command to the remote machine to "mail" the mail
12*8196Smckusick    to this machine.
13*8196Smckusick 
14*8196Smckusick    If the -c option is specified, this command is a mail check command,
15*8196Smckusick    and in this mode it logs into the remote machine as "network"
16*8196Smckusick    and determines if mach:username has mail.
17*8196Smckusick    It writes/mails a message to that effect.
18*8196Smckusick    This variant is intended to be used in .login files, silently
19*8196Smckusick    checking if you have mail on another machine.
20*8196Smckusick 
21*8196Smckusick    Must duplicate effort that will be redone by the net command-
22*8196Smckusick    the calls to commandfile and promptlogin are necessary
23*8196Smckusick    to get a value for the login name to send to the prmail
24*8196Smckusick    command on the other machine.
25*8196Smckusick    May read the passwd file:
26*8196Smckusick 	1. Commandfile calls getenv(HOME) to get the home directory.
27*8196Smckusick 	   If not easily reached,....
28*8196Smckusick 	2. SnCurrent() calls getlogin(). If no entry in utmp file,
29*8196Smckusick 	   will read passwd file.
30*8196Smckusick  */
31*8196Smckusick # include "defs.h"
32*8196Smckusick 
33*8196Smckusick /* global variables */
34*8196Smckusick struct userinfo status;
35*8196Smckusick 
main(argc,argv)36*8196Smckusick main(argc,argv)
37*8196Smckusick   char **argv; {
38*8196Smckusick 	char *s;
39*8196Smckusick 	char machparm[BUFSIZ], fromaddress[BUFSIZ], fMailCheck = 0;
40*8196Smckusick 	char rcmd[BUFSIZ], fquiet = 0;
41*8196Smckusick 	debugflg = DBV;
42*8196Smckusick 	strcpy(rcmd,"netmail");
43*8196Smckusick 	argc--; argv++;
44*8196Smckusick 	while(argc > 0 && argv[0][0] == '-'){
45*8196Smckusick 		switch(argv[0][1]){
46*8196Smckusick 		case 'b':	status.nonotify++; appss(rcmd,argv[0]); break;
47*8196Smckusick 		case 'c':	fMailCheck++;      appss(rcmd,argv[0]); break;
48*8196Smckusick 		case 'f':	status.force++;    appss(rcmd,argv[0]); break;
49*8196Smckusick 		case 'l':	harg(status.login); break;
50*8196Smckusick 		case 'n':	status.nowrite++;  appss(rcmd,argv[0]); break;
51*8196Smckusick 		case 'p': 	harg(status.mpasswd); break;
52*8196Smckusick 		case 'q':	fquiet = 1; 	   appss(rcmd,argv[0]); break;
53*8196Smckusick 		default:
54*8196Smckusick 			fprintf(stderr,
55*8196Smckusick 	"Usage: netmail [-l login] [-p password] [-c] [-f] [-n] [-q] [mach]\n");
56*8196Smckusick 			exit(EX_USAGE);
57*8196Smckusick 		}
58*8196Smckusick 		argc--, argv++;
59*8196Smckusick 		}
60*8196Smckusick 	if(argc > 0){
61*8196Smckusick 		if(FMemberSCh(argv[0],':'))
62*8196Smckusick 			remote = MchSFromAddr(status.login,argv[0]);
63*8196Smckusick 		else
64*8196Smckusick 			remote = lookup(argv[0]);
65*8196Smckusick 		if(remote == 0){
66*8196Smckusick 			fprintf(stderr,"Unknown machine %s\n",argv[0]);
67*8196Smckusick 			exit(EX_NOHOST);
68*8196Smckusick 		}
69*8196Smckusick 		appss(rcmd,argv[0]);
70*8196Smckusick 	}
71*8196Smckusick 
72*8196Smckusick 	/* read the .netrc file to get a value for remote */
73*8196Smckusick 	/* will get status.login, passwd, and force for fetch variant */
74*8196Smckusick 	commandfile();
75*8196Smckusick 	if(remote == 0)remote = getremote(local);
76*8196Smckusick 	sprintf(machparm,"-m%c",remote);
77*8196Smckusick 
78*8196Smckusick 
79*8196Smckusick 	if(remote == local){
80*8196Smckusick 		fprintf(stderr,
81*8196Smckusick 		"Use the mail command to read your mail on this machine.\n");
82*8196Smckusick 		exit(EX_USAGE);
83*8196Smckusick 	}
84*8196Smckusick 
85*8196Smckusick /* read pw file, get local addr to send to prmail, store in status.localname */
86*8196Smckusick 	s = SnFromUid(getuid());
87*8196Smckusick 	if(s == NULL){
88*8196Smckusick 		fprintf(stderr,"Unknown local user\n");
89*8196Smckusick 		exit(EX_OSFILE);
90*8196Smckusick 	}
91*8196Smckusick 	strcpy(status.localname,s);
92*8196Smckusick 	sprintf(fromaddress,"%s:%s",longname(local),s);
93*8196Smckusick 
94*8196Smckusick 	/* mail check variant */
95*8196Smckusick 	if(fMailCheck){
96*8196Smckusick 		if(status.login[0] == 0){
97*8196Smckusick 			fprintf(stderr,
98*8196Smckusick 			"Must supply a remote user name for mail check.\n");
99*8196Smckusick 			exit(EX_USAGE);
100*8196Smckusick 		}
101*8196Smckusick 		/* send mail check over, no passwd needed */
102*8196Smckusick 		if(fquiet)
103*8196Smckusick 			mexecl(netcmd,"net","-q",machparm,"-l","network",
104*8196Smckusick 			"-c",rcmd,
105*8196Smckusick 			PRMAIL,"-c","-l",status.login,"-f",fromaddress,0);
106*8196Smckusick 		else
107*8196Smckusick 			mexecl(netcmd,"net","-q",machparm,"-l","network",
108*8196Smckusick 			"-c",rcmd,
109*8196Smckusick 			PRMAIL,"-c","-l",status.login,"-f",fromaddress,"-k",0);
110*8196Smckusick 		perror(netcmd);
111*8196Smckusick 		fprintf(stderr,"Network is down\n");
112*8196Smckusick 		exit(EX_UNAVAILABLE);
113*8196Smckusick 	}
114*8196Smckusick 
115*8196Smckusick 	/* mail forward variant */
116*8196Smckusick 
117*8196Smckusick 	/*
118*8196Smckusick 	   get name to send as parameter to prmail.
119*8196Smckusick 	   required for multiple login names with the same uid's
120*8196Smckusick 	   stored in status.login
121*8196Smckusick 	*/
122*8196Smckusick 	envloginpasswd(remote,status.login,status.mpasswd); /* look in env */
123*8196Smckusick 	promptlogin(remote);	/* prompt for name, passwd explicitely */
124*8196Smckusick 
125*8196Smckusick 	if(fquiet)
126*8196Smckusick 		kexecl(netcmd,"net","-q",machparm,"-c",rcmd,PRMAIL,"-l",
127*8196Smckusick 			status.login,"-f",fromaddress,"-r",0);
128*8196Smckusick 	else
129*8196Smckusick 		kexecl(netcmd,"net","-q",machparm,"-c",rcmd,PRMAIL,"-l",
130*8196Smckusick 			status.login,"-f",fromaddress,"-r","-k",0);
131*8196Smckusick 	perror(netcmd);
132*8196Smckusick 	fprintf(stderr,"Network is down\n");
133*8196Smckusick 	exit(EX_UNAVAILABLE);
134*8196Smckusick 	}
135*8196Smckusick /*
136*8196Smckusick 	append string sfrom to end of string sto, preceded by blank */
appss(sto,sfrom)137*8196Smckusick appss(sto,sfrom)
138*8196Smckusick 	register char *sto, *sfrom;
139*8196Smckusick {
140*8196Smckusick 	strcat(sto," ");
141*8196Smckusick 	strcat(sto,sfrom);
142*8196Smckusick }
143