git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@294 77e5149b-7576-45b1-b177-96237e5ba77b
Nigel Horne authored on 2004/02/16 20:57:27... | ... |
@@ -1,3 +1,8 @@ |
1 |
+Mon Feb 16 12:00:06 GMT 2004 (njh) |
|
2 |
+---------------------------------- |
|
3 |
+ * clamav-milter: Added clamfi_free, this helps to ease the task |
|
4 |
+ of writing cleanups for unexpected errors |
|
5 |
+ |
|
1 | 6 |
Mon Feb 16 09:44:34 GMT 2004 (njh) |
2 | 7 |
---------------------------------- |
3 | 8 |
* clamav-milter: Upissued to 0.67 |
... | ... |
@@ -228,9 +228,13 @@ |
228 | 228 |
* Removed duplication in version string |
229 | 229 |
* Handle machines that don't have in_port_t |
230 | 230 |
* 0.67 16/2/04 Upissued to 0.67 |
231 |
+ * 0.67a 16/2/04 Added clamfi_free |
|
231 | 232 |
* |
232 | 233 |
* Change History: |
233 | 234 |
* $Log: clamav-milter.c,v $ |
235 |
+ * Revision 1.47 2004/02/16 11:55:24 nigelhorne |
|
236 |
+ * Added clamfi_free which helps with the tidying up |
|
237 |
+ * |
|
234 | 238 |
* Revision 1.46 2004/02/16 09:39:22 nigelhorne |
235 | 239 |
* Upissued to 0.67 |
236 | 240 |
* |
... | ... |
@@ -354,9 +358,9 @@ |
354 | 354 |
* Revision 1.6 2003/09/28 16:37:23 nigelhorne |
355 | 355 |
* Added -f flag use MaxThreads if --max-children not set |
356 | 356 |
*/ |
357 |
-static char const rcsid[] = "$Id: clamav-milter.c,v 1.46 2004/02/16 09:39:22 nigelhorne Exp $"; |
|
357 |
+static char const rcsid[] = "$Id: clamav-milter.c,v 1.47 2004/02/16 11:55:24 nigelhorne Exp $"; |
|
358 | 358 |
|
359 |
-#define CM_VERSION "0.67" |
|
359 |
+#define CM_VERSION "0.67a" |
|
360 | 360 |
|
361 | 361 |
/*#define CONFDIR "/usr/local/etc"*/ |
362 | 362 |
|
... | ... |
@@ -479,6 +483,7 @@ static sfsistat clamfi_eom(SMFICTX *ctx); |
479 | 479 |
static sfsistat clamfi_abort(SMFICTX *ctx); |
480 | 480 |
static sfsistat clamfi_close(SMFICTX *ctx); |
481 | 481 |
static void clamfi_cleanup(SMFICTX *ctx); |
482 |
+static void clamfi_free(struct privdata *privdata); |
|
482 | 483 |
static int clamfi_send(const struct privdata *privdata, size_t len, const char *format, ...); |
483 | 484 |
static char *strrcpy(char *dest, const char *source); |
484 | 485 |
static int clamd_recv(int sock, char *buf, size_t len); |
... | ... |
@@ -1448,7 +1453,7 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1448 | 1448 |
} |
1449 | 1449 |
} |
1450 | 1450 |
|
1451 |
- privdata = (struct privdata *)calloc(1, sizeof(struct privdata)); |
|
1451 |
+ privdata = (struct privdata *)cli_calloc(1, sizeof(struct privdata)); |
|
1452 | 1452 |
privdata->dataSocket = -1; /* 0.4 */ |
1453 | 1453 |
privdata->cmdSocket = -1; /* 0.4 */ |
1454 | 1454 |
|
... | ... |
@@ -1459,18 +1464,17 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1459 | 1459 |
*/ |
1460 | 1460 |
int ntries = 5; |
1461 | 1461 |
|
1462 |
- privdata->filename = malloc(strlen(quarantine_dir) + 12); |
|
1462 |
+ privdata->filename = (char *)cli_malloc(strlen(quarantine_dir) + 12); |
|
1463 | 1463 |
|
1464 | 1464 |
do { |
1465 | 1465 |
sprintf(privdata->filename, "%s/msg.XXXXXX", quarantine_dir); |
1466 |
-#if defined(C_LINUX) || defined(C_BSD) |
|
1466 |
+#if defined(C_LINUX) || defined(C_BSD) || defined(HAVE_MKSTEMP) || defined(C_SOLARIS) |
|
1467 | 1467 |
privdata->dataSocket = mkstemp(privdata->filename); |
1468 | 1468 |
#else |
1469 | 1469 |
if(mktemp(privdata->filename) == NULL) { |
1470 | 1470 |
if(use_syslog) |
1471 | 1471 |
syslog(LOG_ERR, "mktemp %s failed", privdata->filename); |
1472 |
- free(privdata->filename); |
|
1473 |
- privdata->filename = NULL; |
|
1472 |
+ clamfi_free(privdata); |
|
1474 | 1473 |
return cl_error; |
1475 | 1474 |
} |
1476 | 1475 |
privdata->dataSocket = open(privdata->filename, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC, 0600); |
... | ... |
@@ -1480,8 +1484,7 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1480 | 1480 |
if(privdata->dataSocket < 0) { |
1481 | 1481 |
if(use_syslog) |
1482 | 1482 |
syslog(LOG_ERR, "tempfile %s creation failed", privdata->filename); |
1483 |
- free(privdata->filename); |
|
1484 |
- privdata->filename = NULL; |
|
1483 |
+ clamfi_free(privdata); |
|
1485 | 1484 |
return cl_error; |
1486 | 1485 |
} |
1487 | 1486 |
} else { |
... | ... |
@@ -1498,10 +1501,12 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1498 | 1498 |
|
1499 | 1499 |
if((privdata->cmdSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { |
1500 | 1500 |
perror("socket"); |
1501 |
+ clamfi_free(privdata); |
|
1501 | 1502 |
return cl_error; |
1502 | 1503 |
} |
1503 | 1504 |
if(connect(privdata->cmdSocket, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) < 0) { |
1504 | 1505 |
perror(localSocket); |
1506 |
+ clamfi_free(privdata); |
|
1505 | 1507 |
return cl_error; |
1506 | 1508 |
} |
1507 | 1509 |
freeServer = 0; |
... | ... |
@@ -1515,17 +1520,21 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1515 | 1515 |
assert(serverIPs != NULL); |
1516 | 1516 |
|
1517 | 1517 |
freeServer = findServer(); |
1518 |
- if(freeServer < 0) |
|
1518 |
+ if(freeServer < 0) { |
|
1519 |
+ clamfi_free(privdata); |
|
1519 | 1520 |
return cl_error; |
1521 |
+ } |
|
1520 | 1522 |
|
1521 | 1523 |
server.sin_addr.s_addr = serverIPs[freeServer]; |
1522 | 1524 |
|
1523 |
- if((privdata->cmdSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
|
1525 |
+ if((privdata->cmdSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
|
1524 | 1526 |
perror("socket"); |
1527 |
+ clamfi_free(privdata); |
|
1525 | 1528 |
return cl_error; |
1526 | 1529 |
} |
1527 | 1530 |
if(connect(privdata->cmdSocket, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0) { |
1528 | 1531 |
perror("connect"); |
1532 |
+ clamfi_free(privdata); |
|
1529 | 1533 |
return cl_error; |
1530 | 1534 |
} |
1531 | 1535 |
} |
... | ... |
@@ -1535,8 +1544,7 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1535 | 1535 |
*/ |
1536 | 1536 |
if((privdata->dataSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
1537 | 1537 |
perror("socket"); |
1538 |
- close(privdata->cmdSocket); |
|
1539 |
- free(privdata); |
|
1538 |
+ clamfi_free(privdata); |
|
1540 | 1539 |
if(use_syslog) |
1541 | 1540 |
syslog(LOG_ERR, "failed to create socket"); |
1542 | 1541 |
return cl_error; |
... | ... |
@@ -1546,9 +1554,7 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1546 | 1546 |
|
1547 | 1547 |
if(send(privdata->cmdSocket, "STREAM\n", 7, 0) < 7) { |
1548 | 1548 |
perror("send"); |
1549 |
- close(privdata->dataSocket); |
|
1550 |
- close(privdata->cmdSocket); |
|
1551 |
- free(privdata); |
|
1549 |
+ clamfi_free(privdata); |
|
1552 | 1550 |
if(use_syslog) |
1553 | 1551 |
syslog(LOG_ERR, "send failed to clamd"); |
1554 | 1552 |
return cl_error; |
... | ... |
@@ -1559,9 +1565,7 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1559 | 1559 |
nbytes = clamd_recv(privdata->cmdSocket, buf, sizeof(buf)); |
1560 | 1560 |
if(nbytes < 0) { |
1561 | 1561 |
perror("recv"); |
1562 |
- close(privdata->dataSocket); |
|
1563 |
- close(privdata->cmdSocket); |
|
1564 |
- free(privdata); |
|
1562 |
+ clamfi_free(privdata); |
|
1565 | 1563 |
if(use_syslog) |
1566 | 1564 |
syslog(LOG_ERR, "recv failed from clamd getting PORT"); |
1567 | 1565 |
return cl_error; |
... | ... |
@@ -1572,9 +1576,7 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1572 | 1572 |
printf("Received: %s", buf); |
1573 | 1573 |
#endif |
1574 | 1574 |
if(sscanf(buf, "PORT %hu\n", &port) != 1) { |
1575 |
- close(privdata->dataSocket); |
|
1576 |
- close(privdata->cmdSocket); |
|
1577 |
- free(privdata); |
|
1575 |
+ clamfi_free(privdata); |
|
1578 | 1576 |
if(use_syslog) |
1579 | 1577 |
syslog(LOG_ERR, "Expected port information from clamd, got '%s'", |
1580 | 1578 |
buf); |
... | ... |
@@ -1602,9 +1604,7 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1602 | 1602 |
if(rc < 0) { |
1603 | 1603 |
perror("connect"); |
1604 | 1604 |
|
1605 |
- close(privdata->dataSocket); |
|
1606 |
- close(privdata->cmdSocket); |
|
1607 |
- free(privdata); |
|
1605 |
+ clamfi_free(privdata); |
|
1608 | 1606 |
|
1609 | 1607 |
/* 0.4 - use better error message */ |
1610 | 1608 |
if(use_syslog) { |
... | ... |
@@ -1627,9 +1627,15 @@ clamfi_envfrom(SMFICTX *ctx, char **argv) |
1627 | 1627 |
|
1628 | 1628 |
if (hflag) |
1629 | 1629 |
privdata->headers = header_list_new(); |
1630 |
- else privdata->headers = NULL; |
|
1630 |
+ else |
|
1631 |
+ privdata->headers = NULL; |
|
1632 |
+ |
|
1633 |
+ if(smfi_setpriv(ctx, privdata) == MI_SUCCESS) |
|
1634 |
+ return SMFIS_CONTINUE; |
|
1635 |
+ |
|
1636 |
+ clamfi_free(privdata); |
|
1631 | 1637 |
|
1632 |
- return (smfi_setpriv(ctx, privdata) == MI_SUCCESS) ? SMFIS_CONTINUE : cl_error; |
|
1638 |
+ return cl_error; |
|
1633 | 1639 |
} |
1634 | 1640 |
|
1635 | 1641 |
static sfsistat |
... | ... |
@@ -2092,6 +2098,15 @@ clamfi_cleanup(SMFICTX *ctx) |
2092 | 2092 |
struct privdata *privdata = (struct privdata *)smfi_getpriv(ctx); |
2093 | 2093 |
|
2094 | 2094 |
if(privdata) { |
2095 |
+ clamfi_free(privdata); |
|
2096 |
+ smfi_setpriv(ctx, NULL); |
|
2097 |
+ } |
|
2098 |
+} |
|
2099 |
+ |
|
2100 |
+static void |
|
2101 |
+clamfi_free(struct privdata *privdata) |
|
2102 |
+{ |
|
2103 |
+ if(privdata) { |
|
2095 | 2104 |
if(privdata->body) |
2096 | 2105 |
free(privdata->body); |
2097 | 2106 |
|
... | ... |
@@ -2153,7 +2168,6 @@ clamfi_cleanup(SMFICTX *ctx) |
2153 | 2153 |
puts("Free privdata"); |
2154 | 2154 |
#endif |
2155 | 2155 |
free(privdata); |
2156 |
- smfi_setpriv(ctx, NULL); |
|
2157 | 2156 |
} |
2158 | 2157 |
|
2159 | 2158 |
if(max_children > 0) { |