/* Create TABLE with hostname categories on BDD */ int crv_mysql_create_host_table_cat(MYSQL *sql, const char *host) { string_t *str; string_t *esc; str = string_new(); esc = string_new(); string_ajout(str, "CREATE TABLE IF NOT EXISTS `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "_cat`(`sha1` varchar(40) NOT NULL DEFAULT '',\ `cat` varchar(128) NOT NULL,\ PRIMARY KEY (`sha1`)\ )"); if (0 != mysql_query(sql, (const char *)str->str)) { fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); return 0; }
/* Fill TABLE with hostname_grp on BDD */ int crv_mysql_insert_host_table_grp(MYSQL *sql, const char *host, const char *sha1) { string_t *str; string_t *esc; str = string_new(); esc = string_new(); /* * INSERT INTO `crv_127.0.0.1_grp` (`sha1`, `grp`) VALUES * ('uygfe', 'public'); */ string_ajout(str, "INSERT INTO `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "_group` (`sha1`, `group`) VALUES ('"); string_my_escape(sql, esc, sha1); string_ajout(str, esc->str); string_ajout(str, "', 'public')"); if (0 != mysql_query(sql, (const char *)str->str)) { fprintf(stderr, "Echec: erreur num %d\n", mysql_errno(sql)); fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); return 0; }
/* Create TABLE with hostname on BDD */ int crv_mysql_create_host_table(MYSQL *sql, const char *host) { string_t *str; string_t *esc; str = string_new(); esc = string_new(); string_ajout(str, "CREATE TABLE IF NOT EXISTS `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "`(`id` int(11) NOT NULL AUTO_INCREMENT,\ `sha1` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,\ `size` int(11) NOT NULL,\ `name` TEXT,\ `date_file` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\ PRIMARY KEY (`id`),\ UNIQUE KEY `sha1` (`sha1`)\ )"); if (0 != mysql_query(sql, (const char *)str->str)) { fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); return 0; }
/* Fill TABLE with hostname on BDD */ int crv_mysql_insert_host_table_file(MYSQL *sql, const char *host, const char *sha1, const char *size, const char *filename) { string_t *str; string_t *esc; str = string_new(); esc = string_new(); /* * INSERT INTO `crv`.`crv_127.0.0.1` ( * `id` , * `sha1` , * `size` * ) * VALUES ( * NULL , 'fffrtgtfrdeszqazfrfffèvvvervtyurdrrfeyrd', '187625' * ); */ string_ajout(str, "INSERT INTO `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "` (`id`, `sha1`, `size`, `name`) VALUES ( NULL, '"); string_my_escape(sql, esc, sha1); string_ajout(str, esc->str); string_ajout(str, "', '"); string_my_escape(sql, esc, size); string_ajout(str, esc->str); string_ajout(str, "', '"); string_my_escape(sql, esc, filename); string_ajout(str, esc->str); string_ajout(str, "')"); if (0 != mysql_query(sql, (const char *)str->str)) { if (mysql_errno(sql) == 1062) { return 2; } fprintf(stderr, "Echec: erreur num %d\n", mysql_errno(sql)); fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); return 0; }
/* INSERT TABLE with hostname_info on BDD */ int crv_mysql_insert_host_table_info(MYSQL *sql, const char *host, int bandwidth, int ports) { string_t *str; string_t *esc; char value[10]; str = string_new(); esc = string_new(); /* * INSERT INTO `crv_127.0.0.1_info` (`host`, `bandwidth`, `port`) VALUES * ('127.0.0.1', 850, 1664); */ string_ajout(str, "INSERT INTO `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "_info` (`host`, `bandwidth`, `port`) VALUES ('"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "',"); snprintf(value, 10, "%d", bandwidth); string_my_escape(sql, esc, value); string_ajout(str, esc->str); string_ajout(str, ", "); snprintf(value, 10, "%d", ports); string_my_escape(sql, esc, value); string_ajout(str, esc->str); string_ajout(str, ")"); if (0 != mysql_query(sql, (const char *)str->str)) { fprintf(stderr, "Echec: erreur num %d\n", mysql_errno(sql)); fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); return 0; }
/* Create HOST INFO on BDD */ int crv_mysql_create_host_table_info(MYSQL *sql, const char *host) { string_t *str; string_t *esc; str = string_new(); esc = string_new(); /* * CREATE TABLE IF NOT EXISTS `crv_127.0.0.1_info` ( * `host` varchar(128) NOT NULL, * `bandwidth` int(10) NOT NULL, * `port` int(5) NOT NULL * ) */ string_ajout(str, "CREATE TABLE IF NOT EXISTS `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "_info`(`host` varchar(128) NOT NULL,\ `bandwidth` int(10) NOT NULL,\ `port` int(5) NOT NULL\ )"); if (0 != mysql_query(sql, (const char *)str->str)) { fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); return 0; }
int crv_mysql_truncate_host_tables(MYSQL *sql, const char *host) { string_t *str; string_t *esc; /* * TRUNCATE `crv_127.0.0.1`; */ str = string_new(); esc = string_new(); string_ajout(str, "TRUNCATE `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "`;"); if (0 != mysql_query(sql, (const char *)str->str)) { fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); /* * TRUNCATE `crv_127.0.0.1_cat`; */ str = string_new(); esc = string_new(); string_ajout(str, "TRUNCATE `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "_cat`;"); if (0 != mysql_query(sql, (const char *)str->str)) { fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); /* * TRUNCATE `crv_127.0.0.1_info`; */ str = string_new(); esc = string_new(); string_ajout(str, "TRUNCATE `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "_info`;"); if (0 != mysql_query(sql, (const char *)str->str)) { fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); /* * TRUNCATE `crv_127.0.0.1_grp`; */ str = string_new(); esc = string_new(); string_ajout(str, "TRUNCATE `crv_"); string_my_escape(sql, esc, host); string_ajout(str, esc->str); string_ajout(str, "_group`;"); if (0 != mysql_query(sql, (const char *)str->str)) { fprintf(stderr, "Echec: '%s' %s\n", str->str, mysql_error(sql)); mysql_close(sql); exit(EXIT_FAILURE); } string_free(str); string_free(esc); return 0; }
int main(int argc, const char * argv[]) { //expect 4 args : email, user name, plain password (10 to 20 chars), days. String controls have been made before calling this program. if (argc != 5) exit(EXIT_FAILURE); newUser_t *user; char *local; char *rsapriv; char rsapriv_key[33]; size_t pwd_size; user = malloc(sizeof(newUser_t)); user->email = strdup(argv[1]); user->name = strdup(argv[2]); user->days = strdup(argv[4]); //passphrase & aeskey pwd_size = strlen(argv[3]); if ( pwd_size < 10 || pwd_size > 20 ) { fprintf(stderr, "incorrect password length : %zu chars.", pwd_size); exit(EXIT_FAILURE); } user->passphrase = random_string(32); memcpy(rsapriv_key, argv[3], pwd_size); memcpy(rsapriv_key + pwd_size, user->passphrase + pwd_size, 32 - pwd_size); rsapriv_key[32] = '\0'; libgcrypt_initialize(); //rsa keys generate_rsa_keypair( &user->rsapub, &rsapriv); cipher_key(rsapriv, rsapriv_key, &user->rsapriv_crypt); //hash password char *settings; settings = crypt_gensalt_ra("$2a$", 7, random_string(16), 16); user->pwd = string_new(); string_ajout(user->pwd, "{BLF-CRYPT}"); string_ajout(user->pwd, crypt(argv[3], settings)); //build maildir string user->maildir = string_new(); string_ajout(user->maildir, "/"); local = strtok(strdup(argv[1]), "@"); string_ajout(user->maildir, strtok(NULL, "@")); string_ajout(user->maildir, "/"); string_ajout(user->maildir, local); string_ajout(user->maildir, "/"); //insert user in 2 tables : users & aliases if (pg_creer_utilisateur(user) != -1) { printf("OK 1/2 : Utilisateur ajouté dans la table users.\n"); if (pg_creer_alias(user->email) != -1) { printf("OK 2/2 : Alias créé.\n"); } else { fprintf(stderr, "Erreur lors de la création de l'alias."); exit(EXIT_FAILURE); } } else { fprintf(stderr, "Erreur lors de l'insertion de l'utilisateur dans la base users."); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }