]> jfr.im git - irc/quakenet/newserv.git/commitdiff
CHANSERV: refactor authlib slightly
authorChris Porter <redacted>
Sun, 22 Jan 2012 04:34:06 +0000 (04:34 +0000)
committerChris Porter <redacted>
Sun, 22 Jan 2012 04:34:06 +0000 (04:34 +0000)
chanserv/authcmds/hello.c
chanserv/authcmds/newpass.c
chanserv/authlib.c
chanserv/authlib.h

index be45c9585c45da7e4e8007e3fdc85a6f7eba7a0c..6c3745ca8ea39b0fa37d6c1f6acf584f00f54f57 100644 (file)
@@ -21,7 +21,6 @@
 #include <stdio.h>
 #include <string.h>
 
-/* REMEMBER YOU CAN ALSO CREATE ACCOUNTS IN CHANSERV_RELAY.C */
 int csa_dohello(void *source, int cargc, char **cargv) {
   nick *sender=source;
   reguser *rup;
@@ -33,7 +32,6 @@ int csa_dohello(void *source, int cargc, char **cargv) {
   char *dupemail;
   activeuser *aup;
   maillock *mlp;
-  time_t t;
 
   if (getreguserfromnick(sender))
     return CMD_ERROR;
@@ -115,19 +113,39 @@ int csa_dohello(void *source, int cargc, char **cargv) {
     }
   }
 
-  mdp=findorcreatemaildomain(cargv[0]);
+  free(dupemail);
 
   aup->helloattempts++;
-  
-/* REMEMBER YOU CAN ALSO CREATE ACCOUNTS IN CHANSERV_RELAY.C */
-/* REMEMBER YOU CAN ALSO CREATE ACCOUNTS IN CHANSERV_RELAY.C */
-/* REMEMBER YOU CAN ALSO CREATE ACCOUNTS IN CHANSERV_RELAY.C */
 
-  t=time(NULL);
-  rup=getreguser();
+  rup=csa_createaccount(sender->nick,"", cargv[0]);
+  csa_createrandompw(rup->password, PASSLEN);
+  sprintf(userhost,"%s@%s",sender->ident,sender->host->name->content);
+  rup->lastuserhost=getsstring(userhost,USERLEN+HOSTLEN+1);
+
+  chanservstdmessage(sender, QM_NEWACCOUNT, rup->username,rup->email->content);
+  cs_log(sender,"HELLO OK created auth %s (%s)",rup->username,rup->email->content); 
+  csdb_createuser(rup);
+  csdb_createmail(rup, QMAIL_NEWACCOUNT);
+
+  return CMD_OK;
+}
+
+reguser *csa_createaccount(char *username, char *password, char *email) {
+  time_t t = time(NULL);
+  char *local, *dupemail;
+
+  dupemail = strdup(email);
+  local=strchr(dupemail, '@');
+  if(!local) {
+    free(dupemail);
+    return NULL;
+  }
+  *(local++)='\0';
+
+  reguser *rup=getreguser();
   rup->status=0;
   rup->ID=++lastuserID;
-  strncpy(rup->username,sender->nick,NICKLEN); rup->username[NICKLEN]='\0';
+  strncpy(rup->username,username,NICKLEN); rup->username[NICKLEN]='\0';
   rup->created=t;
   rup->lastauth=0;
   rup->lastemailchange=t;
@@ -138,18 +156,17 @@ int csa_dohello(void *source, int cargc, char **cargv) {
   rup->suspendexp=0;
   rup->suspendtime=0;
   rup->lockuntil=0;
-  rup->password[0]='\0';
-  rup->email=getsstring(cargv[0],EMAILLEN);
+  strncpy(rup->password,password,PASSLEN); rup->password[PASSLEN]='\0';
+  rup->email=getsstring(email,EMAILLEN);
   rup->lastemail=NULL;
 
   rup->localpart=getsstring(dupemail,EMAILLEN);
   free(dupemail);
 
-  rup->domain=mdp;
+  rup->domain=findorcreatemaildomain(email);
   addregusertomaildomain(rup, rup->domain);
   rup->info=NULL;
-  sprintf(userhost,"%s@%s",sender->ident,sender->host->name->content);
-  rup->lastuserhost=getsstring(userhost,USERLEN+HOSTLEN+1);
+  rup->lastuserhost=NULL;
   rup->suspendreason=NULL;
   rup->comment=NULL;
   rup->knownon=NULL;
@@ -157,11 +174,7 @@ int csa_dohello(void *source, int cargc, char **cargv) {
   rup->stealcount=0;
   rup->fakeuser=NULL;
   addregusertohash(rup);
-  csa_createrandompw(rup->password, PASSLEN);
-  chanservstdmessage(sender, QM_NEWACCOUNT, rup->username,rup->email->content);
-  cs_log(sender,"HELLO OK created auth %s (%s)",rup->username,rup->email->content); 
-  csdb_createuser(rup);
-  csdb_createmail(rup, QMAIL_NEWACCOUNT);
-  return CMD_OK;
+
+  return rup;
 }
+
index 69e78f2d7cf7c58b1970a124d96b4feb87f5d590..968a818836ff1dac5712ce91630d364aa3a9de72 100644 (file)
@@ -30,9 +30,9 @@
 int csa_donewpw(void *source, int cargc, char **cargv) {
   reguser *rup;
   nick *sender=source;
-  int i, cntweak = 0, cntdigits = 0, cntletters = 0;
   unsigned int same=0;
   time_t t;
+  int pq;
 
   if (cargc<3) {
     chanservstdmessage(sender, QM_NOTENOUGHPARAMS, "newpass");
@@ -54,30 +54,25 @@ int csa_donewpw(void *source, int cargc, char **cargv) {
     return CMD_ERROR;
   }
 
-  if (strlen(cargv[1]) < 6) {
-    chanservstdmessage(sender, QM_PWTOSHORT); /* new password to short */
-    cs_log(sender,"NEWPASS FAIL username %s password to short %s (%zu characters)",rup->username,cargv[1],strlen(cargv[1]));
-    return CMD_ERROR;
-  }
-
   if (!strcmp(cargv[0],cargv[1])) {
     /* If they are the same then continue anyway but don't send the hook later. */
     same=1;
   }
 
-  for ( i = 0; cargv[1][i] && i < PASSLEN; i++ ) {
-    if ( cargv[1][i] == cargv[1][i+1] || cargv[1][i] + 1 == cargv[1][i+1] || cargv[1][i] - 1 == cargv[1][i+1] )
-      cntweak++;
-    if(isdigit(cargv[1][i]))
-      cntdigits++;
-    if(islower(cargv[1][i]) || isupper(cargv[1][i]))
-      cntletters++;
-  }
-
-  if( cntweak > 3 || !cntdigits || !cntletters) {
+  pq = csa_checkpasswordquality(cargv[1]);
+  if(pq == QM_PWTOSHORT) {
+    chanservstdmessage(sender, QM_PWTOSHORT); /* new password to short */
+    cs_log(sender,"NEWPASS FAIL username %s password to short %s (%zu characters)",rup->username,cargv[1],strlen(cargv[1]));
+    return CMD_ERROR;
+  } else if(pq == QM_PWTOWEAK) {
     chanservstdmessage(sender, QM_PWTOWEAK); /* new password is weak */
     cs_log(sender,"NEWPASS FAIL username %s password to weak %s",rup->username,cargv[1]);
     return CMD_ERROR;
+  } else if(pq == -1) {
+    /* all good */
+  } else {
+    chanservsendmessage(sender, "unknown error in newpass.c... contact #help");
+    return CMD_ERROR;
   }
 
   t=time(NULL);
index 5cf36b9036b700629e507064451d7e3e0d850adf..c587361470184853164ff43fbbff110f77e8b35f 100644 (file)
@@ -1,9 +1,10 @@
 /* authlib.c */
 
-#include "authlib.h"
 #include "chanserv.h"
 #include "../lib/irc_string.h"
+#include "authlib.h"
 
+#include <stdio.h>
 #include <string.h>
 #include <ctype.h>
 #include <sys/types.h>
@@ -40,36 +41,28 @@ void csa_freeregex(void) {
 /*
  * use regex matching to determine if it's a valid eboy or not
  */
-int csa_checkeboy(nick *sender, char *eboy)
-{ 
+int csa_checkeboy_r(char *eboy)
+{
   int i, len;
 
   len = (((strlen(eboy)) < (EMAILLEN)) ? (strlen(eboy)) : (EMAILLEN));
   if (len <= 4) {
-    if (sender)
-      chanservstdmessage(sender, QM_EMAILTOOSHORT, eboy);
-    return (1);
+    return QM_EMAILTOOSHORT;
   }
 
   if (strstr(&eboy[1], "@") == NULL) {
-    if (sender)
-      chanservstdmessage(sender, QM_EMAILNOAT, eboy);
-    return (1);
+    return QM_EMAILNOAT;
   }
 
   if (eboy[len - 1] == '@') {
-    if (sender)
-      chanservstdmessage(sender, QM_EMAILATEND, eboy);
-    return (1);
+    return QM_EMAILATEND;
   }
 
   for (i = 0; i < len; i++) {
     if (!isalpha(eboy[i]) && !isdigit(eboy[i])
         && !(eboy[i] == '@') && !(eboy[i] == '.')
         && !(eboy[i] == '_') && !(eboy[i] == '-')) {
-      if (sender)
-        chanservstdmessage(sender, QM_EMAILINVCHR, eboy);
-      return (1);
+      return QM_EMAILINVCHR;
     }
   }
 
@@ -77,33 +70,47 @@ int csa_checkeboy(nick *sender, char *eboy)
   if (!ircd_strncmp("user@mymailhost.xx", eboy, len) || !ircd_strncmp("info@quakenet.org", eboy, len)
       || !ircd_strncmp("user@mymail.xx", eboy, len) || !ircd_strncmp("user@mail.cc", eboy, len)
       || !ircd_strncmp("user@host.com", eboy, len) || !ircd_strncmp("Jackie@your.isp.com", eboy, len)
-      || !ircd_strncmp("QBot@QuakeNet.org", eboy, len) || !ircd_strncmp("Q@CServe.quakenet.org", eboy, len)) {
-    if (sender)
-      chanservstdmessage(sender, QM_NOTYOUREMAIL, eboy);
-    return (1);
+      || !ircd_strncmp("QBot@QuakeNet.org", eboy, len) || !ircd_strncmp("Q@CServe.quakenet.org", eboy, len)
+      || !ircd_strncmp("badger@example.com", eboy, len)) {
+    return QM_NOTYOUREMAIL;
   }
 
   if (regexec(&remail, eboy, (size_t) 0, NULL, 0)) {
-    if (sender)
-      chanservstdmessage(sender, QM_INVALIDEMAIL, eboy);
-    return (1);
+    return QM_INVALIDEMAIL;
   }
 
-  return (0);
+  return -1;
+}
+
+int csa_checkeboy(nick *sender, char *eboy)
+{
+  int r = csa_checkeboy_r(eboy);
+  if (r == -1)
+    return 0;
+
+  if(sender)
+    chanservstdmessage(sender, r, eboy);
+
+  return 1;
 }
 
 /*
  * use regex matching to determine if it's a valid account name or not
  */
-int csa_checkaccountname(nick *sender, char *accountname) {
+int csa_checkaccountname_r(char *accountname) {
   if (regexec(&raccount, accountname, (size_t) 0, NULL, 0)) {
-    if (sender)
-      chanservstdmessage(sender, QM_INVALIDACCOUNTNAME);
     return (1);
   }
   return (0);
 }
 
+int csa_checkaccountname(nick *sender, char *accountname) {
+  int r = csa_checkaccountname_r(accountname);
+  if(r && sender)
+    chanservstdmessage(sender, QM_INVALIDACCOUNTNAME);
+
+  return r;
+}
 
 /*
  * create a random pw. code stolen from fox's O
@@ -142,3 +149,71 @@ int csa_checkthrottled(nick *sender, reguser *rup, char *s)
   }
   return 0;
 }
+
+int csa_checkpasswordquality(char *password) {
+  int i, cntweak = 0, cntdigits = 0, cntletters = 0;
+  if (strlen(password) < 6)
+    return QM_PWTOSHORT;
+
+  for ( i = 0; password[i] && i < PASSLEN; i++ ) {
+    if ( password[i] == password[i+1] || password[i] + 1 == password[i+1] || password[i] - 1 == password[i+1] )
+      cntweak++;
+    if(isdigit(password[i]))
+      cntdigits++;
+    if(islower(password[i]) || isupper(password[i]))
+      cntletters++;
+  }
+
+  if( cntweak > 3 || !cntdigits || !cntletters)
+    return QM_PWTOWEAK;
+
+  return -1;
+}
+
+reguser *csa_createaccount(char *username, char *password, char *email) {
+  time_t t = time(NULL);
+  char *local, *dupemail;
+
+  dupemail = strdup(email);
+  local=strchr(dupemail, '@');
+  if(!local) {
+    free(dupemail);
+    return NULL;
+  }
+  *(local++)='\0';
+
+  reguser *rup=getreguser();
+  rup->status=0;
+  rup->ID=++lastuserID;
+  strncpy(rup->username,username,NICKLEN); rup->username[NICKLEN]='\0';
+  rup->created=t;
+  rup->lastauth=0;
+  rup->lastemailchange=t;
+  rup->lastpasschange=t;
+  rup->flags=QUFLAG_NOTICE;
+  rup->languageid=0;
+  rup->suspendby=0;
+  rup->suspendexp=0;
+  rup->suspendtime=0;
+  rup->lockuntil=0;
+  strncpy(rup->password,password,PASSLEN); rup->password[PASSLEN]='\0';
+  rup->email=getsstring(email,EMAILLEN);
+  rup->lastemail=NULL;
+
+  rup->localpart=getsstring(dupemail,EMAILLEN);
+  free(dupemail);
+
+  rup->domain=findorcreatemaildomain(email);
+  addregusertomaildomain(rup, rup->domain);
+  rup->info=NULL;
+  rup->lastuserhost=NULL;
+  rup->suspendreason=NULL;
+  rup->comment=NULL;
+  rup->knownon=NULL;
+  rup->checkshd=NULL;
+  rup->stealcount=0;
+  rup->fakeuser=NULL;
+  addregusertohash(rup);
+
+  return rup;
+}
index e6a29b26a3501c16915ded679930d2c0d85dd697..251f396bae8080336ebb38c29b031bb94487e912 100644 (file)
@@ -8,3 +8,7 @@ void cs_createrandompw(char *arg, int n);
 int csa_initregex(void);
 void csa_freeregex(void);
 int csa_checkaccountname(nick *sender, char *accountname);
+int csa_checkaccountname_r(char *accountname);
+int cs_checkeboy_r(char *arg);
+int csa_checkpasswordquality(char *password);
+reguser *csa_createaccount(char *username, char *password, char *email);