Return to accmqtt.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / mqtt / src |
1.2 misho 1: #include "global.h"
2:
3:
4: extern const char sql_schema[];
5:
6:
7: /*
8: * mqtt_rtlm_log() Log database connection message
9: *
10: * @fmt = format string
11: * @... = argument list
12: * return: none
13: */
14: static void
15: mqtt_rtlm_log(const char *fmt, ...)
16: {
17: va_list lst;
18:
19: va_start(lst, fmt);
20: vsyslog(LOG_ERR, fmt, lst);
21: va_end(lst);
22: }
1.2.2.3 misho 23: #define MQTT_RTLM_LOG(_sql) (assert((_sql)), mqtt_rtlm_log("Error:: %s(%d) SQL #%d - %s", \
24: __func__, __LINE__, \
1.2 misho 25: sqlite3_errcode((_sql)), sqlite3_errmsg((_sql))))
26:
27:
28: /*
29: * mqtt_rtlm_open() Open database connection
30: *
31: * @cfg = loaded config
32: * return: NULL error or SQL handle
33: */
34: sqlite3 *
1.2.2.1 misho 35: mqtt_rtlm_open(cfg_root_t *cfg)
1.2 misho 36: {
37: sqlite3 *sql = NULL;
38: const char *str = NULL;
39:
40: if (!cfg)
41: return NULL;
42:
1.2.2.1 misho 43: str = (const char*) cfg_getAttribute(cfg, "mqtt_acc", "name");
1.2 misho 44: if (!str) {
45: mqtt_rtlm_log("Error:: Unknown database name ...\n");
46: return NULL;
47: }
48:
49: if (sqlite3_open_v2(str, &sql, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) {
50: MQTT_RTLM_LOG(sql);
51: sqlite3_close(sql);
52: return NULL;
53: }
54:
55: if (sqlite3_exec(sql, sql_schema, NULL, NULL, NULL)) {
56: MQTT_RTLM_LOG(sql);
57: sqlite3_close(sql);
58: return NULL;
59: }
1.2.2.4 ! misho 60:
1.2 misho 61: return sql;
62: }
63:
64: /*
65: * mqtt_rtlm_close() Close database connection
66: *
67: * @sql = SQL handle
68: * return: none
69: */
70: void
71: mqtt_rtlm_close(sqlite3 *sql)
72: {
73: sqlite3_close(sql);
74: }
75:
76: /*
77: * mqtt_rtlm_login() Verify login account
78: *
79: * @cfg = loaded config
80: * @sql = SQL handle
81: * @user = username
82: * @pass = password
83: * return: -1 error, 0 ALLOW and 1 REJECT
84: */
85: int
1.2.2.1 misho 86: mqtt_rtlm_login(cfg_root_t *cfg, sqlite3 *sql, const char *user, const char *pass)
1.2 misho 87: {
88: /* insert into Users values (NULL, "", "", 1, strftime('%s','now')); */
89: int ret = 0;
90: sqlite3_stmt *stmt;
91: char *str, szStmt[BUFSIZ] = { 0 };
92:
93: if (!sql)
94: return -1;
95:
1.2.2.1 misho 96: str = (char*) cfg_getAttribute(cfg, "mqtt_acc", "tbl_users");
1.2 misho 97: if (!str) {
98: mqtt_rtlm_log("Error:: not found users table name");
99: return -1;
100: }
101: snprintf(szStmt, sizeof szStmt, "SELECT DISTINCT Username, Password, Access FROM %s "
102: "WHERE Username = '%s' AND Password = '%s' AND Access > 0;", str, user, pass);
103:
104: if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) {
105: MQTT_RTLM_LOG(sql);
106: return -1;
107: }
108: while (sqlite3_step(stmt) == SQLITE_ROW) {
109: if (sqlite3_data_count(stmt) < 1)
110: ret = 0;
111: else
112: ret = 1;
113: break;
114: }
115: sqlite3_finalize(stmt);
116:
117: return ret;
118: }