Functions to test nickname validity

svn:r2269
This commit is contained in:
Nick Mathewson 2004-08-18 04:44:24 +00:00
parent adccf3aea6
commit 86980cf6e2
6 changed files with 50 additions and 7 deletions

View File

@ -81,6 +81,8 @@ struct timeval {
#define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/ "
#endif
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
size_t strlcat(char *dst, const char *src, size_t siz);
size_t strlcpy(char *dst, const char *src, size_t siz);

View File

@ -735,6 +735,10 @@ int getconfig(int argc, char **argv, or_options_t *options) {
log_fn(LOG_WARN, "Nickname '%s' contains illegal characters.", options->Nickname);
result = -1;
}
if (strlen(options->Nickname) == 0) {
log_fn(LOG_WARN, "Nickname must have at least one character");
result = -1;
}
if (strlen(options->Nickname) > MAX_NICKNAME_LEN) {
log_fn(LOG_WARN, "Nickname '%s' has more than %d characters.",
options->Nickname, MAX_NICKNAME_LEN);

View File

@ -1386,6 +1386,8 @@ int router_is_me(routerinfo_t *router);
int router_rebuild_descriptor(void);
int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
crypto_pk_env_t *ident_key);
int is_legal_nickname(const char *s);
int is_legal_nickname_or_hexdigest(const char *s);
/********************************* routerlist.c ***************************/

View File

@ -774,6 +774,26 @@ int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
return written+1;
}
int is_legal_nickname(const char *s)
{
size_t len;
tor_assert(s);
len = strlen(s);
return len > 0 && len <= MAX_NICKNAME_LEN &&
strspn(s,LEGAL_NICKNAME_CHARACTERS)==len;
}
int is_legal_nickname_or_hexdigest(const char *s)
{
size_t len;
tor_assert(s);
if (*s!='$')
return is_legal_nickname(s);
len = strlen(s);
return len == HEX_DIGEST_LEN+1 && strspn(s+1,HEX_CHARACTERS)==len-1;
}
/*
Local Variables:
mode:c

View File

@ -681,13 +681,8 @@ routerinfo_t *router_parse_entry_from_string(const char *s,
if (tok->n_args == 2 || tok->n_args == 5 || tok->n_args == 6) {
router->nickname = tor_strdup(tok->args[0]);
if (strlen(router->nickname) > MAX_NICKNAME_LEN) {
log_fn(LOG_WARN,"Router nickname too long.");
goto err;
}
if (strspn(router->nickname, LEGAL_NICKNAME_CHARACTERS) !=
strlen(router->nickname)) {
log_fn(LOG_WARN, "Router nickname contains illegal characters.");
if (!is_legal_nickname(router->nickname)) {
log_fn(LOG_WARN,"Router nickname is invalid");
goto err;
}
router->address = tor_strdup(tok->args[1]);

View File

@ -758,6 +758,26 @@ test_dir_format()
test_assert(! crypto_pk_generate_key(pk2));
test_assert(! crypto_pk_generate_key(pk3));
test_assert( is_legal_nickname("a"));
test_assert(!is_legal_nickname(""));
test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
test_assert(!is_legal_nickname("hyphen-")); /* bad char */
test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
/* valid */
test_assert( is_legal_nickname_or_hexdigest(
"$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
/* too short */
test_assert(!is_legal_nickname_or_hexdigest(
"$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
/* illegal char */
test_assert(!is_legal_nickname_or_hexdigest(
"$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
get_platform_str(platform, sizeof(platform));
r1.address = "testaddr1.foo.bar";