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

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

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