#117917 by chx, greg, webchick et al: fix problems with the automatic domain extraction -- prevents users from logging in. Backport from HEAD.