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(void *ctx) 90 { 91 syslog_opts_t *sys = ctx; 92 93 if (sys->facpri == -1) 94 return; 95 96 if (sys->fac == -1) { 97 printf(".%s", pri_toname(sys->pri)); 98 } else if (sys->pri == -1) { 99 printf("%s.", fac_toname(sys->fac)); 100 } else { 101 printf("%s.%s", fac_toname(sys->facpri & LOG_FACMASK), 102 pri_toname(sys->facpri & LOG_PRIMASK)); 103 } 104 } 105 106 107 static void 108 syslog_destroy(void *ctx) 109 { 110 free(ctx); 111 } 112 113 114 static int 115 syslog_send(void *ctx, ipmon_msg_t *msg) 116 { 117 syslog_opts_t *sys = ctx; 118 int facpri; 119 120 if (sys->facpri == -1) { 121 facpri = msg->imm_loglevel; 122 } else { 123 if (sys->pri == -1) { 124 facpri = sys->fac | (msg->imm_loglevel & LOG_PRIMASK); 125 } else if (sys->fac == -1) { 126 facpri = sys->pri | (msg->imm_loglevel & LOG_FACMASK); 127 } else { 128 facpri = sys->facpri; 129 } 130 } 131 syslog(facpri, "%s", msg->imm_msg); 132 return (0); 133 } 134