1/*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source.  A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12/*
13 * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
14 */
15
16#include <stdio.h>
17#include <stdarg.h>
18#include <errno.h>
19#include <string.h>
20#include <syslog.h>
21
22#include <smbsrv/smbinfo.h>
23#include <smbsrv/smb_ioctl.h>
24#include "smbd.h"
25
26#define	CBUFSIZ 26	/* ctime(3c) */
27
28static const char *pri_name[LOG_DEBUG+1] = {
29	"emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"
30};
31
32static void
33smb_svc_log(int pri, const char *fmt, va_list ap)
34{
35	static time_t prev_ts;
36	char fbuf[SMBD_LOG_MSGSIZE];
37	char cbuf[CBUFSIZ];
38	char *newfmt;
39	time_t ts;
40	int save_errno = errno;
41
42	pri &= LOG_PRIMASK;
43	if (smbd.s_debug == 0 && pri == LOG_DEBUG)
44		return;
45
46	ts = time(NULL);
47	if (prev_ts != ts) {
48		prev_ts = ts;
49		/* NB: cbuf has \n */
50		(void) fprintf(stdout, "@ %s",
51		    ctime_r(&ts, cbuf, sizeof (cbuf)));
52	}
53
54	newfmt = smb_syslog_fmt_m(fbuf, sizeof (fbuf), fmt, save_errno);
55
56	flockfile(stdout);
57	(void) fprintf(stdout, "smbd.%s: ", pri_name[pri]);
58	/* LINTED E_SEC_PRINTF_VAR_FMT */
59	(void) vfprintf(stdout, newfmt, ap);
60	(void) fprintf(stdout, "\n");
61	funlockfile(stdout);
62
63	(void) fflush(stdout);
64}
65
66/*
67 * Provide a replacement for libsmb:smb_vsyslog() that prints messages
68 * both to the normal sysloc(3c), and to stdout, which ends up in:
69 *  /var/svc/log/network-smb-server:default.log
70 * It's much easier to follow debug messages in the service log.
71 */
72void
73smb_vsyslog(int pri, const char *fmt, va_list ap)
74{
75	va_list tap;
76
77	va_copy(tap, ap);
78	smb_svc_log(pri, fmt, tap);
79	va_end(tap);
80
81	vsyslog(pri, fmt, ap);
82}
83
84/*
85 * An override for libsmb:smb_trace().  As the comment there says:
86 *
87 * This function is designed to be used with dtrace, i.e. see:
88 * usr/src/cmd/smbsrv/dtrace/smbd-all.d
89 *
90 * Outside of dtrace, the messages passed to this function usually
91 * lack sufficient context to be useful, so don't log them.
92 * However, if you insist, set debug >= 3 and this will log them.
93 */
94void
95smb_trace(const char *s)
96{
97	if (smbd.s_debug >= 3)
98		(void) fprintf(stdout, "smbd.trace: %s\n", s);
99}
100