9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Axel Belinfante <Axel.Belinfante@cs.utwente.nl>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] u9fs: dealing with user names that differ between unix and plan 9?
Date: Mon,  8 Apr 2002 15:14:21 +0200	[thread overview]
Message-ID: <200204081314.g38DEMW18473@copernicus.cs.utwente.nl> (raw)
In-Reply-To: Your message of "Fri, 05 Apr 2002 20:23:06 +0200." <200204051823.g35IN6e06640@copernicus.cs.utwente.nl>

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

In addition to using the user name mapping file I now also
look for a '-l user' prefix in the attach 'aname' field
(if present, I just use it and skip the mapping file lookup).

Axel.

[-- Attachment #2: diff --]
[-- Type: text/plain , Size: 4068 bytes --]

--- u9fs.c.original	Fri Apr  5 19:54:35 2002
+++ u9fs.c	Mon Apr  8 14:05:30 2002
@@ -96,6 +96,8 @@
 int	readonly;
 int	myuid;
 char	*onlyuser;
+char	*mapfile;
+char	mapbuf[1024];

 void	io(void);
 void	error(char*);
@@ -112,6 +114,8 @@
 Ulong	vers(struct stat*);
 void	errjmp(const char*);
 int	omode(int);
+char*	stripusername(char*, char**);
+char*	remote2local(char*, char*);
 char*	id2name(Pass**, int);
 Pass*	id2pass(Pass**, int);
 Pass*	name2pass(Pass**, char*);
@@ -150,11 +154,12 @@
 char	Euid[] =	"can't set uid";
 char	Egid[] =	"can't set gid";
 char	Eowner[] =	"not owner";
+char	Emapfileerr[] =	"error opening mapping file";

 void
 usage(void)
 {
-	fprintf(stderr, "u9fs [-r] [-u user uid]\n");
+	fprintf(stderr, "u9fs [-r] [-u user uid] [-m mapfile]\n");
 	exit(1);
 }

@@ -172,6 +177,7 @@
 	DBG(fprintf(stderr, "u9fs\nkill %d\n", getpid()));

 	onlyuid = -1;
+	mapfile = 0;
 	ARGBEGIN{
 	case 'r':
 		readonly++;
@@ -185,6 +191,11 @@
 			usage();
 		onlyuid = atoi(s);
 		break;
+	case 'm':
+		mapfile = ARGF();
+		if(mapfile == 0)
+			usage();
+		break;
 	default:
 		usage();
 		break;
@@ -311,6 +322,9 @@
 {
 	Rfile *rf;
 	Pass *p;
+	char *localname;
+	char *aname;
+	char *username;

 	if(file0 == 0){
 		file0 = newfile();
@@ -321,7 +335,10 @@
 	}
 	if(!okfid(rhdr.fid))
 		errjmp(Ebadfid);
-	if(strncmp(rhdr.aname, "device", 6) == 0){
+	username = 0;
+	aname = stripusername(rhdr.aname, &username);
+	DBG(fprintf(stderr, "uname %s username:%s\n", aname, username?username:""));
+	if(strncmp(aname, "device", 6) == 0){
 		if(connected && !devallowed)
 			errjmp(Especial0);
 		devallowed = 1;
@@ -334,14 +351,20 @@
 	rf = &rfile[rhdr.fid];
 	if(rf->busy)
 		errjmp(Efidactive);
-	p = name2pass(uidname, rhdr.uname);
+	if (username == 0)
+		localname = remote2local(mapfile, rhdr.uname);
+	else
+		localname = username;
+	if(localname == 0)
+		errjmp(Eunknown);
+	p = name2pass(uidname, localname);
 	if(p == 0)
 		errjmp(Eunknown);
 	if(p->id == 0 || (uid_t)p->id == (uid_t)-1
 	|| myuid && p->id != myuid)
 		errjmp(Eperm);
 #	ifdef SOCKETS
-	if(!myuid && ruserok(bsdhost, 0, rhdr.uname, rhdr.uname) < 0)
+	if(!myuid && ruserok(bsdhost, 0, rhdr.uname, localname) < 0)
 		errjmp(Eperm);
 #	endif
 	/* mark busy & inc ref cnt only after committed to succeed */
@@ -955,6 +978,81 @@
 	q |= qdev[dev]<<24;
 	q |= st->st_ino & 0x00FFFFFFUL;
 	return q;
+}
+
+char*
+stripusername(char *aname, char **username)
+{
+	char *p = aname;
+	char *e;
+	DBG(fprintf(stderr, "aname: %s\n", aname));
+	while(*p && isspace(*p))
+		p++;
+	if (strncmp(p, "-l", 2) != 0)
+		return aname;
+	p++;
+	p++;
+	DBG(fprintf(stderr, "aname: found -l\n"));
+	if (! isspace(*p))
+		return aname;
+	DBG(fprintf(stderr, "aname: found space\n"));
+	while(*p && isspace(*p))
+		p++;
+	*username = p;
+	while(*p && !isspace(*p))
+		p++;
+	e = p;
+	while(*p && isspace(*p))
+		p++;
+	*e = '\0';
+	DBG(fprintf(stderr, "aname: found username: %s\n", *username));
+	DBG(fprintf(stderr, "aname: rest: %s\n", p));
+	return p;
+}
+
+char*
+remote2local(char *mapfile, char *name)
+{
+	FILE *f;
+	char *p;
+	char *r;
+	char *re;
+	char *l;
+	char *le;
+	char *res = name;
+	if (mapfile == 0)
+		return res;
+	if ((f = fopen(mapfile, "r")) == 0)
+		errjmp(Emapfileerr);
+	DBG(fprintf(stderr, "using mapfile %s\n", mapfile));
+	while(fgets(mapbuf, sizeof(mapbuf), f)) {
+		DBG(fprintf(stderr, "mapfile line: %s\n", mapbuf));
+		p = mapbuf;
+		while(*p && isspace(*p))
+			p++;
+		if (*p == '#')
+			continue;
+		r = p;
+		while(*p && !isspace(*p))
+			p++;
+		re = p;
+		while(*p && isspace(*p))
+			p++;
+		l = p;
+		while(*p && !isspace(*p))
+			p++;
+		le = p;
+		*re = '\0';
+		*le = '\0';
+		DBG(fprintf(stderr, "\tmap '%s' -> '%s'\n", r, l));
+		if (strcmp(r, name) == 0) {
+			res = l;
+			break;
+		}
+	}
+	DBG(fprintf(stderr, "mapped '%s' -> '%s'\n", name, res));
+	fclose(f);
+	return res;
 }

 Pass*

  reply	other threads:[~2002-04-08 13:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-05 15:44 forsyth
2002-04-05 18:23 ` Axel Belinfante
2002-04-08 13:14   ` Axel Belinfante [this message]
  -- strict thread matches above, loose matches on Subject: below --
2002-04-05 15:33 Russ Cox
2002-04-05 14:23 Axel Belinfante

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200204081314.g38DEMW18473@copernicus.cs.utwente.nl \
    --to=axel.belinfante@cs.utwente.nl \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).