فهرست منبع

vccd: support users

intirain 4 سال پیش
والد
کامیت
26a3b1fd2c
6فایلهای تغییر یافته به همراه83 افزوده شده و 25 حذف شده
  1. 2 2
      Makefile
  2. 4 0
      include/vcc/vcc.h
  3. 5 0
      include/vcc/vccd.h
  4. 1 1
      include/vcc/version.h
  5. 64 7
      users.c
  6. 7 15
      vcc.c

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 VCCD_MAJOR 		= 0
 VCCD_MAJOR 		= 0
-VCCD_MINOR 		= 3
-VCCD_PATCH 		= 4
+VCCD_MINOR 		= 4
+VCCD_PATCH 		= 0
 VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)
 VCCD_COMPILE_BY 	= $(shell whoami)
 
 

+ 4 - 0
include/vcc/vcc.h

@@ -37,6 +37,10 @@
 /* client -> server. get users logged-in */
 /* client -> server. get users logged-in */
 #define REQ_CTL_USRS 	3
 #define REQ_CTL_USRS 	3
 
 
+/* client -> server. login with password and username.
+ * server -> client. login ack  */
+#define REQ_CTL_LOGIN 	4
+
 #define REQ_SIZE 	4096
 #define REQ_SIZE 	4096
 #define USRNAME_SIZE 	32
 #define USRNAME_SIZE 	32
 #define MSG_SIZE 	(REQ_SIZE - 3 * sizeof(int) - USRNAME_SIZE)
 #define MSG_SIZE 	(REQ_SIZE - 3 * sizeof(int) - USRNAME_SIZE)

+ 5 - 0
include/vcc/vccd.h

@@ -26,6 +26,7 @@
 #include <arpa/inet.h>
 #include <arpa/inet.h>
 #include <vcc/vcc.h>
 #include <vcc/vcc.h>
 #include <sys/times.h>
 #include <sys/times.h>
+#include <klist.h>
 
 
 #define vcc_log(msg, ...) 	fprintf(stderr, "[%ld] " msg, times(NULL), ##__VA_ARGS__)
 #define vcc_log(msg, ...) 	fprintf(stderr, "[%ld] " msg, times(NULL), ##__VA_ARGS__)
 
 
@@ -35,6 +36,10 @@ int vcc_close_connection(int fd);
 
 
 int vcc_write_packages(int fd);
 int vcc_write_packages(int fd);
 int vcc_new_msg(int fd, struct vcc_request *buf);
 int vcc_new_msg(int fd, struct vcc_request *buf);
+int vcc_send_msg(struct klist_node *, void *);
+int vcc_message_broadcast(char *msg, char *usr, int except_fd);
+
+int do_user_login(int fd, struct vcc_request *buf);
 
 
 
 
 #endif
 #endif

+ 1 - 1
include/vcc/version.h

@@ -1 +1 @@
-#define VCCD_VERSION "vccd 0.3.4 Sat Mar 12 11:50:37 CST 2022 root"
+#define VCCD_VERSION "vccd 0.4.0 Sat Mar 12 15:40:28 CST 2022 root"

+ 64 - 7
users.c

@@ -39,9 +39,14 @@
 
 
 
 
 struct klist_node user_list = KLIST_NODE_INIT(&user_list);
 struct klist_node user_list = KLIST_NODE_INIT(&user_list);
-char *vccd_dir, *getbyte_mmap;
+char *vccd_dir;
+int nusers = 0;
 
 
 static int get_users(char *, int);
 static int get_users(char *, int);
+
+int vcc_send_req(struct klist_node *, struct vcc_request *);
+struct klist_node *find_connection(int fd);
+
 extern int clear_name_list(void);
 extern int clear_name_list(void);
 
 
 
 
@@ -104,9 +109,10 @@ int users_init(void) {
 }
 }
 
 
 
 
-static int get_password(char *usr) {
-	char 	*fname;
-	int 	fdt;
+int get_password(char *usr, char *passwd_buf) {
+	struct readconf_strconf 	*conf;
+	char 				*fname;
+	int 				fdt;
 
 
 	if (unlikely(!(fname = malloc(PATH_MAX)))) {
 	if (unlikely(!(fname = malloc(PATH_MAX)))) {
 		vcc_log("*** malloc() failed\n");
 		vcc_log("*** malloc() failed\n");
@@ -126,7 +132,15 @@ static int get_password(char *usr) {
 	clear_name_list();
 	clear_name_list();
 
 
 	readconf_init();
 	readconf_init();
-	show_names();
+
+	if (unlikely(!(conf = readconf_find_strname("passwd_raw")))) {
+		vcc_log("get password for user %s failed. ", usr);
+
+		return 1;
+	}
+
+	strcpy(passwd_buf, conf->conf_value);
+	vcc_log("password of %s: %s\n", usr, passwd_buf);
 
 
 	close(fdt);
 	close(fdt);
 	free(fname);
 	free(fname);
@@ -167,11 +181,9 @@ static int get_users(char *p, int size) {
 		}
 		}
 
 
 		klist_init(n);
 		klist_init(n);
-
 		n->data = usrname;
 		n->data = usrname;
 
 
 		vcc_log("user %s\n", usrname);
 		vcc_log("user %s\n", usrname);
-		get_password(usrname);
 
 
 		p = s;
 		p = s;
 	}
 	}
@@ -180,4 +192,49 @@ static int get_users(char *p, int size) {
 }
 }
 
 
 
 
+int do_user_login(int fd, struct vcc_request *req) {
+	struct vcc_request *out;
+	char right_passwd[MSG_SIZE];
+	const static char join_msg[] = {"%s logged in. now there are %d users. \n"};
+	char *t;
+
+	if (unlikely(!(out = malloc(sizeof(struct vcc_request))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	out->magic = htonl(VCC_MAGIC);
+	out->reqtype = htonl(REQ_CTL_LOGIN);
+
+	if (get_password(req->usrname, right_passwd) || strcmp(req->msg, right_passwd)) {
+		vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
+
+		out->uid = 0;
+		vcc_send_req(find_connection(fd), out);
+
+		return 0;
+	}
+
+	vcc_log("%s logged in. \n", req->usrname);
+
+	out->uid = htonl(1);
+
+	if (unlikely(!(t = malloc(sizeof(join_msg))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	vcc_send_req(find_connection(fd), out);
+	sprintf(t, join_msg, req->usrname, ++nusers);
+
+	vcc_message_broadcast(t, "system", 0);
+
+	free(t);
+
+	return 0;
+}
+
+
 
 

+ 7 - 15
vcc.c

@@ -40,13 +40,11 @@
 
 
 
 
 struct klist_node connections = KLIST_NODE_INIT(&connections);
 struct klist_node connections = KLIST_NODE_INIT(&connections);
-int vcc_message_broadcast(char *msg, char *usrname, int except_fd);
-int nusers = 0;
+extern int nusers;
+
 
 
 int vcc_new_connection(int fd, struct in_addr *addr) {
 int vcc_new_connection(int fd, struct in_addr *addr) {
 	struct klist_node *n;
 	struct klist_node *n;
-	char *new_msg;
-	const static char join_msg[] = {"New user from %s joined. Now there are %d users. \n"};
 
 
 	vcc_log("New connection from %s\n", inet_ntoa(*addr));
 	vcc_log("New connection from %s\n", inet_ntoa(*addr));
 
 
@@ -69,17 +67,6 @@ int vcc_new_connection(int fd, struct in_addr *addr) {
 	klist_init(n->sending);	
 	klist_init(n->sending);	
 	klist_add(&connections, n);
 	klist_add(&connections, n);
 
 
-	if (unlikely(!(new_msg = malloc(128)))) {
-		vcc_log("*** malloc() failed\n");
-
-		return 1;
-	}
-
-	sprintf(new_msg, join_msg, inet_ntoa(*addr), ++nusers);
-	vcc_message_broadcast(new_msg, "system", 0);
-
-	free(new_msg);
-
 	return 0;
 	return 0;
 }
 }
 
 
@@ -284,6 +271,11 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 
 
 		break;
 		break;
 
 
+	case REQ_CTL_LOGIN:
+		do_user_login(fd, req);
+
+		break;
+
 	default:
 	default:
 		vcc_log("--- bad request type from %d\n", fd);
 		vcc_log("--- bad request type from %d\n", fd);