From 07d2f9e8f641a93977a34f60d9ee187ab0268a1a Mon Sep 17 00:00:00 2001 From: piaip Date: Wed, 19 Aug 2009 13:34:39 +0000 Subject: * prepare to move ambiguous id check into regmaild (it should be named as regcheckd instead someday) git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4754 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- daemon/regmaild/regmaild.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) (limited to 'daemon/regmaild/regmaild.c') diff --git a/daemon/regmaild/regmaild.c b/daemon/regmaild/regmaild.c index 96acf878..9dda4c05 100644 --- a/daemon/regmaild/regmaild.c +++ b/daemon/regmaild/regmaild.c @@ -264,6 +264,85 @@ end: return ret; } +/////////////////////////////////////////////////////////////////////// +// Ambiguous user id checking + +void +build_unambiguous_userid(char *uid) +{ + int i = 0; + const char *ambtbl[] = { // need to also update ambchars if you touch these + "0Oo", + "1Il", + NULL + }; + + for (i = 0; ambtbl[i]; ) + { + size_t pos = strcspn(uid, ambtbl[i]); + if (!uid[pos]) + { + i++; + continue; + } + uid[pos] = ambtbl[i][0]; + uid += (pos+1); // skip the processed character + } +} + +// TODO XXX cache these results someday + +int +find_ambiguous_userid(const char *userid) +{ + const char *ambchars = "0Oo1Il"; // super set of ambtbl + size_t uidlen = 0, iamb; + char ambuid[IDLEN+1], shmuid[IDLEN+1]; + int i; + + assert(userid && *userid); + + // if NULL, found nothing. + iamb = strcspn(userid, ambchars); + if (!userid[iamb]) + return 0; + + // build un-ambiguous uid + uidlen = strlcpy(ambuid, userid, sizeof(ambuid)); + build_unambiguous_userid(ambuid); + + for (i = 0; i < MAX_USERS; i++) + { + const char *ruid = SHM->userid[i]; + + // quick test: same non-amb prefix, and remote uid has amb characters + if (iamb > 0 && tolower(*ruid) != tolower(*ambuid)) + continue; + if (!ruid[strcspn(ruid, ambchars)]) + continue; + + // copy and check remote uid length + if (strlcpy(shmuid, ruid, sizeof(shmuid)) != uidlen) + continue; + + build_unambiguous_userid(shmuid); + if (strcasecmp(shmuid, ambuid) == 0) + return 1; + } + + return 0; +} + +int +regcheck_ambiguous_id(const char *userid) +{ + if (!userid || !*userid) + return 0; + if (find_ambiguous_userid(userid)) + return 1; + return 0; +} + /////////////////////////////////////////////////////////////////////// // Callbacks @@ -325,6 +404,16 @@ client_cb(int fd, short event, void *arg) } break; + case REGCHECK_REQ_AMBIGUOUS: + ret = regcheck_ambiguous_id(req.userid); + fprintf(stderr, "%-*s check ambiguous id exist (result: %d)\r\n", + IDLEN, req.userid, ret); + if (towrite(fd, &ret, sizeof(ret)) != sizeof(ret)) + { + fprintf(stderr, " error: cannot write response...\r\n"); + } + break; + default: fprintf(stderr, "error: invalid operation: %d.\r\n", req.operation); close(fd); -- cgit v1.2.3