Exemple #1
0
/* Returns a copy of the string s, but with each instance of
   string pat replaced with string rep. The caller is responsible
   for freeing the dynamically allocated memory for the returned
   c string. 

   Input:  C string - The string to be modified
           C string - The string containing the pattern
           to be replaced
           C string - The string containing the pattern
           to replace with

   Output: C string - A copy of s with all instances of pat
           replaced with rep */
char *replace(char *s, char *pat, char *rep)
{
	if(s == NULL)
		return NULL;
	int ls = strlen(s);
	int lpat = strlen(pat);
	int lrep = strlen(rep);
	int o = count_occurences(s, pat);
	int lns = ls - (o*lpat) + (o*lrep) + 1;
	char *ns;
	if((ns = calloc(lns, sizeof(char))) == NULL)
	{
		return NULL;
	}

	char *ps = s;
	char *pns = ns;
	char *ppat = pat;
	char *prep = rep;
	int i;

	while(*ps != '\0')
	{
		int match = 1;
		for(i = 0; i < lpat && match == 1; i++)
		{
			if(*ps == '\0' || *ps != *ppat)
				match = 0;
			else
				ppat++;
			ps++;
		}
		if(match == 1)
		{
			prep = rep;
			for(i = 0; i < lrep; i++)
				*pns++ = *prep++;
		}
		else
		{
			ps -= i;
			*pns++ = *ps;
			ps += i;
			ps -= i - 1;
		}
		ppat = pat;
	}
	*pns = '\0';

	return ns;
}
Exemple #2
0
/*
 * Create one URLTranslation. Return NULL for failure, pointer to it for OK.
 */
static URLTranslation *create_onetrans(CfgGroup *grp)
{
    URLTranslation *ot;
    Octstr *url, *post_url, *post_xml, *text, *file, *exec;
    Octstr *accepted_smsc, *accepted_account, *forced_smsc, *default_smsc;
    Octstr *grpname;
    int is_sms_service, regex_flag = REG_EXTENDED;
    Octstr *accepted_smsc_regex;
    Octstr *accepted_account_regex;
    Octstr *allowed_prefix_regex;
    Octstr *denied_prefix_regex;
    Octstr *allowed_receiver_prefix_regex;
    Octstr *denied_receiver_prefix_regex;
    Octstr *white_list_regex;
    Octstr *black_list_regex;
    Octstr *keyword_regex;
    Octstr *os, *tmp;
    
    grpname = cfg_get_group_name(grp);
    if (grpname == NULL)
    	return NULL;

    if (octstr_str_compare(grpname, "sms-service") == 0)
        is_sms_service = 1;
    else if (octstr_str_compare(grpname, "sendsms-user") == 0)
        is_sms_service = 0;
    else {
        octstr_destroy(grpname);
        return NULL;
    }
    octstr_destroy(grpname);

    ot = gw_malloc(sizeof(URLTranslation));
    memset(ot, 0, sizeof(*ot));
    
    if (is_sms_service) {
        cfg_get_bool(&ot->catch_all, grp, octstr_imm("catch-all"));

        ot->dlr_url = cfg_get(grp, octstr_imm("dlr-url"));
        if (cfg_get_integer(&ot->dlr_mask, grp, octstr_imm("dlr-mask")) == -1)
            ot->dlr_mask = DLR_UNDEFINED;
        ot->alt_charset = cfg_get(grp, octstr_imm("alt-charset"));
	    
        url = cfg_get(grp, octstr_imm("get-url"));
        if (url == NULL)
            url = cfg_get(grp, octstr_imm("url"));
	    
	post_url = cfg_get(grp, octstr_imm("post-url"));
	post_xml = cfg_get(grp, octstr_imm("post-xml"));
	file = cfg_get(grp, octstr_imm("file"));
	text = cfg_get(grp, octstr_imm("text"));
	exec = cfg_get(grp, octstr_imm("exec"));
	if (url != NULL) {
	    ot->type = TRANSTYPE_GET_URL;
	    ot->pattern = octstr_duplicate(url);
	} else if (post_url != NULL) {
	    ot->type = TRANSTYPE_POST_URL;
	    ot->pattern = octstr_duplicate(post_url);
	    ot->catch_all = 1;
	} else if (post_xml != NULL) {
	    ot->type = TRANSTYPE_POST_XML;
	    ot->pattern = octstr_duplicate(post_xml);
	    ot->catch_all = 1;
	} else if (file != NULL) {
	    ot->type = TRANSTYPE_FILE;
	    ot->pattern = octstr_duplicate(file);
	} else if (text != NULL) {
	    ot->type = TRANSTYPE_TEXT;
	    ot->pattern = octstr_duplicate(text);
	} else if (exec != NULL) {
	    ot->type = TRANSTYPE_EXECUTE;
	    ot->pattern = octstr_duplicate(exec);
	} else {
	    octstr_destroy(url);
	    octstr_destroy(post_url);
	    octstr_destroy(post_xml);
	    octstr_destroy(file);
	    octstr_destroy(text);
	    octstr_destroy(exec);
	    error(0, "Configuration group `sms-service' "
	    	     "did not specify get-url, post-url, post-xml, file or text.");
    	    goto error;
	}
	octstr_destroy(url);
	octstr_destroy(post_url);
	octstr_destroy(post_xml);
	octstr_destroy(file);
	octstr_destroy(text);
	octstr_destroy(exec);

	tmp = cfg_get(grp, octstr_imm("keyword"));
        keyword_regex = cfg_get(grp, octstr_imm("keyword-regex"));
	if (tmp == NULL && keyword_regex == NULL) {
	    error(0, "Group 'sms-service' must include either 'keyword' or 'keyword-regex'.");
	    goto error;
	}
	if (tmp != NULL && keyword_regex != NULL) {
	    error(0, "Group 'sms-service' may inlcude either 'keyword' or 'keyword-regex'.");
	    octstr_destroy(tmp);
	    octstr_destroy(keyword_regex);
	    goto error;
	}
	
	if (tmp != NULL && octstr_str_compare(tmp, "default") == 0) {
	    /* default sms-service */
	    ot->keyword_regex = NULL;
	    octstr_destroy(tmp);
	} else if (tmp != NULL) {
	    Octstr *aliases;
	    
	    /* convert to regex */
	    regex_flag |= REG_ICASE;
	    keyword_regex = octstr_format("^[ ]*(%S", tmp);
	    octstr_destroy(tmp);

	    aliases = cfg_get(grp, octstr_imm("aliases"));
	    if (aliases != NULL) {
	        long i;
	        List *l;

	        l = octstr_split(aliases, octstr_imm(";"));
	        octstr_destroy(aliases);
	        
	        for (i = 0; i < gwlist_len(l); ++i) {
	            os = gwlist_get(l, i);
	            octstr_format_append(keyword_regex, "|%S", os);
	        }
	        gwlist_destroy(l, octstr_destroy_item);
	    }
	    
	    octstr_append_cstr(keyword_regex, ")[ ]*");
	}

        if (keyword_regex != NULL && (ot->keyword_regex = gw_regex_comp(keyword_regex, regex_flag)) == NULL) {
            error(0, "Could not compile pattern '%s'", octstr_get_cstr(keyword_regex));
            octstr_destroy(keyword_regex);
            goto error;
        }

	ot->name = cfg_get(grp, octstr_imm("name"));
	if (ot->name == NULL)
	    ot->name = keyword_regex ? octstr_duplicate(keyword_regex) : octstr_create("default");
	octstr_destroy(keyword_regex);

	accepted_smsc = cfg_get(grp, octstr_imm("accepted-smsc"));
	if (accepted_smsc != NULL) {
	    ot->accepted_smsc = octstr_split(accepted_smsc, octstr_imm(";"));
	    octstr_destroy(accepted_smsc);
	}
	accepted_account = cfg_get(grp, octstr_imm("accepted-account"));
	if (accepted_account != NULL) {
	    ot->accepted_account = octstr_split(accepted_account, octstr_imm(";"));
	    octstr_destroy(accepted_account);
	}
        accepted_smsc_regex = cfg_get(grp, octstr_imm("accepted-smsc-regex"));
        if (accepted_smsc_regex != NULL) { 
            if ( (ot->accepted_smsc_regex = gw_regex_comp(accepted_smsc_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(accepted_smsc_regex));
            octstr_destroy(accepted_smsc_regex);
        }
        accepted_account_regex = cfg_get(grp, octstr_imm("accepted-account-regex"));
        if (accepted_account_regex != NULL) { 
            if ( (ot->accepted_account_regex = gw_regex_comp(accepted_account_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(accepted_account_regex));
            octstr_destroy(accepted_account_regex);
        }

	cfg_get_bool(&ot->assume_plain_text, grp, 
		     octstr_imm("assume-plain-text"));
	cfg_get_bool(&ot->accept_x_kannel_headers, grp, 
		     octstr_imm("accept-x-kannel-headers"));
	cfg_get_bool(&ot->strip_keyword, grp, octstr_imm("strip-keyword"));
	cfg_get_bool(&ot->send_sender, grp, octstr_imm("send-sender"));
	
	ot->prefix = cfg_get(grp, octstr_imm("prefix"));
	ot->suffix = cfg_get(grp, octstr_imm("suffix"));
        ot->allowed_recv_prefix = cfg_get(grp, octstr_imm("allowed-receiver-prefix"));
        allowed_receiver_prefix_regex = cfg_get(grp, octstr_imm("allowed-receiver-prefix-regex"));
        if (allowed_receiver_prefix_regex != NULL) {
            if ((ot->allowed_receiver_prefix_regex = gw_regex_comp(allowed_receiver_prefix_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(allowed_receiver_prefix_regex));
            octstr_destroy(allowed_receiver_prefix_regex);
        }

	ot->allowed_recv_prefix = cfg_get(grp, octstr_imm("allowed-receiver-prefix"));
	ot->denied_recv_prefix = cfg_get(grp, octstr_imm("denied-receiver-prefix"));
        denied_receiver_prefix_regex = cfg_get(grp, octstr_imm("denied-receiver-prefix-regex"));
        if (denied_receiver_prefix_regex != NULL) {
            if ((ot->denied_receiver_prefix_regex = gw_regex_comp(denied_receiver_prefix_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'",octstr_get_cstr(denied_receiver_prefix_regex));
            octstr_destroy(denied_receiver_prefix_regex);
        }

	ot->args = count_occurences(ot->pattern, octstr_imm("%s"));
	ot->args += count_occurences(ot->pattern, octstr_imm("%S"));
	ot->has_catchall_arg = 
	    (count_occurences(ot->pattern, octstr_imm("%r")) > 0) ||
	    (count_occurences(ot->pattern, octstr_imm("%a")) > 0);

    } else {
	ot->type = TRANSTYPE_SENDSMS;
	ot->pattern = octstr_create("");
	ot->args = 0;
	ot->has_catchall_arg = 0;
	ot->catch_all = 1;
	ot->username = cfg_get(grp, octstr_imm("username"));
	ot->password = cfg_get(grp, octstr_imm("password"));
	ot->dlr_url = cfg_get(grp, octstr_imm("dlr-url"));
	grp_dump(grp);
	if (ot->password == NULL) {
	    error(0, "Password required for send-sms user");
	    goto error;
	}
	ot->name = cfg_get(grp, octstr_imm("name"));
	if (ot->name == NULL)
	    ot->name = octstr_duplicate(ot->username);

	forced_smsc = cfg_get(grp, octstr_imm("forced-smsc"));
	default_smsc = cfg_get(grp, octstr_imm("default-smsc"));
	if (forced_smsc != NULL) {
	    if (default_smsc != NULL) {
		info(0, "Redundant default-smsc for send-sms user %s", 
		     octstr_get_cstr(ot->username));
	    }
	    ot->forced_smsc = forced_smsc;
	    octstr_destroy(default_smsc);
	} else  if (default_smsc != NULL)
	    ot->default_smsc = default_smsc;

	ot->deny_ip = cfg_get(grp, octstr_imm("user-deny-ip"));
	ot->allow_ip = cfg_get(grp, octstr_imm("user-allow-ip"));
	ot->default_sender = cfg_get(grp, octstr_imm("default-sender"));
    }
    
    ot->allowed_prefix = cfg_get(grp, octstr_imm("allowed-prefix"));
    allowed_prefix_regex = cfg_get(grp, octstr_imm("allowed-prefix-regex"));
    if (allowed_prefix_regex != NULL) {
        if ((ot->allowed_prefix_regex = gw_regex_comp(allowed_prefix_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(allowed_prefix_regex));
        octstr_destroy(allowed_prefix_regex);
    }
    ot->denied_prefix = cfg_get(grp, octstr_imm("denied-prefix"));
    denied_prefix_regex = cfg_get(grp, octstr_imm("denied-prefix-regex"));
    if (denied_prefix_regex != NULL) {
        if ((ot->denied_prefix_regex = gw_regex_comp(denied_prefix_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(denied_prefix_regex));
        octstr_destroy(denied_prefix_regex);
    }
    
    os = cfg_get(grp, octstr_imm("white-list"));
    if (os != NULL) {
        ot->white_list = numhash_create(octstr_get_cstr(os));
        octstr_destroy(os);
    }
    white_list_regex = cfg_get(grp, octstr_imm("white-list-regex"));
    if (white_list_regex != NULL) {
        if ((ot->white_list_regex = gw_regex_comp(white_list_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(white_list_regex));
        octstr_destroy(white_list_regex);
    }

    os = cfg_get(grp, octstr_imm("black-list"));
    if (os != NULL) {
        ot->black_list = numhash_create(octstr_get_cstr(os));
        octstr_destroy(os);
    }
    black_list_regex = cfg_get(grp, octstr_imm("black-list-regex"));
    if (black_list_regex != NULL) {
        if ((ot->black_list_regex = gw_regex_comp(black_list_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(black_list_regex));
        octstr_destroy(black_list_regex);
    }

    if (cfg_get_integer(&ot->max_messages, grp, octstr_imm("max-messages")) == -1)
	ot->max_messages = 1;
    cfg_get_bool(&ot->concatenation, grp, octstr_imm("concatenation"));
    cfg_get_bool(&ot->omit_empty, grp, octstr_imm("omit-empty"));
    
    ot->header = cfg_get(grp, octstr_imm("header"));
    ot->footer = cfg_get(grp, octstr_imm("footer"));
    ot->faked_sender = cfg_get(grp, octstr_imm("faked-sender"));
    ot->split_chars = cfg_get(grp, octstr_imm("split-chars"));
    ot->split_suffix = cfg_get(grp, octstr_imm("split-suffix"));

    if ( (ot->prefix == NULL && ot->suffix != NULL) ||
	 (ot->prefix != NULL && ot->suffix == NULL) ) {
	warning(0, "Service : suffix and prefix are only used"
		   " if both are set.");
    }
    if ((ot->prefix != NULL || ot->suffix != NULL) &&
        ot->type != TRANSTYPE_GET_URL) {
	warning(0, "Service : suffix and prefix are only used"
                   " if type is 'get-url'.");
    }
    
    return ot;

error:
    error(0, "Couldn't create a URLTranslation.");
    destroy_onetrans(ot);
    return NULL;
}
Exemple #3
0
char *get_namelist(
    char *s,
    long n,
    FILE *fp
    )
{
    register char *flag, *ptr, *ptr1;
    register long l;

    while ((flag=CNL_fgetsSkipComments(s, n, fp, '!'))) {
        if ((ptr=strchr(s, '&'))) {
            if (!is_quoted(s, ptr, '"'))
                break;
            }
#if DOLLARSIGN_TOO
        if (ptr=strchr(s, '$')) {
            if (!is_quoted(s, ptr, '"'))
                break;
            }
#endif
        }
#ifdef DEBUG
        printf("get_namelist: s = <%s>\n", s);
#endif

    if (flag==NULL)
        return(NULL);

#if DOLLARSIGN_TOO
    if ((count_occurences(s, '$', "")+count_occurences(s, '&', ""))>=2)
        return(s);
#else
    if (count_occurences(s, '&', "")>=2)
        return(s);
#endif

    s[strlen(s)-1] = ' ';
/*    chop_nl(s); */
    ptr = s;
    do {
        l = strlen(ptr);
        ptr += l;
        if ((n-=l)<=1) {
            puts("error: namelist text buffer too small");
            abort();
            }
        do {
            if (!CNL_fgetsSkipComments(ptr, n, fp, '!'))
                return(s);
            if (ptr[0]!='!')
                break;
            } while (1);
        *(ptr+strlen(ptr)-1) = ' ';
/*        chop_nl(ptr); */
        if ((ptr1=strrchr(ptr, '&'))) {
            if (!is_quoted(s, ptr1, '"'))
                break;
            }
#if DOLLARSIGN_TOO
        if (ptr1=strrchr(ptr, '$')) {
            if (!is_quoted(s, ptr1, '"'))
                break;
            }
#endif
        } while (1) ;
    
    return(s);
    }          
Exemple #4
0
long scan_namelist(NAMELIST_TEXT *nl, char *line)
{
    register char *ptr, *ptr_e;
    char *ptr_p, *ptr_to_free ;
    long n_entities, i, j, length_values, length;
    char *values;
    static char *buffer = NULL;
    static long bufsize = 0;
#define BIGBUFSIZE 16384
#define INDEX_LIMIT 16380
#if !defined(vxWorks)
    long k, m, n, len, insideCommand;
    char psBuffer[16384], tempptr[16384], command[16384];
    FILE *fID;
#ifdef CONDOR_COMPILE
    char tmpName[1024];
#endif
#endif

    values = NULL;
    length_values = 0;
    if (!buffer)
        buffer = tmalloc(sizeof(*buffer)*(bufsize = 128));

    nl->n_entities = 0;
    nl->group_name = NULL;
    nl->entity   = NULL;
    nl->value    = NULL;
    nl->n_values = nl->n_subscripts = NULL;
    nl->subscript = nl->repeat = NULL;

#ifdef DEBUG
    printf("scan_namelist: line = <%s>\n", line);
#endif
    cp_str(&ptr_to_free, line);
    ptr = ptr_to_free;

    /* Find group name--it's a token starting with '$' or '&'. */
#if DOLLARSIGN_TOO
    if (!(ptr_p=next_unquoted_char(ptr, '$', '"')) &&
        !(ptr_p=next_unquoted_char(ptr, '&', '"')) ) {
        free(ptr_to_free);
        fprintf(stderr, "error: namelist scanning problem---missing group name:\n%s\n", ptr);
        return(-1);
        }
#else
    if (!(ptr_p=next_unquoted_char(ptr, '&', '"'))) {
        free(ptr_to_free);
        fprintf(stderr, "error: namelist scanning problem---missing group name:\n%s\n", ptr);
        return(-1);
        }
#endif
    ptr_p++;
    if ((nl->group_name=get_token(ptr_p))==NULL) {
        free(ptr_to_free);
        fprintf(stderr, "error: namelist scanning problem---missing group name:\n%s\n", ptr);
        return(-1);
        }
#ifdef DEBUG
    printf("scan_namelist: nl->group_name = <%s>\n", nl->group_name);
#endif

    /* Count entities--each entity is associated with an equals sign,
     * since these are all assignments. */
#if DOLLARSIGN_TOO
    n_entities = count_occurences(ptr_p, '=', "$&");
#else
    n_entities = count_occurences(ptr_p, '=', "&");
#endif
    if (n_entities==0) {
        free(ptr_to_free);
        return(nl->n_entities=0);
        }
#ifdef DEBUG
    printf("scan_namelist: n_entities = %ld\n", n_entities);
#endif

    /* allocate memory for parallel arrays representing the namelist:
     *   nl->entity is an array of entity names
     *   nl->value is an array of arrays of values assigned
     *   nl->repeat is an array of arrays of repeat factors applied to values
     *   nl->n_values is an array of numbers of values
     *   nl->n_subscripts is an array of numbers of subscripts
     *   nl->subscript is an array of arrays of subscripts
     */
    nl->entity   = tmalloc(sizeof(*(nl->entity  ))*n_entities);
    nl->value    = tmalloc(sizeof(*(nl->value   ))*n_entities);
    nl->repeat   = tmalloc(sizeof(*(nl->repeat  ))*n_entities);
    nl->n_values = tmalloc(sizeof(*(nl->n_values))*n_entities);
    nl->n_subscripts = tmalloc(sizeof(*(nl->n_subscripts))*n_entities);
    nl->subscript    = tmalloc(sizeof(*(nl->subscript))*n_entities);

#ifdef DEBUG
    printf("scan_namelist: allocation successful\n");
#endif

    /* scan individual items--look for entity names and successively extract
     * strings containing assignments for single entities.  For each string,
     * extract a list of values and repeat factors.
     */
    for (i=0; i<n_entities; i++) {
        /* Find next entity name, which terminates with '='.  ptr_p
         * points to the section of the string being parsed.  I.e., everything
         * before ptr_p has been parsed already. */
        if (!(ptr_e = next_unquoted_char(ptr_p, '=', '"'))) {
            fprintf(stderr, "scan_namelist: no '=' in string >%s<\n", ptr_p);
            exit(1);
            }

        ptr = ptr_e;
        while (isspace(*(ptr-1)))
            ptr--;
        *ptr = 0;                /* end entity name string at '=' or space */

        while (isspace(*ptr_p))    /* advance to start of name      */
            ptr_p++;
        cp_str(nl->entity+i, ptr_p);   /* copy the entity name */
        nl->n_subscripts[i] = extract_subscripts(nl->entity[i], nl->subscript+i);

#ifdef DEBUG
        printf("scan_namelist: nl->entity[%ld] = <%s>\n", i, nl->entity[i]);
        printf("remainder: <%s>\n", ptr_e+1);
#endif

        /* Set ptr to point to start of values list, which follows the '='
         * that was just found and deleted. */
        ptr = ptr_e + 1;

        /* Find next entity name, if there is one, and insert a 0 before it
         * to mark end of current values list. */
        if ((ptr_e = next_unquoted_char(ptr, '=', '"'))) {
            /* set ptr_e to point to end of values list for current entity,
             * which is at the comma or space preceeding the next entity name
             */
#ifdef DEBUG
            printf("scan_namelist: next list is <%s>\n", ptr_e);
#endif
            /* Skip whitespace and get to the entity name. */
            ptr_e--;
            while (isspace(*ptr_e) && ptr_e!=ptr)
                ptr_e--;
            /* Skip over the entity name. */
            while (*ptr_e==')' || *ptr_e=='}') {
                ptr_e--;
                while (*ptr_e!='(' && *ptr_e!='{' && ptr_e!=ptr)
                    ptr_e--;
                }
            while (!(isspace(*ptr_e) || *ptr_e==',') && ptr_e!=ptr)
                ptr_e--;
            ptr_p = ptr_e+1;      /* Save start of next entity. */
            /* Skip trailing whitespace and commas in the values list. */
            while ((isspace(*ptr_e) || *ptr_e==',') && ptr_e!=ptr)
                ptr_e--;
            /* If there's nothing left, there was no value given. */
            if (++ptr_e==ptr)
                bomb("missing value in namelist", NULL);
            /* Mark end of values with a NUL, and set ptr_p to the point
             * where processing begins for the next entity. */
            *ptr_e = 0;
            }
        else {
            /* There is no following entity, so there should be a '$end'
             * '&end', or just '$' or '&'.   */
            /* set ptr_e to $end of string, check for error */
#ifdef DEBUG
            printf("scan_namelist: last item is <%s>\n", ptr);
#endif
#if DOLLARSIGN_TOO
            if (!(ptr_e=next_unquoted_char(ptr, '&', '"')) && !(ptr_e=next_unquoted_char(ptr, '$', '"')) ) {
                fprintf(stderr, "error: namelist improperly terminated\n");
                return(-1);
                }
#else
            if (!(ptr_e=next_unquoted_char(ptr, '&', '"'))) {
                fprintf(stderr, "error: namelist improperly terminated\n");
                return(-1);
                }
#endif
            *ptr_e = 0;           /* Delete the symbol. */

            /* If the number of entities doesn't match what was counted before,
             * something is really goofy. */
            if (i!=n_entities-1) {
#ifdef DEBUG
                printf("scan_namelist: fatal error: n_entites=%ld, i=%ld\n", n_entities,
                      i);
#endif
                return(-1);
                }

            /* Scan backwards to the first token. Make sure there are some
             * values listed. */
            ptr_e--;
            while ((isspace(*ptr_e) || *ptr_e==',') && ptr_e!=ptr)
                ptr_e--;
            if (++ptr_e==ptr) {
#ifdef DEBUG
            printf("scan_namelist: fatal error: ptr_e = ptr\n");
            printf("scan_namelist: ptr = <%s>\n", ptr);
#endif
                fprintf(stderr, "error: no values listed for namelist field %s\n",
                        nl->entity[i]);
                return(-1);     /* No values listed. */
                }
            *ptr_e = 0;
            ptr_p = ptr_e;
            }
        /* ptr now points to the beginning of the values list, which is
         * NUL terminated.  ptr_p points to the beginning of the rest of
         * what remains to be parsed. */
#ifdef DEBUG
        printf("scan_namelist: values list = <%s>\n", ptr);
        printf("scan_namelist: remainder = <%s>\n", ptr_p);
#endif
#if !defined(vxWorks)
	length = strlen(ptr);
	if (length == 0) {
            fprintf(stderr, "error: missing values for namelist field %s\n",
                    nl->entity[i]);
            return -1;
	}
	n = 0;
	j = 0;
	k = 0;
	insideCommand = 0;
	while (n<length) {
	  if (j>INDEX_LIMIT) {
	      fprintf(stderr, "error: values for namelist field %s is too long\n",
		      nl->entity[i]);
	      return -1;
	  }
	  if (ptr[n]=='\\') {
            if (ptr[n+1]!='}' && ptr[n+1]!='{')
              tempptr[j++] = ptr[n++];
            else
              n++;
	    if (n<length) {
	      if (insideCommand) {
		command[k] = ptr[n];
		k++;
		n++;		
	      } else {
		tempptr[j] = ptr[n];
		j++;
		n++;
	      }
	    }
	  } else if (ptr[n]=='{') {
	    if (insideCommand) {
	      fprintf(stderr, "error: values for namelist field %s has invalid command brackets\n",
		      nl->entity[i]);
	      return -1;
	    }
	    insideCommand = 1;
	    k = 0;
	    n++;
	  } else if (ptr[n]=='}') {
	    if (!insideCommand) {
	      fprintf(stderr, "error: values for namelist field %s has invalid command brackets\n",
		      nl->entity[i]);
	      return -1;
	    }
	    insideCommand = 0;
	    command[k] = 0;
	    n++;
#ifndef CONDOR_COMPILE
	    if ((fID = popen(command, "r")) == NULL) {
	      fprintf(stderr, "error: invalid command for namelist field %s\n",
		      nl->entity[i]);
              fprintf(stderr, "command was %s\n", command);
	      return -1;
	    }
	    if (feof(fID)) {
	      fprintf(stderr, "error: command for namelist field %s returns EOF\n",
		      nl->entity[i]);
              fprintf(stderr, "command was %s\n", command);
	      return -1;
	    }
	    if (fgets(psBuffer, 128, fID) == NULL) {
	      fprintf(stderr, "error: command for namelist field %s returns NULL\n",
		      nl->entity[i]);
              fprintf(stderr, "command was %s\n", command);
	      return -1;
	    }
            pclose(fID);
#else
            tmpnam(tmpName);
            sprintf(psBuffer, "%s > %s", command, tmpName);
            system(psBuffer);
            if (!(fID = fopen(tmpName, "r"))) {
	      fprintf(stderr, "error: command for namelist field %s failed\n",
		      nl->entity[i]);
              fprintf(stderr, "command was %s\n", command);
	      return -1;
            }
            if (fgets(psBuffer, 128, fID) == NULL) {
	      fprintf(stderr, "error: command for namelist field %s returns NULL\n",
		      nl->entity[i]);
              fprintf(stderr, "command was %s\n", command);
	      return -1;
            }
            fclose(fID);
            remove(tmpName);
 #endif
	    len = strlen(psBuffer)-1;
	    if ((len > 0) && (psBuffer[len] == '\n')) {
	      psBuffer[len] = 0;
	    }
	    m = 0;
	    len = strlen(psBuffer);
	    if (j+len>INDEX_LIMIT) {
	      fprintf(stderr, "error: values for namelist field %s is too long\n",
		      nl->entity[i]);
	      return -1;
	    }
	    while (m < len) {
	      tempptr[j] = psBuffer[m];
	      j++;
	      m++;
	    } 
	  } else {
	    if (insideCommand) {
	      command[k] = ptr[n];
	      n++;
	      k++;
	    } else {
	      tempptr[j] = ptr[n];
	      n++;
	      j++;
	    }
	  }
	}
	tempptr[j] = 0;
	
	ptr = tempptr;
#endif
        /* Process string of values.  Values must be separated by commas. */
        length = strlen(ptr)+1;
        if (length==1) {
            fprintf(stderr, "error: missing values for namelist field %s\n",
                    nl->entity[i]);
            return -1;
            }
        if (values==NULL) {
            values = tmalloc(length*sizeof(*values));
            length_values = length;
            }
        else if (length>length_values) {
            tfree(values);
            values = tmalloc(length*sizeof(*values));
            }
        strcpy_ss(values, ptr);
        nl->n_values[i] = count_occurences(values, ',', "")+1;
        nl->value[i]    = tmalloc(sizeof(*(nl->value[i]))*nl->n_values[i]);
        nl->repeat[i]   = tmalloc(sizeof(*(nl->repeat[i]))*nl->n_values[i]);
#ifdef DEBUG
        printf("scan_namelist: allocation successful--nl->n_values[%ld]=%ld\n",
            i, nl->n_values[i]);
#endif

        for (j=0; j<nl->n_values[i]; j++) {
#ifdef DEBUG
          printf("scan_namelist: scanning tokens from >%s<\n",
                 values);
#endif
            ptr = nl->value[i][j] =
                    get_token_tq(values, ", ", ", ", "\"'", "\"'");
#ifdef DEBUG
            printf("scan_namelist: looping over values--j=%ld\n", j);
            printf("scan_namelist: token is <%s>\n", ptr);
#endif 
            if (!ptr) {
                fprintf(stderr, "error: missing values for namelist field %s\n",
                        nl->entity[i]);
                return -1;
                }
            if (!isdigit(*ptr)) {
                nl->repeat[i][j] = 1;
                un_quote(ptr);
                }
            else {
                ptr_e = ptr;
                while (isdigit(*ptr))
                    ptr++;
                if (*ptr!='*') {
                    nl->repeat[i][j] = 1;
                    continue;
                    }
                *ptr = 0;
                if ((length=strlen(ptr_e))-1>bufsize)
                    buffer = trealloc(buffer, sizeof(*buffer)*(bufsize=length+1024));
                strcpy_ss(buffer, ptr_e);
                strcpy_ss(nl->value[i][j], ptr+1);
                un_quote(nl->value[i][j]);
#ifdef DEBUG
                printf("doing repeat scan: ptr_e = <%s>  ptr+1 = <%s>\nbuffer = <%s>\n",
                    ptr_e, ptr+1, buffer);
#endif
                if (sscanf(buffer, "%ld", nl->repeat[i]+j)!=1)
                    bomb("bad repeat specifier in namelist", NULL);
                }
#ifdef DEBUG
            printf("scan_namelist: nl->value[%ld][%ld] = %ld*<%s>\n",
                i, j, nl->repeat[i][j], nl->value[i][j]);
            printf("remainder: <%s>\n\n", values);
#endif
            }
        }

    free(values);
    free(ptr_to_free);
#ifdef DEBUG
    nl->n_entities = n_entities;
    show_namelist(stdout, nl);
#endif
    return(nl->n_entities=n_entities);
    }
Exemple #5
0
/*
 * Create one URLTranslation. Return NULL for failure, pointer to it for OK.
 */
static URLTranslation *create_onetrans(CfgGroup *grp)
{
    URLTranslation *ot;
    Octstr *aliases, *url, *post_url, *post_xml, *text, *file, *exec;
    Octstr *accepted_smsc, *forced_smsc, *default_smsc;
    Octstr *grpname, *sendsms_user, *sms_service;
    int is_sms_service;
    Octstr *accepted_smsc_regex;
    Octstr *allowed_prefix_regex;
    Octstr *denied_prefix_regex;
    Octstr *allowed_receiver_prefix_regex;
    Octstr *denied_receiver_prefix_regex;
    Octstr *white_list_regex;
    Octstr *black_list_regex;
    Octstr *keyword_regex;
    Octstr *os;
    
    grpname = cfg_get_group_name(grp);
    if (grpname == NULL)
    	return NULL;

    sms_service = octstr_imm("sms-service");
    sendsms_user = octstr_imm("sendsms-user");
    if (octstr_compare(grpname, sms_service) == 0)
    	is_sms_service = 1;
    else if (octstr_compare(grpname, sendsms_user) == 0)
    	is_sms_service = 0;
    else {
	octstr_destroy(grpname);
	return NULL;
    }
    octstr_destroy(grpname);

    ot = gw_malloc(sizeof(URLTranslation));

    ot->keyword = NULL;
    ot->aliases = NULL;
    ot->pattern = NULL;
    ot->prefix = NULL;
    ot->suffix = NULL;
    ot->faked_sender = NULL;
    ot->default_sender = NULL;
    ot->split_chars = NULL;
    ot->split_suffix = NULL;
    ot->footer = NULL;
    ot->header = NULL;
    ot->name = NULL;
    ot->username = NULL;
    ot->password = NULL;
    ot->omit_empty = 0;
    ot->accepted_smsc = NULL;
    ot->forced_smsc = NULL;
    ot->default_smsc = NULL;
    ot->allow_ip = NULL;
    ot->deny_ip = NULL;
    ot->allowed_prefix = NULL;
    ot->denied_prefix = NULL;
    ot->allowed_recv_prefix = NULL;
    ot->denied_recv_prefix = NULL;
    ot->white_list = NULL;
    ot->black_list = NULL;
    ot->keyword_regex = NULL;
    ot->accepted_smsc_regex = NULL;
    ot->allowed_prefix_regex = NULL;
    ot->denied_prefix_regex = NULL;
    ot->allowed_receiver_prefix_regex = NULL;
    ot->denied_receiver_prefix_regex = NULL;
    ot->white_list_regex = NULL;
    ot->black_list_regex = NULL;
    
    if (is_sms_service) {
	cfg_get_bool(&ot->catch_all, grp, octstr_imm("catch-all"));

	ot->dlr_url = cfg_get(grp, octstr_imm("dlr-url"));
	    
	url = cfg_get(grp, octstr_imm("get-url"));
	if (url == NULL)
	    url = cfg_get(grp, octstr_imm("url"));
	    
	post_url = cfg_get(grp, octstr_imm("post-url"));
	post_xml = cfg_get(grp, octstr_imm("post-xml"));
	file = cfg_get(grp, octstr_imm("file"));
	text = cfg_get(grp, octstr_imm("text"));
	exec = cfg_get(grp, octstr_imm("exec"));
	if (url != NULL) {
	    ot->type = TRANSTYPE_GET_URL;
	    ot->pattern = url;
	} else if (post_url != NULL) {
	    ot->type = TRANSTYPE_POST_URL;
	    ot->pattern = post_url;
	    ot->catch_all = 1;
	} else if (post_xml != NULL) {
	    ot->type = TRANSTYPE_POST_XML;
	    ot->pattern = post_xml;
	    ot->catch_all = 1;
	} else if (file != NULL) {
	    ot->type = TRANSTYPE_FILE;
	    ot->pattern = file;
	} else if (text != NULL) {
	    ot->type = TRANSTYPE_TEXT;
	    ot->pattern = text;
	} else if (exec != NULL) {
	    ot->type = TRANSTYPE_EXECUTE;
	    ot->pattern = exec;
	} else {
	    error(0, "Configuration group `sms-service' "
	    	     "did not specify get-url, post-url, post-xml, file or text.");
    	    goto error;
	}

	ot->keyword = cfg_get(grp, octstr_imm("keyword"));
	if (ot->keyword == NULL) {
	    error(0, "Group 'sms-service' must include 'keyword'.");
	    goto error;
	}
	octstr_convert_range(ot->keyword, 0, octstr_len(ot->keyword), 
	    	    	     tolower);

        keyword_regex = cfg_get(grp, octstr_imm("keyword-regex"));
        if (keyword_regex != NULL) {
            if ((ot->keyword_regex = gw_regex_comp(keyword_regex, REG_EXTENDED)) == NULL)
                  panic(0, "Could not compile pattern '%s'", octstr_get_cstr(keyword_regex));
            octstr_destroy(keyword_regex);
        }

	ot->name = cfg_get(grp, octstr_imm("name"));
	if (ot->name == NULL)
	    ot->name = octstr_duplicate(ot->keyword);

	aliases = cfg_get(grp, octstr_imm("aliases"));
	if (aliases == NULL)
	    ot->aliases = list_create();
	else {
	    long i;
	    Octstr *os;

	    ot->aliases = octstr_split(aliases, octstr_imm(";"));
	    octstr_destroy(aliases);
	    for (i = 0; i < list_len(ot->aliases); ++i) {
		os = list_get(ot->aliases, i);
	    	octstr_convert_range(os, 0, octstr_len(os), tolower);
	    }
	}

	accepted_smsc = cfg_get(grp, octstr_imm("accepted-smsc"));
	if (accepted_smsc != NULL) {
	    ot->accepted_smsc = octstr_split(accepted_smsc, octstr_imm(";"));
	    octstr_destroy(accepted_smsc);
	}
        accepted_smsc_regex = cfg_get(grp, octstr_imm("accepted-smsc-regex"));
        if (accepted_smsc_regex != NULL) { 
            if ( (ot->accepted_smsc_regex = gw_regex_comp(accepted_smsc_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(accepted_smsc_regex));
            octstr_destroy(accepted_smsc_regex);
        }

	cfg_get_bool(&ot->assume_plain_text, grp, 
		     octstr_imm("assume-plain-text"));
	cfg_get_bool(&ot->accept_x_kannel_headers, grp, 
		     octstr_imm("accept-x-kannel-headers"));
	cfg_get_bool(&ot->strip_keyword, grp, octstr_imm("strip-keyword"));
	cfg_get_bool(&ot->send_sender, grp, octstr_imm("send-sender"));
	
	ot->prefix = cfg_get(grp, octstr_imm("prefix"));
	ot->suffix = cfg_get(grp, octstr_imm("suffix"));
        ot->allowed_recv_prefix = cfg_get(grp, octstr_imm("allowed-receiver-prefix"));
        allowed_receiver_prefix_regex = cfg_get(grp, octstr_imm("allowed-receiver-prefix-regex"));
        if (allowed_receiver_prefix_regex != NULL) {
            if ((ot->allowed_receiver_prefix_regex = gw_regex_comp(allowed_receiver_prefix_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(allowed_receiver_prefix_regex));
            octstr_destroy(allowed_receiver_prefix_regex);
        }

	ot->allowed_recv_prefix = cfg_get(grp, octstr_imm("allowed-receiver-prefix"));
    ot->denied_recv_prefix = cfg_get(grp, octstr_imm("denied-receiver-prefix"));
        denied_receiver_prefix_regex = cfg_get(grp, octstr_imm("denied-receiver-prefix-regex"));
        if (denied_receiver_prefix_regex != NULL) {
            if ((ot->denied_receiver_prefix_regex = gw_regex_comp(denied_receiver_prefix_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'",octstr_get_cstr(denied_receiver_prefix_regex));
            octstr_destroy(denied_receiver_prefix_regex);
        }

	ot->args = count_occurences(ot->pattern, octstr_imm("%s"));
	ot->args += count_occurences(ot->pattern, octstr_imm("%S"));
	ot->has_catchall_arg = 
	    (count_occurences(ot->pattern, octstr_imm("%r")) > 0) ||
	    (count_occurences(ot->pattern, octstr_imm("%a")) > 0);

    } else {
	ot->type = TRANSTYPE_SENDSMS;
	ot->pattern = octstr_create("");
	ot->args = 0;
	ot->has_catchall_arg = 0;
	ot->catch_all = 1;
	ot->username = cfg_get(grp, octstr_imm("username"));
	ot->password = cfg_get(grp, octstr_imm("password"));
	ot->dlr_url = cfg_get(grp, octstr_imm("dlr-url"));
	if (ot->password == NULL) {
	    error(0, "Password required for send-sms user");
	    goto error;
	}
	ot->name = cfg_get(grp, octstr_imm("name"));
	if (ot->name == NULL)
	    ot->name = octstr_duplicate(ot->username);

	forced_smsc = cfg_get(grp, octstr_imm("forced-smsc"));
	default_smsc = cfg_get(grp, octstr_imm("default-smsc"));
	if (forced_smsc != NULL) {
	    if (default_smsc != NULL) {
		info(0, "Redundant default-smsc for send-sms user %s", 
		     octstr_get_cstr(ot->username));
	    }
	    ot->forced_smsc = forced_smsc;
	    octstr_destroy(default_smsc);
	} else  if (default_smsc != NULL)
	    ot->default_smsc = default_smsc;

	ot->deny_ip = cfg_get(grp, octstr_imm("user-deny-ip"));
	ot->allow_ip = cfg_get(grp, octstr_imm("user-allow-ip"));
	ot->default_sender = cfg_get(grp, octstr_imm("default-sender"));
    }
    
    ot->allowed_prefix = cfg_get(grp, octstr_imm("allowed-prefix"));
    allowed_prefix_regex = cfg_get(grp, octstr_imm("allowed-prefix-regex"));
    if (allowed_prefix_regex != NULL) {
        if ((ot->allowed_prefix_regex = gw_regex_comp(allowed_prefix_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(allowed_prefix_regex));
        octstr_destroy(allowed_prefix_regex);
    }
    ot->denied_prefix = cfg_get(grp, octstr_imm("denied-prefix"));
    denied_prefix_regex = cfg_get(grp, octstr_imm("denied-prefix-regex"));
    if (denied_prefix_regex != NULL) {
        if ((ot->denied_prefix_regex = gw_regex_comp(denied_prefix_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(denied_prefix_regex));
        octstr_destroy(denied_prefix_regex);
    }
    
	os = cfg_get(grp, octstr_imm("white-list"));
	if (os != NULL) {
	    ot->white_list = numhash_create(octstr_get_cstr(os));
	    octstr_destroy(os);
	}
    white_list_regex = cfg_get(grp, octstr_imm("white-list-regex"));
    if (white_list_regex != NULL) {
        if ((ot->white_list_regex = gw_regex_comp(white_list_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(white_list_regex));
        octstr_destroy(white_list_regex);
    }

	os = cfg_get(grp, octstr_imm("black-list"));
	if (os != NULL) {
	    ot->black_list = numhash_create(octstr_get_cstr(os));
	    octstr_destroy(os);
	}
    black_list_regex = cfg_get(grp, octstr_imm("black-list-regex"));
    if (black_list_regex != NULL) {
        if ((ot->black_list_regex = gw_regex_comp(black_list_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'", octstr_get_cstr(black_list_regex));
        octstr_destroy(black_list_regex);
    }

    if (cfg_get_integer(&ot->max_messages, grp, 
    	    	    	octstr_imm("max-messages")) == -1)
	ot->max_messages = 1;
    cfg_get_bool(&ot->concatenation, grp, 
		 octstr_imm("concatenation"));
    cfg_get_bool(&ot->omit_empty, grp, 
		 octstr_imm("omit-empty"));
    
    ot->header = cfg_get(grp, octstr_imm("header"));
    ot->footer = cfg_get(grp, octstr_imm("footer"));
    ot->faked_sender = cfg_get(grp, octstr_imm("faked-sender"));
    ot->split_chars = cfg_get(grp, octstr_imm("split-chars"));
    ot->split_suffix = cfg_get(grp, octstr_imm("split-suffix"));

    if ( (ot->prefix == NULL && ot->suffix != NULL) ||
	 (ot->prefix != NULL && ot->suffix == NULL) ) {
	warning(0, "Service <%s>: suffix and prefix are only used"
		   " if both are set.", octstr_get_cstr(ot->keyword));
    }
    if ((ot->prefix != NULL || ot->suffix != NULL) &&
        ot->type != TRANSTYPE_GET_URL) {
	warning(0, "Service <%s>: suffix and prefix are only used"
                   " if type is 'get-url'.", octstr_get_cstr(ot->keyword));
    }
    
    return ot;

error:
    error(0, "Couldn't create a URLTranslation.");
    destroy_onetrans(ot);
    return NULL;
}