#include #include #include #include #include #include #include #include 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; }