1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <unistd.h>
44*0Sstevel@tonic-gate #include <stdio.h>
45*0Sstevel@tonic-gate #include <syslog.h>
46*0Sstevel@tonic-gate #include <ctype.h>
47*0Sstevel@tonic-gate #include <stdlib.h>
48*0Sstevel@tonic-gate #include <string.h>
49*0Sstevel@tonic-gate #include <locale.h>
50*0Sstevel@tonic-gate #include <limits.h>
51*0Sstevel@tonic-gate #include <pwd.h>
52*0Sstevel@tonic-gate #include <errno.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #define	LOG_MARK	(LOG_NFACILITIES << 3)	/* mark "facility" */
55*0Sstevel@tonic-gate #define	LOGGER_BUFLEN	1024
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate struct code {
58*0Sstevel@tonic-gate 	char	*c_name;
59*0Sstevel@tonic-gate 	int	c_val;
60*0Sstevel@tonic-gate };
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate static struct code	PriNames[] = {
63*0Sstevel@tonic-gate 	"panic",	LOG_EMERG,
64*0Sstevel@tonic-gate 	"emerg",	LOG_EMERG,
65*0Sstevel@tonic-gate 	"alert",	LOG_ALERT,
66*0Sstevel@tonic-gate 	"crit",		LOG_CRIT,
67*0Sstevel@tonic-gate 	"err",		LOG_ERR,
68*0Sstevel@tonic-gate 	"error",	LOG_ERR,
69*0Sstevel@tonic-gate 	"warn",		LOG_WARNING,
70*0Sstevel@tonic-gate 	"warning", 	LOG_WARNING,
71*0Sstevel@tonic-gate 	"notice",	LOG_NOTICE,
72*0Sstevel@tonic-gate 	"info",		LOG_INFO,
73*0Sstevel@tonic-gate 	"debug",	LOG_DEBUG,
74*0Sstevel@tonic-gate 	NULL,		-1
75*0Sstevel@tonic-gate };
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static struct code	FacNames[] = {
78*0Sstevel@tonic-gate 	"kern",		LOG_KERN,
79*0Sstevel@tonic-gate 	"user",		LOG_USER,
80*0Sstevel@tonic-gate 	"mail",		LOG_MAIL,
81*0Sstevel@tonic-gate 	"daemon",	LOG_DAEMON,
82*0Sstevel@tonic-gate 	"auth",		LOG_AUTH,
83*0Sstevel@tonic-gate 	"security",	LOG_AUTH,
84*0Sstevel@tonic-gate 	"mark",		LOG_MARK,
85*0Sstevel@tonic-gate 	"syslog",	LOG_SYSLOG,
86*0Sstevel@tonic-gate 	"lpr",		LOG_LPR,
87*0Sstevel@tonic-gate 	"news",		LOG_NEWS,
88*0Sstevel@tonic-gate 	"uucp",		LOG_UUCP,
89*0Sstevel@tonic-gate 	"cron",		LOG_CRON,
90*0Sstevel@tonic-gate 	"audit",	LOG_AUDIT,
91*0Sstevel@tonic-gate 	"local0",	LOG_LOCAL0,
92*0Sstevel@tonic-gate 	"local1",	LOG_LOCAL1,
93*0Sstevel@tonic-gate 	"local2",	LOG_LOCAL2,
94*0Sstevel@tonic-gate 	"local3",	LOG_LOCAL3,
95*0Sstevel@tonic-gate 	"local4",	LOG_LOCAL4,
96*0Sstevel@tonic-gate 	"local5",	LOG_LOCAL5,
97*0Sstevel@tonic-gate 	"local6",	LOG_LOCAL6,
98*0Sstevel@tonic-gate 	"local7",	LOG_LOCAL7,
99*0Sstevel@tonic-gate 	NULL,		-1
100*0Sstevel@tonic-gate };
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate static int	pencode(register char *);
103*0Sstevel@tonic-gate static int	decode(char *, struct code *);
104*0Sstevel@tonic-gate static void	bailout(char *, char *);
105*0Sstevel@tonic-gate static void	usage(void);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  *  LOGGER -- read and log utility
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  *	This routine reads from an input and arranges to write the
111*0Sstevel@tonic-gate  *	result on the system log, along with a useful tag.
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate main(argc, argv)
115*0Sstevel@tonic-gate int argc;
116*0Sstevel@tonic-gate char **argv;
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	char tmp[23];
119*0Sstevel@tonic-gate 	char *tag = NULL;
120*0Sstevel@tonic-gate 	char *infile = NULL;
121*0Sstevel@tonic-gate 	char *buf = NULL;
122*0Sstevel@tonic-gate 	size_t buflen;
123*0Sstevel@tonic-gate 	int pri = LOG_NOTICE;
124*0Sstevel@tonic-gate 	int logflags = 0;
125*0Sstevel@tonic-gate 	int opt;
126*0Sstevel@tonic-gate 	int pid_len = 0;
127*0Sstevel@tonic-gate 	struct passwd *pw;
128*0Sstevel@tonic-gate 	uid_t u;
129*0Sstevel@tonic-gate 	char fmt_uid[16];
130*0Sstevel@tonic-gate 	char *p, *endp;
131*0Sstevel@tonic-gate 	size_t len;
132*0Sstevel@tonic-gate 	ptrdiff_t offset = 0;
133*0Sstevel@tonic-gate 	int status = 0;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
136*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
137*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
138*0Sstevel@tonic-gate #endif
139*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
140*0Sstevel@tonic-gate 	/* initialize */
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "it:p:f:")) != EOF)
143*0Sstevel@tonic-gate 		switch (opt) {
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 		    case 't':		/* tag */
146*0Sstevel@tonic-gate 			tag = optarg;
147*0Sstevel@tonic-gate 			break;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		    case 'p':		/* priority */
150*0Sstevel@tonic-gate 			pri = pencode(optarg);
151*0Sstevel@tonic-gate 			break;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 		    case 'i':		/* log process id also */
154*0Sstevel@tonic-gate 			logflags |= LOG_PID;
155*0Sstevel@tonic-gate 			pid_len = sprintf(tmp, "%ld", (long)getpid());
156*0Sstevel@tonic-gate 			pid_len = (pid_len <= 0) ? 0 : pid_len +2;
157*0Sstevel@tonic-gate 			break;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 		    case 'f':		/* file to log */
160*0Sstevel@tonic-gate 			if (strcmp(optarg, "-") == 0)
161*0Sstevel@tonic-gate 				break;
162*0Sstevel@tonic-gate 			infile = optarg;
163*0Sstevel@tonic-gate 			if (freopen(infile, "r", stdin) == NULL)
164*0Sstevel@tonic-gate 			{
165*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("logger: "));
166*0Sstevel@tonic-gate 				perror(infile);
167*0Sstevel@tonic-gate 				exit(1);
168*0Sstevel@tonic-gate 			}
169*0Sstevel@tonic-gate 			break;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 		    default:
172*0Sstevel@tonic-gate 			usage();
173*0Sstevel@tonic-gate 		}
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		argc -= optind;
176*0Sstevel@tonic-gate 		argv = &argv[optind];
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	if ((tag == NULL) && ((tag = getlogin()) == NULL)) {
179*0Sstevel@tonic-gate 		u = getuid();
180*0Sstevel@tonic-gate 		if ((pw = getpwuid(u)) == NULL) {
181*0Sstevel@tonic-gate 			(void) sprintf(fmt_uid, "%ld", u);
182*0Sstevel@tonic-gate 			tag = fmt_uid;
183*0Sstevel@tonic-gate 		} else
184*0Sstevel@tonic-gate 			tag = pw->pw_name;
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	/* setup for logging */
188*0Sstevel@tonic-gate 	openlog(tag, logflags, 0);
189*0Sstevel@tonic-gate 	(void) fclose(stdout);
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	/* log input line if appropriate */
192*0Sstevel@tonic-gate 	if (argc > 0) {
193*0Sstevel@tonic-gate 		/*
194*0Sstevel@tonic-gate 		 * Log arguments from command line
195*0Sstevel@tonic-gate 		 */
196*0Sstevel@tonic-gate 		int i;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 		len = 0;
199*0Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
200*0Sstevel@tonic-gate 			len += strlen(argv[i]) + 1;	/* add 1 for <space> */
201*0Sstevel@tonic-gate 		}
202*0Sstevel@tonic-gate 		if ((buf = malloc(len + 1)) == NULL) {
203*0Sstevel@tonic-gate 			perror("logger");
204*0Sstevel@tonic-gate 			exit(1);
205*0Sstevel@tonic-gate 		}
206*0Sstevel@tonic-gate 		buf[0] = '\0';
207*0Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
208*0Sstevel@tonic-gate 			if (i != 0) {
209*0Sstevel@tonic-gate 				(void) strcat(buf, " ");
210*0Sstevel@tonic-gate 			}
211*0Sstevel@tonic-gate 			(void) strcat(buf, argv[i]);
212*0Sstevel@tonic-gate 		}
213*0Sstevel@tonic-gate #ifdef DEBUG
214*0Sstevel@tonic-gate 		(void) fprintf(stderr, "len=%d, buf >%s<\n", len, buf);
215*0Sstevel@tonic-gate #endif
216*0Sstevel@tonic-gate 		syslog(pri, "%s", buf);
217*0Sstevel@tonic-gate 	} else {
218*0Sstevel@tonic-gate 		/*
219*0Sstevel@tonic-gate 		 * Log arguments from stdin (or input file).
220*0Sstevel@tonic-gate 		 * When reading from stdin, logger grows its buffer if
221*0Sstevel@tonic-gate 		 * needed, to handle long lines.
222*0Sstevel@tonic-gate 		 */
223*0Sstevel@tonic-gate 		if ((buf = malloc(LOGGER_BUFLEN)) == NULL) {
224*0Sstevel@tonic-gate 			perror("logger");
225*0Sstevel@tonic-gate 			exit(1);
226*0Sstevel@tonic-gate 		}
227*0Sstevel@tonic-gate 		buflen = LOGGER_BUFLEN;
228*0Sstevel@tonic-gate 		p = buf;
229*0Sstevel@tonic-gate 		endp = buf + buflen;
230*0Sstevel@tonic-gate 		offset = 0;
231*0Sstevel@tonic-gate 		while (fgets(p, endp - p, stdin) != NULL) {
232*0Sstevel@tonic-gate 			len = strlen(p);
233*0Sstevel@tonic-gate 			if (p[len - 1] == '\n') {
234*0Sstevel@tonic-gate #ifdef DEBUG
235*0Sstevel@tonic-gate 				(void) fprintf(stderr,
236*0Sstevel@tonic-gate 				    "p-buf =%d, len=%d, buflen=%d, buf >%s<\n",
237*0Sstevel@tonic-gate 				    p-buf, len, buflen, buf);
238*0Sstevel@tonic-gate #endif
239*0Sstevel@tonic-gate 				syslog(pri, "%s", buf);
240*0Sstevel@tonic-gate 				p = buf;
241*0Sstevel@tonic-gate 				offset = 0;
242*0Sstevel@tonic-gate 			} else if (len < endp - p - 1) {
243*0Sstevel@tonic-gate 				/* short read or line with no <newline> */
244*0Sstevel@tonic-gate 				p += len;
245*0Sstevel@tonic-gate 				offset += len;
246*0Sstevel@tonic-gate #ifdef DEBUG
247*0Sstevel@tonic-gate 				(void) fprintf(stderr,
248*0Sstevel@tonic-gate 				    "p-buf=%d, len=%d, buflen=%d, buf >%s<\n",
249*0Sstevel@tonic-gate 				    p-buf, len, buflen, buf);
250*0Sstevel@tonic-gate #endif
251*0Sstevel@tonic-gate 				continue;
252*0Sstevel@tonic-gate 			} else {
253*0Sstevel@tonic-gate 				/* line longer than buflen, so get larger buf */
254*0Sstevel@tonic-gate 				buflen += LOGGER_BUFLEN;
255*0Sstevel@tonic-gate 				offset += len;
256*0Sstevel@tonic-gate #ifdef DEBUG
257*0Sstevel@tonic-gate 				(void) fprintf(stderr,
258*0Sstevel@tonic-gate 				    "Realloc endp-p=%d, len=%d, offset=%d, "
259*0Sstevel@tonic-gate 				    "buflen %d\n",
260*0Sstevel@tonic-gate 				    endp - p, len, offset, buflen);
261*0Sstevel@tonic-gate #endif
262*0Sstevel@tonic-gate 				if ((buf = realloc(buf, buflen)) == NULL) {
263*0Sstevel@tonic-gate 					perror("logger");
264*0Sstevel@tonic-gate 					exit(1);
265*0Sstevel@tonic-gate 				}
266*0Sstevel@tonic-gate 				p = buf + offset;
267*0Sstevel@tonic-gate 				endp = buf + buflen;
268*0Sstevel@tonic-gate 			}
269*0Sstevel@tonic-gate 		}	/* while */
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 		if (feof(stdin)) {
272*0Sstevel@tonic-gate 			if (p > buf) {
273*0Sstevel@tonic-gate 				/* the last line did not end with newline */
274*0Sstevel@tonic-gate #ifdef DEBUG
275*0Sstevel@tonic-gate 				(void) fprintf(stderr,
276*0Sstevel@tonic-gate 				    "(2) p-buf=%d, len=%d, buflen=%d, "
277*0Sstevel@tonic-gate 				    "buf >%s<\n",
278*0Sstevel@tonic-gate 				    p-buf, len, buflen, buf);
279*0Sstevel@tonic-gate #endif
280*0Sstevel@tonic-gate 				syslog(pri, "%s", buf);
281*0Sstevel@tonic-gate 			}
282*0Sstevel@tonic-gate 		} else {
283*0Sstevel@tonic-gate 			/*
284*0Sstevel@tonic-gate 			 * fgets() encountered an error.  Log unlogged data
285*0Sstevel@tonic-gate 			 * from earlier fgets() (if any).  Write null byte
286*0Sstevel@tonic-gate 			 * after last full read, in case the fgets() that
287*0Sstevel@tonic-gate 			 * encountered error removed it and failed to null
288*0Sstevel@tonic-gate 			 * terminate.
289*0Sstevel@tonic-gate 			 */
290*0Sstevel@tonic-gate 			perror("logger");
291*0Sstevel@tonic-gate 			if (p > buf) {
292*0Sstevel@tonic-gate 				*p = '\0';
293*0Sstevel@tonic-gate 				syslog(pri, "%s", buf);
294*0Sstevel@tonic-gate 			}
295*0Sstevel@tonic-gate 			status = 1;
296*0Sstevel@tonic-gate 		}
297*0Sstevel@tonic-gate 	}	/* else !(argc > 0) */
298*0Sstevel@tonic-gate 	free(buf);
299*0Sstevel@tonic-gate 	return (status);
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate /*
303*0Sstevel@tonic-gate  *  Decode a symbolic name to a numeric value
304*0Sstevel@tonic-gate  */
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate static int
308*0Sstevel@tonic-gate pencode(s)
309*0Sstevel@tonic-gate register char *s;
310*0Sstevel@tonic-gate {
311*0Sstevel@tonic-gate 	register char *p;
312*0Sstevel@tonic-gate 	int lev;
313*0Sstevel@tonic-gate 	int fac = 0;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	for (p = s; *s && *s != '.'; s++);
316*0Sstevel@tonic-gate 	if (*s) {
317*0Sstevel@tonic-gate 		*s = '\0';
318*0Sstevel@tonic-gate 		fac = decode(p, FacNames);
319*0Sstevel@tonic-gate 		if (fac < 0)
320*0Sstevel@tonic-gate 			bailout("unknown facility name: ", p);
321*0Sstevel@tonic-gate 		*s++ = '.';
322*0Sstevel@tonic-gate 	} else
323*0Sstevel@tonic-gate 		s = p;
324*0Sstevel@tonic-gate 	lev = decode(s, PriNames);
325*0Sstevel@tonic-gate 	if (lev < 0)
326*0Sstevel@tonic-gate 		bailout("unknown priority name: ", s);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
329*0Sstevel@tonic-gate }
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate static int
333*0Sstevel@tonic-gate decode(name, codetab)
334*0Sstevel@tonic-gate char *name;
335*0Sstevel@tonic-gate struct code *codetab;
336*0Sstevel@tonic-gate {
337*0Sstevel@tonic-gate 	register struct code *c;
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	if (isdigit(*name))
340*0Sstevel@tonic-gate 		return (atoi(name));
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	for (c = codetab; c->c_name; c++)
343*0Sstevel@tonic-gate 		if (strcasecmp(name, c->c_name) == 0)
344*0Sstevel@tonic-gate 			return (c->c_val);
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	return (-1);
347*0Sstevel@tonic-gate }
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate static void
351*0Sstevel@tonic-gate bailout(a, b)
352*0Sstevel@tonic-gate char *a, *b;
353*0Sstevel@tonic-gate {
354*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("logger: %s%s\n"), a, b);
355*0Sstevel@tonic-gate 	exit(1);
356*0Sstevel@tonic-gate }
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate static void
360*0Sstevel@tonic-gate usage(void)
361*0Sstevel@tonic-gate {
362*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
363*0Sstevel@tonic-gate 	    "Usage:\tlogger string\n"
364*0Sstevel@tonic-gate 	    "\tlogger [-i] [-f filename] [-p priority] [-t tag] "
365*0Sstevel@tonic-gate 		"[message] ...\n"));
366*0Sstevel@tonic-gate 	exit(1);
367*0Sstevel@tonic-gate }
368