| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- /* 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 <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <string.h>
- #include <limits.h>
- #include <sys/mman.h>
- #include <vcc/vcc.h>
- #include <vcc/vccd.h>
- #include <vcc/version.h>
- #include <klist.h>
- char *vccd_dir;
- int nusers = 0;
- struct klist_node logged_users = KLIST_NODE_INIT(&logged_users);
- int users_init(void) {
- int fdt;
- 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);
- return 0;
- }
- int open_user_file(char *usr) {
- 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;
- }
- 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);
- return 0;
- }
- int do_user_login(int fd, struct vcc_request *req) {
- 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))))) {
- vcc_log("*** malloc() failed\n");
- return 1;
- }
- if (unlikely(!(usr = malloc(sizeof(struct user))))) {
- vcc_log("*** malloc() failed\n");
- return 1;
- }
- out->magic = htonl(VCC_MAGIC);
- out->reqtype = htonl(REQ_CTL_LOGIN);
- if (get_user(req->usrname, usr) || strcmp(req->msg, usr->passwd)) {
- vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
- out->uid = 0;
- vcc_send_req(find_connection(fd), out);
- free(usr);
- return 0;
- }
- vcc_log("%s logged in. \n", req->usrname);
- if (unlikely(!(t = malloc(128)))) {
- vcc_log("*** malloc() failed\n");
- 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);
- /* nusers already inc'ed when it connects */
- sprintf(t, join_msg, req->usrname, nusers);
- vcc_message_broadcast(t, "system", 0);
- free(t);
- return 0;
- }
- 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;
- }
|