| 1 |
#include "common.h" |
| 2 |
#include "configuration.h" |
| 3 |
#include "database.h" |
| 4 |
#include "http.h" |
| 5 |
#include "rfc822.h" |
| 6 |
#include "template.h" |
| 7 |
#include "question.h" |
| 8 |
#include "strutl.h" |
| 9 |
|
| 10 |
#include <dirent.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
#include <unistd.h> |
| 14 |
#include <sys/types.h> |
| 15 |
#include <sys/stat.h> |
| 16 |
#include <errno.h> |
| 17 |
|
| 18 |
static void parse_variables(struct question *q, char *string) |
| 19 |
{ |
| 20 |
char *wc, *owc; |
| 21 |
if (!string) |
| 22 |
return; |
| 23 |
owc = wc = strdup(string); |
| 24 |
|
| 25 |
while (wc != NULL && *wc != '\0') |
| 26 |
{ |
| 27 |
char *delim = wc; |
| 28 |
char *striptmp_var, *striptmp_val; |
| 29 |
int finished = 0; |
| 30 |
while (*delim != '=' && *delim != '\0') |
| 31 |
delim++; |
| 32 |
if (*delim == '\0') |
| 33 |
finished = 1; |
| 34 |
*delim = '\0'; |
| 35 |
|
| 36 |
striptmp_var = strdup(strstrip(wc)); |
| 37 |
|
| 38 |
delim++; |
| 39 |
wc = delim; |
| 40 |
while (*delim != '\n' && *delim != '\0') |
| 41 |
delim++; |
| 42 |
if (*delim == '\0') |
| 43 |
finished = 1; |
| 44 |
*delim = '\0'; |
| 45 |
|
| 46 |
striptmp_val = strdup(strstrip(wc)); |
| 47 |
question_variable_add(q,striptmp_var, striptmp_val); |
| 48 |
|
| 49 |
free(striptmp_val); |
| 50 |
free(striptmp_var); |
| 51 |
|
| 52 |
wc = delim; |
| 53 |
wc++; |
| 54 |
if (finished != 0) |
| 55 |
break; |
| 56 |
|
| 57 |
while (*wc == ' ' || *wc == '\t') |
| 58 |
{ |
| 59 |
wc++; |
| 60 |
} |
| 61 |
|
| 62 |
} |
| 63 |
free(owc); |
| 64 |
} |
| 65 |
|
| 66 |
static void parse_owners(struct question *q, char *string) |
| 67 |
{ |
| 68 |
char *wc, *owc; |
| 69 |
|
| 70 |
if (!string) |
| 71 |
return; |
| 72 |
|
| 73 |
owc = wc = strdup(string); |
| 74 |
|
| 75 |
while (wc != NULL) |
| 76 |
{ |
| 77 |
char *delim = wc; |
| 78 |
int finished = 0; |
| 79 |
while (*delim != ',' && *delim != '\0') |
| 80 |
delim++; |
| 81 |
if (*delim == '\0') |
| 82 |
finished = 1; |
| 83 |
*delim = '\0'; |
| 84 |
question_owner_add(q,wc); |
| 85 |
if (finished != 0) |
| 86 |
break; |
| 87 |
wc = delim; |
| 88 |
while (*wc == ' ' || *wc == '\t' || *wc == '\0') |
| 89 |
{ |
| 90 |
wc++; |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
free(owc); |
| 96 |
} |
| 97 |
|
| 98 |
static void parse_flags(struct question *q, char *string) |
| 99 |
{ |
| 100 |
char *wc, *owc; |
| 101 |
|
| 102 |
if (!string) |
| 103 |
return; |
| 104 |
|
| 105 |
owc = wc = strdup(string); |
| 106 |
|
| 107 |
while (wc != NULL) |
| 108 |
{ |
| 109 |
char *delim = wc; |
| 110 |
int finished = 0; |
| 111 |
while (*delim != ',' && *delim != '\0') |
| 112 |
delim++; |
| 113 |
if (*delim == '\0') |
| 114 |
finished = 1; |
| 115 |
*delim = '\0'; |
| 116 |
question_set_flag(q, wc); |
| 117 |
if (finished != 0) |
| 118 |
break; |
| 119 |
wc = delim + 1; |
| 120 |
while (*wc == ' ' || *wc == '\t') |
| 121 |
{ |
| 122 |
wc++; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
free(owc); |
| 127 |
} |
| 128 |
|
| 129 |
static char *translate_tag_name(const char *buf) |
| 130 |
{ |
| 131 |
char *ret = STRDUP(buf); |
| 132 |
/* remove / from the tag name so that we can use it as a filename */ |
| 133 |
char *t = ret; |
| 134 |
for (; *t != 0; t++) |
| 135 |
if (*t == '/') *t = ':'; /* : is illegal in templates etc */ |
| 136 |
return ret; |
| 137 |
} |
| 138 |
|
| 139 |
static char *template_url(struct template_db *db, const char *tag) |
| 140 |
{ |
| 141 |
static char filename[1024]; |
| 142 |
static char tmp[1024]; |
| 143 |
char tagname[1024]; |
| 144 |
filename[0] = 0; |
| 145 |
|
| 146 |
strncpy(tagname, tag, sizeof(tagname)); |
| 147 |
|
| 148 |
snprintf(tmp, sizeof(tmp), "%s::baseurl", db->configpath); |
| 149 |
|
| 150 |
snprintf(filename, sizeof(filename), "%s/%s", |
| 151 |
db->config->get(db->config, tmp, ""), tagname); |
| 152 |
|
| 153 |
return filename; |
| 154 |
} |
| 155 |
|
| 156 |
static char *question_url(struct question_db *db, const char *tag) |
| 157 |
{ |
| 158 |
static char filename[1024]; |
| 159 |
static char tmp[1024]; |
| 160 |
char tagname[1024]; |
| 161 |
|
| 162 |
filename[0] = 0; |
| 163 |
|
| 164 |
strncpy(tagname, tag, sizeof(tagname)); |
| 165 |
|
| 166 |
snprintf(tmp, sizeof(tmp), "%s::baseurl", db->configpath); |
| 167 |
|
| 168 |
snprintf(filename, sizeof(filename), "%s/%s", |
| 169 |
db->config->get(db->config, tmp, "."), tagname); |
| 170 |
|
| 171 |
return filename; |
| 172 |
} |
| 173 |
|
| 174 |
static struct template *http_lookup_cached_template( |
| 175 |
const struct template_db *db, const char *tag) |
| 176 |
{ |
| 177 |
struct template_db_cache *dbdata = db->data; |
| 178 |
struct template *result; |
| 179 |
for (result = dbdata->templates; result; result = result->next) |
| 180 |
{ |
| 181 |
if (strcmp(result->tag, tag) == 0) break; |
| 182 |
} |
| 183 |
return result; |
| 184 |
} |
| 185 |
|
| 186 |
static void http_remove_cached_template(struct template_db *db, |
| 187 |
const char *tag) |
| 188 |
{ |
| 189 |
struct template_db_cache *dbdata = db->data; |
| 190 |
struct template **result; |
| 191 |
for (result = &dbdata->templates; *result; result = &(*result)->next) |
| 192 |
{ |
| 193 |
if (strcmp((*result)->tag, tag) == 0) |
| 194 |
{ |
| 195 |
template_deref(*result); |
| 196 |
*result = (*result)->next; |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
/*static struct question *http_lookup_cached_question( |
| 204 |
const struct database *db, const char *tag) |
| 205 |
{ |
| 206 |
struct question_db_cache *dbdata = db->data; |
| 207 |
struct question *result; |
| 208 |
for (result = dbdata->questions; result; result = result->next) |
| 209 |
{ |
| 210 |
if (strcmp(result->tag, tag) == 0) break; |
| 211 |
} |
| 212 |
return result; |
| 213 |
}*/ |
| 214 |
|
| 215 |
|
| 216 |
static int http_template_initialize(struct template_db *db, struct configuration *cfg) |
| 217 |
{ |
| 218 |
struct template_db_cache *dbdata; |
| 219 |
dbdata = malloc(sizeof(struct template_db_cache)); |
| 220 |
|
| 221 |
if (dbdata == NULL) |
| 222 |
return DC_NOTOK; |
| 223 |
|
| 224 |
dbdata->templates = NULL; |
| 225 |
db->data = dbdata; |
| 226 |
|
| 227 |
return DC_OK; |
| 228 |
} |
| 229 |
|
| 230 |
static int http_template_shutdown(struct template_db *db) |
| 231 |
{ |
| 232 |
struct template_db_cache *dbdata = db->data; |
| 233 |
struct template *t; |
| 234 |
|
| 235 |
while (dbdata->templates != NULL) |
| 236 |
{ |
| 237 |
t = dbdata->templates; |
| 238 |
dbdata->templates = dbdata->templates->next; |
| 239 |
t->next = NULL; |
| 240 |
template_deref(t); |
| 241 |
} |
| 242 |
|
| 243 |
return DC_OK; |
| 244 |
} |
| 245 |
|
| 246 |
static int http_template_set(struct template_db *db, struct template *t) |
| 247 |
{ |
| 248 |
return DC_NOTIMPL; |
| 249 |
} |
| 250 |
|
| 251 |
static struct template *http_template_get_real(struct template_db *db, |
| 252 |
const char *ltag) |
| 253 |
{ |
| 254 |
char buf[1024]; |
| 255 |
struct template *t; |
| 256 |
char *translated_tag = translate_tag_name(ltag); |
| 257 |
char *url; |
| 258 |
|
| 259 |
if (ltag == NULL) return DC_NOTOK; |
| 260 |
url = template_url(db, ltag); |
| 261 |
snprintf(buf, sizeof(buf), "wget '%s' -O '/tmp/cdebconf_t_%s'", |
| 262 |
url, translated_tag); |
| 263 |
|
| 264 |
system(buf); |
| 265 |
snprintf(buf, sizeof(buf), "/tmp/cdebconf_t_%s", translated_tag); |
| 266 |
t = template_load(buf); |
| 267 |
free(translated_tag); |
| 268 |
|
| 269 |
return t; |
| 270 |
} |
| 271 |
|
| 272 |
static struct template *http_template_get(struct template_db *db, |
| 273 |
const char *ltag) |
| 274 |
{ |
| 275 |
struct template *result; |
| 276 |
|
| 277 |
result = http_lookup_cached_template(db, ltag); |
| 278 |
if (!result && (result = http_template_get_real(db, ltag))) |
| 279 |
{ |
| 280 |
struct template_db_cache *dbdata = db->data; |
| 281 |
result->next = dbdata->templates; |
| 282 |
dbdata->templates = result; |
| 283 |
} |
| 284 |
if (result) |
| 285 |
template_ref(result); |
| 286 |
|
| 287 |
return result; |
| 288 |
} |
| 289 |
|
| 290 |
static int http_template_remove(struct template_db *db, const char *tag) |
| 291 |
{ |
| 292 |
if (tag == NULL) return DC_NOTOK; |
| 293 |
|
| 294 |
http_remove_cached_template(db, tag); |
| 295 |
return DC_NOTIMPL; |
| 296 |
} |
| 297 |
|
| 298 |
static struct template *http_template_iterate(struct template_db *db, |
| 299 |
void **iter) |
| 300 |
{ |
| 301 |
return NULL; |
| 302 |
} |
| 303 |
|
| 304 |
static int http_question_initialize(struct question_db *db, struct configuration *cfg) |
| 305 |
{ |
| 306 |
struct question_db_cache *dbdata; |
| 307 |
dbdata = malloc(sizeof(struct question_db_cache)); |
| 308 |
|
| 309 |
if (dbdata == NULL) |
| 310 |
return DC_NOTOK; |
| 311 |
|
| 312 |
dbdata->questions = NULL; |
| 313 |
db->data = dbdata; |
| 314 |
|
| 315 |
return DC_OK; |
| 316 |
} |
| 317 |
|
| 318 |
static int http_question_shutdown(struct question_db *db) |
| 319 |
{ |
| 320 |
struct question_db_cache *dbdata = db->data; |
| 321 |
struct question *q; |
| 322 |
|
| 323 |
while (dbdata->questions != NULL) |
| 324 |
{ |
| 325 |
q = dbdata->questions; |
| 326 |
dbdata->questions = dbdata->questions->next; |
| 327 |
q->next = q->prev = NULL; |
| 328 |
question_deref(q); |
| 329 |
} |
| 330 |
|
| 331 |
return DC_OK; |
| 332 |
} |
| 333 |
|
| 334 |
static int http_question_set(struct question_db *db, struct question *q) |
| 335 |
{ |
| 336 |
return DC_NOTIMPL; |
| 337 |
} |
| 338 |
|
| 339 |
static struct question *http_question_get(struct question_db *db, |
| 340 |
const char *ltag) |
| 341 |
{ |
| 342 |
struct question *q; |
| 343 |
char *path; |
| 344 |
char *translated_tag = translate_tag_name(ltag); |
| 345 |
char *url; |
| 346 |
char *buf; |
| 347 |
FILE *inf; |
| 348 |
struct rfc822_header *header = NULL; |
| 349 |
|
| 350 |
if (ltag == NULL) return DC_NOTOK; |
| 351 |
url = question_url(db, ltag); |
| 352 |
|
| 353 |
asprintf(&buf, "wget '%s' -O '/tmp/cdebconf_q_%s'", |
| 354 |
url, translated_tag); |
| 355 |
|
| 356 |
system(buf); |
| 357 |
free(buf); |
| 358 |
INFO(INFO_DEBUG, "%s: url = [%s]", __FILE__, url); |
| 359 |
asprintf(&path, "/tmp/cdebconf_q_%s", translated_tag); |
| 360 |
|
| 361 |
if (path == NULL || |
| 362 |
(inf = fopen(path, "r")) == NULL) |
| 363 |
{ |
| 364 |
if (errno == ENOENT) |
| 365 |
return DC_NOTOK; |
| 366 |
|
| 367 |
INFO(INFO_ERROR, "Cannot get question %s: %s", |
| 368 |
ltag, strerror(errno)); |
| 369 |
free(path); |
| 370 |
return DC_NOTOK; |
| 371 |
} |
| 372 |
|
| 373 |
while ((header = rfc822_parse_stanza(inf)) != NULL) |
| 374 |
{ |
| 375 |
struct question *tmp; |
| 376 |
const char *name; |
| 377 |
|
| 378 |
name = rfc822_header_lookup(header, "name"); |
| 379 |
|
| 380 |
if (name == NULL || *name == 0) |
| 381 |
{ |
| 382 |
INFO(INFO_ERROR, "Read a stanza without a name"); |
| 383 |
rfc822_header_destroy(header); |
| 384 |
continue; |
| 385 |
} |
| 386 |
|
| 387 |
tmp = question_new(name); |
| 388 |
|
| 389 |
question_setvalue(tmp, rfc822_header_lookup(header, "value")); |
| 390 |
parse_flags(tmp, rfc822_header_lookup(header,"flags")); |
| 391 |
parse_owners(tmp, rfc822_header_lookup(header, "owners")); |
| 392 |
parse_variables(tmp, rfc822_header_lookup(header, "variables")); |
| 393 |
tmp->template = db->tdb->methods.get(db->tdb, rfc822_header_lookup(header, "template")); |
| 394 |
if (tmp->template == NULL) { |
| 395 |
tmp->template = template_new(name); |
| 396 |
db->tdb->methods.set(db->tdb, tmp->template); |
| 397 |
} |
| 398 |
rfc822_header_destroy(header); |
| 399 |
} |
| 400 |
|
| 401 |
fclose(inf); |
| 402 |
free(path); |
| 403 |
return q; |
| 404 |
} |
| 405 |
|
| 406 |
static int http_question_disown(struct question_db *db, const char *tag, |
| 407 |
const char *owner) |
| 408 |
{ |
| 409 |
return DC_NOTIMPL; |
| 410 |
} |
| 411 |
|
| 412 |
static struct question *http_question_iterate(struct question_db *db, |
| 413 |
void **iter) |
| 414 |
{ |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
|
| 418 |
struct template_db_module debconf_template_db_module = { |
| 419 |
initialize: http_template_initialize, |
| 420 |
shutdown: http_template_shutdown, |
| 421 |
set: http_template_set, |
| 422 |
get: http_template_get, |
| 423 |
remove: http_template_remove, |
| 424 |
iterate: http_template_iterate, |
| 425 |
}; |
| 426 |
|
| 427 |
struct question_db_module debconf_question_db_module = { |
| 428 |
initialize: http_question_initialize, |
| 429 |
shutdown: http_question_shutdown, |
| 430 |
set: http_question_set, |
| 431 |
get: http_question_get, |
| 432 |
disown: http_question_disown, |
| 433 |
iterate: http_question_iterate, |
| 434 |
}; |
| 435 |
|
| 436 |
/* |
| 437 |
Local Variables: |
| 438 |
c-basic-offset: 8 |
| 439 |
tab-width: 8 |
| 440 |
indent-tabs-mode: t |
| 441 |
End: |
| 442 |
*/ |