[PATCH] improve checks on usernames V3.

Tito farmatito at tiscali.it
Tue Aug 2 19:57:34 UTC 2011


Hi,
minor improvements vs. v2 patch:
1) some more comments added.
2) we now print the position of the illegal character.

Hints, critics, improvements are welcome.
Thanks to all who helped.

Ciao,
Tito

void FAST_FUNC die_if_bad_username(const char *name)
{
	const char *s = name;

	assert(name != NULL);
	
	/* The minimum size of the login name is one char or two if
	 * last char is the '$', this exception is catched later 
	 * as the dollar sign could not be the first char.
	 * The maximum size of the login name is LOGIN_NAME_MAX 
	 * including the terminating null byte. 
	 */
	if (!*name || strlen(name) + 1 > LOGIN_NAME_MAX)
		bb_error_msg_and_die("illegal name length");
		
	do {
		/* We don't use isalnum  as it will allow locale-specific non-ASCII */
		/* letters in legacy 8-bit locales. */
		if (((*name == '-' || *name == '.' || *name == '@') && name != s) /* not as first char */
		 || (*name == '$' && (!name[1] && name != s)) /* not as first, only as last char */
		 || *name == '_'
		 || (*name >= 48 && *name <= 57)  /* 0-9 */
		 || (*name >= 65 && *name <= 90)  /* A-Z */
		 || (*name >= 97 && *name <= 122) /* a-z */
		) {
			continue;
		}
		bb_error_msg_and_die("illegal character at position '%d'", name - s);
	} while (*++name);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: die_if_bad_username_v3.patch
Type: text/x-patch
Size: 3758 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20110802/8e509558/attachment.bin>


More information about the busybox mailing list