Use a proper rcode for no more rows
[freeradius.git] / src / modules / rlm_sql / sql.c
index d44743c..51ba11d 100644 (file)
@@ -49,6 +49,7 @@ const FR_NAME_NUMBER sql_rcode_table[] = {
        { "server error",       RLM_SQL_ERROR           },
        { "query invalid",      RLM_SQL_QUERY_INVALID   },
        { "no connection",      RLM_SQL_RECONNECT       },
+       { "no more rows",       RLM_SQL_NO_MORE_ROWS    },
        { NULL, 0 }
 };
 
@@ -109,13 +110,13 @@ int sql_fr_pair_list_afrom_str(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **h
        char const *ptr, *value;
        char buf[MAX_STRING_LEN];
        char do_xlat = 0;
-       FR_TOKEN token, operator = T_EOL;
+       FR_TOKEN token, op = T_EOL;
 
        /*
         *      Verify the 'Attribute' field
         */
        if (!row[2] || row[2][0] == '\0') {
-               REDEBUG("The 'Attribute' field is empty or NULL, skipping the entire row");
+               REDEBUG("Attribute field is empty or NULL, skipping the entire row");
                return -1;
        }
 
@@ -124,10 +125,9 @@ int sql_fr_pair_list_afrom_str(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **h
         */
        if (row[4] != NULL && row[4][0] != '\0') {
                ptr = row[4];
-               operator = gettoken(&ptr, buf, sizeof(buf), false);
-               if ((operator < T_OP_ADD) ||
-                   (operator > T_OP_CMP_EQ)) {
-                       REDEBUG("Invalid operator \"%s\" for attribute %s", row[4], row[2]);
+               op = gettoken(&ptr, buf, sizeof(buf), false);
+               if (!fr_assignment_op[op] && !fr_equality_op[op]) {
+                       REDEBUG("Invalid op \"%s\" for attribute %s", row[4], row[2]);
                        return -1;
                }
 
@@ -135,15 +135,21 @@ int sql_fr_pair_list_afrom_str(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **h
                /*
                 *  Complain about empty or invalid 'op' field
                 */
-               operator = T_OP_CMP_EQ;
-               REDEBUG("The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
+               op = T_OP_CMP_EQ;
+               REDEBUG("The op field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
                REDEBUG("You MUST FIX THIS if you want the configuration to behave as you expect");
        }
 
        /*
         *      The 'Value' field may be empty or NULL
         */
+       if (!row[3]) {
+               REDEBUG("Value field is empty or NULL, skipping the entire row");
+               return -1;
+       }
+
        value = row[3];
+
        /*
         *      If we have a new-style quoted string, where the
         *      *entire* string is quoted, do xlat's.
@@ -166,9 +172,9 @@ int sql_fr_pair_list_afrom_str(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **h
                 *      Mark the pair to be allocated later.
                 */
                case T_BACK_QUOTED_STRING:
-                       value = NULL;
                        do_xlat = 1;
-                       break;
+
+                       /* FALL-THROUGH */
 
                /*
                 *      Keep the original string.
@@ -182,7 +188,7 @@ int sql_fr_pair_list_afrom_str(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **h
        /*
         *      Create the pair
         */
-       vp = fr_pair_make(ctx, NULL, row[2], NULL, operator);
+       vp = fr_pair_make(ctx, NULL, row[2], NULL, op);
        if (!vp) {
                REDEBUG("Failed to create the pair: %s", fr_strerror());
                return -1;
@@ -190,7 +196,7 @@ int sql_fr_pair_list_afrom_str(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **h
 
        if (do_xlat) {
                if (fr_pair_mark_xlat(vp, value) < 0) {
-                       REDEBUG("Error marking pair for xlat");
+                       REDEBUG("Error marking pair for xlat: %s", fr_strerror());
 
                        talloc_free(vp);
                        return -1;
@@ -484,7 +490,7 @@ int sql_getvpdata(TALLOC_CTX *ctx, rlm_sql_t *inst, REQUEST *request, rlm_sql_ha
        rcode = rlm_sql_select_query(inst, request, handle, query);
        if (rcode != RLM_SQL_OK) return -1; /* error handled by rlm_sql_select_query */
 
-       while (rlm_sql_fetch_row(inst, request, handle) == 0) {
+       while (rlm_sql_fetch_row(inst, request, handle) == RLM_SQL_OK) {
                row = (*handle)->row;
                if (!row) break;
                if (sql_fr_pair_list_afrom_str(ctx, request, pair, row) != 0) {