From: <ph...@us...> - 2003-04-04 17:22:32
|
Update of /cvsroot/msyslog/syslog/src/modules In directory sc8-pr-cvs1:/tmp/cvs-serv2565/syslog/src/modules Modified Files: im_tcp.c im_udp.c om_queue.c Log Message: While complient with RFC 3164 the host parsing is not appropriate when the timestamp is missing but the priority is not. Index: im_tcp.c =================================================================== RCS file: /cvsroot/msyslog/syslog/src/modules/im_tcp.c,v retrieving revision 1.44 retrieving revision 1.45 diff -u -d -r1.44 -r1.45 --- im_tcp.c 22 Feb 2003 03:40:58 -0000 1.44 +++ im_tcp.c 4 Apr 2003 17:22:24 -0000 1.45 @@ -386,8 +386,8 @@ &n1, host, &n2) != 1 && sscanf(thisline, "%*3s %*i %*i:%*i:%*i %n%89s %n", &n1, host, &n2) != 1 && - sscanf(thisline, "%n%89s %n", - &n1, host, &n2) != 1) + sscanf(ret->im_msg, "<%*d>%n%89s %n%*s", &n1, host, &n2) != 1 && + sscanf(thisline, "%n%89s %n", &n1, host, &n2) != 1) || im->im_buf[n2] == '\0') { m_dprintf(MSYSLOG_INFORMATIVE, Index: im_udp.c =================================================================== RCS file: /cvsroot/msyslog/syslog/src/modules/im_udp.c,v retrieving revision 1.81 retrieving revision 1.82 diff -u -d -r1.81 -r1.82 --- im_udp.c 1 Apr 2003 21:07:26 -0000 1.81 +++ im_udp.c 4 Apr 2003 17:22:24 -0000 1.82 @@ -209,10 +209,11 @@ int n1 = 0; int n2 = 0; - if ((sscanf(ret->im_msg, "<%*d>%*3s %*i %*i:%*i:%*i %n%89s " - "%n%*s", &n1, host, &n2) != 1 + if ((sscanf(ret->im_msg, "<%*d>%*3s %*i %*i:%*i:%*i %n%89s %n%*s", + &n1, host, &n2) != 1 && sscanf(ret->im_msg, "%*3s %*i %*i:%*i:%*i %n%89s %n%*s", - &n1, host, &n2) != 1 + &n1, host, &n2) != 1 + && sscanf(ret->im_msg, "<%*d>%n%89s %n%*s", &n1, host, &n2) != 1 && sscanf(ret->im_msg, "%n%89s %n%*s", &n1, host, &n2) != 1) || ret->im_msg[n2] == '\0') { Index: om_queue.c =================================================================== RCS file: /cvsroot/msyslog/syslog/src/modules/om_queue.c,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- om_queue.c 26 Sep 2002 18:46:07 -0000 1.6 +++ om_queue.c 4 Apr 2003 17:22:25 -0000 1.7 @@ -115,6 +115,51 @@ struct om_queue_element_node *footer_node; /* the first footer's node */ }; +struct xml_encoding { + char to_encode; + char encoding[16]; +}; + +static struct xml_encoding encodings[] = { + { '&', "&" }, /* AMPERSAND **MUST** GO FIRST !! */ + { '<', "<" }, + { '>', ">" }, +}; + +static int no_encodings = sizeof(encodings)/sizeof(encodings[0]); + +static char *xml_encode(char *from, char *to, int to_length) { + int to1_length = strlen(from) * 6; + char to1[to1_length], to2[to1_length]; + + char *swap1 = to1, *swap2 = to2; + int ix; + + strcpy(swap1, from); + for(ix = 0 ; ix < no_encodings ; ++ix) { + + char to_encode = encodings[ix].to_encode; + char *encoding = encodings[ix].encoding; + + char *position = swap1, *new_position; + char *swapper; + + *swap2 = '\0'; + + while( ( new_position = strchr(position, to_encode) ) != 0 ) { + strncat(swap2, position, new_position - position); + strcat(swap2, encoding); + position = new_position + 1; + } + strcat(swap2, position); + + swapper = swap1; swap1 = swap2; swap2 = swapper; + } + + strncpy(to, swap1, to_length); to[to_length - 1] = '\0'; + +return to; +} /* WRITE -- Write to the directory queue @@ -190,26 +235,56 @@ for( node = ctx->header_node; node != 0; node = node->element_node ) { if (! node->element) continue; - fprintf( filehandle, - ((*node->payload == '\0') ? "\n<%s%s%s/>" : "\n<%s%s%s>%s</%s>"), - node->element, (*node->attribute == '\0' ? "":" "), node->attribute, - node->payload, node->element ); + + if (*node->payload == '\0') { + fprintf( filehandle, "\n<%s%s%s/>", + node->element, (*node->attribute == '\0' ? "":" "), node->attribute ); + continue; + } + + int encoded_length = strlen(node->payload) * 6; + char encoded_payload[encoded_length]; + fprintf( filehandle, "\n<%s%s%s>%s</%s>", + node->element, (*node->attribute == '\0' ? "":" "), node->attribute, + xml_encode(node->payload, encoded_payload, encoded_length), node->element ); } - fprintf( filehandle, "\n<patient>%s</patient>", fil->f_prevhost ); + { + int encoded_length = strlen(fil->f_prevhost) * 6; + char encoded_payload[encoded_length]; + fprintf( filehandle, "\n<patient>%s</patient>", + xml_encode(fil->f_prevhost, encoded_payload, encoded_length) ); + } fprintf( filehandle, "\n<timestamp>%d</timestamp>", (int)timer ); - if (ctx->key) fprintf( filehandle, "\n<key>%s</key>", ctx->key ); + if (ctx->key) { + int encoded_length = strlen(ctx->key) * 6; + char encoded_payload[encoded_length]; + fprintf( filehandle, "\n<key>%s</key>", + xml_encode(ctx->key, encoded_payload, encoded_length) ); + } - fprintf( filehandle, "\n<message facility=\"%d\" priority=\"%d\">%s</message>", - msg->fac, msg->pri, msg->msg ); + { + int encoded_length = strlen(msg->msg) * 6; + char encoded_payload[encoded_length]; + fprintf( filehandle, "\n<message facility=\"%d\" priority=\"%d\">%s</message>", + msg->fac, msg->pri, xml_encode(msg->msg, encoded_payload, encoded_length) ); + } for( node = ctx->footer_node; node != 0; node = node->element_node ) { if (! node->element) continue; - fprintf( filehandle, - ((*node->payload == '\0') ? "\n<%s%s%s/>" : "\n<%s%s%s>%s</%s>"), - node->element, (*node->attribute == '\0' ? "":" "), node->attribute, - node->payload, node->element ); + + if (*node->payload == '\0') { + fprintf( filehandle, "\n<%s%s%s/>", + node->element, (*node->attribute == '\0' ? "":" "), node->attribute ); + continue; + } + + int encoded_length = strlen(node->payload) * 6; + char encoded_payload[encoded_length]; + fprintf( filehandle, "\n<%s%s%s>%s</%s>", + node->element, (*node->attribute == '\0' ? "":" "), node->attribute, + xml_encode(node->payload, encoded_payload, encoded_length), node->element ); } { fprintf( filehandle, "\n</ticket>\n" ); } |