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_write(srv, __FILE__, __LINE__, "s", 80 "PGsetdbLogin() failed, exiting..."); 81 return -1; 82 } 83 84 if (CONNECTION_OK != PQstatus(dbconn)) { 85 log_error_write(srv, __FILE__, __LINE__, "s", 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 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_write(srv, __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 static void mod_vhostdb_free_config(plugin_data * const p) { 175 if (NULL == p->cvlist) return; 176 /* (init i to 0 if global context; to 1 to skip empty global context) */ 177 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 178 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 179 for (; -1 != cpv->k_id; ++cpv) { 180 if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 181 switch (cpv->k_id) { 182 case 0: /* vhostdb.<db> */ 183 mod_vhostdb_dbconf_free(cpv->v.v); 184 break; 185 default: 186 break; 187 } 188 } 189 } 190 } 191 192 FREE_FUNC(mod_vhostdb_cleanup) { 193 plugin_data *p = p_d; 194 if (!p) return HANDLER_GO_ON; 195 196 mod_vhostdb_free_config(p); 197 198 free(p->cvlist); 199 free(p); 200 201 UNUSED(srv); 202 return HANDLER_GO_ON; 203 } 204 205 static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { 206 switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 207 case 0: /* vhostdb.<db> */ 208 if (cpv->vtype == T_CONFIG_LOCAL) 209 pconf->vdata = cpv->v.v; 210 break; 211 default:/* should not happen */ 212 return; 213 } 214 } 215 216 static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { 217 do { 218 mod_vhostdb_merge_config_cpv(pconf, cpv); 219 } while ((++cpv)->k_id != -1); 220 } 221 222 static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p) { 223 memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); 224 for (int i = 1, used = p->nconfig; i < used; ++i) { 225 if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) 226 mod_vhostdb_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]); 227 } 228 } 229 230 SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { 231 static const config_plugin_keys_t cpk[] = { 232 { CONST_STR_LEN("vhostdb.pgsql"), 233 T_CONFIG_ARRAY, 234 T_CONFIG_SCOPE_CONNECTION } 235 ,{ NULL, 0, 236 T_CONFIG_UNSET, 237 T_CONFIG_SCOPE_UNSET } 238 }; 239 240 plugin_data * const p = p_d; 241 if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb_pgsql")) 242 return HANDLER_ERROR; 243 244 /* process and validate config directives 245 * (init i to 0 if global context; to 1 to skip empty global context) */ 246 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 247 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 248 for (; -1 != cpv->k_id; ++cpv) { 249 switch (cpv->k_id) { 250 case 0: /* vhostdb.<db> */ 251 if (cpv->v.a->used) { 252 if (!array_is_kvstring(cpv->v.a)) { 253 log_error(srv->errh, __FILE__, __LINE__, 254 "unexpected value for %s; " 255 "expected list of \"option\" => \"value\"", 256 cpk[cpv->k_id].k); 257 return HANDLER_ERROR; 258 } 259 if (0 != mod_vhostdb_dbconf_setup(srv, cpv->v.a, &cpv->v.v)) 260 return HANDLER_ERROR; 261 if (NULL != cpv->v.v) 262 cpv->vtype = T_CONFIG_LOCAL; 263 } 264 break; 265 default:/* should not happen */ 266 break; 267 } 268 } 269 } 270 271 /* initialize p->defaults from global config context */ 272 if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 273 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 274 if (-1 != cpv->k_id) 275 mod_vhostdb_merge_config(&p->defaults, cpv); 276 } 277 278 return HANDLER_GO_ON; 279 } 280 281 282 int mod_vhostdb_pgsql_plugin_init (plugin *p); 283 int mod_vhostdb_pgsql_plugin_init (plugin *p) 284 { 285 p->version = LIGHTTPD_VERSION_ID; 286 p->name = "vhostdb_pgsql"; 287 288 p->init = mod_vhostdb_init; 289 p->cleanup = mod_vhostdb_cleanup; 290 p->set_defaults = mod_vhostdb_set_defaults; 291 292 return 0; 293 } 294