~ubuntu-branches/ubuntu/hardy/freeradius/hardy-proposed

« back to all changes in this revision

Viewing changes to src/modules/rlm_sql/sql.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2006-12-16 20:45:11 UTC
  • mfrom: (3.1.10 feisty)
  • Revision ID: james.westby@ubuntu.com-20061216204511-3pbbsu4s8jtehsor
Tags: 1.1.3-3
Fix POSIX compliance problem in init script.  Closes: #403384. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *  sql.c               rlm_sql - FreeRADIUS SQL Module
3
3
 *              Main code directly taken from ICRADIUS
4
4
 *
5
 
 * Version:     $Id: sql.c,v 1.79 2004/05/25 18:35:32 aland Exp $
 
5
 * Version:     $Id: sql.c,v 1.79.2.3 2005/08/26 00:37:47 aland Exp $
6
6
 *
7
7
 *   This program is free software; you can redistribute it and/or modify
8
8
 *   it under the terms of the GNU General Public License as published by
105
105
                radlog(L_DBG, "rlm_sql (%s): starting %d",
106
106
                       inst->config->xlat_name, i);
107
107
 
108
 
                sqlsocket = rad_malloc(sizeof(SQLSOCK));
 
108
                sqlsocket = rad_malloc(sizeof(*sqlsocket));
109
109
                if (sqlsocket == NULL) {
110
110
                        return -1;
111
111
                }
 
112
                memset(sqlsocket, 0, sizeof(*sqlsocket));
112
113
                sqlsocket->conn = NULL;
113
114
                sqlsocket->id = i;
114
115
                sqlsocket->state = sockunconnected;
326
327
 *************************************************************************/
327
328
int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row, int querymode)
328
329
{
329
 
        DICT_ATTR *attr;
330
330
        VALUE_PAIR *pair, *check;
331
 
        char *ptr;
332
 
        char buf[128];
333
 
        char value[256];
334
 
        LRAD_TOKEN xlat, pairmode = T_EOL;
 
331
        char *ptr, *value;
 
332
        char buf[MAX_STRING_LEN];
 
333
        char do_xlat = 0;
 
334
        LRAD_TOKEN token, operator = T_EOL;
335
335
 
336
 
        if ((attr = dict_attrbyname(row[2])) == (DICT_ATTR *) NULL) {
337
 
                radlog(L_ERR | L_CONS, "rlm_sql: unknown attribute %s",
338
 
                       row[2]);
339
 
                return (-1);
 
336
        /*
 
337
         *      Verify the 'Attribute' field
 
338
         */
 
339
        if (row[2] == NULL || row[2][0] == '\0') {
 
340
                radlog(L_ERR, "rlm_sql: The 'Attribute' field is empty or NULL, skipping the entire row.");
 
341
                return -1;
340
342
        }
341
343
 
342
 
        if (row[4] != NULL && strlen(row[4]) > 0) {
 
344
        /*
 
345
         *      Verify the 'op' field
 
346
         */
 
347
        if (row[4] != NULL && row[4][0] != '\0') {
343
348
                ptr = row[4];
344
 
                pairmode = gettoken(&ptr, buf, sizeof(buf));
345
 
        } else {
 
349
                operator = gettoken(&ptr, buf, sizeof(buf));
 
350
        }
 
351
        if (operator <= T_EOL) {
346
352
                /*
347
 
                 *  'op' fields of NULL are a plague, and a bane on the
348
 
                 *  existence of mankind.
 
353
                 *  Complain about empty or invalid 'op' field
349
354
                 */
 
355
                operator = T_OP_CMP_EQ;
350
356
                radlog(L_ERR, "rlm_sql: The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
351
357
                radlog(L_ERR, "rlm_sql: You MUST FIX THIS if you want the configuration to behave as you expect.");
352
358
        }
353
 
        if (pairmode <= T_EOL) pairmode = T_OP_CMP_EQ;
354
359
 
355
360
        /*
356
 
         * If attribute is already there, skip it because we checked usercheck first
357
 
         * and we want user settings to over ride group settings
 
361
         *      The 'Value' field may be empty or NULL
358
362
         */
359
 
        if (pairmode != T_OP_ADD && (check = pairfind(*first_pair, attr->attr)) != NULL &&
360
 
#ifdef ASCEND_BINARY
361
 
                        attr->type != PW_TYPE_ABINARY &&
362
 
#endif
363
 
                        querymode == PW_VP_GROUPDATA)
364
 
                return 0;
365
 
 
 
363
        value = row[3];
366
364
        /*
367
365
         *      If we have a new-style quoted string, where the
368
366
         *      *entire* string is quoted, do xlat's.
369
367
         */
370
 
        if (((row[3][0] == '\'') ||
371
 
             (row[3][0] == '`') ||
372
 
             (row[3][0] == '"')) &&
373
 
            (row[3][0] == row[3][strlen(row[3])-1])) {
 
368
        if (row[3] != NULL &&
 
369
           ((row[3][0] == '\'') || (row[3][0] == '`') || (row[3][0] == '"')) &&
 
370
           (row[3][0] == row[3][strlen(row[3])-1])) {
374
371
 
375
 
                ptr = row[3];
376
 
                xlat = gettoken(&ptr, value, sizeof(value));
377
 
                switch (xlat) {
 
372
                token = gettoken(&value, buf, sizeof(buf));
 
373
                switch (token) {
378
374
                        /*
379
 
                         *      Make the full pair now.
 
375
                         *      Take the unquoted string.
380
376
                         */
381
 
                default:
382
 
                        pair = pairmake(row[2], row[3], pairmode);
383
 
                        break;
384
 
 
385
377
                case T_SINGLE_QUOTED_STRING:
386
378
                case T_DOUBLE_QUOTED_STRING:
387
 
                        pair = pairmake(row[2], value, pairmode);
 
379
                        value = buf;
388
380
                        break;
389
381
 
390
382
                        /*
391
383
                         *      Mark the pair to be allocated later.
392
384
                         */
393
385
                case T_BACK_QUOTED_STRING:
394
 
                        pair = pairmake(row[2], NULL, pairmode);
395
 
                        if (pair) {
396
 
                                pair->flags.do_xlat = 1;
397
 
                                strNcpy(pair->strvalue, value, sizeof(pair->strvalue));
398
 
                                pair->length = 0;
399
 
                        }
 
386
                        value = NULL;
 
387
                        do_xlat = 1;
 
388
                        break;
 
389
 
 
390
                        /*
 
391
                         *      Keep the original string.
 
392
                         */
 
393
                default:
 
394
                        value = row[3];
 
395
                        break;
400
396
                }
401
 
        } else {
402
 
                /*
403
 
                 * String starts and ends differently. Take it literally
404
 
                 * */
405
 
                pair = pairmake(row[2], row[3], pairmode);
406
 
        }
 
397
        }
 
398
 
 
399
        /*
 
400
         *      Create the pair
 
401
         */
 
402
        pair = pairmake(row[2], value, operator);
 
403
        if (pair == NULL) {
 
404
                radlog(L_ERR, "rlm_sql: Failed to create the pair: %s", librad_errstr);
 
405
                return -1;
 
406
        }
 
407
        if (do_xlat) {
 
408
                pair->flags.do_xlat = 1;
 
409
                strNcpy(pair->strvalue, buf, sizeof(pair->strvalue));
 
410
                pair->length = 0;
 
411
        }
 
412
 
 
413
        /*
 
414
         *      If attribute is already there, skip it because we
 
415
         *      checked usercheck first and we want user settings to
 
416
         *      override group settings
 
417
         */
 
418
        if (operator != T_OP_ADD && (check = pairfind(*first_pair, pair->attribute)) != NULL &&
 
419
#ifdef ASCEND_BINARY
 
420
            pair->type != PW_TYPE_ABINARY &&
 
421
#endif
 
422
            querymode == PW_VP_GROUPDATA) {
 
423
                pairbasicfree(pair);
 
424
                return 0;
 
425
        }
 
426
 
 
427
        /*
 
428
         *      Add the pair into the packet
 
429
         */
407
430
        pairadd(first_pair, pair);
408
 
 
409
431
        return 0;
410
432
}
411
433