Fix bug 7045 - Bad (non memory copying) interfaces in smbc_setXXXX calls.
[Samba.git] / source / libsmb / libsmb_context.c
blobfb14c267f016c7b0e79e35b3c600b6b8124fdbbc
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
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.
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.
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/>.
25 #include "includes.h"
26 #include "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Is the logging working / configfile read ?
33 static bool SMBC_initialized;
34 static unsigned int initialized_ctx_count;
37 * Get a new empty handle to fill in with your own info
39 SMBCCTX *
40 smbc_new_context(void)
42 SMBCCTX *context;
45 * All newly added context fields should be placed in
46 * SMBC_internal_data, not directly in SMBCCTX.
48 context = SMB_MALLOC_P(SMBCCTX);
49 if (!context) {
50 errno = ENOMEM;
51 return NULL;
54 ZERO_STRUCTP(context);
56 context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
57 if (!context->internal) {
58 SAFE_FREE(context);
59 errno = ENOMEM;
60 return NULL;
63 /* Initialize the context and establish reasonable defaults */
64 ZERO_STRUCTP(context->internal);
66 smbc_setDebug(context, 0);
67 smbc_setTimeout(context, 20000);
69 smbc_setOptionFullTimeNames(context, False);
70 smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
71 smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
72 smbc_setOptionCaseSensitive(context, False);
73 smbc_setOptionBrowseMaxLmbCount(context, 3); /* # LMBs to query */
74 smbc_setOptionUrlEncodeReaddirEntries(context, False);
75 smbc_setOptionOneSharePerServer(context, False);
77 smbc_setFunctionAuthData(context, SMBC_get_auth_data);
78 smbc_setFunctionCheckServer(context, SMBC_check_server);
79 smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
81 smbc_setOptionUserData(context, NULL);
82 smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
83 smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
84 smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
85 smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
87 smbc_setFunctionOpen(context, SMBC_open_ctx);
88 smbc_setFunctionCreat(context, SMBC_creat_ctx);
89 smbc_setFunctionRead(context, SMBC_read_ctx);
90 smbc_setFunctionWrite(context, SMBC_write_ctx);
91 smbc_setFunctionClose(context, SMBC_close_ctx);
92 smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
93 smbc_setFunctionRename(context, SMBC_rename_ctx);
94 smbc_setFunctionLseek(context, SMBC_lseek_ctx);
95 smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
96 smbc_setFunctionStat(context, SMBC_stat_ctx);
97 smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx);
98 smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx);
99 smbc_setFunctionFstat(context, SMBC_fstat_ctx);
100 smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
101 smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
102 smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
103 smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
104 smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
105 smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
106 smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
107 smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
108 smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
109 smbc_setFunctionChmod(context, SMBC_chmod_ctx);
110 smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
111 smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
112 smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
113 smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
114 smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
116 smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
117 smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
118 smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
119 smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
121 return context;
125 * Free a context
127 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
128 * and thus you'll be leaking memory if not handled properly.
132 smbc_free_context(SMBCCTX *context,
133 int shutdown_ctx)
135 if (!context) {
136 errno = EBADF;
137 return 1;
140 if (shutdown_ctx) {
141 SMBCFILE * f;
142 DEBUG(1,("Performing aggressive shutdown.\n"));
144 f = context->internal->files;
145 while (f) {
146 smbc_getFunctionClose(context)(context, f);
147 f = f->next;
149 context->internal->files = NULL;
151 /* First try to remove the servers the nice way. */
152 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
153 SMBCSRV * s;
154 SMBCSRV * next;
155 DEBUG(1, ("Could not purge all servers, "
156 "Nice way shutdown failed.\n"));
157 s = context->internal->servers;
158 while (s) {
159 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
160 s, s->cli->fd));
161 cli_shutdown(s->cli);
162 smbc_getFunctionRemoveCachedServer(context)(context,
164 next = s->next;
165 DLIST_REMOVE(context->internal->servers, s);
166 SAFE_FREE(s);
167 s = next;
169 context->internal->servers = NULL;
172 else {
173 /* This is the polite way */
174 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
175 DEBUG(1, ("Could not purge all servers, "
176 "free_context failed.\n"));
177 errno = EBUSY;
178 return 1;
180 if (context->internal->servers) {
181 DEBUG(1, ("Active servers in context, "
182 "free_context failed.\n"));
183 errno = EBUSY;
184 return 1;
186 if (context->internal->files) {
187 DEBUG(1, ("Active files in context, "
188 "free_context failed.\n"));
189 errno = EBUSY;
190 return 1;
194 /* Things we have to clean up */
195 smbc_setWorkgroup(context, NULL);
196 smbc_setNetbiosName(context, NULL);
197 smbc_setUser(context, NULL);
199 DEBUG(3, ("Context %p successfully freed\n", context));
201 SAFE_FREE(context->internal);
202 SAFE_FREE(context);
204 if (initialized_ctx_count) {
205 initialized_ctx_count--;
208 if (initialized_ctx_count == 0 && SMBC_initialized) {
209 gencache_shutdown();
210 secrets_shutdown();
211 gfree_all();
212 SMBC_initialized = false;
214 return 0;
219 * Deprecated interface. Do not use. Instead, use the various
220 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
222 void
223 smbc_option_set(SMBCCTX *context,
224 char *option_name,
225 ... /* option_value */)
227 va_list ap;
228 union {
229 int i;
230 bool b;
231 smbc_get_auth_data_with_context_fn auth_fn;
232 void *v;
233 const char *s;
234 } option_value;
236 va_start(ap, option_name);
238 if (strcmp(option_name, "debug_to_stderr") == 0) {
239 option_value.b = (bool) va_arg(ap, int);
240 smbc_setOptionDebugToStderr(context, option_value.b);
242 } else if (strcmp(option_name, "full_time_names") == 0) {
243 option_value.b = (bool) va_arg(ap, int);
244 smbc_setOptionFullTimeNames(context, option_value.b);
246 } else if (strcmp(option_name, "open_share_mode") == 0) {
247 option_value.i = va_arg(ap, int);
248 smbc_setOptionOpenShareMode(context, option_value.i);
250 } else if (strcmp(option_name, "auth_function") == 0) {
251 option_value.auth_fn =
252 va_arg(ap, smbc_get_auth_data_with_context_fn);
253 smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
255 } else if (strcmp(option_name, "user_data") == 0) {
256 option_value.v = va_arg(ap, void *);
257 smbc_setOptionUserData(context, option_value.v);
259 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
260 option_value.s = va_arg(ap, const char *);
261 if (strcmp(option_value.s, "none") == 0) {
262 smbc_setOptionSmbEncryptionLevel(context,
263 SMBC_ENCRYPTLEVEL_NONE);
264 } else if (strcmp(option_value.s, "request") == 0) {
265 smbc_setOptionSmbEncryptionLevel(context,
266 SMBC_ENCRYPTLEVEL_REQUEST);
267 } else if (strcmp(option_value.s, "require") == 0) {
268 smbc_setOptionSmbEncryptionLevel(context,
269 SMBC_ENCRYPTLEVEL_REQUIRE);
272 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
273 option_value.i = va_arg(ap, int);
274 smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
276 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
277 option_value.b = (bool) va_arg(ap, int);
278 smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
280 } else if (strcmp(option_name, "one_share_per_server") == 0) {
281 option_value.b = (bool) va_arg(ap, int);
282 smbc_setOptionOneSharePerServer(context, option_value.b);
284 } else if (strcmp(option_name, "use_kerberos") == 0) {
285 option_value.b = (bool) va_arg(ap, int);
286 smbc_setOptionUseKerberos(context, option_value.b);
288 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
289 option_value.b = (bool) va_arg(ap, int);
290 smbc_setOptionFallbackAfterKerberos(context, option_value.b);
292 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
293 option_value.b = (bool) va_arg(ap, int);
294 smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
297 va_end(ap);
302 * Deprecated interface. Do not use. Instead, use the various
303 * smbc_getOption*() functions.
305 void *
306 smbc_option_get(SMBCCTX *context,
307 char *option_name)
309 if (strcmp(option_name, "debug_stderr") == 0) {
310 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
311 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
312 #else
313 return (void *) smbc_getOptionDebugToStderr(context);
314 #endif
316 } else if (strcmp(option_name, "full_time_names") == 0) {
317 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
318 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
319 #else
320 return (void *) smbc_getOptionFullTimeNames(context);
321 #endif
323 } else if (strcmp(option_name, "open_share_mode") == 0) {
324 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
325 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
326 #else
327 return (void *) smbc_getOptionOpenShareMode(context);
328 #endif
330 } else if (strcmp(option_name, "auth_function") == 0) {
331 return (void *) smbc_getFunctionAuthDataWithContext(context);
333 } else if (strcmp(option_name, "user_data") == 0) {
334 return smbc_getOptionUserData(context);
336 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
337 switch(smbc_getOptionSmbEncryptionLevel(context))
339 case 0:
340 return (void *) "none";
341 case 1:
342 return (void *) "request";
343 case 2:
344 return (void *) "require";
347 } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
348 SMBCSRV *s;
349 unsigned int num_servers = 0;
351 for (s = context->internal->servers; s; s = s->next) {
352 num_servers++;
353 if (s->cli->trans_enc_state == NULL) {
354 return (void *)false;
357 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
358 return (void *) (intptr_t) (bool) (num_servers > 0);
359 #else
360 return (void *) (bool) (num_servers > 0);
361 #endif
363 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
364 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
365 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
366 #else
367 return (void *) smbc_getOptionBrowseMaxLmbCount(context);
368 #endif
370 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
371 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
372 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
373 #else
374 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
375 #endif
377 } else if (strcmp(option_name, "one_share_per_server") == 0) {
378 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
379 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
380 #else
381 return (void *) (bool) smbc_getOptionOneSharePerServer(context);
382 #endif
384 } else if (strcmp(option_name, "use_kerberos") == 0) {
385 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
386 return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
387 #else
388 return (void *) (bool) smbc_getOptionUseKerberos(context);
389 #endif
391 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
392 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
393 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
394 #else
395 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
396 #endif
398 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
399 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
400 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
401 #else
402 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
403 #endif
406 return NULL;
411 * Initialize the library, etc.
413 * We accept a struct containing handle information.
414 * valid values for info->debug from 0 to 100,
415 * and insist that info->fn must be non-null.
417 SMBCCTX *
418 smbc_init_context(SMBCCTX *context)
420 int pid;
421 char *home = NULL;
423 if (!context) {
424 errno = EBADF;
425 return NULL;
428 /* Do not initialise the same client twice */
429 if (context->internal->initialized) {
430 return NULL;
433 if ((!smbc_getFunctionAuthData(context) &&
434 !smbc_getFunctionAuthDataWithContext(context)) ||
435 smbc_getDebug(context) < 0 ||
436 smbc_getDebug(context) > 100) {
438 errno = EINVAL;
439 return NULL;
443 if (!SMBC_initialized) {
445 * Do some library-wide intializations the first time we get
446 * called
448 bool conf_loaded = False;
449 TALLOC_CTX *frame = talloc_stackframe();
451 load_case_tables();
453 setup_logging("libsmbclient", True);
454 if (context->internal->debug_stderr) {
455 dbf = x_stderr;
456 x_setbuf(x_stderr, NULL);
459 /* Here we would open the smb.conf file if needed ... */
461 lp_set_in_client(True);
463 home = getenv("HOME");
464 if (home) {
465 char *conf = NULL;
466 if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
467 if (lp_load(conf, True, False, False, True)) {
468 conf_loaded = True;
469 } else {
470 DEBUG(5, ("Could not load config file: %s\n",
471 conf));
473 SAFE_FREE(conf);
477 if (!conf_loaded) {
479 * Well, if that failed, try the get_dyn_CONFIGFILE
480 * Which points to the standard locn, and if that
481 * fails, silently ignore it and use the internal
482 * defaults ...
485 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
486 DEBUG(5, ("Could not load config file: %s\n",
487 get_dyn_CONFIGFILE()));
488 } else if (home) {
489 char *conf;
491 * We loaded the global config file. Now lets
492 * load user-specific modifications to the
493 * global config.
495 if (asprintf(&conf,
496 "%s/.smb/smb.conf.append",
497 home) > 0) {
498 if (!lp_load(conf, True, False, False, False)) {
499 DEBUG(10,
500 ("Could not append config file: "
501 "%s\n",
502 conf));
504 SAFE_FREE(conf);
509 load_interfaces(); /* Load the list of interfaces ... */
511 reopen_logs(); /* Get logging working ... */
514 * Block SIGPIPE (from lib/util_sock.c: write())
515 * It is not needed and should not stop execution
517 BlockSignals(True, SIGPIPE);
519 /* Done with one-time initialisation */
520 SMBC_initialized = true;
522 TALLOC_FREE(frame);
525 if (!smbc_getUser(context)) {
527 * FIXME: Is this the best way to get the user info?
529 char *user = getenv("USER");
530 /* walk around as "guest" if no username can be found */
531 if (!user) {
532 user = SMB_STRDUP("guest");
533 } else {
534 user = SMB_STRDUP(user);
537 if (!user) {
538 errno = ENOMEM;
539 return NULL;
542 smbc_setUser(context, user);
543 SAFE_FREE(user);
545 if (!smbc_getUser(context)) {
546 errno = ENOMEM;
547 return NULL;
551 if (!smbc_getNetbiosName(context)) {
553 * We try to get our netbios name from the config. If that
554 * fails we fall back on constructing our netbios name from
555 * our hostname etc
557 char *netbios_name;
558 if (global_myname()) {
559 netbios_name = SMB_STRDUP(global_myname());
560 } else {
562 * Hmmm, I want to get hostname as well, but I am too
563 * lazy for the moment
565 pid = sys_getpid();
566 netbios_name = (char *)SMB_MALLOC(17);
567 if (!netbios_name) {
568 errno = ENOMEM;
569 return NULL;
571 slprintf(netbios_name, 16,
572 "smbc%s%d", smbc_getUser(context), pid);
575 if (!netbios_name) {
576 errno = ENOMEM;
577 return NULL;
580 smbc_setNetbiosName(context, netbios_name);
581 SAFE_FREE(netbios_name);
583 if (!smbc_getNetbiosName(context)) {
584 errno = ENOMEM;
585 return NULL;
589 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
591 if (!smbc_getWorkgroup(context)) {
592 char *workgroup;
594 if (lp_workgroup()) {
595 workgroup = SMB_STRDUP(lp_workgroup());
597 else {
598 /* TODO: Think about a decent default workgroup */
599 workgroup = SMB_STRDUP("samba");
602 if (!workgroup) {
603 errno = ENOMEM;
604 return NULL;
607 smbc_setWorkgroup(context, workgroup);
608 SAFE_FREE(workgroup);
610 if (!smbc_getWorkgroup(context)) {
611 errno = ENOMEM;
612 return NULL;
616 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
618 /* shortest timeout is 1 second */
619 if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
620 smbc_setTimeout(context, 1000);
623 * FIXME: Should we check the function pointers here?
626 context->internal->initialized = True;
627 initialized_ctx_count++;
629 return context;
633 /* Return the verion of samba, and thus libsmbclient */
634 const char *
635 smbc_version(void)
637 return SAMBA_VERSION_STRING;
642 * Set the credentials so DFS will work when following referrals.
644 void
645 smbc_set_credentials(const char *workgroup,
646 const char *user,
647 const char *password,
648 smbc_bool use_kerberos,
649 const char *signing_state)
652 set_cmdline_auth_info_username(user);
653 set_cmdline_auth_info_password(password);
654 set_cmdline_auth_info_use_kerberos(use_kerberos);
655 if (! set_cmdline_auth_info_signing_state(signing_state)) {
656 DEBUG(0, ("Invalid signing state: %s", signing_state));
658 set_global_myworkgroup(workgroup);
659 cli_cm_set_credentials();
662 void smbc_set_credentials_with_fallback(SMBCCTX *context,
663 const char *workgroup,
664 const char *user,
665 const char *password)
667 smbc_bool use_kerberos = false;
668 const char *signing_state = "off";
670 if (! context) {
672 return;
675 if (! workgroup || ! *workgroup) {
676 workgroup = smbc_getWorkgroup(context);
679 if (! user) {
680 user = smbc_getUser(context);
683 if (! password) {
684 password = "";
687 if (smbc_getOptionUseKerberos(context)) {
688 use_kerberos = True;
691 if (lp_client_signing()) {
692 signing_state = "on";
695 if (lp_client_signing() == Required) {
696 signing_state = "force";
699 smbc_set_credentials(workgroup, user, password,
700 use_kerberos, signing_state);
702 if (smbc_getOptionFallbackAfterKerberos(context)) {
703 cli_cm_set_fallback_after_kerberos();