1 /* $OpenBSD: syslogc.c,v 1.11 2005/09/28 08:49:28 stevesk 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 <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <unistd.h> 28 29 #define DEFAULT_CTLSOCK "/var/run/syslogd.sock" 30 31 #define MAX_MEMBUF_NAME 64 /* Max length of membuf log name */ 32 33 /* 34 * Client protocol NB. all numeric fields in network byte order 35 */ 36 #define CTL_VERSION 0 37 38 /* Request */ 39 struct ctl_cmd { 40 u_int32_t version; 41 #define CMD_READ 1 /* Read out log */ 42 #define CMD_READ_CLEAR 2 /* Read and clear log */ 43 #define CMD_CLEAR 3 /* Clear log */ 44 #define CMD_LIST 4 /* List available logs */ 45 #define CMD_FLAGS 5 /* Query flags only */ 46 u_int32_t cmd; 47 char logname[MAX_MEMBUF_NAME]; 48 }; 49 50 /* Reply */ 51 struct ctl_reply_hdr { 52 u_int32_t version; 53 #define CTL_HDR_FLAG_OVERFLOW 0x01 54 u_int32_t flags; 55 /* Reply text follows, up to MAX_MEMBUF long */ 56 }; 57 58 static void 59 usage(void) 60 { 61 extern char *__progname; 62 63 fprintf(stderr, "Usage: %s [-Ccoq] [-s ctlsock] logname\n", __progname); 64 exit(1); 65 } 66 67 int 68 main(int argc, char **argv) 69 { 70 const char *ctlsock_path; 71 char buf[8192]; 72 struct sockaddr_un ctl; 73 int ctlsock, ch, oflag, rval; 74 FILE *ctlf; 75 extern char *optarg; 76 extern int optind; 77 struct ctl_cmd cc; 78 struct ctl_reply_hdr rr; 79 80 memset(&cc, '\0', sizeof(cc)); 81 82 ctlsock_path = DEFAULT_CTLSOCK; 83 rval = oflag = 0; 84 while ((ch = getopt(argc, argv, "Cchoqs:")) != -1) { 85 switch (ch) { 86 case 'C': 87 cc.cmd = CMD_CLEAR; 88 break; 89 case 'c': 90 cc.cmd = CMD_READ_CLEAR; 91 break; 92 case 'h': 93 usage(); 94 break; 95 case 'o': 96 cc.cmd = CMD_FLAGS; 97 oflag = 1; 98 break; 99 case 'q': 100 cc.cmd = CMD_LIST; 101 break; 102 case 's': 103 ctlsock_path = optarg; 104 break; 105 default: 106 usage(); 107 break; 108 } 109 } 110 111 if (cc.cmd == 0) 112 cc.cmd = CMD_READ; 113 114 if ((cc.cmd != CMD_LIST && optind != argc - 1) || 115 (cc.cmd == CMD_LIST && optind != argc)) 116 usage(); 117 118 if (cc.cmd != CMD_LIST) { 119 if (strlcpy(cc.logname, argv[optind], sizeof(cc.logname)) >= 120 sizeof(cc.logname)) 121 errx(1, "Specified log name is too long"); 122 } 123 124 memset(&ctl, '\0', sizeof(ctl)); 125 strlcpy(ctl.sun_path, ctlsock_path, sizeof(ctl.sun_path)); 126 ctl.sun_family = AF_UNIX; 127 128 if ((ctlsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) 129 err(1, "socket"); 130 if (connect(ctlsock, (struct sockaddr *)&ctl, sizeof(ctl)) == -1) 131 err(1, "connect: %s", ctl.sun_path); 132 if ((ctlf = fdopen(ctlsock, "r+")) == NULL) 133 err(1, "fdopen"); 134 135 cc.version = htonl(CTL_VERSION); 136 cc.cmd = htonl(cc.cmd); 137 /* Send command */ 138 if (fwrite(&cc, sizeof(cc), 1, ctlf) != 1) 139 err(1, "fwrite"); 140 141 fflush(ctlf); 142 setlinebuf(ctlf); 143 144 /* Fetch header */ 145 if (fread(&rr, sizeof(rr), 1, ctlf) != 1) 146 err(1, "fread header"); 147 148 if (ntohl(rr.version) != CTL_VERSION) 149 errx(1, "unsupported syslogd version"); 150 151 /* Write out reply */ 152 while ((fgets(buf, sizeof(buf), ctlf)) != NULL) 153 fputs(buf, stdout); 154 155 if (oflag && (ntohl(rr.flags) & CTL_HDR_FLAG_OVERFLOW)) { 156 printf("%s has overflowed\n", cc.logname); 157 rval = 1; 158 } 159 160 fclose(ctlf); 161 close(ctlsock); 162 163 exit(rval); 164 } 165