add group file, and user/group data functions getpw*/getgr*

This commit is contained in:
Lisa Milne 2023-12-06 20:53:05 +10:00
parent 1536bb264d
commit bb8d7d0da1
3 changed files with 147 additions and 26 deletions

View file

@ -54,13 +54,21 @@ Options:
function writeNodeData(st,fd) {
if (long) {
var pw = stdlib.getpwuid(st.uid);
var gr = stdlib.getgrgid(st.gid);
var uname = st.uid.toString();
var gname = st.gid.toString();
if (pw)
uname = pw.pw_name;
if (gr)
gname = gr.gr_name;
if (st.type == stdio.types.FT_LINK) {
var d = stdio.readAll(fd);
if (!d)
d = '?';
stdio.printf(st.perms+'\t'+st.uid+'\t'+st.gid+'\t'+st.name+' -> '+d);
stdio.printf('%s%16s%16s%16s -> %s\n',st.perms,uname,gname,st.name,d);
}else{
stdio.printf(st.perms+'\t'+st.uid+'\t'+st.gid+'\t'+st.name);
stdio.printf('%s%16s%16s%16s\n',st.perms,uname,gname,st.name);
}
}else{
stdio.printf(st.name);
@ -1192,14 +1200,13 @@ clite.commands.load('test',function(args,env,io) {
stdio.printf('uid:%d',stdlib.getuid());
stdio.printf('%#15x:%15o:%15.5f:%0$15.5f:%%:',10,12,8.123456789);
function rcb(data) {
stdio.printf(data);
stdio.write(io.stdout,data);
var u = stdlib.getpwnam(data);
if (u)
stdio.printf('%s %d %d\n',u.pw_name,u.pw_uid,u.pw_gid);
io.exit(0);
}
var fd = clite.io.openp(io.pid,'/var/logs',stdio.flags.O_RDONLY,function(fd) {
alert(fd.node.data.content);
});
stdio.read(io.stdin,rcb);
return null;

View file

@ -40,9 +40,9 @@ var clite = {
},
file:function(name,callback) {
if (window.location.protocol == 'file:') { // this is a dirty hack and I hate it, just let me open a file: path!
//clite.term.setCustom({type:'file',callback:callback});
//clite.shell.writeLine('open file: '+name);
//return;
clite.term.setCustom({type:'file',callback:callback});
clite.term.writeLine('open file: '+name);
return;
// fuck it, I'll just update about:config in firefox
}
@ -264,6 +264,19 @@ PATH=/bin
n.data.content = `
root:x:0:0:root:/root:/bin/sh
guest:x:1:1:guest:/usr/home/guest:/bin/sh
`;
// TODO: check cookies for a local user
n.perms = '-rw-r--r--';
// /etc/passwd contains user account details
vfsapi.mkFile(0,'/etc/group');
var n = vfsapi.getNode(0,'/etc/group');
if (!n) {
clite.log.write('group config failure');
return false;
}
n.data.content = `
root:x:0:
guest:x:1:
`;
// TODO: check cookies for a local user
n.perms = '-rw-r--r--';
@ -723,21 +736,35 @@ clite.proc = {
clite.user = {
init:function(vfsapi) { // returns true if there's a user login or false for guest
var users = [{
name:'root',
uid:0,
gid:0,
groups:[],
env:{
HOME:'/',
USER:this.name,
}
}];
var data = {
users:[{
name:'root',
uid:0,
gid:0,
groups:[],
env:{
HOME:'/',
USER:this.name,
}
}],
groups:[{
name:'root',
gid:0,
users:[]
}]
};
var env = {};
function getUser(uid) {
for (var i=0; i<users.length; i++) {
if (users[i].uid == uid)
return users[i];
for (var i=0; i<data.users.length; i++) {
if (data.users[i].uid == uid)
return data.users[i];
}
return null;
}
function getGroup(gid) {
for (var i=0; i<data.groups.length; i++) {
if (data.groups[i].gid == gid)
return data.groups[i];
}
return null;
}
@ -764,7 +791,7 @@ clite.user = {
var udata = getUser(uid);
if (!udata) {
udata = {};
users.push(udata);
data.users.push(udata);
}
udata.name = sects[0];
@ -777,7 +804,32 @@ clite.user = {
udata.env.USER = udata.name;
udata.env.PWD = udata.env.HOME;
});
// TODO: read in data from /etc/groups
// load in data from /etc/group
n = vfsapi.getFile(0,'/etc/group');
if (!n)
return false;
var lines = n.split('\n');
lines.forEach(function(line) {
if (line[0] == '#')
return;
var sects = line.split(':');
// 0 groupname
// 1 null
// 2 gid
// 3 csv users
if (sects.length != 4)
return;
var gid = parseInt(sects[2]);
var gdata = getGroup(gid);
if (!gdata) {
gdata = {};
data.groups.push(gdata);
}
gdata.name = sects[0];
gdata.gid = parseInt(sects[2]);
gdata.users = sects[3].split(',');
});
// load the environment
n = vfsapi.getFile(0,'/etc/env');
@ -793,7 +845,7 @@ clite.user = {
var name = sects[0].trim();
var value = sects[1].trim();
env[name] = value;
users.forEach(function(u) {
data.users.forEach(function(u) {
u.env[name] = value;
});
});
@ -853,6 +905,44 @@ cat -l /usr/share/introduction
return structuredClone(env);
return structuredClone(udata.env);
}
clite.user.getPWData = function(uid) {
var udata = getUser(uid);
if (!udata)
return null;
return Object.create({
pw_name:udata.name, // User's login name.
pw_uid:udata.uid, // Numerical user ID.
pw_gid:udata.gid, // Numerical group ID.
pw_dir:udata.env.HOME, // Initial working directory.
pw_shell:udata.env.SHELL // Program to use as shell.
});
}
clite.user.getGRData = function(gid) {
var gdata = getGroup(gid);
if (!gdata)
return null;
return Object.create({
gr_name:gdata.name, // The name of the group.
gr_gid:gdata.gid, // Numerical group ID.
gr_mem:gdata.users // array of member names.
});
}
clite.user.mapUserName = function(name) {
for (var i=0; i<data.users.length; i++) {
if (data.users[i].name == name)
return data.users[i].uid;
}
return -1;
}
clite.user.mapGroupName = function(name) {
for (var i=0; i<data.groups.length; i++) {
if (data.groups[i].name == name)
return data.groups[i].gid;
}
return -1;
}
loadUsers();

View file

@ -48,11 +48,35 @@ return Object.create({
return clite.proc.getUID(io.pid);
},
// gets the passwd file data for a user based on their uid
getpwuid:function(uid) {
return clite.user.getPWData(uid);
},
// gets the passwd file data for a user based on their name
getpwnam:function(name) {
var uid = clite.user.mapUserName(name);
if (uid < 0)
return null;
return clite.user.getPWData(uid);
},
// returns the current group id
getgid:function() {
return clite.proc.getGID(io.pid);
},
// gets teh group file data for a group based on their gid
getgrgid:function(gid) {
return clite.user.getGRData(gid);
},
// gets teh group file data for a group based on their name
getgrnam:function(name) {
var gid = clite.user.mapGroupName(name);
if (gid < 0)
return null;
return clite.user.getGRData(gid);
},
// wait for any child process to exit
wait:function(cb) {
return clite.proc.addWaitGroupAny(io.pid,cb);