Ejemplo n.º 1
0
Archivo: ppp.c Proyecto: sthen/nsh
void
conf_sppp_mh(FILE *output, struct sauthreq *spa, char *ifname, char *pfx)
{
	int i;
	char type[TYPESZ];
	StringList *req;

	if (!(spa->proto | spa->name[0] | spa->secret[0] | (spa->flags &
	    AUTHFLAG_NOCALLOUT) | (spa->flags & AUTHFLAG_NORECHALLENGE)))
		return;
	fprintf(output, " %s", pfx);
	if (spa->proto)
		for (i = 0; i < nitems(spppproto); i++)
			if (spa->proto == spppproto[i].type)
				fprintf(output, " proto %s", spppproto[i].name);
	if (spa->name[0])
		fprintf(output, " name %s", spa->name);

	snprintf(type, TYPESZ, "%skey", pfx);
	req = sl_init();
	if (db_select_flag_x_ctl(req, type, ifname) >= 0) {
		if (req->sl_cur > 0)
			fprintf(output, " key %s", req->sl_str[0]);
	}
	sl_free(req, 1);

	if (spa->flags & AUTHFLAG_NOCALLOUT)
		fprintf(output, " flag callin");
	if (spa->flags & AUTHFLAG_NORECHALLENGE)
		fprintf(output, " flag norechallenge");
	fprintf(output, "\n");
}
Ejemplo n.º 2
0
Archivo: conf.c Proyecto: sthen/nsh
void
conf_db_single(FILE *output, char *dbname, char *lookup, char *ifname)
{
	StringList *dbreturn;
	dbreturn = sl_init();

	if (db_select_flag_x_ctl(dbreturn, dbname, ifname) < 0) {
		printf("%% conf_db_single %s database select failed\n", dbname);
	}
	if (dbreturn->sl_cur > 0) {
		if (lookup == NULL)
			fprintf(output, " %s\n", dbname);
		else if (strcmp(dbreturn->sl_str[0], lookup) != 0)
			fprintf(output, " %s %s\n", dbname, dbreturn->sl_str[0]);
	}
	sl_free(dbreturn, 1);
}
Ejemplo n.º 3
0
Archivo: if.c Proyecto: yellowman/nsh
int
intrtd(char *ifname, int ifs, int argc, char **argv)
{
	StringList *dbreturn;
	char *cmdpath, *cmdname;
	int set;

	if (NO_ARG(argv[0])) {
		argv++;
		argc--;
		set = 0;
	} else
		set = 1;

	if (isprefix(argv[0], "rtadvd")) {
		cmdname = "rtadvd";
		cmdpath = RTADVD;
	} else {
		printf("%% intrtd: Internal error\n");
		return 0;
	}

	if (argc > 1) {
		printf ("%% %s\n", cmdname);
		printf ("%% no %s\n", cmdname);
		return(0);
	}

	dbreturn = sl_init();
	if (db_select_flag_x_ctl(dbreturn, cmdname, ifname) < 0) {
		printf("%% database failure select flag x ctl\n");
		sl_free(dbreturn, 1);
		return(1);
	}
	if (dbreturn->sl_cur > 0) {
		/* already found in db for ifname */
		if (!set) {
			if (db_delete_flag_x_ctl(cmdname, ifname) < 0)
				printf("%% database delete failure\n");
		} else {
			printf("%% %s already running\n", cmdname);
		}
		if (!set && strcmp(cmdname, "rtadvd") == 0) {
			char *args[] = { PKILL, cmdpath, "-c",
			    "/var/run/rtadvd.0", ifname, '\0' };

			cmdargs(PKILL, args);
		}
	} else {
		/* not found in db for ifname */
		if (set) {
			if(db_insert_flag_x(cmdname, ifname, 0, DB_X_ENABLE,
			    NULL) < 0) {
				printf("%% database insert failure\n");
				sl_free(dbreturn, 1);
				return(1);
			}
		} else {
			printf("%% %s not running\n", cmdname);
		}
		if (set && strcmp(cmdname, "rtadvd") == 0) {
			char *args[] = { cmdpath, "-c", "/var/run/rtadvd.0",
			    ifname, '\0' };

			cmdargs(cmdpath, args);
		}
	}
	sl_free(dbreturn, 1);
	return(0);
}
Ejemplo n.º 4
0
void conf_interfaces(FILE *output, char *only)
{
	FILE *dhcpif;
	int ifs, flags, ippntd, br;
	char leasefile[sizeof(LEASEPREFIX)+1+IFNAMSIZ];
	char *lladdr;
	char ifdescr[IFDESCRSIZE];

	struct if_nameindex *ifn_list, *ifnp;
	struct ifreq ifr, ifrdesc;
	struct if_data if_data;
	struct vlanreq vreq;

	if ((ifn_list = if_nameindex()) == NULL) {
		printf("%% conf_interfaces: if_nameindex failed\n");
		return;
	}

	if ((ifs = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		printf("%% conf_interfaces socket: %s\n", strerror(errno));
		if_freenameindex(ifn_list);
		return;
	}

	for (ifnp = ifn_list; ifnp->if_name != NULL; ifnp++) {
		if (only && !isprefix(only, ifnp->if_name))
			/* only display interfaces which start with ... */
			continue;
		if (!only && islateif(ifnp->if_name))
			/* interface prefixes to exclude on generic run */
			continue;

		strlcpy(ifr.ifr_name, ifnp->if_name, sizeof(ifr.ifr_name));

		if (ioctl(ifs, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
			printf("%% conf: SIOCGIFFLAGS: %s\n", strerror(errno));
			continue;
		}
		flags = ifr.ifr_flags;

		ifr.ifr_data = (caddr_t)&if_data;
		if (ioctl(ifs, SIOCGIFDATA, (caddr_t)&ifr) < 0) {
			printf("%% conf: SIOCGIFDATA: %s\n", strerror(errno));
			continue;
		}

		/*
		 * Keep in mind that the order in which things are displayed
		 * here is important.  For instance, we want to setup the
		 * vlan tag before setting the IP address since the vlan
		 * must know what parent to inherit the parent interface
		 * flags from before it is brought up.  Another example of
		 * this would be that we need to setup the members on a
		 * bridge before we setup flags on them.
		 */

		/*
		 * set interface/bridge mode
		 */
		if (!(br = is_bridge(ifs, ifnp->if_name)))
			br = 0;
		fprintf(output, "%s %s\n", br ? "bridge" : "interface",
		    ifnp->if_name);

		/*
		 * description, if available
		 * copied straight from ifconfig.c
		 */
		memset(&ifrdesc, 0, sizeof(ifrdesc));
		strlcpy(ifrdesc.ifr_name, ifnp->if_name,
		    sizeof(ifrdesc.ifr_name));
		ifrdesc.ifr_data = (caddr_t)&ifdescr;
		if (ioctl(ifs, SIOCGIFDESCR, &ifrdesc) == 0 &&
		    strlen(ifrdesc.ifr_data))
			fprintf(output, " description %s\n", ifrdesc.ifr_data);

		/*
		 * print lladdr if necessary
		 */
		if ((lladdr = get_hwdaddr(ifnp->if_name)) != NULL) {
			/* We assume lladdr only useful if we can get_hwdaddr */
			StringList *hwdaddr;
			hwdaddr = sl_init();

			if (db_select_flag_x_ctl(hwdaddr, "lladdr",
			    ifnp->if_name) < 0) {
				printf("%% lladdr database select failed\n");
			}
			if (hwdaddr->sl_cur > 0 && (strcmp(hwdaddr->sl_str[0],
			    lladdr) != 0))
				fprintf(output, " lladdr %s\n", lladdr);
			sl_free(hwdaddr, 1);
		}
		 
		/*
		 * print vlan tag, parent if available.  if a tag is set
		 * but there is no parent, discard.
		 */
		bzero(&vreq, sizeof(struct vlanreq));
		ifr.ifr_data = (caddr_t)&vreq;  

		if (ioctl(ifs, SIOCGETVLAN, (caddr_t)&ifr) != -1) {
			if(vreq.vlr_tag && (vreq.vlr_parent[0] != '\0')) {
				fprintf(output, " vlan %d parent %s",
				    vreq.vlr_tag, vreq.vlr_parent);
				fprintf(output, "\n");
			}
		}

		conf_rdomain(output, ifs, ifnp->if_name);
		conf_intrtlabel(output, ifs, ifnp->if_name);
		conf_intgroup(output, ifs, ifnp->if_name);

		snprintf(leasefile, sizeof(leasefile), "%s.%s",
		    LEASEPREFIX, ifnp->if_name);
		if ((dhcpif = fopen(leasefile, "r"))) {
			fprintf(output, " ip dhcp\n");
			fclose(dhcpif);
			conf_ifaddrs(output, ifnp->if_name, flags, AF_INET6);
			ippntd = 1;
		} else {
			ippntd = conf_ifaddrs(output, ifnp->if_name, flags, 0);
		}

		if (br) {
			conf_brcfg(output, ifs, ifn_list, ifnp->if_name);
		} else {
			char tmp[24];

			conf_media_status(output, ifs, ifnp->if_name);
			conf_ifmetrics(output, ifs, if_data, ifnp->if_name);
			conf_keepalive(output, ifs, ifnp->if_name);
			conf_pfsync(output, ifs, ifnp->if_name);
			conf_carp(output, ifs, ifnp->if_name);
			conf_trunk(output, ifs, ifnp->if_name);
			conf_pflow(output, ifs, ifnp->if_name);
			conf_ifxflags(output, ifs, ifnp->if_name);
			if (timeslot_status(ifs, ifnp->if_name, tmp,
			    sizeof(tmp)) == 1) 
				fprintf(output, " timeslots %s\n", tmp);
			if (conf_dhcrelay(ifnp->if_name, tmp, sizeof(tmp))
			    > 0)
				fprintf(output, " dhcrelay %s\n", tmp);
		}

		/*
		 * print various flags
		 */
		if (flags & IFF_DEBUG)
			fprintf(output, " debug\n");
		if (flags & (IFF_LINK0|IFF_LINK1|IFF_LINK2)) {
			fprintf(output, " link ");
			if(flags & IFF_LINK0)
				fprintf(output, "0 ");
			if(flags & IFF_LINK1)
				fprintf(output, "1 ");
			if(flags & IFF_LINK2)
				fprintf(output, "2");
			fprintf(output, "\n");
		}
		if (flags & IFF_NOARP)
			fprintf(output, " no arp\n");
		/*
		 * ip X/Y turns the interface up (just like 'no shutdown')
		 * ...but if we never had an ip address set and the interface
		 * is up, we need to save this state explicitly.
		 */
		if (!ippntd && (flags & IFF_UP))
			fprintf(output, " no shutdown\n");
		else if (!(flags & IFF_UP))
			fprintf(output, " shutdown\n");
		fprintf(output, "!\n");
	}
	close(ifs);
	if_freenameindex(ifn_list);
}
Ejemplo n.º 5
0
Archivo: if.c Proyecto: yellowman/nsh
int
intlladdr(char *ifname, int ifs, int argc, char **argv)
{
	StringList *hwdaddr;
	char *lladdr, llorig[sizeof("00:00:00:00:00:00") + 1];
	struct ether_addr *addr;
	struct ifreq ifr;
	int set;

	if (NO_ARG(argv[0])) {
		argv++;
		argc--;
		set = 0;
	} else
		set = 1;

	if (set && argc < 2) {
		printf ("%% lladdr <link level address|random>\n");
		printf ("%% no lladdr\n");
		return(0);
	}

	if ((lladdr = get_hwdaddr(ifname)) == NULL) {
		printf("%% Failed to retrieve current link level address\n");
		return(1);
	}

	hwdaddr = sl_init();
	if (db_select_flag_x_ctl(hwdaddr, "lladdr", ifname) < 0) {
		printf("%% database failure select flag x ctl\n");
		sl_free(hwdaddr, 1);
		return(1);
	}
	if (hwdaddr->sl_cur > 0) {
		strlcpy(llorig, hwdaddr->sl_str[0], sizeof(llorig));
		if (!set && db_delete_flag_x_ctl("lladdr", ifname) < 0) {
				printf("%% database delete failure\n");
				sl_free(hwdaddr, 1);
				return(1);
		}
	} else {
		strlcpy(llorig, lladdr, sizeof(llorig));
		if (set && db_insert_flag_x("lladdr", ifname, 0, DB_X_ENABLE,
		    llorig) < 0) {
			printf("%% database delete failure\n");
			sl_free(hwdaddr, 1);
			return(1);
		}
		if (!set) {
			printf("%% No stored lladdr to reinstate\n");
			sl_free(hwdaddr, 1);
			return(1);
		}
	}
	sl_free(hwdaddr, 1);

	/* At this point, llorig will always represent the booted lladdr */

	if (set && isprefix(argv[1], "random")) {
		struct ether_addr eabuf;

		arc4random_buf(&eabuf, sizeof eabuf);
		eabuf.ether_addr_octet[0] &= 0xfc;
		addr = &eabuf;
	} else {
		addr = ether_aton(set ? argv[1] : llorig);
		if (addr == NULL) {
			if (set) {
				printf("%% MAC addresses are six hexadecimal "
				    "fields, up to two digits each,\n"
				    " %% separated with colons"
				    " (1:23:45:ab:cd:ef)\n");
				return(1);
			} else {
				printf("%% database corrupted, unable to "
				    " retrieve original lladdr\n");
				return(1);
			}
		} 
	}

	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
	ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
	ifr.ifr_addr.sa_family = AF_LINK;
	bcopy(addr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
	if(ioctl(ifs, SIOCSIFLLADDR, (caddr_t)&ifr) < 0) {
		switch(errno) {
		case EINVAL:
			printf("%% Requested link level address denied\n");
			break;
		default:
			printf("%% intlladdr: SIOCSIFLLADDR: %s\n",
			    strerror(errno));
		}
		return(1);
	}

	return(0);
}