LCOV - code coverage report
Current view: top level - source3/lib - util.c (source / functions) Hit Total Coverage
Test: coverage report for recycleplus df22b230 Lines: 417 810 51.5 %
Date: 2024-02-14 10:14:15 Functions: 55 72 76.4 %

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

Generated by: LCOV version 1.14