1/*
2 * Copyright (c) 2000, Boris Popov
3 * All rights reserved.
4 *
5 * Portions Copyright (C) 2001 - 2011 Apple Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *    This product includes software developed by Boris Popov.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $Id: subr.c,v 1.20 2006/04/12 04:55:30 lindak Exp $
35 */
36#include <mach/mach.h>
37#include <mach/mach_error.h>
38#include <servers/bootstrap.h>
39#include <IOKit/kext/KextManager.h>
40
41#include <netsmb/netbios.h>
42#include <netsmb/smb_lib.h>
43#include <netsmb/nb_lib.h>
44
45#include <smbfs/smbfs.h>
46
47#define CFENVFORMATSTRING "__CF_USER_TEXT_ENCODING=0x%X:0:0"
48
49void smb_ctx_hexdump(const char *func, const char *s, unsigned char *buf, size_t inlen)
50{
51	char printstr[512];
52	size_t maxlen;
53	char *strPtr;
54    int32_t addr, len = (int32_t)inlen;
55    int32_t i;
56
57	smb_log_info("%s: %s %p length %ld", ASL_LEVEL_DEBUG,  func, s, buf, inlen);
58	if (buf == NULL) {
59		return;
60	}
61    addr = 0;
62    while( addr < len )
63    {
64		strPtr = printstr;
65		maxlen = sizeof(printstr);
66        strPtr += snprintf(strPtr, maxlen, "%6.6x - " , addr );
67		maxlen -= (strPtr - printstr);
68        for( i=0; i<16; i++ )
69        {
70            if( addr+i < len )
71				strPtr += snprintf(strPtr, maxlen, "%2.2x ", buf[addr+i]);
72            else
73 				strPtr += snprintf(strPtr, maxlen, "   ");
74			maxlen -= (strPtr - printstr);
75       }
76		strPtr += snprintf(strPtr, maxlen, " \"");
77		maxlen -= (strPtr - printstr);
78        for( i=0; i<16; i++ )
79        {
80            if( addr+i < len )
81            {
82                if(( buf[addr+i] > 0x19 ) && ( buf[addr+i] < 0x7e ) )
83					strPtr += snprintf(strPtr, maxlen, "%c", buf[addr+i] );
84                else
85					strPtr += snprintf(strPtr, maxlen, ".");
86				maxlen -= (strPtr - printstr);
87            }
88        }
89		smb_log_info("%s", ASL_LEVEL_DEBUG, printstr);
90        addr += 16;
91    }
92	smb_log_info(" ", ASL_LEVEL_DEBUG);
93}
94
95/*
96 * Generic routine to log information or errors
97 */
98void smb_log_info(const char *fmt, int log_level,...)
99{
100	int save_errno = errno;
101	va_list ap;
102	aslmsg m = asl_new(ASL_TYPE_MSG);
103
104
105	va_start(ap, log_level);
106	asl_vlog(NULL, m, log_level, fmt, ap);
107	va_end(ap);
108	asl_free(m);
109	errno = save_errno; /* Never let this routine change errno */
110}
111
112void LogToMessageTracer(const char *domain, const char *signature,
113						const char *optResult, const char *optValue,
114						const char *fmt,...)
115{
116	aslmsg m;
117	va_list ap;
118
119	if ( (domain == NULL) || (signature == NULL) || (fmt == NULL) ) {
120		/* domain, signature and msg are required */
121		return;
122	}
123
124	m = asl_new(ASL_TYPE_MSG);
125	asl_set(m, "com.apple.message.domain", domain);
126	asl_set(m, "com.apple.message.signature", signature);
127
128	if (optResult != NULL) {
129		asl_set(m, "com.apple.message.result", optResult);
130	}
131	if (optValue != NULL) {
132		asl_set(m, "com.apple.message.value", optValue);
133	}
134
135	va_start(ap, fmt);
136	asl_vlog(NULL, m, ASL_LEVEL_NOTICE, fmt, ap);
137	va_end(ap);
138
139	asl_free(m);
140}
141
142/*
143 * Load our kext
144 */
145int smb_load_library()
146{
147	struct vfsconf vfc;
148	kern_return_t status;
149
150	setlocale(LC_CTYPE, "");
151	if (getvfsbyname(SMBFS_VFSNAME, &vfc) != 0) {
152		/* Need to load the kext */
153		status = KextManagerLoadKextWithIdentifier(CFSTR("com.apple.filesystems.smbfs") ,NULL);
154        if (status != KERN_SUCCESS) {
155			smb_log_info("Loading com.apple.filesystems.smbfs status = %d",
156						 ASL_LEVEL_ERR, status);
157			return EIO;
158        }
159	}
160	return 0;
161}
162