|
@@ -36,13 +36,9 @@
|
|
|
#include <klist.h>
|
|
#include <klist.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
-char *vccd_dir;
|
|
|
|
|
-int nusers = 0;
|
|
|
|
|
-
|
|
|
|
|
-int vcc_send_req(struct klist_node *, struct vcc_request *);
|
|
|
|
|
-struct klist_node *find_connection(int fd);
|
|
|
|
|
-
|
|
|
|
|
-extern int clear_name_list(void);
|
|
|
|
|
|
|
+char *vccd_dir;
|
|
|
|
|
+int nusers = 0;
|
|
|
|
|
+struct klist_node logged_users = KLIST_NODE_INIT(&logged_users);
|
|
|
|
|
|
|
|
|
|
|
|
|
int users_init(void) {
|
|
int users_init(void) {
|
|
@@ -73,15 +69,14 @@ int users_init(void) {
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-int get_user(char *usr, struct user *buf) {
|
|
|
|
|
|
|
+int open_user_file(char *usr) {
|
|
|
char *fname;
|
|
char *fname;
|
|
|
int fdt;
|
|
int fdt;
|
|
|
|
|
|
|
|
if (unlikely(!(fname = malloc(PATH_MAX)))) {
|
|
if (unlikely(!(fname = malloc(PATH_MAX)))) {
|
|
|
vcc_log("*** malloc() failed\n");
|
|
vcc_log("*** malloc() failed\n");
|
|
|
|
|
|
|
|
- return 1;
|
|
|
|
|
|
|
+ return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
sprintf(fname, "%s/users/%s", vccd_dir, usr);
|
|
sprintf(fname, "%s/users/%s", vccd_dir, usr);
|
|
@@ -89,23 +84,37 @@ int get_user(char *usr, struct user *buf) {
|
|
|
if (unlikely((fdt = open(fname, O_RDONLY)) < 0)) {
|
|
if (unlikely((fdt = open(fname, O_RDONLY)) < 0)) {
|
|
|
perror("open()");
|
|
perror("open()");
|
|
|
|
|
|
|
|
- return 1;
|
|
|
|
|
|
|
+ return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- read(fdt, buf, sizeof(struct user));
|
|
|
|
|
|
|
+ free(fname);
|
|
|
|
|
+
|
|
|
|
|
+ return fdt;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+int get_user(char *usr, struct user *buf) {
|
|
|
|
|
+ int fdt;
|
|
|
|
|
+
|
|
|
|
|
+ if (unlikely(((fdt = open_user_file(usr)) == -1))) {
|
|
|
|
|
+ vcc_log("*** user not found. \n");
|
|
|
|
|
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ read(fdt, buf, sizeof(struct user));
|
|
|
close(fdt);
|
|
close(fdt);
|
|
|
- free(fname);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
int do_user_login(int fd, struct vcc_request *req) {
|
|
int do_user_login(int fd, struct vcc_request *req) {
|
|
|
- struct vcc_request *out;
|
|
|
|
|
- struct user usr;
|
|
|
|
|
- static const char *join_msg = "%s logged in. now there are %d users. \n";
|
|
|
|
|
- char *t;
|
|
|
|
|
|
|
+ struct vcc_request *out;
|
|
|
|
|
+ struct user *usr;
|
|
|
|
|
+ struct klist_node *n;
|
|
|
|
|
+ static const char *join_msg = "%s logged in. now there are %d users. \n";
|
|
|
|
|
+ char *t;
|
|
|
|
|
|
|
|
if (unlikely(!(out = malloc(sizeof(struct vcc_request))))) {
|
|
if (unlikely(!(out = malloc(sizeof(struct vcc_request))))) {
|
|
|
vcc_log("*** malloc() failed\n");
|
|
vcc_log("*** malloc() failed\n");
|
|
@@ -113,33 +122,53 @@ int do_user_login(int fd, struct vcc_request *req) {
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (unlikely(!(usr = malloc(sizeof(struct user))))) {
|
|
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
|
|
+
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
out->magic = htonl(VCC_MAGIC);
|
|
out->magic = htonl(VCC_MAGIC);
|
|
|
out->reqtype = htonl(REQ_CTL_LOGIN);
|
|
out->reqtype = htonl(REQ_CTL_LOGIN);
|
|
|
|
|
|
|
|
- if (get_user(req->usrname, &usr) || strcmp(req->msg, usr.passwd)) {
|
|
|
|
|
|
|
+ if (get_user(req->usrname, usr) || strcmp(req->msg, usr->passwd)) {
|
|
|
vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
|
|
vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
|
|
|
|
|
|
|
|
out->uid = 0;
|
|
out->uid = 0;
|
|
|
vcc_send_req(find_connection(fd), out);
|
|
vcc_send_req(find_connection(fd), out);
|
|
|
|
|
|
|
|
|
|
+ free(usr);
|
|
|
|
|
+
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
vcc_log("%s logged in. \n", req->usrname);
|
|
vcc_log("%s logged in. \n", req->usrname);
|
|
|
|
|
|
|
|
- out->uid = htonl(1);
|
|
|
|
|
-
|
|
|
|
|
if (unlikely(!(t = malloc(128)))) {
|
|
if (unlikely(!(t = malloc(128)))) {
|
|
|
vcc_log("*** malloc() failed\n");
|
|
vcc_log("*** malloc() failed\n");
|
|
|
|
|
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
|
|
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
|
|
+
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ klist_init(n);
|
|
|
|
|
+
|
|
|
|
|
+ n->value = fd;
|
|
|
|
|
+ n->data = usr;
|
|
|
|
|
+
|
|
|
|
|
+ klist_add(&logged_users, n);
|
|
|
|
|
+
|
|
|
|
|
+ out->uid = htonl(1);
|
|
|
vcc_send_req(find_connection(fd), out);
|
|
vcc_send_req(find_connection(fd), out);
|
|
|
|
|
|
|
|
- /* nusers incs when it connects */
|
|
|
|
|
- sprintf(t, join_msg, req->usrname, nusers);
|
|
|
|
|
|
|
+ /* nusers already inc'ed when it connects */
|
|
|
|
|
|
|
|
|
|
+ sprintf(t, join_msg, req->usrname, nusers);
|
|
|
vcc_message_broadcast(t, "system", 0);
|
|
vcc_message_broadcast(t, "system", 0);
|
|
|
|
|
|
|
|
free(t);
|
|
free(t);
|
|
@@ -148,4 +177,46 @@ int do_user_login(int fd, struct vcc_request *req) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+static struct klist_node *find_user(int fd) {
|
|
|
|
|
+ struct klist_node *n;
|
|
|
|
|
+
|
|
|
|
|
+ for (n = logged_users.next; n != &logged_users; n = n->next)
|
|
|
|
|
+ if (n->value == fd)
|
|
|
|
|
+ return n;
|
|
|
|
|
+
|
|
|
|
|
+ vcc_log("--- user not found: %d\n", fd);
|
|
|
|
|
+
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+int do_user_logout(int fd) {
|
|
|
|
|
+ struct klist_node *n;
|
|
|
|
|
+ struct user *usr;
|
|
|
|
|
+ int fdt;
|
|
|
|
|
+
|
|
|
|
|
+ if (unlikely(!(n = find_user(fd))))
|
|
|
|
|
+ return 1;
|
|
|
|
|
+
|
|
|
|
|
+ usr = n->data;
|
|
|
|
|
+
|
|
|
|
|
+ if (unlikely(((fdt = open_user_file(usr->username)) == -1))) {
|
|
|
|
|
+ vcc_log("*** user not found: %s. \n", usr->username);
|
|
|
|
|
+
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ write(fdt, usr, sizeof(struct user));
|
|
|
|
|
+ vcc_log("Bye, %s. \n", usr->username);
|
|
|
|
|
+
|
|
|
|
|
+ klist_del(&logged_users, n);
|
|
|
|
|
+
|
|
|
|
|
+ free(usr);
|
|
|
|
|
+ free(n);
|
|
|
|
|
+
|
|
|
|
|
+ close(fdt);
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
|