080a160c7b9595d516c9c70e83689a09b60839d0
galt
  Mon Jun 3 12:16:53 2013 -0700
fix SQL Injection
diff --git src/hg/dbTrash/dbTrash.c src/hg/dbTrash/dbTrash.c
index b06a6b8..79c183b 100644
--- src/hg/dbTrash/dbTrash.c
+++ src/hg/dbTrash/dbTrash.c
@@ -72,51 +72,51 @@
 {
 char query[256];
 struct sqlResult *sr;
 char **row;
 char buffer[4 * 1024];
 char *name = buffer;
 struct slName *list = NULL;
 
 if (! sqlTableExists(conn, CT_EXTFILE))
     {
     verbose(2,"WARNING: -extFile option specified, extFile table does not exist\n");
     verbose(2,"at this time (Jan 2009), the extFile table is unused.\n");
     return;
     }
 
-safef(query,sizeof(query),"select id,path from %s",CT_EXTFILE);
+sqlSafef(query,sizeof(query),"select id,path from %s",CT_EXTFILE);
 sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     {
     if (topDir != NULL)
 	safef(buffer, sizeof buffer, "%s/%s",topDir, row[1]);
     else
 	name = row[1];
 
     if (!fileExists(name))
 	{
 	struct slName *new = newSlName(row[0]);
 	slAddHead(&list, new);
 	}
     }
 sqlFreeResult(&sr);
 
 struct slName *one;
 for(one = list; one; one = one->next)
     {
-    safef(query,sizeof(query),"delete from %s where id='%s'",
+    sqlSafef(query,sizeof(query),"delete from %s where id='%s'",
 	CT_EXTFILE, one->name);
     if (extDel)
 	sqlUpdate(conn, query);
     verbose(2,"%s\n",query);
     }
 slFreeList(&list);
 }
 
 // Macro some common code used twice below
 
 #define STATUS_INIT \
 sr = sqlGetResult(conn, query); \
 nameIx = sqlFieldColumn(sr, "Name"); \
 createTimeIx = sqlFieldColumn(sr, "Create_time"); \
 updateTimeIx = sqlFieldColumn(sr, "Update_time"); \
@@ -165,47 +165,47 @@
 int nameIx;
 int timeIxUsed;
 unsigned long long totalSize = 0;
 // expiredTableNames: table exists and is in metaInfo and subject to age limits
 struct slName *expiredTableNames = NULL;
 struct slName *lostTables = NULL;	// tables existing but not in metaInfo
 unsigned long long lostTableCount = 0;
 struct hash *expiredHash = newHash(10); // as determined by metaInfo
 struct hash *notExpiredHash = newHash(10);
 struct sqlConnection *conn = sqlConnect(db);
 
 if (extFileCheck)
     checkExtFile(conn);
 
 time_t ageSeconds = (time_t)(ageHours * 3600);	/*	age in seconds	*/
-safef(query,sizeof(query),"select name,UNIX_TIMESTAMP(lastUse) from %s WHERE "
+sqlSafef(query,sizeof(query),"select name,UNIX_TIMESTAMP(lastUse) from %s WHERE "
     "lastUse < DATE_SUB(NOW(), INTERVAL %ld SECOND);", CT_META_INFO,ageSeconds);
 sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     hashAddInt(expiredHash, row[0], sqlSigned(row[1]));
 sqlFreeResult(&sr);
-safef(query,sizeof(query),"select name,UNIX_TIMESTAMP(lastUse) from %s WHERE "
+sqlSafef(query,sizeof(query),"select name,UNIX_TIMESTAMP(lastUse) from %s WHERE "
     "lastUse >= DATE_SUB(NOW(), INTERVAL %ld SECOND);",CT_META_INFO,ageSeconds);
 sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     hashAddInt(notExpiredHash, row[0], sqlSigned(row[1]));
 sqlFreeResult(&sr);
 
 if (tableStatus)  // show table status is very expensive, use only when asked
     {
     /*	run through the table status business to get table size information */
-    safef(query,sizeof(query),"show table status");
+    sqlSafef(query,sizeof(query),"show table status");
     STATUS_INIT;
     while ((row = sqlNextRow(sr)) != NULL)
 	{
 	/* if not doing history too, and this is the history table, next row */
 	if ((!historyToo) && (sameWord(row[nameIx],"history")))
 	    continue;
 	/* also skip the metaInfo table */
 	if ((!historyToo) && (sameWord(row[nameIx],CT_META_INFO)))
 	    continue;
 	/* don't delete the extFile table  */
 	if (sameWord(row[nameIx],CT_EXTFILE))
 	    continue;
 
 	SCAN_STATUS;
 
@@ -237,31 +237,31 @@
 		    if ( ((char *)NULL != row[dataLengthIx]) &&
 			((char *)NULL != row[indexLengthIx]) )
 			    totalSize += sqlLongLong(row[dataLengthIx])
 				+ sqlLongLong(row[indexLengthIx]);
 		    }
 		else
 		    verbose(3,"%s %ld OKt %s\n",row[timeIxUsed],
 			(unsigned long)timep, row[nameIx]);
 		}
 	    }
 	}
     sqlFreeResult(&sr);
     }
 else
     {	// simple 'show tables' is more efficient than 'show table status'
-    safef(query,sizeof(query),"show tables");
+    sqlSafef(query,sizeof(query),"show tables");
     sr = sqlGetResult(conn, query);
     while ((row = sqlNextRow(sr)) != NULL)
         {
 	if (hashLookup(expiredHash,row[0]))
 	    {
 	    slNameAddHead(&expiredTableNames, row[0]);
 	    time_t lastUse = (time_t)hashIntVal(expiredHash,row[0]);
 	    struct tm *lastUseTm = localtime(&lastUse);
 	    verbose(3,"%4d-%02d-%02d %02d:%02d:%02d %ld drop %s\n",
 		lastUseTm->tm_year+1900, lastUseTm->tm_mon+1,
 		lastUseTm->tm_mday, lastUseTm->tm_hour, lastUseTm->tm_min,
 		lastUseTm->tm_sec, (unsigned long)lastUse,row[0]);
 	    hashRemove(expiredHash, row[0]);
 	    }
 	else if (hashLookup(notExpiredHash,row[0]))
@@ -287,31 +287,31 @@
     // do this operation with the stand-alone perl script on the customTrash
     // database machine.
     if (delLostTable && lostTables)
 	{
 	struct slName *el;
 	for (el = lostTables; el != NULL; el = el->next)
 	    {
 	    if (sameWord(el->name,"history"))
 		continue;
 	    if (sameWord(el->name,CT_META_INFO))
 		continue;
 	    if (sameWord(el->name,CT_EXTFILE))
 		continue;
 	    boolean oneTableOnly = FALSE; // protect against multiple tables
 	    /*	get table time information to see if it is expired */
-	    safef(query,sizeof(query),"show table status like '%s'", el->name);
+	    sqlSafef(query,sizeof(query),"show table status like '%s'", el->name);
 	    STATUS_INIT;
 
 	    while ((row = sqlNextRow(sr)) != NULL)
 		{
 		if (oneTableOnly)
 		    errAbort("ERROR: query: '%s' returned more than one table "
 				"name\n", query);
 		else
 		    oneTableOnly = TRUE;
 		if (differentWord(row[nameIx], el->name))
 		    errAbort("ERROR: query: '%s' did not return table name '%s' != '%s'\n", query, el->name, row[nameIx]);
 
 		SCAN_STATUS;
 
 		if (timep < dropTime)