]> jfr.im git - irc/quakenet/newserv.git/blobdiff - chanserv/chanservstdcmds.c
CHANSERV: Fix possibly unsafe string handling
[irc/quakenet/newserv.git] / chanserv / chanservstdcmds.c
index 556427a593e49455f02788234abd3a3e28994b9c..1c0277c883897a78ed684670478594cb310980f5 100644 (file)
@@ -5,9 +5,10 @@
 #include "chanserv.h"
 #include "../core/schedule.h"
 #include "../lib/irc_string.h"
-#include "../pqsql/pqsql.h"
+#include "../dbapi/dbapi.h"
 
 #include <string.h>
+#include <stdio.h>
 
 int cs_dorehash(void *source, int cargc, char **cargv) {
   nick *sender=source;
@@ -44,6 +45,24 @@ int cs_doquit(void *source, int cargc, char **cargv) {
   return CMD_OK;
 }
 
+int cs_dosetquitreason(void *source, int cargc, char **cargv) {
+  nick *sender=(nick *)source;
+
+  if (cargc<0) {
+    chanservstdmessage(sender, QM_NOTENOUGHPARAMS, "setquitreason");
+    return CMD_ERROR;
+  }
+  
+  if (cs_quitreason)
+    freesstring(cs_quitreason);
+  cs_quitreason=getsstring(cargv[0], 250);
+
+  chanservstdmessage(sender, QM_DONE);
+
+  return CMD_OK;
+}
+
 int cs_dorename(void *source, int cargc, char **cargv) {
   char newnick[NICKLEN+1];
   nick *sender=source;
@@ -70,6 +89,9 @@ int cs_doshowcommands(void *source, int cargc, char **cargv) {
   int lang;
   char *message;
   cmdsummary *summary;
+  char cmdbuf[50];
+  char *ct;
+  unsigned int dumpcount=0;
   
   n=getcommandlist(cscommands, cmdlist, 200);
   rup=getreguserfromnick(sender);
@@ -81,8 +103,49 @@ int cs_doshowcommands(void *source, int cargc, char **cargv) {
 
   chanservstdmessage(sender, QM_COMMANDLIST);
 
+  if (cargc>0 && rup && UIsDev(rup) && !ircd_strcmp(cargv[0], "-v")) {
+    dumpcount=1;
+  }
+
   for (i=0;i<n;i++) {
-    char cmdbuf[50];
+    /* Generate the appropriate strings for the command name (including 
+     * prefixes for privileged users) and the summary text.
+     *
+     * We do this before we're sure we will print the command to make things
+     * easier when we are doing -v */    
+    summary=(cmdsummary *)cmdlist[i]->ext;
+    
+    if (rup && UHasStaffPriv(rup)) {
+      if (cmdlist[i]->level & QCMD_DEV) {
+        sprintf(cmdbuf,"+d %s",cmdlist[i]->command->content);
+      } else if(cmdlist[i]->level & QCMD_ADMIN) {
+        sprintf(cmdbuf,"+a %s",cmdlist[i]->command->content);
+      } else if(cmdlist[i]->level & QCMD_OPER) {
+        sprintf(cmdbuf,"+o %s",cmdlist[i]->command->content);
+      } else if(cmdlist[i]->level & QCMD_HELPER) {
+        sprintf(cmdbuf,"+h %s",cmdlist[i]->command->content);
+      } else if(cmdlist[i]->level & QCMD_STAFF) {
+        sprintf(cmdbuf,"+q %s",cmdlist[i]->command->content);
+      } else {
+        sprintf(cmdbuf,"   %s",cmdlist[i]->command->content);
+      }
+      ct=cmdbuf;
+    } else {
+      ct=cmdlist[i]->command->content;
+    }
+    
+    if (summary->bylang[lang]) {
+      message=summary->bylang[lang]->content;
+    } else if (summary->bylang[0]) {
+      message=summary->bylang[0]->content;
+    } else {
+      message=summary->def->content;
+    }
+    
+    if (dumpcount) {
+      chanservsendmessage(sender,"%-20s %u", cmdbuf, cmdlist[i]->calls);
+      continue;
+    }
     
     if (cargc>0 && !match2strings(cargv[0],cmdlist[i]->command->content))
       continue;
@@ -98,6 +161,10 @@ int cs_doshowcommands(void *source, int cargc, char **cargv) {
     if ((cmdlist[i]->level & QCMD_NOTAUTHED) && rup)
       continue;
 
+    if ((cmdlist[i]->level & QCMD_STAFF) && 
+       (!rup || !UHasStaffPriv(rup)))
+      continue;
+
     if ((cmdlist[i]->level & QCMD_HELPER) && 
        (!rup || !UHasHelperPriv(rup)))
       continue;
@@ -114,29 +181,8 @@ int cs_doshowcommands(void *source, int cargc, char **cargv) {
        (!rup || !UIsDev(rup) || !IsOper(sender)))
       continue;
     
-    summary=(cmdsummary *)cmdlist[i]->ext;
-    
-    if (cmdlist[i]->level & QCMD_DEV) {
-      sprintf(cmdbuf,"%s (+d)",cmdlist[i]->command->content);
-    } else if(cmdlist[i]->level & QCMD_ADMIN) {
-      sprintf(cmdbuf,"%s (+a)",cmdlist[i]->command->content);
-    } else if(cmdlist[i]->level & QCMD_OPER) {
-      sprintf(cmdbuf,"%s (+o)",cmdlist[i]->command->content);
-    } else if(cmdlist[i]->level & QCMD_HELPER) {
-      sprintf(cmdbuf,"%s (+h)",cmdlist[i]->command->content);
-    } else {
-      strcpy(cmdbuf, cmdlist[i]->command->content);
-    }
-    
-    if (summary->bylang[lang]) {
-      message=summary->bylang[lang]->content;
-    } else if (summary->bylang[0]) {
-      message=summary->bylang[0]->content;
-    } else {
-      message=summary->def->content;
-    }
-    
-    chanservsendmessage(sender, "%-20s %s",cmdbuf, message);
+    /* We passed all the checks, send the message */    
+    chanservsendmessage(sender, "%-20s %s",ct, message);
   }
 
   chanservstdmessage(sender, QM_ENDOFLIST);
@@ -160,7 +206,8 @@ int cs_sendhelp(nick *sender, char *thecmd, int oneline) {
   rup=getreguserfromnick(sender);
   
   /* Don't showhelp for privileged users to others.. */
-  if (((cmd->level & QCMD_HELPER) && (!rup || !UHasHelperPriv(rup))) ||
+  if (((cmd->level & QCMD_STAFF) && (!rup || !UHasStaffPriv(rup))) ||
+      ((cmd->level & QCMD_HELPER) && (!rup || !UHasHelperPriv(rup))) ||
       ((cmd->level & QCMD_OPER) && (!rup || !UHasOperPriv(rup))) ||
       ((cmd->level & QCMD_ADMIN) && (!rup || !UHasAdminPriv(rup))) ||
       ((cmd->level & QCMD_DEV) && (!rup || !UIsDev(rup)))) {
@@ -172,9 +219,9 @@ int cs_sendhelp(nick *sender, char *thecmd, int oneline) {
 
   if (sum->defhelp && *(sum->defhelp)) {
     if (oneline) {
-      chanservsendmessageoneline(sender, sum->defhelp);
+      chanservsendmessageoneline(sender, "%s", sum->defhelp);
     } else {
-      chanservsendmessage(sender, sum->defhelp);
+      chanservsendmessage(sender, "%s", sum->defhelp);
     }
   } else {
     if (!oneline)
@@ -205,15 +252,15 @@ int cs_doctcpping(void *source, int cargc, char **cargv) {
 }
   
 int cs_doctcpversion(void *source, int cargc, char **cargv) {
-  sendnoticetouser(chanservnick, source, "\01VERSION Q9 version %s (Compiled on " __DATE__ ")  (C) 2002-3 David Mansell (splidge)\01", QVERSION);
-  sendnoticetouser(chanservnick, source, "\01VERSION Built on newserv version 1.00.  (C) 2002-3 David Mansell (splidge)\01");
+  sendnoticetouser(chanservnick, source, "\01VERSION Q9 version %s (Compiled on " __DATE__ ")  (C) 2002-8 David Mansell (splidge) and others.\01", QVERSION);
+  sendnoticetouser(chanservnick, source, "\01VERSION Built on newserv.  (C) 2002-8 David Mansell (splidge) and others.\01");
 
   return CMD_OK;
 }
 
 int cs_doversion(void *source, int cargc, char **cargv) {
-  chanservsendmessage((nick *)source, "Q9 version %s (Compiled on " __DATE__ ") (C) 2002-3 David Mansell (splidge)", QVERSION);
-  chanservsendmessage((nick *)source, "Built on newserv version 1.00.  (C) 2002-3 David Mansell (splidge)");
+  chanservsendmessage((nick *)source, "Q9 version %s (Compiled on " __DATE__ ") (C) 2002-8 David Mansell (splidge) and others.", QVERSION);
+  chanservsendmessage((nick *)source, "Built on newserv.  (C) 2002-8 David Mansell (splidge) and others.");
   return CMD_OK;
 }
 
@@ -223,7 +270,7 @@ int cs_doctcpgender(void *source, int cargc, char **cargv) {
   return CMD_OK;
 }
 
-void csdb_dohelp_real(PGconn *, void *);
+void csdb_dohelp_real(DBConn *, void *);
 
 struct helpinfo {
   unsigned int numeric;
@@ -242,16 +289,16 @@ void csdb_dohelp(nick *np, Command *cmd) {
   hip->cmd=cmd;
 
   q9asyncquery(csdb_dohelp_real, (void *)hip, 
-                 "SELECT languageID, fullinfo from help where lower(command)=lower('%s')",cmd->command->content);
+                 "SELECT languageID, fullinfo from chanserv.help where lower(command)=lower('%s')",cmd->command->content);
 }
 
-void csdb_dohelp_real(PGconn *dbconn, void *arg) {
+void csdb_dohelp_real(DBConn *dbconn, void *arg) {
   struct helpinfo *hip=arg;
   nick *np=getnickbynumeric(hip->numeric);
   reguser *rup;
   char *result;
-  PGresult *pgres;
-  int i,j,num,lang=0;
+  DBResult *pgres;
+  int j,lang=0;
   
   if(!dbconn) {
     freesstring(hip->commandname);
@@ -259,27 +306,25 @@ void csdb_dohelp_real(PGconn *dbconn, void *arg) {
     return; 
   }
 
-  pgres=PQgetResult(dbconn);
+  pgres=dbgetresult(dbconn);
 
-  if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
+  if (!dbquerysuccessful(pgres)) {
     Error("chanserv",ERR_ERROR,"Error loading help text.");
     freesstring(hip->commandname);
     free(hip);
     return; 
   }
 
-  if (PQnfields(pgres)!=2) {
+  if (dbnumfields(pgres)!=2) {
     Error("chanserv",ERR_ERROR,"Help text format error.");
-    PQclear(pgres);
+    dbclear(pgres);
     freesstring(hip->commandname);
     free(hip);
     return;
   }
   
-  num=PQntuples(pgres);
-  
   if (!np) {
-    PQclear(pgres);
+    dbclear(pgres);
     freesstring(hip->commandname);
     free(hip);
     return;
@@ -290,21 +335,21 @@ void csdb_dohelp_real(PGconn *dbconn, void *arg) {
 
   result=NULL;
   
-  for (i=0;i<num;i++) {
-    j=strtoul(PQgetvalue(pgres,i,0),NULL,10);
+  while(dbfetchrow(pgres)) {
+    j=strtoul(dbgetvalue(pgres,0),NULL,10);
     if ((j==0 && result==NULL) || (j==lang)) {
-      result=PQgetvalue(pgres,i,1);
+      result=dbgetvalue(pgres,1);
       if(strlen(result)==0)
        result=NULL;
     }
   }
 
   if (result) {
-    chanservsendmessage(np, result);
+    chanservsendmessage(np, "%s", result);
   } else {
     cmdsummary *sum=hip->cmd->ext;
     if (sum->defhelp && *(sum->defhelp)) {
-      chanservsendmessage(np, sum->defhelp);
+      chanservsendmessage(np, "%s", sum->defhelp);
     } else {
       chanservstdmessage(np, QM_NOHELP, hip->commandname->content);
     }
@@ -312,4 +357,5 @@ void csdb_dohelp_real(PGconn *dbconn, void *arg) {
   
   freesstring(hip->commandname);
   free(hip);
+  dbclear(pgres);
 }