/* vccd/users.c * * This file is part of vccd. * * vccd is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * vccd is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with vccd. If not, see */ #include #include #include #include #include #include #include #include #include #include #include #include #include struct klist_node user_list = KLIST_NODE_INIT(&user_list); char *vccd_dir; int nusers = 0; 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); int users_init(void) { char *users, *tp; int fdt, size; if (unlikely((fdt = open("/etc/vccd-runtime", O_RDONLY)) < 0)) { perror("open()"); exit(1); } if (unlikely(!(vccd_dir = malloc(PATH_MAX)))) { vcc_log("*** malloc() failed\n"); exit(1); } if (unlikely(read(fdt, vccd_dir, PATH_MAX) < 0)) { perror("read()"); exit(1); } close(fdt); vcc_log("vccd data directory at %s\n", vccd_dir); if (unlikely(!(tp = malloc(PATH_MAX)))) { vcc_log("*** malloc() failed\n"); exit(1); } strcpy(tp, vccd_dir); strcat(tp, "/usrlist"); if (unlikely((fdt = open(tp, O_RDONLY)) < 0)) { perror("open()"); exit(1); } free(tp); size = lseek(fdt, 0, SEEK_END); if (unlikely((users = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fdt, 0)) == MAP_FAILED)) { perror("mmap()"); exit(1); } get_users(users, size); munmap(users, size); return 0; } int get_password(char *usr, char *passwd_buf) { struct readconf_strconf *conf; char *fname; int fdt; if (unlikely(!(fname = malloc(PATH_MAX)))) { vcc_log("*** malloc() failed\n"); return 1; } sprintf(fname, "%s/users/%s", vccd_dir, usr); if (unlikely((fdt = open(fname, O_RDONLY)) < 0)) { perror("open()"); return 1; } getbyte_fd = fdt; clear_name_list(); readconf_init(); 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); free(fname); return 0; } static int get_users(char *p, int size) { struct klist_node *n; char *end = p + size, *usrname, *s; int len; for (; p < end; p++) { len = 0; s = p; while (*s != '\n' && s < end) { ++s; ++len; } if (unlikely(s == end)) /* no '\n' at end of file */ return 0; if (unlikely(!(usrname = strndup(p, len)))) { vcc_log("*** malloc() failed\n"); continue; } if (unlikely(!(n = malloc(sizeof(struct klist_node))))) { vcc_log("*** malloc() failed\n"); continue; } klist_init(n); n->data = usrname; vcc_log("user %s\n", usrname); p = s; } return 0; } 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; }