Use C99 initialisations for iterators
[exim.git] / src / src / lookups / redis.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8 #include "../exim.h"
9
10 #ifdef LOOKUP_REDIS
11
12 #include "lf_functions.h"
13
14 #include <hiredis/hiredis.h>
15
16 #ifndef nele
17 # define nele(arr) (sizeof(arr) / sizeof(*arr))
18 #endif
19
20 /* Structure and anchor for caching connections. */
21 typedef struct redis_connection {
22   struct redis_connection *next;
23   uschar  *server;
24   redisContext    *handle;
25 } redis_connection;
26
27 static redis_connection *redis_connections = NULL;
28
29
30 static void *
31 redis_open(uschar *filename, uschar **errmsg)
32 {
33 return (void *)(1);
34 }
35
36
37 void
38 redis_tidy(void)
39 {
40 redis_connection *cn;
41
42 /* XXX: Not sure how often this is called!
43  Guess its called after every lookup which probably would mean to just
44  not use the _tidy() function at all and leave with exim exiting to
45  GC connections!  */
46
47 while ((cn = redis_connections))
48   {
49   redis_connections = cn->next;
50   DEBUG(D_lookup) debug_printf("close REDIS connection: %s\n", cn->server);
51   redisFree(cn->handle);
52   }
53 }
54
55
56 /* This function is called from the find entry point to do the search for a
57 single server.
58
59     Arguments:
60       query        the query string
61       server       the server string
62       resultptr    where to store the result
63       errmsg       where to point an error message
64       defer_break  TRUE if no more servers are to be tried after DEFER
65       do_cache     set false if data is changed
66
67     The server string is of the form "host/dbnumber/password". The host can be
68     host:port. This string is in a nextinlist temporary buffer, so can be
69     overwritten.
70
71     Returns:       OK, FAIL, or DEFER 
72 */
73
74 static int
75 perform_redis_search(const uschar *command, uschar *server, uschar **resultptr,
76   uschar **errmsg, BOOL *defer_break, uint *do_cache)
77 {
78 redisContext *redis_handle = NULL;        /* Keep compilers happy */
79 redisReply *redis_reply = NULL;
80 redisReply *entry = NULL;
81 redisReply *tentry = NULL;
82 redis_connection *cn;
83 int yield = DEFER;
84 int i, j;
85 gstring * result = NULL;
86 uschar *server_copy = NULL;
87 uschar *sdata[3];
88
89 /* Disaggregate the parameters from the server argument.
90 The order is host:port(socket)
91 We can write to the string, since it is in a nextinlist temporary buffer.
92 This copy is also used for debugging output.  */
93
94 memset(sdata, 0, sizeof(sdata)) /* Set all to NULL */;
95 for (int i = 2; i > 0; i--)
96   {
97   uschar *pp = Ustrrchr(server, '/');
98
99   if (!pp)
100     {
101     *errmsg = string_sprintf("incomplete Redis server data: %s",
102       i == 2 ? server : server_copy);
103     *defer_break = TRUE;
104     return DEFER;
105     }
106   *pp++ = 0;
107   sdata[i] = pp;
108   if (i == 2) server_copy = string_copy(server);  /* sans password */
109   }
110 sdata[0] = server;   /* What's left at the start */
111
112 /* If the database or password is an empty string, set it NULL */
113 if (sdata[1][0] == 0) sdata[1] = NULL;
114 if (sdata[2][0] == 0) sdata[2] = NULL;
115
116 /* See if we have a cached connection to the server */
117
118 for (cn = redis_connections; cn; cn = cn->next)
119   if (Ustrcmp(cn->server, server_copy) == 0)
120     {
121     redis_handle = cn->handle;
122     break;
123     }
124
125 if (!cn)
126   {
127   uschar *p;
128   uschar *socket = NULL;
129   int port = 0;
130   /* int redis_err = REDIS_OK; */
131
132   if ((p = Ustrchr(sdata[0], '(')))
133     {
134     *p++ = 0;
135     socket = p;
136     while (*p != 0 && *p != ')') p++;
137     *p = 0;
138     }
139
140   if ((p = Ustrchr(sdata[0], ':')))
141     {
142     *p++ = 0;
143     port = Uatoi(p);
144     }
145   else
146     port = Uatoi("6379");
147
148   if (Ustrchr(server, '/'))
149     {
150     *errmsg = string_sprintf("unexpected slash in Redis server hostname: %s",
151       sdata[0]);
152     *defer_break = TRUE;
153     return DEFER;
154     }
155
156   DEBUG(D_lookup)
157     debug_printf("REDIS new connection: host=%s port=%d socket=%s database=%s\n",
158       sdata[0], port, socket, sdata[1]);
159
160   /* Get store for a new handle, initialize it, and connect to the server */
161   /* XXX: Use timeouts ? */
162   redis_handle =
163     socket ? redisConnectUnix(CCS socket) : redisConnect(CCS server, port);
164   if (!redis_handle)
165     {
166     *errmsg = string_sprintf("REDIS connection failed");
167     *defer_break = FALSE;
168     goto REDIS_EXIT;
169     }
170
171   /* Add the connection to the cache */
172   cn = store_get(sizeof(redis_connection));
173   cn->server = server_copy;
174   cn->handle = redis_handle;
175   cn->next = redis_connections;
176   redis_connections = cn;
177   }
178 else
179   {
180   DEBUG(D_lookup)
181     debug_printf("REDIS using cached connection for %s\n", server_copy);
182 }
183
184 /* Authenticate if there is a password */
185 if(sdata[2])
186   if (!(redis_reply = redisCommand(redis_handle, "AUTH %s", sdata[2])))
187     {
188     *errmsg = string_sprintf("REDIS Authentication failed: %s\n", redis_handle->errstr);
189     *defer_break = FALSE;
190     goto REDIS_EXIT;
191     }
192
193 /* Select the database if there is a dbnumber passed */
194 if(sdata[1])
195   {
196   if (!(redis_reply = redisCommand(redis_handle, "SELECT %s", sdata[1])))
197     {
198     *errmsg = string_sprintf("REDIS: Selecting database=%s failed: %s\n", sdata[1], redis_handle->errstr);
199     *defer_break = FALSE;
200     goto REDIS_EXIT;
201     }
202   DEBUG(D_lookup) debug_printf("REDIS: Selecting database=%s\n", sdata[1]);
203   }
204
205 /* split string on whitespace into argv */
206   {
207   uschar * argv[32];
208   const uschar * s = command;
209   int siz, ptr, i;
210   uschar c;
211
212   while (isspace(*s)) s++;
213
214   for (i = 0; *s && i < nele(argv); i++)
215     {
216     gstring * g;
217
218     for (g = NULL; (c = *s) && !isspace(c); s++)
219       if (c != '\\' || *++s)            /* backslash protects next char */
220         g = string_catn(g, s, 1);
221     argv[i] = string_from_gstring(g);
222
223     DEBUG(D_lookup) debug_printf("REDIS: argv[%d] '%s'\n", i, argv[i]);
224     while (isspace(*s)) s++;
225     }
226
227   /* Run the command. We use the argv form rather than plain as that parses
228   into args by whitespace yet has no escaping mechanism. */
229
230   redis_reply = redisCommandArgv(redis_handle, i, (const char **) argv, NULL);
231   if (!redis_reply)
232     {
233     *errmsg = string_sprintf("REDIS: query failed: %s\n", redis_handle->errstr);
234     *defer_break = FALSE;
235     goto REDIS_EXIT;
236     }
237   }
238
239 switch (redis_reply->type)
240   {
241   case REDIS_REPLY_ERROR:
242     *errmsg = string_sprintf("REDIS: lookup result failed: %s\n", redis_reply->str);
243
244     /* trap MOVED cluster responses and follow them */
245     if (Ustrncmp(redis_reply->str, "MOVED", 5) == 0)
246       {
247       DEBUG(D_lookup)
248         debug_printf("REDIS: cluster redirect %s\n", redis_reply->str);
249       /* follow redirect
250       This is cheating, we simply set defer_break = FALSE to move on to
251       the next server in the redis_servers list */
252       *defer_break = FALSE;
253       return DEFER;
254       } else {
255       *defer_break = TRUE;
256       }
257     *do_cache = 0;
258     goto REDIS_EXIT;
259     /* NOTREACHED */
260
261   case REDIS_REPLY_NIL:
262     DEBUG(D_lookup)
263       debug_printf("REDIS: query was not one that returned any data\n");
264     result = string_catn(result, US"", 1);
265     *do_cache = 0;
266     goto REDIS_EXIT;
267     /* NOTREACHED */
268
269   case REDIS_REPLY_INTEGER:
270     result = string_cat(result, redis_reply->integer != 0 ? US"true" : US"false");
271     break;
272
273   case REDIS_REPLY_STRING:
274   case REDIS_REPLY_STATUS:
275     result = string_catn(result, US redis_reply->str, redis_reply->len);
276     break;
277
278   case REDIS_REPLY_ARRAY:
279  
280     /* NOTE: For now support 1 nested array result. If needed a limitless
281     result can be parsed */
282
283     for (int i = 0; i < redis_reply->elements; i++)
284       {
285       entry = redis_reply->element[i];
286
287       if (result)
288         result = string_catn(result, US"\n", 1);
289
290       switch (entry->type)
291         {
292         case REDIS_REPLY_INTEGER:
293           result = string_fmt_append(result, "%d", entry->integer);
294           break;
295         case REDIS_REPLY_STRING:
296           result = string_catn(result, US entry->str, entry->len);
297           break;
298         case REDIS_REPLY_ARRAY:
299           for (int j = 0; j < entry->elements; j++)
300             {
301             tentry = entry->element[j];
302
303             if (result)
304               result = string_catn(result, US"\n", 1);
305
306             switch (tentry->type)
307               {
308               case REDIS_REPLY_INTEGER:
309                 result = string_fmt_append(result, "%d", tentry->integer);
310                 break;
311               case REDIS_REPLY_STRING:
312                 result = string_catn(result, US tentry->str, tentry->len);
313                 break;
314               case REDIS_REPLY_ARRAY:
315                 DEBUG(D_lookup)
316                   debug_printf("REDIS: result has nesting of arrays which"
317                     " is not supported. Ignoring!\n");
318                 break;
319               default:
320                 DEBUG(D_lookup) debug_printf(
321                           "REDIS: result has unsupported type. Ignoring!\n");
322                 break;
323               }
324             }
325             break;
326           default:
327             DEBUG(D_lookup) debug_printf("REDIS: query returned unsupported type\n");
328             break;
329           }
330         }
331       break;
332   }
333
334
335 if (result)
336   store_reset(result->s + result->ptr + 1);
337 else
338   {
339   yield = FAIL;
340   *errmsg = US"REDIS: no data found";
341   }
342
343 REDIS_EXIT:
344
345 /* Free store for any result that was got; don't close the connection,
346 as it is cached. */
347
348 if (redis_reply) freeReplyObject(redis_reply);
349
350 /* Non-NULL result indicates a successful result */
351
352 if (result)
353   {
354   *resultptr = string_from_gstring(result);
355   return OK;
356   }
357 else
358   {
359   DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
360   /* NOTE: Required to close connection since it needs to be reopened */
361   return yield;      /* FAIL or DEFER */
362   }
363 }
364
365
366
367 /*************************************************
368 *               Find entry point                 *
369 *************************************************/
370 /*
371  * See local README for interface description. The handle and filename
372  * arguments are not used. The code to loop through a list of servers while the
373  * query is deferred with a retryable error is now in a separate function that is
374  * shared with other noSQL lookups.
375  */
376
377 static int
378 redis_find(void *handle __attribute__((unused)),
379   uschar *filename __attribute__((unused)),
380   const uschar *command, int length, uschar **result, uschar **errmsg,
381   uint *do_cache)
382 {
383 return lf_sqlperform(US"Redis", US"redis_servers", redis_servers, command,
384   result, errmsg, do_cache, perform_redis_search);
385 }
386
387
388
389 /*************************************************
390 *               Quote entry point                *
391 *************************************************/
392
393 /* Prefix any whitespace, or backslash, with a backslash.
394 This is not a Redis thing but instead to let the argv splitting
395 we do to split on whitespace, yet provide means for getting
396 whitespace into an argument.
397
398 Arguments:
399   s          the string to be quoted
400   opt        additional option text or NULL if none
401
402 Returns:     the processed string or NULL for a bad option
403 */
404
405 static uschar *
406 redis_quote(uschar *s, uschar *opt)
407 {
408 register int c;
409 int count = 0;
410 uschar *t = s;
411 uschar *quoted;
412
413 if (opt) return NULL;     /* No options recognized */
414
415 while ((c = *t++) != 0)
416   if (isspace(c) || c == '\\') count++;
417
418 if (count == 0) return s;
419 t = quoted = store_get(Ustrlen(s) + count + 1);
420
421 while ((c = *s++) != 0)
422   {
423   if (isspace(c) || c == '\\') *t++ = '\\';
424   *t++ = c;
425   }
426
427 *t = 0;
428 return quoted;
429 }
430
431
432 /*************************************************
433 *         Version reporting entry point          *
434 *************************************************/
435 #include "../version.h"
436
437 void
438 redis_version_report(FILE *f)
439 {
440 fprintf(f, "Library version: REDIS: Compile: %d [%d]\n",
441                HIREDIS_MAJOR, HIREDIS_MINOR);
442 #ifdef DYNLOOKUP
443 fprintf(f, "                        Exim version %s\n", EXIM_VERSION_STR);
444 #endif
445 }
446
447
448
449 /* These are the lookup_info blocks for this driver */
450 static lookup_info redis_lookup_info = {
451   US"redis",                     /* lookup name */
452   lookup_querystyle,             /* query-style lookup */
453   redis_open,                    /* open function */
454   NULL,                          /* no check function */
455   redis_find,                    /* find function */
456   NULL,                          /* no close function */
457   redis_tidy,                    /* tidy function */
458   redis_quote,                   /* quoting function */
459   redis_version_report           /* version reporting */
460 };
461
462 #ifdef DYNLOOKUP
463 # define redis_lookup_module_info _lookup_module_info
464 #endif /* DYNLOOKUP */
465
466 static lookup_info *_lookup_list[] = { &redis_lookup_info };
467 lookup_module_info redis_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
468
469 #endif /* LOOKUP_REDIS */
470 /* End of lookups/redis.c */