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

« back to all changes in this revision

Viewing changes to source3/lib/util.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:
11
11
   it under the terms of the GNU General Public License as published by
12
12
   the Free Software Foundation; either version 3 of the License, or
13
13
   (at your option) any later version.
14
 
   
 
14
 
15
15
   This program is distributed in the hope that it will be useful,
16
16
   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
17
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
18
   GNU General Public License for more details.
19
 
   
 
19
 
20
20
   You should have received a copy of the GNU General Public License
21
21
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
*/
23
23
 
24
24
#include "includes.h"
 
25
#include "system/passwd.h"
 
26
#include "system/filesys.h"
 
27
#include "util_tdb.h"
 
28
#include "ctdbd_conn.h"
 
29
#include "../lib/util/util_pw.h"
 
30
#include "messages.h"
 
31
#include "libcli/security/security.h"
25
32
 
26
33
extern char *global_clobber_region_function;
27
34
extern unsigned int global_clobber_region_line;
73
80
 Definitions for all names.
74
81
***********************************************************************/
75
82
 
76
 
static char *smb_myname;
77
 
static char *smb_myworkgroup;
78
83
static char *smb_scope;
79
84
static int smb_num_netbios_names;
80
85
static char **smb_my_netbios_names;
81
86
 
82
87
/***********************************************************************
83
 
 Allocate and set myname. Ensure upper case.
84
 
***********************************************************************/
85
 
 
86
 
bool set_global_myname(const char *myname)
87
 
{
88
 
        SAFE_FREE(smb_myname);
89
 
        smb_myname = SMB_STRDUP(myname);
90
 
        if (!smb_myname)
91
 
                return False;
92
 
        strupper_m(smb_myname);
93
 
        return True;
94
 
}
95
 
 
96
 
const char *global_myname(void)
97
 
{
98
 
        return smb_myname;
99
 
}
100
 
 
101
 
/***********************************************************************
102
 
 Allocate and set myworkgroup. Ensure upper case.
103
 
***********************************************************************/
104
 
 
105
 
bool set_global_myworkgroup(const char *myworkgroup)
106
 
{
107
 
        SAFE_FREE(smb_myworkgroup);
108
 
        smb_myworkgroup = SMB_STRDUP(myworkgroup);
109
 
        if (!smb_myworkgroup)
110
 
                return False;
111
 
        strupper_m(smb_myworkgroup);
112
 
        return True;
113
 
}
114
 
 
115
 
const char *lp_workgroup(void)
116
 
{
117
 
        return smb_myworkgroup;
118
 
}
119
 
 
120
 
/***********************************************************************
121
88
 Allocate and set scope. Ensure upper case.
122
89
***********************************************************************/
123
90
 
184
151
 
185
152
void gfree_names(void)
186
153
{
187
 
        SAFE_FREE( smb_myname );
188
 
        SAFE_FREE( smb_myworkgroup );
 
154
        gfree_netbios_names();
189
155
        SAFE_FREE( smb_scope );
190
156
        free_netbios_names_array();
191
157
        free_local_machine_name();
261
227
 
262
228
        if (global_myname() == NULL || *global_myname() == '\0') {
263
229
                if (!set_global_myname(myhostname())) {
264
 
                        DEBUG( 0, ( "init_structs: malloc fail.\n" ) );
 
230
                        DEBUG( 0, ( "init_names: malloc fail.\n" ) );
265
231
                        return False;
266
232
                }
267
233
        }
268
234
 
269
235
        if (!set_netbios_aliases(lp_netbios_aliases())) {
270
 
                DEBUG( 0, ( "init_structs: malloc fail.\n" ) );
 
236
                DEBUG( 0, ( "init_names: malloc fail.\n" ) );
271
237
                return False;
272
238
        }
273
239
 
282
248
        return( True );
283
249
}
284
250
 
285
 
/**************************************************************************n
286
 
  Code to cope with username/password auth options from the commandline.
287
 
  Used mainly in client tools.
288
 
****************************************************************************/
289
 
 
290
 
struct user_auth_info *user_auth_info_init(TALLOC_CTX *mem_ctx)
291
 
{
292
 
        struct user_auth_info *result;
293
 
 
294
 
        result = TALLOC_ZERO_P(mem_ctx, struct user_auth_info);
295
 
        if (result == NULL) {
296
 
                return NULL;
297
 
        }
298
 
 
299
 
        result->signing_state = Undefined;
300
 
        return result;
301
 
}
302
 
 
303
 
const char *get_cmdline_auth_info_username(const struct user_auth_info *auth_info)
304
 
{
305
 
        if (!auth_info->username) {
306
 
                return "";
307
 
        }
308
 
        return auth_info->username;
309
 
}
310
 
 
311
 
void set_cmdline_auth_info_username(struct user_auth_info *auth_info,
312
 
                                    const char *username)
313
 
{
314
 
        TALLOC_FREE(auth_info->username);
315
 
        auth_info->username = talloc_strdup(auth_info, username);
316
 
        if (!auth_info->username) {
317
 
                exit(ENOMEM);
318
 
        }
319
 
}
320
 
 
321
 
const char *get_cmdline_auth_info_domain(const struct user_auth_info *auth_info)
322
 
{
323
 
        if (!auth_info->domain) {
324
 
                return "";
325
 
        }
326
 
        return auth_info->domain;
327
 
}
328
 
 
329
 
void set_cmdline_auth_info_domain(struct user_auth_info *auth_info,
330
 
                                  const char *domain)
331
 
{
332
 
        TALLOC_FREE(auth_info->domain);
333
 
        auth_info->domain = talloc_strdup(auth_info, domain);
334
 
        if (!auth_info->domain) {
335
 
                exit(ENOMEM);
336
 
        }
337
 
}
338
 
 
339
 
const char *get_cmdline_auth_info_password(const struct user_auth_info *auth_info)
340
 
{
341
 
        if (!auth_info->password) {
342
 
                return "";
343
 
        }
344
 
        return auth_info->password;
345
 
}
346
 
 
347
 
void set_cmdline_auth_info_password(struct user_auth_info *auth_info,
348
 
                                    const char *password)
349
 
{
350
 
        TALLOC_FREE(auth_info->password);
351
 
        if (password == NULL) {
352
 
                password = "";
353
 
        }
354
 
        auth_info->password = talloc_strdup(auth_info, password);
355
 
        if (!auth_info->password) {
356
 
                exit(ENOMEM);
357
 
        }
358
 
        auth_info->got_pass = true;
359
 
}
360
 
 
361
 
bool set_cmdline_auth_info_signing_state(struct user_auth_info *auth_info,
362
 
                                         const char *arg)
363
 
{
364
 
        auth_info->signing_state = -1;
365
 
        if (strequal(arg, "off") || strequal(arg, "no") ||
366
 
                        strequal(arg, "false")) {
367
 
                auth_info->signing_state = false;
368
 
        } else if (strequal(arg, "on") || strequal(arg, "yes") ||
369
 
                        strequal(arg, "true") || strequal(arg, "auto")) {
370
 
                auth_info->signing_state = true;
371
 
        } else if (strequal(arg, "force") || strequal(arg, "required") ||
372
 
                        strequal(arg, "forced")) {
373
 
                auth_info->signing_state = Required;
374
 
        } else {
375
 
                return false;
376
 
        }
377
 
        return true;
378
 
}
379
 
 
380
 
int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)
381
 
{
382
 
        return auth_info->signing_state;
383
 
}
384
 
 
385
 
void set_cmdline_auth_info_use_ccache(struct user_auth_info *auth_info, bool b)
386
 
{
387
 
        auth_info->use_ccache = b;
388
 
}
389
 
 
390
 
bool get_cmdline_auth_info_use_ccache(const struct user_auth_info *auth_info)
391
 
{
392
 
        return auth_info->use_ccache;
393
 
}
394
 
 
395
 
void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,
396
 
                                        bool b)
397
 
{
398
 
        auth_info->use_kerberos = b;
399
 
}
400
 
 
401
 
bool get_cmdline_auth_info_use_kerberos(const struct user_auth_info *auth_info)
402
 
{
403
 
        return auth_info->use_kerberos;
404
 
}
405
 
 
406
 
void set_cmdline_auth_info_fallback_after_kerberos(struct user_auth_info *auth_info,
407
 
                                        bool b)
408
 
{
409
 
        auth_info->fallback_after_kerberos = b;
410
 
}
411
 
 
412
 
bool get_cmdline_auth_info_fallback_after_kerberos(const struct user_auth_info *auth_info)
413
 
{
414
 
        return auth_info->fallback_after_kerberos;
415
 
}
416
 
 
417
 
/* This should only be used by lib/popt_common.c JRA */
418
 
void set_cmdline_auth_info_use_krb5_ticket(struct user_auth_info *auth_info)
419
 
{
420
 
        auth_info->use_kerberos = true;
421
 
        auth_info->got_pass = true;
422
 
}
423
 
 
424
 
/* This should only be used by lib/popt_common.c JRA */
425
 
void set_cmdline_auth_info_smb_encrypt(struct user_auth_info *auth_info)
426
 
{
427
 
        auth_info->smb_encrypt = true;
428
 
}
429
 
 
430
 
void set_cmdline_auth_info_use_machine_account(struct user_auth_info *auth_info)
431
 
{
432
 
        auth_info->use_machine_account = true;
433
 
}
434
 
 
435
 
bool get_cmdline_auth_info_got_pass(const struct user_auth_info *auth_info)
436
 
{
437
 
        return auth_info->got_pass;
438
 
}
439
 
 
440
 
bool get_cmdline_auth_info_smb_encrypt(const struct user_auth_info *auth_info)
441
 
{
442
 
        return auth_info->smb_encrypt;
443
 
}
444
 
 
445
 
bool get_cmdline_auth_info_use_machine_account(const struct user_auth_info *auth_info)
446
 
{
447
 
        return auth_info->use_machine_account;
448
 
}
449
 
 
450
 
struct user_auth_info *get_cmdline_auth_info_copy(TALLOC_CTX *mem_ctx,
451
 
                                                  const struct user_auth_info *src)
452
 
{
453
 
        struct user_auth_info *result;
454
 
 
455
 
        result = user_auth_info_init(mem_ctx);
456
 
        if (result == NULL) {
457
 
                return NULL;
458
 
        }
459
 
 
460
 
        *result = *src;
461
 
 
462
 
        result->username = talloc_strdup(
463
 
                result, get_cmdline_auth_info_username(src));
464
 
        result->password = talloc_strdup(
465
 
                result, get_cmdline_auth_info_password(src));
466
 
        if ((result->username == NULL) || (result->password == NULL)) {
467
 
                TALLOC_FREE(result);
468
 
                return NULL;
469
 
        }
470
 
 
471
 
        return result;
472
 
}
473
 
 
474
 
bool set_cmdline_auth_info_machine_account_creds(struct user_auth_info *auth_info)
475
 
{
476
 
        char *pass = NULL;
477
 
        char *account = NULL;
478
 
 
479
 
        if (!get_cmdline_auth_info_use_machine_account(auth_info)) {
480
 
                return false;
481
 
        }
482
 
 
483
 
        if (!secrets_init()) {
484
 
                d_printf("ERROR: Unable to open secrets database\n");
485
 
                return false;
486
 
        }
487
 
 
488
 
        if (asprintf(&account, "%s$@%s", global_myname(), lp_realm()) < 0) {
489
 
                return false;
490
 
        }
491
 
 
492
 
        pass = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
493
 
        if (!pass) {
494
 
                d_printf("ERROR: Unable to fetch machine password for "
495
 
                        "%s in domain %s\n",
496
 
                        account, lp_workgroup());
497
 
                SAFE_FREE(account);
498
 
                return false;
499
 
        }
500
 
 
501
 
        set_cmdline_auth_info_username(auth_info, account);
502
 
        set_cmdline_auth_info_password(auth_info, pass);
503
 
 
504
 
        SAFE_FREE(account);
505
 
        SAFE_FREE(pass);
506
 
 
507
 
        return true;
508
 
}
509
 
 
510
 
/****************************************************************************
511
 
 Ensure we have a password if one not given.
512
 
****************************************************************************/
513
 
 
514
 
void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)
515
 
{
516
 
        char *label = NULL;
517
 
        char *pass;
518
 
        TALLOC_CTX *frame;
519
 
 
520
 
        if (get_cmdline_auth_info_got_pass(auth_info) ||
521
 
                        get_cmdline_auth_info_use_kerberos(auth_info)) {
522
 
                /* Already got one... */
523
 
                return;
524
 
        }
525
 
 
526
 
        frame = talloc_stackframe();
527
 
        label = talloc_asprintf(frame, "Enter %s's password: ",
528
 
                        get_cmdline_auth_info_username(auth_info));
529
 
        pass = getpass(label);
530
 
        if (pass) {
531
 
                set_cmdline_auth_info_password(auth_info, pass);
532
 
        }
533
 
        TALLOC_FREE(frame);
534
 
}
535
 
 
536
251
/*******************************************************************
537
252
 Check if a file exists - call vfs_file_exist for samba files.
538
253
********************************************************************/
595
310
 
596
311
        attrstr[0] = 0;
597
312
 
598
 
        if (mode & aVOLID) fstrcat(attrstr,"V");
599
 
        if (mode & aDIR) fstrcat(attrstr,"D");
600
 
        if (mode & aARCH) fstrcat(attrstr,"A");
601
 
        if (mode & aHIDDEN) fstrcat(attrstr,"H");
602
 
        if (mode & aSYSTEM) fstrcat(attrstr,"S");
603
 
        if (mode & aRONLY) fstrcat(attrstr,"R");          
 
313
        if (mode & FILE_ATTRIBUTE_VOLUME) fstrcat(attrstr,"V");
 
314
        if (mode & FILE_ATTRIBUTE_DIRECTORY) fstrcat(attrstr,"D");
 
315
        if (mode & FILE_ATTRIBUTE_ARCHIVE) fstrcat(attrstr,"A");
 
316
        if (mode & FILE_ATTRIBUTE_HIDDEN) fstrcat(attrstr,"H");
 
317
        if (mode & FILE_ATTRIBUTE_SYSTEM) fstrcat(attrstr,"S");
 
318
        if (mode & FILE_ATTRIBUTE_READONLY) fstrcat(attrstr,"R");
604
319
 
605
320
        return talloc_strdup(talloc_tos(), attrstr);
606
321
}
616
331
 
617
332
        if (!DEBUGLVL(5))
618
333
                return;
619
 
        
 
334
 
620
335
        DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
621
336
                        smb_len(buf),
622
337
                        (int)CVAL(buf,smb_com),
635
350
        for (i=0;i<(int)CVAL(buf,smb_wct);i++)
636
351
                DEBUGADD(5,("smb_vwv[%2d]=%5d (0x%X)\n",i,
637
352
                        SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
638
 
        
 
353
 
639
354
        bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
640
355
 
641
356
        DEBUGADD(5,("smb_bcc=%d\n",bcc));
862
577
#endif
863
578
}
864
579
 
865
 
/*******************************************************************
866
 
 Sleep for a specified number of milliseconds.
867
 
********************************************************************/
868
 
 
869
 
void smb_msleep(unsigned int t)
870
 
{
871
 
#if defined(HAVE_NANOSLEEP)
872
 
        struct timespec tval;
873
 
        int ret;
874
 
 
875
 
        tval.tv_sec = t/1000;
876
 
        tval.tv_nsec = 1000000*(t%1000);
877
 
 
878
 
        do {
879
 
                errno = 0;
880
 
                ret = nanosleep(&tval, &tval);
881
 
        } while (ret < 0 && errno == EINTR && (tval.tv_sec > 0 || tval.tv_nsec > 0));
882
 
#else
883
 
        unsigned int tdiff=0;
884
 
        struct timeval tval,t1,t2;  
885
 
        fd_set fds;
886
 
 
887
 
        GetTimeOfDay(&t1);
888
 
        t2 = t1;
889
 
  
890
 
        while (tdiff < t) {
891
 
                tval.tv_sec = (t-tdiff)/1000;
892
 
                tval.tv_usec = 1000*((t-tdiff)%1000);
893
 
 
894
 
                /* Never wait for more than 1 sec. */
895
 
                if (tval.tv_sec > 1) {
896
 
                        tval.tv_sec = 1; 
897
 
                        tval.tv_usec = 0;
898
 
                }
899
 
 
900
 
                FD_ZERO(&fds);
901
 
                errno = 0;
902
 
                sys_select_intr(0,&fds,NULL,NULL,&tval);
903
 
 
904
 
                GetTimeOfDay(&t2);
905
 
                if (t2.tv_sec < t1.tv_sec) {
906
 
                        /* Someone adjusted time... */
907
 
                        t1 = t2;
908
 
                }
909
 
 
910
 
                tdiff = TvalDiff(&t1,&t2);
911
 
        }
912
 
#endif
913
 
}
914
580
 
915
581
NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
916
 
                       struct event_context *ev_ctx,
917
 
                       bool parent_longlived)
 
582
                           struct event_context *ev_ctx,
 
583
                           struct server_id id,
 
584
                           bool parent_longlived)
918
585
{
919
586
        NTSTATUS status = NT_STATUS_OK;
920
587
 
931
598
                goto done;
932
599
        }
933
600
 
934
 
        if (ev_ctx) {
935
 
                event_context_reinit(ev_ctx);
 
601
        if (ev_ctx && tevent_re_initialise(ev_ctx) != 0) {
 
602
                smb_panic(__location__ ": Failed to re-initialise event context");
936
603
        }
937
604
 
938
605
        if (msg_ctx) {
940
607
                 * For clustering, we need to re-init our ctdbd connection after the
941
608
                 * fork
942
609
                 */
943
 
                status = messaging_reinit(msg_ctx);
 
610
                status = messaging_reinit(msg_ctx, id);
944
611
                if (!NT_STATUS_IS_OK(status)) {
945
612
                        DEBUG(0,("messaging_reinit() failed: %s\n",
946
613
                                 nt_errstr(status)));
950
617
        return status;
951
618
}
952
619
 
953
 
/****************************************************************************
954
 
 Put up a yes/no prompt.
955
 
****************************************************************************/
956
 
 
957
 
bool yesno(const char *p)
958
 
{
959
 
        char ans[20];
960
 
        printf("%s",p);
961
 
 
962
 
        if (!fgets(ans,sizeof(ans)-1,stdin))
963
 
                return(False);
964
 
 
965
 
        if (*ans == 'y' || *ans == 'Y')
966
 
                return(True);
967
 
 
968
 
        return(False);
969
 
}
970
 
 
971
620
#if defined(PARANOID_MALLOC_CHECKER)
972
621
 
973
622
/****************************************************************************
1207
856
                return(PROTOCOL_COREPLUS);
1208
857
        if (strequal(str,"CORE+"))
1209
858
                return(PROTOCOL_COREPLUS);
1210
 
  
 
859
 
1211
860
        DEBUG(0,("Unrecognised protocol level %s\n",str));
1212
 
  
 
861
 
1213
862
        return(def);
1214
863
}
1215
864
 
1352
1001
        }
1353
1002
 
1354
1003
#ifdef CLUSTER_SUPPORT
1355
 
        return ctdbd_process_exists(messaging_ctdbd_connection(), pid.vnn,
1356
 
                                    pid.pid);
 
1004
        return ctdbd_process_exists(messaging_ctdbd_connection(),
 
1005
                                    pid.vnn, pid.pid);
1357
1006
#else
1358
1007
        return False;
1359
1008
#endif
1410
1059
        char *p;
1411
1060
        uid_t u;
1412
1061
 
1413
 
        pass = Get_Pwnam_alloc(talloc_autofree_context(), name);
 
1062
        pass = Get_Pwnam_alloc(talloc_tos(), name);
1414
1063
        if (pass) {
1415
1064
                u = pass->pw_uid;
1416
1065
                TALLOC_FREE(pass);
1570
1219
 
1571
1220
        DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 
1572
1221
                  (unsigned long)backtrace_size));
1573
 
        
 
1222
 
1574
1223
        if (backtrace_strings) {
1575
1224
                int i;
1576
1225
 
1634
1283
 
1635
1284
        if (!p)
1636
1285
                return(NULL);
1637
 
  
 
1286
 
1638
1287
        ptr = (SMB_STRUCT_DIRENT *)sys_readdir(p);
1639
1288
        if (!ptr)
1640
1289
                return(NULL);
1708
1357
 remove a potentially expensive call to mask_match
1709
1358
 if possible.
1710
1359
********************************************************************/
1711
 
 
 
1360
 
1712
1361
void set_namearray(name_compare_entry **ppname_array, const char *namelist)
1713
1362
{
1714
1363
        char *name_end;
1784
1433
                nameptr = name_end + 1;
1785
1434
                i++;
1786
1435
        }
1787
 
  
 
1436
 
1788
1437
        (*ppname_array)[i].name = NULL;
1789
1438
 
1790
1439
        return;
1844
1493
        *poffset = lock.l_start;
1845
1494
        *pcount = lock.l_len;
1846
1495
        *ppid = lock.l_pid;
1847
 
        
 
1496
 
1848
1497
        DEBUG(3,("fcntl_getlock: fd %d is returned info %d pid %u\n",
1849
1498
                        fd, (int)lock.l_type, (unsigned int)lock.l_pid));
1850
1499
        return True;
1962
1611
        case RA_CIFSFS:
1963
1612
                remote_arch_str = "CIFSFS";
1964
1613
                break;
 
1614
        case RA_OSX:
 
1615
                remote_arch_str = "OSX";
 
1616
                break;
1965
1617
        default:
1966
1618
                ra_type = RA_UNKNOWN;
1967
1619
                remote_arch_str = "UNKNOWN";
2001
1653
int str_checksum(const char *s)
2002
1654
{
2003
1655
        TDB_DATA key = string_tdb_data(s);
2004
 
        return jenkins_hash(&key);
 
1656
        return tdb_jenkins_hash(&key);
2005
1657
}
2006
1658
 
2007
1659
/*****************************************************************
2145
1797
{
2146
1798
        static char *ret;
2147
1799
        if (ret == NULL) {
2148
 
                /* This is cached forever so
2149
 
                 * use talloc_autofree_context() ctx. */
2150
 
                ret = get_myname(talloc_autofree_context());
 
1800
                ret = get_myname(NULL);
2151
1801
        }
2152
1802
        return ret;
2153
1803
}
2294
1944
{
2295
1945
        char *p;
2296
1946
        ptrdiff_t len;
2297
 
 
 
1947
 
2298
1948
        p = strrchr_m(dir, '/'); /* Find final '/', if any */
2299
1949
 
2300
1950
        if (p == NULL) {
2370
2020
 
2371
2021
bool mask_match(const char *string, const char *pattern, bool is_case_sensitive)
2372
2022
{
2373
 
        if (strcmp(string,"..") == 0)
 
2023
        if (ISDOTDOT(string))
2374
2024
                string = ".";
2375
 
        if (strcmp(pattern,".") == 0)
 
2025
        if (ISDOT(pattern))
2376
2026
                return False;
2377
 
        
 
2027
 
2378
2028
        return ms_fnmatch(pattern, string, Protocol <= PROTOCOL_LANMAN2, is_case_sensitive) == 0;
2379
2029
}
2380
2030
 
2386
2036
 
2387
2037
bool mask_match_search(const char *string, const char *pattern, bool is_case_sensitive)
2388
2038
{
2389
 
        if (strcmp(string,"..") == 0)
 
2039
        if (ISDOTDOT(string))
2390
2040
                string = ".";
2391
 
        if (strcmp(pattern,".") == 0)
 
2041
        if (ISDOT(pattern))
2392
2042
                return False;
2393
 
        
 
2043
 
2394
2044
        return ms_fnmatch(pattern, string, True, is_case_sensitive) == 0;
2395
2045
}
2396
2046
 
2659
2309
        return my_vnn;
2660
2310
}
2661
2311
 
 
2312
static uint64_t my_unique_id = 0;
 
2313
 
 
2314
void set_my_unique_id(uint64_t unique_id)
 
2315
{
 
2316
        my_unique_id = unique_id;
 
2317
}
 
2318
 
2662
2319
struct server_id pid_to_procid(pid_t pid)
2663
2320
{
2664
2321
        struct server_id result;
2665
2322
        result.pid = pid;
2666
 
#ifdef CLUSTER_SUPPORT
 
2323
        result.unique_id = my_unique_id;
2667
2324
        result.vnn = my_vnn;
2668
 
#endif
2669
2325
        return result;
2670
2326
}
2671
2327
 
2674
2330
        return pid_to_procid(sys_getpid());
2675
2331
}
2676
2332
 
2677
 
struct server_id server_id_self(void)
2678
 
{
2679
 
        return procid_self();
2680
 
}
2681
 
 
2682
2333
bool procid_equal(const struct server_id *p1, const struct server_id *p2)
2683
2334
{
2684
2335
        if (p1->pid != p2->pid)
2685
2336
                return False;
2686
 
#ifdef CLUSTER_SUPPORT
2687
2337
        if (p1->vnn != p2->vnn)
2688
2338
                return False;
2689
 
#endif
2690
2339
        return True;
2691
2340
}
2692
2341
 
2700
2349
{
2701
2350
        if (pid->pid != sys_getpid())
2702
2351
                return False;
2703
 
#ifdef CLUSTER_SUPPORT
2704
2352
        if (pid->vnn != my_vnn)
2705
2353
                return False;
2706
 
#endif
2707
2354
        return True;
2708
2355
}
2709
2356
 
2711
2358
{
2712
2359
        struct server_id result;
2713
2360
        int pid;
2714
 
#ifdef CLUSTER_SUPPORT
2715
2361
        unsigned int vnn;
2716
2362
        if (sscanf(pid_string, "%u:%d", &vnn, &pid) == 2) {
2717
2363
                result.vnn = vnn;
2725
2371
                result.vnn = NONCLUSTER_VNN;
2726
2372
                result.pid = -1;
2727
2373
        }
2728
 
#else
2729
 
        if (sscanf(pid_string, "%d", &pid) != 1) {
2730
 
                result.pid = -1;
2731
 
        } else {
2732
 
                result.pid = pid;
2733
 
        }
2734
 
#endif
2735
2374
        /* Assigning to result.pid may have overflowed
2736
2375
           Map negative pid to -1: i.e. error */
2737
2376
        if (result.pid < 0) {
2738
2377
                result.pid = -1;
2739
2378
        }
 
2379
        result.unique_id = 0;
2740
2380
        return result;
2741
2381
}
2742
2382
 
2743
2383
char *procid_str(TALLOC_CTX *mem_ctx, const struct server_id *pid)
2744
2384
{
2745
 
#ifdef CLUSTER_SUPPORT
2746
2385
        if (pid->vnn == NONCLUSTER_VNN) {
2747
2386
                return talloc_asprintf(mem_ctx,
2748
2387
                                "%d",
2754
2393
                                        (unsigned)pid->vnn,
2755
2394
                                        (int)pid->pid);
2756
2395
        }
2757
 
#else
2758
 
        return talloc_asprintf(mem_ctx,
2759
 
                        "%d",
2760
 
                        (int)pid->pid);
2761
 
#endif
2762
2396
}
2763
2397
 
2764
2398
char *procid_str_static(const struct server_id *pid)
2773
2407
 
2774
2408
bool procid_is_local(const struct server_id *pid)
2775
2409
{
2776
 
#ifdef CLUSTER_SUPPORT
2777
2410
        return pid->vnn == my_vnn;
2778
 
#else
2779
 
        return True;
2780
 
#endif
2781
 
}
2782
 
 
2783
 
int this_is_smp(void)
2784
 
{
2785
 
#if defined(HAVE_SYSCONF)
2786
 
 
2787
 
#if defined(SYSCONF_SC_NPROC_ONLN)
2788
 
        return (sysconf(_SC_NPROC_ONLN) > 1) ? 1 : 0;
2789
 
#elif defined(SYSCONF_SC_NPROCESSORS_ONLN)
2790
 
        return (sysconf(_SC_NPROCESSORS_ONLN) > 1) ? 1 : 0;
2791
 
#else
2792
 
        return 0;
2793
 
#endif
2794
 
 
2795
 
#else
2796
 
        return 0;
2797
 
#endif
2798
2411
}
2799
2412
 
2800
2413
/****************************************************************
3028
2641
}
3029
2642
#endif
3030
2643
 
3031
 
bool is_valid_policy_hnd(const struct policy_handle *hnd)
3032
 
{
3033
 
        struct policy_handle tmp;
3034
 
        ZERO_STRUCT(tmp);
3035
 
        return (memcmp(&tmp, hnd, sizeof(tmp)) != 0);
3036
 
}
3037
 
 
3038
 
bool policy_hnd_equal(const struct policy_handle *hnd1,
3039
 
                      const struct policy_handle *hnd2)
3040
 
{
3041
 
        if (!hnd1 || !hnd2) {
3042
 
                return false;
3043
 
        }
3044
 
 
3045
 
        return (memcmp(hnd1, hnd2, sizeof(*hnd1)) == 0);
3046
 
}
3047
 
 
3048
2644
/****************************************************************
3049
2645
 strip off leading '\\' from a hostname
3050
2646
****************************************************************/
3075
2671
        }
3076
2672
        return ret;
3077
2673
}
 
2674
 
 
2675
bool any_nt_status_not_ok(NTSTATUS err1, NTSTATUS err2, NTSTATUS *result)
 
2676
{
 
2677
        if (!NT_STATUS_IS_OK(err1)) {
 
2678
                *result = err1;
 
2679
                return true;
 
2680
        }
 
2681
        if (!NT_STATUS_IS_OK(err2)) {
 
2682
                *result = err2;
 
2683
                return true;
 
2684
        }
 
2685
        return false;
 
2686
}
 
2687
 
 
2688
int timeval_to_msec(struct timeval t)
 
2689
{
 
2690
        return t.tv_sec * 1000 + (t.tv_usec+999) / 1000;
 
2691
}
 
2692
 
 
2693
/*******************************************************************
 
2694
 Check a given DOS pathname is valid for a share.
 
2695
********************************************************************/
 
2696
 
 
2697
char *valid_share_pathname(TALLOC_CTX *ctx, const char *dos_pathname)
 
2698
{
 
2699
        char *ptr = NULL;
 
2700
 
 
2701
        if (!dos_pathname) {
 
2702
                return NULL;
 
2703
        }
 
2704
 
 
2705
        ptr = talloc_strdup(ctx, dos_pathname);
 
2706
        if (!ptr) {
 
2707
                return NULL;
 
2708
        }
 
2709
        /* Convert any '\' paths to '/' */
 
2710
        unix_format(ptr);
 
2711
        ptr = unix_clean_name(ctx, ptr);
 
2712
        if (!ptr) {
 
2713
                return NULL;
 
2714
        }
 
2715
 
 
2716
        /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
 
2717
        if (strlen(ptr) > 2 && ptr[1] == ':' && ptr[0] != '/')
 
2718
                ptr += 2;
 
2719
 
 
2720
        /* Only absolute paths allowed. */
 
2721
        if (*ptr != '/')
 
2722
                return NULL;
 
2723
 
 
2724
        return ptr;
 
2725
}
 
2726
 
 
2727
/*******************************************************************
 
2728
 Return True if the filename is one of the special executable types.
 
2729
********************************************************************/
 
2730
 
 
2731
bool is_executable(const char *fname)
 
2732
{
 
2733
        if ((fname = strrchr_m(fname,'.'))) {
 
2734
                if (strequal(fname,".com") ||
 
2735
                    strequal(fname,".dll") ||
 
2736
                    strequal(fname,".exe") ||
 
2737
                    strequal(fname,".sym")) {
 
2738
                        return True;
 
2739
                }
 
2740
        }
 
2741
        return False;
 
2742
}
 
2743
 
 
2744
/****************************************************************************
 
2745
 Open a file with a share mode - old openX method - map into NTCreate.
 
2746
****************************************************************************/
 
2747
 
 
2748
bool map_open_params_to_ntcreate(const char *smb_base_fname,
 
2749
                                 int deny_mode, int open_func,
 
2750
                                 uint32 *paccess_mask,
 
2751
                                 uint32 *pshare_mode,
 
2752
                                 uint32 *pcreate_disposition,
 
2753
                                 uint32 *pcreate_options,
 
2754
                                 uint32_t *pprivate_flags)
 
2755
{
 
2756
        uint32 access_mask;
 
2757
        uint32 share_mode;
 
2758
        uint32 create_disposition;
 
2759
        uint32 create_options = FILE_NON_DIRECTORY_FILE;
 
2760
        uint32_t private_flags = 0;
 
2761
 
 
2762
        DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
 
2763
                  "open_func = 0x%x\n",
 
2764
                  smb_base_fname, (unsigned int)deny_mode,
 
2765
                  (unsigned int)open_func ));
 
2766
 
 
2767
        /* Create the NT compatible access_mask. */
 
2768
        switch (GET_OPENX_MODE(deny_mode)) {
 
2769
                case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
 
2770
                case DOS_OPEN_RDONLY:
 
2771
                        access_mask = FILE_GENERIC_READ;
 
2772
                        break;
 
2773
                case DOS_OPEN_WRONLY:
 
2774
                        access_mask = FILE_GENERIC_WRITE;
 
2775
                        break;
 
2776
                case DOS_OPEN_RDWR:
 
2777
                case DOS_OPEN_FCB:
 
2778
                        access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
 
2779
                        break;
 
2780
                default:
 
2781
                        DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
 
2782
                                  (unsigned int)GET_OPENX_MODE(deny_mode)));
 
2783
                        return False;
 
2784
        }
 
2785
 
 
2786
        /* Create the NT compatible create_disposition. */
 
2787
        switch (open_func) {
 
2788
                case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
 
2789
                        create_disposition = FILE_CREATE;
 
2790
                        break;
 
2791
 
 
2792
                case OPENX_FILE_EXISTS_OPEN:
 
2793
                        create_disposition = FILE_OPEN;
 
2794
                        break;
 
2795
 
 
2796
                case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
 
2797
                        create_disposition = FILE_OPEN_IF;
 
2798
                        break;
 
2799
 
 
2800
                case OPENX_FILE_EXISTS_TRUNCATE:
 
2801
                        create_disposition = FILE_OVERWRITE;
 
2802
                        break;
 
2803
 
 
2804
                case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
 
2805
                        create_disposition = FILE_OVERWRITE_IF;
 
2806
                        break;
 
2807
 
 
2808
                default:
 
2809
                        /* From samba4 - to be confirmed. */
 
2810
                        if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
 
2811
                                create_disposition = FILE_CREATE;
 
2812
                                break;
 
2813
                        }
 
2814
                        DEBUG(10,("map_open_params_to_ntcreate: bad "
 
2815
                                  "open_func 0x%x\n", (unsigned int)open_func));
 
2816
                        return False;
 
2817
        }
 
2818
 
 
2819
        /* Create the NT compatible share modes. */
 
2820
        switch (GET_DENY_MODE(deny_mode)) {
 
2821
                case DENY_ALL:
 
2822
                        share_mode = FILE_SHARE_NONE;
 
2823
                        break;
 
2824
 
 
2825
                case DENY_WRITE:
 
2826
                        share_mode = FILE_SHARE_READ;
 
2827
                        break;
 
2828
 
 
2829
                case DENY_READ:
 
2830
                        share_mode = FILE_SHARE_WRITE;
 
2831
                        break;
 
2832
 
 
2833
                case DENY_NONE:
 
2834
                        share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
 
2835
                        break;
 
2836
 
 
2837
                case DENY_DOS:
 
2838
                        private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
 
2839
                        if (is_executable(smb_base_fname)) {
 
2840
                                share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
 
2841
                        } else {
 
2842
                                if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
 
2843
                                        share_mode = FILE_SHARE_READ;
 
2844
                                } else {
 
2845
                                        share_mode = FILE_SHARE_NONE;
 
2846
                                }
 
2847
                        }
 
2848
                        break;
 
2849
 
 
2850
                case DENY_FCB:
 
2851
                        private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
 
2852
                        share_mode = FILE_SHARE_NONE;
 
2853
                        break;
 
2854
 
 
2855
                default:
 
2856
                        DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
 
2857
                                (unsigned int)GET_DENY_MODE(deny_mode) ));
 
2858
                        return False;
 
2859
        }
 
2860
 
 
2861
        DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
 
2862
                  "share_mode = 0x%x, create_disposition = 0x%x, "
 
2863
                  "create_options = 0x%x private_flags = 0x%x\n",
 
2864
                  smb_base_fname,
 
2865
                  (unsigned int)access_mask,
 
2866
                  (unsigned int)share_mode,
 
2867
                  (unsigned int)create_disposition,
 
2868
                  (unsigned int)create_options,
 
2869
                  (unsigned int)private_flags));
 
2870
 
 
2871
        if (paccess_mask) {
 
2872
                *paccess_mask = access_mask;
 
2873
        }
 
2874
        if (pshare_mode) {
 
2875
                *pshare_mode = share_mode;
 
2876
        }
 
2877
        if (pcreate_disposition) {
 
2878
                *pcreate_disposition = create_disposition;
 
2879
        }
 
2880
        if (pcreate_options) {
 
2881
                *pcreate_options = create_options;
 
2882
        }
 
2883
        if (pprivate_flags) {
 
2884
                *pprivate_flags = private_flags;
 
2885
        }
 
2886
 
 
2887
        return True;
 
2888
 
 
2889
}