1 #include "ipf.h" 2 #include "ipmon.h" 3 #include <syslog.h> 4 5 static void *syslog_parse(char **); 6 static void syslog_destroy(void *); 7 static int syslog_send(void *, ipmon_msg_t *); 8 static void syslog_print(void *); 9 10 typedef struct syslog_opts_s { 11 int facpri; 12 int fac; 13 int pri; 14 } syslog_opts_t; 15 16 ipmon_saver_t syslogsaver = { 17 "syslog", 18 syslog_destroy, 19 NULL, /* dup */ 20 NULL, /* match */ 21 syslog_parse, 22 syslog_print, 23 syslog_send 24 }; 25 26 27 static void * 28 syslog_parse(char **strings) 29 { 30 syslog_opts_t *ctx; 31 char *str; 32 char *s; 33 34 ctx = calloc(1, sizeof(*ctx)); 35 if (ctx == NULL) 36 return NULL; 37 38 ctx->facpri = -1; 39 40 if (strings[0] != NULL && strings[0][0] != '\0') { 41 str = strdup(*strings); 42 if (str != NULL && *str != '\0') { 43 int fac = -1, pri = -1; 44 45 s = strchr(str, '.'); 46 if (s != NULL) 47 *s++ = '\0'; 48 49 if (*str != '\0') { 50 fac = fac_findname(str); 51 if (fac == -1) { 52 free(str); 53 free(ctx); 54 return NULL; 55 } 56 } 57 58 if (s != NULL && *s != '\0') { 59 pri = pri_findname(s); 60 if (pri == -1) { 61 free(str); 62 free(ctx); 63 return NULL; 64 } 65 } 66 free(str); 67 68 ctx->fac = fac; 69 ctx->pri = pri; 70 if (pri == -1) 71 ctx->facpri = fac; 72 else if (fac == -1) 73 ctx->facpri = pri; 74 else 75 ctx->facpri = fac | pri; 76 } else { 77 if (str != NULL) 78 free(str); 79 free(ctx); 80 ctx = NULL; 81 } 82 } 83 84 return ctx; 85 } 86 87 88 static void 89 syslog_print(ctx) 90 void *ctx; 91 { 92 syslog_opts_t *sys = ctx; 93 94 if (sys->facpri == -1) 95 return; 96 97 if (sys->fac == -1) { 98 printf(".%s", pri_toname(sys->pri)); 99 } else if (sys->pri == -1) { 100 printf("%s.", fac_toname(sys->fac)); 101 } else { 102 printf("%s.%s", fac_toname(sys->facpri & LOG_FACMASK), 103 pri_toname(sys->facpri & LOG_PRIMASK)); 104 } 105 } 106 107 108 static void 109 syslog_destroy(ctx) 110 void *ctx; 111 { 112 free(ctx); 113 } 114 115 116 static int 117 syslog_send(ctx, msg) 118 void *ctx; 119 ipmon_msg_t *msg; 120 { 121 syslog_opts_t *sys = ctx; 122 int facpri; 123 124 if (sys->facpri == -1) { 125 facpri = msg->imm_loglevel; 126 } else { 127 if (sys->pri == -1) { 128 facpri = sys->fac | (msg->imm_loglevel & LOG_PRIMASK); 129 } else if (sys->fac == -1) { 130 facpri = sys->pri | (msg->imm_loglevel & LOG_FACMASK); 131 } else { 132 facpri = sys->facpri; 133 } 134 } 135 syslog(facpri, "%s", msg->imm_msg); 136 return 0; 137 } 138