netsetup.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 1,405 行 · 第 1/3 页

C
1,405
字号
#ifndef lintstatic	char	*sccsid = "@(#)netsetup.c	4.3	(ULTRIX)	10/12/90";#endif lint/************************************************************************ *									* *			Copyright (c) 1984 by				* *		Digital Equipment Corporation, Maynard, MA		* *			All rights reserved.				* *									* *   This software is furnished under a license and may be used and	* *   copied  only  in accordance with the terms of such license and	* *   with the  inclusion  of  the  above  copyright  notice.   This	* *   software  or  any  other copies thereof may not be provided or	* *   otherwise made available to any other person.  No title to and	* *   ownership of the software is hereby transferred.			* *									* *   This software is  derived  from  software  received  from  the	* *   University    of   California,   Berkeley,   and   from   Bell	* *   Laboratories.  Use, duplication, or disclosure is  subject  to	* *   restrictions  under  license  agreements  with  University  of	* *   California and with AT&T.						* *									* *   The information in this software is subject to change  without	* *   notice  and should not be construed as a commitment by Digital	* *   Equipment Corporation.						* *									* *   Digital assumes no responsibility for the use  or  reliability	* *   of its software on equipment which is not supplied by Digital.	* *									* ************************************************************************//* *  Sets up: *	/etc/hosts *	/etc/networks *	/etc/hosts.equiv *	/etc/dgateway *	/usr/spool/rwho directory *	/usr/hosts directory(s) * *  Modifies: *	/etc/rc.local *	/opr/restart * *  Does NOT handle: *	/usr/lib/sendmail.cf *	multiple interfaces (networks) * * Modification History: * * 20-Jul-90  -  Lea Gottfredsen *	Fixed check for setupisdone, no longer relies on networks file. *	There always is one now.  Also added question on routed. * * 13-Oct-89	robin *	ISIS pool merge and "ne" driver additions for chc * * 07-Mar-89  -  templin *	Added XNA driver support * * 04-Jan-88  -  logcher *	Fixed the minor bug in how /etc/networks is generated when  *	using subnets. * * 25-Aug-87  -  lp *	Fixed a minor bug in how /etc/networks file was being generated. * * 31-Jul-87  -  logcher *	Added hostname to printf for Class C network.  Updated *	network device name table. * * 11-Dec-86  -  Marc Teitelbaum *	Look in /sys/conf/MACHINE for the default network interface. *	This is better than guessing from CPU type, as BI machines *	can have a unibus.  /etc/sizer should have the last say on *	whats out there anyway.  Also, fix boundary conditions for *	network and host numbers to match documentation. * * 06-Aug-86  -  marc *	Rewritten from ULTRIX-11 netsetup.c *	Reworked prompts with Liza. * *	For internets consisting of multiple LANS and many hosts, it *	is expected that "netsetup install" will be used to setup a host *	on the single LAN it is directly connected to, and then the *	system administrator will rcp the /etc/hosts and /etc/networks *	files from some host on that LAN which acts as the central *	network administrative site.  I.e., i can't imagine anyone would *	want to use a program to type in 60 or more host names and internet *	addresses...   */#include <stdio.h>#include <sys/types.h>#include <ctype.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <sys/stat.h>#include <strings.h>/*#define	DEBUG*/#ifndef	DEBUG#define	HOSTS		"/etc/hosts"#define	RC		"/etc/rc.local"#define	RESTART		"/opr/restart"#define	HOSTS_EQUIV	"/etc/hosts.equiv"#define	NETWORKS	"/etc/networks"#define	DGATEWAY	"/etc/dgateway"#define RWHODIR		"/usr/spool/rwho"#define	UHOSTS		"/usr/hosts"#ifndef CONFDIR#define CONFDIR		"/sys/conf"#endif	CONFDIR#else	DEBUG#define	HOSTS		"./hosts"#define	RC		"./rc.local"#define	RESTART		"./restart"#define	HOSTS_EQUIV	"./hosts.equiv"#define	NETWORKS	"./networks"#define	DGATEWAY	"./dgateway"#define RWHODIR		"./rwho"#define	UHOSTS		"./usr.hosts"#undef CONFDIR#define CONFDIR		"./conf"#endif	DEBUG#define	NO	0#define	YES	1#define	YESNO	2#define TRUE    1#define FALSE   0#define eqsn(s1,s2,n) (strncmp(s1,s2,n) == 0)#define plural(s) (index(s,' ')||index(s,'\t'))/* * Non-int function declarations. */FILE *edit();char *prompt();char *quote();char *emalloc();char *get_interface();char hostname[256];char *prmsg;int subbits; 	/* number of bits of host used for subnet, 0 => no subnet *//* * This is the list of interface names we will try to recognize from * the config file in determining the default network interface. * The first match wins - so watch the order when adding new entries. */char *if_table[] = {	"xna",	/* DEBNI - BI bus */	"ni", 	/* DEBNT, DEBNA - BI bus */	"de",	/* DEUNA, DELUA - UNIBUS */	"qe",	/* DEQNA, DELQA - Q-Bus  */	"ln",	/* DESVA, Busless LANCE network interface */	"ne",	/* SGEC network interface */ 	"fza",	/* DEFZA - TURBOchannel FDDI network interface */	"dmc",	/* Point to Point */	"scs",	/* SCS network driver - currently to the CI only*/	0};struct dqueue {	struct dqueue *dq_forw;	struct dqueue *dq_back;};#define MAXDEVNAMLEN 9struct device {	struct dqueue dv_queue;	/* linkage */	char dv_name[MAXDEVNAMLEN + 1];};/* * List of devices found in config file. */struct dqueue device = { &device, &device };main(argc, argv)int argc;char **argv;{	char gateway[256];	char gateacct[256];	char machabbrev[256];	char machname[256];	char netname[256];	char netalias[256];  	char trustname[256];	char buf[512];	char buf2[512];	char s[256];	char *cp, *xp;	long netnum, machnum;	struct in_addr addr, broadaddr, netmask, taddr1, taddr2;	FILE *fp;	struct hostent *hp;	struct netent *np;	long getnet(), getmach();	int max;     /* max number of bits allowed for subnet on a		      * given class network.  we allow up to the total		      * host length minus three (we save 3 for the host).		      */	int mask; /* number of hostnumber bits (network - subnet) */	int oneorzero; /* flag if we use all ones or all zero broadcast addr */	int installflg = FALSE;	int setupisdone = FALSE;	int dosetup = FALSE;	int routed = FALSE;	int i;	char *if_default = 0; 	/* default NI as determined by get_interface() */	for (i = 3; i < 20; i++)		close(i);	dup2(1, 3);#ifndef DEBUG	if (getuid()) {		fprintf(stderr, "\nYou must be root to set up your system on a local area network.\n");		leave(1);	}#endif  DEBUG	printf("\nYou will be asked a series of questions about your system.\n");	printf("Default answers are shown in square brackets ([]).\n");	printf("To use a default answer, press the RETURN key.\n\n");	hostname[0] = '\0';	if (gethostname(hostname, 255) < 0 || !hostname[0]) {		/*		 * Grab a line like		 *	hostname myname		 * or		 *	/bin/hostname myname		 * out of the /etc/rc file, to find the name.		 */		fp = fopen(RC, "r");		if (fp != NULL) {			cp = NULL;			while (fgets(buf, 512, fp) != NULL) {				if (strncmp(buf, "hostname", 8) == 0)					cp = &buf[8];				else if (strncmp(buf, "/bin/hostname", 13) == 0)					cp = &buf[13];				else					continue;				break;			}			if (cp != NULL) {				while (*cp == ' ' || *cp == '\t')					cp++;				if (*cp) {					xp = hostname;					while (*cp && *cp != ' ' &&						*cp != '\t' && *cp != '\n')						*xp++ = *cp++;					*xp = '\0';				}			}			fclose(fp);		}	}	if (!hostname[0]) {		fprintf(stderr, "\nYour system does not have a name.\n");		fprintf(stderr, "Check the /etc/rc.local file to be sure it\n");		fprintf(stderr, "contains a hostname entry.\n\n");		leave(1);	}	if (argc > 1 && (strcmp(argv[1], "install") == 0))		installflg = TRUE; 	sprintf(s,"grep -s \"ifconfig HDWR\" %s",RC);	if (system(s))		setupisdone = TRUE;	if (setupisdone == FALSE && installflg == TRUE) {		dosetup = TRUE;         /* No questions - just do it */	} else if (setupisdone == FALSE && installflg == FALSE) {prmsg="\n\Your network does not appear to be set up.  Unless you intend to set it\n\up by hand, you should set up the network files now.  Even if your\n\system is not physically connected to a network, you need to set up the\n\network files for some other commands to function properly.\n";		fprintf(stderr,prmsg);		if (yesno(YES, "\nDo you want to set up your system on a local area network now", hostname))			dosetup = TRUE;		else			leave(1);	}	else if (setupisdone == TRUE && installflg == TRUE) {			dosetup = TRUE;	} /* else if setup is done and no install flag ==> no initial setup */	if (dosetup == TRUE) {		if (!yesno(YES,"Your system's name is \"%s\".  Is this correct", hostname)){						fprintf(stderr, "\nPlease edit the /etc/rc.local file to contain the proper ");			fprintf(stderr, "system name.\n");			leave(1);		}prmsg="\n\Your system can have one or more abbreviation names.  An abbreviation\n\name reduces the number of keystrokes required for network commands. A\n\common abbreviation name is the first letter of the system name.\n\n";		printf(prmsg);		do {prmsg="\n\Press the RETURN key if you do not want any abbreviation names for\n\%s.  Otherwise, enter one or more abbreviation names, separated by\n\blank spaces: ";			if (prompt(machabbrev, 128, prmsg, hostname) == NULL)			{			    			    i = yesno(YES,			        "\nYou do not want any abbreviation names for %s.  Is this correct",								hostname);			} else {			    i = yesno(YES,				"\nThe abbreviation%s for %s %s %s.\nIs this correct",					plural(machabbrev) ? "s" : "",					hostname,					plural(machabbrev) ?						"are:" : "is:",					quote(machabbrev) );			}		} while (i == NO);	       /**************		REMOVE DECNET GATEWAY SYUFF		printf("\nA DECnet/ULTRIX-32 site can be used to act as ");		printf("a gateway to\ngain access to DECnet.\n\n");		do {		   i=yesno(NO,"Do you have a site to act as a DECnet gateway");		   if (i)			j = yesno(YES, "Verify: There is a gateway site");		   else			j = yesno(YES, "Verify: No site to act as a gateway");		} while (j == NO);		if (i) {			printf("\nInformation about the site name and the acc");			printf("ount name to\nuse is kept in %s.\n", DGATEWAY);			do {				if (NULL == prompt(gateway, 32,				   "What is the name of the gateway machine? "))					continue;				i = yesno(YES, "Verify: gateway is \"%s\"",								gateway);			} while (i == NO);			do {				if (prompt(gateacct, 32,		    "What account name on \"%s\" should be used? [guest] ",							gateway) == NULL)					strcpy(gateacct, "guest");				i = yesno(YES, "Verify: account is \"%s\"",								gateacct);			} while (i == NO);			printf("\n***** Putting gateway information into ");			printf("%s *****\n", DGATEWAY);			if (!gateacct[0])				strcpy(gateacct, "guest");			fp = fopen(DGATEWAY, "w");			if (fp == NULL) {				perror(DGATEWAY);				fprintf(stderr, "Cannot create %s\n", DGATEWAY);			} else {				fprintf(fp, "%s %s /etc/dgated\n", gateway,						gateacct);				fflush(fp);				fclose(fp);			}			printf("\nIn order for the DECnet gateway software ");			printf("to work, the \"%s\" ", gateacct);			printf("account\nmust be set up on \"%s\"", gateway);			printf(" so that \"%s\" on \"%s\" ",gateacct,hostname);			printf("can\nrlogin as a trusted user.  In a little ");			printf("bit you will be asked to add\nother hosts ");			printf("to your %s file. Do not forget to ", HOSTS);			printf("add \"%s\"\nat that time.\n", gateway);		}		******* END REMOVING DECNET GATEWAY SUFF *******/		/* Get the network number for the network. */		printf("\n");		netnum = getnet(hostname);prmsg="\n\Subnetworks allow the systems on a given local area network to be on\n\different network wires.  If your existing local area network is using\n\subnet routing, you need to know how many bits of your host number\n\are being reserved for specifying the subnetwork address.\n\\n";		printf(prmsg);		subbits = 0;		do {prmsg="Are you setting up %s on a network that uses subnet routing";			subbits = yesno(NO, prmsg, hostname);			if (subbits) {prmsg="\n\You are setting up %s on a network that uses subnet routing.\n\Is this correct";			    i = yesno(YES, prmsg, hostname);			} else {prmsg="\n\You will NOT set up %s on a network that uses subnet\n\routing.  Is this correct";			    i = yesno(YES, prmsg, hostname);			}		} while (!i);					printf("\n");		machnum = getmach(hostname, netnum);		addr = inet_makeaddr(netnum, machnum);		hp = gethostbyname(hostname);		fp = edit(HOSTS);		if (fp == NULL) {			fprintf(stderr, "%s not set up\n", HOSTS);		} else {			printf("\n***** UPDATING %s WITH %s AND localhost *****\n",HOSTS,hostname);			fprintf(fp, "g/^.*[ 	]localhost.*$/d\n");			fprintf(fp, "0a\n");			fprintf(fp, "127.0.0.1 localhost\n");			fprintf(fp, ".\n");			fprintf(fp, "w\n");			if (hp) {				fprintf(fp, "g/^.*[ 	]%s.*$/d\n", hostname);				fprintf(fp, "w\n");			}			fprintf(fp, "a\n");			fprintf(fp, "%s %s %s\n", inet_ntoa(addr),hostname,					machabbrev);			fprintf(fp, ".\n");			fprintf(fp, "w\n");			fprintf(fp, "q\n");			fflush(fp);			pclose(fp);		}		if (subbits) {			if (IN_CLASSA(ntohl(addr.s_addr)))				max = 21;			else if (IN_CLASSB(ntohl(addr.s_addr)))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?