summaryrefslogtreecommitdiff
path: root/accman.c
blob: d12ce2ae8f8f16c65358a994c1adb0090250df42 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#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;
}

char *get_password(void)
{
	char *p, *tmp;
	size_t len;

	tmp = getpass("Password: ");
	len = strlen(tmp);

	if ((len < MIN_PASSWD_LEN) || (len > MAX_PASSWD_LEN)) {
		fprintf(stderr,
			"Password must be between %d and %d characters\n",
			MIN_PASSWD_LEN, MAX_PASSWD_LEN);
		exit(EXIT_FAILURE);
	}

	p = (char*) malloc (len + 1);
	strcpy(p, tmp);

	tmp = getpass("Re-type password: ");
	if (strcmp(p, tmp)) {
		fprintf(stderr, "The passwords don't match, operation aborted\n");
		exit(EXIT_FAILURE);
	}

	return get_crypt_sha256(p);
}

int main (void)
{
	struct user usr;

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

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

	return 0;
}