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 1224887 : enum protocol_types get_Protocol(void)
58 : {
59 1224887 : return Protocol;
60 : }
61 :
62 30686 : void set_Protocol(enum protocol_types p)
63 : {
64 30686 : Protocol = p;
65 30686 : }
66 :
67 : static enum remote_arch_types ra_type = RA_UNKNOWN;
68 :
69 14437 : void gfree_all( void )
70 : {
71 14437 : gfree_loadparm();
72 14437 : gfree_charcnv();
73 14437 : gfree_interfaces();
74 14437 : gfree_debugsyms();
75 14437 : gfree_memcache();
76 :
77 14437 : }
78 :
79 : /*******************************************************************
80 : Check if a file exists - call vfs_file_exist for samba files.
81 : ********************************************************************/
82 :
83 1024 : 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 1024 : if (!sbuf)
88 0 : sbuf = &st;
89 :
90 1024 : if (sys_stat(fname, sbuf, fake_dir_create_times) != 0)
91 0 : return(False);
92 :
93 1024 : 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 2341046 : uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
114 : {
115 2341046 : return sbuf->st_ex_size;
116 : }
117 :
118 : /****************************************************************************
119 : Check two stats have identical dev and ino fields.
120 : ****************************************************************************/
121 :
122 292673 : bool check_same_dev_ino(const SMB_STRUCT_STAT *sbuf1,
123 : const SMB_STRUCT_STAT *sbuf2)
124 : {
125 584633 : return ((sbuf1->st_ex_dev == sbuf2->st_ex_dev) &&
126 292673 : (sbuf1->st_ex_ino == sbuf2->st_ex_ino));
127 : }
128 :
129 : /****************************************************************************
130 : Check if a stat struct is identical for use.
131 : ****************************************************************************/
132 :
133 2 : bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
134 : const SMB_STRUCT_STAT *sbuf2)
135 : {
136 4 : return ((sbuf1->st_ex_uid == sbuf2->st_ex_uid) &&
137 4 : (sbuf1->st_ex_gid == sbuf2->st_ex_gid) &&
138 2 : check_same_dev_ino(sbuf1, sbuf2));
139 : }
140 :
141 : /*******************************************************************
142 : Show a smb message structure.
143 : ********************************************************************/
144 :
145 1314676 : void show_msg(const char *buf)
146 : {
147 15942 : int i;
148 1314676 : int bcc=0;
149 :
150 1314676 : if (!DEBUGLVL(5))
151 1314676 : 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 95343 : int set_message_bcc(char *buf,int num_bytes)
190 : {
191 95343 : int num_words = CVAL(buf,smb_wct);
192 95343 : SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
193 95343 : _smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
194 95343 : 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 38982 : ssize_t message_push_blob(uint8_t **outbuf, DATA_BLOB blob)
203 : {
204 38982 : size_t newlen = smb_len(*outbuf) + 4 + blob.length;
205 266 : uint8_t *tmp;
206 :
207 38982 : if (!(tmp = talloc_realloc(NULL, *outbuf, uint8_t, newlen))) {
208 0 : DEBUG(0, ("talloc failed\n"));
209 0 : return -1;
210 : }
211 38982 : *outbuf = tmp;
212 :
213 38982 : memcpy(tmp + smb_len(tmp) + 4, blob.data, blob.length);
214 38982 : set_message_bcc((char *)tmp, smb_buflen(tmp) + blob.length);
215 38982 : return blob.length;
216 : }
217 :
218 : /*******************************************************************
219 : Reduce a file name, removing .. elements.
220 : ********************************************************************/
221 :
222 15618 : static char *dos_clean_name(TALLOC_CTX *ctx, const char *s)
223 : {
224 15618 : char *p = NULL;
225 15618 : char *str = NULL;
226 :
227 15618 : DEBUG(3,("dos_clean_name [%s]\n",s));
228 :
229 : /* remove any double slashes */
230 15618 : str = talloc_all_string_sub(ctx, s, "\\\\", "\\");
231 15618 : if (!str) {
232 0 : return NULL;
233 : }
234 :
235 : /* Remove leading .\\ characters */
236 15618 : 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 15680 : while ((p = strstr_m(str,"\\..\\")) != NULL) {
247 0 : char *s1;
248 :
249 62 : *p = 0;
250 62 : s1 = p+3;
251 :
252 62 : if ((p=strrchr_m(str,'\\')) != NULL) {
253 60 : *p = 0;
254 : } else {
255 2 : *str = 0;
256 : }
257 62 : str = talloc_asprintf(ctx,
258 : "%s%s",
259 : str,
260 : s1);
261 62 : if (!str) {
262 0 : return NULL;
263 : }
264 : }
265 :
266 15618 : trim_string(str,NULL,"\\..");
267 15618 : return talloc_all_string_sub(ctx, str, "\\.\\", "\\");
268 : }
269 :
270 : /*******************************************************************
271 : Reduce a file name, removing .. elements.
272 : ********************************************************************/
273 :
274 16056 : char *unix_clean_name(TALLOC_CTX *ctx, const char *s)
275 : {
276 16056 : char *p = NULL;
277 16056 : char *str = NULL;
278 :
279 16056 : DEBUG(3,("unix_clean_name [%s]\n",s));
280 :
281 : /* remove any double slashes */
282 16056 : str = talloc_all_string_sub(ctx, s, "//","/");
283 16056 : if (!str) {
284 0 : return NULL;
285 : }
286 :
287 : /* Remove leading ./ characters */
288 16056 : 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 16056 : 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 16056 : trim_string(str,NULL,"/..");
319 16056 : return talloc_all_string_sub(ctx, str, "/./", "/");
320 : }
321 :
322 15618 : char *clean_name(TALLOC_CTX *ctx, const char *s)
323 : {
324 15618 : char *str = dos_clean_name(ctx, s);
325 15618 : if (!str) {
326 0 : return NULL;
327 : }
328 15618 : 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 88 : NTSTATUS init_before_fork(void)
374 : {
375 0 : int ret;
376 :
377 88 : ret = pipe(reinit_after_fork_pipe);
378 88 : 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 88 : 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 12 : 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 12 : 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 12 : TALLOC_FREE(fde);
422 12 : (void)kill(getpid(), SIGTERM);
423 : }
424 12 : }
425 :
426 :
427 31623 : NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
428 : struct tevent_context *ev_ctx,
429 : bool parent_longlived)
430 : {
431 31623 : 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 31623 : per_thread_cwd_disable();
440 :
441 31623 : if (reinit_after_fork_pipe[1] != -1) {
442 31380 : close(reinit_after_fork_pipe[1]);
443 31380 : reinit_after_fork_pipe[1] = -1;
444 : }
445 :
446 : /* tdb needs special fork handling */
447 31623 : 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 31623 : 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 31623 : tevent_set_trace_callback(ev_ctx, NULL, NULL);
459 31623 : tevent_set_trace_fd_callback(ev_ctx, NULL, NULL);
460 31623 : tevent_set_trace_signal_callback(ev_ctx, NULL, NULL);
461 31623 : tevent_set_trace_timer_callback(ev_ctx, NULL, NULL);
462 31623 : tevent_set_trace_immediate_callback(ev_ctx, NULL, NULL);
463 31623 : tevent_set_trace_queue_callback(ev_ctx, NULL, NULL);
464 31623 : if (tevent_re_initialise(ev_ctx) != 0) {
465 0 : smb_panic(__location__ ": Failed to re-initialise event context");
466 : }
467 : }
468 :
469 31623 : if (reinit_after_fork_pipe[0] != -1) {
470 842 : struct tevent_fd *fde;
471 :
472 31380 : 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 31380 : if (fde == NULL) {
476 0 : smb_panic(__location__ ": Failed to add reinit_after_fork pipe event");
477 : }
478 : }
479 :
480 31623 : if (msg_ctx) {
481 : /*
482 : * For clustering, we need to re-init our ctdbd connection after the
483 : * fork
484 : */
485 31623 : status = messaging_reinit(msg_ctx);
486 31623 : if (!NT_STATUS_IS_OK(status)) {
487 0 : DEBUG(0,("messaging_reinit() failed: %s\n",
488 : nt_errstr(status)));
489 : }
490 :
491 31623 : 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 31623 : done:
503 31623 : 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 59647 : char *get_mydnsdomname(TALLOC_CTX *ctx)
566 : {
567 0 : const char *domname;
568 0 : char *p;
569 :
570 59647 : domname = get_mydnsfullname();
571 59647 : if (!domname) {
572 0 : return NULL;
573 : }
574 :
575 59647 : p = strchr_m(domname, '.');
576 59647 : if (p) {
577 59647 : p++;
578 59647 : return talloc_strdup(ctx, p);
579 : } else {
580 0 : return talloc_strdup(ctx, "");
581 : }
582 : }
583 :
584 62 : bool process_exists(const struct server_id pid)
585 : {
586 62 : return serverid_exists(&pid);
587 : }
588 :
589 : /*******************************************************************
590 : Convert a uid into a user name.
591 : ********************************************************************/
592 :
593 176 : const char *uidtoname(uid_t uid)
594 : {
595 176 : TALLOC_CTX *ctx = talloc_tos();
596 176 : char *name = NULL;
597 176 : struct passwd *pass = NULL;
598 :
599 176 : pass = getpwuid_alloc(ctx,uid);
600 176 : if (pass) {
601 46 : name = talloc_strdup(ctx,pass->pw_name);
602 46 : TALLOC_FREE(pass);
603 : } else {
604 130 : name = talloc_asprintf(ctx,
605 : "%ld",
606 : (long int)uid);
607 : }
608 176 : return name;
609 : }
610 :
611 : /*******************************************************************
612 : Convert a gid into a group name.
613 : ********************************************************************/
614 :
615 60 : char *gidtoname(gid_t gid)
616 : {
617 4 : struct group *grp;
618 :
619 60 : grp = getgrgid(gid);
620 60 : if (grp) {
621 60 : 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 197 : gid_t nametogid(const char *name)
659 : {
660 0 : struct group *grp;
661 0 : char *p;
662 0 : gid_t g;
663 :
664 197 : g = (gid_t)strtol(name, &p, 0);
665 197 : if ((p != name) && (*p == '\0'))
666 0 : return g;
667 :
668 197 : grp = getgrnam(name);
669 197 : if (grp)
670 197 : 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 6210240 : 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 6210240 : if ((namelist == NULL) || (namelist[0].name == NULL)) {
761 6186450 : return False;
762 : }
763 :
764 : /* Do not reject path components if namelist is set to '.*' */
765 504 : if (ISDOT(name) || ISDOTDOT(name)) {
766 70 : return false;
767 : }
768 :
769 434 : DEBUG(8, ("is_in_path: %s\n", name));
770 :
771 : /* Get the last component of the unix name. */
772 434 : last_component = strrchr_m(name, '/');
773 434 : if (!last_component) {
774 419 : last_component = name;
775 : } else {
776 29 : last_component++; /* Go past '/' */
777 : }
778 :
779 800 : for(; namelist->name != NULL; namelist++) {
780 453 : if(namelist->is_wild) {
781 427 : if (mask_match(last_component, namelist->name, case_sensitive)) {
782 76 : DEBUG(8,("is_in_path: mask match succeeded\n"));
783 76 : 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 347 : DEBUG(8,("is_in_path: match not found\n"));
794 342 : 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 88637 : 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 88637 : int num_entries = 0;
815 1437 : int i;
816 :
817 88637 : (*ppname_array) = NULL;
818 :
819 88637 : if((namelist_in == NULL ) || ((namelist_in != NULL) && (*namelist_in == '\0')))
820 87100 : return;
821 :
822 101 : namelist = talloc_strdup(talloc_tos(), namelist_in);
823 101 : if (namelist == NULL) {
824 0 : DEBUG(0,("set_namearray: talloc fail\n"));
825 0 : return;
826 : }
827 101 : nameptr = namelist;
828 :
829 101 : 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 301 : while(nameptr <= namelist_end) {
837 301 : if ( *nameptr == '/' ) {
838 : /* cope with multiple (useless) /s) */
839 97 : nameptr++;
840 97 : continue;
841 : }
842 : /* anything left? */
843 204 : if ( *nameptr == '\0' )
844 100 : break;
845 :
846 : /* find the next '/' or consume remaining */
847 103 : name_end = strchr_m(nameptr, '/');
848 103 : if (name_end == NULL) {
849 : /* Point nameptr at the terminating '\0' */
850 4 : nameptr += strlen(nameptr);
851 : } else {
852 : /* next segment please */
853 99 : nameptr = name_end + 1;
854 : }
855 103 : num_entries++;
856 : }
857 :
858 101 : if(num_entries == 0) {
859 0 : talloc_free(namelist);
860 0 : return;
861 : }
862 :
863 101 : 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 100 : nameptr = namelist;
871 100 : i = 0;
872 301 : while(nameptr <= namelist_end) {
873 301 : if ( *nameptr == '/' ) {
874 : /* cope with multiple (useless) /s) */
875 97 : nameptr++;
876 97 : continue;
877 : }
878 : /* anything left? */
879 204 : if ( *nameptr == '\0' )
880 100 : break;
881 :
882 : /* find the next '/' or consume remaining */
883 103 : name_end = strchr_m(nameptr, '/');
884 103 : if (name_end != NULL) {
885 99 : *name_end = '\0';
886 : }
887 :
888 103 : (*ppname_array)[i].is_wild = ms_has_wild(nameptr);
889 103 : 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 103 : if (name_end == NULL) {
896 : /* Point nameptr at the terminating '\0' */
897 4 : nameptr += strlen(nameptr);
898 : } else {
899 : /* next segment please */
900 99 : nameptr = name_end + 1;
901 : }
902 103 : i++;
903 : }
904 :
905 101 : (*ppname_array)[i].name = NULL;
906 :
907 101 : talloc_free(namelist);
908 101 : 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 205679 : 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 205679 : 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 205679 : lock.l_type = *ptype;
930 205679 : lock.l_whence = SEEK_SET;
931 205679 : lock.l_start = *poffset;
932 205679 : lock.l_len = *pcount;
933 205679 : lock.l_pid = 0;
934 :
935 205679 : ret = sys_fcntl_ptr(fd,op,&lock);
936 :
937 205679 : 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 205679 : *ptype = lock.l_type;
946 205679 : *poffset = lock.l_start;
947 205679 : *pcount = lock.l_len;
948 205679 : *ppid = lock.l_pid;
949 :
950 205679 : 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 205566 : return True;
953 : }
954 :
955 : #if defined(HAVE_OFD_LOCKS)
956 211359 : int map_process_lock_to_ofd_lock(int op)
957 : {
958 211359 : switch (op) {
959 205566 : case F_GETLK:
960 : case F_OFD_GETLK:
961 205566 : op = F_OFD_GETLK;
962 205566 : break;
963 5680 : case F_SETLK:
964 : case F_OFD_SETLK:
965 5680 : op = F_OFD_SETLK;
966 5680 : 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 211212 : 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 21633 : static bool nb_name_equal(const char *s1, const char *s2)
992 : {
993 21633 : int cmp = strncasecmp_m(s1, s2, MAX_NETBIOSNAME_LEN-1);
994 21633 : return (cmp == 0);
995 : }
996 :
997 18705 : bool is_myname(const char *s)
998 : {
999 18705 : const char **aliases = NULL;
1000 18705 : bool ok = false;
1001 :
1002 18705 : ok = nb_name_equal(lp_netbios_name(), s);
1003 18705 : if (ok) {
1004 2308 : goto done;
1005 : }
1006 :
1007 16397 : aliases = lp_netbios_aliases();
1008 16397 : if (aliases == NULL) {
1009 14911 : goto done;
1010 : }
1011 :
1012 4326 : while (*aliases != NULL) {
1013 2928 : ok = nb_name_equal(*aliases, s);
1014 2928 : if (ok) {
1015 88 : goto done;
1016 : }
1017 2840 : aliases += 1;
1018 : }
1019 :
1020 1398 : done:
1021 18705 : DBG_DEBUG("is_myname(\"%s\") returns %d\n", s, (int)ok);
1022 18705 : 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 17498 : const char *get_remote_arch_str(void)
1063 : {
1064 17498 : 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 17498 : return remote_arch_strings[ra_type];
1074 : }
1075 :
1076 711 : enum remote_arch_types get_remote_arch_from_str(const char *remote_arch_string)
1077 : {
1078 2 : int i;
1079 :
1080 6399 : for (i = 0; i < ARRAY_SIZE(remote_arch_strings); i++) {
1081 6399 : if (strcmp(remote_arch_string, remote_arch_strings[i]) == 0) {
1082 711 : 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 30690 : void set_remote_arch(enum remote_arch_types type)
1093 : {
1094 30690 : 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 30690 : ra_type = type;
1106 30690 : 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 2205198 : enum remote_arch_types get_remote_arch(void)
1115 : {
1116 2205198 : return ra_type;
1117 : }
1118 :
1119 : #define RA_CACHE_TTL 7*24*3600
1120 :
1121 26073 : static bool remote_arch_cache_key(const struct GUID *client_guid,
1122 : fstring key)
1123 : {
1124 755 : struct GUID_txt_buf guid_buf;
1125 26073 : const char *guid_string = NULL;
1126 :
1127 26073 : guid_string = GUID_buf_string(client_guid, &guid_buf);
1128 26073 : if (guid_string == NULL) {
1129 0 : return false;
1130 : }
1131 :
1132 26073 : fstr_sprintf(key, "RA/%s", guid_string);
1133 26073 : return true;
1134 : }
1135 :
1136 : struct ra_parser_state {
1137 : bool found;
1138 : enum remote_arch_types ra;
1139 : };
1140 :
1141 711 : static void ra_parser(const struct gencache_timeout *t,
1142 : DATA_BLOB blob,
1143 : void *priv_data)
1144 : {
1145 711 : struct ra_parser_state *state = (struct ra_parser_state *)priv_data;
1146 711 : const char *ra_str = NULL;
1147 :
1148 711 : if (gencache_timeout_expired(t)) {
1149 0 : return;
1150 : }
1151 :
1152 711 : 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 711 : ra_str = (const char *)blob.data;
1158 711 : DBG_INFO("Got remote arch [%s] from cache\n", ra_str);
1159 :
1160 711 : state->ra = get_remote_arch_from_str(ra_str);
1161 711 : state->found = true;
1162 711 : return;
1163 : }
1164 :
1165 8430 : static bool remote_arch_cache_get(const struct GUID *client_guid)
1166 : {
1167 307 : bool ok;
1168 307 : fstring ra_key;
1169 8430 : struct ra_parser_state state = (struct ra_parser_state) {
1170 : .found = false,
1171 : .ra = RA_UNKNOWN,
1172 : };
1173 :
1174 8430 : ok = remote_arch_cache_key(client_guid, ra_key);
1175 8430 : if (!ok) {
1176 0 : return false;
1177 : }
1178 :
1179 8430 : ok = gencache_parse(ra_key, ra_parser, &state);
1180 8430 : if (!ok || !state.found) {
1181 7414 : return true;
1182 : }
1183 :
1184 711 : if (state.ra == RA_UNKNOWN) {
1185 0 : return true;
1186 : }
1187 :
1188 711 : set_remote_arch(state.ra);
1189 711 : return true;
1190 : }
1191 :
1192 17496 : static bool remote_arch_cache_set(const struct GUID *client_guid)
1193 : {
1194 442 : bool ok;
1195 442 : fstring ra_key;
1196 17496 : const char *ra_str = NULL;
1197 :
1198 17496 : if (get_remote_arch() == RA_UNKNOWN) {
1199 0 : return true;
1200 : }
1201 :
1202 17496 : ok = remote_arch_cache_key(client_guid, ra_key);
1203 17496 : if (!ok) {
1204 0 : return false;
1205 : }
1206 :
1207 17496 : ra_str = get_remote_arch_str();
1208 17496 : if (ra_str == NULL) {
1209 0 : return false;
1210 : }
1211 :
1212 17496 : ok = gencache_set(ra_key, ra_str, time(NULL) + RA_CACHE_TTL);
1213 17496 : if (!ok) {
1214 0 : return false;
1215 : }
1216 :
1217 17054 : return true;
1218 : }
1219 :
1220 25926 : bool remote_arch_cache_update(const struct GUID *client_guid)
1221 : {
1222 749 : bool ok;
1223 :
1224 25926 : if (get_remote_arch() == RA_UNKNOWN) {
1225 :
1226 8430 : become_root();
1227 8430 : ok = remote_arch_cache_get(client_guid);
1228 8430 : unbecome_root();
1229 :
1230 8430 : return ok;
1231 : }
1232 :
1233 17496 : become_root();
1234 17496 : ok = remote_arch_cache_set(client_guid);
1235 17496 : unbecome_root();
1236 :
1237 17496 : return ok;
1238 : }
1239 :
1240 147 : bool remote_arch_cache_delete(const struct GUID *client_guid)
1241 : {
1242 6 : bool ok;
1243 6 : fstring ra_key;
1244 :
1245 147 : ok = remote_arch_cache_key(client_guid, ra_key);
1246 147 : if (!ok) {
1247 0 : return false;
1248 : }
1249 :
1250 147 : become_root();
1251 147 : ok = gencache_del(ra_key);
1252 147 : unbecome_root();
1253 :
1254 147 : if (!ok) {
1255 135 : 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 510 : int str_checksum(const char *s)
1272 : {
1273 0 : TDB_DATA key;
1274 510 : if (s == NULL)
1275 0 : return 0;
1276 :
1277 510 : key = (TDB_DATA) { .dptr = discard_const_p(uint8_t, s),
1278 510 : .dsize = strlen(s) };
1279 :
1280 510 : return tdb_jenkins_hash(&key);
1281 : }
1282 :
1283 : /*****************************************************************
1284 : Zero a memory area then free it. Used to catch bugs faster.
1285 : *****************************************************************/
1286 :
1287 31 : void zero_free(void *p, size_t size)
1288 : {
1289 31 : memset(p, 0, size);
1290 31 : SAFE_FREE(p);
1291 31 : }
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 495051 : void *smb_xmalloc_array(size_t size, unsigned int count)
1381 : {
1382 12740 : void *p;
1383 495051 : if (size == 0) {
1384 0 : smb_panic("smb_xmalloc_array: called with zero size");
1385 : }
1386 495051 : if (count >= MAX_ALLOC_SIZE/size) {
1387 0 : smb_panic("smb_xmalloc_array: alloc size too large");
1388 : }
1389 495051 : 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 495051 : 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 87170 : char *myhostname_upper(void)
1415 : {
1416 212 : static char *ret;
1417 87170 : if (ret == NULL) {
1418 39547 : char *name = get_myname(NULL);
1419 39547 : if (name == NULL) {
1420 0 : return NULL;
1421 : }
1422 39547 : ret = strupper_talloc(NULL, name);
1423 39547 : talloc_free(name);
1424 : }
1425 87170 : return ret;
1426 : }
1427 :
1428 : /*******************************************************************
1429 : Given a filename - get its directory name
1430 : ********************************************************************/
1431 :
1432 11782 : 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 11782 : p = strrchr_m(dir, '/'); /* Find final '/', if any */
1439 :
1440 11782 : if (p == NULL) {
1441 5658 : if (!(*parent = talloc_strdup(mem_ctx, "."))) {
1442 0 : return False;
1443 : }
1444 5658 : if (name) {
1445 5254 : *name = dir;
1446 : }
1447 5658 : return True;
1448 : }
1449 :
1450 6124 : len = p-dir;
1451 :
1452 6124 : *parent = talloc_strndup(mem_ctx, dir, len);
1453 6124 : if (*parent == NULL) {
1454 0 : return False;
1455 : }
1456 :
1457 6124 : if (name) {
1458 4608 : *name = p+1;
1459 : }
1460 6092 : return True;
1461 : }
1462 :
1463 : /*******************************************************************
1464 : Determine if a pattern contains any Microsoft wildcard characters.
1465 : *******************************************************************/
1466 :
1467 1645640 : bool ms_has_wild(const char *s)
1468 : {
1469 1645640 : const char *found = strpbrk(s, "*?<>\"");
1470 1645640 : 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 914979 : bool mask_match(const char *string, const char *pattern, bool is_case_sensitive)
1496 : {
1497 914979 : if (ISDOTDOT(string))
1498 18143 : string = ".";
1499 914979 : if (ISDOT(pattern))
1500 72 : return False;
1501 :
1502 914907 : 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 296 : bool mask_match_list(const char *string, char **list, int listLen, bool is_case_sensitive)
1511 : {
1512 752 : while (listLen-- > 0) {
1513 516 : if (mask_match(string, *list++, is_case_sensitive))
1514 60 : return True;
1515 : }
1516 236 : 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 208 : struct server_id interpret_pid(const char *pid_string)
1579 : {
1580 208 : 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 131384 : bool any_nt_status_not_ok(NTSTATUS err1, NTSTATUS err2, NTSTATUS *result)
1682 : {
1683 131384 : if (!NT_STATUS_IS_OK(err1)) {
1684 128 : *result = err1;
1685 128 : return true;
1686 : }
1687 131256 : if (!NT_STATUS_IS_OK(err2)) {
1688 1686 : *result = err2;
1689 1686 : return true;
1690 : }
1691 129570 : 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 27239 : 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 27239 : uint32_t create_options = FILE_NON_DIRECTORY_FILE;
1766 27239 : uint32_t private_flags = 0;
1767 :
1768 27239 : 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 27239 : switch (GET_OPENX_MODE(deny_mode)) {
1775 4729 : case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1776 : case DOS_OPEN_RDONLY:
1777 4729 : access_mask = FILE_GENERIC_READ;
1778 4729 : break;
1779 2459 : case DOS_OPEN_WRONLY:
1780 2459 : access_mask = FILE_GENERIC_WRITE;
1781 2459 : break;
1782 20035 : case DOS_OPEN_RDWR:
1783 : case DOS_OPEN_FCB:
1784 20035 : access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1785 20035 : 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 27239 : 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 10075 : case OPENX_FILE_EXISTS_OPEN:
1799 10075 : create_disposition = FILE_OPEN;
1800 10075 : 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 2222 : case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1811 2222 : create_disposition = FILE_OVERWRITE_IF;
1812 2222 : 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 27229 : 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 20075 : case DENY_NONE:
1840 20075 : share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1841 20075 : 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 27221 : 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 27221 : if (paccess_mask) {
1878 27221 : *paccess_mask = access_mask;
1879 : }
1880 27221 : if (pshare_mode) {
1881 27221 : *pshare_mode = share_mode;
1882 : }
1883 27221 : if (pcreate_disposition) {
1884 27221 : *pcreate_disposition = create_disposition;
1885 : }
1886 27221 : if (pcreate_options) {
1887 27221 : *pcreate_options = create_options;
1888 : }
1889 27221 : if (pprivate_flags) {
1890 23386 : *pprivate_flags = private_flags;
1891 : }
1892 :
1893 27101 : return True;
1894 :
1895 : }
1896 :
1897 : /*************************************************************************
1898 : Return a talloced copy of a struct security_unix_token. NULL on fail.
1899 : *************************************************************************/
1900 :
1901 170365 : 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 170365 : cpy = talloc(ctx, struct security_unix_token);
1906 170365 : if (!cpy) {
1907 0 : return NULL;
1908 : }
1909 :
1910 170365 : cpy->uid = tok->uid;
1911 170365 : cpy->gid = tok->gid;
1912 170365 : cpy->ngroups = tok->ngroups;
1913 170365 : if (tok->ngroups) {
1914 : /* Make this a talloc child of cpy. */
1915 170365 : cpy->groups = (gid_t *)talloc_memdup(
1916 : cpy, tok->groups, tok->ngroups * sizeof(gid_t));
1917 170365 : if (!cpy->groups) {
1918 0 : TALLOC_FREE(cpy);
1919 0 : return NULL;
1920 : }
1921 : } else {
1922 0 : cpy->groups = NULL;
1923 : }
1924 170025 : return cpy;
1925 : }
1926 :
1927 : /****************************************************************************
1928 : Return a root token
1929 : ****************************************************************************/
1930 :
1931 10075 : struct security_unix_token *root_unix_token(TALLOC_CTX *mem_ctx)
1932 : {
1933 10075 : struct security_unix_token *t = NULL;
1934 :
1935 10075 : t = talloc_zero(mem_ctx, struct security_unix_token);
1936 10075 : if (t == NULL) {
1937 0 : return NULL;
1938 : }
1939 :
1940 : /*
1941 : * This is not needed, but lets make it explicit, not implicit.
1942 : */
1943 10075 : *t = (struct security_unix_token) {
1944 : .uid = 0,
1945 : .gid = 0,
1946 : .ngroups = 0,
1947 : .groups = NULL
1948 : };
1949 :
1950 10075 : return t;
1951 : }
1952 :
1953 152 : 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 304 : str = talloc_asprintf(
1959 : mem_ctx,
1960 : "uid=%ju, gid=%ju, %"PRIu32" groups:",
1961 152 : (uintmax_t)(tok->uid),
1962 152 : (uintmax_t)(tok->gid),
1963 152 : tok->ngroups);
1964 :
1965 1034 : for (i=0; i<tok->ngroups; i++) {
1966 882 : talloc_asprintf_addbuf(
1967 882 : &str, " %ju", (uintmax_t)tok->groups[i]);
1968 : }
1969 :
1970 152 : return str;
1971 : }
1972 :
1973 : /****************************************************************************
1974 : Check that a file matches a particular file type.
1975 : ****************************************************************************/
1976 :
1977 1009028 : 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 1009028 : 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 1000472 : mask = ((dirtype >> 8) & (FILE_ATTRIBUTE_DIRECTORY|
1995 : FILE_ATTRIBUTE_ARCHIVE|
1996 : FILE_ATTRIBUTE_READONLY|
1997 : FILE_ATTRIBUTE_HIDDEN|
1998 : FILE_ATTRIBUTE_SYSTEM)); /* & 0x37 */
1999 1000472 : 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 984042 : return true;
2013 : }
|