--- /dev/null
+# $Id$
+
+EXTRA_CFLAGS =
+EXTRA_LDFLAGS =
+EXTRA_LIBS = -lcrypto
+
+.PHONY: all clean install
+
+all: ./mkpasswd
+
+./mkpasswd: ./mkpasswd.o ./util.o ./db.o ./crypto.o
+ $(CC) $(LDFLAGS) $(EXTRA_LDFLAGS) -o $@ $^ $(LIBS) $(EXTRA_LIBS)
+
+./mkpasswd.o: ./mkpasswd.c
+ $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
+
+./util.o: ../Mandshurica/util.c
+ $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
+
+./db.o: ../Mandshurica/db.c
+ $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
+
+./crypto.o: ../Mandshurica/crypto.c
+ $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
+
+install:
+ cp ./mkpasswd $(PREFIX)/bin/
+
+clean:
+ rm -f ./mkpasswd ./*.o
--- /dev/null
+/* $Id$ */
+/* --- START LICENSE --- */
+/* --- END LICENSE --- */
+
+#include "../Mandshurica/ms_crypto.h"
+#include "../Mandshurica/ms_db.h"
+#include "../Mandshurica/ms_util.h"
+
+#include <termios.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+
+const char avail[] = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
+
+int main(int argc, char** argv){
+ if(argc != 3){
+ fprintf(stderr, "Usage: %s database username\n", argv[0]);
+ return 1;
+ }
+ int retcode = 0;
+ struct ms_db* db = mandshurica_db_open(argv[1]);
+ if(db != NULL){
+ struct termios oldattr, newattr;
+ tcgetattr(STDIN_FILENO, &oldattr);
+ newattr = oldattr;
+ newattr.c_lflag &= ~(ECHO | ICANON);
+ tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
+ char cbuf[2];
+ cbuf[1] = 0;
+ printf("New password: ");
+ fflush(stdout);
+ char* passwd = malloc(1);
+ passwd[0] = 0;
+ while(1){
+ cbuf[0] = getchar();
+ if(cbuf[0] == '\n') break;
+ char* tmp = passwd;
+ passwd = mandshurica_strcat(tmp, cbuf);
+ free(tmp);
+ }
+ printf("\n");
+ printf("Verify password: ");
+ fflush(stdout);
+ char* vpasswd = malloc(1);
+ vpasswd[0] = 0;
+ while(1){
+ cbuf[0] = getchar();
+ if(cbuf[0] == '\n') break;
+ char* tmp = vpasswd;
+ vpasswd = mandshurica_strcat(tmp, cbuf);
+ free(tmp);
+ }
+ printf("\n");
+ if(strcmp(passwd, vpasswd) == 0){
+ char* salt = malloc(17);
+ salt[16] = 0;
+ int i;
+ srand(time(NULL));
+ for(i = 0; i < 16; i++){
+ salt[i] = avail[rand() % sizeof(avail)];
+ }
+ char* _pwd = mandshurica_strcat(passwd, salt);
+ char* pwd_sha512 = mandshurica_sha512(_pwd);
+ free(_pwd);
+ char* pwd = mandshurica_strcat3(pwd_sha512, "$", salt);
+ free(pwd_sha512);
+ free(salt);
+ free(vpasswd);
+ free(passwd);
+ mandshurica_db_write(db, argv[2], pwd);
+ free(pwd);
+ }else{
+ fprintf(stderr, "Password mismatch\n");
+ retcode = 1;
+ }
+
+ tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
+ mandshurica_db_close(db);
+ }else{
+ fprintf(stderr, "Failed to open the database\n");
+ retcode = 1;
+ }
+ return retcode;
+}
.PHONY: all clean format replace ./Mandshurica ./Module ./Tool
-all: ./config.mk ./Mandshurica ./Module ./Tool
+all: ./config.mk ./Mandshurica ./Module ./Tool ./CTool
./config.mk:
echo "PREFIX := $(PREFIX)" > $@
echo "TOOL_PREFIX := $(TOOL_PREFIX)" >> $@
echo "DATABASE_PREFIX := $(DATABASE_PREFIX)" >> $@
+PREFIXES := PREFIX="$(PREFIX)" MODULE_PREFIX="$(MODULE_PREFIX)" PROJECT_PREFIX="$(PROJECT_PREFIX)" TOOL_PREFIX="$(TOOL_PREFIX)" WEBROOT_PREFIX="$(WEBROOT_PREFIX)"
+
CC := gcc
CFLAGS := -g -std=c99 -DPREFIX=\\\"$(PREFIX)\\\" -DWEBROOT_PREFIX=\\\"$(WEBROOT_PREFIX)\\\" -DMODULE_PREFIX=\\\"$(MODULE_PREFIX)\\\" -DPROJECT_PREFIX=\\\"$(PROJECT_PREFIX)\\\" -DTOOL_PREFIX=\\\"$(TOOL_PREFIX)\\\" -DDATABASE_PREFIX=\\\"$(DATABASE_PREFIX)\\\" -D_DEFAULT_SOURCE
LDFLAGS :=
./Tool::
$(MAKE) -C $@ CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" LIBS="$(LIBS)"
+./CTool::
+ $(MAKE) -C $@ CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" LIBS="$(LIBS)"
+
format:
clang-format -i $(wildcard Module/*.c Module/*.h Mandshurica/*.c Mandshurica/*.h)
touch $(PREFIX)/lib/mandshurica/db/auth.db $(PREFIX)/lib/mandshurica/db/login.db $(PREFIX)/lib/mandshurica/db/project.db
cp ./mkpasswd.pl $(PREFIX)/bin/
./Mandshurica/mandshurica --create $(PREFIX)/etc/mandshurica.conf
- $(MAKE) -C ./Mandshurica install PREFIX="$(PREFIX)" MODULE_PREFIX="$(MODULE_PREFIX)" PROJECT_PREFIX="$(PROJECT_PREFIX)" TOOL_PREFIX="$(TOOL_PREFIX)" WEBROOT_PREFIX="$(WEBROOT_PREFIX)"
- $(MAKE) -C ./Module install PREFIX="$(PREFIX)" MODULE_PREFIX="$(MODULE_PREFIX)" PROJECT_PREFIX="$(PROJECT_PREFIX)" TOOL_PREFIX="$(TOOL_PREFIX)" WEBROOT_PREFIX="$(WEBROOT_PREFIX)" DATABASE_PREFIX="$(DATABASE_PREFIX)"
- $(MAKE) -C ./Tool install PREFIX="$(PREFIX)" MODULE_PREFIX="$(MODULE_PREFIX)" PROJECT_PREFIX="$(PROJECT_PREFIX)" TOOL_PREFIX="$(TOOL_PREFIX)" WEBROOT_PREFIX="$(WEBROOT_PREFIX)" DATABASE_PREFIX="$(DATABASE_PREFIX)"
- $(MAKE) -C ./Webroot install PREFIX="$(PREFIX)" MODULE_PREFIX="$(MODULE_PREFIX)" PROJECT_PREFIX="$(PROJECT_PREFIX)" TOOL_PREFIX="$(TOOL_PREFIX)" WEBROOT_PREFIX="$(WEBROOT_PREFIX)" DATABASE_PREFIX="$(DATABASE_PREFIX)"
+ $(MAKE) -C ./Mandshurica install $(PREFIXES)
+ $(MAKE) -C ./Module install $(PREFIXES)
+ $(MAKE) -C ./Tool install $(PREFIXES)
+ $(MAKE) -C ./Webroot install $(PREFIXES)
+ $(MAKE) -C ./CTool install $(PREFIXES)
@echo "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
@echo "Mandshurica executable has been installed to $(PREFIX)/bin"
- @echo "mkpasswd.pl has been installed to $(PREFIX)/bin"
+ @echo "mkpasswd has been installed to $(PREFIX)/bin"
@echo "Authentication database is at $(PREFIX)/lib/mandshurica/db/auth.db"
@echo
@echo "Thank you for choosing Mandshurica"
$(MAKE) -C ./Mandshurica clean
$(MAKE) -C ./Module clean
$(MAKE) -C ./Tool clean
+ $(MAKE) -C ./CTool clean
rm -f ./config.mk
./main.o: ./main.c ./mandshurica.h
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
-./%.o: ./%.c ./mandshurica%.h
+./%.o: ./%.c ./ms_%.h
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
install:
mandshurica_db_close(db);
return MS_AUTH_FAIL;
} else {
- char* sh = config->mandshurica_sha512(password);
+ int i;
+ bool has_salt = false;
+ char* salt = NULL;
+ for(i = 0; pwd[i] != 0; i++) {
+ if(pwd[i] == '$') {
+ pwd[i] = 0;
+ has_salt = true;
+ salt = mandshurica_strdup(pwd + i + 1);
+ break;
+ }
+ }
+ char* sh;
+ if(has_salt) {
+ char* password_wsalt = mandshurica_strcat(password, salt);
+ sh = config->mandshurica_sha512(password_wsalt);
+ free(password_wsalt);
+ free(salt);
+ } else {
+ /* Backward compatible, not recommended */
+ sh = config->mandshurica_sha512(password);
+ }
if(strcmp(sh, pwd) == 0) {
free(sh);
*ptr = generate_token(username);
+ free(pwd);
return MS_AUTH_OK;
}
free(sh);
+ free(pwd);
return MS_AUTH_FAIL;
}
mandshurica_db_close(db);