summaryrefslogtreecommitdiff
path: root/accman.c
blob: 04a5b4410121430513d24e2421dbe95baaffe2d8 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#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;

	int got = scanf("%s", name);
	if(got != 1) {
		fprintf(stderr, "failed to read name...\n");
		exit(EXIT_FAILURE);
	}
	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);
		memset(tmp, 0, len);
		exit(EXIT_FAILURE);
	}

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

	/* clear password from memory */
	memset(tmp, 0, len);

	tmp = getpass("Re-type password: ");

	int scmp = strcmp(p, tmp);
	memset(tmp, 0, len);

	if (scmp) {
		fprintf(stderr,
			"The passwords don't match, operation aborted\n");
		/* clear passwd */
		memset(p, 0, len);
		exit(EXIT_FAILURE);
	}

	tmp = get_crypt_sha256(p);
	memset(p, 0, len);
	free(p);

	return tmp;
}

int main (void)
{
	struct user usr;

	printf("Insert user name (user@" DOMAIN "): ");
	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;
}