1 /* $OpenBSD: syslogc.c,v 1.6 2004/06/25 19:11:38 djm Exp $ */ 2 3 /* 4 * Copyright (c) 2004 Damien Miller 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <sys/un.h> 22 23 #include <err.h> 24 #include <errno.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #define DEFAULT_CTLSOCK "/var/run/syslogd.sock" 31 32 #define MAX_MEMBUF_NAME 64 /* Max length of membuf log name */ 33 34 /* 35 * Client protocol NB. all numeric fields in network byte order 36 */ 37 #define CTL_VERSION 0 38 39 /* Request */ 40 struct ctl_cmd { 41 u_int32_t version; 42 #define CMD_READ 1 /* Read out log */ 43 #define CMD_READ_CLEAR 2 /* Read and clear log */ 44 #define CMD_CLEAR 3 /* Clear log */ 45 #define CMD_LIST 4 /* List available logs */ 46 #define CMD_FLAGS 5 /* Query flags only */ 47 u_int32_t cmd; 48 char logname[MAX_MEMBUF_NAME]; 49 }; 50 51 /* Reply */ 52 struct ctl_reply_hdr { 53 u_int32_t version; 54 #define CTL_HDR_FLAG_OVERFLOW 0x01 55 u_int32_t flags; 56 /* Reply text follows, up to MAX_MEMBUF long */ 57 }; 58 59 /* Protocol parameters - must match syslogd */ 60 #define CTL_VERSION 0 61 #define CTL_HDR_LEN 8 62 63 static void 64 usage(void) 65 { 66 extern char *__progname; 67 68 fprintf(stderr, "Usage: %s [-Ccq] [-s ctlsock] logname\n", __progname); 69 exit(1); 70 } 71 72 int 73 main(int argc, char **argv) 74 { 75 const char *ctlsock_path; 76 char buf[8192]; 77 struct sockaddr_un ctl; 78 socklen_t ctllen; 79 int ctlsock, ch, oflag, rval; 80 FILE *ctlf; 81 extern char *optarg; 82 extern int optind; 83 struct ctl_cmd cc; 84 struct ctl_reply_hdr rr; 85 u_int32_t header[2]; 86 87 memset(&cc, '\0', sizeof(cc)); 88 89 ctlsock_path = DEFAULT_CTLSOCK; 90 rval = oflag = 0; 91 while ((ch = getopt(argc, argv, "Cchoqs:")) != -1) { 92 switch (ch) { 93 case 'C': 94 cc.cmd = CMD_CLEAR; 95 break; 96 case 'c': 97 cc.cmd = CMD_READ_CLEAR; 98 break; 99 case 'h': 100 usage(); 101 break; 102 case 'o': 103 cc.cmd = CMD_FLAGS; 104 oflag = 1; 105 break; 106 case 'q': 107 cc.cmd = CMD_LIST; 108 break; 109 case 's': 110 ctlsock_path = optarg; 111 break; 112 default: 113 fprintf(stderr, "Invalid commandline option.\n"); 114 usage(); 115 break; 116 } 117 } 118 119 if (cc.cmd == 0) 120 cc.cmd = CMD_READ; 121 122 if ((cc.cmd != CMD_LIST && optind != argc - 1) || 123 (cc.cmd == CMD_LIST && optind != argc)) 124 usage(); 125 126 if (cc.cmd != CMD_LIST) { 127 if (strlcpy(cc.logname, argv[optind], sizeof(cc.logname)) >= 128 sizeof(cc.logname)) 129 errx(1, "Specified log name is too long"); 130 } 131 132 memset(&ctl, '\0', sizeof(ctl)); 133 strlcpy(ctl.sun_path, ctlsock_path, sizeof(ctl.sun_path)); 134 ctl.sun_family = AF_UNIX; 135 136 if ((ctlsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) 137 err(1, "socket"); 138 if (connect(ctlsock, (struct sockaddr*)&ctl, sizeof(ctl)) == -1) 139 err(1, "connect: %s", ctl.sun_path); 140 if ((ctlf = fdopen(ctlsock, "r+")) == NULL) 141 err(1, "fdopen"); 142 143 cc.version = htonl(CTL_VERSION); 144 cc.cmd = htonl(cc.cmd); 145 /* Send command */ 146 if (fwrite(&cc, sizeof(cc), 1, ctlf) != 1) 147 err(1, "fwrite"); 148 149 fflush(ctlf); 150 setlinebuf(ctlf); 151 152 /* Fetch header */ 153 if (fread(&rr, sizeof(rr), 1, ctlf) != 1) 154 err(1, "fread header"); 155 156 if (ntohl(rr.version) != CTL_VERSION) 157 err(1, "unsupported syslogd version"); 158 159 /* Write out reply */ 160 while((fgets(buf, sizeof(buf), ctlf)) != NULL) 161 fputs(buf, stdout); 162 163 if (oflag && (ntohl(rr.flags) & CTL_HDR_FLAG_OVERFLOW)) { 164 printf("%s has overflowed\n", cc.logname); 165 rval = 1; 166 } 167 168 fclose(ctlf); 169 close(ctlsock); 170 171 exit(rval); 172 } 173