/* 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
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);
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 get_user(char *usr, struct user *buf) {
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;
}
read(fdt, buf, sizeof(struct user));
close(fdt);
free(fname);
return 0;
}
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;
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_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);
return 0;
}
vcc_log("%s logged in. \n", req->usrname);
out->uid = htonl(1);
if (unlikely(!(t = malloc(128)))) {
vcc_log("*** malloc() failed\n");
return 1;
}
vcc_send_req(find_connection(fd), out);
/* nusers incs when it connects */
sprintf(t, join_msg, req->usrname, nusers);
vcc_message_broadcast(t, "system", 0);
free(t);
return 0;
}