1 | /*
|
---|
2 | Unix SMB/Netbios implementation.
|
---|
3 | SMB client library implementation
|
---|
4 | Copyright (C) Andrew Tridgell 1998
|
---|
5 | Copyright (C) Richard Sharpe 2000, 2002
|
---|
6 | Copyright (C) John Terpstra 2000
|
---|
7 | Copyright (C) Tom Jansen (Ninja ISD) 2002
|
---|
8 | Copyright (C) Derrell Lipman 2003-2008
|
---|
9 | Copyright (C) Jeremy Allison 2007, 2008
|
---|
10 |
|
---|
11 | This program is free software; you can redistribute it and/or modify
|
---|
12 | it under the terms of the GNU General Public License as published by
|
---|
13 | the Free Software Foundation; either version 3 of the License, or
|
---|
14 | (at your option) any later version.
|
---|
15 |
|
---|
16 | This program is distributed in the hope that it will be useful,
|
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | GNU General Public License for more details.
|
---|
20 |
|
---|
21 | You should have received a copy of the GNU General Public License
|
---|
22 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "includes.h"
|
---|
26 | #include "libsmbclient.h"
|
---|
27 | #include "libsmb_internal.h"
|
---|
28 |
|
---|
29 |
|
---|
30 | /*
|
---|
31 | * Is the logging working / configfile read ?
|
---|
32 | */
|
---|
33 | static bool SMBC_initialized = false;
|
---|
34 | static unsigned int initialized_ctx_count = 0;
|
---|
35 | static void *initialized_ctx_count_mutex = NULL;
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Do some module- and library-wide intializations
|
---|
39 | */
|
---|
40 | static void
|
---|
41 | SMBC_module_init(void * punused)
|
---|
42 | {
|
---|
43 | bool conf_loaded = False;
|
---|
44 | char *home = NULL;
|
---|
45 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
46 |
|
---|
47 | load_case_tables();
|
---|
48 |
|
---|
49 | setup_logging("libsmbclient", True);
|
---|
50 |
|
---|
51 | /* Here we would open the smb.conf file if needed ... */
|
---|
52 |
|
---|
53 | lp_set_in_client(True);
|
---|
54 |
|
---|
55 | home = getenv("HOME");
|
---|
56 | if (home) {
|
---|
57 | char *conf = NULL;
|
---|
58 | if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
|
---|
59 | if (lp_load(conf, True, False, False, True)) {
|
---|
60 | conf_loaded = True;
|
---|
61 | } else {
|
---|
62 | DEBUG(5, ("Could not load config file: %s\n",
|
---|
63 | conf));
|
---|
64 | }
|
---|
65 | SAFE_FREE(conf);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (!conf_loaded) {
|
---|
70 | /*
|
---|
71 | * Well, if that failed, try the get_dyn_CONFIGFILE
|
---|
72 | * Which points to the standard locn, and if that
|
---|
73 | * fails, silently ignore it and use the internal
|
---|
74 | * defaults ...
|
---|
75 | */
|
---|
76 |
|
---|
77 | if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
|
---|
78 | DEBUG(5, ("Could not load config file: %s\n",
|
---|
79 | get_dyn_CONFIGFILE()));
|
---|
80 | } else if (home) {
|
---|
81 | char *conf;
|
---|
82 | /*
|
---|
83 | * We loaded the global config file. Now lets
|
---|
84 | * load user-specific modifications to the
|
---|
85 | * global config.
|
---|
86 | */
|
---|
87 | if (asprintf(&conf,
|
---|
88 | "%s/.smb/smb.conf.append",
|
---|
89 | home) > 0) {
|
---|
90 | if (!lp_load(conf, True, False, False, False)) {
|
---|
91 | DEBUG(10,
|
---|
92 | ("Could not append config file: "
|
---|
93 | "%s\n",
|
---|
94 | conf));
|
---|
95 | }
|
---|
96 | SAFE_FREE(conf);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | load_interfaces(); /* Load the list of interfaces ... */
|
---|
102 |
|
---|
103 | reopen_logs(); /* Get logging working ... */
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Block SIGPIPE (from lib/util_sock.c: write())
|
---|
107 | * It is not needed and should not stop execution
|
---|
108 | */
|
---|
109 | BlockSignals(True, SIGPIPE);
|
---|
110 |
|
---|
111 | /* Create the mutex we'll use to protect initialized_ctx_count */
|
---|
112 | if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
|
---|
113 | initialized_ctx_count_mutex) != 0) {
|
---|
114 | smb_panic("SMBC_module_init: "
|
---|
115 | "failed to create 'initialized_ctx_count' mutex");
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | TALLOC_FREE(frame);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | static void
|
---|
124 | SMBC_module_terminate(void)
|
---|
125 | {
|
---|
126 | secrets_shutdown();
|
---|
127 | gfree_all();
|
---|
128 | SMBC_initialized = false;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Get a new empty handle to fill in with your own info
|
---|
134 | */
|
---|
135 | SMBCCTX *
|
---|
136 | smbc_new_context(void)
|
---|
137 | {
|
---|
138 | SMBCCTX *context;
|
---|
139 |
|
---|
140 | /* The first call to this function should initialize the module */
|
---|
141 | SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);
|
---|
142 |
|
---|
143 | /*
|
---|
144 | * All newly added context fields should be placed in
|
---|
145 | * SMBC_internal_data, not directly in SMBCCTX.
|
---|
146 | */
|
---|
147 | context = SMB_MALLOC_P(SMBCCTX);
|
---|
148 | if (!context) {
|
---|
149 | errno = ENOMEM;
|
---|
150 | return NULL;
|
---|
151 | }
|
---|
152 |
|
---|
153 | ZERO_STRUCTP(context);
|
---|
154 |
|
---|
155 | context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
|
---|
156 | if (!context->internal) {
|
---|
157 | SAFE_FREE(context);
|
---|
158 | errno = ENOMEM;
|
---|
159 | return NULL;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /* Initialize the context and establish reasonable defaults */
|
---|
163 | ZERO_STRUCTP(context->internal);
|
---|
164 |
|
---|
165 | smbc_setDebug(context, 0);
|
---|
166 | smbc_setTimeout(context, 20000);
|
---|
167 |
|
---|
168 | smbc_setOptionFullTimeNames(context, False);
|
---|
169 | smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
|
---|
170 | smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
|
---|
171 | smbc_setOptionUseCCache(context, True);
|
---|
172 | smbc_setOptionCaseSensitive(context, False);
|
---|
173 | smbc_setOptionBrowseMaxLmbCount(context, 3); /* # LMBs to query */
|
---|
174 | smbc_setOptionUrlEncodeReaddirEntries(context, False);
|
---|
175 | smbc_setOptionOneSharePerServer(context, False);
|
---|
176 | if (getenv("LIBSMBCLIENT_NO_CCACHE") == NULL) {
|
---|
177 | smbc_setOptionUseCCache(context, true);
|
---|
178 | }
|
---|
179 |
|
---|
180 | smbc_setFunctionAuthData(context, SMBC_get_auth_data);
|
---|
181 | smbc_setFunctionCheckServer(context, SMBC_check_server);
|
---|
182 | smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
|
---|
183 |
|
---|
184 | smbc_setOptionUserData(context, NULL);
|
---|
185 | smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
|
---|
186 | smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
|
---|
187 | smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
|
---|
188 | smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
|
---|
189 |
|
---|
190 | smbc_setFunctionOpen(context, SMBC_open_ctx);
|
---|
191 | smbc_setFunctionCreat(context, SMBC_creat_ctx);
|
---|
192 | smbc_setFunctionRead(context, SMBC_read_ctx);
|
---|
193 | smbc_setFunctionWrite(context, SMBC_write_ctx);
|
---|
194 | smbc_setFunctionClose(context, SMBC_close_ctx);
|
---|
195 | smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
|
---|
196 | smbc_setFunctionRename(context, SMBC_rename_ctx);
|
---|
197 | smbc_setFunctionLseek(context, SMBC_lseek_ctx);
|
---|
198 | smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
|
---|
199 | smbc_setFunctionStat(context, SMBC_stat_ctx);
|
---|
200 | smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx);
|
---|
201 | smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx);
|
---|
202 | smbc_setFunctionFstat(context, SMBC_fstat_ctx);
|
---|
203 | smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
|
---|
204 | smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
|
---|
205 | smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
|
---|
206 | smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
|
---|
207 | smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
|
---|
208 | smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
|
---|
209 | smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
|
---|
210 | smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
|
---|
211 | smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
|
---|
212 | smbc_setFunctionChmod(context, SMBC_chmod_ctx);
|
---|
213 | smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
|
---|
214 | smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
|
---|
215 | smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
|
---|
216 | smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
|
---|
217 | smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
|
---|
218 |
|
---|
219 | smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
|
---|
220 | smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
|
---|
221 | smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
|
---|
222 | smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
|
---|
223 |
|
---|
224 | return context;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Free a context
|
---|
229 | *
|
---|
230 | * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
|
---|
231 | * and thus you'll be leaking memory if not handled properly.
|
---|
232 | *
|
---|
233 | */
|
---|
234 | int
|
---|
235 | smbc_free_context(SMBCCTX *context,
|
---|
236 | int shutdown_ctx)
|
---|
237 | {
|
---|
238 | if (!context) {
|
---|
239 | errno = EBADF;
|
---|
240 | return 1;
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (shutdown_ctx) {
|
---|
244 | SMBCFILE * f;
|
---|
245 | DEBUG(1,("Performing aggressive shutdown.\n"));
|
---|
246 |
|
---|
247 | f = context->internal->files;
|
---|
248 | while (f) {
|
---|
249 | smbc_getFunctionClose(context)(context, f);
|
---|
250 | f = f->next;
|
---|
251 | }
|
---|
252 | context->internal->files = NULL;
|
---|
253 |
|
---|
254 | /* First try to remove the servers the nice way. */
|
---|
255 | if (smbc_getFunctionPurgeCachedServers(context)(context)) {
|
---|
256 | SMBCSRV * s;
|
---|
257 | SMBCSRV * next;
|
---|
258 | DEBUG(1, ("Could not purge all servers, "
|
---|
259 | "Nice way shutdown failed.\n"));
|
---|
260 | s = context->internal->servers;
|
---|
261 | while (s) {
|
---|
262 | DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
|
---|
263 | s, s->cli->fd));
|
---|
264 | cli_shutdown(s->cli);
|
---|
265 | smbc_getFunctionRemoveCachedServer(context)(context,
|
---|
266 | s);
|
---|
267 | next = s->next;
|
---|
268 | DLIST_REMOVE(context->internal->servers, s);
|
---|
269 | SAFE_FREE(s);
|
---|
270 | s = next;
|
---|
271 | }
|
---|
272 | context->internal->servers = NULL;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | else {
|
---|
276 | /* This is the polite way */
|
---|
277 | if (smbc_getFunctionPurgeCachedServers(context)(context)) {
|
---|
278 | DEBUG(1, ("Could not purge all servers, "
|
---|
279 | "free_context failed.\n"));
|
---|
280 | errno = EBUSY;
|
---|
281 | return 1;
|
---|
282 | }
|
---|
283 | if (context->internal->servers) {
|
---|
284 | DEBUG(1, ("Active servers in context, "
|
---|
285 | "free_context failed.\n"));
|
---|
286 | errno = EBUSY;
|
---|
287 | return 1;
|
---|
288 | }
|
---|
289 | if (context->internal->files) {
|
---|
290 | DEBUG(1, ("Active files in context, "
|
---|
291 | "free_context failed.\n"));
|
---|
292 | errno = EBUSY;
|
---|
293 | return 1;
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | /* Things we have to clean up */
|
---|
298 | smbc_setWorkgroup(context, NULL);
|
---|
299 | smbc_setNetbiosName(context, NULL);
|
---|
300 | smbc_setUser(context, NULL);
|
---|
301 |
|
---|
302 | DEBUG(3, ("Context %p successfully freed\n", context));
|
---|
303 |
|
---|
304 | /* Free any DFS auth context. */
|
---|
305 | TALLOC_FREE(context->internal->auth_info);
|
---|
306 |
|
---|
307 | SAFE_FREE(context->internal);
|
---|
308 | SAFE_FREE(context);
|
---|
309 |
|
---|
310 | /* Protect access to the count of contexts in use */
|
---|
311 | if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
|
---|
312 | smb_panic("error locking 'initialized_ctx_count'");
|
---|
313 | }
|
---|
314 |
|
---|
315 | if (initialized_ctx_count) {
|
---|
316 | initialized_ctx_count--;
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (initialized_ctx_count == 0) {
|
---|
320 | SMBC_module_terminate();
|
---|
321 | }
|
---|
322 |
|
---|
323 | /* Unlock the mutex */
|
---|
324 | if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
|
---|
325 | smb_panic("error unlocking 'initialized_ctx_count'");
|
---|
326 | }
|
---|
327 |
|
---|
328 | return 0;
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Deprecated interface. Do not use. Instead, use the various
|
---|
334 | * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
|
---|
335 | */
|
---|
336 | void
|
---|
337 | smbc_option_set(SMBCCTX *context,
|
---|
338 | char *option_name,
|
---|
339 | ... /* option_value */)
|
---|
340 | {
|
---|
341 | va_list ap;
|
---|
342 | union {
|
---|
343 | int i;
|
---|
344 | bool b;
|
---|
345 | smbc_get_auth_data_with_context_fn auth_fn;
|
---|
346 | void *v;
|
---|
347 | const char *s;
|
---|
348 | } option_value;
|
---|
349 |
|
---|
350 | va_start(ap, option_name);
|
---|
351 |
|
---|
352 | if (strcmp(option_name, "debug_to_stderr") == 0) {
|
---|
353 | option_value.b = (bool) va_arg(ap, int);
|
---|
354 | smbc_setOptionDebugToStderr(context, option_value.b);
|
---|
355 |
|
---|
356 | } else if (strcmp(option_name, "full_time_names") == 0) {
|
---|
357 | option_value.b = (bool) va_arg(ap, int);
|
---|
358 | smbc_setOptionFullTimeNames(context, option_value.b);
|
---|
359 |
|
---|
360 | } else if (strcmp(option_name, "open_share_mode") == 0) {
|
---|
361 | option_value.i = va_arg(ap, int);
|
---|
362 | smbc_setOptionOpenShareMode(context, option_value.i);
|
---|
363 |
|
---|
364 | } else if (strcmp(option_name, "auth_function") == 0) {
|
---|
365 | option_value.auth_fn =
|
---|
366 | va_arg(ap, smbc_get_auth_data_with_context_fn);
|
---|
367 | smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
|
---|
368 |
|
---|
369 | } else if (strcmp(option_name, "user_data") == 0) {
|
---|
370 | option_value.v = va_arg(ap, void *);
|
---|
371 | smbc_setOptionUserData(context, option_value.v);
|
---|
372 |
|
---|
373 | } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
|
---|
374 | option_value.s = va_arg(ap, const char *);
|
---|
375 | if (strcmp(option_value.s, "none") == 0) {
|
---|
376 | smbc_setOptionSmbEncryptionLevel(context,
|
---|
377 | SMBC_ENCRYPTLEVEL_NONE);
|
---|
378 | } else if (strcmp(option_value.s, "request") == 0) {
|
---|
379 | smbc_setOptionSmbEncryptionLevel(context,
|
---|
380 | SMBC_ENCRYPTLEVEL_REQUEST);
|
---|
381 | } else if (strcmp(option_value.s, "require") == 0) {
|
---|
382 | smbc_setOptionSmbEncryptionLevel(context,
|
---|
383 | SMBC_ENCRYPTLEVEL_REQUIRE);
|
---|
384 | }
|
---|
385 |
|
---|
386 | } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
|
---|
387 | option_value.i = va_arg(ap, int);
|
---|
388 | smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
|
---|
389 |
|
---|
390 | } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
|
---|
391 | option_value.b = (bool) va_arg(ap, int);
|
---|
392 | smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
|
---|
393 |
|
---|
394 | } else if (strcmp(option_name, "one_share_per_server") == 0) {
|
---|
395 | option_value.b = (bool) va_arg(ap, int);
|
---|
396 | smbc_setOptionOneSharePerServer(context, option_value.b);
|
---|
397 |
|
---|
398 | } else if (strcmp(option_name, "use_kerberos") == 0) {
|
---|
399 | option_value.b = (bool) va_arg(ap, int);
|
---|
400 | smbc_setOptionUseKerberos(context, option_value.b);
|
---|
401 |
|
---|
402 | } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
|
---|
403 | option_value.b = (bool) va_arg(ap, int);
|
---|
404 | smbc_setOptionFallbackAfterKerberos(context, option_value.b);
|
---|
405 |
|
---|
406 | } else if (strcmp(option_name, "use_ccache") == 0) {
|
---|
407 | option_value.b = (bool) va_arg(ap, int);
|
---|
408 | smbc_setOptionUseCCache(context, option_value.b);
|
---|
409 |
|
---|
410 | } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
|
---|
411 | option_value.b = (bool) va_arg(ap, int);
|
---|
412 | smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
|
---|
413 | }
|
---|
414 |
|
---|
415 | va_end(ap);
|
---|
416 | }
|
---|
417 |
|
---|
418 |
|
---|
419 | /*
|
---|
420 | * Deprecated interface. Do not use. Instead, use the various
|
---|
421 | * smbc_getOption*() functions.
|
---|
422 | */
|
---|
423 | void *
|
---|
424 | smbc_option_get(SMBCCTX *context,
|
---|
425 | char *option_name)
|
---|
426 | {
|
---|
427 | if (strcmp(option_name, "debug_stderr") == 0) {
|
---|
428 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
429 | return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
|
---|
430 | #else
|
---|
431 | return (void *) smbc_getOptionDebugToStderr(context);
|
---|
432 | #endif
|
---|
433 |
|
---|
434 | } else if (strcmp(option_name, "full_time_names") == 0) {
|
---|
435 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
436 | return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
|
---|
437 | #else
|
---|
438 | return (void *) smbc_getOptionFullTimeNames(context);
|
---|
439 | #endif
|
---|
440 |
|
---|
441 | } else if (strcmp(option_name, "open_share_mode") == 0) {
|
---|
442 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
443 | return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
|
---|
444 | #else
|
---|
445 | return (void *) smbc_getOptionOpenShareMode(context);
|
---|
446 | #endif
|
---|
447 |
|
---|
448 | } else if (strcmp(option_name, "auth_function") == 0) {
|
---|
449 | return (void *) smbc_getFunctionAuthDataWithContext(context);
|
---|
450 |
|
---|
451 | } else if (strcmp(option_name, "user_data") == 0) {
|
---|
452 | return smbc_getOptionUserData(context);
|
---|
453 |
|
---|
454 | } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
|
---|
455 | switch(smbc_getOptionSmbEncryptionLevel(context))
|
---|
456 | {
|
---|
457 | case 0:
|
---|
458 | return (void *) "none";
|
---|
459 | case 1:
|
---|
460 | return (void *) "request";
|
---|
461 | case 2:
|
---|
462 | return (void *) "require";
|
---|
463 | }
|
---|
464 |
|
---|
465 | } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
|
---|
466 | SMBCSRV *s;
|
---|
467 | unsigned int num_servers = 0;
|
---|
468 |
|
---|
469 | for (s = context->internal->servers; s; s = s->next) {
|
---|
470 | num_servers++;
|
---|
471 | if (s->cli->trans_enc_state == NULL) {
|
---|
472 | return (void *)false;
|
---|
473 | }
|
---|
474 | }
|
---|
475 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
476 | return (void *) (intptr_t) (bool) (num_servers > 0);
|
---|
477 | #else
|
---|
478 | return (void *) (bool) (num_servers > 0);
|
---|
479 | #endif
|
---|
480 |
|
---|
481 | } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
|
---|
482 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
483 | return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
|
---|
484 | #else
|
---|
485 | return (void *) smbc_getOptionBrowseMaxLmbCount(context);
|
---|
486 | #endif
|
---|
487 |
|
---|
488 | } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
|
---|
489 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
490 | return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
|
---|
491 | #else
|
---|
492 | return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
|
---|
493 | #endif
|
---|
494 |
|
---|
495 | } else if (strcmp(option_name, "one_share_per_server") == 0) {
|
---|
496 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
497 | return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
|
---|
498 | #else
|
---|
499 | return (void *) (bool) smbc_getOptionOneSharePerServer(context);
|
---|
500 | #endif
|
---|
501 |
|
---|
502 | } else if (strcmp(option_name, "use_kerberos") == 0) {
|
---|
503 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
504 | return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
|
---|
505 | #else
|
---|
506 | return (void *) (bool) smbc_getOptionUseKerberos(context);
|
---|
507 | #endif
|
---|
508 |
|
---|
509 | } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
|
---|
510 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
511 | return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
|
---|
512 | #else
|
---|
513 | return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
|
---|
514 | #endif
|
---|
515 |
|
---|
516 | } else if (strcmp(option_name, "use_ccache") == 0) {
|
---|
517 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
518 | return (void *) (intptr_t) smbc_getOptionUseCCache(context);
|
---|
519 | #else
|
---|
520 | return (void *) (bool) smbc_getOptionUseCCache(context);
|
---|
521 | #endif
|
---|
522 |
|
---|
523 | } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
|
---|
524 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
525 | return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
|
---|
526 | #else
|
---|
527 | return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
|
---|
528 | #endif
|
---|
529 | }
|
---|
530 |
|
---|
531 | return NULL;
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 | /*
|
---|
536 | * Initialize the library, etc.
|
---|
537 | *
|
---|
538 | * We accept a struct containing handle information.
|
---|
539 | * valid values for info->debug from 0 to 100,
|
---|
540 | * and insist that info->fn must be non-null.
|
---|
541 | */
|
---|
542 | SMBCCTX *
|
---|
543 | smbc_init_context(SMBCCTX *context)
|
---|
544 | {
|
---|
545 | int pid;
|
---|
546 |
|
---|
547 | if (!context) {
|
---|
548 | errno = EBADF;
|
---|
549 | return NULL;
|
---|
550 | }
|
---|
551 |
|
---|
552 | /* Do not initialise the same client twice */
|
---|
553 | if (context->internal->initialized) {
|
---|
554 | return NULL;
|
---|
555 | }
|
---|
556 |
|
---|
557 | if (context->internal->debug_stderr) {
|
---|
558 | /*
|
---|
559 | * Hmmm... Do we want a unique dbf per-thread? For now, we'll just
|
---|
560 | * leave it up to the user. If any one context spefies debug to
|
---|
561 | * stderr then all will be.
|
---|
562 | */
|
---|
563 | dbf = x_stderr;
|
---|
564 | x_setbuf(x_stderr, NULL);
|
---|
565 | }
|
---|
566 |
|
---|
567 | if ((!smbc_getFunctionAuthData(context) &&
|
---|
568 | !smbc_getFunctionAuthDataWithContext(context)) ||
|
---|
569 | smbc_getDebug(context) < 0 ||
|
---|
570 | smbc_getDebug(context) > 100) {
|
---|
571 |
|
---|
572 | errno = EINVAL;
|
---|
573 | return NULL;
|
---|
574 |
|
---|
575 | }
|
---|
576 |
|
---|
577 | if (!smbc_getUser(context)) {
|
---|
578 | /*
|
---|
579 | * FIXME: Is this the best way to get the user info?
|
---|
580 | */
|
---|
581 | char *user = getenv("USER");
|
---|
582 | /* walk around as "guest" if no username can be found */
|
---|
583 | if (!user) {
|
---|
584 | user = SMB_STRDUP("guest");
|
---|
585 | } else {
|
---|
586 | user = SMB_STRDUP(user);
|
---|
587 | }
|
---|
588 |
|
---|
589 | if (!user) {
|
---|
590 | errno = ENOMEM;
|
---|
591 | return NULL;
|
---|
592 | }
|
---|
593 |
|
---|
594 | smbc_setUser(context, user);
|
---|
595 | SAFE_FREE(user);
|
---|
596 |
|
---|
597 | if (!smbc_getUser(context)) {
|
---|
598 | errno = ENOMEM;
|
---|
599 | return NULL;
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | if (!smbc_getNetbiosName(context)) {
|
---|
604 | /*
|
---|
605 | * We try to get our netbios name from the config. If that
|
---|
606 | * fails we fall back on constructing our netbios name from
|
---|
607 | * our hostname etc
|
---|
608 | */
|
---|
609 | char *netbios_name;
|
---|
610 | if (global_myname()) {
|
---|
611 | netbios_name = SMB_STRDUP(global_myname());
|
---|
612 | } else {
|
---|
613 | /*
|
---|
614 | * Hmmm, I want to get hostname as well, but I am too
|
---|
615 | * lazy for the moment
|
---|
616 | */
|
---|
617 | pid = sys_getpid();
|
---|
618 | netbios_name = (char *)SMB_MALLOC(17);
|
---|
619 | if (!netbios_name) {
|
---|
620 | errno = ENOMEM;
|
---|
621 | return NULL;
|
---|
622 | }
|
---|
623 | slprintf(netbios_name, 16,
|
---|
624 | "smbc%s%d", smbc_getUser(context), pid);
|
---|
625 | }
|
---|
626 |
|
---|
627 | if (!netbios_name) {
|
---|
628 | errno = ENOMEM;
|
---|
629 | return NULL;
|
---|
630 | }
|
---|
631 |
|
---|
632 | smbc_setNetbiosName(context, netbios_name);
|
---|
633 | SAFE_FREE(netbios_name);
|
---|
634 |
|
---|
635 | if (!smbc_getNetbiosName(context)) {
|
---|
636 | errno = ENOMEM;
|
---|
637 | return NULL;
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 | DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
|
---|
642 |
|
---|
643 | if (!smbc_getWorkgroup(context)) {
|
---|
644 | char *workgroup;
|
---|
645 |
|
---|
646 | if (lp_workgroup()) {
|
---|
647 | workgroup = SMB_STRDUP(lp_workgroup());
|
---|
648 | }
|
---|
649 | else {
|
---|
650 | /* TODO: Think about a decent default workgroup */
|
---|
651 | workgroup = SMB_STRDUP("samba");
|
---|
652 | }
|
---|
653 |
|
---|
654 | if (!workgroup) {
|
---|
655 | errno = ENOMEM;
|
---|
656 | return NULL;
|
---|
657 | }
|
---|
658 |
|
---|
659 | smbc_setWorkgroup(context, workgroup);
|
---|
660 | SAFE_FREE(workgroup);
|
---|
661 |
|
---|
662 | if (!smbc_getWorkgroup(context)) {
|
---|
663 | errno = ENOMEM;
|
---|
664 | return NULL;
|
---|
665 | }
|
---|
666 | }
|
---|
667 |
|
---|
668 | DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
|
---|
669 |
|
---|
670 | /* shortest timeout is 1 second */
|
---|
671 | if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
|
---|
672 | smbc_setTimeout(context, 1000);
|
---|
673 |
|
---|
674 | context->internal->initialized = True;
|
---|
675 |
|
---|
676 | /* Protect access to the count of contexts in use */
|
---|
677 | if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
|
---|
678 | smb_panic("error locking 'initialized_ctx_count'");
|
---|
679 | }
|
---|
680 |
|
---|
681 | initialized_ctx_count++;
|
---|
682 |
|
---|
683 | /* Unlock the mutex */
|
---|
684 | if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
|
---|
685 | smb_panic("error unlocking 'initialized_ctx_count'");
|
---|
686 | }
|
---|
687 |
|
---|
688 | return context;
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | /* Return the verion of samba, and thus libsmbclient */
|
---|
693 | const char *
|
---|
694 | smbc_version(void)
|
---|
695 | {
|
---|
696 | return samba_version_string();
|
---|
697 | }
|
---|
698 |
|
---|
699 | /*
|
---|
700 | * Set the credentials so DFS will work when following referrals.
|
---|
701 | * This function is broken and must be removed. No SMBCCTX arg...
|
---|
702 | * JRA.
|
---|
703 | */
|
---|
704 |
|
---|
705 | void
|
---|
706 | smbc_set_credentials(const char *workgroup,
|
---|
707 | const char *user,
|
---|
708 | const char *password,
|
---|
709 | smbc_bool use_kerberos,
|
---|
710 | const char *signing_state)
|
---|
711 | {
|
---|
712 | d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
|
---|
713 | }
|
---|
714 |
|
---|
715 | void smbc_set_credentials_with_fallback(SMBCCTX *context,
|
---|
716 | const char *workgroup,
|
---|
717 | const char *user,
|
---|
718 | const char *password)
|
---|
719 | {
|
---|
720 | smbc_bool use_kerberos = false;
|
---|
721 | const char *signing_state = "off";
|
---|
722 | struct user_auth_info *auth_info = NULL;
|
---|
723 |
|
---|
724 | if (! context) {
|
---|
725 |
|
---|
726 | return;
|
---|
727 | }
|
---|
728 |
|
---|
729 | if (! workgroup || ! *workgroup) {
|
---|
730 | workgroup = smbc_getWorkgroup(context);
|
---|
731 | }
|
---|
732 |
|
---|
733 | if (! user) {
|
---|
734 | user = smbc_getUser(context);
|
---|
735 | }
|
---|
736 |
|
---|
737 | if (! password) {
|
---|
738 | password = "";
|
---|
739 | }
|
---|
740 |
|
---|
741 | auth_info = user_auth_info_init(NULL);
|
---|
742 |
|
---|
743 | if (! auth_info) {
|
---|
744 | DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
|
---|
745 | return;
|
---|
746 | }
|
---|
747 |
|
---|
748 | if (smbc_getOptionUseKerberos(context)) {
|
---|
749 | use_kerberos = True;
|
---|
750 | }
|
---|
751 |
|
---|
752 | if (lp_client_signing()) {
|
---|
753 | signing_state = "on";
|
---|
754 | }
|
---|
755 |
|
---|
756 | if (lp_client_signing() == Required) {
|
---|
757 | signing_state = "force";
|
---|
758 | }
|
---|
759 |
|
---|
760 | set_cmdline_auth_info_username(auth_info, user);
|
---|
761 | set_cmdline_auth_info_password(auth_info, password);
|
---|
762 | set_cmdline_auth_info_use_kerberos(auth_info, use_kerberos);
|
---|
763 | set_cmdline_auth_info_signing_state(auth_info, signing_state);
|
---|
764 | set_cmdline_auth_info_fallback_after_kerberos(auth_info,
|
---|
765 | smbc_getOptionFallbackAfterKerberos(context));
|
---|
766 | set_cmdline_auth_info_use_ccache(
|
---|
767 | auth_info, smbc_getOptionUseCCache(context));
|
---|
768 | set_global_myworkgroup(workgroup);
|
---|
769 |
|
---|
770 | TALLOC_FREE(context->internal->auth_info);
|
---|
771 |
|
---|
772 | context->internal->auth_info = auth_info;
|
---|
773 | }
|
---|