s3: Make is_executable() available in lib/
[amitay/samba.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 "util_tdb.h"
28 #include "ctdbd_conn.h"
29 #include "../lib/util/util_pw.h"
30 #include "messages.h"
31 #include <ccan/hash/hash.h>
32
33 /* Max allowable allococation - 256mb - 0x10000000 */
34 #define MAX_ALLOC_SIZE (1024*1024*256)
35
36 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
37 #ifdef WITH_NISPLUS_HOME
38 #ifdef BROKEN_NISPLUS_INCLUDE_FILES
39 /*
40  * The following lines are needed due to buggy include files
41  * in Solaris 2.6 which define GROUP in both /usr/include/sys/acl.h and
42  * also in /usr/include/rpcsvc/nis.h. The definitions conflict. JRA.
43  * Also GROUP_OBJ is defined as 0x4 in /usr/include/sys/acl.h and as
44  * an enum in /usr/include/rpcsvc/nis.h.
45  */
46
47 #if defined(GROUP)
48 #undef GROUP
49 #endif
50
51 #if defined(GROUP_OBJ)
52 #undef GROUP_OBJ
53 #endif
54
55 #endif /* BROKEN_NISPLUS_INCLUDE_FILES */
56
57 #include <rpcsvc/nis.h>
58
59 #endif /* WITH_NISPLUS_HOME */
60 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
61
62 static enum protocol_types Protocol = PROTOCOL_COREPLUS;
63
64 enum protocol_types get_Protocol(void)
65 {
66         return Protocol;
67 }
68
69 void set_Protocol(enum protocol_types  p)
70 {
71         Protocol = p;
72 }
73
74 static enum remote_arch_types ra_type = RA_UNKNOWN;
75
76 void gfree_all( void )
77 {
78         gfree_names();
79         gfree_loadparm();
80         gfree_charcnv();
81         gfree_interfaces();
82         gfree_debugsyms();
83 }
84
85 /*******************************************************************
86  Check if a file exists - call vfs_file_exist for samba files.
87 ********************************************************************/
88
89 bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf,
90                      bool fake_dir_create_times)
91 {
92         SMB_STRUCT_STAT st;
93         if (!sbuf)
94                 sbuf = &st;
95
96         if (sys_stat(fname, sbuf, fake_dir_create_times) != 0)
97                 return(False);
98
99         return((S_ISREG(sbuf->st_ex_mode)) || (S_ISFIFO(sbuf->st_ex_mode)));
100 }
101
102 /*******************************************************************
103  Check if a unix domain socket exists - call vfs_file_exist for samba files.
104 ********************************************************************/
105
106 bool socket_exist(const char *fname)
107 {
108         SMB_STRUCT_STAT st;
109         if (sys_stat(fname, &st, false) != 0)
110                 return(False);
111
112         return S_ISSOCK(st.st_ex_mode);
113 }
114
115 /*******************************************************************
116  Returns the size in bytes of the named given the stat struct.
117 ********************************************************************/
118
119 uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
120 {
121         return sbuf->st_ex_size;
122 }
123
124 /*******************************************************************
125  Returns the size in bytes of the named file.
126 ********************************************************************/
127
128 SMB_OFF_T get_file_size(char *file_name)
129 {
130         SMB_STRUCT_STAT buf;
131         buf.st_ex_size = 0;
132         if (sys_stat(file_name, &buf, false) != 0)
133                 return (SMB_OFF_T)-1;
134         return get_file_size_stat(&buf);
135 }
136
137 /*******************************************************************
138  Show a smb message structure.
139 ********************************************************************/
140
141 void show_msg(const char *buf)
142 {
143         int i;
144         int bcc=0;
145
146         if (!DEBUGLVL(5))
147                 return;
148
149         DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
150                         smb_len(buf),
151                         (int)CVAL(buf,smb_com),
152                         (int)CVAL(buf,smb_rcls),
153                         (int)CVAL(buf,smb_reh),
154                         (int)SVAL(buf,smb_err),
155                         (int)CVAL(buf,smb_flg),
156                         (int)SVAL(buf,smb_flg2)));
157         DEBUGADD(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\n",
158                         (int)SVAL(buf,smb_tid),
159                         (int)SVAL(buf,smb_pid),
160                         (int)SVAL(buf,smb_uid),
161                         (int)SVAL(buf,smb_mid)));
162         DEBUGADD(5,("smt_wct=%d\n",(int)CVAL(buf,smb_wct)));
163
164         for (i=0;i<(int)CVAL(buf,smb_wct);i++)
165                 DEBUGADD(5,("smb_vwv[%2d]=%5d (0x%X)\n",i,
166                         SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
167
168         bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
169
170         DEBUGADD(5,("smb_bcc=%d\n",bcc));
171
172         if (DEBUGLEVEL < 10)
173                 return;
174
175         if (DEBUGLEVEL < 50)
176                 bcc = MIN(bcc, 512);
177
178         dump_data(10, (const uint8 *)smb_buf_const(buf), bcc);
179 }
180
181 /*******************************************************************
182  Set the length and marker of an encrypted smb packet.
183 ********************************************************************/
184
185 void smb_set_enclen(char *buf,int len,uint16 enc_ctx_num)
186 {
187         _smb_setlen(buf,len);
188
189         SCVAL(buf,4,0xFF);
190         SCVAL(buf,5,'E');
191         SSVAL(buf,6,enc_ctx_num);
192 }
193
194 /*******************************************************************
195  Set the length and marker of an smb packet.
196 ********************************************************************/
197
198 void smb_setlen(char *buf,int len)
199 {
200         _smb_setlen(buf,len);
201
202         SCVAL(buf,4,0xFF);
203         SCVAL(buf,5,'S');
204         SCVAL(buf,6,'M');
205         SCVAL(buf,7,'B');
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 **outbuf, DATA_BLOB blob)
226 {
227         size_t newlen = smb_len(*outbuf) + 4 + blob.length;
228         uint8 *tmp;
229
230         if (!(tmp = talloc_realloc(NULL, *outbuf, uint8, 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, SMB_OFF_T pos)
359 {
360         size_t total=0;
361         ssize_t ret;
362
363         if (pos == (SMB_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 (sys_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
395 NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
396                            struct event_context *ev_ctx,
397                            struct server_id id,
398                            bool parent_longlived)
399 {
400         NTSTATUS status = NT_STATUS_OK;
401
402         /* Reset the state of the random
403          * number generation system, so
404          * children do not get the same random
405          * numbers as each other */
406         set_need_random_reseed();
407
408         /* tdb needs special fork handling */
409         if (tdb_reopen_all(parent_longlived ? 1 : 0) != 0) {
410                 DEBUG(0,("tdb_reopen_all failed.\n"));
411                 status = NT_STATUS_OPEN_FAILED;
412                 goto done;
413         }
414
415         if (ev_ctx && tevent_re_initialise(ev_ctx) != 0) {
416                 smb_panic(__location__ ": Failed to re-initialise event context");
417         }
418
419         if (msg_ctx) {
420                 /*
421                  * For clustering, we need to re-init our ctdbd connection after the
422                  * fork
423                  */
424                 status = messaging_reinit(msg_ctx, id);
425                 if (!NT_STATUS_IS_OK(status)) {
426                         DEBUG(0,("messaging_reinit() failed: %s\n",
427                                  nt_errstr(status)));
428                 }
429         }
430  done:
431         return status;
432 }
433
434 /****************************************************************************
435  (Hopefully) efficient array append.
436 ****************************************************************************/
437
438 void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
439                         void *element, void *_array, uint32 *num_elements,
440                         ssize_t *array_size)
441 {
442         void **array = (void **)_array;
443
444         if (*array_size < 0) {
445                 return;
446         }
447
448         if (*array == NULL) {
449                 if (*array_size == 0) {
450                         *array_size = 128;
451                 }
452
453                 if (*array_size >= MAX_ALLOC_SIZE/element_size) {
454                         goto error;
455                 }
456
457                 *array = TALLOC(mem_ctx, element_size * (*array_size));
458                 if (*array == NULL) {
459                         goto error;
460                 }
461         }
462
463         if (*num_elements == *array_size) {
464                 *array_size *= 2;
465
466                 if (*array_size >= MAX_ALLOC_SIZE/element_size) {
467                         goto error;
468                 }
469
470                 *array = TALLOC_REALLOC(mem_ctx, *array,
471                                         element_size * (*array_size));
472
473                 if (*array == NULL) {
474                         goto error;
475                 }
476         }
477
478         memcpy((char *)(*array) + element_size*(*num_elements),
479                element, element_size);
480         *num_elements += 1;
481
482         return;
483
484  error:
485         *num_elements = 0;
486         *array_size = -1;
487 }
488
489 /****************************************************************************
490  Get my own domain name, or "" if we have none.
491 ****************************************************************************/
492
493 char *get_mydnsdomname(TALLOC_CTX *ctx)
494 {
495         const char *domname;
496         char *p;
497
498         domname = get_mydnsfullname();
499         if (!domname) {
500                 return NULL;
501         }
502
503         p = strchr_m(domname, '.');
504         if (p) {
505                 p++;
506                 return talloc_strdup(ctx, p);
507         } else {
508                 return talloc_strdup(ctx, "");
509         }
510 }
511
512 /****************************************************************************
513  Interpret a protocol description string, with a default.
514 ****************************************************************************/
515
516 int interpret_protocol(const char *str,int def)
517 {
518         if (strequal(str,"NT1"))
519                 return(PROTOCOL_NT1);
520         if (strequal(str,"LANMAN2"))
521                 return(PROTOCOL_LANMAN2);
522         if (strequal(str,"LANMAN1"))
523                 return(PROTOCOL_LANMAN1);
524         if (strequal(str,"CORE"))
525                 return(PROTOCOL_CORE);
526         if (strequal(str,"COREPLUS"))
527                 return(PROTOCOL_COREPLUS);
528         if (strequal(str,"CORE+"))
529                 return(PROTOCOL_COREPLUS);
530
531         DEBUG(0,("Unrecognised protocol level %s\n",str));
532
533         return(def);
534 }
535
536
537 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
538 /******************************************************************
539  Remove any mount options such as -rsize=2048,wsize=2048 etc.
540  Based on a fix from <Thomas.Hepper@icem.de>.
541  Returns a malloc'ed string.
542 *******************************************************************/
543
544 static char *strip_mount_options(TALLOC_CTX *ctx, const char *str)
545 {
546         if (*str == '-') {
547                 const char *p = str;
548                 while(*p && !isspace(*p))
549                         p++;
550                 while(*p && isspace(*p))
551                         p++;
552                 if(*p) {
553                         return talloc_strdup(ctx, p);
554                 }
555         }
556         return NULL;
557 }
558
559 /*******************************************************************
560  Patch from jkf@soton.ac.uk
561  Split Luke's automount_server into YP lookup and string splitter
562  so can easily implement automount_path().
563  Returns a malloc'ed string.
564 *******************************************************************/
565
566 #ifdef WITH_NISPLUS_HOME
567 char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
568 {
569         char *value = NULL;
570
571         char *nis_map = (char *)lp_nis_home_map_name();
572
573         char buffer[NIS_MAXATTRVAL + 1];
574         nis_result *result;
575         nis_object *object;
576         entry_obj  *entry;
577
578         snprintf(buffer, sizeof(buffer), "[key=%s],%s", user_name, nis_map);
579         DEBUG(5, ("NIS+ querystring: %s\n", buffer));
580
581         if (result = nis_list(buffer, FOLLOW_PATH|EXPAND_NAME|HARD_LOOKUP, NULL, NULL)) {
582                 if (result->status != NIS_SUCCESS) {
583                         DEBUG(3, ("NIS+ query failed: %s\n", nis_sperrno(result->status)));
584                 } else {
585                         object = result->objects.objects_val;
586                         if (object->zo_data.zo_type == ENTRY_OBJ) {
587                                 entry = &object->zo_data.objdata_u.en_data;
588                                 DEBUG(5, ("NIS+ entry type: %s\n", entry->en_type));
589                                 DEBUG(3, ("NIS+ result: %s\n", entry->en_cols.en_cols_val[1].ec_value.ec_value_val));
590
591                                 value = talloc_strdup(ctx,
592                                                 entry->en_cols.en_cols_val[1].ec_value.ec_value_val);
593                                 if (!value) {
594                                         nis_freeresult(result);
595                                         return NULL;
596                                 }
597                                 value = talloc_string_sub(ctx,
598                                                 value,
599                                                 "&",
600                                                 user_name);
601                         }
602                 }
603         }
604         nis_freeresult(result);
605
606         if (value) {
607                 value = strip_mount_options(ctx, value);
608                 DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n",
609                                         user_name, value));
610         }
611         return value;
612 }
613 #else /* WITH_NISPLUS_HOME */
614
615 char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
616 {
617         char *value = NULL;
618
619         int nis_error;        /* returned by yp all functions */
620         char *nis_result;     /* yp_match inits this */
621         int nis_result_len;  /* and set this */
622         char *nis_domain;     /* yp_get_default_domain inits this */
623         char *nis_map = (char *)lp_nis_home_map_name();
624
625         if ((nis_error = yp_get_default_domain(&nis_domain)) != 0) {
626                 DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
627                 return NULL;
628         }
629
630         DEBUG(5, ("NIS Domain: %s\n", nis_domain));
631
632         if ((nis_error = yp_match(nis_domain, nis_map, user_name,
633                                         strlen(user_name), &nis_result,
634                                         &nis_result_len)) == 0) {
635                 if (nis_result_len > 0 && nis_result[nis_result_len] == '\n') {
636                         nis_result[nis_result_len] = '\0';
637                 }
638                 value = talloc_strdup(ctx, nis_result);
639                 if (!value) {
640                         return NULL;
641                 }
642                 value = strip_mount_options(ctx, value);
643         } else if(nis_error == YPERR_KEY) {
644                 DEBUG(3, ("YP Key not found:  while looking up \"%s\" in map \"%s\"\n", 
645                                 user_name, nis_map));
646                 DEBUG(3, ("using defaults for server and home directory\n"));
647         } else {
648                 DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n", 
649                                 yperr_string(nis_error), user_name, nis_map));
650         }
651
652         if (value) {
653                 DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name, value));
654         }
655         return value;
656 }
657 #endif /* WITH_NISPLUS_HOME */
658 #endif
659
660 /****************************************************************************
661  Check if a process exists. Does this work on all unixes?
662 ****************************************************************************/
663
664 bool process_exists(const struct server_id pid)
665 {
666         if (procid_is_me(&pid)) {
667                 return True;
668         }
669
670         if (procid_is_local(&pid)) {
671                 return (kill(pid.pid,0) == 0 || errno != ESRCH);
672         }
673
674 #ifdef CLUSTER_SUPPORT
675         return ctdbd_process_exists(messaging_ctdbd_connection(),
676                                     pid.vnn, pid.pid);
677 #else
678         return False;
679 #endif
680 }
681
682 /*******************************************************************
683  Convert a uid into a user name.
684 ********************************************************************/
685
686 const char *uidtoname(uid_t uid)
687 {
688         TALLOC_CTX *ctx = talloc_tos();
689         char *name = NULL;
690         struct passwd *pass = NULL;
691
692         pass = getpwuid_alloc(ctx,uid);
693         if (pass) {
694                 name = talloc_strdup(ctx,pass->pw_name);
695                 TALLOC_FREE(pass);
696         } else {
697                 name = talloc_asprintf(ctx,
698                                 "%ld",
699                                 (long int)uid);
700         }
701         return name;
702 }
703
704 /*******************************************************************
705  Convert a gid into a group name.
706 ********************************************************************/
707
708 char *gidtoname(gid_t gid)
709 {
710         struct group *grp;
711
712         grp = getgrgid(gid);
713         if (grp) {
714                 return talloc_strdup(talloc_tos(), grp->gr_name);
715         }
716         else {
717                 return talloc_asprintf(talloc_tos(),
718                                         "%d",
719                                         (int)gid);
720         }
721 }
722
723 /*******************************************************************
724  Convert a user name into a uid.
725 ********************************************************************/
726
727 uid_t nametouid(const char *name)
728 {
729         struct passwd *pass;
730         char *p;
731         uid_t u;
732
733         pass = Get_Pwnam_alloc(talloc_tos(), name);
734         if (pass) {
735                 u = pass->pw_uid;
736                 TALLOC_FREE(pass);
737                 return u;
738         }
739
740         u = (uid_t)strtol(name, &p, 0);
741         if ((p != name) && (*p == '\0'))
742                 return u;
743
744         return (uid_t)-1;
745 }
746
747 /*******************************************************************
748  Convert a name to a gid_t if possible. Return -1 if not a group. 
749 ********************************************************************/
750
751 gid_t nametogid(const char *name)
752 {
753         struct group *grp;
754         char *p;
755         gid_t g;
756
757         g = (gid_t)strtol(name, &p, 0);
758         if ((p != name) && (*p == '\0'))
759                 return g;
760
761         grp = sys_getgrnam(name);
762         if (grp)
763                 return(grp->gr_gid);
764         return (gid_t)-1;
765 }
766
767 /*******************************************************************
768  Something really nasty happened - panic !
769 ********************************************************************/
770
771 void smb_panic_s3(const char *why)
772 {
773         char *cmd;
774         int result;
775
776         DEBUG(0,("PANIC (pid %llu): %s\n",
777                     (unsigned long long)sys_getpid(), why));
778         log_stack_trace();
779
780         cmd = lp_panic_action();
781         if (cmd && *cmd) {
782                 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
783                 result = system(cmd);
784
785                 if (result == -1)
786                         DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
787                                           strerror(errno)));
788                 else
789                         DEBUG(0, ("smb_panic(): action returned status %d\n",
790                                           WEXITSTATUS(result)));
791         }
792
793         dump_core();
794 }
795
796 /*******************************************************************
797  Print a backtrace of the stack to the debug log. This function
798  DELIBERATELY LEAKS MEMORY. The expectation is that you should
799  exit shortly after calling it.
800 ********************************************************************/
801
802 #ifdef HAVE_LIBUNWIND_H
803 #include <libunwind.h>
804 #endif
805
806 #ifdef HAVE_EXECINFO_H
807 #include <execinfo.h>
808 #endif
809
810 #ifdef HAVE_LIBEXC_H
811 #include <libexc.h>
812 #endif
813
814 void log_stack_trace(void)
815 {
816 #ifdef HAVE_LIBUNWIND
817         /* Try to use libunwind before any other technique since on ia64
818          * libunwind correctly walks the stack in more circumstances than
819          * backtrace.
820          */ 
821         unw_cursor_t cursor;
822         unw_context_t uc;
823         unsigned i = 0;
824
825         char procname[256];
826         unw_word_t ip, sp, off;
827
828         procname[sizeof(procname) - 1] = '\0';
829
830         if (unw_getcontext(&uc) != 0) {
831                 goto libunwind_failed;
832         }
833
834         if (unw_init_local(&cursor, &uc) != 0) {
835                 goto libunwind_failed;
836         }
837
838         DEBUG(0, ("BACKTRACE:\n"));
839
840         do {
841             ip = sp = 0;
842             unw_get_reg(&cursor, UNW_REG_IP, &ip);
843             unw_get_reg(&cursor, UNW_REG_SP, &sp);
844
845             switch (unw_get_proc_name(&cursor,
846                         procname, sizeof(procname) - 1, &off) ) {
847             case 0:
848                     /* Name found. */
849             case -UNW_ENOMEM:
850                     /* Name truncated. */
851                     DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
852                             i, procname, (long long)off,
853                             (long long)ip, (long long) sp));
854                     break;
855             default:
856             /* case -UNW_ENOINFO: */
857             /* case -UNW_EUNSPEC: */
858                     /* No symbol name found. */
859                     DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
860                             i, "<unknown symbol>",
861                             (long long)ip, (long long) sp));
862             }
863             ++i;
864         } while (unw_step(&cursor) > 0);
865
866         return;
867
868 libunwind_failed:
869         DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
870
871 #elif HAVE_BACKTRACE_SYMBOLS
872         void *backtrace_stack[BACKTRACE_STACK_SIZE];
873         size_t backtrace_size;
874         char **backtrace_strings;
875
876         /* get the backtrace (stack frames) */
877         backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
878         backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
879
880         DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 
881                   (unsigned long)backtrace_size));
882
883         if (backtrace_strings) {
884                 int i;
885
886                 for (i = 0; i < backtrace_size; i++)
887                         DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
888
889                 /* Leak the backtrace_strings, rather than risk what free() might do */
890         }
891
892 #elif HAVE_LIBEXC
893
894         /* The IRIX libexc library provides an API for unwinding the stack. See
895          * libexc(3) for details. Apparantly trace_back_stack leaks memory, but
896          * since we are about to abort anyway, it hardly matters.
897          */
898
899 #define NAMESIZE 32 /* Arbitrary */
900
901         __uint64_t      addrs[BACKTRACE_STACK_SIZE];
902         char *          names[BACKTRACE_STACK_SIZE];
903         char            namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
904
905         int             i;
906         int             levels;
907
908         ZERO_ARRAY(addrs);
909         ZERO_ARRAY(names);
910         ZERO_ARRAY(namebuf);
911
912         /* We need to be root so we can open our /proc entry to walk
913          * our stack. It also helps when we want to dump core.
914          */
915         become_root();
916
917         for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
918                 names[i] = namebuf + (i * NAMESIZE);
919         }
920
921         levels = trace_back_stack(0, addrs, names,
922                         BACKTRACE_STACK_SIZE, NAMESIZE - 1);
923
924         DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
925         for (i = 0; i < levels; i++) {
926                 DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
927         }
928 #undef NAMESIZE
929
930 #else
931         DEBUG(0, ("unable to produce a stack trace on this platform\n"));
932 #endif
933 }
934
935 /*******************************************************************
936   A readdir wrapper which just returns the file name.
937  ********************************************************************/
938
939 const char *readdirname(SMB_STRUCT_DIR *p)
940 {
941         SMB_STRUCT_DIRENT *ptr;
942         char *dname;
943
944         if (!p)
945                 return(NULL);
946
947         ptr = (SMB_STRUCT_DIRENT *)sys_readdir(p);
948         if (!ptr)
949                 return(NULL);
950
951         dname = ptr->d_name;
952
953 #ifdef NEXT2
954         if (telldir(p) < 0)
955                 return(NULL);
956 #endif
957
958 #ifdef HAVE_BROKEN_READDIR_NAME
959         /* using /usr/ucb/cc is BAD */
960         dname = dname - 2;
961 #endif
962
963         return talloc_strdup(talloc_tos(), dname);
964 }
965
966 /*******************************************************************
967  Utility function used to decide if the last component 
968  of a path matches a (possibly wildcarded) entry in a namelist.
969 ********************************************************************/
970
971 bool is_in_path(const char *name, name_compare_entry *namelist, bool case_sensitive)
972 {
973         const char *last_component;
974
975         /* if we have no list it's obviously not in the path */
976         if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL))) {
977                 return False;
978         }
979
980         DEBUG(8, ("is_in_path: %s\n", name));
981
982         /* Get the last component of the unix name. */
983         last_component = strrchr_m(name, '/');
984         if (!last_component) {
985                 last_component = name;
986         } else {
987                 last_component++; /* Go past '/' */
988         }
989
990         for(; namelist->name != NULL; namelist++) {
991                 if(namelist->is_wild) {
992                         if (mask_match(last_component, namelist->name, case_sensitive)) {
993                                 DEBUG(8,("is_in_path: mask match succeeded\n"));
994                                 return True;
995                         }
996                 } else {
997                         if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
998                                                 (!case_sensitive && (strcasecmp_m(last_component, namelist->name) == 0))) {
999                                 DEBUG(8,("is_in_path: match succeeded\n"));
1000                                 return True;
1001                         }
1002                 }
1003         }
1004         DEBUG(8,("is_in_path: match not found\n"));
1005         return False;
1006 }
1007
1008 /*******************************************************************
1009  Strip a '/' separated list into an array of 
1010  name_compare_enties structures suitable for 
1011  passing to is_in_path(). We do this for
1012  speed so we can pre-parse all the names in the list 
1013  and don't do it for each call to is_in_path().
1014  We also check if the entry contains a wildcard to
1015  remove a potentially expensive call to mask_match
1016  if possible.
1017 ********************************************************************/
1018
1019 void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
1020 {
1021         char *name_end;
1022         char *namelist;
1023         char *nameptr;
1024         int num_entries = 0;
1025         int i;
1026
1027         (*ppname_array) = NULL;
1028
1029         if((namelist_in == NULL ) || ((namelist_in != NULL) && (*namelist_in == '\0'))) 
1030                 return;
1031
1032         namelist = talloc_strdup(talloc_tos(), namelist_in);
1033         if (namelist == NULL) {
1034                 DEBUG(0,("set_namearray: talloc fail\n"));
1035                 return;
1036         }
1037         nameptr = namelist;
1038
1039         /* We need to make two passes over the string. The
1040                 first to count the number of elements, the second
1041                 to split it.
1042         */
1043
1044         while(*nameptr) {
1045                 if ( *nameptr == '/' ) {
1046                         /* cope with multiple (useless) /s) */
1047                         nameptr++;
1048                         continue;
1049                 }
1050                 /* anything left? */
1051                 if ( *nameptr == '\0' )
1052                         break;
1053
1054                 /* find the next '/' or consume remaining */
1055                 name_end = strchr_m(nameptr, '/');
1056                 if (name_end == NULL)
1057                         name_end = (char *)nameptr + strlen(nameptr);
1058
1059                 /* next segment please */
1060                 nameptr = name_end + 1;
1061                 num_entries++;
1062         }
1063
1064         if(num_entries == 0) {
1065                 talloc_free(namelist);
1066                 return;
1067         }
1068
1069         if(( (*ppname_array) = SMB_MALLOC_ARRAY(name_compare_entry, num_entries + 1)) == NULL) {
1070                 DEBUG(0,("set_namearray: malloc fail\n"));
1071                 talloc_free(namelist);
1072                 return;
1073         }
1074
1075         /* Now copy out the names */
1076         nameptr = namelist;
1077         i = 0;
1078         while(*nameptr) {
1079                 if ( *nameptr == '/' ) {
1080                         /* cope with multiple (useless) /s) */
1081                         nameptr++;
1082                         continue;
1083                 }
1084                 /* anything left? */
1085                 if ( *nameptr == '\0' )
1086                         break;
1087
1088                 /* find the next '/' or consume remaining */
1089                 name_end = strchr_m(nameptr, '/');
1090                 if (name_end)
1091                         *name_end = '\0';
1092                 else
1093                         name_end = nameptr + strlen(nameptr);
1094
1095                 (*ppname_array)[i].is_wild = ms_has_wild(nameptr);
1096                 if(((*ppname_array)[i].name = SMB_STRDUP(nameptr)) == NULL) {
1097                         DEBUG(0,("set_namearray: malloc fail (1)\n"));
1098                         talloc_free(namelist);
1099                         return;
1100                 }
1101
1102                 /* next segment please */
1103                 nameptr = name_end + 1;
1104                 i++;
1105         }
1106
1107         (*ppname_array)[i].name = NULL;
1108
1109         talloc_free(namelist);
1110         return;
1111 }
1112
1113 #undef DBGC_CLASS
1114 #define DBGC_CLASS DBGC_LOCKING
1115
1116 /****************************************************************************
1117  Simple routine to query existing file locks. Cruft in NFS and 64->32 bit mapping
1118  is dealt with in posix.c
1119  Returns True if we have information regarding this lock region (and returns
1120  F_UNLCK in *ptype if the region is unlocked). False if the call failed.
1121 ****************************************************************************/
1122
1123 bool fcntl_getlock(int fd, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
1124 {
1125         SMB_STRUCT_FLOCK lock;
1126         int ret;
1127
1128         DEBUG(8,("fcntl_getlock fd=%d offset=%.0f count=%.0f type=%d\n",
1129                     fd,(double)*poffset,(double)*pcount,*ptype));
1130
1131         lock.l_type = *ptype;
1132         lock.l_whence = SEEK_SET;
1133         lock.l_start = *poffset;
1134         lock.l_len = *pcount;
1135         lock.l_pid = 0;
1136
1137         ret = sys_fcntl_ptr(fd,SMB_F_GETLK,&lock);
1138
1139         if (ret == -1) {
1140                 int sav = errno;
1141                 DEBUG(3,("fcntl_getlock: lock request failed at offset %.0f count %.0f type %d (%s)\n",
1142                         (double)*poffset,(double)*pcount,*ptype,strerror(errno)));
1143                 errno = sav;
1144                 return False;
1145         }
1146
1147         *ptype = lock.l_type;
1148         *poffset = lock.l_start;
1149         *pcount = lock.l_len;
1150         *ppid = lock.l_pid;
1151
1152         DEBUG(3,("fcntl_getlock: fd %d is returned info %d pid %u\n",
1153                         fd, (int)lock.l_type, (unsigned int)lock.l_pid));
1154         return True;
1155 }
1156
1157 #undef DBGC_CLASS
1158 #define DBGC_CLASS DBGC_ALL
1159
1160 /*******************************************************************
1161  Is the name specified one of my netbios names.
1162  Returns true if it is equal, false otherwise.
1163 ********************************************************************/
1164
1165 bool is_myname(const char *s)
1166 {
1167         int n;
1168         bool ret = False;
1169
1170         for (n=0; my_netbios_names(n); n++) {
1171                 if (strequal(my_netbios_names(n), s)) {
1172                         ret=True;
1173                         break;
1174                 }
1175         }
1176         DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret));
1177         return(ret);
1178 }
1179
1180 /*******************************************************************
1181  Is the name specified our workgroup/domain.
1182  Returns true if it is equal, false otherwise.
1183 ********************************************************************/
1184
1185 bool is_myworkgroup(const char *s)
1186 {
1187         bool ret = False;
1188
1189         if (strequal(s, lp_workgroup())) {
1190                 ret=True;
1191         }
1192
1193         DEBUG(8, ("is_myworkgroup(\"%s\") returns %d\n", s, ret));
1194         return(ret);
1195 }
1196
1197 /*******************************************************************
1198  we distinguish between 2K and XP by the "Native Lan Manager" string
1199    WinXP => "Windows 2002 5.1"
1200    WinXP 64bit => "Windows XP 5.2"
1201    Win2k => "Windows 2000 5.0"
1202    NT4   => "Windows NT 4.0"
1203    Win9x => "Windows 4.0"
1204  Windows 2003 doesn't set the native lan manager string but
1205  they do set the domain to "Windows 2003 5.2" (probably a bug).
1206 ********************************************************************/
1207
1208 void ra_lanman_string( const char *native_lanman )
1209 {
1210         if ( strcmp( native_lanman, "Windows 2002 5.1" ) == 0 )
1211                 set_remote_arch( RA_WINXP );
1212         else if ( strcmp( native_lanman, "Windows XP 5.2" ) == 0 )
1213                 set_remote_arch( RA_WINXP64 );
1214         else if ( strcmp( native_lanman, "Windows Server 2003 5.2" ) == 0 )
1215                 set_remote_arch( RA_WIN2K3 );
1216 }
1217
1218 static const char *remote_arch_str;
1219
1220 const char *get_remote_arch_str(void)
1221 {
1222         if (!remote_arch_str) {
1223                 return "UNKNOWN";
1224         }
1225         return remote_arch_str;
1226 }
1227
1228 /*******************************************************************
1229  Set the horrid remote_arch string based on an enum.
1230 ********************************************************************/
1231
1232 void set_remote_arch(enum remote_arch_types type)
1233 {
1234         ra_type = type;
1235         switch( type ) {
1236         case RA_WFWG:
1237                 remote_arch_str = "WfWg";
1238                 break;
1239         case RA_OS2:
1240                 remote_arch_str = "OS2";
1241                 break;
1242         case RA_WIN95:
1243                 remote_arch_str = "Win95";
1244                 break;
1245         case RA_WINNT:
1246                 remote_arch_str = "WinNT";
1247                 break;
1248         case RA_WIN2K:
1249                 remote_arch_str = "Win2K";
1250                 break;
1251         case RA_WINXP:
1252                 remote_arch_str = "WinXP";
1253                 break;
1254         case RA_WINXP64:
1255                 remote_arch_str = "WinXP64";
1256                 break;
1257         case RA_WIN2K3:
1258                 remote_arch_str = "Win2K3";
1259                 break;
1260         case RA_VISTA:
1261                 remote_arch_str = "Vista";
1262                 break;
1263         case RA_SAMBA:
1264                 remote_arch_str = "Samba";
1265                 break;
1266         case RA_CIFSFS:
1267                 remote_arch_str = "CIFSFS";
1268                 break;
1269         case RA_OSX:
1270                 remote_arch_str = "OSX";
1271                 break;
1272         default:
1273                 ra_type = RA_UNKNOWN;
1274                 remote_arch_str = "UNKNOWN";
1275                 break;
1276         }
1277
1278         DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n",
1279                                 remote_arch_str));
1280 }
1281
1282 /*******************************************************************
1283  Get the remote_arch type.
1284 ********************************************************************/
1285
1286 enum remote_arch_types get_remote_arch(void)
1287 {
1288         return ra_type;
1289 }
1290
1291 const char *tab_depth(int level, int depth)
1292 {
1293         if( CHECK_DEBUGLVL(level) ) {
1294                 dbgtext("%*s", depth*4, "");
1295         }
1296         return "";
1297 }
1298
1299 /*****************************************************************************
1300  Provide a checksum on a string
1301
1302  Input:  s - the null-terminated character string for which the checksum
1303              will be calculated.
1304
1305   Output: The checksum value calculated for s.
1306 *****************************************************************************/
1307
1308 int str_checksum(const char *s)
1309 {
1310         if (s == NULL)
1311                 return 0;
1312         return hash(s, strlen(s), 0);
1313 }
1314
1315 /*****************************************************************
1316  Zero a memory area then free it. Used to catch bugs faster.
1317 *****************************************************************/  
1318
1319 void zero_free(void *p, size_t size)
1320 {
1321         memset(p, 0, size);
1322         SAFE_FREE(p);
1323 }
1324
1325 /*****************************************************************
1326  Set our open file limit to a requested max and return the limit.
1327 *****************************************************************/  
1328
1329 int set_maxfiles(int requested_max)
1330 {
1331 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
1332         struct rlimit rlp;
1333         int saved_current_limit;
1334
1335         if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1336                 DEBUG(0,("set_maxfiles: getrlimit (1) for RLIMIT_NOFILE failed with error %s\n",
1337                         strerror(errno) ));
1338                 /* just guess... */
1339                 return requested_max;
1340         }
1341
1342         /* 
1343          * Set the fd limit to be real_max_open_files + MAX_OPEN_FUDGEFACTOR to
1344          * account for the extra fd we need 
1345          * as well as the log files and standard
1346          * handles etc. Save the limit we want to set in case
1347          * we are running on an OS that doesn't support this limit (AIX)
1348          * which always returns RLIM_INFINITY for rlp.rlim_max.
1349          */
1350
1351         /* Try raising the hard (max) limit to the requested amount. */
1352
1353 #if defined(RLIM_INFINITY)
1354         if (rlp.rlim_max != RLIM_INFINITY) {
1355                 int orig_max = rlp.rlim_max;
1356
1357                 if ( rlp.rlim_max < requested_max )
1358                         rlp.rlim_max = requested_max;
1359
1360                 /* This failing is not an error - many systems (Linux) don't
1361                         support our default request of 10,000 open files. JRA. */
1362
1363                 if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1364                         DEBUG(3,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d max files failed with error %s\n", 
1365                                 (int)rlp.rlim_max, strerror(errno) ));
1366
1367                         /* Set failed - restore original value from get. */
1368                         rlp.rlim_max = orig_max;
1369                 }
1370         }
1371 #endif
1372
1373         /* Now try setting the soft (current) limit. */
1374
1375         saved_current_limit = rlp.rlim_cur = MIN(requested_max,rlp.rlim_max);
1376
1377         if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1378                 DEBUG(0,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d files failed with error %s\n", 
1379                         (int)rlp.rlim_cur, strerror(errno) ));
1380                 /* just guess... */
1381                 return saved_current_limit;
1382         }
1383
1384         if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1385                 DEBUG(0,("set_maxfiles: getrlimit (2) for RLIMIT_NOFILE failed with error %s\n",
1386                         strerror(errno) ));
1387                 /* just guess... */
1388                 return saved_current_limit;
1389     }
1390
1391 #if defined(RLIM_INFINITY)
1392         if(rlp.rlim_cur == RLIM_INFINITY)
1393                 return saved_current_limit;
1394 #endif
1395
1396         if((int)rlp.rlim_cur > saved_current_limit)
1397                 return saved_current_limit;
1398
1399         return rlp.rlim_cur;
1400 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
1401         /*
1402          * No way to know - just guess...
1403          */
1404         return requested_max;
1405 #endif
1406 }
1407
1408 /*****************************************************************
1409  malloc that aborts with smb_panic on fail or zero size.
1410  *****************************************************************/  
1411
1412 void *smb_xmalloc_array(size_t size, unsigned int count)
1413 {
1414         void *p;
1415         if (size == 0) {
1416                 smb_panic("smb_xmalloc_array: called with zero size");
1417         }
1418         if (count >= MAX_ALLOC_SIZE/size) {
1419                 smb_panic("smb_xmalloc_array: alloc size too large");
1420         }
1421         if ((p = SMB_MALLOC(size*count)) == NULL) {
1422                 DEBUG(0, ("smb_xmalloc_array failed to allocate %lu * %lu bytes\n",
1423                         (unsigned long)size, (unsigned long)count));
1424                 smb_panic("smb_xmalloc_array: malloc failed");
1425         }
1426         return p;
1427 }
1428
1429 /*
1430   vasprintf that aborts on malloc fail
1431 */
1432
1433  int smb_xvasprintf(char **ptr, const char *format, va_list ap)
1434 {
1435         int n;
1436         va_list ap2;
1437
1438         va_copy(ap2, ap);
1439
1440         n = vasprintf(ptr, format, ap2);
1441         va_end(ap2);
1442         if (n == -1 || ! *ptr) {
1443                 smb_panic("smb_xvasprintf: out of memory");
1444         }
1445         return n;
1446 }
1447
1448 /*****************************************************************
1449  Get local hostname and cache result.
1450 *****************************************************************/
1451
1452 char *myhostname(void)
1453 {
1454         static char *ret;
1455         if (ret == NULL) {
1456                 ret = get_myname(NULL);
1457         }
1458         return ret;
1459 }
1460
1461 /*****************************************************************
1462  Get local hostname and cache result.
1463 *****************************************************************/
1464
1465 char *myhostname_upper(void)
1466 {
1467         char *name;
1468         static char *ret;
1469         if (ret == NULL) {
1470                 name = get_myname(talloc_tos());
1471                 ret = strupper_talloc(NULL, name);
1472                 talloc_free(name);
1473         }
1474         return ret;
1475 }
1476
1477 /**
1478  * @brief Returns an absolute path to a file concatenating the provided
1479  * @a rootpath and @a basename
1480  *
1481  * @param name Filename, relative to @a rootpath
1482  *
1483  * @retval Pointer to a string containing the full path.
1484  **/
1485
1486 static char *xx_path(const char *name, const char *rootpath)
1487 {
1488         char *fname = NULL;
1489
1490         fname = talloc_strdup(talloc_tos(), rootpath);
1491         if (!fname) {
1492                 return NULL;
1493         }
1494         trim_string(fname,"","/");
1495
1496         if (!directory_exist(fname)) {
1497                 if (!mkdir(fname,0755))
1498                         DEBUG(1, ("Unable to create directory %s for file %s. "
1499                               "Error was %s\n", fname, name, strerror(errno)));
1500         }
1501
1502         return talloc_asprintf(talloc_tos(),
1503                                 "%s/%s",
1504                                 fname,
1505                                 name);
1506 }
1507
1508 /**
1509  * @brief Returns an absolute path to a file in the Samba lock directory.
1510  *
1511  * @param name File to find, relative to LOCKDIR.
1512  *
1513  * @retval Pointer to a talloc'ed string containing the full path.
1514  **/
1515
1516 char *lock_path(const char *name)
1517 {
1518         return xx_path(name, lp_lockdir());
1519 }
1520
1521 /**
1522  * @brief Returns an absolute path to a file in the Samba pid directory.
1523  *
1524  * @param name File to find, relative to PIDDIR.
1525  *
1526  * @retval Pointer to a talloc'ed string containing the full path.
1527  **/
1528
1529 char *pid_path(const char *name)
1530 {
1531         return xx_path(name, lp_piddir());
1532 }
1533
1534 /**
1535  * @brief Returns an absolute path to a file in the Samba state directory.
1536  *
1537  * @param name File to find, relative to STATEDIR.
1538  *
1539  * @retval Pointer to a talloc'ed string containing the full path.
1540  **/
1541
1542 char *state_path(const char *name)
1543 {
1544         return xx_path(name, lp_statedir());
1545 }
1546
1547 /**
1548  * @brief Returns an absolute path to a file in the Samba cache directory.
1549  *
1550  * @param name File to find, relative to CACHEDIR.
1551  *
1552  * @retval Pointer to a talloc'ed string containing the full path.
1553  **/
1554
1555 char *cache_path(const char *name)
1556 {
1557         return xx_path(name, lp_cachedir());
1558 }
1559
1560 /*******************************************************************
1561  Given a filename - get its directory name
1562 ********************************************************************/
1563
1564 bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
1565                     const char **name)
1566 {
1567         char *p;
1568         ptrdiff_t len;
1569
1570         p = strrchr_m(dir, '/'); /* Find final '/', if any */
1571
1572         if (p == NULL) {
1573                 if (!(*parent = talloc_strdup(mem_ctx, "."))) {
1574                         return False;
1575                 }
1576                 if (name) {
1577                         *name = dir;
1578                 }
1579                 return True;
1580         }
1581
1582         len = p-dir;
1583
1584         if (!(*parent = (char *)talloc_memdup(mem_ctx, dir, len+1))) {
1585                 return False;
1586         }
1587         (*parent)[len] = '\0';
1588
1589         if (name) {
1590                 *name = p+1;
1591         }
1592         return True;
1593 }
1594
1595 /*******************************************************************
1596  Determine if a pattern contains any Microsoft wildcard characters.
1597 *******************************************************************/
1598
1599 bool ms_has_wild(const char *s)
1600 {
1601         char c;
1602
1603         if (lp_posix_pathnames()) {
1604                 /* With posix pathnames no characters are wild. */
1605                 return False;
1606         }
1607
1608         while ((c = *s++)) {
1609                 switch (c) {
1610                 case '*':
1611                 case '?':
1612                 case '<':
1613                 case '>':
1614                 case '"':
1615                         return True;
1616                 }
1617         }
1618         return False;
1619 }
1620
1621 bool ms_has_wild_w(const smb_ucs2_t *s)
1622 {
1623         smb_ucs2_t c;
1624         if (!s) return False;
1625         while ((c = *s++)) {
1626                 switch (c) {
1627                 case UCS2_CHAR('*'):
1628                 case UCS2_CHAR('?'):
1629                 case UCS2_CHAR('<'):
1630                 case UCS2_CHAR('>'):
1631                 case UCS2_CHAR('"'):
1632                         return True;
1633                 }
1634         }
1635         return False;
1636 }
1637
1638 /*******************************************************************
1639  A wrapper that handles case sensitivity and the special handling
1640  of the ".." name.
1641 *******************************************************************/
1642
1643 bool mask_match(const char *string, const char *pattern, bool is_case_sensitive)
1644 {
1645         if (ISDOTDOT(string))
1646                 string = ".";
1647         if (ISDOT(pattern))
1648                 return False;
1649
1650         return ms_fnmatch(pattern, string, Protocol <= PROTOCOL_LANMAN2, is_case_sensitive) == 0;
1651 }
1652
1653 /*******************************************************************
1654  A wrapper that handles case sensitivity and the special handling
1655  of the ".." name. Varient that is only called by old search code which requires
1656  pattern translation.
1657 *******************************************************************/
1658
1659 bool mask_match_search(const char *string, const char *pattern, bool is_case_sensitive)
1660 {
1661         if (ISDOTDOT(string))
1662                 string = ".";
1663         if (ISDOT(pattern))
1664                 return False;
1665
1666         return ms_fnmatch(pattern, string, True, is_case_sensitive) == 0;
1667 }
1668
1669 /*******************************************************************
1670  A wrapper that handles a list of patters and calls mask_match()
1671  on each.  Returns True if any of the patterns match.
1672 *******************************************************************/
1673
1674 bool mask_match_list(const char *string, char **list, int listLen, bool is_case_sensitive)
1675 {
1676        while (listLen-- > 0) {
1677                if (mask_match(string, *list++, is_case_sensitive))
1678                        return True;
1679        }
1680        return False;
1681 }
1682
1683 /*********************************************************
1684  Recursive routine that is called by unix_wild_match.
1685 *********************************************************/
1686
1687 static bool unix_do_match(const char *regexp, const char *str)
1688 {
1689         const char *p;
1690
1691         for( p = regexp; *p && *str; ) {
1692
1693                 switch(*p) {
1694                         case '?':
1695                                 str++;
1696                                 p++;
1697                                 break;
1698
1699                         case '*':
1700
1701                                 /*
1702                                  * Look for a character matching 
1703                                  * the one after the '*'.
1704                                  */
1705                                 p++;
1706                                 if(!*p)
1707                                         return true; /* Automatic match */
1708                                 while(*str) {
1709
1710                                         while(*str && (*p != *str))
1711                                                 str++;
1712
1713                                         /*
1714                                          * Patch from weidel@multichart.de. In the case of the regexp
1715                                          * '*XX*' we want to ensure there are at least 2 'X' characters
1716                                          * in the string after the '*' for a match to be made.
1717                                          */
1718
1719                                         {
1720                                                 int matchcount=0;
1721
1722                                                 /*
1723                                                  * Eat all the characters that match, but count how many there were.
1724                                                  */
1725
1726                                                 while(*str && (*p == *str)) {
1727                                                         str++;
1728                                                         matchcount++;
1729                                                 }
1730
1731                                                 /*
1732                                                  * Now check that if the regexp had n identical characters that
1733                                                  * matchcount had at least that many matches.
1734                                                  */
1735
1736                                                 while ( *(p+1) && (*(p+1) == *p)) {
1737                                                         p++;
1738                                                         matchcount--;
1739                                                 }
1740
1741                                                 if ( matchcount <= 0 )
1742                                                         return false;
1743                                         }
1744
1745                                         str--; /* We've eaten the match char after the '*' */
1746
1747                                         if(unix_do_match(p, str))
1748                                                 return true;
1749
1750                                         if(!*str)
1751                                                 return false;
1752                                         else
1753                                                 str++;
1754                                 }
1755                                 return false;
1756
1757                         default:
1758                                 if(*str != *p)
1759                                         return false;
1760                                 str++;
1761                                 p++;
1762                                 break;
1763                 }
1764         }
1765
1766         if(!*p && !*str)
1767                 return true;
1768
1769         if (!*p && str[0] == '.' && str[1] == 0)
1770                 return true;
1771
1772         if (!*str && *p == '?') {
1773                 while (*p == '?')
1774                         p++;
1775                 return(!*p);
1776         }
1777
1778         if(!*str && (*p == '*' && p[1] == '\0'))
1779                 return true;
1780
1781         return false;
1782 }
1783
1784 /*******************************************************************
1785  Simple case insensitive interface to a UNIX wildcard matcher.
1786  Returns True if match, False if not.
1787 *******************************************************************/
1788
1789 bool unix_wild_match(const char *pattern, const char *string)
1790 {
1791         TALLOC_CTX *ctx = talloc_stackframe();
1792         char *p2;
1793         char *s2;
1794         char *p;
1795         bool ret = false;
1796
1797         p2 = talloc_strdup(ctx,pattern);
1798         s2 = talloc_strdup(ctx,string);
1799         if (!p2 || !s2) {
1800                 TALLOC_FREE(ctx);
1801                 return false;
1802         }
1803         strlower_m(p2);
1804         strlower_m(s2);
1805
1806         /* Remove any *? and ** from the pattern as they are meaningless */
1807         for(p = p2; *p; p++) {
1808                 while( *p == '*' && (p[1] == '?' ||p[1] == '*')) {
1809                         memmove(&p[1], &p[2], strlen(&p[2])+1);
1810                 }
1811         }
1812
1813         if (strequal(p2,"*")) {
1814                 TALLOC_FREE(ctx);
1815                 return true;
1816         }
1817
1818         ret = unix_do_match(p2, s2);
1819         TALLOC_FREE(ctx);
1820         return ret;
1821 }
1822
1823 /**********************************************************************
1824  Converts a name to a fully qualified domain name.
1825  Returns true if lookup succeeded, false if not (then fqdn is set to name)
1826  Note we deliberately use gethostbyname here, not getaddrinfo as we want
1827  to examine the h_aliases and I don't know how to do that with getaddrinfo.
1828 ***********************************************************************/
1829
1830 bool name_to_fqdn(fstring fqdn, const char *name)
1831 {
1832         char *full = NULL;
1833         struct hostent *hp = gethostbyname(name);
1834
1835         if (!hp || !hp->h_name || !*hp->h_name) {
1836                 DEBUG(10,("name_to_fqdn: lookup for %s failed.\n", name));
1837                 fstrcpy(fqdn, name);
1838                 return false;
1839         }
1840
1841         /* Find out if the fqdn is returned as an alias
1842          * to cope with /etc/hosts files where the first
1843          * name is not the fqdn but the short name */
1844         if (hp->h_aliases && (! strchr_m(hp->h_name, '.'))) {
1845                 int i;
1846                 for (i = 0; hp->h_aliases[i]; i++) {
1847                         if (strchr_m(hp->h_aliases[i], '.')) {
1848                                 full = hp->h_aliases[i];
1849                                 break;
1850                         }
1851                 }
1852         }
1853         if (full && (strcasecmp_m(full, "localhost.localdomain") == 0)) {
1854                 DEBUG(1, ("WARNING: your /etc/hosts file may be broken!\n"));
1855                 DEBUGADD(1, ("    Specifing the machine hostname for address 127.0.0.1 may lead\n"));
1856                 DEBUGADD(1, ("    to Kerberos authentication problems as localhost.localdomain\n"));
1857                 DEBUGADD(1, ("    may end up being used instead of the real machine FQDN.\n"));
1858                 full = hp->h_name;
1859         }
1860         if (!full) {
1861                 full = hp->h_name;
1862         }
1863
1864         DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name, full));
1865         fstrcpy(fqdn, full);
1866         return true;
1867 }
1868
1869 /**********************************************************************
1870  Append a DATA_BLOB to a talloc'ed object
1871 ***********************************************************************/
1872
1873 void *talloc_append_blob(TALLOC_CTX *mem_ctx, void *buf, DATA_BLOB blob)
1874 {
1875         size_t old_size = 0;
1876         char *result;
1877
1878         if (blob.length == 0) {
1879                 return buf;
1880         }
1881
1882         if (buf != NULL) {
1883                 old_size = talloc_get_size(buf);
1884         }
1885
1886         result = (char *)TALLOC_REALLOC(mem_ctx, buf, old_size + blob.length);
1887         if (result == NULL) {
1888                 return NULL;
1889         }
1890
1891         memcpy(result + old_size, blob.data, blob.length);
1892         return result;
1893 }
1894
1895 uint32 map_share_mode_to_deny_mode(uint32 share_access, uint32 private_options)
1896 {
1897         switch (share_access & ~FILE_SHARE_DELETE) {
1898                 case FILE_SHARE_NONE:
1899                         return DENY_ALL;
1900                 case FILE_SHARE_READ:
1901                         return DENY_WRITE;
1902                 case FILE_SHARE_WRITE:
1903                         return DENY_READ;
1904                 case FILE_SHARE_READ|FILE_SHARE_WRITE:
1905                         return DENY_NONE;
1906         }
1907         if (private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) {
1908                 return DENY_DOS;
1909         } else if (private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_FCB) {
1910                 return DENY_FCB;
1911         }
1912
1913         return (uint32)-1;
1914 }
1915
1916 pid_t procid_to_pid(const struct server_id *proc)
1917 {
1918         return proc->pid;
1919 }
1920
1921 static uint32 my_vnn = NONCLUSTER_VNN;
1922
1923 void set_my_vnn(uint32 vnn)
1924 {
1925         DEBUG(10, ("vnn pid %d = %u\n", (int)sys_getpid(), (unsigned int)vnn));
1926         my_vnn = vnn;
1927 }
1928
1929 uint32 get_my_vnn(void)
1930 {
1931         return my_vnn;
1932 }
1933
1934 static uint64_t my_unique_id = 0;
1935
1936 void set_my_unique_id(uint64_t unique_id)
1937 {
1938         my_unique_id = unique_id;
1939 }
1940
1941 struct server_id pid_to_procid(pid_t pid)
1942 {
1943         struct server_id result;
1944         result.pid = pid;
1945         result.task_id = 0;
1946         result.unique_id = my_unique_id;
1947         result.vnn = my_vnn;
1948         return result;
1949 }
1950
1951 struct server_id procid_self(void)
1952 {
1953         return pid_to_procid(sys_getpid());
1954 }
1955
1956 bool procid_equal(const struct server_id *p1, const struct server_id *p2)
1957 {
1958         if (p1->pid != p2->pid)
1959                 return False;
1960         if (p1->task_id != p2->task_id)
1961                 return False;
1962         if (p1->vnn != p2->vnn)
1963                 return False;
1964         return True;
1965 }
1966
1967 bool cluster_id_equal(const struct server_id *id1,
1968                       const struct server_id *id2)
1969 {
1970         return procid_equal(id1, id2);
1971 }
1972
1973 bool procid_is_me(const struct server_id *pid)
1974 {
1975         if (pid->pid != sys_getpid())
1976                 return False;
1977         if (pid->task_id != 0)
1978                 return False;
1979         if (pid->vnn != my_vnn)
1980                 return False;
1981         return True;
1982 }
1983
1984 struct server_id interpret_pid(const char *pid_string)
1985 {
1986         struct server_id result;
1987         unsigned long long pid;
1988         unsigned int vnn, task_id = 0;
1989
1990         ZERO_STRUCT(result);
1991
1992         /* We accept various forms with 1, 2 or 3 component forms
1993          * because the server_id_str() can print different forms, and
1994          * we want backwards compatibility for scripts that may call
1995          * smbclient. */
1996         if (sscanf(pid_string, "%u:%llu.%u", &vnn, &pid, &task_id) == 3) {
1997                 result.vnn = vnn;
1998                 result.pid = pid;
1999                 result.task_id = task_id;
2000         } else if (sscanf(pid_string, "%u:%llu", &vnn, &pid) == 2) {
2001                 result.vnn = vnn;
2002                 result.pid = pid;
2003                 result.task_id = 0;
2004         } else if (sscanf(pid_string, "%llu.%u", &pid, &task_id) == 2) {
2005                 result.vnn = get_my_vnn();
2006                 result.pid = pid;
2007                 result.task_id = task_id;
2008         } else if (sscanf(pid_string, "%llu", &pid) == 1) {
2009                 result.vnn = get_my_vnn();
2010                 result.pid = pid;
2011         } else {
2012                 result.vnn = NONCLUSTER_VNN;
2013                 result.pid = (uint64_t)-1;
2014         }
2015         return result;
2016 }
2017
2018 char *procid_str_static(const struct server_id *pid)
2019 {
2020         return server_id_str(talloc_tos(), pid);
2021 }
2022
2023 bool procid_valid(const struct server_id *pid)
2024 {
2025         return (pid->pid != (uint64_t)-1);
2026 }
2027
2028 bool procid_is_local(const struct server_id *pid)
2029 {
2030         return pid->vnn == my_vnn;
2031 }
2032
2033 /****************************************************************
2034  Check if offset/length fit into bufsize. Should probably be
2035  merged with is_offset_safe, but this would require a rewrite
2036  of lanman.c. Later :-)
2037 ****************************************************************/
2038
2039 bool trans_oob(uint32_t bufsize, uint32_t offset, uint32_t length)
2040 {
2041         if ((offset + length < offset) || (offset + length < length)) {
2042                 /* wrap */
2043                 return true;
2044         }
2045         if ((offset > bufsize) || (offset + length > bufsize)) {
2046                 /* overflow */
2047                 return true;
2048         }
2049         return false;
2050 }
2051
2052 /****************************************************************
2053  Check if an offset into a buffer is safe.
2054  If this returns True it's safe to indirect into the byte at
2055  pointer ptr+off.
2056 ****************************************************************/
2057
2058 bool is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
2059 {
2060         const char *end_base = buf_base + buf_len;
2061         char *end_ptr = ptr + off;
2062
2063         if (!buf_base || !ptr) {
2064                 return False;
2065         }
2066
2067         if (end_base < buf_base || end_ptr < ptr) {
2068                 return False; /* wrap. */
2069         }
2070
2071         if (end_ptr < end_base) {
2072                 return True;
2073         }
2074         return False;
2075 }
2076
2077 /****************************************************************
2078  Return a safe pointer into a buffer, or NULL.
2079 ****************************************************************/
2080
2081 char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
2082 {
2083         return is_offset_safe(buf_base, buf_len, ptr, off) ?
2084                         ptr + off : NULL;
2085 }
2086
2087 /****************************************************************
2088  Return a safe pointer into a string within a buffer, or NULL.
2089 ****************************************************************/
2090
2091 char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
2092 {
2093         if (!is_offset_safe(buf_base, buf_len, ptr, off)) {
2094                 return NULL;
2095         }
2096         /* Check if a valid string exists at this offset. */
2097         if (skip_string(buf_base,buf_len, ptr + off) == NULL) {
2098                 return NULL;
2099         }
2100         return ptr + off;
2101 }
2102
2103 /****************************************************************
2104  Return an SVAL at a pointer, or failval if beyond the end.
2105 ****************************************************************/
2106
2107 int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
2108 {
2109         /*
2110          * Note we use off+1 here, not off+2 as SVAL accesses ptr[0] and ptr[1],
2111          * NOT ptr[2].
2112          */
2113         if (!is_offset_safe(buf_base, buf_len, ptr, off+1)) {
2114                 return failval;
2115         }
2116         return SVAL(ptr,off);
2117 }
2118
2119 /****************************************************************
2120  Return an IVAL at a pointer, or failval if beyond the end.
2121 ****************************************************************/
2122
2123 int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
2124 {
2125         /*
2126          * Note we use off+3 here, not off+4 as IVAL accesses 
2127          * ptr[0] ptr[1] ptr[2] ptr[3] NOT ptr[4].
2128          */
2129         if (!is_offset_safe(buf_base, buf_len, ptr, off+3)) {
2130                 return failval;
2131         }
2132         return IVAL(ptr,off);
2133 }
2134
2135 /****************************************************************
2136  Split DOM\user into DOM and user. Do not mix with winbind variants of that
2137  call (they take care of winbind separator and other winbind specific settings).
2138 ****************************************************************/
2139
2140 void split_domain_user(TALLOC_CTX *mem_ctx,
2141                        const char *full_name,
2142                        char **domain,
2143                        char **user)
2144 {
2145         const char *p = NULL;
2146
2147         p = strchr_m(full_name, '\\');
2148
2149         if (p != NULL) {
2150                 *domain = talloc_strndup(mem_ctx, full_name,
2151                                          PTR_DIFF(p, full_name));
2152                 *user = talloc_strdup(mem_ctx, p+1);
2153         } else {
2154                 *domain = talloc_strdup(mem_ctx, "");
2155                 *user = talloc_strdup(mem_ctx, full_name);
2156         }
2157 }
2158
2159 /****************************************************************
2160  strip off leading '\\' from a hostname
2161 ****************************************************************/
2162
2163 const char *strip_hostname(const char *s)
2164 {
2165         if (!s) {
2166                 return NULL;
2167         }
2168
2169         if (strlen_m(s) < 3) {
2170                 return s;
2171         }
2172
2173         if (s[0] == '\\') s++;
2174         if (s[0] == '\\') s++;
2175
2176         return s;
2177 }
2178
2179 bool tevent_req_poll_ntstatus(struct tevent_req *req,
2180                               struct tevent_context *ev,
2181                               NTSTATUS *status)
2182 {
2183         bool ret = tevent_req_poll(req, ev);
2184         if (!ret) {
2185                 *status = map_nt_error_from_unix(errno);
2186         }
2187         return ret;
2188 }
2189
2190 bool any_nt_status_not_ok(NTSTATUS err1, NTSTATUS err2, NTSTATUS *result)
2191 {
2192         if (!NT_STATUS_IS_OK(err1)) {
2193                 *result = err1;
2194                 return true;
2195         }
2196         if (!NT_STATUS_IS_OK(err2)) {
2197                 *result = err2;
2198                 return true;
2199         }
2200         return false;
2201 }
2202
2203 int timeval_to_msec(struct timeval t)
2204 {
2205         return t.tv_sec * 1000 + (t.tv_usec+999) / 1000;
2206 }
2207
2208 /*******************************************************************
2209  Check a given DOS pathname is valid for a share.
2210 ********************************************************************/
2211
2212 char *valid_share_pathname(TALLOC_CTX *ctx, const char *dos_pathname)
2213 {
2214         char *ptr = NULL;
2215
2216         if (!dos_pathname) {
2217                 return NULL;
2218         }
2219
2220         ptr = talloc_strdup(ctx, dos_pathname);
2221         if (!ptr) {
2222                 return NULL;
2223         }
2224         /* Convert any '\' paths to '/' */
2225         unix_format(ptr);
2226         ptr = unix_clean_name(ctx, ptr);
2227         if (!ptr) {
2228                 return NULL;
2229         }
2230
2231         /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
2232         if (strlen(ptr) > 2 && ptr[1] == ':' && ptr[0] != '/')
2233                 ptr += 2;
2234
2235         /* Only absolute paths allowed. */
2236         if (*ptr != '/')
2237                 return NULL;
2238
2239         return ptr;
2240 }
2241
2242 /*******************************************************************
2243  Return True if the filename is one of the special executable types.
2244 ********************************************************************/
2245
2246 bool is_executable(const char *fname)
2247 {
2248         if ((fname = strrchr_m(fname,'.'))) {
2249                 if (strequal(fname,".com") ||
2250                     strequal(fname,".dll") ||
2251                     strequal(fname,".exe") ||
2252                     strequal(fname,".sym")) {
2253                         return True;
2254                 }
2255         }
2256         return False;
2257 }