Fix a "nested extern declaration" warning
[Samba/bjacke.git] / source / libsmb / libsmb_context.c
blob212b42fa884c6916f80b95bbae1ebf9e84abe551
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"
30 extern bool in_client;
33 * Is the logging working / configfile read ?
35 static int SMBC_initialized = 0;
40 * Get a new empty handle to fill in with your own info
42 SMBCCTX *
43 smbc_new_context(void)
45 SMBCCTX *context;
48 * All newly added context fields should be placed in
49 * SMBC_internal_data, not directly in SMBCCTX.
51 context = SMB_MALLOC_P(SMBCCTX);
52 if (!context) {
53 errno = ENOMEM;
54 return NULL;
57 ZERO_STRUCTP(context);
59 context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
60 if (!context->internal) {
61 SAFE_FREE(context);
62 errno = ENOMEM;
63 return NULL;
66 /* Initialize the context and establish reasonable defaults */
67 ZERO_STRUCTP(context->internal);
69 smbc_setDebug(context, 0);
70 smbc_setTimeout(context, 20000);
72 smbc_setOptionFullTimeNames(context, False);
73 smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
74 smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
75 smbc_setOptionBrowseMaxLmbCount(context, 3); /* # LMBs to query */
76 smbc_setOptionUrlEncodeReaddirEntries(context, False);
77 smbc_setOptionOneSharePerServer(context, False);
79 smbc_setFunctionAuthData(context, SMBC_get_auth_data);
80 smbc_setFunctionCheckServer(context, SMBC_check_server);
81 smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
83 smbc_setOptionUserData(context, NULL);
84 smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
85 smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
86 smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
87 smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
89 smbc_setFunctionOpen(context, SMBC_open_ctx);
90 smbc_setFunctionCreat(context, SMBC_creat_ctx);
91 smbc_setFunctionRead(context, SMBC_read_ctx);
92 smbc_setFunctionWrite(context, SMBC_write_ctx);
93 smbc_setFunctionClose(context, SMBC_close_ctx);
94 smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
95 smbc_setFunctionRename(context, SMBC_rename_ctx);
96 smbc_setFunctionLseek(context, SMBC_lseek_ctx);
97 smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
98 smbc_setFunctionStat(context, SMBC_stat_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 free(smbc_getWorkgroup(context));
196 smbc_setWorkgroup(context, NULL);
198 free(smbc_getNetbiosName(context));
199 smbc_setNetbiosName(context, NULL);
201 free(smbc_getUser(context));
202 smbc_setUser(context, NULL);
204 DEBUG(3, ("Context %p successfully freed\n", context));
205 SAFE_FREE(context);
206 return 0;
211 * Deprecated interface. Do not use. Instead, use the various
212 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
214 void
215 smbc_option_set(SMBCCTX *context,
216 char *option_name,
217 ... /* option_value */)
219 va_list ap;
220 union {
221 int i;
222 bool b;
223 smbc_get_auth_data_with_context_fn auth_fn;
224 void *v;
225 const char *s;
226 } option_value;
228 va_start(ap, option_name);
230 if (strcmp(option_name, "debug_to_stderr") == 0) {
231 option_value.b = (bool) va_arg(ap, int);
232 smbc_setOptionDebugToStderr(context, option_value.b);
234 } else if (strcmp(option_name, "full_time_names") == 0) {
235 option_value.b = (bool) va_arg(ap, int);
236 smbc_setOptionFullTimeNames(context, option_value.b);
238 } else if (strcmp(option_name, "open_share_mode") == 0) {
239 option_value.i = va_arg(ap, int);
240 smbc_setOptionOpenShareMode(context, option_value.i);
242 } else if (strcmp(option_name, "auth_function") == 0) {
243 option_value.auth_fn =
244 va_arg(ap, smbc_get_auth_data_with_context_fn);
245 smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
247 } else if (strcmp(option_name, "user_data") == 0) {
248 option_value.v = va_arg(ap, void *);
249 smbc_setOptionUserData(context, option_value.v);
251 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
252 option_value.s = va_arg(ap, const char *);
253 if (strcmp(option_value.s, "none") == 0) {
254 smbc_setOptionSmbEncryptionLevel(context,
255 SMBC_ENCRYPTLEVEL_NONE);
256 } else if (strcmp(option_value.s, "request") == 0) {
257 smbc_setOptionSmbEncryptionLevel(context,
258 SMBC_ENCRYPTLEVEL_REQUEST);
259 } else if (strcmp(option_value.s, "require") == 0) {
260 smbc_setOptionSmbEncryptionLevel(context,
261 SMBC_ENCRYPTLEVEL_REQUIRE);
264 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
265 option_value.i = va_arg(ap, int);
266 smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
268 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
269 option_value.b = (bool) va_arg(ap, int);
270 smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
272 } else if (strcmp(option_name, "one_share_per_server") == 0) {
273 option_value.b = (bool) va_arg(ap, int);
274 smbc_setOptionOneSharePerServer(context, option_value.b);
276 } else if (strcmp(option_name, "use_kerberos") == 0) {
277 option_value.b = (bool) va_arg(ap, int);
278 smbc_setOptionUseKerberos(context, option_value.b);
280 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
281 option_value.b = (bool) va_arg(ap, int);
282 smbc_setOptionFallbackAfterKerberos(context, option_value.b);
284 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
285 option_value.b = (bool) va_arg(ap, int);
286 smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
289 va_end(ap);
294 * Deprecated interface. Do not use. Instead, use the various
295 * smbc_getOption*() functions.
297 void *
298 smbc_option_get(SMBCCTX *context,
299 char *option_name)
301 if (strcmp(option_name, "debug_stderr") == 0) {
302 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
303 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
304 #else
305 return (void *) smbc_getOptionDebugToStderr(context);
306 #endif
308 } else if (strcmp(option_name, "full_time_names") == 0) {
309 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
310 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
311 #else
312 return (void *) smbc_getOptionFullTimeNames(context);
313 #endif
315 } else if (strcmp(option_name, "open_share_mode") == 0) {
316 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
317 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
318 #else
319 return (void *) smbc_getOptionOpenShareMode(context);
320 #endif
322 } else if (strcmp(option_name, "auth_function") == 0) {
323 return (void *) smbc_getFunctionAuthDataWithContext(context);
325 } else if (strcmp(option_name, "user_data") == 0) {
326 return smbc_getOptionUserData(context);
328 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
329 switch(smbc_getOptionSmbEncryptionLevel(context))
331 case 0:
332 return (void *) "none";
333 case 1:
334 return (void *) "request";
335 case 2:
336 return (void *) "require";
339 } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
340 SMBCSRV *s;
341 unsigned int num_servers = 0;
343 for (s = context->internal->servers; s; s = s->next) {
344 num_servers++;
345 if (s->cli->trans_enc_state == NULL) {
346 return (void *)false;
349 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
350 return (void *) (intptr_t) (bool) (num_servers > 0);
351 #else
352 return (void *) (bool) (num_servers > 0);
353 #endif
355 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
356 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
357 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
358 #else
359 return (void *) smbc_getOptionBrowseMaxLmbCount(context);
360 #endif
362 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
363 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
364 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
365 #else
366 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
367 #endif
369 } else if (strcmp(option_name, "one_share_per_server") == 0) {
370 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
371 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
372 #else
373 return (void *) (bool) smbc_getOptionOneSharePerServer(context);
374 #endif
376 } else if (strcmp(option_name, "use_kerberos") == 0) {
377 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
378 return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
379 #else
380 return (void *) (bool) smbc_getOptionUseKerberos(context);
381 #endif
383 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
384 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
385 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
386 #else
387 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
388 #endif
390 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
391 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
392 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
393 #else
394 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
395 #endif
398 return NULL;
403 * Initialize the library, etc.
405 * We accept a struct containing handle information.
406 * valid values for info->debug from 0 to 100,
407 * and insist that info->fn must be non-null.
409 SMBCCTX *
410 smbc_init_context(SMBCCTX *context)
412 int pid;
413 char *user = NULL;
414 char *home = NULL;
416 if (!context) {
417 errno = EBADF;
418 return NULL;
421 /* Do not initialise the same client twice */
422 if (context->internal->initialized) {
423 return NULL;
426 if (!smbc_getFunctionAuthData(context) ||
427 smbc_getDebug(context) < 0 ||
428 smbc_getDebug(context) > 100) {
430 errno = EINVAL;
431 return NULL;
435 if (!SMBC_initialized) {
437 * Do some library-wide intializations the first time we get
438 * called
440 bool conf_loaded = False;
441 TALLOC_CTX *frame = talloc_stackframe();
443 load_case_tables();
445 setup_logging("libsmbclient", True);
446 if (context->internal->debug_stderr) {
447 dbf = x_stderr;
448 x_setbuf(x_stderr, NULL);
451 /* Here we would open the smb.conf file if needed ... */
453 in_client = True; /* FIXME, make a param */
455 home = getenv("HOME");
456 if (home) {
457 char *conf = NULL;
458 if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
459 if (lp_load(conf, True, False, False, True)) {
460 conf_loaded = True;
461 } else {
462 DEBUG(5, ("Could not load config file: %s\n",
463 conf));
465 SAFE_FREE(conf);
469 if (!conf_loaded) {
471 * Well, if that failed, try the get_dyn_CONFIGFILE
472 * Which points to the standard locn, and if that
473 * fails, silently ignore it and use the internal
474 * defaults ...
477 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
478 DEBUG(5, ("Could not load config file: %s\n",
479 get_dyn_CONFIGFILE()));
480 } else if (home) {
481 char *conf;
483 * We loaded the global config file. Now lets
484 * load user-specific modifications to the
485 * global config.
487 if (asprintf(&conf,
488 "%s/.smb/smb.conf.append",
489 home) > 0) {
490 if (!lp_load(conf, True, False, False, False)) {
491 DEBUG(10,
492 ("Could not append config file: "
493 "%s\n",
494 conf));
496 SAFE_FREE(conf);
501 load_interfaces(); /* Load the list of interfaces ... */
503 reopen_logs(); /* Get logging working ... */
506 * Block SIGPIPE (from lib/util_sock.c: write())
507 * It is not needed and should not stop execution
509 BlockSignals(True, SIGPIPE);
511 /* Done with one-time initialisation */
512 SMBC_initialized = 1;
514 TALLOC_FREE(frame);
517 if (!smbc_getUser(context)) {
519 * FIXME: Is this the best way to get the user info?
521 user = getenv("USER");
522 /* walk around as "guest" if no username can be found */
523 if (!user) {
524 user = SMB_STRDUP("guest");
525 } else {
526 user = SMB_STRDUP(user);
529 if (!user) {
530 errno = ENOMEM;
531 return NULL;
534 smbc_setUser(context, user);
537 if (!smbc_getNetbiosName(context)) {
539 * We try to get our netbios name from the config. If that
540 * fails we fall back on constructing our netbios name from
541 * our hostname etc
543 char *netbios_name;
544 if (global_myname()) {
545 netbios_name = SMB_STRDUP(global_myname());
546 } else {
548 * Hmmm, I want to get hostname as well, but I am too
549 * lazy for the moment
551 pid = sys_getpid();
552 netbios_name = (char *)SMB_MALLOC(17);
553 if (!netbios_name) {
554 errno = ENOMEM;
555 return NULL;
557 slprintf(netbios_name, 16,
558 "smbc%s%d", smbc_getUser(context), pid);
561 if (!netbios_name) {
562 errno = ENOMEM;
563 return NULL;
566 smbc_setNetbiosName(context, netbios_name);
569 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
571 if (!smbc_getWorkgroup(context)) {
572 char *workgroup;
574 if (lp_workgroup()) {
575 workgroup = SMB_STRDUP(lp_workgroup());
577 else {
578 /* TODO: Think about a decent default workgroup */
579 workgroup = SMB_STRDUP("samba");
582 if (!workgroup) {
583 errno = ENOMEM;
584 return NULL;
587 smbc_setWorkgroup(context, workgroup);
590 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
592 /* shortest timeout is 1 second */
593 if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
594 smbc_setTimeout(context, 1000);
597 * FIXME: Should we check the function pointers here?
600 context->internal->initialized = True;
602 return context;
606 /* Return the verion of samba, and thus libsmbclient */
607 const char *
608 smbc_version(void)
610 return samba_version_string();