upgradeprovision: Change test to always use 2008 R2 schema
[kai/samba-autobuild/.git] / source3 / lib / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 2001-2007
6    Copyright (C) Simo Sorce 2001
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8    Copyright (C) James Peach 2006
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "system/passwd.h"
26 #include "system/filesys.h"
27 #include "lib/util/server_id.h"
28 #include "util_tdb.h"
29 #include "ctdbd_conn.h"
30 #include "../lib/util/util_pw.h"
31 #include "messages.h"
32 #include "messages_dgm.h"
33 #include "libcli/security/security.h"
34 #include "serverid.h"
35 #include "lib/util/sys_rw.h"
36 #include "lib/util/sys_rw_data.h"
37 #include "lib/util/util_process.h"
38 #include "lib/dbwrap/dbwrap_ctdb.h"
39
40 #ifdef HAVE_SYS_PRCTL_H
41 #include <sys/prctl.h>
42 #endif
43
44 /* Max allowable allococation - 256mb - 0x10000000 */
45 #define MAX_ALLOC_SIZE (1024*1024*256)
46
47 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
48 #ifdef WITH_NISPLUS_HOME
49 #ifdef BROKEN_NISPLUS_INCLUDE_FILES
50 /*
51  * The following lines are needed due to buggy include files
52  * in Solaris 2.6 which define GROUP in both /usr/include/sys/acl.h and
53  * also in /usr/include/rpcsvc/nis.h. The definitions conflict. JRA.
54  * Also GROUP_OBJ is defined as 0x4 in /usr/include/sys/acl.h and as
55  * an enum in /usr/include/rpcsvc/nis.h.
56  */
57
58 #if defined(GROUP)
59 #undef GROUP
60 #endif
61
62 #if defined(GROUP_OBJ)
63 #undef GROUP_OBJ
64 #endif
65
66 #endif /* BROKEN_NISPLUS_INCLUDE_FILES */
67
68 #include <rpcsvc/nis.h>
69
70 #endif /* WITH_NISPLUS_HOME */
71 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
72
73 static enum protocol_types Protocol = PROTOCOL_COREPLUS;
74
75 enum protocol_types get_Protocol(void)
76 {
77         return Protocol;
78 }
79
80 void set_Protocol(enum protocol_types  p)
81 {
82         Protocol = p;
83 }
84
85 static enum remote_arch_types ra_type = RA_UNKNOWN;
86
87 void gfree_all( void )
88 {
89         gfree_names();
90         gfree_loadparm();
91         gfree_charcnv();
92         gfree_interfaces();
93         gfree_debugsyms();
94 }
95
96 /*******************************************************************
97  Check if a file exists - call vfs_file_exist for samba files.
98 ********************************************************************/
99
100 bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf,
101                      bool fake_dir_create_times)
102 {
103         SMB_STRUCT_STAT st;
104         if (!sbuf)
105                 sbuf = &st;
106
107         if (sys_stat(fname, sbuf, fake_dir_create_times) != 0)
108                 return(False);
109
110         return((S_ISREG(sbuf->st_ex_mode)) || (S_ISFIFO(sbuf->st_ex_mode)));
111 }
112
113 /*******************************************************************
114  Check if a unix domain socket exists - call vfs_file_exist for samba files.
115 ********************************************************************/
116
117 bool socket_exist(const char *fname)
118 {
119         SMB_STRUCT_STAT st;
120         if (sys_stat(fname, &st, false) != 0)
121                 return(False);
122
123         return S_ISSOCK(st.st_ex_mode);
124 }
125
126 /*******************************************************************
127  Returns the size in bytes of the named given the stat struct.
128 ********************************************************************/
129
130 uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
131 {
132         return sbuf->st_ex_size;
133 }
134
135 /****************************************************************************
136  Check two stats have identical dev and ino fields.
137 ****************************************************************************/
138
139 bool check_same_dev_ino(const SMB_STRUCT_STAT *sbuf1,
140                         const SMB_STRUCT_STAT *sbuf2)
141 {
142         if (sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
143                         sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
144                 return false;
145         }
146         return true;
147 }
148
149 /****************************************************************************
150  Check if a stat struct is identical for use.
151 ****************************************************************************/
152
153 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
154                         const SMB_STRUCT_STAT *sbuf2)
155 {
156         if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
157                         sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
158                         !check_same_dev_ino(sbuf1, sbuf2)) {
159                 return false;
160         }
161         return true;
162 }
163
164 /*******************************************************************
165  Show a smb message structure.
166 ********************************************************************/
167
168 void show_msg(const char *buf)
169 {
170         int i;
171         int bcc=0;
172
173         if (!DEBUGLVL(5))
174                 return;
175
176         DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
177                         smb_len(buf),
178                         (int)CVAL(buf,smb_com),
179                         (int)CVAL(buf,smb_rcls),
180                         (int)CVAL(buf,smb_reh),
181                         (int)SVAL(buf,smb_err),
182                         (int)CVAL(buf,smb_flg),
183                         (int)SVAL(buf,smb_flg2)));
184         DEBUGADD(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\n",
185                         (int)SVAL(buf,smb_tid),
186                         (int)SVAL(buf,smb_pid),
187                         (int)SVAL(buf,smb_uid),
188                         (int)SVAL(buf,smb_mid)));
189         DEBUGADD(5,("smt_wct=%d\n",(int)CVAL(buf,smb_wct)));
190
191         for (i=0;i<(int)CVAL(buf,smb_wct);i++)
192                 DEBUGADD(5,("smb_vwv[%2d]=%5d (0x%X)\n",i,
193                         SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
194
195         bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
196
197         DEBUGADD(5,("smb_bcc=%d\n",bcc));
198
199         if (DEBUGLEVEL < 10)
200                 return;
201
202         if (DEBUGLEVEL < 50)
203                 bcc = MIN(bcc, 512);
204
205         dump_data(10, (const uint8_t *)smb_buf_const(buf), bcc);
206 }
207
208 /*******************************************************************
209  Setup only the byte count for a smb message.
210 ********************************************************************/
211
212 int set_message_bcc(char *buf,int num_bytes)
213 {
214         int num_words = CVAL(buf,smb_wct);
215         SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
216         _smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
217         return (smb_size + num_words*2 + num_bytes);
218 }
219
220 /*******************************************************************
221  Add a data blob to the end of a smb_buf, adjusting bcc and smb_len.
222  Return the bytes added
223 ********************************************************************/
224
225 ssize_t message_push_blob(uint8_t **outbuf, DATA_BLOB blob)
226 {
227         size_t newlen = smb_len(*outbuf) + 4 + blob.length;
228         uint8_t *tmp;
229
230         if (!(tmp = talloc_realloc(NULL, *outbuf, uint8_t, newlen))) {
231                 DEBUG(0, ("talloc failed\n"));
232                 return -1;
233         }
234         *outbuf = tmp;
235
236         memcpy(tmp + smb_len(tmp) + 4, blob.data, blob.length);
237         set_message_bcc((char *)tmp, smb_buflen(tmp) + blob.length);
238         return blob.length;
239 }
240
241 /*******************************************************************
242  Reduce a file name, removing .. elements.
243 ********************************************************************/
244
245 static char *dos_clean_name(TALLOC_CTX *ctx, const char *s)
246 {
247         char *p = NULL;
248         char *str = NULL;
249
250         DEBUG(3,("dos_clean_name [%s]\n",s));
251
252         /* remove any double slashes */
253         str = talloc_all_string_sub(ctx, s, "\\\\", "\\");
254         if (!str) {
255                 return NULL;
256         }
257
258         /* Remove leading .\\ characters */
259         if(strncmp(str, ".\\", 2) == 0) {
260                 trim_string(str, ".\\", NULL);
261                 if(*str == 0) {
262                         str = talloc_strdup(ctx, ".\\");
263                         if (!str) {
264                                 return NULL;
265                         }
266                 }
267         }
268
269         while ((p = strstr_m(str,"\\..\\")) != NULL) {
270                 char *s1;
271
272                 *p = 0;
273                 s1 = p+3;
274
275                 if ((p=strrchr_m(str,'\\')) != NULL) {
276                         *p = 0;
277                 } else {
278                         *str = 0;
279                 }
280                 str = talloc_asprintf(ctx,
281                                 "%s%s",
282                                 str,
283                                 s1);
284                 if (!str) {
285                         return NULL;
286                 }
287         }
288
289         trim_string(str,NULL,"\\..");
290         return talloc_all_string_sub(ctx, str, "\\.\\", "\\");
291 }
292
293 /*******************************************************************
294  Reduce a file name, removing .. elements.
295 ********************************************************************/
296
297 char *unix_clean_name(TALLOC_CTX *ctx, const char *s)
298 {
299         char *p = NULL;
300         char *str = NULL;
301
302         DEBUG(3,("unix_clean_name [%s]\n",s));
303
304         /* remove any double slashes */
305         str = talloc_all_string_sub(ctx, s, "//","/");
306         if (!str) {
307                 return NULL;
308         }
309
310         /* Remove leading ./ characters */
311         if(strncmp(str, "./", 2) == 0) {
312                 trim_string(str, "./", NULL);
313                 if(*str == 0) {
314                         str = talloc_strdup(ctx, "./");
315                         if (!str) {
316                                 return NULL;
317                         }
318                 }
319         }
320
321         while ((p = strstr_m(str,"/../")) != NULL) {
322                 char *s1;
323
324                 *p = 0;
325                 s1 = p+3;
326
327                 if ((p=strrchr_m(str,'/')) != NULL) {
328                         *p = 0;
329                 } else {
330                         *str = 0;
331                 }
332                 str = talloc_asprintf(ctx,
333                                 "%s%s",
334                                 str,
335                                 s1);
336                 if (!str) {
337                         return NULL;
338                 }
339         }
340
341         trim_string(str,NULL,"/..");
342         return talloc_all_string_sub(ctx, str, "/./", "/");
343 }
344
345 char *clean_name(TALLOC_CTX *ctx, const char *s)
346 {
347         char *str = dos_clean_name(ctx, s);
348         if (!str) {
349                 return NULL;
350         }
351         return unix_clean_name(ctx, str);
352 }
353
354 /*******************************************************************
355  Write data into an fd at a given offset. Ignore seek errors.
356 ********************************************************************/
357
358 ssize_t write_data_at_offset(int fd, const char *buffer, size_t N, off_t pos)
359 {
360         size_t total=0;
361         ssize_t ret;
362
363         if (pos == (off_t)-1) {
364                 return write_data(fd, buffer, N);
365         }
366 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
367         while (total < N) {
368                 ret = sys_pwrite(fd,buffer + total,N - total, pos);
369                 if (ret == -1 && errno == ESPIPE) {
370                         return write_data(fd, buffer + total,N - total);
371                 }
372                 if (ret == -1) {
373                         DEBUG(0,("write_data_at_offset: write failure. Error = %s\n", strerror(errno) ));
374                         return -1;
375                 }
376                 if (ret == 0) {
377                         return total;
378                 }
379                 total += ret;
380                 pos += ret;
381         }
382         return (ssize_t)total;
383 #else
384         /* Use lseek and write_data. */
385         if (lseek(fd, pos, SEEK_SET) == -1) {
386                 if (errno != ESPIPE) {
387                         return -1;
388                 }
389         }
390         return write_data(fd, buffer, N);
391 #endif
392 }
393
394 static int reinit_after_fork_pipe[2] = { -1, -1 };
395
396 NTSTATUS init_before_fork(void)
397 {
398         int ret;
399
400         ret = pipe(reinit_after_fork_pipe);
401         if (ret == -1) {
402                 NTSTATUS status;
403
404                 status = map_nt_error_from_unix_common(errno);
405
406                 DEBUG(0, ("Error creating child_pipe: %s\n",
407                           nt_errstr(status)));
408
409                 return status;
410         }
411
412         return NT_STATUS_OK;
413 }
414
415 /**
416  * Detect died parent by detecting EOF on the pipe
417  */
418 static void reinit_after_fork_pipe_handler(struct tevent_context *ev,
419                                            struct tevent_fd *fde,
420                                            uint16_t flags,
421                                            void *private_data)
422 {
423         char c;
424
425         if (sys_read(reinit_after_fork_pipe[0], &c, 1) != 1) {
426                 /*
427                  * we have reached EOF on stdin, which means the
428                  * parent has exited. Shutdown the server
429                  */
430                 (void)kill(getpid(), SIGTERM);
431         }
432 }
433
434
435 NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
436                            struct tevent_context *ev_ctx,
437                            bool parent_longlived,
438                            const char *comment)
439 {
440         NTSTATUS status = NT_STATUS_OK;
441         int ret;
442
443         if (reinit_after_fork_pipe[1] != -1) {
444                 close(reinit_after_fork_pipe[1]);
445                 reinit_after_fork_pipe[1] = -1;
446         }
447
448         /* tdb needs special fork handling */
449         if (tdb_reopen_all(parent_longlived ? 1 : 0) != 0) {
450                 DEBUG(0,("tdb_reopen_all failed.\n"));
451                 status = NT_STATUS_OPEN_FAILED;
452                 goto done;
453         }
454
455         if (ev_ctx != NULL) {
456                 tevent_set_trace_callback(ev_ctx, NULL, NULL);
457                 if (tevent_re_initialise(ev_ctx) != 0) {
458                         smb_panic(__location__ ": Failed to re-initialise event context");
459                 }
460         }
461
462         if (reinit_after_fork_pipe[0] != -1) {
463                 struct tevent_fd *fde;
464
465                 fde = tevent_add_fd(ev_ctx, ev_ctx /* TALLOC_CTX */,
466                                     reinit_after_fork_pipe[0], TEVENT_FD_READ,
467                                     reinit_after_fork_pipe_handler, NULL);
468                 if (fde == NULL) {
469                         smb_panic(__location__ ": Failed to add reinit_after_fork pipe event");
470                 }
471         }
472
473         if (msg_ctx) {
474                 /*
475                  * For clustering, we need to re-init our ctdbd connection after the
476                  * fork
477                  */
478                 status = messaging_reinit(msg_ctx);
479                 if (!NT_STATUS_IS_OK(status)) {
480                         DEBUG(0,("messaging_reinit() failed: %s\n",
481                                  nt_errstr(status)));
482                 }
483
484                 if (lp_clustering()) {
485                         ret = ctdb_async_ctx_reinit(
486                                 NULL, messaging_tevent_context(msg_ctx));
487                         if (ret != 0) {
488                                 DBG_ERR("db_ctdb_async_ctx_reinit failed: %s\n",
489                                         strerror(errno));
490                                 return map_nt_error_from_unix(ret);
491                         }
492                 }
493         }
494
495         if (comment) {
496                 prctl_set_comment(comment);
497         }
498
499  done:
500         return status;
501 }
502
503 /****************************************************************************
504  (Hopefully) efficient array append.
505 ****************************************************************************/
506
507 void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
508                         void *element, void *_array, uint32_t *num_elements,
509                         ssize_t *array_size)
510 {
511         void **array = (void **)_array;
512
513         if (*array_size < 0) {
514                 return;
515         }
516
517         if (*array == NULL) {
518                 if (*array_size == 0) {
519                         *array_size = 128;
520                 }
521
522                 if (*array_size >= MAX_ALLOC_SIZE/element_size) {
523                         goto error;
524                 }
525
526                 *array = TALLOC(mem_ctx, element_size * (*array_size));
527                 if (*array == NULL) {
528                         goto error;
529                 }
530         }
531
532         if (*num_elements == *array_size) {
533                 *array_size *= 2;
534
535                 if (*array_size >= MAX_ALLOC_SIZE/element_size) {
536                         goto error;
537                 }
538
539                 *array = TALLOC_REALLOC(mem_ctx, *array,
540                                         element_size * (*array_size));
541
542                 if (*array == NULL) {
543                         goto error;
544                 }
545         }
546
547         memcpy((char *)(*array) + element_size*(*num_elements),
548                element, element_size);
549         *num_elements += 1;
550
551         return;
552
553  error:
554         *num_elements = 0;
555         *array_size = -1;
556 }
557
558 /****************************************************************************
559  Get my own domain name, or "" if we have none.
560 ****************************************************************************/
561
562 char *get_mydnsdomname(TALLOC_CTX *ctx)
563 {
564         const char *domname;
565         char *p;
566
567         domname = get_mydnsfullname();
568         if (!domname) {
569                 return NULL;
570         }
571
572         p = strchr_m(domname, '.');
573         if (p) {
574                 p++;
575                 return talloc_strdup(ctx, p);
576         } else {
577                 return talloc_strdup(ctx, "");
578         }
579 }
580
581 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
582 /******************************************************************
583  Remove any mount options such as -rsize=2048,wsize=2048 etc.
584  Based on a fix from <Thomas.Hepper@icem.de>.
585  Returns a malloc'ed string.
586 *******************************************************************/
587
588 static char *strip_mount_options(TALLOC_CTX *ctx, const char *str)
589 {
590         if (*str == '-') {
591                 const char *p = str;
592                 while(*p && !isspace(*p))
593                         p++;
594                 while(*p && isspace(*p))
595                         p++;
596                 if(*p) {
597                         return talloc_strdup(ctx, p);
598                 }
599         }
600         return NULL;
601 }
602
603 /*******************************************************************
604  Patch from jkf@soton.ac.uk
605  Split Luke's automount_server into YP lookup and string splitter
606  so can easily implement automount_path().
607  Returns a malloc'ed string.
608 *******************************************************************/
609
610 #ifdef WITH_NISPLUS_HOME
611 char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
612 {
613         char *value = NULL;
614
615         char *nis_map = (char *)lp_homedir_map();
616
617         char buffer[NIS_MAXATTRVAL + 1];
618         nis_result *result;
619         nis_object *object;
620         entry_obj  *entry;
621
622         snprintf(buffer, sizeof(buffer), "[key=%s],%s", user_name, nis_map);
623         DEBUG(5, ("NIS+ querystring: %s\n", buffer));
624
625         if (result = nis_list(buffer, FOLLOW_PATH|EXPAND_NAME|HARD_LOOKUP, NULL, NULL)) {
626                 if (result->status != NIS_SUCCESS) {
627                         DEBUG(3, ("NIS+ query failed: %s\n", nis_sperrno(result->status)));
628                 } else {
629                         object = result->objects.objects_val;
630                         if (object->zo_data.zo_type == ENTRY_OBJ) {
631                                 entry = &object->zo_data.objdata_u.en_data;
632                                 DEBUG(5, ("NIS+ entry type: %s\n", entry->en_type));
633                                 DEBUG(3, ("NIS+ result: %s\n", entry->en_cols.en_cols_val[1].ec_value.ec_value_val));
634
635                                 value = talloc_strdup(ctx,
636                                                 entry->en_cols.en_cols_val[1].ec_value.ec_value_val);
637                                 if (!value) {
638                                         nis_freeresult(result);
639                                         return NULL;
640                                 }
641                                 value = talloc_string_sub(ctx,
642                                                 value,
643                                                 "&",
644                                                 user_name);
645                         }
646                 }
647         }
648         nis_freeresult(result);
649
650         if (value) {
651                 value = strip_mount_options(ctx, value);
652                 DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n",
653                                         user_name, value));
654         }
655         return value;
656 }
657 #else /* WITH_NISPLUS_HOME */
658
659 char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
660 {
661         char *value = NULL;
662
663         int nis_error;        /* returned by yp all functions */
664         char *nis_result;     /* yp_match inits this */
665         int nis_result_len;  /* and set this */
666         char *nis_domain;     /* yp_get_default_domain inits this */
667         char *nis_map = lp_homedir_map(talloc_tos());
668
669         if ((nis_error = yp_get_default_domain(&nis_domain)) != 0) {
670                 DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
671                 return NULL;
672         }
673
674         DEBUG(5, ("NIS Domain: %s\n", nis_domain));
675
676         if ((nis_error = yp_match(nis_domain, nis_map, user_name,
677                                         strlen(user_name), &nis_result,
678                                         &nis_result_len)) == 0) {
679                 if (nis_result_len > 0 && nis_result[nis_result_len] == '\n') {
680                         nis_result[nis_result_len] = '\0';
681                 }
682                 value = talloc_strdup(ctx, nis_result);
683                 if (!value) {
684                         return NULL;
685                 }
686                 value = strip_mount_options(ctx, value);
687         } else if(nis_error == YPERR_KEY) {
688                 DEBUG(3, ("YP Key not found:  while looking up \"%s\" in map \"%s\"\n", 
689                                 user_name, nis_map));
690                 DEBUG(3, ("using defaults for server and home directory\n"));
691         } else {
692                 DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n", 
693                                 yperr_string(nis_error), user_name, nis_map));
694         }
695
696         if (value) {
697                 DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name, value));
698         }
699         return value;
700 }
701 #endif /* WITH_NISPLUS_HOME */
702 #endif
703
704 bool process_exists(const struct server_id pid)
705 {
706         return serverid_exists(&pid);
707 }
708
709 /*******************************************************************
710  Convert a uid into a user name.
711 ********************************************************************/
712
713 const char *uidtoname(uid_t uid)
714 {
715         TALLOC_CTX *ctx = talloc_tos();
716         char *name = NULL;
717         struct passwd *pass = NULL;
718
719         pass = getpwuid_alloc(ctx,uid);
720         if (pass) {
721                 name = talloc_strdup(ctx,pass->pw_name);
722                 TALLOC_FREE(pass);
723         } else {
724                 name = talloc_asprintf(ctx,
725                                 "%ld",
726                                 (long int)uid);
727         }
728         return name;
729 }
730
731 /*******************************************************************
732  Convert a gid into a group name.
733 ********************************************************************/
734
735 char *gidtoname(gid_t gid)
736 {
737         struct group *grp;
738
739         grp = getgrgid(gid);
740         if (grp) {
741                 return talloc_strdup(talloc_tos(), grp->gr_name);
742         }
743         else {
744                 return talloc_asprintf(talloc_tos(),
745                                         "%d",
746                                         (int)gid);
747         }
748 }
749
750 /*******************************************************************
751  Convert a user name into a uid.
752 ********************************************************************/
753
754 uid_t nametouid(const char *name)
755 {
756         struct passwd *pass;
757         char *p;
758         uid_t u;
759
760         pass = Get_Pwnam_alloc(talloc_tos(), name);
761         if (pass) {
762                 u = pass->pw_uid;
763                 TALLOC_FREE(pass);
764                 return u;
765         }
766
767         u = (uid_t)strtol(name, &p, 0);
768         if ((p != name) && (*p == '\0'))
769                 return u;
770
771         return (uid_t)-1;
772 }
773
774 /*******************************************************************
775  Convert a name to a gid_t if possible. Return -1 if not a group. 
776 ********************************************************************/
777
778 gid_t nametogid(const char *name)
779 {
780         struct group *grp;
781         char *p;
782         gid_t g;
783
784         g = (gid_t)strtol(name, &p, 0);
785         if ((p != name) && (*p == '\0'))
786                 return g;
787
788         grp = getgrnam(name);
789         if (grp)
790                 return(grp->gr_gid);
791         return (gid_t)-1;
792 }
793
794 /*******************************************************************
795  Something really nasty happened - panic !
796 ********************************************************************/
797
798 void smb_panic_s3(const char *why)
799 {
800         char *cmd;
801         int result;
802
803         DEBUG(0,("PANIC (pid %llu): %s\n",
804                     (unsigned long long)getpid(), why));
805         log_stack_trace();
806
807 #if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
808         /*
809          * Make sure all children can attach a debugger.
810          */
811         prctl(PR_SET_PTRACER, getpid(), 0, 0, 0);
812 #endif
813
814         cmd = lp_panic_action(talloc_tos());
815         if (cmd && *cmd) {
816                 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
817                 result = system(cmd);
818
819                 if (result == -1)
820                         DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
821                                           strerror(errno)));
822                 else
823                         DEBUG(0, ("smb_panic(): action returned status %d\n",
824                                           WEXITSTATUS(result)));
825         }
826
827         dump_core();
828 }
829
830 /*******************************************************************
831  Print a backtrace of the stack to the debug log. This function
832  DELIBERATELY LEAKS MEMORY. The expectation is that you should
833  exit shortly after calling it.
834 ********************************************************************/
835
836 #ifdef HAVE_LIBUNWIND_H
837 #include <libunwind.h>
838 #endif
839
840 #ifdef HAVE_EXECINFO_H
841 #include <execinfo.h>
842 #endif
843
844 #ifdef HAVE_LIBEXC_H
845 #include <libexc.h>
846 #endif
847
848 void log_stack_trace(void)
849 {
850 #ifdef HAVE_LIBUNWIND
851         /* Try to use libunwind before any other technique since on ia64
852          * libunwind correctly walks the stack in more circumstances than
853          * backtrace.
854          */ 
855         unw_cursor_t cursor;
856         unw_context_t uc;
857         unsigned i = 0;
858
859         char procname[256];
860         unw_word_t ip, sp, off;
861
862         procname[sizeof(procname) - 1] = '\0';
863
864         if (unw_getcontext(&uc) != 0) {
865                 goto libunwind_failed;
866         }
867
868         if (unw_init_local(&cursor, &uc) != 0) {
869                 goto libunwind_failed;
870         }
871
872         DEBUG(0, ("BACKTRACE:\n"));
873
874         do {
875             ip = sp = 0;
876             unw_get_reg(&cursor, UNW_REG_IP, &ip);
877             unw_get_reg(&cursor, UNW_REG_SP, &sp);
878
879             switch (unw_get_proc_name(&cursor,
880                         procname, sizeof(procname) - 1, &off) ) {
881             case 0:
882                     /* Name found. */
883             case -UNW_ENOMEM:
884                     /* Name truncated. */
885                     DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
886                             i, procname, (long long)off,
887                             (long long)ip, (long long) sp));
888                     break;
889             default:
890             /* case -UNW_ENOINFO: */
891             /* case -UNW_EUNSPEC: */
892                     /* No symbol name found. */
893                     DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
894                             i, "<unknown symbol>",
895                             (long long)ip, (long long) sp));
896             }
897             ++i;
898         } while (unw_step(&cursor) > 0);
899
900         return;
901
902 libunwind_failed:
903         DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
904
905 #elif HAVE_BACKTRACE_SYMBOLS
906         void *backtrace_stack[BACKTRACE_STACK_SIZE];
907         size_t backtrace_size;
908         char **backtrace_strings;
909
910         /* get the backtrace (stack frames) */
911         backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
912         backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
913
914         DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 
915                   (unsigned long)backtrace_size));
916
917         if (backtrace_strings) {
918                 int i;
919
920                 for (i = 0; i < backtrace_size; i++)
921                         DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
922
923                 /* Leak the backtrace_strings, rather than risk what free() might do */
924         }
925
926 #elif HAVE_LIBEXC
927
928         /* The IRIX libexc library provides an API for unwinding the stack. See
929          * libexc(3) for details. Apparantly trace_back_stack leaks memory, but
930          * since we are about to abort anyway, it hardly matters.
931          */
932
933 #define NAMESIZE 32 /* Arbitrary */
934
935         __uint64_t      addrs[BACKTRACE_STACK_SIZE];
936         char *          names[BACKTRACE_STACK_SIZE];
937         char            namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
938
939         int             i;
940         int             levels;
941
942         ZERO_ARRAY(addrs);
943         ZERO_ARRAY(names);
944         ZERO_ARRAY(namebuf);
945
946         /* We need to be root so we can open our /proc entry to walk
947          * our stack. It also helps when we want to dump core.
948          */
949         become_root();
950
951         for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
952                 names[i] = namebuf + (i * NAMESIZE);
953         }
954
955         levels = trace_back_stack(0, addrs, names,
956                         BACKTRACE_STACK_SIZE, NAMESIZE - 1);
957
958         DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
959         for (i = 0; i < levels; i++) {
960                 DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
961         }
962 #undef NAMESIZE
963
964 #else
965         DEBUG(0, ("unable to produce a stack trace on this platform\n"));
966 #endif
967 }
968
969 /*******************************************************************
970   A readdir wrapper which just returns the file name.
971  ********************************************************************/
972
973 const char *readdirname(DIR *p)
974 {
975         struct dirent *ptr;
976         char *dname;
977
978         if (!p)
979                 return(NULL);
980
981         ptr = (struct dirent *)readdir(p);
982         if (!ptr)
983                 return(NULL);
984
985         dname = ptr->d_name;
986
987 #ifdef NEXT2
988         if (telldir(p) < 0)
989                 return(NULL);
990 #endif
991
992 #ifdef HAVE_BROKEN_READDIR_NAME
993         /* using /usr/ucb/cc is BAD */
994         dname = dname - 2;
995 #endif
996
997         return talloc_strdup(talloc_tos(), dname);
998 }
999
1000 /*******************************************************************
1001  Utility function used to decide if the last component 
1002  of a path matches a (possibly wildcarded) entry in a namelist.
1003 ********************************************************************/
1004
1005 bool is_in_path(const char *name, name_compare_entry *namelist, bool case_sensitive)
1006 {
1007         const char *last_component;
1008
1009         /* if we have no list it's obviously not in the path */
1010         if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL))) {
1011                 return False;
1012         }
1013
1014         DEBUG(8, ("is_in_path: %s\n", name));
1015
1016         /* Get the last component of the unix name. */
1017         last_component = strrchr_m(name, '/');
1018         if (!last_component) {
1019                 last_component = name;
1020         } else {
1021                 last_component++; /* Go past '/' */
1022         }
1023
1024         for(; namelist->name != NULL; namelist++) {
1025                 if(namelist->is_wild) {
1026                         if (mask_match(last_component, namelist->name, case_sensitive)) {
1027                                 DEBUG(8,("is_in_path: mask match succeeded\n"));
1028                                 return True;
1029                         }
1030                 } else {
1031                         if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
1032                                                 (!case_sensitive && (strcasecmp_m(last_component, namelist->name) == 0))) {
1033                                 DEBUG(8,("is_in_path: match succeeded\n"));
1034                                 return True;
1035                         }
1036                 }
1037         }
1038         DEBUG(8,("is_in_path: match not found\n"));
1039         return False;
1040 }
1041
1042 /*******************************************************************
1043  Strip a '/' separated list into an array of 
1044  name_compare_enties structures suitable for 
1045  passing to is_in_path(). We do this for
1046  speed so we can pre-parse all the names in the list 
1047  and don't do it for each call to is_in_path().
1048  We also check if the entry contains a wildcard to
1049  remove a potentially expensive call to mask_match
1050  if possible.
1051 ********************************************************************/
1052
1053 void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
1054 {
1055         char *name_end;
1056         char *namelist;
1057         char *namelist_end;
1058         char *nameptr;
1059         int num_entries = 0;
1060         int i;
1061
1062         (*ppname_array) = NULL;
1063
1064         if((namelist_in == NULL ) || ((namelist_in != NULL) && (*namelist_in == '\0'))) 
1065                 return;
1066
1067         namelist = talloc_strdup(talloc_tos(), namelist_in);
1068         if (namelist == NULL) {
1069                 DEBUG(0,("set_namearray: talloc fail\n"));
1070                 return;
1071         }
1072         nameptr = namelist;
1073
1074         namelist_end = &namelist[strlen(namelist)];
1075
1076         /* We need to make two passes over the string. The
1077                 first to count the number of elements, the second
1078                 to split it.
1079         */
1080
1081         while(nameptr <= namelist_end) {
1082                 if ( *nameptr == '/' ) {
1083                         /* cope with multiple (useless) /s) */
1084                         nameptr++;
1085                         continue;
1086                 }
1087                 /* anything left? */
1088                 if ( *nameptr == '\0' )
1089                         break;
1090
1091                 /* find the next '/' or consume remaining */
1092                 name_end = strchr_m(nameptr, '/');
1093                 if (name_end == NULL) {
1094                         /* Point nameptr at the terminating '\0' */
1095                         nameptr += strlen(nameptr);
1096                 } else {
1097                         /* next segment please */
1098                         nameptr = name_end + 1;
1099                 }
1100                 num_entries++;
1101         }
1102
1103         if(num_entries == 0) {
1104                 talloc_free(namelist);
1105                 return;
1106         }
1107
1108         if(( (*ppname_array) = SMB_MALLOC_ARRAY(name_compare_entry, num_entries + 1)) == NULL) {
1109                 DEBUG(0,("set_namearray: malloc fail\n"));
1110                 talloc_free(namelist);
1111                 return;
1112         }
1113
1114         /* Now copy out the names */
1115         nameptr = namelist;
1116         i = 0;
1117         while(nameptr <= namelist_end) {
1118                 if ( *nameptr == '/' ) {
1119                         /* cope with multiple (useless) /s) */
1120                         nameptr++;
1121                         continue;
1122                 }
1123                 /* anything left? */
1124                 if ( *nameptr == '\0' )
1125                         break;
1126
1127                 /* find the next '/' or consume remaining */
1128                 name_end = strchr_m(nameptr, '/');
1129                 if (name_end != NULL) {
1130                         *name_end = '\0';
1131                 }
1132
1133                 (*ppname_array)[i].is_wild = ms_has_wild(nameptr);
1134                 if(((*ppname_array)[i].name = SMB_STRDUP(nameptr)) == NULL) {
1135                         DEBUG(0,("set_namearray: malloc fail (1)\n"));
1136                         talloc_free(namelist);
1137                         return;
1138                 }
1139
1140                 if (name_end == NULL) {
1141                         /* Point nameptr at the terminating '\0' */
1142                         nameptr += strlen(nameptr);
1143                 } else {
1144                         /* next segment please */
1145                         nameptr = name_end + 1;
1146                 }
1147                 i++;
1148         }
1149
1150         (*ppname_array)[i].name = NULL;
1151
1152         talloc_free(namelist);
1153         return;
1154 }
1155
1156 #undef DBGC_CLASS
1157 #define DBGC_CLASS DBGC_LOCKING
1158
1159 /****************************************************************************
1160  Simple routine to query existing file locks. Cruft in NFS and 64->32 bit mapping
1161  is dealt with in posix.c
1162  Returns True if we have information regarding this lock region (and returns
1163  F_UNLCK in *ptype if the region is unlocked). False if the call failed.
1164 ****************************************************************************/
1165
1166 bool fcntl_getlock(int fd, int op, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1167 {
1168         struct flock lock;
1169         int ret;
1170
1171         DEBUG(8,("fcntl_getlock fd=%d op=%d offset=%.0f count=%.0f type=%d\n",
1172                     fd,op,(double)*poffset,(double)*pcount,*ptype));
1173
1174         lock.l_type = *ptype;
1175         lock.l_whence = SEEK_SET;
1176         lock.l_start = *poffset;
1177         lock.l_len = *pcount;
1178         lock.l_pid = 0;
1179
1180         ret = sys_fcntl_ptr(fd,op,&lock);
1181
1182         if (ret == -1) {
1183                 int sav = errno;
1184                 DEBUG(3,("fcntl_getlock: lock request failed at offset %.0f count %.0f type %d (%s)\n",
1185                         (double)*poffset,(double)*pcount,*ptype,strerror(errno)));
1186                 errno = sav;
1187                 return False;
1188         }
1189
1190         *ptype = lock.l_type;
1191         *poffset = lock.l_start;
1192         *pcount = lock.l_len;
1193         *ppid = lock.l_pid;
1194
1195         DEBUG(3,("fcntl_getlock: fd %d is returned info %d pid %u\n",
1196                         fd, (int)lock.l_type, (unsigned int)lock.l_pid));
1197         return True;
1198 }
1199
1200 #if defined(HAVE_OFD_LOCKS)
1201 int map_process_lock_to_ofd_lock(int op, bool *use_ofd_locks)
1202 {
1203         switch (op) {
1204         case F_GETLK:
1205         case F_OFD_GETLK:
1206                 op = F_OFD_GETLK;
1207                 break;
1208         case F_SETLK:
1209         case F_OFD_SETLK:
1210                 op = F_OFD_SETLK;
1211                 break;
1212         case F_SETLKW:
1213         case F_OFD_SETLKW:
1214                 op = F_OFD_SETLKW;
1215                 break;
1216         default:
1217                 *use_ofd_locks = false;
1218                 return -1;
1219         }
1220         *use_ofd_locks = true;
1221         return op;
1222 }
1223 #else /* HAVE_OFD_LOCKS */
1224 int map_process_lock_to_ofd_lock(int op, bool *use_ofd_locks)
1225 {
1226         *use_ofd_locks = false;
1227         return op;
1228 }
1229 #endif /* HAVE_OFD_LOCKS */
1230
1231 #undef DBGC_CLASS
1232 #define DBGC_CLASS DBGC_ALL
1233
1234 /*******************************************************************
1235  Is the name specified one of my netbios names.
1236  Returns true if it is equal, false otherwise.
1237 ********************************************************************/
1238
1239 bool is_myname(const char *s)
1240 {
1241         int n;
1242         bool ret = False;
1243
1244         for (n=0; my_netbios_names(n); n++) {
1245                 const char *nbt_name = my_netbios_names(n);
1246
1247                 if (strncasecmp_m(nbt_name, s, MAX_NETBIOSNAME_LEN-1) == 0) {
1248                         ret=True;
1249                         break;
1250                 }
1251         }
1252         DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret));
1253         return(ret);
1254 }
1255
1256 /*******************************************************************
1257  we distinguish between 2K and XP by the "Native Lan Manager" string
1258    WinXP => "Windows 2002 5.1"
1259    WinXP 64bit => "Windows XP 5.2"
1260    Win2k => "Windows 2000 5.0"
1261    NT4   => "Windows NT 4.0"
1262    Win9x => "Windows 4.0"
1263  Windows 2003 doesn't set the native lan manager string but
1264  they do set the domain to "Windows 2003 5.2" (probably a bug).
1265 ********************************************************************/
1266
1267 void ra_lanman_string( const char *native_lanman )
1268 {
1269         if ( strcmp( native_lanman, "Windows 2002 5.1" ) == 0 )
1270                 set_remote_arch( RA_WINXP );
1271         else if ( strcmp( native_lanman, "Windows XP 5.2" ) == 0 )
1272                 set_remote_arch( RA_WINXP64 );
1273         else if ( strcmp( native_lanman, "Windows Server 2003 5.2" ) == 0 )
1274                 set_remote_arch( RA_WIN2K3 );
1275 }
1276
1277 static const char *remote_arch_strings[] = {
1278         [RA_UNKNOWN] =  "UNKNOWN",
1279         [RA_WFWG] =     "WfWg",
1280         [RA_OS2] =      "OS2",
1281         [RA_WIN95] =    "Win95",
1282         [RA_WINNT] =    "WinNT",
1283         [RA_WIN2K] =    "Win2K",
1284         [RA_WINXP] =    "WinXP",
1285         [RA_WIN2K3] =   "Win2K3",
1286         [RA_VISTA] =    "Vista",
1287         [RA_SAMBA] =    "Samba",
1288         [RA_CIFSFS] =   "CIFSFS",
1289         [RA_WINXP64] =  "WinXP64",
1290         [RA_OSX] =      "OSX",
1291 };
1292
1293 const char *get_remote_arch_str(void)
1294 {
1295         if (ra_type >= ARRAY_SIZE(remote_arch_strings)) {
1296                 /*
1297                  * set_remote_arch() already checks this so ra_type
1298                  * should be in the allowed range, but anyway, let's
1299                  * do another bound check here.
1300                  */
1301                 DBG_ERR("Remote arch info out of sync [%d] missing\n", ra_type);
1302                 ra_type = RA_UNKNOWN;
1303         }
1304         return remote_arch_strings[ra_type];
1305 }
1306
1307 enum remote_arch_types get_remote_arch_from_str(const char *remote_arch_string)
1308 {
1309         int i;
1310
1311         for (i = 0; i < ARRAY_SIZE(remote_arch_strings); i++) {
1312                 if (strcmp(remote_arch_string, remote_arch_strings[i]) == 0) {
1313                         return i;
1314                 }
1315         }
1316         return RA_UNKNOWN;
1317 }
1318
1319 /*******************************************************************
1320  Set the horrid remote_arch string based on an enum.
1321 ********************************************************************/
1322
1323 void set_remote_arch(enum remote_arch_types type)
1324 {
1325         if (ra_type >= ARRAY_SIZE(remote_arch_strings)) {
1326                 /*
1327                  * This protects against someone adding values to enum
1328                  * remote_arch_types without updating
1329                  * remote_arch_strings array.
1330                  */
1331                 DBG_ERR("Remote arch info out of sync [%d] missing\n", ra_type);
1332                 ra_type = RA_UNKNOWN;
1333                 return;
1334         }
1335
1336         ra_type = type;
1337         DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n",
1338                   get_remote_arch_str()));
1339 }
1340
1341 /*******************************************************************
1342  Get the remote_arch type.
1343 ********************************************************************/
1344
1345 enum remote_arch_types get_remote_arch(void)
1346 {
1347         return ra_type;
1348 }
1349
1350 #define RA_CACHE_TTL 7*24*3600
1351
1352 static bool remote_arch_cache_key(const struct GUID *client_guid,
1353                                   fstring key)
1354 {
1355         struct GUID_txt_buf guid_buf;
1356         const char *guid_string = NULL;
1357
1358         guid_string = GUID_buf_string(client_guid, &guid_buf);
1359         if (guid_string == NULL) {
1360                 return false;
1361         }
1362
1363         fstr_sprintf(key, "RA/%s", guid_string);
1364         return true;
1365 }
1366
1367 struct ra_parser_state {
1368         bool found;
1369         enum remote_arch_types ra;
1370 };
1371
1372 static void ra_parser(time_t timeout, DATA_BLOB blob, void *priv_data)
1373 {
1374         struct ra_parser_state *state = (struct ra_parser_state *)priv_data;
1375         const char *ra_str = NULL;
1376
1377         if (timeout <= time(NULL)) {
1378                 return;
1379         }
1380
1381         if ((blob.length == 0) || (blob.data[blob.length-1] != '\0')) {
1382                 DBG_ERR("Remote arch cache key not a string\n");
1383                 return;
1384         }
1385
1386         ra_str = (const char *)blob.data;
1387         DBG_INFO("Got remote arch [%s] from cache\n", ra_str);
1388
1389         state->ra = get_remote_arch_from_str(ra_str);
1390         state->found = true;
1391         return;
1392 }
1393
1394 static bool remote_arch_cache_get(const struct GUID *client_guid)
1395 {
1396         bool ok;
1397         fstring ra_key;
1398         struct ra_parser_state state = (struct ra_parser_state) {
1399                 .found = false,
1400                 .ra = RA_UNKNOWN,
1401         };
1402
1403         ok = remote_arch_cache_key(client_guid, ra_key);
1404         if (!ok) {
1405                 return false;
1406         }
1407
1408         ok = gencache_parse(ra_key, ra_parser, &state);
1409         if (!ok || !state.found) {
1410                 return true;
1411         }
1412
1413         if (state.ra == RA_UNKNOWN) {
1414                 return true;
1415         }
1416
1417         set_remote_arch(state.ra);
1418         return true;
1419 }
1420
1421 static bool remote_arch_cache_set(const struct GUID *client_guid)
1422 {
1423         bool ok;
1424         fstring ra_key;
1425         const char *ra_str = NULL;
1426
1427         if (get_remote_arch() == RA_UNKNOWN) {
1428                 return true;
1429         }
1430
1431         ok = remote_arch_cache_key(client_guid, ra_key);
1432         if (!ok) {
1433                 return false;
1434         }
1435
1436         ra_str = get_remote_arch_str();
1437         if (ra_str == NULL) {
1438                 return false;
1439         }
1440
1441         ok = gencache_set(ra_key, ra_str, time(NULL) + RA_CACHE_TTL);
1442         if (!ok) {
1443                 return false;
1444         }
1445
1446         return true;
1447 }
1448
1449 bool remote_arch_cache_update(const struct GUID *client_guid)
1450 {
1451         bool ok;
1452
1453         if (get_remote_arch() == RA_UNKNOWN) {
1454
1455                 become_root();
1456                 ok = remote_arch_cache_get(client_guid);
1457                 unbecome_root();
1458
1459                 return ok;
1460         }
1461
1462         become_root();
1463         ok = remote_arch_cache_set(client_guid);
1464         unbecome_root();
1465
1466         return ok;
1467 }
1468
1469 bool remote_arch_cache_delete(const struct GUID *client_guid)
1470 {
1471         bool ok;
1472         fstring ra_key;
1473
1474         ok = remote_arch_cache_key(client_guid, ra_key);
1475         if (!ok) {
1476                 return false;
1477         }
1478
1479         become_root();
1480         ok = gencache_del(ra_key);
1481         unbecome_root();
1482
1483         if (!ok) {
1484                 return false;
1485         }
1486
1487         return true;
1488 }
1489
1490 const char *tab_depth(int level, int depth)
1491 {
1492         if( CHECK_DEBUGLVL(level) ) {
1493                 dbgtext("%*s", depth*4, "");
1494         }
1495         return "";
1496 }
1497
1498 /*****************************************************************************
1499  Provide a checksum on a string
1500
1501  Input:  s - the null-terminated character string for which the checksum
1502              will be calculated.
1503
1504   Output: The checksum value calculated for s.
1505 *****************************************************************************/
1506
1507 int str_checksum(const char *s)
1508 {
1509         TDB_DATA key;
1510         if (s == NULL)
1511                 return 0;
1512
1513         key = (TDB_DATA) { .dptr = discard_const_p(uint8_t, s),
1514                            .dsize = strlen(s) };
1515
1516         return tdb_jenkins_hash(&key);
1517 }
1518
1519 /*****************************************************************
1520  Zero a memory area then free it. Used to catch bugs faster.
1521 *****************************************************************/  
1522
1523 void zero_free(void *p, size_t size)
1524 {
1525         memset(p, 0, size);
1526         SAFE_FREE(p);
1527 }
1528
1529 /*****************************************************************
1530  Set our open file limit to a requested max and return the limit.
1531 *****************************************************************/  
1532
1533 int set_maxfiles(int requested_max)
1534 {
1535 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
1536         struct rlimit rlp;
1537         int saved_current_limit;
1538
1539         if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1540                 DEBUG(0,("set_maxfiles: getrlimit (1) for RLIMIT_NOFILE failed with error %s\n",
1541                         strerror(errno) ));
1542                 /* just guess... */
1543                 return requested_max;
1544         }
1545
1546         /* 
1547          * Set the fd limit to be real_max_open_files + MAX_OPEN_FUDGEFACTOR to
1548          * account for the extra fd we need 
1549          * as well as the log files and standard
1550          * handles etc. Save the limit we want to set in case
1551          * we are running on an OS that doesn't support this limit (AIX)
1552          * which always returns RLIM_INFINITY for rlp.rlim_max.
1553          */
1554
1555         /* Try raising the hard (max) limit to the requested amount. */
1556
1557 #if defined(RLIM_INFINITY)
1558         if (rlp.rlim_max != RLIM_INFINITY) {
1559                 int orig_max = rlp.rlim_max;
1560
1561                 if ( rlp.rlim_max < requested_max )
1562                         rlp.rlim_max = requested_max;
1563
1564                 /* This failing is not an error - many systems (Linux) don't
1565                         support our default request of 10,000 open files. JRA. */
1566
1567                 if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1568                         DEBUG(3,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d max files failed with error %s\n", 
1569                                 (int)rlp.rlim_max, strerror(errno) ));
1570
1571                         /* Set failed - restore original value from get. */
1572                         rlp.rlim_max = orig_max;
1573                 }
1574         }
1575 #endif
1576
1577         /* Now try setting the soft (current) limit. */
1578
1579         saved_current_limit = rlp.rlim_cur = MIN(requested_max,rlp.rlim_max);
1580
1581         if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1582                 DEBUG(0,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d files failed with error %s\n", 
1583                         (int)rlp.rlim_cur, strerror(errno) ));
1584                 /* just guess... */
1585                 return saved_current_limit;
1586         }
1587
1588         if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1589                 DEBUG(0,("set_maxfiles: getrlimit (2) for RLIMIT_NOFILE failed with error %s\n",
1590                         strerror(errno) ));
1591                 /* just guess... */
1592                 return saved_current_limit;
1593     }
1594
1595 #if defined(RLIM_INFINITY)
1596         if(rlp.rlim_cur == RLIM_INFINITY)
1597                 return saved_current_limit;
1598 #endif
1599
1600         if((int)rlp.rlim_cur > saved_current_limit)
1601                 return saved_current_limit;
1602
1603         return rlp.rlim_cur;
1604 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
1605         /*
1606          * No way to know - just guess...
1607          */
1608         return requested_max;
1609 #endif
1610 }
1611
1612 /*****************************************************************
1613  malloc that aborts with smb_panic on fail or zero size.
1614  *****************************************************************/  
1615
1616 void *smb_xmalloc_array(size_t size, unsigned int count)
1617 {
1618         void *p;
1619         if (size == 0) {
1620                 smb_panic("smb_xmalloc_array: called with zero size");
1621         }
1622         if (count >= MAX_ALLOC_SIZE/size) {
1623                 smb_panic("smb_xmalloc_array: alloc size too large");
1624         }
1625         if ((p = SMB_MALLOC(size*count)) == NULL) {
1626                 DEBUG(0, ("smb_xmalloc_array failed to allocate %lu * %lu bytes\n",
1627                         (unsigned long)size, (unsigned long)count));
1628                 smb_panic("smb_xmalloc_array: malloc failed");
1629         }
1630         return p;
1631 }
1632
1633 /*
1634   vasprintf that aborts on malloc fail
1635 */
1636
1637  int smb_xvasprintf(char **ptr, const char *format, va_list ap)
1638 {
1639         int n;
1640         va_list ap2;
1641
1642         va_copy(ap2, ap);
1643
1644         n = vasprintf(ptr, format, ap2);
1645         va_end(ap2);
1646         if (n == -1 || ! *ptr) {
1647                 smb_panic("smb_xvasprintf: out of memory");
1648         }
1649         return n;
1650 }
1651
1652 /*****************************************************************
1653  Get local hostname and cache result.
1654 *****************************************************************/
1655
1656 char *myhostname(void)
1657 {
1658         static char *ret;
1659         if (ret == NULL) {
1660                 ret = get_myname(NULL);
1661         }
1662         return ret;
1663 }
1664
1665 /*****************************************************************
1666  Get local hostname and cache result.
1667 *****************************************************************/
1668
1669 char *myhostname_upper(void)
1670 {
1671         static char *ret;
1672         if (ret == NULL) {
1673                 char *name = get_myname(NULL);
1674                 if (name == NULL) {
1675                         return NULL;
1676                 }
1677                 ret = strupper_talloc(NULL, name);
1678                 talloc_free(name);
1679         }
1680         return ret;
1681 }
1682
1683 /*******************************************************************
1684  Given a filename - get its directory name
1685 ********************************************************************/
1686
1687 bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
1688                     const char **name)
1689 {
1690         char *p;
1691         ptrdiff_t len;
1692
1693         p = strrchr_m(dir, '/'); /* Find final '/', if any */
1694
1695         if (p == NULL) {
1696                 if (!(*parent = talloc_strdup(mem_ctx, "."))) {
1697                         return False;
1698                 }
1699                 if (name) {
1700                         *name = dir;
1701                 }
1702                 return True;
1703         }
1704
1705         len = p-dir;
1706
1707         if (!(*parent = (char *)talloc_memdup(mem_ctx, dir, len+1))) {
1708                 return False;
1709         }
1710         (*parent)[len] = '\0';
1711
1712         if (name) {
1713                 *name = p+1;
1714         }
1715         return True;
1716 }
1717
1718 /*******************************************************************
1719  Determine if a pattern contains any Microsoft wildcard characters.
1720 *******************************************************************/
1721
1722 bool ms_has_wild(const char *s)
1723 {
1724         char c;
1725
1726         while ((c = *s++)) {
1727                 switch (c) {
1728                 case '*':
1729                 case '?':
1730                 case '<':
1731                 case '>':
1732                 case '"':
1733                         return True;
1734                 }
1735         }
1736         return False;
1737 }
1738
1739 bool ms_has_wild_w(const smb_ucs2_t *s)
1740 {
1741         smb_ucs2_t c;
1742         if (!s) return False;
1743         while ((c = *s++)) {
1744                 switch (c) {
1745                 case UCS2_CHAR('*'):
1746                 case UCS2_CHAR('?'):
1747                 case UCS2_CHAR('<'):
1748                 case UCS2_CHAR('>'):
1749                 case UCS2_CHAR('"'):
1750                         return True;
1751                 }
1752         }
1753         return False;
1754 }
1755
1756 /*******************************************************************
1757  A wrapper that handles case sensitivity and the special handling
1758  of the ".." name.
1759 *******************************************************************/
1760
1761 bool mask_match(const char *string, const char *pattern, bool is_case_sensitive)
1762 {
1763         if (ISDOTDOT(string))
1764                 string = ".";
1765         if (ISDOT(pattern))
1766                 return False;
1767
1768         return ms_fnmatch_protocol(pattern, string, Protocol, is_case_sensitive) == 0;
1769 }
1770
1771 /*******************************************************************
1772  A wrapper that handles case sensitivity and the special handling
1773  of the ".." name. Varient that is only called by old search code which requires
1774  pattern translation.
1775 *******************************************************************/
1776
1777 bool mask_match_search(const char *string, const char *pattern, bool is_case_sensitive)
1778 {
1779         if (ISDOTDOT(string))
1780                 string = ".";
1781         if (ISDOT(pattern))
1782                 return False;
1783
1784         return ms_fnmatch(pattern, string, True, is_case_sensitive) == 0;
1785 }
1786
1787 /*******************************************************************
1788  A wrapper that handles a list of patters and calls mask_match()
1789  on each.  Returns True if any of the patterns match.
1790 *******************************************************************/
1791
1792 bool mask_match_list(const char *string, char **list, int listLen, bool is_case_sensitive)
1793 {
1794        while (listLen-- > 0) {
1795                if (mask_match(string, *list++, is_case_sensitive))
1796                        return True;
1797        }
1798        return False;
1799 }
1800
1801 /**********************************************************************
1802   Converts a name to a fully qualified domain name.
1803   Returns true if lookup succeeded, false if not (then fqdn is set to name)
1804   Uses getaddrinfo() with AI_CANONNAME flag to obtain the official
1805   canonical name of the host. getaddrinfo() may use a variety of sources
1806   including /etc/hosts to obtain the domainname. It expects aliases in
1807   /etc/hosts to NOT be the FQDN. The FQDN should come first.
1808 ************************************************************************/
1809
1810 bool name_to_fqdn(fstring fqdn, const char *name)
1811 {
1812         char *full = NULL;
1813         struct addrinfo hints;
1814         struct addrinfo *result;
1815         int s;
1816
1817         /* Configure hints to obtain canonical name */
1818
1819         memset(&hints, 0, sizeof(struct addrinfo));
1820         hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
1821         hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
1822         hints.ai_flags = AI_CANONNAME;  /* Get host's FQDN */
1823         hints.ai_protocol = 0;          /* Any protocol */
1824
1825         s = getaddrinfo(name, NULL, &hints, &result);
1826         if (s != 0) {
1827                 DEBUG(1, ("getaddrinfo: %s\n", gai_strerror(s)));
1828                 DEBUG(10,("name_to_fqdn: lookup for %s failed.\n", name));
1829                 fstrcpy(fqdn, name);
1830                 return false;
1831         }
1832         full = result->ai_canonname;
1833
1834         /* Find out if the FQDN is returned as an alias
1835          * to cope with /etc/hosts files where the first
1836          * name is not the FQDN but the short name.
1837          * getaddrinfo provides no easy way of handling aliases
1838          * in /etc/hosts. Users should make sure the FQDN
1839          * comes first in /etc/hosts. */
1840         if (full && (! strchr_m(full, '.'))) {
1841                 DEBUG(1, ("WARNING: your /etc/hosts file may be broken!\n"));
1842                 DEBUGADD(1, ("    Full qualified domain names (FQDNs) should not be specified\n"));
1843                 DEBUGADD(1, ("    as an alias in /etc/hosts. FQDN should be the first name\n"));
1844                 DEBUGADD(1, ("    prior to any aliases.\n"));
1845         }
1846         if (full && (strcasecmp_m(full, "localhost.localdomain") == 0)) {
1847                 DEBUG(1, ("WARNING: your /etc/hosts file may be broken!\n"));
1848                 DEBUGADD(1, ("    Specifying the machine hostname for address 127.0.0.1 may lead\n"));
1849                 DEBUGADD(1, ("    to Kerberos authentication problems as localhost.localdomain\n"));
1850                 DEBUGADD(1, ("    may end up being used instead of the real machine FQDN.\n"));
1851         }
1852
1853         DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name, full));
1854         fstrcpy(fqdn, full);
1855         freeaddrinfo(result);           /* No longer needed */
1856         return true;
1857 }
1858
1859 uint32_t map_share_mode_to_deny_mode(uint32_t share_access, uint32_t private_options)
1860 {
1861         switch (share_access & ~FILE_SHARE_DELETE) {
1862                 case FILE_SHARE_NONE:
1863                         return DENY_ALL;
1864                 case FILE_SHARE_READ:
1865                         return DENY_WRITE;
1866                 case FILE_SHARE_WRITE:
1867                         return DENY_READ;
1868                 case FILE_SHARE_READ|FILE_SHARE_WRITE:
1869                         return DENY_NONE;
1870         }
1871         if (private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) {
1872                 return DENY_DOS;
1873         } else if (private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_FCB) {
1874                 return DENY_FCB;
1875         }
1876
1877         return (uint32_t)-1;
1878 }
1879
1880 struct server_id interpret_pid(const char *pid_string)
1881 {
1882         return server_id_from_string(get_my_vnn(), pid_string);
1883 }
1884
1885 /****************************************************************
1886  Check if an offset into a buffer is safe.
1887  If this returns True it's safe to indirect into the byte at
1888  pointer ptr+off.
1889 ****************************************************************/
1890
1891 bool is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
1892 {
1893         const char *end_base = buf_base + buf_len;
1894         char *end_ptr = ptr + off;
1895
1896         if (!buf_base || !ptr) {
1897                 return False;
1898         }
1899
1900         if (end_base < buf_base || end_ptr < ptr) {
1901                 return False; /* wrap. */
1902         }
1903
1904         if (end_ptr < end_base) {
1905                 return True;
1906         }
1907         return False;
1908 }
1909
1910 /****************************************************************
1911  Return a safe pointer into a buffer, or NULL.
1912 ****************************************************************/
1913
1914 char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
1915 {
1916         return is_offset_safe(buf_base, buf_len, ptr, off) ?
1917                         ptr + off : NULL;
1918 }
1919
1920 /****************************************************************
1921  Return a safe pointer into a string within a buffer, or NULL.
1922 ****************************************************************/
1923
1924 char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
1925 {
1926         if (!is_offset_safe(buf_base, buf_len, ptr, off)) {
1927                 return NULL;
1928         }
1929         /* Check if a valid string exists at this offset. */
1930         if (skip_string(buf_base,buf_len, ptr + off) == NULL) {
1931                 return NULL;
1932         }
1933         return ptr + off;
1934 }
1935
1936 /****************************************************************
1937  Return an SVAL at a pointer, or failval if beyond the end.
1938 ****************************************************************/
1939
1940 int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
1941 {
1942         /*
1943          * Note we use off+1 here, not off+2 as SVAL accesses ptr[0] and ptr[1],
1944          * NOT ptr[2].
1945          */
1946         if (!is_offset_safe(buf_base, buf_len, ptr, off+1)) {
1947                 return failval;
1948         }
1949         return SVAL(ptr,off);
1950 }
1951
1952 /****************************************************************
1953  Return an IVAL at a pointer, or failval if beyond the end.
1954 ****************************************************************/
1955
1956 int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
1957 {
1958         /*
1959          * Note we use off+3 here, not off+4 as IVAL accesses 
1960          * ptr[0] ptr[1] ptr[2] ptr[3] NOT ptr[4].
1961          */
1962         if (!is_offset_safe(buf_base, buf_len, ptr, off+3)) {
1963                 return failval;
1964         }
1965         return IVAL(ptr,off);
1966 }
1967
1968 /****************************************************************
1969  Split DOM\user into DOM and user. Do not mix with winbind variants of that
1970  call (they take care of winbind separator and other winbind specific settings).
1971 ****************************************************************/
1972
1973 bool split_domain_user(TALLOC_CTX *mem_ctx,
1974                        const char *full_name,
1975                        char **domain,
1976                        char **user)
1977 {
1978         const char *p = NULL;
1979
1980         p = strchr_m(full_name, '\\');
1981
1982         if (p != NULL) {
1983                 *domain = talloc_strndup(mem_ctx, full_name,
1984                                          PTR_DIFF(p, full_name));
1985                 if (*domain == NULL) {
1986                         return false;
1987                 }
1988                 *user = talloc_strdup(mem_ctx, p+1);
1989                 if (*user == NULL) {
1990                         TALLOC_FREE(*domain);
1991                         return false;
1992                 }
1993         } else {
1994                 *domain = NULL;
1995                 *user = talloc_strdup(mem_ctx, full_name);
1996                 if (*user == NULL) {
1997                         return false;
1998                 }
1999         }
2000
2001         return true;
2002 }
2003
2004 /****************************************************************
2005  strip off leading '\\' from a hostname
2006 ****************************************************************/
2007
2008 const char *strip_hostname(const char *s)
2009 {
2010         if (!s) {
2011                 return NULL;
2012         }
2013
2014         if (strlen_m(s) < 3) {
2015                 return s;
2016         }
2017
2018         if (s[0] == '\\') s++;
2019         if (s[0] == '\\') s++;
2020
2021         return s;
2022 }
2023
2024 bool any_nt_status_not_ok(NTSTATUS err1, NTSTATUS err2, NTSTATUS *result)
2025 {
2026         if (!NT_STATUS_IS_OK(err1)) {
2027                 *result = err1;
2028                 return true;
2029         }
2030         if (!NT_STATUS_IS_OK(err2)) {
2031                 *result = err2;
2032                 return true;
2033         }
2034         return false;
2035 }
2036
2037 int timeval_to_msec(struct timeval t)
2038 {
2039         return t.tv_sec * 1000 + (t.tv_usec+999) / 1000;
2040 }
2041
2042 /*******************************************************************
2043  Check a given DOS pathname is valid for a share.
2044 ********************************************************************/
2045
2046 char *valid_share_pathname(TALLOC_CTX *ctx, const char *dos_pathname)
2047 {
2048         char *ptr = NULL;
2049
2050         if (!dos_pathname) {
2051                 return NULL;
2052         }
2053
2054         ptr = talloc_strdup(ctx, dos_pathname);
2055         if (!ptr) {
2056                 return NULL;
2057         }
2058         /* Convert any '\' paths to '/' */
2059         unix_format(ptr);
2060         ptr = unix_clean_name(ctx, ptr);
2061         if (!ptr) {
2062                 return NULL;
2063         }
2064
2065         /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
2066         if (strlen(ptr) > 2 && ptr[1] == ':' && ptr[0] != '/')
2067                 ptr += 2;
2068
2069         /* Only absolute paths allowed. */
2070         if (*ptr != '/')
2071                 return NULL;
2072
2073         return ptr;
2074 }
2075
2076 /*******************************************************************
2077  Return True if the filename is one of the special executable types.
2078 ********************************************************************/
2079
2080 bool is_executable(const char *fname)
2081 {
2082         if ((fname = strrchr_m(fname,'.'))) {
2083                 if (strequal(fname,".com") ||
2084                     strequal(fname,".dll") ||
2085                     strequal(fname,".exe") ||
2086                     strequal(fname,".sym")) {
2087                         return True;
2088                 }
2089         }
2090         return False;
2091 }
2092
2093 /****************************************************************************
2094  Open a file with a share mode - old openX method - map into NTCreate.
2095 ****************************************************************************/
2096
2097 bool map_open_params_to_ntcreate(const char *smb_base_fname,
2098                                  int deny_mode, int open_func,
2099                                  uint32_t *paccess_mask,
2100                                  uint32_t *pshare_mode,
2101                                  uint32_t *pcreate_disposition,
2102                                  uint32_t *pcreate_options,
2103                                  uint32_t *pprivate_flags)
2104 {
2105         uint32_t access_mask;
2106         uint32_t share_mode;
2107         uint32_t create_disposition;
2108         uint32_t create_options = FILE_NON_DIRECTORY_FILE;
2109         uint32_t private_flags = 0;
2110
2111         DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
2112                   "open_func = 0x%x\n",
2113                   smb_base_fname, (unsigned int)deny_mode,
2114                   (unsigned int)open_func ));
2115
2116         /* Create the NT compatible access_mask. */
2117         switch (GET_OPENX_MODE(deny_mode)) {
2118                 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
2119                 case DOS_OPEN_RDONLY:
2120                         access_mask = FILE_GENERIC_READ;
2121                         break;
2122                 case DOS_OPEN_WRONLY:
2123                         access_mask = FILE_GENERIC_WRITE;
2124                         break;
2125                 case DOS_OPEN_RDWR:
2126                 case DOS_OPEN_FCB:
2127                         access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
2128                         break;
2129                 default:
2130                         DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
2131                                   (unsigned int)GET_OPENX_MODE(deny_mode)));
2132                         return False;
2133         }
2134
2135         /* Create the NT compatible create_disposition. */
2136         switch (open_func) {
2137                 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
2138                         create_disposition = FILE_CREATE;
2139                         break;
2140
2141                 case OPENX_FILE_EXISTS_OPEN:
2142                         create_disposition = FILE_OPEN;
2143                         break;
2144
2145                 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
2146                         create_disposition = FILE_OPEN_IF;
2147                         break;
2148
2149                 case OPENX_FILE_EXISTS_TRUNCATE:
2150                         create_disposition = FILE_OVERWRITE;
2151                         break;
2152
2153                 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
2154                         create_disposition = FILE_OVERWRITE_IF;
2155                         break;
2156
2157                 default:
2158                         /* From samba4 - to be confirmed. */
2159                         if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
2160                                 create_disposition = FILE_CREATE;
2161                                 break;
2162                         }
2163                         DEBUG(10,("map_open_params_to_ntcreate: bad "
2164                                   "open_func 0x%x\n", (unsigned int)open_func));
2165                         return False;
2166         }
2167
2168         /* Create the NT compatible share modes. */
2169         switch (GET_DENY_MODE(deny_mode)) {
2170                 case DENY_ALL:
2171                         share_mode = FILE_SHARE_NONE;
2172                         break;
2173
2174                 case DENY_WRITE:
2175                         share_mode = FILE_SHARE_READ;
2176                         break;
2177
2178                 case DENY_READ:
2179                         share_mode = FILE_SHARE_WRITE;
2180                         break;
2181
2182                 case DENY_NONE:
2183                         share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
2184                         break;
2185
2186                 case DENY_DOS:
2187                         private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
2188                         if (is_executable(smb_base_fname)) {
2189                                 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
2190                         } else {
2191                                 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
2192                                         share_mode = FILE_SHARE_READ;
2193                                 } else {
2194                                         share_mode = FILE_SHARE_NONE;
2195                                 }
2196                         }
2197                         break;
2198
2199                 case DENY_FCB:
2200                         private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
2201                         share_mode = FILE_SHARE_NONE;
2202                         break;
2203
2204                 default:
2205                         DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
2206                                 (unsigned int)GET_DENY_MODE(deny_mode) ));
2207                         return False;
2208         }
2209
2210         DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
2211                   "share_mode = 0x%x, create_disposition = 0x%x, "
2212                   "create_options = 0x%x private_flags = 0x%x\n",
2213                   smb_base_fname,
2214                   (unsigned int)access_mask,
2215                   (unsigned int)share_mode,
2216                   (unsigned int)create_disposition,
2217                   (unsigned int)create_options,
2218                   (unsigned int)private_flags));
2219
2220         if (paccess_mask) {
2221                 *paccess_mask = access_mask;
2222         }
2223         if (pshare_mode) {
2224                 *pshare_mode = share_mode;
2225         }
2226         if (pcreate_disposition) {
2227                 *pcreate_disposition = create_disposition;
2228         }
2229         if (pcreate_options) {
2230                 *pcreate_options = create_options;
2231         }
2232         if (pprivate_flags) {
2233                 *pprivate_flags = private_flags;
2234         }
2235
2236         return True;
2237
2238 }
2239
2240 /*************************************************************************
2241  Return a talloced copy of a struct security_unix_token. NULL on fail.
2242 *************************************************************************/
2243
2244 struct security_unix_token *copy_unix_token(TALLOC_CTX *ctx, const struct security_unix_token *tok)
2245 {
2246         struct security_unix_token *cpy;
2247
2248         cpy = talloc(ctx, struct security_unix_token);
2249         if (!cpy) {
2250                 return NULL;
2251         }
2252
2253         cpy->uid = tok->uid;
2254         cpy->gid = tok->gid;
2255         cpy->ngroups = tok->ngroups;
2256         if (tok->ngroups) {
2257                 /* Make this a talloc child of cpy. */
2258                 cpy->groups = (gid_t *)talloc_memdup(
2259                         cpy, tok->groups, tok->ngroups * sizeof(gid_t));
2260                 if (!cpy->groups) {
2261                         TALLOC_FREE(cpy);
2262                         return NULL;
2263                 }
2264         } else {
2265                 cpy->groups = NULL;
2266         }
2267         return cpy;
2268 }
2269
2270 /****************************************************************************
2271  Check that a file matches a particular file type.
2272 ****************************************************************************/
2273
2274 bool dir_check_ftype(uint32_t mode, uint32_t dirtype)
2275 {
2276         uint32_t mask;
2277
2278         /* Check the "may have" search bits. */
2279         if (((mode & ~dirtype) &
2280                         (FILE_ATTRIBUTE_HIDDEN |
2281                          FILE_ATTRIBUTE_SYSTEM |
2282                          FILE_ATTRIBUTE_DIRECTORY)) != 0) {
2283                 return false;
2284         }
2285
2286         /* Check the "must have" bits,
2287            which are the may have bits shifted eight */
2288         /* If must have bit is set, the file/dir can
2289            not be returned in search unless the matching
2290            file attribute is set */
2291         mask = ((dirtype >> 8) & (FILE_ATTRIBUTE_DIRECTORY|
2292                                     FILE_ATTRIBUTE_ARCHIVE|
2293                                    FILE_ATTRIBUTE_READONLY|
2294                                      FILE_ATTRIBUTE_HIDDEN|
2295                                      FILE_ATTRIBUTE_SYSTEM)); /* & 0x37 */
2296         if(mask) {
2297                 if((mask & (mode & (FILE_ATTRIBUTE_DIRECTORY|
2298                                       FILE_ATTRIBUTE_ARCHIVE|
2299                                      FILE_ATTRIBUTE_READONLY|
2300                                        FILE_ATTRIBUTE_HIDDEN|
2301                                         FILE_ATTRIBUTE_SYSTEM))) == mask) {
2302                         /* check if matching attribute present */
2303                         return true;
2304                 } else {
2305                         return false;
2306                 }
2307         }
2308
2309         return true;
2310 }