diff --git a/commandline.c b/commandline.c index 4a9271af..f9842cdd 100644 --- a/commandline.c +++ b/commandline.c @@ -428,6 +428,11 @@ int app_echo(int argc, const char *const *argv, struct command_line_option *o) return 0; } +int cli_lookup_did(const char *text) +{ + return text[0] == '\0' || strcmp(text, "*") || str_is_did(text); +} + int app_dna_lookup(int argc, const char *const *argv, struct command_line_option *o) { int i; @@ -441,7 +446,7 @@ int app_dna_lookup(int argc, const char *const *argv, struct command_line_option char uris[MAXREPLIES][MAXURILEN]; const char *did; - if (cli_arg(argc, argv, o, "did", &did, NULL, "*") == -1) + if (cli_arg(argc, argv, o, "did", &did, cli_lookup_did, "*") == -1) return -1; /* Bind to MDP socket and await confirmation */ @@ -1407,15 +1412,19 @@ int app_keyring_add(int argc, const char *const *argv, struct command_line_optio return 0; } +int cli_optional_did(const char *text) +{ + return text[0] == '\0' || str_is_did(text); +} + int app_keyring_set_did(int argc, const char *const *argv, struct command_line_option *o) { const char *sid, *did, *pin, *name; - cli_arg(argc, argv, o, "sid", &sid, NULL, ""); - cli_arg(argc, argv, o, "did", &did, NULL, ""); + cli_arg(argc, argv, o, "sid", &sid, str_is_subscriber_id, ""); + cli_arg(argc, argv, o, "did", &did, str_is_did, ""); cli_arg(argc, argv, o, "name", &name, NULL, ""); cli_arg(argc, argv, o, "pin", &pin, NULL, ""); - if (strlen(did)>31) return WHY("DID too long (31 digits max)"); if (strlen(name)>63) return WHY("Name too long (31 char max)"); if (!(keyring = keyring_open_with_pins(pin))) diff --git a/dataformats.c b/dataformats.c index 527bebfe..aa5e84c2 100644 --- a/dataformats.c +++ b/dataformats.c @@ -99,7 +99,8 @@ int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary) int str_is_subscriber_id(const char *sid) { - return strcasecmp(sid, "broadcast") == 0 || _is_xstring(sid, SID_STRLEN); + size_t len = 0; + return strn_is_subscriber_id(sid, &len) && sid[len] == '\0'; } int strn_is_subscriber_id(const char *sid, size_t *lenp) @@ -157,6 +158,32 @@ int rhizome_str_is_file_hash(const char *hash) return _is_xstring(hash, RHIZOME_FILEHASH_STRLEN); } +int str_is_did(const char *did) +{ + size_t len = 0; + return strn_is_did(did, &len) && did[len] == '\0'; +} + +int strn_is_did(const char *did, size_t *lenp) +{ + int i; + for (i = 0; i < DID_MAXSIZE; ++i) { + switch (did[i]) { + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + case '*': case '#': case '+': + break; + default: + return 0; + } + } + if (i < DID_MINSIZE) + return 0; + if (lenp) + *lenp = i; + return 1; +} + int extractDid(unsigned char *packet,int *ofs,char *did) { int d=0; diff --git a/serval.h b/serval.h index 35dd4f1f..da1ce86e 100755 --- a/serval.h +++ b/serval.h @@ -300,6 +300,7 @@ void keyring_identity_extract(const keyring_identity *id, const unsigned char ** */ #define SID_SIZE 32 +#define DID_MINSIZE 5 #define DID_MAXSIZE 32 #define SIDDIDFIELD_LEN (SID_SIZE+1) #define PINFIELD_LEN 32 @@ -688,6 +689,8 @@ char *str_toupper_inplace(char *s); int str_is_subscriber_id(const char *sid); int strn_is_subscriber_id(const char *sid, size_t *lenp); +int str_is_did(const char *did); +int strn_is_did(const char *did, size_t *lenp); int stowSid(unsigned char *packet, int ofs, const char *sid); int stowDid(unsigned char *packet,int *ofs,char *did); diff --git a/tests/dnahelper b/tests/dnahelper index 7b1f9b2b..6f3749cf 100755 --- a/tests/dnahelper +++ b/tests/dnahelper @@ -43,64 +43,78 @@ configure_servald_server() { setup_dnahelper() { dnahelper="$TFWTMP/dnahelper" - cat >"$dnahelper" <"$dnahelper" <<'EOF' +#!/bin/sh +echo STARTED +while read line +do + token="${line%%|*}" + line="${line#*|}" + did="${line%%|*}" + line="${line#*|}" + case "$token|$did|$line" in + '|'*'|') + echo "empty token" >&2 + ;; + *'||') + echo "empty DID" >&2 + ;; + *'|00000|') + echo "$token|A|$did|B|" + ;; + *'|00001|') + echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|" + ;; + *'|00002|') + echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|" + sleep 0.1 + echo "$token|sip://$token@10.1.0.2|$did|Joe B. Bloggs|" + sleep 0.1 + ;; + *'|00003|') + echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|" + sleep 0.1 + echo "$token|sip://$token@10.1.0.2|$did|Joe B. Bloggs|" + sleep 0.1 + echo "$token|sip://$token@10.1.0.3|$did|Joe C. Bloggs|" + sleep 0.1 + ;; + *'|00004|') + # Empty URI + echo "$token||$did|Eccles|" + ;; + # Test malformed URI + # Test mismatched token + # Test empty token + # Test long token + # Test empty DID + # Test mismatched DID + # Test long DID + # Test malformed line + # Test long reply + # Test DONE timeout + # Test die + # Test fork and die + *'|'*'|') + ;; + *) + echo "garbage line" >&2 + ;; + esac + echo DONE +done EOF chmod 0755 "$dnahelper" executeOk "$dnahelper" <