2021-07-13 07:34:34 +00:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
2021-07-19 04:58:08 +00:00
|
|
|
#include <stdio.h>
|
2021-07-13 07:34:34 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "cart.h"
|
|
|
|
#include "realm.h"
|
|
|
|
#include "user.h"
|
|
|
|
|
|
|
|
extern UserInfo users_table[64];
|
|
|
|
|
|
|
|
UserInfo* find_user(UserInfo* table, char* uname) {
|
|
|
|
UserInfo* u = table;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
if (scmp(uname, u->name))
|
|
|
|
return u;
|
|
|
|
u++;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2021-07-21 06:27:40 +00:00
|
|
|
int login(UserInfo* table, char* uname, char* password) {
|
2021-07-13 07:34:34 +00:00
|
|
|
UserInfo* u = find_user(table, uname);
|
|
|
|
|
|
|
|
if (u == nil)
|
|
|
|
return 0;
|
|
|
|
|
2021-07-21 06:27:40 +00:00
|
|
|
u->password = hash(password, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int logout(UserInfo* table, char* uname) {
|
|
|
|
UserInfo* u = table;
|
|
|
|
|
|
|
|
if (u == nil)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*(u->name) = 0;
|
|
|
|
u->password = 0;
|
|
|
|
|
|
|
|
if (u->scope != nil) {
|
|
|
|
free(u->scope);
|
|
|
|
u->scope = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
u->random = 0;
|
|
|
|
|
|
|
|
if (u->realm != nil)
|
|
|
|
leave_realm(table, uname);
|
|
|
|
|
|
|
|
if (u->cart != nil)
|
|
|
|
unload_cart(table, uname);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int protect_realm(UserInfo* table, char* uname, char* password) {
|
|
|
|
UserInfo* u = find_user(table, uname);
|
|
|
|
|
|
|
|
if (u != nil && u->realm != nil && scmp(uname, u->realm->master)) {
|
|
|
|
u->realm->password = hash(password, 0);
|
2021-07-13 07:34:34 +00:00
|
|
|
return 1;
|
2021-07-21 06:27:40 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int transfer_realm(UserInfo* table, char* from, char* to) {
|
|
|
|
UserInfo* old_user = find_user(table, from);
|
|
|
|
UserInfo* new_user = find_user(table, to);
|
|
|
|
|
|
|
|
if (old_user == nil || new_user == nil || old_user->realm == nil)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
scpy(to, old_user->realm->master, 32);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int load_cart(UserInfo* table, char* uname, char* cart_name) {
|
|
|
|
UserInfo* u = find_user(table, uname);
|
|
|
|
|
|
|
|
if (u == nil)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
u->cart = create_cart(cart_name);
|
|
|
|
if (u->cart == nil) {
|
|
|
|
fprintf(stderr, "failed creating cart\n");
|
|
|
|
return 0;
|
2021-07-13 07:34:34 +00:00
|
|
|
} else {
|
2021-07-21 06:27:40 +00:00
|
|
|
return 1;
|
2021-07-13 07:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int enter_realm(UserInfo* table, char* uname, char* realm_name) {
|
|
|
|
Realm* r = find_realm(table, realm_name);
|
|
|
|
UserInfo* u = find_user(table, uname);
|
2021-07-19 04:58:08 +00:00
|
|
|
int i, j = 0;
|
2021-07-13 07:34:34 +00:00
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
if (u == nil) {
|
|
|
|
fprintf(stderr, "no user\n");
|
2021-07-13 07:34:34 +00:00
|
|
|
return 0;
|
2021-07-19 04:58:08 +00:00
|
|
|
}
|
|
|
|
if (u->cart == nil) {
|
|
|
|
fprintf(stderr, "no cart\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-07-13 07:34:34 +00:00
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
fprintf(stderr, "iterating through users table: slot %d\n", i);
|
2021-07-13 07:34:34 +00:00
|
|
|
if (table[i].realm != nil && scmp(table[i].realm->name, realm_name))
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r != nil) {
|
2021-07-19 04:58:08 +00:00
|
|
|
if (j < r->max) {
|
|
|
|
u->realm = r;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-07-13 07:34:34 +00:00
|
|
|
} else {
|
2021-07-19 04:58:08 +00:00
|
|
|
r = parse_realm(u->cart->name, realm_name);
|
|
|
|
if (r == nil)
|
2021-07-13 07:34:34 +00:00
|
|
|
return 0;
|
2021-07-19 04:58:08 +00:00
|
|
|
if (j <= r->max)
|
|
|
|
return 0;
|
|
|
|
u->realm = r;
|
|
|
|
return 1;
|
2021-07-13 07:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int leave_realm(UserInfo* table, char* uname) {
|
|
|
|
UserInfo* u = find_user(table, uname);
|
|
|
|
Realm* r;
|
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
if (u == nil) {
|
|
|
|
fprintf(stderr, "couldn't find user: %s\n", uname);
|
2021-07-13 07:34:34 +00:00
|
|
|
return 0;
|
2021-07-19 04:58:08 +00:00
|
|
|
}
|
2021-07-13 07:34:34 +00:00
|
|
|
|
|
|
|
r = u->realm;
|
2021-07-19 04:58:08 +00:00
|
|
|
if (r == nil) {
|
|
|
|
fprintf(stderr, "%s is not in a realm!\n", uname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u->cart == nil)
|
2021-07-13 07:34:34 +00:00
|
|
|
return 0;
|
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
save_realm(u->cart->name, r);
|
2021-07-13 07:34:34 +00:00
|
|
|
u->realm = nil;
|
|
|
|
if (find_realm(table, r->name) == nil)
|
|
|
|
destroy_realm(r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unload_cart(UserInfo* table, char* uname) {
|
|
|
|
UserInfo* u = find_user(table, uname);
|
|
|
|
|
2021-07-21 06:27:40 +00:00
|
|
|
if (u == nil || u->cart == nil)
|
2021-07-13 07:34:34 +00:00
|
|
|
return 0;
|
|
|
|
|
2021-07-21 06:27:40 +00:00
|
|
|
destroy_cart(u->cart);
|
2021-07-13 07:34:34 +00:00
|
|
|
u->cart = nil;
|
|
|
|
return 1;
|
|
|
|
}
|