aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-11-19 19:11:06 +0200
committergramanas <anastasis.gramm2@gmail.com>2018-11-19 19:11:06 +0200
commit78ee1c72c670a71bfd165448676fc65bff802916 (patch)
tree893b0f09dfc84f3fd7f1ac218213012f33d5b612 /src
parent84bbbb91c9154ca0b1c295eb7d1aa0de59450764 (diff)
downloadck-78ee1c72c670a71bfd165448676fc65bff802916.tar.gz
ck-78ee1c72c670a71bfd165448676fc65bff802916.tar.bz2
ck-78ee1c72c670a71bfd165448676fc65bff802916.zip
Add ability to use ck without secret dir
Diffstat (limited to 'src')
-rw-r--r--src/actions.h1
-rw-r--r--src/add.c15
-rw-r--r--src/clparser.h2
-rw-r--r--src/confparser.c12
-rw-r--r--src/confparser.h13
-rw-r--r--src/dblayer.c21
-rw-r--r--src/dblayer.h2
-rw-r--r--src/init.c45
-rw-r--r--src/list.c2
-rw-r--r--src/queries.c32
-rw-r--r--src/queries.h8
11 files changed, 126 insertions, 27 deletions
diff --git a/src/actions.h b/src/actions.h
index 1b7ef05..0cf4b8f 100644
--- a/src/actions.h
+++ b/src/actions.h
@@ -28,6 +28,7 @@ enum AddOptErrors {
ADD_NO_ERR = 0,
ADD_ERR_WRONG_CONFIG,
ADD_ERR_LINK_CONFIG,
+ ADD_ERR_NO_SECRET,
ADD_ERR_WRONG_FLAGS
};
typedef enum AddOptErrors AddOptErr;
diff --git a/src/add.c b/src/add.c
index 574ac73..79227d1 100644
--- a/src/add.c
+++ b/src/add.c
@@ -204,7 +204,7 @@ static int add_transaction_try(DB *db, const AddOpt * const opt, const char *hom
}
static int link_config(const AddOpt *opt, const char* newPath) {
- hLOG("Linking %s -> %s\n", newPath, opt->confPath);
+ hLOG("Linking %s -> %s", newPath, opt->confPath);
if (util_symlink_file(newPath, opt->confPath) != 0) {
ERR("Could not link file.");
return -1;
@@ -222,7 +222,7 @@ static int move_config(const AddOpt *opt, char *progDir, char *ret) {
return -1;
}
strcpy(ret, newPath);
- hLOG("Moving %s -> %s\n", opt->confPath, newPath);
+ hLOG("Moving %s -> %s", opt->confPath, newPath);
if (util_move_file(opt->confPath, newPath) != 0) {
ERR("Could not move file.");
return -1;
@@ -230,7 +230,7 @@ static int move_config(const AddOpt *opt, char *progDir, char *ret) {
return 0;
}
-static AddOpt add_make_options(cklist *args) {
+static AddOpt add_make_options(cklist *args, DB *db) {
list_rewind(args);
/* since we are here, the first two arguments must exist */
AddOpt addOpt = {
@@ -254,6 +254,10 @@ static AddOpt add_make_options(cklist *args) {
while (list_next(args)) {
if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) {
+ if (!secret_enabled(db)) {
+ addOpt.err = ADD_ERR_NO_SECRET;
+ return addOpt;
+ }
addOpt.secret = 1;
} else if (strcmp(list_get(args), "-p") == 0 && addOpt.prime == 0) {
addOpt.prime = 1;
@@ -304,10 +308,13 @@ int run_ADD(UserOpt * opt, Conf *conf) {
if (open_DB(&db, opt)) {
return -1;
}
- AddOpt addOpt = add_make_options(opt->args);
+ AddOpt addOpt = add_make_options(opt->args, &db);
switch (addOpt.err) {
case ADD_NO_ERR:
break;
+ case ADD_ERR_NO_SECRET:
+ ERR("Secret is not enabled for this ck instance.");
+ goto error;
case ADD_ERR_LINK_CONFIG:
ERR("%s is a link.", addOpt.confPath);
goto error;
diff --git a/src/clparser.h b/src/clparser.h
index 4208a91..afb6260 100644
--- a/src/clparser.h
+++ b/src/clparser.h
@@ -22,7 +22,7 @@
/* NAME | MIN ACCEPTED ARGS | MAX ACCEPTED ARGS */
#define CK_ACTIONS \
- X(INIT, 2 , 2) \
+ X(INIT, 1 , 2) \
X(ADD, 2, 4) \
X(DEL, 1, 2) \
X(EDIT, 1, 5) \
diff --git a/src/confparser.c b/src/confparser.c
index 6c01e6d..48f0c71 100644
--- a/src/confparser.c
+++ b/src/confparser.c
@@ -16,7 +16,7 @@ ERRLOG(configfile);
static const char * const CONFIG_NAME = "/ckrc";
void initialize_conf(Conf *c) {
-#define X(var, str, name) \
+#define X(var, str, name, optional) \
c->var = NULL;
CONFIG_VARIABLES_TABLE
#undef X
@@ -35,7 +35,7 @@ ConfVar match_variables(char *line, char matched[]) {
if (line[0] == '#' || str_is_empty(line)) {
return CV_NO_VAL_OR_COMMENT;
}
-#define X(var, str, name) \
+#define X(var, str, name, optional) \
if (sscanf(line, str, matched) == 1) { \
return CV_##var; \
}
@@ -109,7 +109,7 @@ int config_file_parse(Conf *conf, UserOpt *opt) {
return -1;
}
switch(match_variables(line, matched)) {
-#define X(var, str, name) \
+#define X(var, str, name, optional) \
case CV_##var: \
conf->var = malloc(strlen(matched)+1); \
strcpy(conf->var, matched); \
@@ -131,8 +131,8 @@ int config_file_parse(Conf *conf, UserOpt *opt) {
}
/* Could add an optional row that would make the config var
* optional. */
-#define X(var, str, name) \
- if (!conf->var) { \
+#define X(var, str, name, optional) \
+ if (!optional && !conf->var) { \
ERR("Missing %s", name); \
return -1; \
}
@@ -142,7 +142,7 @@ int config_file_parse(Conf *conf, UserOpt *opt) {
}
void free_conf(Conf *conf) {
-#define X(var,str,name) \
+#define X(var, str, name, optional) \
if (conf->var) { \
free(conf->var); \
}
diff --git a/src/confparser.h b/src/confparser.h
index ebcf0c7..10e7e18 100644
--- a/src/confparser.h
+++ b/src/confparser.h
@@ -18,15 +18,16 @@
#include "clparser.h"
-#define CONFIG_VARIABLES_TABLE \
- X(vc_dir, " version_control_dir = %s ", "Version Control directory") \
- X(scrt_dir, " secret_dir = %s " , "Secret directory") \
- X(home_dir, " home_dir = %s " , "Home directory")
+/* name | match str | desc | optional */
+#define CONFIG_VARIABLES_TABLE \
+ X(vc_dir, " version_control_dir = %s ", "Version Control directory", 0) \
+ X(scrt_dir, " secret_dir = %s " , "Secret directory", 1) \
+ X(home_dir, " home_dir = %s " , "Home directory", 0)
enum ConfingVariables {
CV_NO_VAL_OR_COMMENT,
CV_WRONG_VAL,
-#define X(var, str, name) CV_##var,
+#define X(var, str, name, optional) CV_##var,
CONFIG_VARIABLES_TABLE
#undef X
};
@@ -34,7 +35,7 @@ typedef enum ConfingVariables ConfVar;
typedef struct ConfigValues Conf;
struct ConfigValues {
-#define X(var, str, name) char* var;
+#define X(var, str, name, optional) char* var;
CONFIG_VARIABLES_TABLE
#undef X
};
diff --git a/src/dblayer.c b/src/dblayer.c
index f13236c..3a8dd4f 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -315,3 +315,24 @@ int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int att
}
return -1;
}
+
+int secret_enabled(DB *db) {
+ sqlite3_stmt *stmt;
+ int rc;
+
+ char sql[STR_M] = "";
+ dbh_form_query_secret_enabled(sql);
+
+ rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
+ if (rc != SQLITE_OK) {
+ ERR("while preparing secret_enabled sql.");
+ return -2;
+ }
+ int enabled = 1;
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ enabled = sqlite3_column_int(stmt, 0);
+ break;
+ }
+ sqlite3_finalize(stmt);
+ return enabled;
+}
diff --git a/src/dblayer.h b/src/dblayer.h
index 08299f1..fb3ab90 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -63,8 +63,8 @@ int get_pid_from_cid(DB *db, int cid);
void print_suggested_configs(DB *db, const char *pName);
-/* list.c */
int list_get_paths(DB *db, cklist *ckl, int bName, int attr, const char *home);
int list_get_programs(DB *db, cklist *ckl);
+int secret_enabled(DB *db);
#endif /* DBLAYER_H */
diff --git a/src/init.c b/src/init.c
index 5f47b33..10f38cb 100644
--- a/src/init.c
+++ b/src/init.c
@@ -15,15 +15,18 @@ ERRLOG(init);
static int init_create_config_file(UserOpt *opt) {
char absVCdir[STR_L] = "";
- if (!util_file_exists(list_get_at(opt->args, 0), absVCdir)) {
+ list_rewind(opt->args);
+ if (!util_file_exists(list_get(opt->args), absVCdir)) {
ERR("Version control directory: %s does not exist.", list_get_at(opt->args, 0));
return 1;
}
char absSRdir[STR_L] = "";
- if (!util_file_exists(list_get_at(opt->args, 1), absSRdir)) {
- ERR("Secret directory: %s does not exist.", list_get_at(opt->args, 1));
- return 1;
+ if (list_next(opt->args)) {
+ if (!util_file_exists(list_get_at(opt->args, 1), absSRdir)) {
+ ERR("Secret directory: %s does not exist.", list_get_at(opt->args, 1));
+ return 1;
+ }
}
if (!util_file_exists(opt->confDir, NULL)) {
@@ -43,10 +46,13 @@ static int init_create_config_file(UserOpt *opt) {
strcat(tmp, "\n");
fputs(tmp, f);
- strcpy(tmp, "secret_dir = ");
- strcat(tmp, absSRdir);
- strcat(tmp, "\n");
- fputs(tmp, f);
+ /* if absSRdir is not equal to "" */
+ if (strcmp(absSRdir, "")) {
+ strcpy(tmp, "secret_dir = ");
+ strcat(tmp, absSRdir);
+ strcat(tmp, "\n");
+ fputs(tmp, f);
+ }
strcpy(tmp, "home_dir = ");
strcat(tmp, getenv("HOME"));
@@ -57,6 +63,27 @@ static int init_create_config_file(UserOpt *opt) {
return 0;
}
+static int set_secret_enabled(DB *db, int enabled) {
+ sqlite3_stmt *stmt;
+ int rc;
+
+ char sql[STR_M] = "";
+ dbh_form_query_set_secret_enabled(sql);
+
+ rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
+ if (rc != SQLITE_OK) {
+ ERR("while preparing set_secret_enabled sql.");
+ return -2;
+ }
+ sqlite3_bind_int(stmt, 1, enabled);
+ if (sqlite3_step(stmt) != SQLITE_DONE) {
+ ERR("while excecuting set_secret_enabled sql.");
+ return -1;
+ }
+ sqlite3_finalize(stmt);
+ return 0;
+}
+
static void init_make_tables(DB *db) {
char sql[STR_L] = "";
dbh_form_query_make_tables(sql);
@@ -84,6 +111,8 @@ int run_INIT(UserOpt * opt, Conf *conf) {
return -1;
}
init_make_tables(&db);
+ /* If there are two arguments secret is enabled */
+ set_secret_enabled(&db, list_size(opt->args) == 2);
sqlite3_close(db.ptr);
hLOG("Initialized empty ckdb.");
return 0;
diff --git a/src/list.c b/src/list.c
index 52603b8..0976ac1 100644
--- a/src/list.c
+++ b/src/list.c
@@ -244,7 +244,7 @@ int run_LIST(UserOpt *opt, Conf *conf) {
strcat(tmp, "ck configuration directory path: ");
strcat(tmp, opt->confDir);
list_add(the_list, tmp);
-#define X(var, str, name) \
+#define X(var, str, name, optional) \
strcpy(tmp, ""); \
strcat(tmp, name); \
strcat(tmp, ": "); \
diff --git a/src/queries.c b/src/queries.c
index 15e5acb..ddd9250 100644
--- a/src/queries.c
+++ b/src/queries.c
@@ -40,6 +40,14 @@ void dbh_form_query_make_tables(char *query) {
strcat(tmp, COL_REL_CONFIG_ID);
strcat(tmp, " INT NOT NULL);");
+ strcat(tmp, "CREATE TABLE ");
+ strcat(tmp, TBL_CTX);
+ strcat(tmp, "(");
+ strcat(tmp, COL_CTX_KEY);
+ strcat(tmp, " TEXT NOT NULL, ");
+ strcat(tmp, COL_CTX_VAL);
+ strcat(tmp, " INT NOT NULL);");
+
strcpy(query, tmp);
}
@@ -226,3 +234,27 @@ void dbh_form_query_get_pid_from_cid(char *query) {
strcpy(query, tmp);
}
+
+void dbh_form_query_secret_enabled(char *query) {
+ char tmp[STR_M] = "SELECT ";
+ strcat(tmp, COL_CTX_VAL);
+ strcat(tmp, " FROM ");
+ strcat(tmp, TBL_CTX);
+ strcat(tmp, " WHERE ");
+ strcat(tmp, COL_CTX_KEY);
+ strcat(tmp, " = \"");
+ strcat(tmp, KEY_CTX_SECRET);
+ strcat(tmp, "\";");
+
+ strcpy(query, tmp);
+}
+
+void dbh_form_query_set_secret_enabled(char *query) {
+ char tmp[STR_L] = "INSERT INTO ";
+ strcat(tmp, TBL_CTX);
+ strcat(tmp, " VALUES(\"");
+ strcat(tmp, KEY_CTX_SECRET);
+ strcat(tmp, "\", ?);");
+
+ strcpy(query, tmp);
+}
diff --git a/src/queries.h b/src/queries.h
index 7d5ec47..aaaa862 100644
--- a/src/queries.h
+++ b/src/queries.h
@@ -24,6 +24,7 @@
#define TBL_PROGRAM "PROGRAM"
#define TBL_CONFIG "CONFIG"
#define TBL_REL "REL"
+#define TBL_CTX "CONTEXT"
#define COL_PROGRAM_ID "ID"
#define COL_PROGRAM_NAME "NAME"
@@ -36,6 +37,11 @@
#define COL_REL_PROGRAM_ID "PID"
#define COL_REL_CONFIG_ID "CID"
+#define COL_CTX_KEY "KEY"
+#define COL_CTX_VAL "VAL"
+
+#define KEY_CTX_SECRET "secret_enabled"
+
#define __BEGIN_TRANSACTION__ sqlite3_exec(db->ptr, "BEGIN TRANSACTION;", NULL, NULL, NULL);
#define __END_TRANSACTION__ sqlite3_exec(db->ptr, "END TRANSACTION;", NULL, NULL, NULL);
@@ -59,4 +65,6 @@ void dbh_form_query_select_from_joined_like(char *query, const char *selection,
void dbh_form_query_delete_x_from_y(char *query, const char *x, const char *y);
void dbh_form_query_count_program_relations(char *query);
void dbh_form_query_get_pid_from_cid(char *query);
+void dbh_form_query_secret_enabled(char *query);
+void dbh_form_query_set_secret_enabled(char *query);
#endif /* DBHELPER_H */