changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > plan9front / changeset: sshnet: actually make sure ssh established connection before exiting main proc

changeset 7118: 177a74aa942a
parent 7117: 81227123e1b8
child 7119: 47b33e9a0878
author: cinap_lenrek@felloff.net
date: Tue, 02 Apr 2019 17:28:56 +0200
files: sys/src/cmd/sshnet.c
description: sshnet: actually make sure ssh established connection before exiting main proc

this fixes password prompts and handles errors properly.
     1.1--- a/sys/src/cmd/sshnet.c
     1.2+++ b/sys/src/cmd/sshnet.c
     1.3@@ -32,6 +32,7 @@ enum
     1.4 #define TYPE(path)		((int)(path) & 0xFF)
     1.5 #define NUM(path)		((uint)(path)>>8)
     1.6 
     1.7+Channel *ssherrchan;		/* chan(char*) */
     1.8 Channel *sshmsgchan;		/* chan(Msg*) */
     1.9 Channel *fsreqchan;		/* chan(Req*) */
    1.10 Channel *fsreqwaitchan;		/* chan(nil) */
    1.11@@ -92,6 +93,8 @@ enum {
    1.12 
    1.13 	MaxPacket = 1<<15,
    1.14 	WinPackets = 8,
    1.15+
    1.16+	SESSIONCHAN = 1<<24,
    1.17 };
    1.18 
    1.19 struct Msg
    1.20@@ -1028,7 +1031,7 @@ fsflush(Req *r)
    1.21 static void
    1.22 handlemsg(Msg *m)
    1.23 {
    1.24-	int chan, win, pkt, n;
    1.25+	int chan, win, pkt, n, l;
    1.26 	Client *c;
    1.27 	char *s;
    1.28 
    1.29@@ -1075,6 +1078,10 @@ handlemsg(Msg *m)
    1.30 	case MSG_CHANNEL_OPEN_CONFIRMATION:
    1.31 		if(unpack(m, "_uuuu", &chan, &n, &win, &pkt) < 0)
    1.32 			break;
    1.33+		if(chan == SESSIONCHAN){
    1.34+			sendp(ssherrchan, nil);
    1.35+			break;
    1.36+		}
    1.37 		c = getclient(chan);
    1.38 		if(c == nil || c->state != Dialing)
    1.39 			break;
    1.40@@ -1087,8 +1094,12 @@ handlemsg(Msg *m)
    1.41 		dialedclient(c);
    1.42 		break;
    1.43 	case MSG_CHANNEL_OPEN_FAILURE:
    1.44-		if(unpack(m, "_uu", &chan, &n) < 0)
    1.45+		if(unpack(m, "_uus", &chan, &n, &s, &l) < 0)
    1.46 			break;
    1.47+		if(chan == SESSIONCHAN){
    1.48+			sendp(ssherrchan, smprint("%.*s", utfnlen(s, l), s));
    1.49+			break;
    1.50+		}
    1.51 		c = getclient(chan);
    1.52 		if(c == nil || c->state != Dialing)
    1.53 			break;
    1.54@@ -1228,6 +1239,48 @@ startssh(void *)
    1.55 }
    1.56 
    1.57 void
    1.58+ssh(int argc, char *argv[])
    1.59+{
    1.60+	Alt a[3];
    1.61+	Waitmsg *w;
    1.62+	char *e;
    1.63+
    1.64+	sshargc = argc + 2;
    1.65+	sshargv = emalloc9p(sizeof(char *) * (sshargc + 1));
    1.66+	sshargv[0] = "ssh";
    1.67+	sshargv[1] = "-X";
    1.68+	memcpy(sshargv + 2, argv, argc * sizeof(char *));
    1.69+
    1.70+	pipe(pfd);
    1.71+	sshfd = pfd[0];
    1.72+	procrfork(startssh, nil, mainstacksize, RFFDG|RFNOTEG|RFNAMEG);
    1.73+	close(pfd[1]);
    1.74+
    1.75+	sendmsg(pack(nil, "bsuuu", MSG_CHANNEL_OPEN,
    1.76+		"session", 7,
    1.77+		SESSIONCHAN,
    1.78+		MaxPacket,
    1.79+		MaxPacket));
    1.80+
    1.81+	a[0].op = CHANRCV;
    1.82+	a[0].c = threadwaitchan();
    1.83+	a[0].v = &w;
    1.84+	a[1].op = CHANRCV;
    1.85+	a[1].c = ssherrchan;
    1.86+	a[1].v = &e;
    1.87+	a[2].op = CHANEND;
    1.88+
    1.89+	switch(alt(a)){
    1.90+	case 0:
    1.91+		sysfatal("ssh failed: %s", w->msg);
    1.92+	case 1:
    1.93+		if(e != nil)
    1.94+			sysfatal("ssh failed: %s", e);
    1.95+	}
    1.96+	chanclose(ssherrchan);
    1.97+}
    1.98+
    1.99+void
   1.100 usage(void)
   1.101 {
   1.102 	fprint(2, "usage: sshnet [-m mtpt] [ssh options]\n");
   1.103@@ -1259,27 +1312,18 @@ threadmain(int argc, char **argv)
   1.104 
   1.105 	if(argc == 0)
   1.106 		usage();
   1.107-	
   1.108-	sshargc = argc + 2;
   1.109-	sshargv = emalloc9p(sizeof(char *) * (sshargc + 1));
   1.110-	sshargv[0] = "ssh";
   1.111-	sshargv[1] = "-X";
   1.112-	memcpy(sshargv + 2, argv, argc * sizeof(char *));
   1.113-
   1.114-	pipe(pfd);
   1.115-	sshfd = pfd[0];
   1.116-	procrfork(startssh, nil, mainstacksize, RFFDG|RFNOTEG|RFNAMEG);
   1.117-	close(pfd[1]);
   1.118 
   1.119 	time0 = time(0);
   1.120+	ssherrchan = chancreate(sizeof(char*), 0);
   1.121 	sshmsgchan = chancreate(sizeof(Msg*), 16);
   1.122 	fsreqchan = chancreate(sizeof(Req*), 0);
   1.123 	fsreqwaitchan = chancreate(sizeof(void*), 0);
   1.124 	fsclunkchan = chancreate(sizeof(Fid*), 0);
   1.125 	fsclunkwaitchan = chancreate(sizeof(void*), 0);
   1.126-
   1.127+	procrfork(fsnetproc, nil, mainstacksize, RFNAMEG|RFNOTEG);
   1.128 	procrfork(sshreadproc, nil, mainstacksize, RFNAMEG|RFNOTEG);
   1.129-	procrfork(fsnetproc, nil, mainstacksize, RFNAMEG|RFNOTEG);
   1.130+
   1.131+	ssh(argc, argv);
   1.132 
   1.133 	threadpostmountsrv(&fs, service, mtpt, MREPL);
   1.134 	exits(0);