xref: /lighttpd1.4/src/mod_vhostdb_pgsql.c (revision cc2134c8)
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(connection *con, void *p_d, buffer *docroot)
105 {
106     plugin_data *p = (plugin_data *)p_d;
107     vhostdb_config *dbconf;
108     PGresult *res;
109     int cols, rows;
110 
111     /*(reuse buffer for sql query before generating docroot result)*/
112     buffer *sqlquery = docroot;
113     buffer_clear(sqlquery); /*(also resets docroot (alias))*/
114 
115     mod_vhostdb_patch_config(con, p);
116     if (NULL == p->conf.vdata) return 0; /*(after resetting docroot)*/
117     dbconf = (vhostdb_config *)p->conf.vdata;
118 
119     for (char *b = dbconf->sqlquery->ptr, *d; *b; b = d+1) {
120         if (NULL != (d = strchr(b, '?'))) {
121             /* escape the uri.authority */
122             size_t len;
123             int err;
124             buffer_append_string_len(sqlquery, b, (size_t)(d - b));
125             buffer_string_prepare_append(sqlquery, buffer_string_length(con->uri.authority) * 2);
126             len = PQescapeStringConn(dbconf->dbconn,
127                     sqlquery->ptr + buffer_string_length(sqlquery),
128                     CONST_BUF_LEN(con->uri.authority), &err);
129             buffer_commit(sqlquery, len);
130             if (0 != err) return -1;
131         } else {
132             d = dbconf->sqlquery->ptr + buffer_string_length(dbconf->sqlquery);
133             buffer_append_string_len(sqlquery, b, (size_t)(d - b));
134             break;
135         }
136     }
137 
138     res = PQexec(dbconf->dbconn, sqlquery->ptr);
139 
140     buffer_clear(docroot); /*(reset buffer to store result)*/
141 
142     if (PGRES_TUPLES_OK != PQresultStatus(res)) {
143         log_error(con->conf.errh, __FILE__, __LINE__, "%s",
144           PQerrorMessage(dbconf->dbconn));
145         PQclear(res);
146         return -1;
147     }
148 
149     cols = PQnfields(res);
150     rows = PQntuples(res);
151     if (rows == 1 && cols >= 1) {
152         buffer_copy_string(docroot, PQgetvalue(res, 0, 0));
153     } /* else no such virtual host */
154 
155     PQclear(res);
156     return 0;
157 }
158 
159 
160 
161 
162 INIT_FUNC(mod_vhostdb_init) {
163     static http_vhostdb_backend_t http_vhostdb_backend_pgsql =
164       { "pgsql", mod_vhostdb_pgsql_query, NULL };
165     plugin_data *p = calloc(1, sizeof(*p));
166 
167     /* register http_vhostdb_backend_pgsql */
168     http_vhostdb_backend_pgsql.p_d = p;
169     http_vhostdb_backend_set(&http_vhostdb_backend_pgsql);
170 
171     return p;
172 }
173 
174 FREE_FUNC(mod_vhostdb_cleanup) {
175     plugin_data * const p = p_d;
176     if (NULL == p->cvlist) return;
177     /* (init i to 0 if global context; to 1 to skip empty global context) */
178     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
179         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
180         for (; -1 != cpv->k_id; ++cpv) {
181             if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
182             switch (cpv->k_id) {
183               case 0: /* vhostdb.<db> */
184                 mod_vhostdb_dbconf_free(cpv->v.v);
185                 break;
186               default:
187                 break;
188             }
189         }
190     }
191 }
192 
193 static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
194     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
195       case 0: /* vhostdb.<db> */
196         if (cpv->vtype == T_CONFIG_LOCAL)
197             pconf->vdata = cpv->v.v;
198         break;
199       default:/* should not happen */
200         return;
201     }
202 }
203 
204 static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
205     do {
206         mod_vhostdb_merge_config_cpv(pconf, cpv);
207     } while ((++cpv)->k_id != -1);
208 }
209 
210 static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p) {
211     p->conf = p->defaults; /* copy small struct instead of memcpy() */
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_KVSTRING,
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 (0 != mod_vhostdb_dbconf_setup(srv, cpv->v.a, &cpv->v.v))
242                         return HANDLER_ERROR;
243                     if (NULL != cpv->v.v)
244                         cpv->vtype = T_CONFIG_LOCAL;
245                 }
246                 break;
247               default:/* should not happen */
248                 break;
249             }
250         }
251     }
252 
253     /* initialize p->defaults from global config context */
254     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
255         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
256         if (-1 != cpv->k_id)
257             mod_vhostdb_merge_config(&p->defaults, cpv);
258     }
259 
260     return HANDLER_GO_ON;
261 }
262 
263 
264 int mod_vhostdb_pgsql_plugin_init (plugin *p);
265 int mod_vhostdb_pgsql_plugin_init (plugin *p)
266 {
267     p->version          = LIGHTTPD_VERSION_ID;
268     p->name             = "vhostdb_pgsql";
269 
270     p->init             = mod_vhostdb_init;
271     p->cleanup          = mod_vhostdb_cleanup;
272     p->set_defaults     = mod_vhostdb_set_defaults;
273 
274     return 0;
275 }
276