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

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