LCOV - code coverage report
Current view: top level - source3/lib - util.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 612 871 70.3 %
Date: 2024-02-28 12:06:22 Functions: 63 73 86.3 %

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

Generated by: LCOV version 1.14