remove duplicate triggers
[freeradius.git] / src / modules / rlm_sql / sql.c
1 /*
2  *  sql.c               rlm_sql - FreeRADIUS SQL Module
3  *              Main code directly taken from ICRADIUS
4  *
5  * Version:     $Id$
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * Copyright 2001,2006  The FreeRADIUS server project
22  * Copyright 2000  Mike Machado <mike@innercite.com>
23  * Copyright 2000  Alan DeKok <aland@ox.org>
24  * Copyright 2001  Chad Miller <cmiller@surfsouth.com>
25  */
26
27 RCSID("$Id$")
28
29 #include        <freeradius-devel/radiusd.h>
30 #include        <freeradius-devel/rad_assert.h>
31
32 #include        <sys/file.h>
33 #include        <sys/stat.h>
34
35 #include        <ctype.h>
36
37 #include        "rlm_sql.h"
38
39 #ifdef HAVE_PTHREAD_H
40 #endif
41
42 /*
43  *      Translate rlm_sql rcodes to humanly
44  *      readable reason strings.
45  */
46 const FR_NAME_NUMBER sql_rcode_table[] = {
47         { "success",            RLM_SQL_OK              },
48         { "need alt query",     RLM_SQL_ALT_QUERY       },
49         { "server error",       RLM_SQL_ERROR           },
50         { "query invalid",      RLM_SQL_QUERY_INVALID   },
51         { "no connection",      RLM_SQL_RECONNECT       },
52         { NULL, 0 }
53 };
54
55
56 void *mod_conn_create(TALLOC_CTX *ctx, void *instance)
57 {
58         int rcode;
59         rlm_sql_t *inst = instance;
60         rlm_sql_handle_t *handle;
61
62         /*
63          *      Connections cannot be alloced from the inst or
64          *      pool contexts due to threading issues.
65          */
66         handle = talloc_zero(ctx, rlm_sql_handle_t);
67         if (!handle) return NULL;
68
69         handle->log_ctx = talloc_pool(handle, 2048);
70         if (!handle->log_ctx) {
71                 talloc_free(handle);
72                 return NULL;
73         }
74
75         /*
76          *      Handle requires a pointer to the SQL inst so the
77          *      destructor has access to the module configuration.
78          */
79         handle->inst = inst;
80
81         rcode = (inst->module->sql_socket_init)(handle, inst->config);
82         if (rcode != 0) {
83         fail:
84                 /*
85                  *      Destroy any half opened connections.
86                  */
87                 talloc_free(handle);
88                 return NULL;
89         }
90
91         if (inst->config->connect_query) {
92                 if (rlm_sql_select_query(inst, NULL, &handle, inst->config->connect_query) != RLM_SQL_OK) goto fail;
93                 (inst->module->sql_finish_select_query)(handle, inst->config);
94         }
95
96         return handle;
97 }
98
99 /*************************************************************************
100  *
101  *      Function: sql_fr_pair_list_afrom_str
102  *
103  *      Purpose: Read entries from the database and fill VALUE_PAIR structures
104  *
105  *************************************************************************/
106 int sql_fr_pair_list_afrom_str(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **head, rlm_sql_row_t row)
107 {
108         VALUE_PAIR *vp;
109         char const *ptr, *value;
110         char buf[MAX_STRING_LEN];
111         char do_xlat = 0;
112         FR_TOKEN token, operator = T_EOL;
113
114         /*
115          *      Verify the 'Attribute' field
116          */
117         if (!row[2] || row[2][0] == '\0') {
118                 REDEBUG("The 'Attribute' field is empty or NULL, skipping the entire row");
119                 return -1;
120         }
121
122         /*
123          *      Verify the 'op' field
124          */
125         if (row[4] != NULL && row[4][0] != '\0') {
126                 ptr = row[4];
127                 operator = gettoken(&ptr, buf, sizeof(buf), false);
128                 if ((operator < T_OP_ADD) ||
129                     (operator > T_OP_CMP_EQ)) {
130                         REDEBUG("Invalid operator \"%s\" for attribute %s", row[4], row[2]);
131                         return -1;
132                 }
133
134         } else {
135                 /*
136                  *  Complain about empty or invalid 'op' field
137                  */
138                 operator = T_OP_CMP_EQ;
139                 REDEBUG("The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
140                 REDEBUG("You MUST FIX THIS if you want the configuration to behave as you expect");
141         }
142
143         /*
144          *      The 'Value' field may be empty or NULL
145          */
146         value = row[3];
147         /*
148          *      If we have a new-style quoted string, where the
149          *      *entire* string is quoted, do xlat's.
150          */
151         if (row[3] != NULL &&
152            ((row[3][0] == '\'') || (row[3][0] == '`') || (row[3][0] == '"')) &&
153            (row[3][0] == row[3][strlen(row[3])-1])) {
154
155                 token = gettoken(&value, buf, sizeof(buf), false);
156                 switch (token) {
157                 /*
158                  *      Take the unquoted string.
159                  */
160                 case T_SINGLE_QUOTED_STRING:
161                 case T_DOUBLE_QUOTED_STRING:
162                         value = buf;
163                         break;
164
165                 /*
166                  *      Mark the pair to be allocated later.
167                  */
168                 case T_BACK_QUOTED_STRING:
169                         value = NULL;
170                         do_xlat = 1;
171                         break;
172
173                 /*
174                  *      Keep the original string.
175                  */
176                 default:
177                         value = row[3];
178                         break;
179                 }
180         }
181
182         /*
183          *      Create the pair
184          */
185         vp = fr_pair_make(ctx, NULL, row[2], NULL, operator);
186         if (!vp) {
187                 REDEBUG("Failed to create the pair: %s", fr_strerror());
188                 return -1;
189         }
190
191         if (do_xlat) {
192                 if (fr_pair_mark_xlat(vp, value) < 0) {
193                         REDEBUG("Error marking pair for xlat");
194
195                         talloc_free(vp);
196                         return -1;
197                 }
198         } else {
199                 if (fr_pair_value_from_str(vp, value, -1) < 0) {
200                         REDEBUG("Error parsing value: %s", fr_strerror());
201
202                         talloc_free(vp);
203                         return -1;
204                 }
205         }
206
207         /*
208          *      Add the pair into the packet
209          */
210         fr_pair_add(head, vp);
211         return 0;
212 }
213
214 /** Call the driver's sql_fetch_row function
215  *
216  * Calls the driver's sql_fetch_row logging any errors. On success, will
217  * write row data to (*handle)->row.
218  *
219  * @param inst Instance of rlm_sql.
220  * @param request The Current request, may be NULL.
221  * @param handle Handle to retrieve errors for.
222  * @return on success RLM_SQL_OK, other sql_rcode_t constants on error.
223  */
224 sql_rcode_t rlm_sql_fetch_row(rlm_sql_t *inst, REQUEST *request, rlm_sql_handle_t **handle)
225 {
226         int ret;
227
228         if (!*handle || !(*handle)->conn) return RLM_SQL_ERROR;
229
230         /*
231          *      We can't implement reconnect logic here, because the caller
232          *      may require the original connection to free up queries or
233          *      result sets associated with that connection.
234          */
235         ret = (inst->module->sql_fetch_row)(*handle, inst->config);
236         if (ret < 0) {
237                 MOD_ROPTIONAL(RERROR, ERROR, "Error fetching row");
238
239                 rlm_sql_print_error(inst, request, *handle, false);
240         }
241
242         return ret;
243 }
244
245 /** Retrieve any errors from the SQL driver
246  *
247  * Retrieves errors from the driver from the last operation and writes them to
248  * to request/global log, in the ERROR, WARN, INFO and DEBUG categories.
249  *
250  * @param inst Instance of rlm_sql.
251  * @param request Current request, may be NULL.
252  * @param handle Handle to retrieve errors for.
253  * @param force_debug Force all errors to be logged as debug messages.
254  */
255 void rlm_sql_print_error(rlm_sql_t *inst, REQUEST *request, rlm_sql_handle_t *handle, bool force_debug)
256 {
257         char const      *driver;
258         sql_log_entry_t log[20];
259         size_t          num, i;
260
261         num = (inst->module->sql_error)(handle->log_ctx, log, (sizeof(log) / sizeof(*log)), handle, inst->config);
262         if (num == 0) {
263                 MOD_ROPTIONAL(RERROR, ERROR, "Unknown error");
264                 return;
265         }
266
267         driver = inst->config->sql_driver_name;
268
269         for (i = 0; i < num; i++) {
270                 if (force_debug) goto debug;
271
272                 switch (log[i].type) {
273                 case L_ERR:
274                         MOD_ROPTIONAL(RERROR, ERROR, "%s: %s", driver, log[i].msg);
275                         break;
276
277                 case L_WARN:
278                         MOD_ROPTIONAL(RWARN, WARN, "%s: %s", driver, log[i].msg);
279                         break;
280
281                 case L_INFO:
282                         MOD_ROPTIONAL(RINFO, INFO, "%s: %s", driver, log[i].msg);
283                         break;
284
285                 case L_DBG:
286                 default:
287                 debug:
288                         MOD_ROPTIONAL(RDEBUG, DEBUG, "%s: %s", driver, log[i].msg);
289                         break;
290                 }
291         }
292
293         talloc_free_children(handle->log_ctx);
294 }
295
296 /** Call the driver's sql_query method, reconnecting if necessary.
297  *
298  * @note Caller must call (inst->module->sql_finish_query)(handle, inst->config);
299  *      after they're done with the result.
300  *
301  * @param handle to query the database with. *handle should not be NULL, as this indicates
302  *      previous reconnection attempt has failed.
303  * @param request Current request.
304  * @param inst rlm_sql instance data.
305  * @param query to execute. Should not be zero length.
306  * @return RLM_SQL_OK on success, RLM_SQL_RECONNECT if a new handle is required
307  *      (also sets *handle = NULL), RLM_SQL_QUERY_INVALID/RLM_SQL_ERROR on invalid query or
308  *      connection error, RLM_SQL_ALT_QUERY on constraints violation.
309  */
310 sql_rcode_t rlm_sql_query(rlm_sql_t *inst, REQUEST *request, rlm_sql_handle_t **handle, char const *query)
311 {
312         int ret = RLM_SQL_ERROR;
313         int i, count;
314
315         /* Caller should check they have a valid handle */
316         rad_assert(*handle);
317
318         /* There's no query to run, return an error */
319         if (query[0] == '\0') {
320                 if (request) REDEBUG("Zero length query");
321                 return RLM_SQL_QUERY_INVALID;
322         }
323
324         /*
325          *  inst->pool may be NULL is this function is called by mod_conn_create.
326          */
327         count = inst->pool ? fr_connection_pool_get_num(inst->pool) : 0;
328
329         /*
330          *  Here we try with each of the existing connections, then try to create
331          *  a new connection, then give up.
332          */
333         for (i = 0; i < (count + 1); i++) {
334                 MOD_ROPTIONAL(RDEBUG2, DEBUG2, "Executing query: %s", query);
335
336                 ret = (inst->module->sql_query)(*handle, inst->config, query);
337                 switch (ret) {
338                 case RLM_SQL_OK:
339                         break;
340
341                 /*
342                  *      Run through all available sockets until we exhaust all existing
343                  *      sockets in the pool and fail to establish a *new* connection.
344                  */
345                 case RLM_SQL_RECONNECT:
346                         *handle = fr_connection_reconnect(inst->pool, *handle);
347                         /* Reconnection failed */
348                         if (!*handle) return RLM_SQL_RECONNECT;
349                         /* Reconnection succeeded, try again with the new handle */
350                         continue;
351
352                 /*
353                  *      These are bad and should make rlm_sql return invalid
354                  */
355                 case RLM_SQL_QUERY_INVALID:
356                         rlm_sql_print_error(inst, request, *handle, false);
357                         (inst->module->sql_finish_query)(*handle, inst->config);
358                         break;
359
360                 /*
361                  *      Server or client errors.
362                  *
363                  *      If the driver claims to be able to distinguish between
364                  *      duplicate row errors and other errors, and we hit a
365                  *      general error treat it as a failure.
366                  *
367                  *      Otherwise rewrite it to RLM_SQL_ALT_QUERY.
368                  */
369                 case RLM_SQL_ERROR:
370                         if (inst->module->flags & RLM_SQL_RCODE_FLAGS_ALT_QUERY) {
371                                 rlm_sql_print_error(inst, request, *handle, false);
372                                 (inst->module->sql_finish_query)(*handle, inst->config);
373                                 break;
374                         }
375                         ret = RLM_SQL_ALT_QUERY;
376                         /* FALL-THROUGH */
377
378                 /*
379                  *      Driver suggested using an alternative query
380                  */
381                 case RLM_SQL_ALT_QUERY:
382                         rlm_sql_print_error(inst, request, *handle, true);
383                         (inst->module->sql_finish_query)(*handle, inst->config);
384                         break;
385
386                 }
387
388                 return ret;
389         }
390
391         MOD_ROPTIONAL(RERROR, ERROR, "Hit reconnection limit");
392
393         return RLM_SQL_ERROR;
394 }
395
396 /** Call the driver's sql_select_query method, reconnecting if necessary.
397  *
398  * @note Caller must call (inst->module->sql_finish_select_query)(handle, inst->config);
399  *      after they're done with the result.
400  *
401  * @param inst rlm_sql instance data.
402  * @param request Current request.
403  * @param handle to query the database with. *handle should not be NULL, as this indicates
404  *        previous reconnection attempt has failed.
405  * @param query to execute. Should not be zero length.
406  * @return RLM_SQL_OK on success, RLM_SQL_RECONNECT if a new handle is required (also sets *handle = NULL),
407  *         RLM_SQL_QUERY_INVALID/RLM_SQL_ERROR on invalid query or connection error.
408  */
409 sql_rcode_t rlm_sql_select_query(rlm_sql_t *inst, REQUEST *request, rlm_sql_handle_t **handle,  char const *query)
410 {
411         int ret = RLM_SQL_ERROR;
412         int i, count;
413
414         /* Caller should check they have a valid handle */
415         rad_assert(*handle);
416
417         /* There's no query to run, return an error */
418         if (query[0] == '\0') {
419                 if (request) REDEBUG("Zero length query");
420
421                 return RLM_SQL_QUERY_INVALID;
422         }
423
424         /*
425          *  inst->pool may be NULL is this function is called by mod_conn_create.
426          */
427         count = inst->pool ? fr_connection_pool_get_num(inst->pool) : 0;
428
429         /*
430          *  For sanity, for when no connections are viable, and we can't make a new one
431          */
432         for (i = 0; i < (count + 1); i++) {
433                 MOD_ROPTIONAL(RDEBUG2, DEBUG2, "Executing select query: %s", query);
434
435                 ret = (inst->module->sql_select_query)(*handle, inst->config, query);
436                 switch (ret) {
437                 case RLM_SQL_OK:
438                         break;
439
440                 /*
441                  *      Run through all available sockets until we exhaust all existing
442                  *      sockets in the pool and fail to establish a *new* connection.
443                  */
444                 case RLM_SQL_RECONNECT:
445                         *handle = fr_connection_reconnect(inst->pool, *handle);
446                         /* Reconnection failed */
447                         if (!*handle) return RLM_SQL_RECONNECT;
448                         /* Reconnection succeeded, try again with the new handle */
449                         continue;
450
451                 case RLM_SQL_QUERY_INVALID:
452                 case RLM_SQL_ERROR:
453                 default:
454                         rlm_sql_print_error(inst, request, *handle, false);
455                         (inst->module->sql_finish_select_query)(*handle, inst->config);
456                         break;
457                 }
458
459                 return ret;
460         }
461
462         MOD_ROPTIONAL(RERROR, ERROR, "Hit reconnection limit");
463
464         return RLM_SQL_ERROR;
465 }
466
467
468 /*************************************************************************
469  *
470  *      Function: sql_getvpdata
471  *
472  *      Purpose: Get any group check or reply pairs
473  *
474  *************************************************************************/
475 int sql_getvpdata(TALLOC_CTX *ctx, rlm_sql_t *inst, REQUEST *request, rlm_sql_handle_t **handle,
476                   VALUE_PAIR **pair, char const *query)
477 {
478         rlm_sql_row_t   row;
479         int             rows = 0;
480         sql_rcode_t     rcode;
481
482         rad_assert(request);
483
484         rcode = rlm_sql_select_query(inst, request, handle, query);
485         if (rcode != RLM_SQL_OK) return -1; /* error handled by rlm_sql_select_query */
486
487         while (rlm_sql_fetch_row(inst, request, handle) == 0) {
488                 row = (*handle)->row;
489                 if (!row) break;
490                 if (sql_fr_pair_list_afrom_str(ctx, request, pair, row) != 0) {
491                         REDEBUG("Error parsing user data from database result");
492
493                         (inst->module->sql_finish_select_query)(*handle, inst->config);
494
495                         return -1;
496                 }
497                 rows++;
498         }
499         (inst->module->sql_finish_select_query)(*handle, inst->config);
500
501         return rows;
502 }
503
504 /*
505  *      Log the query to a file.
506  */
507 void rlm_sql_query_log(rlm_sql_t *inst, REQUEST *request,
508                        sql_acct_section_t *section, char const *query)
509 {
510         int fd;
511         char const *filename = NULL;
512         char *expanded = NULL;
513         size_t len;
514         bool failed = false;    /* Write the log message outside of the critical region */
515
516         filename = inst->config->logfile;
517         if (section && section->logfile) filename = section->logfile;
518
519         if (!filename || !*filename) {
520                 return;
521         }
522
523         if (radius_axlat(&expanded, request, filename, NULL, NULL) < 0) {
524                 return;
525         }
526
527         fd = exfile_open(inst->ef, filename, 0640, true);
528         if (fd < 0) {
529                 ERROR("rlm_sql (%s): Couldn't open logfile '%s': %s", inst->name,
530                       expanded, fr_syserror(errno));
531
532                 talloc_free(expanded);
533                 return;
534         }
535
536         len = strlen(query);
537         if ((write(fd, query, len) < 0) || (write(fd, ";\n", 2) < 0)) {
538                 failed = true;
539         }
540
541         if (failed) {
542                 ERROR("rlm_sql (%s): Failed writing to logfile '%s': %s", inst->name, expanded,
543                       fr_syserror(errno));
544         }
545
546         talloc_free(expanded);
547         exfile_close(inst->ef, fd);
548 }