Sync usage with man page.
[netbsd-mini2440.git] / dist / smbfs / lib / smb / subr.c
blobfb9d642254718e6ac25c99b6843545541efd1bb6
1 /*
2 * Copyright (c) 2000, Boris Popov
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * from: Id: subr.c,v 1.12 2001/08/22 03:31:37 bp Exp
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: subr.c,v 1.4 2003/04/11 17:31:58 jdolecek Exp $");
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/errno.h>
41 #include <sys/sysctl.h>
42 #include <sys/syscall.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <err.h>
50 #include <errno.h>
52 #include <netsmb/netbios.h>
53 #include <netsmb/smb_lib.h>
54 #include <netsmb/nb_lib.h>
55 #include <cflib.h>
57 #ifdef APPLE
58 #include <sysexits.h>
59 #include <sys/wait.h>
60 #include <mach/mach.h>
61 #include <mach/mach_error.h>
63 uid_t real_uid, eff_uid;
64 #endif
66 extern char *__progname;
68 static int smblib_initialized;
70 struct rcfile *smb_rc;
72 int
73 smb_lib_init(void)
75 int error;
76 #if __FreeBSD_version > 400000
77 int kv;
78 size_t kvlen = sizeof(kv);
79 #endif
81 if (smblib_initialized)
82 return 0;
83 #if __FreeBSD_version > 400000
84 error = sysctlbyname("net.smb.version", &kv, &kvlen, NULL, 0);
85 if (error) {
86 warnx("%s: can't find kernel module\n", __FUNCTION__);
87 return error;
89 if (NSMB_VERSION != kv) {
90 warnx("%s: kernel module version(%d) don't match library(%d).\n", __FUNCTION__, kv, NSMB_VERSION);
91 return EINVAL;
93 #endif
94 if ((error = nls_setlocale("")) != 0) {
95 warnx("%s: can't initialise locale\n", __func__);
96 return error;
98 smblib_initialized++;
99 return 0;
103 * Print a (descriptive) error message
104 * error values:
105 * 0 - no specific error code available;
106 * 1..32767 - system error
108 void
109 smb_error(const char *fmt, int error,...) {
110 va_list ap;
111 const char *cp;
112 int errtype = error & SMB_ERRTYPE_MASK;
114 fprintf(stderr, "%s: ", __progname);
115 va_start(ap, error);
116 vfprintf(stderr, fmt, ap);
117 va_end(ap);
118 if (error == -1)
119 error = errno;
120 else
121 error &= ~SMB_ERRTYPE_MASK;
122 switch (errtype) {
123 case SMB_SYS_ERROR:
124 if (error)
125 fprintf(stderr, ": syserr = %s\n", strerror(error));
126 else
127 fprintf(stderr, "\n");
128 break;
129 case SMB_RAP_ERROR:
130 fprintf(stderr, ": raperr = %d (0x%04x)\n", error, error);
131 break;
132 case SMB_NB_ERROR:
133 cp = nb_strerror(error);
134 if (cp == NULL)
135 fprintf(stderr, ": nberr = unknown (0x%04x)\n", error);
136 else
137 fprintf(stderr, ": nberr = %s\n", cp);
138 break;
139 default:
140 fprintf(stderr, "\n");
144 char *
145 smb_printb(char *dest, int flags, const struct smb_bitname *bnp) {
146 int first = 1;
148 strcpy(dest, "<");
149 for(; bnp->bn_bit; bnp++) {
150 if (flags & bnp->bn_bit) {
151 strcat(dest, bnp->bn_name);
152 first = 0;
154 if (!first && (flags & bnp[1].bn_bit))
155 strcat(dest, "|");
157 strcat(dest, ">");
158 return dest;
162 * first read ~/.smbrc, next try to merge SMB_CFG_FILE
165 smb_open_rcfile(void)
167 char *home, *fn;
168 int error;
170 home = getenv("HOME");
171 if (home) {
172 fn = malloc(strlen(home) + 20);
173 sprintf(fn, "%s/.nsmbrc", home);
174 error = rc_open(fn, "r", &smb_rc);
175 free(fn);
177 error = rc_merge(SMB_CFG_FILE, &smb_rc);
178 if (smb_rc == NULL)
179 return ENOENT;
180 return 0;
183 void *
184 smb_dumptree(void)
186 #ifdef __NetBSD__
187 /* XXX not supported on NetBSD */
188 return NULL;
189 #else
190 size_t len;
191 void *p;
192 int error;
194 #ifdef APPLE
195 seteuid(eff_uid); /* restore setuid root briefly */
196 #endif
197 error = sysctlbyname("net.smb.treedump", NULL, &len, NULL, 0);
198 #ifdef APPLE
199 seteuid(real_uid); /* and back to real user */
200 #endif
201 if (error)
202 return NULL;
203 p = malloc(len);
204 if (p == NULL)
205 return NULL;
206 #ifdef APPLE
207 seteuid(eff_uid); /* restore setuid root briefly */
208 #endif
209 error = sysctlbyname("net.smb.treedump", p, &len, NULL, 0);
210 #ifdef APPLE
211 seteuid(real_uid); /* and back to real user */
212 #endif
213 if (error) {
214 free(p);
215 return NULL;
217 return p;
218 #endif /* __NetBSD__ */
221 char *
222 smb_simplecrypt(char *dst, const char *src)
224 int ch, pos;
225 char *dp;
227 if (dst == NULL) {
228 dst = malloc(4 + 2 * strlen(src));
229 if (dst == NULL)
230 return NULL;
232 dp = dst;
233 *dst++ = '$';
234 *dst++ = '$';
235 *dst++ = '1';
236 pos = 27;
237 while (*src) {
238 ch = *src++;
239 if (isascii(ch))
240 ch = (isupper(ch) ? ('A' + (ch - 'A' + 13) % 26) :
241 islower(ch) ? ('a' + (ch - 'a' + 13) % 26) : ch);
242 ch ^= pos;
243 pos += 13;
244 sprintf(dst, "%02x", ch);
245 dst += 2;
247 *dst = 0;
248 return dp;
252 smb_simpledecrypt(char *dst, const char *src)
254 char *ep, hexval[3];
255 int len, ch, pos;
257 if (strncmp(src, "$$1", 3) != 0)
258 return EINVAL;
259 src += 3;
260 len = strlen(src);
261 if (len & 1)
262 return EINVAL;
263 len /= 2;
264 hexval[2] = 0;
265 pos = 27;
266 while (len--) {
267 hexval[0] = *src++;
268 hexval[1] = *src++;
269 ch = strtoul(hexval, &ep, 16);
270 if (*ep != 0)
271 return EINVAL;
272 ch ^= pos;
273 pos += 13;
274 if (isascii(ch))
275 ch = (isupper(ch) ? ('A' + (ch - 'A' + 13) % 26) :
276 islower(ch) ? ('a' + (ch - 'a' + 13) % 26) : ch);
277 *dst++ = ch;
279 *dst = 0;
280 return 0;
284 #ifdef APPLE
285 static int
286 safe_execv(char *args[])
288 int pid;
289 union wait status;
291 pid = fork();
292 if (pid == 0) {
293 (void)execv(args[0], args);
294 errx(EX_OSERR, "%s: execv %s failed, %s\n", __progname,
295 args[0], strerror(errno));
297 if (pid == -1) {
298 fprintf(stderr, "%s: fork failed, %s\n", __progname,
299 strerror(errno));
300 return (1);
302 if (wait4(pid, (int *)&status, 0, NULL) != pid) {
303 fprintf(stderr, "%s: BUG executing %s command\n", __progname,
304 args[0]);
305 return (1);
306 } else if (!WIFEXITED(status)) {
307 fprintf(stderr, "%s: %s command aborted by signal %d\n",
308 __progname, args[0], WTERMSIG(status));
309 return (1);
310 } else if (WEXITSTATUS(status)) {
311 fprintf(stderr, "%s: %s command failed, exit status %d: %s\n",
312 __progname, args[0], WEXITSTATUS(status),
313 strerror(WEXITSTATUS(status)));
314 return (1);
316 return (0);
320 void
321 dropsuid()
323 /* drop setuid root privs asap */
324 eff_uid = geteuid();
325 real_uid = getuid();
326 seteuid(real_uid);
327 return;
331 static int
332 kextisloaded(char * kextname)
334 mach_port_t kernel_port;
335 kmod_info_t *k, *loaded_modules = 0;
336 int err, loaded_count = 0;
338 /* on error return not loaded - to make loadsmbvfs fail */
340 err = task_for_pid(mach_task_self(), 0, &kernel_port);
341 if (err) {
342 fprintf(stderr, "%s: %s: %s\n", __progname,
343 "unable to get kernel task port",
344 mach_error_string(err));
345 return (0);
347 err = kmod_get_info(kernel_port, (void *)&loaded_modules,
348 &loaded_count); /* never freed */
349 if (err) {
350 fprintf(stderr, "%s: %s: %s\n", __progname,
351 "kmod_get_info() failed",
352 mach_error_string(err));
353 return (0);
355 for (k = loaded_modules; k; k = k->next ? k+1 : 0)
356 if (!strcmp(k->name, kextname))
357 return (1);
358 return (0);
362 #define KEXTLOAD_COMMAND "/sbin/kextload"
363 #define FS_KEXT_DIR "/System/Library/Extensions/smbfs.kext"
364 #define FULL_KEXTNAME "com.apple.filesystems.smbfs"
368 loadsmbvfs()
370 const char *kextargs[] = {KEXTLOAD_COMMAND, FS_KEXT_DIR, NULL};
371 int error = 0;
374 * temporarily revert to root (required for kextload)
376 seteuid(eff_uid);
377 if (!kextisloaded(FULL_KEXTNAME)) {
378 error = safe_execv(kextargs);
379 if (!error)
380 error = !kextisloaded(FULL_KEXTNAME);
382 seteuid(real_uid); /* and back to real user */
383 return (error);
385 #endif /* APPLE */