Browse Source

Ensure null termination of strings in SNMP code

Nathan Kinder 20 years ago
parent
commit
c62623c876

+ 12 - 6
ldap/servers/slapd/agtmmap.c

@@ -356,17 +356,23 @@ agt_mread_stats (int hdl, struct hdr_stats_t *pHdrInfo, struct ops_stats_t *pDsO
         pHdrInfo->startTime                = pfile_stats->hdr_stats.startTime;
         pHdrInfo->updateTime               = pfile_stats->hdr_stats.updateTime;
         strncpy(pHdrInfo->dsVersion, pfile_stats->hdr_stats.dsVersion,
-               (sizeof(pHdrInfo->dsVersion)/sizeof(char)) - 1);
+               SNMP_FIELD_LENGTH - 1);
+        pHdrInfo->dsVersion[SNMP_FIELD_LENGTH - 1] = (char)0;
         strncpy(pHdrInfo->dsName, pfile_stats->hdr_stats.dsName,
-               (sizeof(pHdrInfo->dsName)/sizeof(char)) - 1);
+               SNMP_FIELD_LENGTH - 1);
+        pHdrInfo->dsName[SNMP_FIELD_LENGTH - 1] = (char)0;
         strncpy(pHdrInfo->dsDescription, pfile_stats->hdr_stats.dsDescription,
-               (sizeof(pHdrInfo->dsDescription)/sizeof(char)) - 1);
+               SNMP_FIELD_LENGTH - 1);
+        pHdrInfo->dsDescription[SNMP_FIELD_LENGTH - 1] = (char)0;
         strncpy(pHdrInfo->dsOrganization, pfile_stats->hdr_stats.dsOrganization,
-               (sizeof(pHdrInfo->dsOrganization)/sizeof(char)) - 1);
+               SNMP_FIELD_LENGTH - 1);
+        pHdrInfo->dsOrganization[SNMP_FIELD_LENGTH - 1] = (char)0;
         strncpy(pHdrInfo->dsLocation, pfile_stats->hdr_stats.dsLocation,
-               (sizeof(pHdrInfo->dsLocation)/sizeof(char)) - 1);
+               SNMP_FIELD_LENGTH - 1);
+        pHdrInfo->dsLocation[SNMP_FIELD_LENGTH - 1] = (char)0;
         strncpy(pHdrInfo->dsContact, pfile_stats->hdr_stats.dsContact,
-               (sizeof(pHdrInfo->dsContact)/sizeof(char)) - 1);
+               SNMP_FIELD_LENGTH - 1);
+        pHdrInfo->dsContact[SNMP_FIELD_LENGTH - 1] = (char)0;
     }
 
     if (pDsOpsTbl != NULL) {

+ 10 - 8
ldap/servers/slapd/agtmmap.h

@@ -33,6 +33,8 @@ extern "C" {
 
 
 #define NUM_SNMP_INT_TBL_ROWS 5
+#define SNMP_FIELD_LENGTH 100
+
 #ifndef  _WIN32
 extern int			errno;
 #endif
@@ -71,12 +73,12 @@ struct hdr_stats_t{
     int		restarted; /* 1/0 = Yes/No */
     time_t	startTime;
     time_t	updateTime;
-    char        dsVersion[100];
-    char	dsName[30];
-    char	dsDescription[30];
-    char	dsOrganization[30];
-    char	dsLocation[30];
-    char	dsContact[30];
+    char        dsVersion[SNMP_FIELD_LENGTH];
+    char	dsName[SNMP_FIELD_LENGTH];
+    char	dsDescription[SNMP_FIELD_LENGTH];
+    char	dsOrganization[SNMP_FIELD_LENGTH];
+    char	dsLocation[SNMP_FIELD_LENGTH];
+    char	dsContact[SNMP_FIELD_LENGTH];
 };
 
 struct ops_stats_t{
@@ -129,14 +131,14 @@ struct int_stats_t
      *   Interaction Table Attributes
      */
     PRUint32 dsIntIndex;
-    char     dsName[100];
+    char     dsName[SNMP_FIELD_LENGTH];
     time_t   dsTimeOfCreation;         
     time_t   dsTimeOfLastAttempt;      
     time_t   dsTimeOfLastSuccess;      
     PRUint32 dsFailuresSinceLastSuccess;
     PRUint32 dsFailures;
     PRUint32 dsSuccesses;
-    char     dsURL[100];
+    char     dsURL[SNMP_FIELD_LENGTH];
 };
 
 struct agt_stats_t

+ 5 - 10
ldap/servers/slapd/snmp_collator.c

@@ -634,28 +634,23 @@ loadConfigStats() {
 
 	/* Load stats into table */
         if ( name != NULL) {
-		strncpy(stats->hdr_stats.dsName, name,
-			(sizeof(stats->hdr_stats.dsName)/sizeof(char)) - 1);
+		PL_strncpyz(stats->hdr_stats.dsName, name, SNMP_FIELD_LENGTH);
         }
 
 	if ( desc != NULL) {
-		strncpy(stats->hdr_stats.dsDescription, desc,
-			(sizeof(stats->hdr_stats.dsDescription)/sizeof(char)) - 1);
+		PL_strncpyz(stats->hdr_stats.dsDescription, desc, SNMP_FIELD_LENGTH);
 	}
 
 	if ( org != NULL) {
-		strncpy(stats->hdr_stats.dsOrganization, org,
-			(sizeof(stats->hdr_stats.dsOrganization)/sizeof(char)) - 1);
+		PL_strncpyz(stats->hdr_stats.dsOrganization, org, SNMP_FIELD_LENGTH);
 	}
 
 	if ( loc != NULL) {
-		strncpy(stats->hdr_stats.dsLocation, loc,
-			(sizeof(stats->hdr_stats.dsLocation)/sizeof(char)) - 1);
+		PL_strncpyz(stats->hdr_stats.dsLocation, loc, SNMP_FIELD_LENGTH);
 	}
 
 	if ( contact != NULL) {
-		strncpy(stats->hdr_stats.dsContact, contact,
-			(sizeof(stats->hdr_stats.dsContact)/sizeof(char)) - 1);
+		PL_strncpyz(stats->hdr_stats.dsContact, contact, SNMP_FIELD_LENGTH);
 	}
 
 	/* Free strings */

+ 7 - 2
ldap/servers/snmp/main.c

@@ -39,7 +39,7 @@ main (int argc, char *argv[]) {
 
     /* Load options */
     while ((--argc > 0) && ((*++argv)[0] == '-')) {
-        while (c = *++argv[0]) {
+        while ((c = *++argv[0])) {
             switch (c) {
             case 'D':
                 log_level = LOG_DEBUG;
@@ -109,6 +109,7 @@ main (int argc, char *argv[]) {
                 if (*(agent_logdir + strlen(agent_logdir)) != '/')
                     strcat((char *) log_hdl->token, "/");
                 strcat((char *) log_hdl->token, LDAP_AGENT_LOGFILE);
+                ((char*)log_hdl->token)[(strlen(agent_logdir) + strlen(LDAP_AGENT_LOGFILE) + 1)] = (char)0;
             }
         } else {
             /* agent-logdir not set */
@@ -134,7 +135,7 @@ main (int argc, char *argv[]) {
     /* run as a daemon */
     if (netsnmp_daemonize(0, 0)) {
         /* sleep to allow pidfile to be created by child */
-        sleep(3);
+        sleep(5);
         if((pid_fp = fopen(pidfile,"r")) == NULL) {
             printf("ldap-agent: Not started!  Check log file for details.\n");
             exit(1);
@@ -224,6 +225,7 @@ load_config(char *conf_path)
                                    strlen(LDAP_AGENT_PIDFILE) + 2)) != NULL) {
                 strncpy(pidfile, conf_path, (p - conf_path + 1));
                 strcat(pidfile, LDAP_AGENT_PIDFILE);
+                pidfile[((p - conf_path) + strlen(LDAP_AGENT_PIDFILE) + 1)] = (char)0;
             } else {
                 printf("ldap-agent: malloc error processing config file\n");
                 exit(1);
@@ -232,6 +234,7 @@ load_config(char *conf_path)
             /* set default logdir to location of config file */
             if ((agent_logdir = malloc((p - conf_path) + 1)) != NULL) {
                 strncpy(agent_logdir, conf_path, (p - conf_path));
+                agent_logdir[(p - conf_path)] = (char)0;
                 break;
             } else {
                 printf("ldap-agent: malloc error processing config file\n");
@@ -278,8 +281,10 @@ load_config(char *conf_path)
                 if ((serv_p->stats_file = malloc(strlen(p) + 18)) != NULL)
                     snprintf(serv_p->stats_file, strlen(p) + 18,
                                      "%s/logs/slapd.stats", p);
+                    serv_p->stats_file[(strlen(p) + 17)] = (char)0;
                 if ((serv_p->dse_ldif = malloc(strlen(p) + 17)) != NULL) {
                     snprintf(serv_p->dse_ldif, strlen(p) + 17, "%s/config/dse.ldif", p);
+                    serv_p->dse_ldif[(strlen(p) + 16)] = (char)0;
                 }
             }