aboutsummaryrefslogtreecommitdiffstats
path: root/src/dblayer.c
blob: 94af08919122f5ebf6a4a6e7adec7dee143eb41a (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* dblayer.c - Database layer for ck -----------------------------------*- C -*-
 *
 * This file is part of ck, the config keeper
 * 
 * -----------------------------------------------------------------------------
 *
 * Copyright (C) 2019  Anastasis Grammenos
 * GPLv3 (see LICENCE for the full notice)
 *
 * -------------------------------------------------------------------------- */
#include <libgen.h>

#include "dblayer.h"
#include "ckerrlog.h"

ERRLOG(ckdb);

static const char * const DB_FILE_NAME = "/ckdb";

/* figure out the database name */
static void make_db_name(char *ret, const char *confPath) {
  char db_path[STR_L] = "";
  strcpy(db_path, confPath);
  strcat(db_path, DB_FILE_NAME);

  strcpy(ret, db_path);
}

/* Check if the db file exists*/
int db_exists(const UserOpt *opt) {
  char db_path[STR_L] = "";
  make_db_name(db_path, opt->confDir);
  return util_is_file_rw(db_path);
}

/* check if db has the correct tables */
static int check_initialized_DB(sqlite3 *db) {
  char sql[STR_M] = "";
  dbh_fq_select_all_tables(sql);
  sqlite3_stmt *stmt;

  sqlite3_prepare_v2(db, sql, (int)strlen(sql), &stmt, NULL);

  int program_table_ok, config_table_ok, rel_table_ok = 0;
  while (sqlite3_step(stmt) != SQLITE_DONE) {
    const unsigned char *tmpbuf  = sqlite3_column_text(stmt, 1);
    if (strcmp((char *)tmpbuf, TBL_PROGRAM) == 0) {
      program_table_ok = 1;
    }
    if (strcmp((char *)tmpbuf, TBL_CONFIG) == 0) {
      config_table_ok = 1;
    }
    if (strcmp((char *)tmpbuf, TBL_REL) == 0) {
      rel_table_ok = 1;
    }
  }

  if (!program_table_ok
      || !config_table_ok
      || !rel_table_ok ) {
    return 1;
  }

  sqlite3_finalize(stmt);
  return 0;
}

void close_DB(DB *db) {
  sqlite3_close(db->ptr);
}

int open_DB(DB *db, const UserOpt *opt) {
  if (!db || !opt) {
    return -1;
  }
  int rc;
  char db_path[STR_L] = "";

  make_db_name(db_path, opt->confDir);
  rc = sqlite3_open(db_path, &db->ptr);

  if (rc) {
    ERR("%s is not a path to an sqlite3 database.", db_path);
    return -1;
  }

  if (opt->action != CKA_INIT) {
    if (check_initialized_DB(db->ptr)) {
      ERR("The database file is currupted. Run ck init anew.");
      return -1;
    }
  }

  LOG("Opened %s", db_path);
  return 0;
}

int get_program_id(DB *db, const char* name) {
  sqlite3_stmt *stmt;
  int rc;

  char sql[STR_M] = "";
  dhb_fq_find_program(sql);

  rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    PRINT_ERR("Error while preparing get_program_id sql.");
    return -2;
  }
  sqlite3_bind_text(stmt, 1, name, (int)strlen(name), 0);
  int id = -1;
  while (sqlite3_step(stmt) == SQLITE_ROW) {
    id = sqlite3_column_int(stmt, 0);
    break;
  }
  sqlite3_finalize(stmt);
  return id;
}

int program_exists(DB *db, const char *pName) {
  if (get_program_id(db, pName) == -1) {
    return 0;
  }
  return 1;
}

int get_config_id(DB *db, const char* path) {
  sqlite3_stmt *stmt;
  int rc;

  char sql[STR_M] = "";
  dhb_fq_find_config(sql);

  rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    PRINT_ERR("while preparing get_config_id sql.");
    return -2;
  }
  sqlite3_bind_text(stmt, 1, path, (int)strlen(path), 0);
  int id = -1;
  while (sqlite3_step(stmt) == SQLITE_ROW) {
    id = sqlite3_column_int(stmt, 0);
    break;
  }
  sqlite3_finalize(stmt);
  return id;
}

int program_has_primary_config(DB *db, const int pid, char *ret, int *sec) {
  sqlite3_stmt *stmt;
  int rc;

  char sql[STR_L] = "";

  char condition[STR_S] = TBL_PROGRAM;
  strcat(condition, ".");
  strcat(condition, COL_PROGRAM_ID);

  char selection[STR_M] = COL_CONFIG_PRIMARY;
  strcat(selection, ", ");
  strcat(selection, COL_CONFIG_PATH);
  strcat(selection, ", ");
  strcat(selection, COL_CONFIG_SECRET);

  dbh_fq_select_from_joined_eq(sql, selection, condition);

  rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    PRINT_ERR("whinnle preparing program_has_primary_exists sql.");
    return -2;
  }

  sqlite3_bind_int(stmt, 1, pid);
  while (sqlite3_step(stmt) == SQLITE_ROW) {
    if (sqlite3_column_int(stmt, 0) == 1) {
      if (ret) {
        strcpy(ret,(char *)sqlite3_column_text(stmt, 1));
      }
      if (sec) {
        *sec = sqlite3_column_int(stmt, 2);
      }
      sqlite3_finalize(stmt);
      return 1;
    }
  }
  sqlite3_finalize(stmt);
  return 0;
}

int get_program_relations(DB *db, int pid) {
  int count = -1;
  sqlite3_stmt *stmt;
  int rc;

  char sql[STR_M] = "";
  dbh_fq_count_program_relations(sql);

  rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    return -1;
  }

  sqlite3_bind_int(stmt, 1, pid);
  while (sqlite3_step(stmt) == SQLITE_ROW) {
    count = sqlite3_column_int(stmt, 0);
  }

  if (rc != SQLITE_OK) {
    return -1;
  }
  sqlite3_finalize(stmt);
  return count;
}

int get_config_number(DB *db, char* pName) {
  int pid = get_program_id(db, pName);
  /* program exists */
  if (pid > -1) {
    return get_program_relations(db, pid);
  }
  return -1;
}

int get_pid_from_cid(DB *db, int cid) {
  int pid = -1;
  sqlite3_stmt *stmt;
  int rc;

  char sql[STR_M] = "";
  dbh_fq_get_pid_from_cid(sql);

  rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    return -1;
  }

  sqlite3_bind_int(stmt, 1, cid);

  while (sqlite3_step(stmt) == SQLITE_ROW) {
    pid = sqlite3_column_int(stmt, 0);
  }

  if (rc != SQLITE_OK) {
    return -1;
  }
  sqlite3_finalize(stmt);
  return pid;
}

void print_suggested_configs(DB *db, const char *pName) {
  char name[STR_M] = "";
  strcat(name, pName);
  strcat(name, ":");
  cklist *paths = list_make_and_add(name);
  get_program_paths(db, paths, pName, 1, 0, NULL);
  list_print(paths);
  list_free(paths);
}

int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int attr, const char *home) {
  int pid = get_program_id(db, pName);
  /* error */
  if (pid == -2) {
    return -1;
  }

  /* program exists */
  if (pid > -1) {
    sqlite3_stmt *stmt;
    int rc;

    char selection[STR_M] = COL_CONFIG_PATH;
    strcat(selection, ",");
    strcat(selection, COL_CONFIG_SECRET);
    strcat(selection, ",");
    strcat(selection, COL_CONFIG_PRIMARY);
    char condition[STR_M] = TBL_PROGRAM;
    strcat(condition, ".");
    strcat(condition, COL_PROGRAM_ID);

    char sql[STR_L] = "";
    dbh_fq_select_from_joined_eq(sql, selection, condition);

    rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
    sqlite3_bind_int(stmt, 1, pid);
    if (rc != SQLITE_OK) {
      return -2;
    }

    while (sqlite3_step(stmt) == SQLITE_ROW) {
      char *tmp = strdup((char *)sqlite3_column_text(stmt, 0));
      char entry[STR_M] = "";
      if (bname) {
        strcat(entry, basename(tmp));
      }
      else {
        char tpath[STR_L] = "";
        if (swap_tilde_with_home(tpath, tmp, home)) {
          strcat(entry, tpath);
        }
        else {
          strcat(entry, tmp);
        }
      }
      if (attr) {
        decorate_entry(entry, sqlite3_column_int(stmt, 1),
                       sqlite3_column_int(stmt, 2),
                       (char *)sqlite3_column_text(stmt, 0));
      }
      list_add(ckl, entry);
      free(tmp);
    }
    sqlite3_finalize(stmt);
    return 0;
  }
  return -1;
}

int secret_enabled(DB *db) {
  sqlite3_stmt *stmt;
  int rc;

  char sql[STR_M] = "";
  dbh_fq_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;
}