1f1b9d127SSheldon Hearn /* 2f1b9d127SSheldon Hearn * Copyright (c) 2000, Boris Popov 3f1b9d127SSheldon Hearn * All rights reserved. 4f1b9d127SSheldon Hearn * 5f1b9d127SSheldon Hearn * Redistribution and use in source and binary forms, with or without 6f1b9d127SSheldon Hearn * modification, are permitted provided that the following conditions 7f1b9d127SSheldon Hearn * are met: 8f1b9d127SSheldon Hearn * 1. Redistributions of source code must retain the above copyright 9f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer. 10f1b9d127SSheldon Hearn * 2. Redistributions in binary form must reproduce the above copyright 11f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer in the 12f1b9d127SSheldon Hearn * documentation and/or other materials provided with the distribution. 13f1b9d127SSheldon Hearn * 3. All advertising materials mentioning features or use of this software 14f1b9d127SSheldon Hearn * must display the following acknowledgement: 15f1b9d127SSheldon Hearn * This product includes software developed by Boris Popov. 16f1b9d127SSheldon Hearn * 4. Neither the name of the author nor the names of any co-contributors 17f1b9d127SSheldon Hearn * may be used to endorse or promote products derived from this software 18f1b9d127SSheldon Hearn * without specific prior written permission. 19f1b9d127SSheldon Hearn * 20f1b9d127SSheldon Hearn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21f1b9d127SSheldon Hearn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22f1b9d127SSheldon Hearn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23f1b9d127SSheldon Hearn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24f1b9d127SSheldon Hearn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25f1b9d127SSheldon Hearn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26f1b9d127SSheldon Hearn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27f1b9d127SSheldon Hearn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28f1b9d127SSheldon Hearn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29f1b9d127SSheldon Hearn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30f1b9d127SSheldon Hearn * SUCH DAMAGE. 31f1b9d127SSheldon Hearn * 32f1b9d127SSheldon Hearn * $Id: subr.c,v 1.11 2001/04/16 04:33:01 bp Exp $ 33f1b9d127SSheldon Hearn */ 34f1b9d127SSheldon Hearn 35f1b9d127SSheldon Hearn #include <sys/param.h> 36f1b9d127SSheldon Hearn #include <sys/types.h> 37f1b9d127SSheldon Hearn #include <sys/errno.h> 38f1b9d127SSheldon Hearn #include <sys/sysctl.h> 39f1b9d127SSheldon Hearn #include <sys/syscall.h> 40f1b9d127SSheldon Hearn #include <unistd.h> 41f1b9d127SSheldon Hearn #include <ctype.h> 42f1b9d127SSheldon Hearn #include <string.h> 43f1b9d127SSheldon Hearn #include <stdio.h> 44f1b9d127SSheldon Hearn #include <stdlib.h> 45f1b9d127SSheldon Hearn #include <stdarg.h> 46f1b9d127SSheldon Hearn #include <err.h> 47f1b9d127SSheldon Hearn 48f1b9d127SSheldon Hearn #include <netsmb/netbios.h> 49f1b9d127SSheldon Hearn #include <netsmb/smb_lib.h> 50f1b9d127SSheldon Hearn #include <netsmb/nb_lib.h> 51f1b9d127SSheldon Hearn #include <cflib.h> 52f1b9d127SSheldon Hearn 53f1b9d127SSheldon Hearn extern char *__progname; 54f1b9d127SSheldon Hearn 55f1b9d127SSheldon Hearn static int smblib_initialized; 56f1b9d127SSheldon Hearn 57f1b9d127SSheldon Hearn struct rcfile *smb_rc; 58f1b9d127SSheldon Hearn 59f1b9d127SSheldon Hearn int 60f1b9d127SSheldon Hearn smb_lib_init(void) 61f1b9d127SSheldon Hearn { 62f1b9d127SSheldon Hearn int error; 63f1b9d127SSheldon Hearn int kv; 64f1b9d127SSheldon Hearn size_t kvlen = sizeof(kv); 65f1b9d127SSheldon Hearn 66f1b9d127SSheldon Hearn if (smblib_initialized) 67f1b9d127SSheldon Hearn return 0; 68f1b9d127SSheldon Hearn #if __FreeBSD_version > 400000 69f1b9d127SSheldon Hearn error = sysctlbyname("net.smb.version", &kv, &kvlen, NULL, 0); 70f1b9d127SSheldon Hearn if (error) { 71f1b9d127SSheldon Hearn warnx("%s: can't find kernel module\n", __FUNCTION__); 72f1b9d127SSheldon Hearn return error; 73f1b9d127SSheldon Hearn } 74f1b9d127SSheldon Hearn if (NSMB_VERSION != kv) { 75f1b9d127SSheldon Hearn warnx("%s: kernel module version(%d) don't match library(%d).\n", __FUNCTION__, kv, NSMB_VERSION); 76f1b9d127SSheldon Hearn return EINVAL; 77f1b9d127SSheldon Hearn } 78f1b9d127SSheldon Hearn #endif 79f1b9d127SSheldon Hearn if ((error = nls_setlocale("")) != 0) { 80f1b9d127SSheldon Hearn warnx("%s: can't initialise locale\n", __FUNCTION__); 81f1b9d127SSheldon Hearn return error; 82f1b9d127SSheldon Hearn } 83f1b9d127SSheldon Hearn smblib_initialized++; 84f1b9d127SSheldon Hearn return 0; 85f1b9d127SSheldon Hearn } 86f1b9d127SSheldon Hearn 87f1b9d127SSheldon Hearn /* 88f1b9d127SSheldon Hearn * Print a (descriptive) error message 89f1b9d127SSheldon Hearn * error values: 90f1b9d127SSheldon Hearn * 0 - no specific error code available; 91f1b9d127SSheldon Hearn * 1..32767 - system error 92f1b9d127SSheldon Hearn */ 93f1b9d127SSheldon Hearn void 94f1b9d127SSheldon Hearn smb_error(const char *fmt, int error,...) { 95f1b9d127SSheldon Hearn va_list ap; 96f1b9d127SSheldon Hearn const char *cp; 97f1b9d127SSheldon Hearn int errtype = error & SMB_ERRTYPE_MASK; 98f1b9d127SSheldon Hearn 99f1b9d127SSheldon Hearn fprintf(stderr, "%s: ", __progname); 100f1b9d127SSheldon Hearn va_start(ap, error); 101f1b9d127SSheldon Hearn vfprintf(stderr, fmt, ap); 102f1b9d127SSheldon Hearn va_end(ap); 103f1b9d127SSheldon Hearn if (error == -1) 104f1b9d127SSheldon Hearn error = errno; 105f1b9d127SSheldon Hearn else 106f1b9d127SSheldon Hearn error &= ~SMB_ERRTYPE_MASK; 107f1b9d127SSheldon Hearn switch (errtype) { 108f1b9d127SSheldon Hearn case SMB_SYS_ERROR: 109f1b9d127SSheldon Hearn if (error) 110f1b9d127SSheldon Hearn fprintf(stderr, ": syserr = %s\n", strerror(error)); 111f1b9d127SSheldon Hearn else 112f1b9d127SSheldon Hearn fprintf(stderr, "\n"); 113f1b9d127SSheldon Hearn break; 114f1b9d127SSheldon Hearn case SMB_RAP_ERROR: 115f1b9d127SSheldon Hearn fprintf(stderr, ": raperr = %d (0x%04x)\n", error, error); 116f1b9d127SSheldon Hearn break; 117f1b9d127SSheldon Hearn case SMB_NB_ERROR: 118f1b9d127SSheldon Hearn cp = nb_strerror(error); 119f1b9d127SSheldon Hearn if (cp == NULL) 120f1b9d127SSheldon Hearn fprintf(stderr, ": nberr = unknown (0x%04x)\n", error); 121f1b9d127SSheldon Hearn else 122f1b9d127SSheldon Hearn fprintf(stderr, ": nberr = %s\n", cp); 123f1b9d127SSheldon Hearn break; 124f1b9d127SSheldon Hearn default: 125f1b9d127SSheldon Hearn fprintf(stderr, "\n"); 126f1b9d127SSheldon Hearn } 127f1b9d127SSheldon Hearn } 128f1b9d127SSheldon Hearn 129f1b9d127SSheldon Hearn char * 130f1b9d127SSheldon Hearn smb_printb(char *dest, int flags, const struct smb_bitname *bnp) { 131f1b9d127SSheldon Hearn int first = 1; 132f1b9d127SSheldon Hearn 133f1b9d127SSheldon Hearn strcpy(dest, "<"); 134f1b9d127SSheldon Hearn for(; bnp->bn_bit; bnp++) { 135f1b9d127SSheldon Hearn if (flags & bnp->bn_bit) { 136f1b9d127SSheldon Hearn strcat(dest, bnp->bn_name); 137f1b9d127SSheldon Hearn first = 0; 138f1b9d127SSheldon Hearn } 139f1b9d127SSheldon Hearn if (!first && (flags & bnp[1].bn_bit)) 140f1b9d127SSheldon Hearn strcat(dest, "|"); 141f1b9d127SSheldon Hearn } 142f1b9d127SSheldon Hearn strcat(dest, ">"); 143f1b9d127SSheldon Hearn return dest; 144f1b9d127SSheldon Hearn } 145f1b9d127SSheldon Hearn 146f1b9d127SSheldon Hearn /* 147f1b9d127SSheldon Hearn * first read ~/.smbrc, next try to merge SMB_CFG_FILE 148f1b9d127SSheldon Hearn */ 149f1b9d127SSheldon Hearn int 150f1b9d127SSheldon Hearn smb_open_rcfile(void) 151f1b9d127SSheldon Hearn { 152f1b9d127SSheldon Hearn char *home, *fn; 153f1b9d127SSheldon Hearn int error; 154f1b9d127SSheldon Hearn 155f1b9d127SSheldon Hearn home = getenv("HOME"); 156f1b9d127SSheldon Hearn if (home) { 157f1b9d127SSheldon Hearn fn = malloc(strlen(home) + 20); 158f1b9d127SSheldon Hearn sprintf(fn, "%s/.nsmbrc", home); 159f1b9d127SSheldon Hearn error = rc_open(fn, "r", &smb_rc); 160f1b9d127SSheldon Hearn free(fn); 161f1b9d127SSheldon Hearn } 162f1b9d127SSheldon Hearn error = rc_merge(SMB_CFG_FILE, &smb_rc); 163f1b9d127SSheldon Hearn if (smb_rc == NULL) { 164f1b9d127SSheldon Hearn printf("Warning: no cfg file(s) found.\n"); 165f1b9d127SSheldon Hearn return ENOENT; 166f1b9d127SSheldon Hearn } 167f1b9d127SSheldon Hearn return 0; 168f1b9d127SSheldon Hearn } 169f1b9d127SSheldon Hearn 170f1b9d127SSheldon Hearn void * 171f1b9d127SSheldon Hearn smb_dumptree(void) 172f1b9d127SSheldon Hearn { 173f1b9d127SSheldon Hearn size_t len; 174f1b9d127SSheldon Hearn void *p; 175f1b9d127SSheldon Hearn int error; 176f1b9d127SSheldon Hearn 177f1b9d127SSheldon Hearn error = sysctlbyname("net.smb.treedump", NULL, &len, NULL, 0); 178f1b9d127SSheldon Hearn if (error) 179f1b9d127SSheldon Hearn return NULL; 180f1b9d127SSheldon Hearn p = malloc(len); 181f1b9d127SSheldon Hearn if (p == NULL) 182f1b9d127SSheldon Hearn return NULL; 183f1b9d127SSheldon Hearn error = sysctlbyname("net.smb.treedump", p, &len, NULL, 0); 184f1b9d127SSheldon Hearn if (error) { 185f1b9d127SSheldon Hearn free(p); 186f1b9d127SSheldon Hearn return NULL; 187f1b9d127SSheldon Hearn } 188f1b9d127SSheldon Hearn return p; 189f1b9d127SSheldon Hearn } 190f1b9d127SSheldon Hearn 191f1b9d127SSheldon Hearn void 192f1b9d127SSheldon Hearn smb_simplecrypt(char *dst, const char *src) 193f1b9d127SSheldon Hearn { 194f1b9d127SSheldon Hearn int ch, pos; 195f1b9d127SSheldon Hearn 196f1b9d127SSheldon Hearn *dst++ = '$'; 197f1b9d127SSheldon Hearn *dst++ = '$'; 198f1b9d127SSheldon Hearn *dst++ = '1'; 199f1b9d127SSheldon Hearn pos = 27; 200f1b9d127SSheldon Hearn while (*src) { 201f1b9d127SSheldon Hearn ch = *src++; 202f1b9d127SSheldon Hearn if (isascii(ch)) 203f1b9d127SSheldon Hearn ch = (isupper(ch) ? ('A' + (ch - 'A' + 13) % 26) : 204f1b9d127SSheldon Hearn islower(ch) ? ('a' + (ch - 'a' + 13) % 26) : ch); 205f1b9d127SSheldon Hearn ch ^= pos; 206f1b9d127SSheldon Hearn pos += 13; 207f1b9d127SSheldon Hearn sprintf(dst, "%02x", ch); 208f1b9d127SSheldon Hearn dst += 2; 209f1b9d127SSheldon Hearn } 210f1b9d127SSheldon Hearn *dst = 0; 211f1b9d127SSheldon Hearn } 212f1b9d127SSheldon Hearn 213f1b9d127SSheldon Hearn int 214f1b9d127SSheldon Hearn smb_simpledecrypt(char *dst, const char *src) 215f1b9d127SSheldon Hearn { 216f1b9d127SSheldon Hearn char *ep, hexval[3]; 217f1b9d127SSheldon Hearn int len, ch, pos; 218f1b9d127SSheldon Hearn 219f1b9d127SSheldon Hearn if (strncmp(src, "$$1", 3) != 0) 220f1b9d127SSheldon Hearn return EINVAL; 221f1b9d127SSheldon Hearn src += 3; 222f1b9d127SSheldon Hearn len = strlen(src); 223f1b9d127SSheldon Hearn if (len & 1) 224f1b9d127SSheldon Hearn return EINVAL; 225f1b9d127SSheldon Hearn len /= 2; 226f1b9d127SSheldon Hearn hexval[2] = 0; 227f1b9d127SSheldon Hearn pos = 27; 228f1b9d127SSheldon Hearn while (len--) { 229f1b9d127SSheldon Hearn hexval[0] = *src++; 230f1b9d127SSheldon Hearn hexval[1] = *src++; 231f1b9d127SSheldon Hearn ch = strtoul(hexval, &ep, 16); 232f1b9d127SSheldon Hearn if (*ep != 0) 233f1b9d127SSheldon Hearn return EINVAL; 234f1b9d127SSheldon Hearn ch ^= pos; 235f1b9d127SSheldon Hearn pos += 13; 236f1b9d127SSheldon Hearn if (isascii(ch)) 237f1b9d127SSheldon Hearn ch = (isupper(ch) ? ('A' + (ch - 'A' + 13) % 26) : 238f1b9d127SSheldon Hearn islower(ch) ? ('a' + (ch - 'a' + 13) % 26) : ch); 239f1b9d127SSheldon Hearn *dst++ = ch; 240f1b9d127SSheldon Hearn } 241f1b9d127SSheldon Hearn *dst = 0; 242f1b9d127SSheldon Hearn return 0; 243f1b9d127SSheldon Hearn } 244