/* 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, *getbyte_mmap;
static int get_users(char *, int);
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;
}
static int get_password(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;
}
getbyte_fd = fdt;
clear_name_list();
readconf_init();
show_names();
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);
get_password(usrname);
p = s;
}
return 0;
}