self is always first in users list; implement logout

This commit is contained in:
Iris Lightshard 2021-07-02 15:59:40 -06:00
parent 6ea6f9be6b
commit edc0e119bc
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
2 changed files with 110 additions and 106 deletions

16
hash.c
View file

@ -1,16 +0,0 @@
#include <stdio.h>
unsigned long long hash(char* str) {
unsigned long long h;
unsigned char* p;
h = 0;
for (p = (unsigned char*)str; *p != '\0'; p++)
h = 37 * h + *p;
return h; // or, h % ARRAY_SIZE;
}
int main(int argc, char** argv) {
printf("%llu\n", hash(argv[1]));
return 0;
}

200
xrxs.c
View file

@ -9,9 +9,6 @@
#include <mp.h> #include <mp.h>
#include <libsec.h> #include <libsec.h>
#define CARTSLOC "./carts/"
#define CTL_HASHV 139931
#define SHUTDOWN_HASHV 11192337284248
/* clang-format off */ /* clang-format off */
char clca(char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; } /* char to lowercase */ char clca(char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; } /* char to lowercase */
@ -36,6 +33,19 @@ static char Euname[] = "username is already taken";
static Tree* tree; static Tree* tree;
typedef enum {
LOGIN = 208176873,
LOAD = 5626172,
CREATE = 7083959236,
PROTECT = 295480618573,
ENTER = 195024746,
LEAVE = 207662601,
LOGOUT = 7702552890,
SAVE = 5962355,
RESET = 218931595,
UNLOAD = 8325026851
} Command;
typedef enum { CTL = 1, USERS, CARTS, SLOT, DATA, REALMS, UNIVERSE } FileType; typedef enum { CTL = 1, USERS, CARTS, SLOT, DATA, REALMS, UNIVERSE } FileType;
typedef struct Aux Aux; typedef struct Aux Aux;
@ -57,11 +67,22 @@ Aux* create_aux(FileType t) {
typedef struct UserInfo UserInfo; typedef struct UserInfo UserInfo;
struct UserInfo { struct UserInfo {
char name[32]; char name[32];
uvlong password;
ushort id; ushort id;
}; };
static UserInfo users_table[64]; static UserInfo users_table[64];
uvlong hash(char* str) {
uvlong h;
uchar* p;
h = 0;
for (p = (unsigned char*)str; *p != '\0'; p++)
h = 37 * h + *p;
return h; // or, h % ARRAY_SIZE;
}
void xrxs_attach(Req* r) { void xrxs_attach(Req* r) {
/* As it is, once the user detaches, they will stay in the table /* As it is, once the user detaches, they will stay in the table
* until the server restarts. We have to figure out some way * until the server restarts. We have to figure out some way
@ -80,12 +101,12 @@ void xrxs_attach(Req* r) {
} }
for (i = 0; i < 64; i++) { for (i = 0; i < 64; i++) {
usr = users_table[i].name; usr = users_table[i].name;
if (scmp(usr, username)){ if (scmp(usr, username)) {
respond(r, Euname); respond(r, Euname);
return; return;
} }
if (*usr == 0) { if (*usr == 0) {
scpy(username, usr, l+1); scpy(username, usr, l + 1);
users_table[i].id = id++; users_table[i].id = id++;
break; break;
} }
@ -93,81 +114,71 @@ void xrxs_attach(Req* r) {
respond(r, nil); respond(r, nil);
} }
void fsread(Req* r) { void login(char* uname, char* password) {
Aux* a; int i;
vlong offset; for (i = 0; i < 64; i++) {
long count; if (scmp(uname, users_table[i].name)) {
users_table[i].password = hash(password);
a = r->fid->file->aux;
offset = r->ifcall.offset;
count = r->ifcall.count;
/*print("read %ld %lld\n", *count, offset); */
if (offset >= a->count) {
r->ofcall.count = 0;
respond(r, nil);
return;
}
if (offset + count >= a->count)
count = a->count - offset;
memmove(r->ofcall.data, a->data + offset, count);
r->ofcall.count = count;
respond(r, nil);
}
unsigned long long hash(char* str) {
unsigned long long h;
unsigned char* p;
h = 0;
for (p = (unsigned char*)str; *p != '\0'; p++)
h = 37 * h + *p;
return h; // or, h % ARRAY_SIZE;
}
void fswrite(Req* r) {
void* v;
Aux* a;
vlong offset;
long count;
a = r->fid->file->aux;
offset = r->ifcall.offset;
count = r->ifcall.count;
if (offset + count >= a->count) {
v = realloc(a->data, offset + count);
if (v == nil) {
respond(r, Enomem);
return;
} }
a->data = v;
a->count = offset + count;
r->fid->file->dir.length = a->count;
} }
memmove(a->data + offset, r->ifcall.data, count); }
r->ofcall.count = count;
respond(r, nil); int logout(char* uname) {
int i;
fprintf(stderr, uname);
for (i = 0; i < 64; i++) {
if (scmp(uname, users_table[i].name)) {
*(users_table[i].name) = 0;
users_table[i].id = 0;
users_table[i].password = 0;
return 1;
}
}
return 0;
} }
void write_ctl(Req* r) { void write_ctl(Req* r) {
char cmd[16]; char cmd[16] = {0};
char* c = r->ifcall.data; char* c = r->ifcall.data;
int i; int i;
/*for (i = 0; i < r->ifcall.count; i++) { for (i = 0; i < r->ifcall.count && *c != ' ' && *c != '\n'; i++) {
ccat(cmd, *c++); ccat(cmd, *c++);
} }
fprintf(stderr, cmd);
unsigned long long const cmd_hashv = hash(cmd); uvlong const cmd_hashv = hash(cmd);
switch (cmd_hashv) { switch (cmd_hashv) {
default:*/ case LOGIN:
createfile(tree->root, "success", nil, 0777, nil); login(r->fid->uid, c);
/*break; break;
}*/ case LOAD:
// load(c);
break;
case CREATE:
// create(c);
break;
case PROTECT:
// protect(c);
break;
case ENTER:
// enter(c);
break;
case LEAVE:
// leave(c);
break;
case LOGOUT:
logout(r->fid->uid);
break;
case SAVE:
// save();
break;
case RESET:
// reset();
break;
case UNLOAD:
// unload();
break;
}
r->ofcall.count = r->ifcall.count; r->ofcall.count = r->ifcall.count;
r->fid->file->dir.length = r->ifcall.count; r->fid->file->dir.length = r->ifcall.count;
respond(r, nil); respond(r, nil);
@ -189,19 +200,25 @@ void xrxs_write(Req* r) {
} }
void read_users(Req* r) { void read_users(Req* r) {
char buf[2112] = {0}; char buf[2113] = {0};
int i; int i;
for (i = 0; i < 64; i++) { for (i = 0; i < 64; i++) {
if (scmp(users_table[i].name, "\0")) { if (scmp(users_table[i].name, r->fid->uid)) {
scat(buf, users_table[i].name);
ccat(buf, '\n');
break; break;
} }
scat(buf, users_table[i].name);
if (i == 63) {
ccat(buf, 0);
} else {
ccat(buf, '\n');
}
} }
for (i = 0; i < 64; i++) {
if (
scmp(users_table[i].name, "\0") ||
scmp(users_table[i].name, r->fid->uid)) {
continue;
}
scat(buf, users_table[i].name);
ccat(buf, '\n');
}
ccat(buf, 0);
readstr(r, buf); readstr(r, buf);
respond(r, nil); respond(r, nil);
} }
@ -233,10 +250,6 @@ void xrxs_read(Req* r) {
} }
} }
void fsopen(Req* r) { respond(r, nil); }
void wstat(Req* r) { respond(r, nil); }
void fs_destroy_file(File* f) { void fs_destroy_file(File* f) {
Aux* a = f->aux; Aux* a = f->aux;
if (a && a->data) { if (a && a->data) {
@ -272,12 +285,7 @@ String** listdir(char* path) {
return self; return self;
} }
Srv fs = { Srv fs = {.attach = xrxs_attach, .read = xrxs_read, .write = xrxs_write};
.attach = xrxs_attach,
.open = fsopen,
.read = xrxs_read,
.write = xrxs_write,
.wstat = wstat};
int threadmaybackground(void) { return 1; } int threadmaybackground(void) { return 1; }
@ -287,6 +295,12 @@ void threadmain(int argc, char* argv[]) {
int i; int i;
String** cart; String** cart;
/* if -h CMD is given, print the hash value of CMD */
if (argc == 3 && scmp(argv[1], "-h")) {
printf("%llu\n", hash(argv[2]));
return;
}
/* if -m PATH is supplied, mount on PATH */ /* if -m PATH is supplied, mount on PATH */
/* if -s NAME is supplied, create a socket for the namespace */ /* if -s NAME is supplied, create a socket for the namespace */
/* otherwise, just use srv() (for wrapping with socat or inetd) */ /* otherwise, just use srv() (for wrapping with socat or inetd) */
@ -306,10 +320,16 @@ void threadmain(int argc, char* argv[]) {
fs.foreground = 1; fs.foreground = 1;
fs.tree = alloctree(nil, nil, DMDIR | 0777, fs_destroy_file); fs.tree = alloctree(nil, nil, DMDIR | 0777, fs_destroy_file);
tree = fs.tree; tree = fs.tree;
closefile(createfile(tree->root, "carts", nil, 0444, create_aux(CARTS)));
closefile( closefile(
createfile(tree->root, "ctl", nil, DMAPPEND | 0300, create_aux(CTL))); createfile(tree->root, "ctl", nil, DMAPPEND | 0300, create_aux(CTL)));
closefile(createfile(tree->root, "users", nil, 0444, create_aux(USERS))); closefile(createfile(tree->root, "carts", nil, 0400, create_aux(CARTS)));
closefile(createfile(tree->root, "users", nil, 0400, create_aux(USERS)));
closefile(createfile(tree->root, "slot", nil, 0400, create_aux(SLOT)));
closefile(createfile(tree->root, "data", nil, DMDIR | 0500, nil));
closefile(createfile(tree->root, "realms", nil, 0400, create_aux(REALMS)));
closefile(
createfile(tree->root, "universe", nil, 0600, create_aux(UNIVERSE)));
/*String** carts = listdir("carts/"); /*String** carts = listdir("carts/");
cart = carts; cart = carts;