~ubuntu-branches/ubuntu/vivid/samba/vivid

« back to all changes in this revision

Viewing changes to source3/libsmb/libsmb_context.c

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2011-12-21 13:18:04 UTC
  • mfrom: (0.39.21 sid)
  • Revision ID: package-import@ubuntu.com-20111221131804-xtlr39wx6njehxxr
Tags: 2:3.6.1-3ubuntu1
* Merge from Debian testing.  Remaining changes:
  + debian/patches/VERSION.patch:
    - set SAMBA_VERSION_SUFFIX to Ubuntu.
  + debian/patches/error-trans.fix-276472:
    - Add the translation of Unix Error code -ENOTSUP to NT Error Code
    - NT_STATUS_NOT_SUPPORTED to prevent the Permission denied error.
  + debian/smb.conf:
    - add "(Samba, Ubuntu)" to server string.
    - comment out the default [homes] share, and add a comment about
      "valid users = %S" to show users how to restrict access to
      \\server\username to only username.
    - Set 'usershare allow guests', so that usershare admins are 
      allowed to create public shares in addition to authenticated
      ones.
    - add map to guest = Bad user, maps bad username to guest access.
  + debian/samba-common.config:
    - Do not change priority to high if dhclient3 is installed.
    - Use priority medium instead of high for the workgroup question.
  + debian/control:
    - Don't build against or suggest ctdb.
    - Add dependency on samba-common-bin to samba.
  + Add ufw integration:
    - Created debian/samba.ufw.profile
    - debian/rules, debian/samba.dirs, debian/samba.files: install
      profile
    - debian/control: have samba suggest ufw
  + Add apport hook:
    - Created debian/source_samba.py.
    - debian/rules, debian/samba.dirs, debian/samba-common-bin.files: install
  + Switch to upstart:
    - Add debian/samba.{nmbd,smbd}.upstart.
  + debian/samba.logrotate, debian/samba-common.dhcp, debian/samba.if-up:
    - Make them upstart compatible
  + debian/samba.postinst: 
    - Avoid scary pdbedit warnings on first import.
  + debian/samba-common.postinst: Add more informative error message for
    the case where smb.conf was manually deleted
  + debian/patches/fix-debuglevel-name-conflict.patch: don't use 'debug_level'
    as a global variable name in an NSS module 
  + Dropped:
    - debian/patches/error-trans.fix-276472
    - debian/patches/fix-debuglevel-name-conflict.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
   Copyright (C) Tom Jansen (Ninja ISD) 2002 
8
8
   Copyright (C) Derrell Lipman 2003-2008
9
9
   Copyright (C) Jeremy Allison 2007, 2008
10
 
   
 
10
 
11
11
   This program is free software; you can redistribute it and/or modify
12
12
   it under the terms of the GNU General Public License as published by
13
13
   the Free Software Foundation; either version 3 of the License, or
14
14
   (at your option) any later version.
15
 
   
 
15
 
16
16
   This program is distributed in the hope that it will be useful,
17
17
   but WITHOUT ANY WARRANTY; without even the implied warranty of
18
18
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
19
   GNU General Public License for more details.
20
 
   
 
20
 
21
21
   You should have received a copy of the GNU General Public License
22
22
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
23
*/
24
24
 
25
25
#include "includes.h"
 
26
#include "libsmb/libsmb.h"
26
27
#include "libsmbclient.h"
27
28
#include "libsmb_internal.h"
 
29
#include "secrets.h"
28
30
 
29
31
 
30
32
/*
43
45
    bool conf_loaded = False;
44
46
    char *home = NULL;
45
47
    TALLOC_CTX *frame = talloc_stackframe();
46
 
                
47
 
    load_case_tables();
48
 
                
49
 
    setup_logging("libsmbclient", True);
 
48
 
 
49
    load_case_tables_library();
 
50
 
 
51
    setup_logging("libsmbclient", DEBUG_STDOUT);
50
52
 
51
53
    /* Here we would open the smb.conf file if needed ... */
52
 
                
 
54
 
53
55
    lp_set_in_client(True);
54
 
                
 
56
 
55
57
    home = getenv("HOME");
56
58
    if (home) {
57
59
        char *conf = NULL;
65
67
            SAFE_FREE(conf);
66
68
        }
67
69
    }
68
 
                
 
70
 
69
71
    if (!conf_loaded) {
70
72
        /*
71
73
         * Well, if that failed, try the get_dyn_CONFIGFILE
73
75
         * fails, silently ignore it and use the internal
74
76
         * defaults ...
75
77
         */
76
 
                        
 
78
 
77
79
        if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
78
80
            DEBUG(5, ("Could not load config file: %s\n",
79
81
                      get_dyn_CONFIGFILE()));
97
99
            }
98
100
        }
99
101
    }
100
 
                
 
102
 
101
103
    load_interfaces();  /* Load the list of interfaces ... */
102
 
                
 
104
 
103
105
    reopen_logs();  /* Get logging working ... */
104
 
                
 
106
 
105
107
    /*
106
108
     * Block SIGPIPE (from lib/util_sock.c: write())
107
109
     * It is not needed and should not stop execution
108
110
     */
109
111
    BlockSignals(True, SIGPIPE);
110
 
                
 
112
 
111
113
    /* Create the mutex we'll use to protect initialized_ctx_count */
112
114
    if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
113
115
                                initialized_ctx_count_mutex) != 0) {
136
138
smbc_new_context(void)
137
139
{
138
140
        SMBCCTX *context;
139
 
        
 
141
 
140
142
        /* The first call to this function should initialize the module */
141
143
        SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);
142
144
 
149
151
                errno = ENOMEM;
150
152
                return NULL;
151
153
        }
152
 
        
 
154
 
153
155
        ZERO_STRUCTP(context);
154
 
        
 
156
 
155
157
        context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
156
158
        if (!context->internal) {
157
159
                SAFE_FREE(context);
158
160
                errno = ENOMEM;
159
161
                return NULL;
160
162
        }
161
 
        
 
163
 
162
164
        /* Initialize the context and establish reasonable defaults */
163
165
        ZERO_STRUCTP(context->internal);
164
 
        
 
166
 
165
167
        smbc_setDebug(context, 0);
166
168
        smbc_setTimeout(context, 20000);
167
 
        
 
169
 
168
170
        smbc_setOptionFullTimeNames(context, False);
169
171
        smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
170
172
        smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
176
178
        if (getenv("LIBSMBCLIENT_NO_CCACHE") == NULL) {
177
179
                smbc_setOptionUseCCache(context, true);
178
180
        }
179
 
        
 
181
 
180
182
        smbc_setFunctionAuthData(context, SMBC_get_auth_data);
181
183
        smbc_setFunctionCheckServer(context, SMBC_check_server);
182
184
        smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
183
 
        
 
185
 
184
186
        smbc_setOptionUserData(context, NULL);
185
187
        smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
186
188
        smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
187
189
        smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
188
190
        smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
189
 
        
 
191
 
190
192
        smbc_setFunctionOpen(context, SMBC_open_ctx);
191
193
        smbc_setFunctionCreat(context, SMBC_creat_ctx);
192
194
        smbc_setFunctionRead(context, SMBC_read_ctx);
215
217
        smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
216
218
        smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
217
219
        smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
218
 
        
 
220
 
219
221
        smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
220
222
        smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
221
223
        smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
222
224
        smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
223
 
        
 
225
 
224
226
        return context;
225
227
}
226
228
 
239
241
                errno = EBADF;
240
242
                return 1;
241
243
        }
242
 
        
 
244
 
243
245
        if (shutdown_ctx) {
244
246
                SMBCFILE * f;
245
247
                DEBUG(1,("Performing aggressive shutdown.\n"));
246
 
                
 
248
 
247
249
                f = context->internal->files;
248
250
                while (f) {
249
251
                        smbc_getFunctionClose(context)(context, f);
250
252
                        f = f->next;
251
253
                }
252
254
                context->internal->files = NULL;
253
 
                
 
255
 
254
256
                /* First try to remove the servers the nice way. */
255
257
                if (smbc_getFunctionPurgeCachedServers(context)(context)) {
256
258
                        SMBCSRV * s;
293
295
                        return 1;
294
296
                }
295
297
        }
296
 
        
 
298
 
297
299
        /* Things we have to clean up */
298
300
        smbc_setWorkgroup(context, NULL);
299
301
        smbc_setNetbiosName(context, NULL);
300
302
        smbc_setUser(context, NULL);
301
 
        
 
303
 
302
304
        DEBUG(3, ("Context %p successfully freed\n", context));
303
305
 
304
306
        /* Free any DFS auth context. */
324
326
        if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
325
327
                smb_panic("error unlocking 'initialized_ctx_count'");
326
328
        }
327
 
        
 
329
 
328
330
        return 0;
329
331
}
330
332
 
346
348
                void *v;
347
349
                const char *s;
348
350
        } option_value;
349
 
        
 
351
 
350
352
        va_start(ap, option_name);
351
 
        
 
353
 
352
354
        if (strcmp(option_name, "debug_to_stderr") == 0) {
353
355
                option_value.b = (bool) va_arg(ap, int);
354
356
                smbc_setOptionDebugToStderr(context, option_value.b);
355
 
                
 
357
 
356
358
        } else if (strcmp(option_name, "full_time_names") == 0) {
357
359
                option_value.b = (bool) va_arg(ap, int);
358
360
                smbc_setOptionFullTimeNames(context, option_value.b);
359
 
                
 
361
 
360
362
        } else if (strcmp(option_name, "open_share_mode") == 0) {
361
363
                option_value.i = va_arg(ap, int);
362
364
                smbc_setOptionOpenShareMode(context, option_value.i);
363
 
                
 
365
 
364
366
        } else if (strcmp(option_name, "auth_function") == 0) {
365
367
                option_value.auth_fn =
366
368
                        va_arg(ap, smbc_get_auth_data_with_context_fn);
367
369
                smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
368
 
                
 
370
 
369
371
        } else if (strcmp(option_name, "user_data") == 0) {
370
372
                option_value.v = va_arg(ap, void *);
371
373
                smbc_setOptionUserData(context, option_value.v);
372
 
                
 
374
 
373
375
        } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
374
376
                option_value.s = va_arg(ap, const char *);
375
377
                if (strcmp(option_value.s, "none") == 0) {
382
384
                        smbc_setOptionSmbEncryptionLevel(context,
383
385
                                                         SMBC_ENCRYPTLEVEL_REQUIRE);
384
386
                }
385
 
                
 
387
 
386
388
        } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
387
389
                option_value.i = va_arg(ap, int);
388
390
                smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
389
 
                
 
391
 
390
392
        } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
391
393
                option_value.b = (bool) va_arg(ap, int);
392
394
                smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
393
 
                
 
395
 
394
396
        } else if (strcmp(option_name, "one_share_per_server") == 0) {
395
397
                option_value.b = (bool) va_arg(ap, int);
396
398
                smbc_setOptionOneSharePerServer(context, option_value.b);
397
 
                
 
399
 
398
400
        } else if (strcmp(option_name, "use_kerberos") == 0) {
399
401
                option_value.b = (bool) va_arg(ap, int);
400
402
                smbc_setOptionUseKerberos(context, option_value.b);
401
 
                
 
403
 
402
404
        } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
403
405
                option_value.b = (bool) va_arg(ap, int);
404
406
                smbc_setOptionFallbackAfterKerberos(context, option_value.b);
405
 
                
 
407
 
406
408
        } else if (strcmp(option_name, "use_ccache") == 0) {
407
409
                option_value.b = (bool) va_arg(ap, int);
408
410
                smbc_setOptionUseCCache(context, option_value.b);
411
413
                option_value.b = (bool) va_arg(ap, int);
412
414
                smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
413
415
        }
414
 
        
 
416
 
415
417
        va_end(ap);
416
418
}
417
419
 
430
432
#else
431
433
                return (void *) smbc_getOptionDebugToStderr(context);
432
434
#endif
433
 
                
 
435
 
434
436
        } else if (strcmp(option_name, "full_time_names") == 0) {
435
437
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
436
438
                return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
437
439
#else
438
440
                return (void *) smbc_getOptionFullTimeNames(context);
439
441
#endif
440
 
                
 
442
 
441
443
        } else if (strcmp(option_name, "open_share_mode") == 0) {
442
444
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
443
445
                return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
444
446
#else
445
447
                return (void *) smbc_getOptionOpenShareMode(context);
446
448
#endif
447
 
                
 
449
 
448
450
        } else if (strcmp(option_name, "auth_function") == 0) {
449
451
                return (void *) smbc_getFunctionAuthDataWithContext(context);
450
 
                
 
452
 
451
453
        } else if (strcmp(option_name, "user_data") == 0) {
452
454
                return smbc_getOptionUserData(context);
453
 
                
 
455
 
454
456
        } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
455
457
                switch(smbc_getOptionSmbEncryptionLevel(context))
456
458
                {
461
463
                case 2:
462
464
                        return (void *) "require";
463
465
                }
464
 
                
 
466
 
465
467
        } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
466
468
                SMBCSRV *s;
467
469
                unsigned int num_servers = 0;
468
 
                
 
470
 
469
471
                for (s = context->internal->servers; s; s = s->next) {
470
472
                        num_servers++;
471
473
                        if (s->cli->trans_enc_state == NULL) {
477
479
#else
478
480
                return (void *) (bool) (num_servers > 0);
479
481
#endif
480
 
                
 
482
 
481
483
        } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
482
484
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
483
485
                return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
484
486
#else
485
487
                return (void *) smbc_getOptionBrowseMaxLmbCount(context);
486
488
#endif
487
 
                
 
489
 
488
490
        } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
489
491
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
490
492
                return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
491
493
#else
492
494
                return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
493
495
#endif
494
 
                
 
496
 
495
497
        } else if (strcmp(option_name, "one_share_per_server") == 0) {
496
498
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
497
499
                return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
498
500
#else
499
501
                return (void *) (bool) smbc_getOptionOneSharePerServer(context);
500
502
#endif
501
 
                
 
503
 
502
504
        } else if (strcmp(option_name, "use_kerberos") == 0) {
503
505
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
504
506
                return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
505
507
#else
506
508
                return (void *) (bool) smbc_getOptionUseKerberos(context);
507
509
#endif
508
 
                
 
510
 
509
511
        } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
510
512
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
511
513
                return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
512
514
#else
513
515
                return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
514
516
#endif
515
 
                
 
517
 
516
518
        } else if (strcmp(option_name, "use_ccache") == 0) {
517
519
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
518
520
                return (void *) (intptr_t) smbc_getOptionUseCCache(context);
527
529
                return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
528
530
#endif
529
531
        }
530
 
        
 
532
 
531
533
        return NULL;
532
534
}
533
535
 
543
545
smbc_init_context(SMBCCTX *context)
544
546
{
545
547
        int pid;
546
 
        
 
548
 
547
549
        if (!context) {
548
550
                errno = EBADF;
549
551
                return NULL;
550
552
        }
551
 
        
 
553
 
552
554
        /* Do not initialise the same client twice */
553
555
        if (context->internal->initialized) {
554
556
                return NULL;
555
557
        }
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
 
                
 
558
 
567
559
        if ((!smbc_getFunctionAuthData(context) &&
568
560
             !smbc_getFunctionAuthDataWithContext(context)) ||
569
561
            smbc_getDebug(context) < 0 ||
570
562
            smbc_getDebug(context) > 100) {
571
 
                
 
563
 
572
564
                errno = EINVAL;
573
565
                return NULL;
574
 
                
 
566
 
575
567
        }
576
 
        
 
568
 
577
569
        if (!smbc_getUser(context)) {
578
570
                /*
579
571
                 * FIXME: Is this the best way to get the user info?
580
572
                 */
581
 
                char *user = getenv("USER");
 
573
                char *user = getenv("USER");
582
574
                /* walk around as "guest" if no username can be found */
583
575
                if (!user) {
584
576
                        user = SMB_STRDUP("guest");
594
586
                smbc_setUser(context, user);
595
587
                SAFE_FREE(user);
596
588
 
597
 
                if (!smbc_getUser(context)) {
 
589
                if (!smbc_getUser(context)) {
598
590
                        errno = ENOMEM;
599
591
                        return NULL;
600
592
                }
601
593
        }
602
 
        
 
594
 
603
595
        if (!smbc_getNetbiosName(context)) {
604
596
                /*
605
597
                 * We try to get our netbios name from the config. If that
628
620
                        errno = ENOMEM;
629
621
                        return NULL;
630
622
                }
631
 
                
 
623
 
632
624
                smbc_setNetbiosName(context, netbios_name);
633
625
                SAFE_FREE(netbios_name);
634
626
 
637
629
                        return NULL;
638
630
                }
639
631
        }
640
 
        
 
632
 
641
633
        DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
642
 
        
 
634
 
643
635
        if (!smbc_getWorkgroup(context)) {
644
636
                char *workgroup;
645
637
 
664
656
                        return NULL;
665
657
                }
666
658
        }
667
 
        
 
659
 
668
660
        DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
669
 
        
 
661
 
670
662
        /* shortest timeout is 1 second */
671
663
        if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
672
664
                smbc_setTimeout(context, 1000);
673
 
        
 
665
 
674
666
        context->internal->initialized = True;
675
667
 
676
668
        /* Protect access to the count of contexts in use */
684
676
        if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
685
677
                smb_panic("error unlocking 'initialized_ctx_count'");
686
678
        }
687
 
        
 
679
 
688
680
        return context;
689
681
}
690
682