diff --git a/import/log4c/log4c.h b/import/log4c/log4c.h index f7fb45d..655d1d4 100644 --- a/import/log4c/log4c.h +++ b/import/log4c/log4c.h @@ -62,8 +62,10 @@ #define LOGIP(X) inet_ntoa(X.sin_addr) +#define LogAssert(test, message, ...) if((test)) { Log(LOG_ERROR, "Assert:%s:%d " message, __FILE__, __LINE__, __VA_ARGS__); } #define Log(...) LogMsg(LOGGERNAME, 0, __VA_ARGS__) #define LogWithID(...) LogMsg(LOGGERNAME, __VA_ARGS__) +#define LogStart() LoggerOpen(LOGGERNAME); #define LogStartup() log4c_init() #define LogShutdown() log4c_fini() diff --git a/src/agents/queue/queue.c b/src/agents/queue/queue.c index 78cad9f..4011589 100644 --- a/src/agents/queue/queue.c +++ b/src/agents/queue/queue.c @@ -236,9 +236,7 @@ SpoolEntryIDLock(unsigned long id) XplSignalLocalSemaphore(Queue.spoolLocks.semaphores[SPOOL_LOCK_ARRAY_MASK & id]); - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_PQE_LOCK_FULL, LOG_INFO, 0, NULL, NULL, id, 0, NULL, 0); - - XplConsolePrintf("bongoqueue: Unable to lock spool entry %x; table full.\r\n", (unsigned int)id); + Log(LOG_INFO, "Unable to lock spool entry %x, table full", (unsigned int) id); } return(NULL); @@ -300,7 +298,7 @@ AddPushAgent(QueueClient *client, int count; QueuePushClient *temp; - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_ADD_QUEUE_AGENT, LOG_INFO, 0, NULL, NULL, XplHostToLittle(client->conn->socketAddress.sin_addr.s_addr), queue, &port, sizeof(port)); + Log(LOG_INFO, "Adding client on host %s:%d to queue %d", LOGIP(client->conn->socketAddress), port, queue); XplMutexLock(Queue.pushClients.lock); @@ -317,10 +315,7 @@ AddPushAgent(QueueClient *client, Queue.pushClients.array = temp; Queue.pushClients.allocated += PUSHCLIENTALLOC; } else { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_NMAP_OUT_OF_MEMORY, LOG_CRITICAL, 0, "Queue", identifier, (Queue.pushClients.allocated + PUSHCLIENTALLOC) * sizeof(QueuePushClient), 0, NULL, 0); - - XplConsolePrintf("bongoqueue: Out of memory processing user %s, mailbox %s; request size: %d bytes\r\n", "bongoqueue:AddPushAgent", - identifier, (int)((Queue.pushClients.allocated + PUSHCLIENTALLOC) * sizeof(QueuePushClient))); + LogAssert(TRUE, "Out of memory processing mailbox %s", identifier); return(-1); } } @@ -360,9 +355,9 @@ RemovePushAgentIndex(int index, BOOL force) if ((Queue.pushClients.array[index].errorCount > MAX_PUSHCLIENTS_ERRORS) || force) { if (force) { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_REREGISTER_QUEUE_AGENT, LOG_INFO, 0, NULL, NULL, XplHostToLittle(Queue.pushClients.array[index].address), Queue.pushClients.array[index].queue, &(Queue.pushClients.array[index].port), sizeof(Queue.pushClients.array[index].port)); + Log(LOG_INFO, "Reregistered queue agent"); } else { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_REMOVED_QUEUE_AGENT, LOG_INFO, 0, NULL, NULL, XplHostToLittle(Queue.pushClients.array[index].address), Queue.pushClients.array[index].queue, &(Queue.pushClients.array[index].port), sizeof(Queue.pushClients.array[index].port)); + Log(LOG_INFO, "Removed queue agent"); } if (index < (Queue.pushClients.count - 1)) { @@ -647,7 +642,7 @@ StartOver: entryID = strtol(entry, NULL, 16); - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_QUEUE, LOGGER_EVENT_PQE_START, LOG_DEBUG, entryID, NULL, NULL, queue, entryID, NULL, 0); + Log(LOG_DEBUG, "Processing entry %ld on queue %d", entryID, queue); idLock = SpoolEntryIDLock(entryID); if (idLock) { @@ -868,7 +863,7 @@ StartOver: MDBFreeValues(vs); } else { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_QUEUE, LOGGER_EVENT_PQE_FIND_OBJECT_FAILED, LOG_INFO, entryID, cur + 1, "", queue, 0, NULL, 0); + Log(LOG_INFO, "Entry %ld queue %d, can't find %s", entryID, queue, cur + 1); if (ptr2) { *ptr2 = ' '; @@ -937,13 +932,13 @@ StartOver: if (!newFH) { sprintf(path, "%s/w%s.%03d",Conf.spoolPath, entry, queue); - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_FILE_OPEN_FAILURE, LOG_CRITICAL, entryID, path, __FILE__, __LINE__, 0, NULL, 0); + LogAssert(TRUE, "File open failure. Entry %ld, path %s", entryID, path); } else if (!qEnvelope) { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_NMAP_OUT_OF_MEMORY, LOG_CRITICAL, entryID, __FILE__, NULL, sb.st_size, 0, NULL, 0); + LogAssert(TRUE, "Out of memory. Entry %ld, size %ld", entryID, sb.st_size); } else if (!fh) { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_FILE_OPEN_FAILURE, LOG_CRITICAL, entryID, path, __FILE__, __LINE__, 0, NULL, 0); + LogAssert(TRUE, "File open failure. Entry %ld, path %s", entryID, path); } else { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_FILE_OPEN_FAILURE, LOG_CRITICAL, entryID, path, __FILE__, __LINE__, 0, NULL, 0); + LogAssert(TRUE, "Event file open failure. Entry %ld, path %s", entryID, path); } if (qEnvelope) { @@ -958,7 +953,7 @@ StartOver: fclose(newFH); } - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_WRITE_ERROR, LOG_WARNING, 0, "Spool", "Queue", __LINE__, 0, NULL, 0); + Log(LOG_WARNING, "Write error in queue"); sprintf(path, "%s/w%s.%03d",Conf.spoolPath, entry, queue); unlink(path); @@ -1075,8 +1070,6 @@ StartOver: authenticatedSender[1]='\0'; } - /* LoggerEvent(NMAP.handle.logging, LOGGER_SUBSYSTEM_QUEUE, LOGGER_EVENT_PQE_FROM, LOG_DEBUG, entryID, entry, sender, queue, 0, MIME_TEXT_PLAIN, authenticatedSender? strlen(authenticatedSender) + 1: 1, authenticatedSender? (char *)authenticatedSender: "", NULL, 0); */ - fprintf(newFH, "%s\r\n", line); break; } @@ -1117,11 +1110,10 @@ StartOver: vs = MDBCreateValueStruct(Agent.agent.directoryHandle, NULL); if (!MsgFindObject(recipient, NULL, NULL, &siaddr, vs)) { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_QUEUE, LOGGER_EVENT_PQE_FIND_OBJECT_FAILED, LOG_WARNING, entryID, recipient, "", queue, 0, NULL, 0); - + Log(LOG_WARNING, "User %s unknown, entry %ld", recipient, entryID); status = DELIVER_USER_UNKNOWN; } else { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_QUEUE, LOGGER_EVENT_PQE_REMOTE_NMAP, LOG_DEBUG, entryID, entry, line + 1, queue, XplHostToLittle(siaddr.sin_addr.s_addr), NULL, 0); + Log(LOG_DEBUG, "Delivering %s on queue %d to %s", entry, queue, line+1); status = DeliverToStore(&list, &siaddr, NMAP_DOCTYPE_CAL, sender, authenticatedSender, dataFilename, data, dSize, recipient, mailbox, flags); if (Agent.agent.state == BONGO_AGENT_STATE_STOPPING) { status = DELIVER_TRY_LATER; @@ -1135,7 +1127,7 @@ StartOver: } if (statusconn, 0); ConnFree(client->conn); @@ -1598,7 +1590,7 @@ StartOver: client->entry.report = report; if (!HandleCommand(client)) { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_QUEUE, LOGGER_EVENT_PQE_FAILED, LOG_WARNING, entryID, entry, NULL, queue, XplHostToLittle(saddr.sin_addr.s_addr), &(Queue.pushClients.array[used].port), sizeof(Queue.pushClients.array[used].port)); + LogAssert(TRUE, "Couldn't handle command on entry %s for host %s", entry, LOGIP(saddr)); } /* fixme - evaluate this section. @@ -1723,9 +1715,7 @@ StartOver: if (ptr2) { if ((handle = QDBHandleAlloc()) != NULL) { ccode = QDBAdd(handle, ptr2 + 1, entryID, queue); - if (ccode == -1) { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_DATABASE, LOGGER_EVENT_DATABASE_INSERT_ERROR, LOG_ERROR, 0, ptr2 + 1, NULL, ccode, 0, NULL, 0); - } + LogAssert(ccode != -1, "Database insert error: %s", ptr2 + 1); QDBHandleRelease(handle); } @@ -1797,7 +1787,7 @@ StartOver: fclose(data); } - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_FILE_OPEN_FAILURE, LOG_CRITICAL, entryID, path, __FILE__, __LINE__, 0, NULL, 0); + LogAssert(TRUE, "File open failure: entry %ld, path %s", entryID, path); ProcessQueueEntryCleanUp(idLock, report); return(TRUE); @@ -1891,7 +1881,7 @@ StartOver: HandleDSN(data, fh); fclose(fh); } else { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_FILE_OPEN_FAILURE, LOG_CRITICAL, entryID, path, __FILE__, __LINE__, 0, NULL, 0); + LogAssert(TRUE, "Couldn't open path %s (%ld)", path, entryID); } fclose(data); @@ -4213,11 +4203,11 @@ CommandQsrchDomain(void *param) ccode = ConnWrite(client->conn, MSG1000OK, sizeof(MSG1000OK) - 1); } } else { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_DATABASE, LOGGER_EVENT_DATABASE_FIND_ERROR, LOG_ERROR, 0, ptr, NULL, ccode, 0, NULL, 0); + LogAssert(TRUE, "Couldn't find %s in QDB", ptr); ccode = ConnWrite(client->conn, MSG4261NODOMAIN, sizeof(MSG4261NODOMAIN) - 1); } } else { - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_NMAP_OUT_OF_MEMORY, LOG_CRITICAL, 0, client->buffer, NULL, sizeof(MDBValueStruct), 0, NULL, 0); + LogAssert(TRUE, "Out of memory", ptr); ccode = ConnWrite(client->conn, MSG5230NOMEMORYERR, sizeof(MSG5230NOMEMORYERR) - 1); } @@ -4776,6 +4766,6 @@ CommandQflush(void *param) QueueClient *client = (QueueClient *)param; Queue.flushNeeded = TRUE; - + return (ConnWrite(client->conn, MSG1000OK, sizeof(MSG1000OK) - 1)); } diff --git a/src/agents/queue/queue.h b/src/agents/queue/queue.h index 4e74642..c1671be 100644 --- a/src/agents/queue/queue.h +++ b/src/agents/queue/queue.h @@ -22,8 +22,8 @@ #ifndef QUEUE_H #define QUEUE_H -#include "conf.h" #include "queued.h" +#include "conf.h" #define SPOOL_LOCK_ARRAY_SIZE 256 #define SPOOL_LOCK_IDARRAY_SIZE 64 diff --git a/src/agents/queue/queued.c b/src/agents/queue/queued.c index 4f37bdc..95eee42 100644 --- a/src/agents/queue/queued.c +++ b/src/agents/queue/queued.c @@ -21,9 +21,11 @@ #include +#include "queue.h" +#include "queued.h" + #include #include -#include #include #include #include @@ -32,9 +34,7 @@ #include #include -#include "queued.h" #include "conf.h" -#include "queue.h" #include "domain.h" #include "messages.h" @@ -122,7 +122,7 @@ CommandAuth(void *param) return(ConnWrite(client->conn, MSG1000OK, sizeof(MSG1000OK) - 1)); } - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_AUTH, LOGGER_EVENT_WRONG_SYSTEM_AUTH, LOG_NOTICE, 0, NULL, NULL, XplHostToLittle(client->conn->socketAddress.sin_addr.s_addr), 0, NULL, 0); + Log(LOG_NOTICE, "SYSTEM AUTH failed from host %s", LOGIP(client->conn->socketAddress)); ConnWrite(client->conn, MSG3242BADAUTH, sizeof(MSG3242BADAUTH) - 1); @@ -174,7 +174,7 @@ CommandPass(void *param) return(ConnWrite(client->conn, MSG4120USERLOCKED, sizeof(MSG4120USERLOCKED) - 1)); } - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_AUTH, LOGGER_EVENT_WRONG_USER_AUTH, LOG_NOTICE, 0, ptr, NULL, XplHostToLittle(client->conn->socketAddress.sin_addr.s_addr), 0, NULL, 0); + Log(LOG_NOTICE, "PASS USER failed for user from host %s", LOGIP(client->conn->socketAddress)); } else if ( (toupper(ptr[0]) == 'S') && (toupper(ptr[1]) == 'Y') && (toupper(ptr[2]) == 'S') @@ -192,7 +192,7 @@ CommandPass(void *param) } #endif - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_AUTH, LOGGER_EVENT_WRONG_SYSTEM_AUTH, LOG_NOTICE, 0, NULL, NULL, XplHostToLittle(client->conn->socketAddress.sin_addr.s_addr), 0, NULL, 0); + Log(LOG_NOTICE, "PASS SYSTEM failed from host %s", LOGIP(client->conn->socketAddress)); } } @@ -308,8 +308,7 @@ HandleCommand(QueueClient *client) ccode = ConnWrite(client->conn, MSG3240NOAUTH, sizeof(MSG3240NOAUTH) - 1); } - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_UNHANDLED, LOGGER_EVENT_UNHANDLED_REQUEST, LOG_INFO, 0, "Queue", - client->buffer, XplHostToLittle(client->conn->socketAddress.sin_addr.s_addr), 0, NULL, 0); + Log(LOG_INFO, "Handled command from %s", LOGIP(client->conn->socketAddress)); } } else if (ccode == -1) { break; @@ -456,24 +455,26 @@ CheckDiskspace(BongoAgent *agent) { unsigned long freeBlocks; unsigned long bytesPerBlock; + unsigned long wantFree; bytesPerBlock = XplGetDiskBlocksize(Conf.spoolPath); freeBlocks = XplGetDiskspaceFree(Conf.spoolPath); + wantFree = (Conf.minimumFree / bytesPerBlock) + 1; if (freeBlocks != 0x7f000000) { - if (freeBlocks < ((Conf.minimumFree / bytesPerBlock) + 1)) { + if (freeBlocks < wantFree) { /* BUG ALERT: */ /* With a block size of 4KB, freeBlocks could */ /* overflow if a volume had more than 16 TB. */ Agent.flags |= QUEUE_AGENT_DISK_SPACE_LOW; - LoggerEvent(Agent.agent.loggingHandle, LOGGER_SUBSYSTEM_GENERAL, LOGGER_EVENT_DISKSPACE_LOW, LOG_ERROR, 0, Conf.spoolPath, NULL, freeBlocks, (Conf.minimumFree/bytesPerBlock)+1, NULL, 0); + Log(LOG_ERROR, "Disk space for %s low: have %ld free, want %ld.", Conf.spoolPath, freeBlocks, wantFree); } else { Agent.flags &= ~QUEUE_AGENT_DISK_SPACE_LOW; } } else { - XplConsolePrintf("bongoqueue: The OS failed to respond to a disk space query\r\n"); + Log(LOG_NOTICE, "Couldn't work out disk space free for %s", Conf.spoolPath); } } @@ -535,6 +536,8 @@ XplServiceMain(int argc, char *argv[]) int ccode; int startupOpts; + LogStart(); + if (XplSetRealUser(MsgGetUnprivilegedUser()) < 0) { XplConsolePrintf(AGENT_NAME ": Could not drop to unprivileged user '%s'\r\n" AGENT_NAME ": exiting.\n", MsgGetUnprivilegedUser()); return -1; @@ -591,6 +594,8 @@ XplServiceMain(int argc, char *argv[]) /* Start the server thread */ XplStartMainThread(AGENT_NAME, &id, QueueServer, 8192, NULL, ccode); + + LogShutdown(); XplUnloadApp(XplGetThreadID()); diff --git a/src/agents/queue/queued.h b/src/agents/queue/queued.h index ef3d575..ff413cc 100644 --- a/src/agents/queue/queued.h +++ b/src/agents/queue/queued.h @@ -22,6 +22,8 @@ #ifndef QUEUED_H #define QUEUED_H +#define LOGGERNAME "queue" + #include #include #include