From 6c31471eb2a4c1cbf9ffdcb308f06106d38dc0f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zden=C4=9Bk=20=C5=A0ustr?= Date: Mon, 10 Sep 2012 08:45:46 +0000 Subject: [PATCH] Grid map filed loading algorithm updated to accept tokens in quotes. --- org.glite.lb.server/src/lb_authz.c | 68 +++++++++++++++++++++++--------------- org.glite.lb.server/src/lb_html.c | 8 ++--- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/org.glite.lb.server/src/lb_authz.c b/org.glite.lb.server/src/lb_authz.c index 42e8c17..b0d4ceb 100644 --- a/org.glite.lb.server/src/lb_authz.c +++ b/org.glite.lb.server/src/lb_authz.c @@ -1298,8 +1298,8 @@ parse_gridmap(edg_wll_Context ctx, { FILE *fd = NULL; char line[4096]; - char *p, *a, *b; - int ret; + char *p, *a; + int ret, index = 0, len, i, lineno = 0; fd = fopen(file, "r"); if (fd == NULL) @@ -1307,42 +1307,58 @@ parse_gridmap(edg_wll_Context ctx, /* XXX -1 */ while (fgets(line, sizeof(line), fd) != NULL) { - p = strchr(line, '\n'); - if (p) + lineno++; + if ((p = strchr(line, '\n'))) // Removes trailing \n *p = '\0'; - if(strlen(line) == strspn(line, " \t")) continue; - - p = line; - while(p && *p == ' ') - p++; - a = p; - - p = strchr(line, ' '); - if (!p) { - ret = edg_wll_SetError(ctx, EINVAL, "Wrong format of mapping file"); - goto end; - } - *p++ = '\0'; - - while(p && *p == ' ') - p++; - b = p; + index = strspn(line, " \t"); + if(strlen(line) == index) continue; // Skips empty lines + if(line[index]=='#') continue; // Skips commented-out lines mapping->rules = realloc(mapping->rules, (mapping->num+1) * sizeof(_edg_wll_mapping_rule)); if (mapping->rules == NULL) { ret = edg_wll_SetError(ctx, ENOMEM, "Not enough memory"); goto end; } - if (!(mapping->rules[mapping->num].a = strdup(a)) || - !(mapping->rules[mapping->num].b = strdup(b))) { - ret = edg_wll_SetError(ctx, ENOMEM, "Not enough memory"); - goto end; + + for (i = 0; i < 2; i++) { + + if (i) { + if((index + len) < strlen(line)) index = index + len + strspn(line + index + len, " \t"); // Move index to next + else { + char *errdesc; + asprintf(&errdesc, "Wrong format of mapping file, line %d", lineno); + ret = edg_wll_SetError(ctx, EINVAL, errdesc); + goto end; + } + } + + len = (line[index] == '"' || line[index] == '\'') ? // Get token length + strcspn(line + (++index), "\"'") : + strcspn(line + index, " "); + + if(!(a = calloc(sizeof(char*), len + 1))) { // Allocate and check + ret = edg_wll_SetError(ctx, ENOMEM, "Not enough memory"); + goto end; + } + + strncpy(a, line + index, len); // Copy token contents and assign + if (!i) mapping->rules[mapping->num].a = a; + else mapping->rules[mapping->num].b = a; + + if(!strlen(a)) { + char *errdesc; + asprintf(&errdesc, "Orphaned ID in mapping file, line %d", lineno); + ret = edg_wll_SetError(ctx, EINVAL, errdesc); + goto end; + } + + if(line[index+len] == '"' || line[index+len] == '\'') len++; } + mapping->num++; } - ret = 0; end: diff --git a/org.glite.lb.server/src/lb_html.c b/org.glite.lb.server/src/lb_html.c index d287fd0..bc90d20 100644 --- a/org.glite.lb.server/src/lb_html.c +++ b/org.glite.lb.server/src/lb_html.c @@ -221,13 +221,13 @@ int edg_wll_ConfigurationToHTML(edg_wll_Context ctx, int admin, char **message, a = NULL; for (i = 0; i < ctx->id_mapping.num; i++ ) { - asprintf(&out_tmp, "%s%s%s%s%s%s%s", + asprintf(&out_tmp, "%s%s\"%s\"%s\"%s\"%s%s", a ? a : "", - text ? "\"" : "", + text ? "" : "", ctx->id_mapping.rules[i].a, - text ? "\"=\"" : " ", + text ? "=" : " ", ctx->id_mapping.rules[i].b, - text ? "\"" : "", + text ? "" : "", i == ctx->id_mapping.num - 1 ? "" : (text ? "," : "
")); free(a); a=out_tmp; -- 1.8.2.3