9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] bnetkey: graphic netkey for the bitsy
@ 2002-05-27 13:56 Fco.J.Ballesteros
  0 siblings, 0 replies; 2+ messages in thread
From: Fco.J.Ballesteros @ 2002-05-27 13:56 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 277 bytes --]

I use this to perform challenge-response authentication
using the bitsy instead of netkey(1). hth.

BTW, I added p9cr auth again to imap4d by adding a -c option that
enables it. Would it be possible to get this on the distribution?
I can send the changed file.

thanks

[-- Attachment #2: bnetkey.c --]
[-- Type: text/plain, Size: 6032 bytes --]

#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <control.h>
#include <authsrv.h>


enum {
	Ncol	= 3,
	Bsz	= 100,
	Dpylen	= 40,
};

/* If PASS is not "", bnetkey will use it instead of asking for
 * your password.
 * That avoids typing but is a security breach, do it at your own risk.
 */

#define PASS		""
#define PASSWARN	"This file should be just on the bitsy. remove it."
#define	Dflcol		"darkyellow"

typedef struct But But;
struct But {
	char*		name;
	Control*	ctl;
	char*		backimg;
	void		(*press)(int);
};

void putdigit(int);
void clrdpy(int);
void response(int);
void readln(char*, char*, int, int);

But	pad[] ={
		{ "1",	nil,	nil,	putdigit },
		{ "2",	nil,	nil,	putdigit },
		{ "3",	nil,	nil,	putdigit },
		{ "4",	nil,	nil,	putdigit },
		{ "5",	nil,	nil,	putdigit },
		{ "6",	nil,	nil,	putdigit },
		{ "7",	nil,	nil,	putdigit },
		{ "8",	nil,	nil,	putdigit },
		{ "9",	nil,	nil,	putdigit },
		{ "EN",	nil,	"cyan",	response },
		{ "0",	nil,	nil,	putdigit },
		{ "CL",	nil,	"cyan",	clrdpy   },
};

Control*	col[Ncol];
Control*	dpy;
Controlset*	cs;
char		dpytext[Bsz];
char 		pass[10];
char		key[DESKEYLEN];
int		ctldeletequits = 1;


void
response(int)
{
	int chal;
	char buf[32];

	chal = atoi(dpytext);
	netcrypt(key, buf);
	chanprint(cs->ctl, "dpy value %s", buf);
	strcpy(dpytext, "0");
}

void
putdigit(int i)
{

	if (strlen(dpytext) > Dpylen)
		return;
	strcat(dpytext, pad[i].name);
	i = atoi(dpytext);
	sprint(dpytext, "%d", i);
	chanprint(cs->ctl, "dpy value %s", dpytext);
}

void
clrdpy(int)
{
	strcpy(dpytext, "0");
	chanprint(cs->ctl, "dpy value %s", dpytext);
}

void
resizecontrolset(Controlset*)
{
	Rectangle r;
	Rectangle rcol[Ncol], rbut, rdpy;
	int	dx, dy;
	int	i, n;

	if (getwindow(display, Refnone) < 0)
		sysfatal("resizecontrolset: %r");

	r = insetrect(screen->r, 10);
	dx = r.max.x - r.min.x;
	dx /= Ncol;
	dy = r.max.y - r.min.y;
	dy /= (nelem(pad)/Ncol + 1);

	rdpy = r;
	rdpy.max.x -= 10;
	rdpy.min.x += 10;
	rdpy.max.y = rdpy.min.y + dy - 3;
	chanprint(cs->ctl, "dpy rect %R\ndpy show", rdpy);
	for (i = 0; i < Ncol; i++){
		rcol[i] = r;
		rcol[i].min.y += dy;
		rcol[i].min.x += dx * i;
		rcol[i].max.x += dx * (i+1);
	}
	for (i = 0; i < nelem(pad); i++){
		rbut.min.x  = r.min.x + dx * (i%Ncol);
		rbut.max.x  = rbut.min.x + dx;
		rbut.min.y  = r.min.y + dy * (i/Ncol) + dy;
		rbut.max.y  = rbut.min.y + dy;
		rbut = insetrect(rbut, 2);
		chanprint(cs->ctl, "%s rect %R", pad[i].name, rbut);
	}
	for (i = 0; i < Ncol; i++)
		chanprint(cs->ctl, "c%d size\nc%d rect %R\nc%d show",
				i, i, rcol[i], i);
	for (i = 0; i < nelem(pad); i++)
		chanprint(cs->ctl, "%s show", pad[i].name);
}


void
threadmain(int, char**)
{
	Channel* 	c;		// events
	char*		ev;
	char		buf[Bsz];
	char*		args[10];
	char*		s;
	char*		in;
	int		i;

	s = getenv("service");
	if(s && strcmp(s, "cpu") == 0){
		fprint(2, "bnetkey must not be run on a cpu server\n");
		exits("grrr");
	}
	strecpy(pass, pass+sizeof pass, PASS);
	if (pass[0] != 0){
		if (stat("#r/backlight", (uchar*)buf, Bsz) < 0){
			fprint(2, "%s\n", PASSWARN);
			exits("grrr");
		}
	} else
		readln("Password: ", pass, sizeof pass, 1);

	c = chancreate(sizeof(char*), 0);
	initdraw(0, 0, "bnetkey");
	initcontrols();
	cs = newcontrolset(screen, nil, nil, nil);

	dpy = createlabel(cs, "dpy");
	chanprint(cs->ctl, "dpy bordercolor black\ndpy align centerright");
	chanprint(cs->ctl, "dpy border 0\ndpy textcolor black");
	strcpy(dpytext, "0");
	chanprint(cs->ctl, "dpy value %s\ndpy size 10 10 100 50", dpytext);
	controlwire(dpy, "event", c);
	activate(dpy);

	for (i = 0; i < Ncol; i++){
		seprint(buf, buf+Bsz, "c%d", i);
		col[i] = createcolumn(cs, buf);
		seprint(buf, buf+Bsz, "c%d separation 3\n",i);
		chanprint(cs->ctl, buf);
	}
	for (i = 0; i < nelem(pad); i++){
		s = pad[i].name;
		pad[i].ctl = createtextbutton(cs, pad[i].name);
		seprint(buf, buf+Bsz, "%s align center\n%s text %s",
			s, s, s);
		chanprint(cs->ctl, buf);
		if (pad[i].backimg == nil)
			pad[i].backimg = Dflcol;
		seprint(buf, buf+Bsz, "%s image %s", s, pad[i].backimg);
		chanprint(cs->ctl, buf);
		seprint(buf, buf+Bsz, "%s size 10 10 100 100", s);
		chanprint(cs->ctl, buf);
		controlwire(pad[i].ctl, "event", c);
		seprint(buf, buf+Bsz, "c%d add %s", i % Ncol, s);
		activate(pad[i].ctl);
	}

	for (i = 0; i < Ncol; i++){
		controlwire(col[i], "event", c);
		activate(col[i]);
	}

	resizecontrolset(cs);
	passtokey(key, pass);
	for(;;){
		ev = recvp(c);
		i = tokenize(ev, args, nelem(args));
		if (i == 3 && strcmp(args[1], "value")==0){
			sleep(1);
			args[0][strlen(args[0])-1] = 0;
			chanprint(cs->ctl, "%s value 0", args[0]);
			for (i = 0; i < nelem(pad); i++)
				if (strcmp(pad[i].name, args[0]) == 0)
				if (pad[i].press)
					pad[i].press(i);
		}
	}
}

void
readln(char *prompt, char *line, int len, int raw)
{
	char *p;
	int fdin, fdout, ctl, n, nr;

	fdin = open("/dev/cons", OREAD);
	fdout = open("/dev/cons", OWRITE);
	fprint(fdout, "%s", prompt);
	if(raw){
		ctl = open("/dev/consctl", OWRITE);
		if(ctl < 0){
			fprint(2, "couldn't set raw mode");
			exits("readln");
		}
		write(ctl, "rawon", 5);
	} else
		ctl = -1;
	nr = 0;
	p = line;
	for(;;){
		n = read(fdin, p, 1);
		if(n < 0){
			close(ctl);
			close(fdin);
			close(fdout);
			fprint(2, "can't read cons");
			exits("readln");
		}
		if(*p == 0x7f)
			exits(0);
		if(n == 0 || *p == '\n' || *p == '\r'){
			*p = '\0';
			if(raw){
				write(ctl, "rawoff", 6);
				write(fdout, "\n", 1);
			}
			close(ctl);
			close(fdin);
			close(fdout);
			return;
		}
		if(*p == '\b'){
			if(nr > 0){
				nr--;
				p--;
			}
		}else{
			nr++;
			p++;
		}
		if(nr == len){
			fprint(fdout, "line too long; try again\n");
			nr = 0;
			p = line;
		}
	}
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [9fans] bnetkey: graphic netkey for the bitsy
@ 2002-05-27 14:55 presotto
  0 siblings, 0 replies; 2+ messages in thread
From: presotto @ 2002-05-27 14:55 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 5 bytes --]

Sure.

[-- Attachment #2: Type: message/rfc822, Size: 7947 bytes --]

[-- Attachment #2.1.1: Type: text/plain, Size: 277 bytes --]

I use this to perform challenge-response authentication
using the bitsy instead of netkey(1). hth.

BTW, I added p9cr auth again to imap4d by adding a -c option that
enables it. Would it be possible to get this on the distribution?
I can send the changed file.

thanks

[-- Attachment #2.1.2: bnetkey.c --]
[-- Type: text/plain, Size: 6032 bytes --]

#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <control.h>
#include <authsrv.h>


enum {
	Ncol	= 3,
	Bsz	= 100,
	Dpylen	= 40,
};

/* If PASS is not "", bnetkey will use it instead of asking for
 * your password.
 * That avoids typing but is a security breach, do it at your own risk.
 */

#define PASS		""
#define PASSWARN	"This file should be just on the bitsy. remove it."
#define	Dflcol		"darkyellow"

typedef struct But But;
struct But {
	char*		name;
	Control*	ctl;
	char*		backimg;
	void		(*press)(int);
};

void putdigit(int);
void clrdpy(int);
void response(int);
void readln(char*, char*, int, int);

But	pad[] ={
		{ "1",	nil,	nil,	putdigit },
		{ "2",	nil,	nil,	putdigit },
		{ "3",	nil,	nil,	putdigit },
		{ "4",	nil,	nil,	putdigit },
		{ "5",	nil,	nil,	putdigit },
		{ "6",	nil,	nil,	putdigit },
		{ "7",	nil,	nil,	putdigit },
		{ "8",	nil,	nil,	putdigit },
		{ "9",	nil,	nil,	putdigit },
		{ "EN",	nil,	"cyan",	response },
		{ "0",	nil,	nil,	putdigit },
		{ "CL",	nil,	"cyan",	clrdpy   },
};

Control*	col[Ncol];
Control*	dpy;
Controlset*	cs;
char		dpytext[Bsz];
char 		pass[10];
char		key[DESKEYLEN];
int		ctldeletequits = 1;


void
response(int)
{
	int chal;
	char buf[32];

	chal = atoi(dpytext);
	netcrypt(key, buf);
	chanprint(cs->ctl, "dpy value %s", buf);
	strcpy(dpytext, "0");
}

void
putdigit(int i)
{

	if (strlen(dpytext) > Dpylen)
		return;
	strcat(dpytext, pad[i].name);
	i = atoi(dpytext);
	sprint(dpytext, "%d", i);
	chanprint(cs->ctl, "dpy value %s", dpytext);
}

void
clrdpy(int)
{
	strcpy(dpytext, "0");
	chanprint(cs->ctl, "dpy value %s", dpytext);
}

void
resizecontrolset(Controlset*)
{
	Rectangle r;
	Rectangle rcol[Ncol], rbut, rdpy;
	int	dx, dy;
	int	i, n;

	if (getwindow(display, Refnone) < 0)
		sysfatal("resizecontrolset: %r");

	r = insetrect(screen->r, 10);
	dx = r.max.x - r.min.x;
	dx /= Ncol;
	dy = r.max.y - r.min.y;
	dy /= (nelem(pad)/Ncol + 1);

	rdpy = r;
	rdpy.max.x -= 10;
	rdpy.min.x += 10;
	rdpy.max.y = rdpy.min.y + dy - 3;
	chanprint(cs->ctl, "dpy rect %R\ndpy show", rdpy);
	for (i = 0; i < Ncol; i++){
		rcol[i] = r;
		rcol[i].min.y += dy;
		rcol[i].min.x += dx * i;
		rcol[i].max.x += dx * (i+1);
	}
	for (i = 0; i < nelem(pad); i++){
		rbut.min.x  = r.min.x + dx * (i%Ncol);
		rbut.max.x  = rbut.min.x + dx;
		rbut.min.y  = r.min.y + dy * (i/Ncol) + dy;
		rbut.max.y  = rbut.min.y + dy;
		rbut = insetrect(rbut, 2);
		chanprint(cs->ctl, "%s rect %R", pad[i].name, rbut);
	}
	for (i = 0; i < Ncol; i++)
		chanprint(cs->ctl, "c%d size\nc%d rect %R\nc%d show",
				i, i, rcol[i], i);
	for (i = 0; i < nelem(pad); i++)
		chanprint(cs->ctl, "%s show", pad[i].name);
}


void
threadmain(int, char**)
{
	Channel* 	c;		// events
	char*		ev;
	char		buf[Bsz];
	char*		args[10];
	char*		s;
	char*		in;
	int		i;

	s = getenv("service");
	if(s && strcmp(s, "cpu") == 0){
		fprint(2, "bnetkey must not be run on a cpu server\n");
		exits("grrr");
	}
	strecpy(pass, pass+sizeof pass, PASS);
	if (pass[0] != 0){
		if (stat("#r/backlight", (uchar*)buf, Bsz) < 0){
			fprint(2, "%s\n", PASSWARN);
			exits("grrr");
		}
	} else
		readln("Password: ", pass, sizeof pass, 1);

	c = chancreate(sizeof(char*), 0);
	initdraw(0, 0, "bnetkey");
	initcontrols();
	cs = newcontrolset(screen, nil, nil, nil);

	dpy = createlabel(cs, "dpy");
	chanprint(cs->ctl, "dpy bordercolor black\ndpy align centerright");
	chanprint(cs->ctl, "dpy border 0\ndpy textcolor black");
	strcpy(dpytext, "0");
	chanprint(cs->ctl, "dpy value %s\ndpy size 10 10 100 50", dpytext);
	controlwire(dpy, "event", c);
	activate(dpy);

	for (i = 0; i < Ncol; i++){
		seprint(buf, buf+Bsz, "c%d", i);
		col[i] = createcolumn(cs, buf);
		seprint(buf, buf+Bsz, "c%d separation 3\n",i);
		chanprint(cs->ctl, buf);
	}
	for (i = 0; i < nelem(pad); i++){
		s = pad[i].name;
		pad[i].ctl = createtextbutton(cs, pad[i].name);
		seprint(buf, buf+Bsz, "%s align center\n%s text %s",
			s, s, s);
		chanprint(cs->ctl, buf);
		if (pad[i].backimg == nil)
			pad[i].backimg = Dflcol;
		seprint(buf, buf+Bsz, "%s image %s", s, pad[i].backimg);
		chanprint(cs->ctl, buf);
		seprint(buf, buf+Bsz, "%s size 10 10 100 100", s);
		chanprint(cs->ctl, buf);
		controlwire(pad[i].ctl, "event", c);
		seprint(buf, buf+Bsz, "c%d add %s", i % Ncol, s);
		activate(pad[i].ctl);
	}

	for (i = 0; i < Ncol; i++){
		controlwire(col[i], "event", c);
		activate(col[i]);
	}

	resizecontrolset(cs);
	passtokey(key, pass);
	for(;;){
		ev = recvp(c);
		i = tokenize(ev, args, nelem(args));
		if (i == 3 && strcmp(args[1], "value")==0){
			sleep(1);
			args[0][strlen(args[0])-1] = 0;
			chanprint(cs->ctl, "%s value 0", args[0]);
			for (i = 0; i < nelem(pad); i++)
				if (strcmp(pad[i].name, args[0]) == 0)
				if (pad[i].press)
					pad[i].press(i);
		}
	}
}

void
readln(char *prompt, char *line, int len, int raw)
{
	char *p;
	int fdin, fdout, ctl, n, nr;

	fdin = open("/dev/cons", OREAD);
	fdout = open("/dev/cons", OWRITE);
	fprint(fdout, "%s", prompt);
	if(raw){
		ctl = open("/dev/consctl", OWRITE);
		if(ctl < 0){
			fprint(2, "couldn't set raw mode");
			exits("readln");
		}
		write(ctl, "rawon", 5);
	} else
		ctl = -1;
	nr = 0;
	p = line;
	for(;;){
		n = read(fdin, p, 1);
		if(n < 0){
			close(ctl);
			close(fdin);
			close(fdout);
			fprint(2, "can't read cons");
			exits("readln");
		}
		if(*p == 0x7f)
			exits(0);
		if(n == 0 || *p == '\n' || *p == '\r'){
			*p = '\0';
			if(raw){
				write(ctl, "rawoff", 6);
				write(fdout, "\n", 1);
			}
			close(ctl);
			close(fdin);
			close(fdout);
			return;
		}
		if(*p == '\b'){
			if(nr > 0){
				nr--;
				p--;
			}
		}else{
			nr++;
			p++;
		}
		if(nr == len){
			fprint(fdout, "line too long; try again\n");
			nr = 0;
			p = line;
		}
	}
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-05-27 14:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-27 13:56 [9fans] bnetkey: graphic netkey for the bitsy Fco.J.Ballesteros
2002-05-27 14:55 presotto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).