[or-cvs] Functions to test nickname validity
Nick Mathewson
nickm at seul.org
Wed Aug 18 04:44:27 UTC 2004
Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv22627/src/or
Modified Files:
router.c routerparse.c test.c config.c or.h
Log Message:
Functions to test nickname validity
Index: router.c
===================================================================
RCS file: /home/or/cvsroot/src/or/router.c,v
retrieving revision 1.84
retrieving revision 1.85
diff -u -d -r1.84 -r1.85
--- router.c 16 Aug 2004 11:43:18 -0000 1.84
+++ router.c 18 Aug 2004 04:44:24 -0000 1.85
@@ -774,6 +774,26 @@
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
Index: routerparse.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routerparse.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -d -r1.34 -r1.35
--- routerparse.c 17 Aug 2004 06:06:05 -0000 1.34
+++ routerparse.c 18 Aug 2004 04:44:24 -0000 1.35
@@ -681,13 +681,8 @@
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]);
Index: test.c
===================================================================
RCS file: /home/or/cvsroot/src/or/test.c,v
retrieving revision 1.111
retrieving revision 1.112
diff -u -d -r1.111 -r1.112
--- test.c 17 Aug 2004 21:06:36 -0000 1.111
+++ test.c 18 Aug 2004 04:44:24 -0000 1.112
@@ -758,6 +758,26 @@
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";
Index: config.c
===================================================================
RCS file: /home/or/cvsroot/src/or/config.c,v
retrieving revision 1.146
retrieving revision 1.147
diff -u -d -r1.146 -r1.147
--- config.c 18 Aug 2004 04:04:08 -0000 1.146
+++ config.c 18 Aug 2004 04:44:24 -0000 1.147
@@ -735,6 +735,10 @@
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);
Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.409
retrieving revision 1.410
diff -u -d -r1.409 -r1.410
--- or.h 17 Aug 2004 06:27:32 -0000 1.409
+++ or.h 18 Aug 2004 04:44:24 -0000 1.410
@@ -1386,6 +1386,8 @@
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 ***************************/
More information about the tor-commits
mailing list