summaryrefslogtreecommitdiff
path: root/accman.c
blob: 186693e658227ace2b9f2efdb989af59e82ffca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <accman.h>
#include <string.h>
#include <encrypt.h>
#include <unistd.h>
#include <encrypt.h>
#include <stdint.h>
#include <ctype.h>
#include <db.h>

char *get_name(void)
{
	char name[32];
	char *usr_n;

	scanf("%s", name);
	usr_n = (char *) malloc(sizeof(name) + DIM_MDOM + 1);
	if (!usr_n) {
		printf("No free memory available\n");
		exit(EXIT_FAILURE);
	}

	strcpy(usr_n, name);
	strcat(usr_n, MDOMAIN);

	return usr_n;
}

uint8_t confirm(struct user u)
{
	int i;
	char choice[32];
	ssize_t len;

	printf("Do you want to insert new user '%s'? [Y/n] \n", u.n);

	len = read(0, choice, 32);
	choice[len-1] = '\0';

	for (i = 0; i < strlen(choice); i++)
		choice[i] = toupper(choice[i]);

	return (!strcmp(choice, "Y") || !strcmp(choice, "YES") ||
			!strcmp(choice, "")) ? 1 : 0;
}

int main (void)
{
	struct user usr;

	printf("Insert user name (user@etezian.org): ");
	usr.n = get_name();
	usr.p = get_crypt_sha256(getpass("Password: "));

	if (confirm(usr)) {
		db_insert_user(usr);
		printf("User %s inserted correctly\n", usr.n);
	}
	else
		printf("user insertion aborted\n");

	return 0;
}