2 * Copyright (c) 2000, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
32 * $Id: subr.c,v 1.19 2005/02/09 00:23:45 lindak Exp $
35 #include <sys/types.h>
36 #include <sys/errno.h>
49 #include <netsmb/netbios.h>
50 #include <netsmb/smb_lib.h>
51 #include <netsmb/nb_lib.h>
57 static int smblib_initialized
;
64 if (smblib_initialized
)
66 if ((error
= nls_setlocale("")) != 0) {
67 fprintf(stdout
, dgettext(TEXT_DOMAIN
,
68 "%s: can't initialise locale\n"), __progname
);
76 smb_getlocalname(char **namepp
)
78 char buf
[SMBIOC_MAX_NAME
], *cp
;
80 if (gethostname(buf
, sizeof (buf
)) != 0)
82 cp
= strchr(buf
, '.');
93 * Private version of strerror(3C) that
94 * knows our special error codes.
103 msg
= dgettext(TEXT_DOMAIN
,
104 "remote call failed");
107 msg
= dgettext(TEXT_DOMAIN
,
108 "authentication failed");
119 * Print a (descriptive) error message
121 * 0 - no specific error code available;
122 * 1..32767 - system error
125 smb_error(const char *fmt
, int error
, ...) {
130 fprintf(stderr
, "%s: ", __progname
);
132 vfprintf(stderr
, fmt
, ap
);
136 errtype
= SMB_SYS_ERROR
;
138 errtype
= error
& SMB_ERRTYPE_MASK
;
139 error
&= ~SMB_ERRTYPE_MASK
;
144 fprintf(stderr
, ": syserr = %s\n", smb_strerror(error
));
146 fprintf(stderr
, "\n");
149 fprintf(stderr
, ": raperr = %d (0x%04x)\n", error
, error
);
152 cp
= nb_strerror(error
);
154 fprintf(stderr
, ": nberr = unknown (0x%04x)\n", error
);
156 fprintf(stderr
, ": nberr = %s\n", cp
);
159 fprintf(stderr
, "\n");
164 smb_printb(char *dest
, int flags
, const struct smb_bitname
*bnp
) {
168 for (; bnp
->bn_bit
; bnp
++) {
169 if (flags
& bnp
->bn_bit
) {
170 strcat(dest
, bnp
->bn_name
);
173 if (!first
&& (flags
& bnp
[1].bn_bit
))
181 smb_simplecrypt(char *dst
, const char *src
)
192 ch
= (isupper(ch
) ? ('A' + (ch
- 'A' + 13) % 26) :
193 islower(ch
) ? ('a' + (ch
- 'a' + 13) % 26) : ch
);
196 sprintf(dst
, "%02x", ch
);
203 smb_simpledecrypt(char *dst
, const char *src
)
208 if (strncmp(src
, "$$1", 3) != 0)
220 ch
= strtoul(hexval
, &ep
, 16);
226 ch
= (isupper(ch
) ? ('A' + (ch
- 'A' + 13) % 26) :
227 islower(ch
) ? ('a' + (ch
- 'a' + 13) % 26) : ch
);
235 * Number of seconds between 1970 and 1601 year
236 * (134774 * 24 * 60 * 60)
238 static const uint64_t DIFF1970TO1601
= 11644473600ULL;
241 smb_time_local2server(struct timeval
*tsp
, int tzoff
, long *seconds
)
243 *seconds
= tsp
->tv_sec
- tzoff
* 60;
247 smb_time_server2local(ulong_t seconds
, int tzoff
, struct timeval
*tsp
)
249 tsp
->tv_sec
= seconds
+ tzoff
* 60;
254 * Time from server comes as UTC, so no need to use tz
258 smb_time_NT2local(uint64_t nsec
, int tzoff
, struct timeval
*tsp
)
260 smb_time_server2local(nsec
/ 10000000 - DIFF1970TO1601
, 0, tsp
);
265 smb_time_local2NT(struct timeval
*tsp
, int tzoff
, uint64_t *nsec
)
269 smb_time_local2server(tsp
, 0, &seconds
);
270 *nsec
= (((uint64_t)(seconds
) & ~1) + DIFF1970TO1601
) *
275 smb_hexdump(const void *buf
, int len
)
277 const uchar_t
*p
= buf
;
282 fprintf(stderr
, "%02X: ", ofs
);
283 fprintf(stderr
, "%02x ", *p
++);
286 fprintf(stderr
, "\n");
289 fprintf(stderr
, "\n");
293 dprint(const char *fname
, const char *fmt
, ...)
300 fprintf(stderr
, "%s: ", fname
);
301 vfprintf(stderr
, fmt
, ap
);
302 fprintf(stderr
, "\n");
309 char *__progname
= NULL
;
316 if (__progname
== NULL
) {
317 __progname
= (char *)getexecname();
318 if ((p
= strrchr(__progname
, '/')) != 0)