summaryrefslogtreecommitdiff
path: root/source3/libsmb/libsmb_context.c
blob: c04f751696b70a89a757ba16e4335e8b41de0c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* 
   Unix SMB/Netbios implementation.
   SMB client library implementation
   Copyright (C) Andrew Tridgell 1998
   Copyright (C) Richard Sharpe 2000, 2002
   Copyright (C) John Terpstra 2000
   Copyright (C) Tom Jansen (Ninja ISD) 2002 
   Copyright (C) Derrell Lipman 2003-2008
   Copyright (C) Jeremy Allison 2007, 2008
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libsmbclient.h"
#include "libsmb_internal.h"


/*
 * Is the logging working / configfile read ? 
 */
static int SMBC_initialized = 0;



/*
 * Get a new empty handle to fill in with your own info
 */
SMBCCTX *
smbc_new_context(void)
{
        SMBCCTX *context;
        
        /*
         * All newly added context fields should be placed in
         * SMBC_internal_data, not directly in SMBCCTX.
         */
        context = SMB_MALLOC_P(SMBCCTX);
        if (!context) {
                errno = ENOMEM;
                return NULL;
        }
        
        ZERO_STRUCTP(context);
        
        context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
        if (!context->internal) {
                SAFE_FREE(context);
                errno = ENOMEM;
                return NULL;
        }
        
        /* Initialize the context and establish reasonable defaults */
        ZERO_STRUCTP(context->internal);
        
        smbc_setDebug(context, 0);
        smbc_setTimeout(context, 20000);
        
        smbc_setOptionFullTimeNames(context, False);
        smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
        smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
        smbc_setOptionBrowseMaxLmbCount(context, 3);    /* # LMBs to query */
        smbc_setOptionUrlEncodeReaddirEntries(context, False);
        smbc_setOptionOneSharePerServer(context, False);
        
        smbc_setFunctionAuthData(context, SMBC_get_auth_data);
        smbc_setFunctionCheckServer(context, SMBC_check_server);
        smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
        
        smbc_setOptionUserData(context, NULL);
        smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
        smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
        smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
        smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
        
        smbc_setFunctionOpen(context, SMBC_open_ctx);
        smbc_setFunctionCreat(context, SMBC_creat_ctx);
        smbc_setFunctionRead(context, SMBC_read_ctx);
        smbc_setFunctionWrite(context, SMBC_write_ctx);
        smbc_setFunctionClose(context, SMBC_close_ctx);
        smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
        smbc_setFunctionRename(context, SMBC_rename_ctx);
        smbc_setFunctionLseek(context, SMBC_lseek_ctx);
        smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
        smbc_setFunctionStat(context, SMBC_stat_ctx);
        smbc_setFunctionFstat(context, SMBC_fstat_ctx);
        smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
        smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
        smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
        smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
        smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
        smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
        smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
        smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
        smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
        smbc_setFunctionChmod(context, SMBC_chmod_ctx);
        smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
        smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
        smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
        smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
        smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
        
        smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
        smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
        smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
        smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
        
        return context;
}

/*
 * Free a context
 *
 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
 * and thus you'll be leaking memory if not handled properly.
 *
 */
int
smbc_free_context(SMBCCTX *context,
                  int shutdown_ctx)
{
        if (!context) {
                errno = EBADF;
                return 1;
        }
        
        if (shutdown_ctx) {
                SMBCFILE * f;
                DEBUG(1,("Performing aggressive shutdown.\n"));
                
                f = context->internal->files;
                while (f) {
                        smbc_getFunctionClose(context)(context, f);
                        f = f->next;
                }
                context->internal->files = NULL;
                
                /* First try to remove the servers the nice way. */
                if (smbc_getFunctionPurgeCachedServers(context)(context)) {
                        SMBCSRV * s;
                        SMBCSRV * next;
                        DEBUG(1, ("Could not purge all servers, "
                                  "Nice way shutdown failed.\n"));
                        s = context->internal->servers;
                        while (s) {
                                DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
                                          s, s->cli->fd));
                                cli_shutdown(s->cli);
                                smbc_getFunctionRemoveCachedServer(context)(context,
                                                                         s);
                                next = s->next;
                                DLIST_REMOVE(context->internal->servers, s);
                                SAFE_FREE(s);
                                s = next;
                        }
                        context->internal->servers = NULL;
                }
        }
        else {
                /* This is the polite way */
                if (smbc_getFunctionPurgeCachedServers(context)(context)) {
                        DEBUG(1, ("Could not purge all servers, "
                                  "free_context failed.\n"));
                        errno = EBUSY;
                        return 1;
                }
                if (context->internal->servers) {
                        DEBUG(1, ("Active servers in context, "
                                  "free_context failed.\n"));
                        errno = EBUSY;
                        return 1;
                }
                if (context->internal->files) {
                        DEBUG(1, ("Active files in context, "
                                  "free_context failed.\n"));
                        errno = EBUSY;
                        return 1;
                }
        }
        
        /* Things we have to clean up */
        free(smbc_getWorkgroup(context));
        smbc_setWorkgroup(context, NULL);

        free(smbc_getNetbiosName(context));
        smbc_setNetbiosName(context, NULL);

        free(smbc_getUser(context));
        smbc_setUser(context, NULL);
        
        DEBUG(3, ("Context %p successfully freed\n", context));
        SAFE_FREE(context);
        return 0;
}


/**
 * Deprecated interface.  Do not use.  Instead, use the various
 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
 */
void
smbc_option_set(SMBCCTX *context,
                char *option_name,
                ... /* option_value */)
{
        va_list ap;
        union {
                int i;
                bool b;
                smbc_get_auth_data_with_context_fn auth_fn;
                void *v;
                const char *s;
        } option_value;
        
        va_start(ap, option_name);
        
        if (strcmp(option_name, "debug_to_stderr") == 0) {
                option_value.b = (bool) va_arg(ap, int);
                smbc_setOptionDebugToStderr(context, option_value.b);
                
        } else if (strcmp(option_name, "full_time_names") == 0) {
                option_value.b = (bool) va_arg(ap, int);
                smbc_setOptionFullTimeNames(context, option_value.b);
                
        } else if (strcmp(option_name, "open_share_mode") == 0) {
                option_value.i = va_arg(ap, int);
                smbc_setOptionOpenShareMode(context, option_value.i);
                
        } else if (strcmp(option_name, "auth_function") == 0) {
                option_value.auth_fn =
                        va_arg(ap, smbc_get_auth_data_with_context_fn);
                smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
                
        } else if (strcmp(option_name, "user_data") == 0) {
                option_value.v = va_arg(ap, void *);
                smbc_setOptionUserData(context, option_value.v);
                
        } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
                option_value.s = va_arg(ap, const char *);
                if (strcmp(option_value.s, "none") == 0) {
                        smbc_setOptionSmbEncryptionLevel(context,
                                                         SMBC_ENCRYPTLEVEL_NONE);
                } else if (strcmp(option_value.s, "request") == 0) {
                        smbc_setOptionSmbEncryptionLevel(context,
                                                         SMBC_ENCRYPTLEVEL_REQUEST);
                } else if (strcmp(option_value.s, "require") == 0) {
                        smbc_setOptionSmbEncryptionLevel(context,
                                                         SMBC_ENCRYPTLEVEL_REQUIRE);
                }
                
        } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
                option_value.i = va_arg(ap, int);
                smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
                
        } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
                option_value.b = (bool) va_arg(ap, int);
                smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
                
        } else if (strcmp(option_name, "one_share_per_server") == 0) {
                option_value.b = (bool) va_arg(ap, int);
                smbc_setOptionOneSharePerServer(context, option_value.b);
                
        } else if (strcmp(option_name, "use_kerberos") == 0) {
                option_value.b = (bool) va_arg(ap, int);
                smbc_setOptionUseKerberos(context, option_value.b);
                
        } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
                option_value.b = (bool) va_arg(ap, int);
                smbc_setOptionFallbackAfterKerberos(context, option_value.b);
                
        } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
                option_value.b = (bool) va_arg(ap, int);
                smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
        }
        
        va_end(ap);
}


/*
 * Deprecated interface.  Do not use.  Instead, use the various
 * smbc_getOption*() functions.
 */
void *
smbc_option_get(SMBCCTX *context,
                char *option_name)
{
        if (strcmp(option_name, "debug_stderr") == 0) {
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
#else
                return (void *) smbc_getOptionDebugToStderr(context);
#endif
                
        } else if (strcmp(option_name, "full_time_names") == 0) {
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
#else
                return (void *) smbc_getOptionFullTimeNames(context);
#endif
                
        } else if (strcmp(option_name, "open_share_mode") == 0) {
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
#else
                return (void *) smbc_getOptionOpenShareMode(context);
#endif
                
        } else if (strcmp(option_name, "auth_function") == 0) {
                return (void *) smbc_getFunctionAuthDataWithContext(context);
                
        } else if (strcmp(option_name, "user_data") == 0) {
                return smbc_getOptionUserData(context);
                
        } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
                switch(smbc_getOptionSmbEncryptionLevel(context))
                {
                case 0:
                        return (void *) "none";
                case 1:
                        return (void *) "request";
                case 2:
                        return (void *) "require";
                }
                
        } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
                SMBCSRV *s;
                unsigned int num_servers = 0;
                
                for (s = context->internal->servers; s; s = s->next) {
                        num_servers++;
                        if (s->cli->trans_enc_state == NULL) {
                                return (void *)false;
                        }
                }
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *) (intptr_t) (bool) (num_servers > 0);
#else
                return (void *) (bool) (num_servers > 0);
#endif
                
        } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
#else
                return (void *) smbc_getOptionBrowseMaxLmbCount(context);
#endif
                
        } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
#else
                return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
#endif
                
        } else if (strcmp(option_name, "one_share_per_server") == 0) {
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
#else
                return (void *) (bool) smbc_getOptionOneSharePerServer(context);
#endif
                
        } else if (strcmp(option_name, "use_kerberos") == 0) {
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
#else
                return (void *) (bool) smbc_getOptionUseKerberos(context);
#endif
                
        } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
#else
                return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
#endif
                
        } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
                return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
#else
                return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
#endif
        }
        
        return NULL;
}


/*
 * Initialize the library, etc.
 *
 * We accept a struct containing handle information.
 * valid values for info->debug from 0 to 100,
 * and insist that info->fn must be non-null.
 */
SMBCCTX *
smbc_init_context(SMBCCTX *context)
{
        int pid;
        char *user = NULL;
        char *home = NULL;
        extern bool in_client;
        
        if (!context) {
                errno = EBADF;
                return NULL;
        }
        
        /* Do not initialise the same client twice */
        if (context->internal->initialized) {
                return NULL;
        }
        
        if (!smbc_getFunctionAuthData(context) ||
            smbc_getDebug(context) < 0 ||
            smbc_getDebug(context) > 100) {
                
                errno = EINVAL;
                return NULL;
                
        }
        
        if (!SMBC_initialized) {
                /*
                 * Do some library-wide intializations the first time we get
                 * called
                 */
                bool conf_loaded = False;
                TALLOC_CTX *frame = talloc_stackframe();
                
                load_case_tables();
                
                setup_logging("libsmbclient", True);
                if (context->internal->debug_stderr) {
                        dbf = x_stderr;
                        x_setbuf(x_stderr, NULL);
                }
                
                /* Here we would open the smb.conf file if needed ... */
                
                in_client = True; /* FIXME, make a param */
                
                home = getenv("HOME");
                if (home) {
                        char *conf = NULL;
                        if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
                                if (lp_load(conf, True, False, False, True)) {
                                        conf_loaded = True;
                                } else {
                                        DEBUG(5, ("Could not load config file: %s\n",
                                                  conf));
                                }
                                SAFE_FREE(conf);
                        }
                }
                
                if (!conf_loaded) {
                        /*
                         * Well, if that failed, try the get_dyn_CONFIGFILE
                         * Which points to the standard locn, and if that
                         * fails, silently ignore it and use the internal
                         * defaults ...
                         */
                        
                        if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
                                DEBUG(5, ("Could not load config file: %s\n",
                                          get_dyn_CONFIGFILE()));
                        } else if (home) {
                                char *conf;
                                /*
                                 * We loaded the global config file.  Now lets
                                 * load user-specific modifications to the
                                 * global config.
                                 */
                                if (asprintf(&conf,
                                             "%s/.smb/smb.conf.append",
                                             home) > 0) {
                                        if (!lp_load(conf, True, False, False, False)) {
                                                DEBUG(10,
                                                      ("Could not append config file: "
                                                       "%s\n",
                                                       conf));
                                        }
                                        SAFE_FREE(conf);
                                }
                        }
                }
                
                load_interfaces();  /* Load the list of interfaces ... */
                
                reopen_logs();  /* Get logging working ... */
                
                /*
                 * Block SIGPIPE (from lib/util_sock.c: write())
                 * It is not needed and should not stop execution
                 */
                BlockSignals(True, SIGPIPE);
                
                /* Done with one-time initialisation */
                SMBC_initialized = 1;
                
                TALLOC_FREE(frame);
        }
        
        if (!smbc_getUser(context)) {
                /*
                 * FIXME: Is this the best way to get the user info?
                 */
                user = getenv("USER");
                /* walk around as "guest" if no username can be found */
                if (!user) {
                        user = SMB_STRDUP("guest");
                } else {
                        user = SMB_STRDUP(user);
                }

                if (!user) {
                        errno = ENOMEM;
                        return NULL;
                }

                smbc_setUser(context, user);
        }
        
        if (!smbc_getNetbiosName(context)) {
                /*
                 * We try to get our netbios name from the config. If that
                 * fails we fall back on constructing our netbios name from
                 * our hostname etc
                 */
                char *netbios_name;
                if (global_myname()) {
                        netbios_name = SMB_STRDUP(global_myname());
                } else {
                        /*
                         * Hmmm, I want to get hostname as well, but I am too
                         * lazy for the moment
                         */
                        pid = sys_getpid();
                        netbios_name = (char *)SMB_MALLOC(17);
                        if (!netbios_name) {
                                errno = ENOMEM;
                                return NULL;
                        }
                        slprintf(netbios_name, 16,
                                 "smbc%s%d", smbc_getUser(context), pid);
                }

                if (!netbios_name) {
                        errno = ENOMEM;
                        return NULL;
                }
                
                smbc_setNetbiosName(context, netbios_name);
        }
        
        DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
        
        if (!smbc_getWorkgroup(context)) {
                char *workgroup;

                if (lp_workgroup()) {
                        workgroup = SMB_STRDUP(lp_workgroup());
                }
                else {
                        /* TODO: Think about a decent default workgroup */
                        workgroup = SMB_STRDUP("samba");
                }

                if (!workgroup) {
                        errno = ENOMEM;
                        return NULL;
                }

                smbc_setWorkgroup(context, workgroup);
        }
        
        DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
        
        /* shortest timeout is 1 second */
        if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
                smbc_setTimeout(context, 1000);
        
        /*
         * FIXME: Should we check the function pointers here?
         */
        
        context->internal->initialized = True;
        
        return context;
}


/* Return the verion of samba, and thus libsmbclient */
const char *
smbc_version(void)
{
        return samba_version_string();
}