Przeglądaj źródła

Fix wifi connectfile argument handling

connectfile built a full "connect \"ssid\" \"pass\" bssid" line and then
passed it to cmd_connect(), which expects only the arguments after the
verb.  It parsed "connect" as the SSID, the quoted SSID as the
passphrase, and the passphrase as the BSSID, so a correct password
always ended in "BSSID must be 12 hex digits".

Split the connect logic into connect_bss(ssid, pass, bssid): the CLI
parser and the staged /share/netmenu.wifi record both call it directly,
removing the string round-trip and its escaping rules.
Johannes Zellner 1 tydzień temu
rodzic
commit
88fcfed156
1 zmienionych plików z 32 dodań i 42 usunięć
  1. 32 42
      ucmd_wifi.c

+ 32 - 42
ucmd_wifi.c

@@ -348,28 +348,18 @@ static void spawn_keeper(struct wpa_sm *sm, unsigned int conn_id)
 		       "drop at the next group rekey\n");
 }
 
-static int cmd_connect(const char *args)
+static int connect_bss(const char *ssid, const char *pass,
+		       const unsigned char *want_bssid)
 {
-	char ssid[33], pass[80], bssid_s[13];
 	struct sys_wifi_connect c;
 	struct sys_wifi_status st;
 	static struct wpa_sm sm;
-	unsigned char pmk[32], want_bssid[6];
+	unsigned char pmk[32];
 	struct sys_wifi_bss *b = 0;
-	int n, i, r, have_bssid = 0;
+	int n, i, r;
 	unsigned int ssid_len;
+	int have_bssid = want_bssid != 0;
 
-	args = word(args, ssid, sizeof(ssid));
-	args = word(args, pass, sizeof(pass));
-	if (*skip_ws(args)) {
-		word(args, bssid_s, sizeof(bssid_s));
-		if (slen(bssid_s) != 12 ||
-		    unhex(bssid_s, want_bssid, 6) < 0) {
-			u_puts("wifi: BSSID must be 12 hex digits\n");
-			return 1;
-		}
-		have_bssid = 1;
-	}
 	ssid_len = (unsigned int)slen(ssid);
 	if (!ssid_len) {
 		u_puts("usage: wifi connect <ssid> [passphrase]\n");
@@ -466,20 +456,24 @@ static int cmd_connect(const char *args)
 	return 0;
 }
 
-/* Quote a raw byte string for the connect argument parser (which understands
- * backslash escapes inside double quotes). */
-static char *put_qbytes(char *p, const unsigned char *s, unsigned int n)
+static int cmd_connect(const char *args)
 {
-	unsigned int i;
+	char ssid[33], pass[80], bssid_s[13];
+	unsigned char want_bssid[6];
+	int have_bssid = 0;
 
-	*p++ = '"';
-	for (i = 0; i < n; i++) {
-		if (s[i] == '"' || s[i] == '\\')
-			*p++ = '\\';
-		*p++ = (char)s[i];
+	args = word(args, ssid, sizeof(ssid));
+	args = word(args, pass, sizeof(pass));
+	if (*skip_ws(args)) {
+		word(args, bssid_s, sizeof(bssid_s));
+		if (slen(bssid_s) != 12 ||
+		    unhex(bssid_s, want_bssid, 6) < 0) {
+			u_puts("wifi: BSSID must be 12 hex digits\n");
+			return 1;
+		}
+		have_bssid = 1;
 	}
-	*p++ = '"';
-	return p;
+	return connect_bss(ssid, pass, have_bssid ? want_bssid : 0);
 }
 
 /* `wifi connectfile <path>`: the network menu stages an SSID + passphrase +
@@ -487,10 +481,9 @@ static char *put_qbytes(char *p, const unsigned char *s, unsigned int n)
  * VFS_PATH_MAX (64) bytes.  The file is removed after it has been read. */
 static int cmd_connect_file(const char *args)
 {
-	static const char hx[] = "0123456789abcdef";
 	struct wifi_cfg cfg;
-	char path[80], line[320], *p;
-	unsigned int i, n;
+	char path[80], ssid[33], pass[65];
+	unsigned int i;
 	int r;
 
 	args = word(args, path, sizeof(path));
@@ -507,21 +500,18 @@ static int cmd_connect_file(const char *args)
 		u_puts("wifi: staged connect request is malformed\n");
 		return 1;
 	}
-	p = line;
-	for (n = 0; "connect "[n]; n++)
-		*p++ = "connect "[n];
-	p = put_qbytes(p, cfg.ssid, cfg.ssid_len);
-	*p++ = ' ';
-	p = put_qbytes(p, (const unsigned char *)cfg.pass, cfg.pass_len);
-	*p++ = ' ';
-	for (i = 0; i < 6; i++) {
-		*p++ = hx[cfg.bssid[i] >> 4];
-		*p++ = hx[cfg.bssid[i] & 15];
-	}
-	*p = '\0';
+	for (i = 0; i < cfg.ssid_len; i++)
+		ssid[i] = (char)cfg.ssid[i];
+	ssid[cfg.ssid_len] = '\0';
+	for (i = 0; i < cfg.pass_len; i++)
+		pass[i] = cfg.pass[i];
+	pass[cfg.pass_len] = '\0';
+	r = connect_bss(ssid, pass, cfg.bssid);
 	for (i = 0; i < sizeof(cfg); i++)
 		((unsigned char *)&cfg)[i] = 0;
-	return cmd_connect(line);
+	for (i = 0; i < sizeof(pass); i++)
+		((volatile char *)pass)[i] = 0;
+	return r;
 }
 
 /* Background: answer group-key rekeys until the connection changes. */