Module Name:    src
Committed By:   martin
Date:           Thu Jan 20 11:45:28 UTC 2022

Modified Files:
        src/usr.sbin/sysinst [netbsd-9]: msg.mi.de msg.mi.en msg.mi.es
            msg.mi.fr msg.mi.pl net.c

Log Message:
Pull up following revision(s) (requested by nia in ticket #1406):

        usr.sbin/sysinst/net.c: revision 1.40
        usr.sbin/sysinst/msg.mi.pl: revision 1.37
        usr.sbin/sysinst/msg.mi.fr: revision 1.36
        usr.sbin/sysinst/msg.mi.de: revision 1.35
        usr.sbin/sysinst/msg.mi.en: revision 1.37
        usr.sbin/sysinst/net.c: revision 1.37
        usr.sbin/sysinst/net.c: revision 1.38
        usr.sbin/sysinst/msg.mi.es: revision 1.31
        usr.sbin/sysinst/net.c: revision 1.39

sysinst(8): Add initial support for connecting to Wi-Fi networks
by spawning wpa_supplicant.
Reviewed by martin.

sysinst(8): Avoid asking Wi-Fi questions about non-Wi-Fi devices.

sysinst(8): Add a "data" argument to do_ifreq that is set after the
ifr struct is zeroed.

This allows us to simplify the code by not having 2x socket creation
code for checking whether interfaces are wireless.
sysinst(8): use scan_ssid=1 so hidden wifi networks can be used


To generate a diff of this commit:
cvs rdiff -u -r1.13.2.9 -r1.13.2.10 src/usr.sbin/sysinst/msg.mi.de
cvs rdiff -u -r1.19.2.10 -r1.19.2.11 src/usr.sbin/sysinst/msg.mi.en
cvs rdiff -u -r1.14.2.9 -r1.14.2.10 src/usr.sbin/sysinst/msg.mi.es
cvs rdiff -u -r1.17.2.10 -r1.17.2.11 src/usr.sbin/sysinst/msg.mi.fr
cvs rdiff -u -r1.20.2.10 -r1.20.2.11 src/usr.sbin/sysinst/msg.mi.pl
cvs rdiff -u -r1.33.2.2 -r1.33.2.3 src/usr.sbin/sysinst/net.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/sysinst/msg.mi.de
diff -u src/usr.sbin/sysinst/msg.mi.de:1.13.2.9 src/usr.sbin/sysinst/msg.mi.de:1.13.2.10
--- src/usr.sbin/sysinst/msg.mi.de:1.13.2.9	Thu Nov  5 08:10:21 2020
+++ src/usr.sbin/sysinst/msg.mi.de	Thu Jan 20 11:45:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg.mi.de,v 1.13.2.9 2020/11/05 08:10:21 sborrill Exp $	*/
+/*	$NetBSD: msg.mi.de,v 1.13.2.10 2022/01/20 11:45:28 martin Exp $	*/
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -639,6 +639,12 @@ message net_defroute
 message net_media
 {Art der Netzwerkverbindung}
 
+message net_ssid
+{WLAN-SSID?}
+
+message net_passphrase
+{WLAN-Passwort?}
+
 message netok
 {Nachfolgend sehen Sie die aktuellen Netzwerkeinstellungen.
 

Index: src/usr.sbin/sysinst/msg.mi.en
diff -u src/usr.sbin/sysinst/msg.mi.en:1.19.2.10 src/usr.sbin/sysinst/msg.mi.en:1.19.2.11
--- src/usr.sbin/sysinst/msg.mi.en:1.19.2.10	Thu Nov  5 08:10:21 2020
+++ src/usr.sbin/sysinst/msg.mi.en	Thu Jan 20 11:45:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg.mi.en,v 1.19.2.10 2020/11/05 08:10:21 sborrill Exp $	*/
+/*	$NetBSD: msg.mi.en,v 1.19.2.11 2022/01/20 11:45:28 martin Exp $	*/
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -616,6 +616,12 @@ message net_defroute
 message net_media
 {Network media type}
 
+message net_ssid
+{Wi-Fi SSID?}
+
+message net_passphrase
+{Wi-Fi passphrase?}
+
 message netok
 {The following are the values you entered.
 

Index: src/usr.sbin/sysinst/msg.mi.es
diff -u src/usr.sbin/sysinst/msg.mi.es:1.14.2.9 src/usr.sbin/sysinst/msg.mi.es:1.14.2.10
--- src/usr.sbin/sysinst/msg.mi.es:1.14.2.9	Thu Nov  5 08:10:21 2020
+++ src/usr.sbin/sysinst/msg.mi.es	Thu Jan 20 11:45:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg.mi.es,v 1.14.2.9 2020/11/05 08:10:21 sborrill Exp $	*/
+/*	$NetBSD: msg.mi.es,v 1.14.2.10 2022/01/20 11:45:28 martin Exp $	*/
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -632,6 +632,12 @@ message net_defroute
 message net_media
 {Tipo de medio de la red}
 
+message net_ssid
+{Wi-Fi SSID?}
+
+message net_passphrase
+{Wi-Fi passphrase?}
+
 message netok
 {Ha introducido los siguientes valores.
 

Index: src/usr.sbin/sysinst/msg.mi.fr
diff -u src/usr.sbin/sysinst/msg.mi.fr:1.17.2.10 src/usr.sbin/sysinst/msg.mi.fr:1.17.2.11
--- src/usr.sbin/sysinst/msg.mi.fr:1.17.2.10	Thu Nov  5 08:10:21 2020
+++ src/usr.sbin/sysinst/msg.mi.fr	Thu Jan 20 11:45:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg.mi.fr,v 1.17.2.10 2020/11/05 08:10:21 sborrill Exp $	*/
+/*	$NetBSD: msg.mi.fr,v 1.17.2.11 2022/01/20 11:45:28 martin Exp $	*/
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -626,6 +626,12 @@ message net_defroute
 message net_media
 {Type de réseau (média)}
 
+message net_ssid
+{Wi-Fi SSID?}
+
+message net_passphrase
+{Wi-Fi passphrase?}
+
 message netok
 {Vous avez spécifié les valeurs suivantes :
 

Index: src/usr.sbin/sysinst/msg.mi.pl
diff -u src/usr.sbin/sysinst/msg.mi.pl:1.20.2.10 src/usr.sbin/sysinst/msg.mi.pl:1.20.2.11
--- src/usr.sbin/sysinst/msg.mi.pl:1.20.2.10	Thu Nov  5 08:10:21 2020
+++ src/usr.sbin/sysinst/msg.mi.pl	Thu Jan 20 11:45:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg.mi.pl,v 1.20.2.10 2020/11/05 08:10:21 sborrill Exp $	*/
+/*	$NetBSD: msg.mi.pl,v 1.20.2.11 2022/01/20 11:45:28 martin Exp $	*/
 /*	Based on english version: */
 /*	NetBSD: msg.mi.pl,v 1.36 2004/04/17 18:55:35 atatat Exp       */
 
@@ -611,6 +611,12 @@ message net_defroute
 message net_media
 {Typ interfejsu sieciowego}
 
+message net_ssid
+{Wi-Fi SSID?}
+
+message net_passphrase
+{Wi-Fi passphrase?}
+
 message netok
 {Ponizej sa wartosci, ktore wprowadziles.
 

Index: src/usr.sbin/sysinst/net.c
diff -u src/usr.sbin/sysinst/net.c:1.33.2.2 src/usr.sbin/sysinst/net.c:1.33.2.3
--- src/usr.sbin/sysinst/net.c:1.33.2.2	Tue Jan 28 10:17:58 2020
+++ src/usr.sbin/sysinst/net.c	Thu Jan 20 11:45:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: net.c,v 1.33.2.2 2020/01/28 10:17:58 msaitoh Exp $	*/
+/*	$NetBSD: net.c,v 1.33.2.3 2022/01/20 11:45:28 martin Exp $	*/
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -47,6 +47,7 @@
 #include <net/if.h>
 #include <net/if_media.h>
 #include <netinet/in.h>
+#include <net80211/ieee80211_ioctl.h>
 
 #include <err.h>
 #include <stdio.h>
@@ -79,6 +80,8 @@ static char net_mask[SSTRSIZE];
 char net_namesvr[STRSIZE];
 static char net_defroute[STRSIZE];
 static char net_media[STRSIZE];
+static char net_ssid[STRSIZE];
+static char net_passphrase[STRSIZE];
 static char sl_flags[STRSIZE];
 static int net_dhcpconf;
 #define DHCPCONF_IPADDR         0x01
@@ -100,8 +103,11 @@ static char *url_encode (char *dst, cons
 static void write_etc_hosts(FILE *f);
 
 #define DHCPCD "/sbin/dhcpcd"
+#define WPA_SUPPLICANT "/usr/sbin/wpa_supplicant"
 #include <signal.h>
+static int config_eth_medium(char *);
 static int config_dhcp(char *);
+static int config_wlan(char *);
 
 #ifdef INET6
 static int is_v6kernel (void);
@@ -269,7 +275,7 @@ get_ifconfig_info(struct net_desc *devs)
 }
 
 static int
-do_ifreq(struct ifreq *ifr, unsigned long cmd)
+do_ifreq(struct ifreq *ifr, unsigned long cmd, void *data)
 {
 	int sock;
 	int rval;
@@ -279,6 +285,7 @@ do_ifreq(struct ifreq *ifr, unsigned lon
 		return -1;
 
 	memset(ifr, 0, sizeof *ifr);
+	ifr->ifr_data = data;
 	strlcpy(ifr->ifr_name, net_dev, sizeof ifr->ifr_name);
 	rval = ioctl(sock, cmd, ifr);
 	close(sock);
@@ -315,10 +322,12 @@ get_ifinterface_info(void)
 	const char *media_opt;
 	const char *sep;
 
-	if (do_ifreq(&ifr, SIOCGIFADDR) == 0 && sa_in->sin_addr.s_addr != 0)
+	if (do_ifreq(&ifr, SIOCGIFADDR, NULL) == 0 &&
+	    sa_in->sin_addr.s_addr != 0)
 		strlcpy(net_ip, inet_ntoa(sa_in->sin_addr), sizeof net_ip);
 
-	if (do_ifreq(&ifr, SIOCGIFNETMASK) == 0 && sa_in->sin_addr.s_addr != 0)
+	if (do_ifreq(&ifr, SIOCGIFNETMASK, NULL) == 0 &&
+	    sa_in->sin_addr.s_addr != 0)
 		strlcpy(net_mask, inet_ntoa(sa_in->sin_addr), sizeof net_mask);
 
 	if (do_ifmreq(&ifmr, SIOCGIFMEDIA) == 0) {
@@ -562,45 +571,8 @@ again:
 		/* domain and host */
 		msg_display(MSG_netinfo);
 
-		/* ethernet medium */
-		for (;;) {
-			msg_prompt_add(MSG_net_media, net_media, net_media,
-					sizeof net_media);
-
-			/*
-			 * ifconfig does not allow media specifiers on
-			 * IFM_MANUAL interfaces.  Our UI gives no way
-			 * to set an option back
-			 * to null-string if it gets accidentally set.
-			 * Check for plausible alternatives.
-			 */
-			if (strcmp(net_media, "<default>") == 0 ||
-			    strcmp(net_media, "default") == 0 ||
-			    strcmp(net_media, "<manual>") == 0 ||
-			    strcmp(net_media, "manual") == 0 ||
-			    strcmp(net_media, "<none>") == 0 ||
-			    strcmp(net_media, "none") == 0 ||
-			    strcmp(net_media, " ") == 0) {
-				*net_media = '\0';
-			}
-
-			if (*net_media == '\0')
-				break;
-			/*
-			 * We must set the media type here - to give dhcp
-			 * a chance
-			 */
-			if (run_program(0, "/sbin/ifconfig %s media %s",
-				    net_dev, net_media) == 0)
-				break;
-			/* Failed to set - output the supported values */
-			if (collect(T_OUTPUT, &textbuf, "/sbin/ifconfig -m %s |"
-				    "while IFS=; read line;"
-				    " do [ \"$line\" = \"${line#*media}\" ] || "
-				    "echo $line;"
-				    " done", net_dev ) > 0)
-				msg_display(textbuf);
-			free(textbuf);
+		if (!config_wlan(net_dev)) {
+			config_eth_medium(net_dev);
 		}
 
 		net_dhcpconf = 0;
@@ -1092,6 +1064,10 @@ mnt_net_config(void)
 	if (net_namesvr[0] != '\0')
 		dup_file_into_target("/etc/resolv.conf");
 
+	/* Copy wpa_supplicant.conf to target. */
+	if (net_ssid[0] != '\0')
+		dup_file_into_target("/etc/wpa_supplicant.conf");
+
 	/*
 	 * bring the interface up, it will be necessary for IPv6, and
 	 * it won't make trouble with IPv4 case either
@@ -1149,6 +1125,11 @@ mnt_net_config(void)
 		add_rc_conf("dhcpcd_flags=\"-qM %s\"\n", net_dev);
         }
 
+	if (net_ssid[0] != '\0') {
+		add_rc_conf("wpa_supplicant=YES\n");
+		add_rc_conf("wpa_supplicant_flags=\"-B -s -i %s -D bsd -c /etc/wpa_supplicant.conf\"\n", net_dev);
+	}
+
 	if (ifconf)
 		fclose(ifconf);
 
@@ -1156,6 +1137,69 @@ mnt_net_config(void)
 }
 
 int
+config_wlan(char *inter)
+{
+	FILE *wpa_conf = NULL;
+	char wpa_cmd[256];
+	struct ifreq ifr = {0};
+	struct ieee80211_nwid nwid = {0};
+
+	/* skip non-WLAN devices */
+	if (do_ifreq(&ifr, SIOCG80211NWID, &nwid) == -1)
+		return 0;
+
+	if (!file_mode_match(WPA_SUPPLICANT, S_IFREG))
+		return 0;
+
+	msg_prompt_add(MSG_net_ssid, net_ssid, net_ssid,
+			sizeof net_ssid);
+	if (net_ssid[0] == '\0')
+		return 0;
+
+	msg_prompt_noecho(MSG_net_passphrase, net_passphrase, net_passphrase,
+			sizeof net_passphrase);
+
+	wpa_conf = fopen("/etc/wpa_supplicant.conf", "a");
+	if (wpa_conf == NULL)
+		return 0;
+
+	scripting_fprintf(NULL,
+	    "cat <<EOF >>%s/etc/wpa_supplicant.conf\n",
+	    target_prefix());
+	scripting_fprintf(wpa_conf, "\n#\n");
+	scripting_fprintf(wpa_conf, "# Added by NetBSD sysinst\n");
+	scripting_fprintf(wpa_conf, "#\n");
+	scripting_fprintf(wpa_conf, "network={\n");
+	scripting_fprintf(wpa_conf,
+	    "\tssid=\"%s\"\n", net_ssid);
+	if (net_passphrase[0] != '\0') {
+		scripting_fprintf(wpa_conf, "\tpsk=\"%s\"\n",
+		    net_passphrase);
+	} else {
+		scripting_fprintf(wpa_conf, "\tkey_mgmt=NONE\n");
+	}
+	scripting_fprintf(wpa_conf, "\tscan_ssid=1\n");
+	scripting_fprintf(wpa_conf, "}\n");
+	(void)fclose(wpa_conf);
+	scripting_fprintf(NULL, "EOF\n");
+
+	if (run_program(RUN_DISPLAY | RUN_PROGRESS,
+	    "/sbin/ifconfig %s up", inter) != 0)
+		return 0;
+
+	/*
+	 * have to use system() here to avoid the server process dying
+	 */
+	if (snprintf(wpa_cmd, sizeof(wpa_cmd),
+	    WPA_SUPPLICANT
+	    " -B -s -i %s -D bsd -c /etc/wpa_supplicant.conf", inter) < 0)
+		return 0;
+	(void)do_system(wpa_cmd);
+
+	return 1;
+}
+
+int
 config_dhcp(char *inter)
 {
 	int dhcpautoconf;
@@ -1176,3 +1220,51 @@ config_dhcp(char *inter)
 	}
 	return 0;
 }
+
+
+int
+config_eth_medium(char *inter)
+{
+	char *textbuf = NULL;
+
+	for (;;) {
+		msg_prompt_add(MSG_net_media, net_media, net_media,
+				sizeof net_media);
+
+		/*
+		 * ifconfig does not allow media specifiers on
+		 * IFM_MANUAL interfaces.  Our UI gives no way
+		 * to set an option back
+		 * to null-string if it gets accidentally set.
+		 * Check for plausible alternatives.
+		 */
+		if (strcmp(net_media, "<default>") == 0 ||
+		    strcmp(net_media, "default") == 0 ||
+		    strcmp(net_media, "<manual>") == 0 ||
+		    strcmp(net_media, "manual") == 0 ||
+		    strcmp(net_media, "<none>") == 0 ||
+		    strcmp(net_media, "none") == 0 ||
+		    strcmp(net_media, " ") == 0) {
+			*net_media = '\0';
+		}
+
+		if (*net_media == '\0')
+			break;
+		/*
+		 * We must set the media type here - to give dhcp
+		 * a chance
+		 */
+		if (run_program(0, "/sbin/ifconfig %s media %s",
+			    net_dev, net_media) == 0)
+			break;
+		/* Failed to set - output the supported values */
+		if (collect(T_OUTPUT, &textbuf, "/sbin/ifconfig -m %s |"
+			    "while IFS=; read line;"
+			    " do [ \"$line\" = \"${line#*media}\" ] || "
+			    "echo $line;"
+			    " done", net_dev ) > 0)
+			msg_display(textbuf);
+		free(textbuf);
+	}
+	return 0;
+}

Reply via email to