Annotation of mqtt/src/accmqtt.c, revision 1.2.2.3
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.2 misho 43: sqlite3_config(SQLITE_CONFIG_SERIALIZED);
44: if (!sqlite3_threadsafe())
1.2 misho 45: return NULL;
46:
1.2.2.1 misho 47: str = (const char*) cfg_getAttribute(cfg, "mqtt_acc", "name");
1.2 misho 48: if (!str) {
49: mqtt_rtlm_log("Error:: Unknown database name ...\n");
50: return NULL;
51: }
52:
53: if (sqlite3_open_v2(str, &sql, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) {
54: MQTT_RTLM_LOG(sql);
55: sqlite3_close(sql);
56: return NULL;
57: }
58:
1.2.2.2 misho 59: sqlite3_mutex_enter(sqlite3_db_mutex(sql));
1.2 misho 60: if (sqlite3_exec(sql, sql_schema, NULL, NULL, NULL)) {
61: MQTT_RTLM_LOG(sql);
1.2.2.2 misho 62: sqlite3_mutex_leave(sqlite3_db_mutex(sql));
1.2 misho 63: sqlite3_close(sql);
64: return NULL;
65: }
1.2.2.2 misho 66: sqlite3_mutex_leave(sqlite3_db_mutex(sql));
1.2 misho 67: return sql;
68: }
69:
70: /*
71: * mqtt_rtlm_close() Close database connection
72: *
73: * @sql = SQL handle
74: * return: none
75: */
76: void
77: mqtt_rtlm_close(sqlite3 *sql)
78: {
79: sqlite3_close(sql);
80: }
81:
82: /*
83: * mqtt_rtlm_login() Verify login account
84: *
85: * @cfg = loaded config
86: * @sql = SQL handle
87: * @user = username
88: * @pass = password
89: * return: -1 error, 0 ALLOW and 1 REJECT
90: */
91: int
1.2.2.1 misho 92: mqtt_rtlm_login(cfg_root_t *cfg, sqlite3 *sql, const char *user, const char *pass)
1.2 misho 93: {
94: /* insert into Users values (NULL, "", "", 1, strftime('%s','now')); */
95: int ret = 0;
96: sqlite3_stmt *stmt;
97: char *str, szStmt[BUFSIZ] = { 0 };
98:
99: if (!sql)
100: return -1;
101:
1.2.2.1 misho 102: str = (char*) cfg_getAttribute(cfg, "mqtt_acc", "tbl_users");
1.2 misho 103: if (!str) {
104: mqtt_rtlm_log("Error:: not found users table name");
105: return -1;
106: }
107: snprintf(szStmt, sizeof szStmt, "SELECT DISTINCT Username, Password, Access FROM %s "
108: "WHERE Username = '%s' AND Password = '%s' AND Access > 0;", str, user, pass);
109:
1.2.2.2 misho 110: sqlite3_mutex_enter(sqlite3_db_mutex(sql));
1.2 misho 111: if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) {
112: MQTT_RTLM_LOG(sql);
1.2.2.2 misho 113: sqlite3_mutex_leave(sqlite3_db_mutex(sql));
1.2 misho 114: return -1;
115: }
116: while (sqlite3_step(stmt) == SQLITE_ROW) {
117: if (sqlite3_data_count(stmt) < 1)
118: ret = 0;
119: else
120: ret = 1;
121: break;
122: }
123: sqlite3_finalize(stmt);
1.2.2.2 misho 124: sqlite3_mutex_leave(sqlite3_db_mutex(sql));
1.2 misho 125:
126: return ret;
127: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>