xref: /lighttpd1.4/src/mod_vhostdb_pgsql.c (revision 010c2894)
1 #include "first.h"
2 
3 #include <libpq-fe.h>
4 
5 #include <string.h>
6 #include <stdlib.h>
7 
8 #include "base.h"
9 #include "http_vhostdb.h"
10 #include "log.h"
11 #include "plugin.h"
12 
13 /*
14  * virtual host plugin using Postgres for domain to directory lookups
15  */
16 
17 typedef struct {
18     PGconn *dbconn;
19     const buffer *sqlquery;
20 } vhostdb_config;
21 
22 typedef struct {
23     void *vdata;
24 } plugin_config;
25 
26 typedef struct {
27     PLUGIN_DATA;
28     plugin_config defaults;
29     plugin_config conf;
30 } plugin_data;
31 
32 static void mod_vhostdb_dbconf_free (void *vdata)
33 {
34     vhostdb_config *dbconf = (vhostdb_config *)vdata;
35     if (!dbconf) return;
36     PQfinish(dbconf->dbconn);
37     free(dbconf);
38 }
39 
40 static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdata)
41 {
42     const buffer *sqlquery = NULL;
43     const char *dbname=NULL, *user=NULL, *pass=NULL, *host=NULL, *port=NULL;
44 
45     for (size_t i = 0; i < opts->used; ++i) {
46         const data_string *ds = (data_string *)opts->data[i];
47         if (ds->type == TYPE_STRING) {
48             if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("sql"))) {
49                 sqlquery = &ds->value;
50             } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("dbname"))) {
51                 dbname = ds->value.ptr;
52             } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("user"))) {
53                 user = ds->value.ptr;
54             } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("password"))) {
55                 pass = ds->value.ptr;
56             } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("host"))) {
57                 host = ds->value.ptr;
58             } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("port"))) {
59                 port = ds->value.ptr;
60             }
61         }
62     }
63 
64     /* required:
65      * - sql    (sql query)
66      * - dbname
67      * - user   (unless dbname is a pgsql conninfo URI)
68      *
69      * optional:
70      * - password, default: empty
71      * - hostname
72      * - port, default: 5432
73      */
74 
75     if (!buffer_string_is_empty(sqlquery) && NULL != dbname) {
76         vhostdb_config *dbconf;
77         PGconn *dbconn = PQsetdbLogin(host,port,NULL,NULL,dbname,user,pass);
78         if (NULL == dbconn) {
79             log_error(srv->errh, __FILE__, __LINE__,
80               "PGsetdbLogin() failed, exiting...");
81             return -1;
82         }
83 
84         if (CONNECTION_OK != PQstatus(dbconn)) {
85             log_error(srv->errh, __FILE__, __LINE__,
86               "Failed to login to database, exiting...");
87             PQfinish(dbconn);
88             return -1;
89         }
90 
91         /* Postgres sets FD_CLOEXEC on database socket descriptors */
92 
93         dbconf = (vhostdb_config *)calloc(1, sizeof(*dbconf));
94         dbconf->dbconn = dbconn;
95         dbconf->sqlquery = sqlquery;
96         *vdata = dbconf;
97     }
98 
99     return 0;
100 }
101 
102 static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p);
103 
104 static int mod_vhostdb_pgsql_query(server *srv, connection *con, void *p_d, buffer *docroot)
105 {
106     UNUSED(srv);
107     plugin_data *p = (plugin_data *)p_d;
108     vhostdb_config *dbconf;
109     PGresult *res;
110     int cols, rows;
111 
112     /*(reuse buffer for sql query before generating docroot result)*/
113     buffer *sqlquery = docroot;
114     buffer_clear(sqlquery); /*(also resets docroot (alias))*/
115 
116     mod_vhostdb_patch_config(con, p);
117     if (NULL == p->conf.vdata) return 0; /*(after resetting docroot)*/
118     dbconf = (vhostdb_config *)p->conf.vdata;
119 
120     for (char *b = dbconf->sqlquery->ptr, *d; *b; b = d+1) {
121         if (NULL != (d = strchr(b, '?'))) {
122             /* escape the uri.authority */
123             size_t len;
124             int err;
125             buffer_append_string_len(sqlquery, b, (size_t)(d - b));
126             buffer_string_prepare_append(sqlquery, buffer_string_length(con->uri.authority) * 2);
127             len = PQescapeStringConn(dbconf->dbconn,
128                     sqlquery->ptr + buffer_string_length(sqlquery),
129                     CONST_BUF_LEN(con->uri.authority), &err);
130             buffer_commit(sqlquery, len);
131             if (0 != err) return -1;
132         } else {
133             d = dbconf->sqlquery->ptr + buffer_string_length(dbconf->sqlquery);
134             buffer_append_string_len(sqlquery, b, (size_t)(d - b));
135             break;
136         }
137     }
138 
139     res = PQexec(dbconf->dbconn, sqlquery->ptr);
140 
141     buffer_clear(docroot); /*(reset buffer to store result)*/
142 
143     if (PGRES_TUPLES_OK != PQresultStatus(res)) {
144         log_error(con->conf.errh, __FILE__, __LINE__, "%s",
145           PQerrorMessage(dbconf->dbconn));
146         PQclear(res);
147         return -1;
148     }
149 
150     cols = PQnfields(res);
151     rows = PQntuples(res);
152     if (rows == 1 && cols >= 1) {
153         buffer_copy_string(docroot, PQgetvalue(res, 0, 0));
154     } /* else no such virtual host */
155 
156     PQclear(res);
157     return 0;
158 }
159 
160 
161 
162 
163 INIT_FUNC(mod_vhostdb_init) {
164     static http_vhostdb_backend_t http_vhostdb_backend_pgsql =
165       { "pgsql", mod_vhostdb_pgsql_query, NULL };
166     plugin_data *p = calloc(1, sizeof(*p));
167 
168     /* register http_vhostdb_backend_pgsql */
169     http_vhostdb_backend_pgsql.p_d = p;
170     http_vhostdb_backend_set(&http_vhostdb_backend_pgsql);
171 
172     return p;
173 }
174 
175 FREE_FUNC(mod_vhostdb_cleanup) {
176     plugin_data * const p = p_d;
177     if (NULL == p->cvlist) return;
178     /* (init i to 0 if global context; to 1 to skip empty global context) */
179     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
180         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
181         for (; -1 != cpv->k_id; ++cpv) {
182             if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
183             switch (cpv->k_id) {
184               case 0: /* vhostdb.<db> */
185                 mod_vhostdb_dbconf_free(cpv->v.v);
186                 break;
187               default:
188                 break;
189             }
190         }
191     }
192 }
193 
194 static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
195     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
196       case 0: /* vhostdb.<db> */
197         if (cpv->vtype == T_CONFIG_LOCAL)
198             pconf->vdata = cpv->v.v;
199         break;
200       default:/* should not happen */
201         return;
202     }
203 }
204 
205 static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
206     do {
207         mod_vhostdb_merge_config_cpv(pconf, cpv);
208     } while ((++cpv)->k_id != -1);
209 }
210 
211 static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p) {
212     memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
213     for (int i = 1, used = p->nconfig; i < used; ++i) {
214         if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id))
215             mod_vhostdb_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]);
216     }
217 }
218 
219 SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) {
220     static const config_plugin_keys_t cpk[] = {
221       { CONST_STR_LEN("vhostdb.pgsql"),
222         T_CONFIG_ARRAY,
223         T_CONFIG_SCOPE_CONNECTION }
224      ,{ NULL, 0,
225         T_CONFIG_UNSET,
226         T_CONFIG_SCOPE_UNSET }
227     };
228 
229     plugin_data * const p = p_d;
230     if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb_pgsql"))
231         return HANDLER_ERROR;
232 
233     /* process and validate config directives
234      * (init i to 0 if global context; to 1 to skip empty global context) */
235     for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
236         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
237         for (; -1 != cpv->k_id; ++cpv) {
238             switch (cpv->k_id) {
239               case 0: /* vhostdb.<db> */
240                 if (cpv->v.a->used) {
241                     if (!array_is_kvstring(cpv->v.a)) {
242                         log_error(srv->errh, __FILE__, __LINE__,
243                           "unexpected value for %s; "
244                           "expected list of \"option\" => \"value\"",
245                           cpk[cpv->k_id].k);
246                         return HANDLER_ERROR;
247                     }
248                     if (0 != mod_vhostdb_dbconf_setup(srv, cpv->v.a, &cpv->v.v))
249                         return HANDLER_ERROR;
250                     if (NULL != cpv->v.v)
251                         cpv->vtype = T_CONFIG_LOCAL;
252                 }
253                 break;
254               default:/* should not happen */
255                 break;
256             }
257         }
258     }
259 
260     /* initialize p->defaults from global config context */
261     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
262         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
263         if (-1 != cpv->k_id)
264             mod_vhostdb_merge_config(&p->defaults, cpv);
265     }
266 
267     return HANDLER_GO_ON;
268 }
269 
270 
271 int mod_vhostdb_pgsql_plugin_init (plugin *p);
272 int mod_vhostdb_pgsql_plugin_init (plugin *p)
273 {
274     p->version          = LIGHTTPD_VERSION_ID;
275     p->name             = "vhostdb_pgsql";
276 
277     p->init             = mod_vhostdb_init;
278     p->cleanup          = mod_vhostdb_cleanup;
279     p->set_defaults     = mod_vhostdb_set_defaults;
280 
281     return 0;
282 }
283