Annotation of mqtt/src/accmqtt.c, revision 1.2.2.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>