2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001
6 Copyright (C) Simo Sorce 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
26 #ifdef WITH_NISPLUS_HOME
27 #ifdef BROKEN_NISPLUS_INCLUDE_FILES
29 * The following lines are needed due to buggy include files
30 * in Solaris 2.6 which define GROUP in both /usr/include/sys/acl.h and
31 * also in /usr/include/rpcsvc/nis.h. The definitions conflict. JRA.
32 * Also GROUP_OBJ is defined as 0x4 in /usr/include/sys/acl.h and as
33 * an enum in /usr/include/rpcsvc/nis.h.
40 #if defined(GROUP_OBJ)
44 #endif /* BROKEN_NISPLUS_INCLUDE_FILES */
46 #include <rpcsvc/nis.h>
48 #else /* !WITH_NISPLUS_HOME */
50 #include "rpcsvc/ypclnt.h"
52 #endif /* WITH_NISPLUS_HOME */
53 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
56 #include <openssl/ssl.h>
57 #undef Realloc /* SSLeay defines this and samba has a function of this name */
62 int Protocol = PROTOCOL_COREPLUS;
64 /* a default finfo structure to ensure all fields are sensible */
65 file_info def_finfo = {-1,0,0,0,0,0,0,"",""};
67 /* this is used by the chaining code */
73 case handling on filenames
75 int case_default = CASE_LOWER;
77 /* the following control case operations - they are put here so the
78 client can link easily */
81 BOOL use_mangled_map = False;
82 BOOL short_case_preserve;
85 static enum remote_arch_types ra_type = RA_UNKNOWN;
86 pstring user_socket_options=DEFAULT_SOCKET_OPTIONS;
88 pstring global_myname = "";
89 fstring global_myworkgroup = "";
90 char **my_netbios_names;
93 /****************************************************************************
94 find a suitable temporary directory. The result should be copied immediately
95 as it may be overwritten by a subsequent call.
96 ****************************************************************************/
100 if ((p = getenv("TMPDIR")))
105 /****************************************************************************
106 Determine whether we are in the specified group.
107 ****************************************************************************/
109 BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups)
113 if (group == current_gid)
116 for (i=0;i<ngroups;i++)
117 if (group == groups[i])
123 /****************************************************************************
124 Like atoi but gets the value up to the separator character.
125 ****************************************************************************/
127 char *Atoic(char *p, int *n, char *c)
129 if (!isdigit((int)*p)) {
130 DEBUG(5, ("Atoic: malformed number\n"));
136 while ((*p) && isdigit((int)*p))
139 if (strchr_m(c, *p) == NULL)
141 DEBUG(5, ("Atoic: no separator characters (%s) not found\n", c));
148 /*************************************************************************
149 Reads a list of numbers.
150 *************************************************************************/
152 char *get_numlist(char *p, uint32 **num, int *count)
156 if (num == NULL || count == NULL)
162 while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':') {
165 tn = Realloc((*num), ((*count)+1) * sizeof(uint32));
172 (*num)[(*count)] = val;
180 /*******************************************************************
181 Check if a file exists - call vfs_file_exist for samba files.
182 ********************************************************************/
184 BOOL file_exist(const char *fname,SMB_STRUCT_STAT *sbuf)
190 if (sys_stat(fname,sbuf) != 0)
193 return(S_ISREG(sbuf->st_mode));
196 /*******************************************************************
197 Check a files mod time.
198 ********************************************************************/
200 time_t file_modtime(const char *fname)
204 if (sys_stat(fname,&st) != 0)
210 /*******************************************************************
211 Check if a directory exists.
212 ********************************************************************/
214 BOOL directory_exist(char *dname,SMB_STRUCT_STAT *st)
221 if (sys_stat(dname,st) != 0)
224 ret = S_ISDIR(st->st_mode);
230 /*******************************************************************
231 returns the size in bytes of the named file
232 ********************************************************************/
233 SMB_OFF_T get_file_size(char *file_name)
237 if(sys_stat(file_name,&buf) != 0)
238 return (SMB_OFF_T)-1;
242 /*******************************************************************
243 return a string representing an attribute for a file
244 ********************************************************************/
245 char *attrib_string(uint16 mode)
247 static fstring attrstr;
251 if (mode & aVOLID) fstrcat(attrstr,"V");
252 if (mode & aDIR) fstrcat(attrstr,"D");
253 if (mode & aARCH) fstrcat(attrstr,"A");
254 if (mode & aHIDDEN) fstrcat(attrstr,"H");
255 if (mode & aSYSTEM) fstrcat(attrstr,"S");
256 if (mode & aRONLY) fstrcat(attrstr,"R");
261 /*******************************************************************
262 show a smb message structure
263 ********************************************************************/
264 void show_msg(char *buf)
269 if (DEBUGLEVEL < 5) return;
271 DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
273 (int)CVAL(buf,smb_com),
274 (int)CVAL(buf,smb_rcls),
275 (int)CVAL(buf,smb_reh),
276 (int)SVAL(buf,smb_err),
277 (int)CVAL(buf,smb_flg),
278 (int)SVAL(buf,smb_flg2)));
279 DEBUG(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\nsmt_wct=%d\n",
280 (int)SVAL(buf,smb_tid),
281 (int)SVAL(buf,smb_pid),
282 (int)SVAL(buf,smb_uid),
283 (int)SVAL(buf,smb_mid),
284 (int)CVAL(buf,smb_wct)));
286 for (i=0;i<(int)CVAL(buf,smb_wct);i++)
288 DEBUG(5,("smb_vwv[%d]=%d (0x%X)\n",i,
289 SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
292 bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
294 DEBUG(5,("smb_bcc=%d\n",bcc));
296 if (DEBUGLEVEL < 10) return;
303 dump_data(10, smb_buf(buf), bcc);
306 /*******************************************************************
307 set the length and marker of an smb packet
308 ********************************************************************/
309 void smb_setlen(char *buf,int len)
311 _smb_setlen(buf,len);
319 /*******************************************************************
320 setup the word count and byte count for a smb message
321 ********************************************************************/
322 int set_message(char *buf,int num_words,int num_bytes,BOOL zero)
325 memset(buf + smb_size,'\0',num_words*2 + num_bytes);
326 SCVAL(buf,smb_wct,num_words);
327 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
328 smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
329 return (smb_size + num_words*2 + num_bytes);
332 /*******************************************************************
333 setup only the byte count for a smb message
334 ********************************************************************/
335 int set_message_bcc(char *buf,int num_bytes)
337 int num_words = CVAL(buf,smb_wct);
338 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
339 smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
340 return (smb_size + num_words*2 + num_bytes);
343 /*******************************************************************
344 setup only the byte count for a smb message, using the end of the
346 ********************************************************************/
347 int set_message_end(void *outbuf,void *end_ptr)
349 return set_message_bcc((char *)outbuf,PTR_DIFF(end_ptr,smb_buf((char *)outbuf)));
352 /*******************************************************************
353 reduce a file name, removing .. elements.
354 ********************************************************************/
355 void dos_clean_name(char *s)
359 DEBUG(3,("dos_clean_name [%s]\n",s));
361 /* remove any double slashes */
362 all_string_sub(s, "\\\\", "\\", 0);
364 while ((p = strstr(s,"\\..\\")) != NULL)
371 if ((p=strrchr_m(s,'\\')) != NULL)
378 trim_string(s,NULL,"\\..");
380 all_string_sub(s, "\\.\\", "\\", 0);
383 /*******************************************************************
384 reduce a file name, removing .. elements.
385 ********************************************************************/
386 void unix_clean_name(char *s)
390 DEBUG(3,("unix_clean_name [%s]\n",s));
392 /* remove any double slashes */
393 all_string_sub(s, "//","/", 0);
395 /* Remove leading ./ characters */
396 if(strncmp(s, "./", 2) == 0) {
397 trim_string(s, "./", NULL);
402 while ((p = strstr(s,"/../")) != NULL)
409 if ((p=strrchr_m(s,'/')) != NULL)
416 trim_string(s,NULL,"/..");
419 /*******************************************************************
421 reduce a file name, removing or reducing /../ , /./ , // elements.
422 remove also any trailing . and /
423 return a new allocated string.
424 ********************************************************************/
425 smb_ucs2_t *unix_clean_path(const smb_ucs2_t *s)
428 smb_ucs2_t *p, *r, *t;
430 DEBUG(3, ("unix_clean_path\n")); /* [%unicode]\n")); */
433 /* convert '\' to '/' */
435 if (!ns) return NULL;
438 /* remove all double slashes */
440 ns = all_string_sub_wa(p, "//", "/");
442 if (!ns) return NULL;
446 ns = all_string_sub_wa(p, "/./", "/");
448 if (!ns) return NULL;
450 /* reduce any /../ */
452 while (*t && (r = strstr_wa(t, "/.."))) {
454 if (*t == UCS2_CHAR('/') || *t == 0) {
456 p = strrchr_w(ns, UCS2_CHAR('/'));
459 else memmove(p, t, (strlen_w(t) + 1) * sizeof(smb_ucs2_t));
464 /* remove any leading ./ trailing /. */
465 trim_string_wa(ns, "./", "/.");
467 /* remove any leading and trailing / */
468 trim_string_wa(ns, "/", "/");
473 /****************************************************************************
475 ****************************************************************************/
476 void make_dir_struct(char *buf,char *mask,char *fname,SMB_OFF_T size,int mode,time_t date)
483 if ((mode & aDIR) != 0)
486 memset(buf+1,' ',11);
487 if ((p = strchr_m(mask2,'.')) != NULL)
490 push_ascii(buf+1,mask2,8, 0);
491 push_ascii(buf+9,p+1,3, 0);
495 push_ascii(buf+1,mask2,11, 0);
497 memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
499 put_dos_date(buf,22,date);
500 SSVAL(buf,26,size & 0xFFFF);
501 SSVAL(buf,28,(size >> 16)&0xFFFF);
502 push_ascii(buf+30,fname,12, 0);
505 DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname));
509 /*******************************************************************
510 close the low 3 fd's and open dev/null in their place
511 ********************************************************************/
512 void close_low_fds(void)
520 /* try and use up these file descriptors, so silly
521 library routines writing to stdout etc won't cause havoc */
523 fd = sys_open("/dev/null",O_RDWR,0);
524 if (fd < 0) fd = sys_open("/dev/null",O_WRONLY,0);
526 DEBUG(0,("Can't open /dev/null\n"));
530 DEBUG(0,("Didn't get file descriptor %d\n",i));
536 /****************************************************************************
537 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
541 ****************************************************************************/
542 int set_blocking(int fd, BOOL set)
546 #define FLAG_TO_SET O_NONBLOCK
549 #define FLAG_TO_SET O_NDELAY
551 #define FLAG_TO_SET FNDELAY
555 if((val = sys_fcntl_long(fd, F_GETFL, 0)) == -1)
557 if(set) /* Turn blocking on - ie. clear nonblock flag */
561 return sys_fcntl_long( fd, F_SETFL, val);
565 /****************************************************************************
566 Transfer some data between two fd's.
567 ****************************************************************************/
569 #ifndef TRANSFER_BUF_SIZE
570 #define TRANSFER_BUF_SIZE 65536
573 ssize_t transfer_file_internal(int infd, int outfd, size_t n, ssize_t (*read_fn)(int, void *, size_t),
574 ssize_t (*write_fn)(int, const void *, size_t))
580 size_t num_to_read_thistime;
581 size_t num_written = 0;
583 if ((buf = malloc(TRANSFER_BUF_SIZE)) == NULL)
587 num_to_read_thistime = MIN((n - total), TRANSFER_BUF_SIZE);
589 read_ret = (*read_fn)(infd, buf, num_to_read_thistime);
590 if (read_ret == -1) {
591 DEBUG(0,("transfer_file_internal: read failure. Error = %s\n", strerror(errno) ));
600 while (num_written < read_ret) {
601 write_ret = (*write_fn)(outfd,buf + num_written, read_ret - num_written);
603 if (write_ret == -1) {
604 DEBUG(0,("transfer_file_internal: write failure. Error = %s\n", strerror(errno) ));
609 return (ssize_t)total;
611 num_written += (size_t)write_ret;
614 total += (size_t)read_ret;
618 return (ssize_t)total;
621 SMB_OFF_T transfer_file(int infd,int outfd,SMB_OFF_T n)
623 return (SMB_OFF_T)transfer_file_internal(infd, outfd, (size_t)n, sys_read, sys_write);
626 /*******************************************************************
627 Sleep for a specified number of milliseconds.
628 ********************************************************************/
630 void msleep(unsigned int t)
632 unsigned int tdiff=0;
633 struct timeval tval,t1,t2;
640 tval.tv_sec = (t-tdiff)/1000;
641 tval.tv_usec = 1000*((t-tdiff)%1000);
643 /* Never wait for more than 1 sec. */
644 if (tval.tv_sec > 1) {
651 sys_select_intr(0,&fds,NULL,NULL,&tval);
654 if (t2.tv_sec < t1.tv_sec) {
655 /* Someone adjusted time... */
659 tdiff = TvalDiff(&t1,&t2);
663 /****************************************************************************
664 Become a daemon, discarding the controlling terminal.
665 ****************************************************************************/
667 void become_daemon(void)
673 /* detach from the terminal */
676 #elif defined(TIOCNOTTY)
678 int i = sys_open("/dev/tty", O_RDWR, 0);
680 ioctl(i, (int) TIOCNOTTY, (char *)0);
684 #endif /* HAVE_SETSID */
686 /* Close fd's 0,1,2. Needed if started by rsh */
691 /****************************************************************************
692 put up a yes/no prompt
693 ****************************************************************************/
699 if (!fgets(ans,sizeof(ans)-1,stdin))
702 if (*ans == 'y' || *ans == 'Y')
708 /****************************************************************************
709 Expand a pointer to be a particular size.
710 ****************************************************************************/
712 void *Realloc(void *p,size_t size)
718 DEBUG(5,("Realloc asked for 0 bytes\n"));
723 ret = (void *)malloc(size);
725 ret = (void *)realloc(p,size);
728 DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
733 /****************************************************************************
734 Free memory, checks for NULL.
735 use directly SAFE_FREE()
736 exist only because we need to pass a function pointer somewhere --SSS
737 ****************************************************************************/
739 void safe_free(void *p)
744 /****************************************************************************
745 Get my own name and IP.
746 ****************************************************************************/
748 BOOL get_myname(char *my_name)
754 /* get my host name */
755 if (gethostname(hostname, sizeof(hostname)) == -1) {
756 DEBUG(0,("gethostname failed\n"));
760 /* Ensure null termination. */
761 hostname[sizeof(hostname)-1] = '\0';
764 /* split off any parts after an initial . */
765 char *p = strchr_m(hostname,'.');
770 fstrcpy(my_name,hostname);
776 /****************************************************************************
777 Interpret a protocol description string, with a default.
778 ****************************************************************************/
780 int interpret_protocol(char *str,int def)
782 if (strequal(str,"NT1"))
783 return(PROTOCOL_NT1);
784 if (strequal(str,"LANMAN2"))
785 return(PROTOCOL_LANMAN2);
786 if (strequal(str,"LANMAN1"))
787 return(PROTOCOL_LANMAN1);
788 if (strequal(str,"CORE"))
789 return(PROTOCOL_CORE);
790 if (strequal(str,"COREPLUS"))
791 return(PROTOCOL_COREPLUS);
792 if (strequal(str,"CORE+"))
793 return(PROTOCOL_COREPLUS);
795 DEBUG(0,("Unrecognised protocol level %s\n",str));
800 /****************************************************************************
801 Return true if a string could be a pure IP address.
802 ****************************************************************************/
804 BOOL is_ipaddress(const char *str)
806 BOOL pure_address = True;
809 for (i=0; pure_address && str[i]; i++)
810 if (!(isdigit((int)str[i]) || str[i] == '.'))
811 pure_address = False;
813 /* Check that a pure number is not misinterpreted as an IP */
814 pure_address = pure_address && (strchr_m(str, '.') != NULL);
819 /****************************************************************************
820 interpret an internet address or name into an IP address in 4 byte form
821 ****************************************************************************/
823 uint32 interpret_addr(const char *str)
828 if (strcmp(str,"0.0.0.0") == 0) return(0);
829 if (strcmp(str,"255.255.255.255") == 0) return(0xFFFFFFFF);
831 /* if it's in the form of an IP address then get the lib to interpret it */
832 if (is_ipaddress(str)) {
833 res = inet_addr(str);
835 /* otherwise assume it's a network name of some sort and use
837 if ((hp = sys_gethostbyname(str)) == 0) {
838 DEBUG(3,("sys_gethostbyname: Unknown host. %s\n",str));
841 if(hp->h_addr == NULL) {
842 DEBUG(3,("sys_gethostbyname: host address is invalid for host %s\n",str));
845 putip((char *)&res,(char *)hp->h_addr);
848 if (res == (uint32)-1) return(0);
853 /*******************************************************************
854 a convenient addition to interpret_addr()
855 ******************************************************************/
856 struct in_addr *interpret_addr2(const char *str)
858 static struct in_addr ret;
859 uint32 a = interpret_addr(str);
864 /*******************************************************************
865 check if an IP is the 0.0.0.0
866 ******************************************************************/
867 BOOL is_zero_ip(struct in_addr ip)
870 putip((char *)&a,(char *)&ip);
874 /* Set an IP to 0.0.0.0 */
876 void zero_ip(struct in_addr *ip)
879 static struct in_addr ipzero;
882 ipzero = *interpret_addr2("0.0.0.0");
889 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
890 /******************************************************************
891 Remove any mount options such as -rsize=2048,wsize=2048 etc.
892 Based on a fix from <Thomas.Hepper@icem.de>.
893 *******************************************************************/
895 static void strip_mount_options( pstring *str)
900 while(*p && !isspace(*p))
902 while(*p && isspace(*p))
908 pstrcpy(*str, tmp_str);
913 /*******************************************************************
914 Patch from jkf@soton.ac.uk
915 Split Luke's automount_server into YP lookup and string splitter
916 so can easily implement automount_path().
917 As we may end up doing both, cache the last YP result.
918 *******************************************************************/
920 #ifdef WITH_NISPLUS_HOME
921 char *automount_lookup(const char *user_name)
923 static fstring last_key = "";
924 static pstring last_value = "";
926 char *nis_map = (char *)lp_nis_home_map_name();
928 char buffer[NIS_MAXATTRVAL + 1];
933 if (strcmp(user_name, last_key))
935 slprintf(buffer, sizeof(buffer)-1, "[key=%s],%s", user_name, nis_map);
936 DEBUG(5, ("NIS+ querystring: %s\n", buffer));
938 if (result = nis_list(buffer, FOLLOW_PATH|EXPAND_NAME|HARD_LOOKUP, NULL, NULL))
940 if (result->status != NIS_SUCCESS)
942 DEBUG(3, ("NIS+ query failed: %s\n", nis_sperrno(result->status)));
943 fstrcpy(last_key, ""); pstrcpy(last_value, "");
947 object = result->objects.objects_val;
948 if (object->zo_data.zo_type == ENTRY_OBJ)
950 entry = &object->zo_data.objdata_u.en_data;
951 DEBUG(5, ("NIS+ entry type: %s\n", entry->en_type));
952 DEBUG(3, ("NIS+ result: %s\n", entry->en_cols.en_cols_val[1].ec_value.ec_value_val));
954 pstrcpy(last_value, entry->en_cols.en_cols_val[1].ec_value.ec_value_val);
955 pstring_sub(last_value, "&", user_name);
956 fstrcpy(last_key, user_name);
960 nis_freeresult(result);
963 strip_mount_options(&last_value);
965 DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n", user_name, last_value));
968 #else /* WITH_NISPLUS_HOME */
969 char *automount_lookup(const char *user_name)
971 static fstring last_key = "";
972 static pstring last_value = "";
974 int nis_error; /* returned by yp all functions */
975 char *nis_result; /* yp_match inits this */
976 int nis_result_len; /* and set this */
977 char *nis_domain; /* yp_get_default_domain inits this */
978 char *nis_map = (char *)lp_nis_home_map_name();
980 if ((nis_error = yp_get_default_domain(&nis_domain)) != 0) {
981 DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
985 DEBUG(5, ("NIS Domain: %s\n", nis_domain));
987 if (!strcmp(user_name, last_key)) {
988 nis_result = last_value;
989 nis_result_len = strlen(last_value);
994 if ((nis_error = yp_match(nis_domain, nis_map,
995 user_name, strlen(user_name),
996 &nis_result, &nis_result_len)) == 0) {
997 if (!nis_error && nis_result_len >= sizeof(pstring)) {
998 nis_result_len = sizeof(pstring)-1;
1000 fstrcpy(last_key, user_name);
1001 strncpy(last_value, nis_result, nis_result_len);
1002 last_value[nis_result_len] = '\0';
1003 strip_mount_options(&last_value);
1005 } else if(nis_error == YPERR_KEY) {
1007 /* If Key lookup fails user home server is not in nis_map
1008 use default information for server, and home directory */
1010 DEBUG(3, ("YP Key not found: while looking up \"%s\" in map \"%s\"\n",
1011 user_name, nis_map));
1012 DEBUG(3, ("using defaults for server and home directory\n"));
1014 DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n",
1015 yperr_string(nis_error), user_name, nis_map));
1020 DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name, last_value));
1023 #endif /* WITH_NISPLUS_HOME */
1027 /*******************************************************************
1028 are two IPs on the same subnet?
1029 ********************************************************************/
1030 BOOL same_net(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
1032 uint32 net1,net2,nmask;
1034 nmask = ntohl(mask.s_addr);
1035 net1 = ntohl(ip1.s_addr);
1036 net2 = ntohl(ip2.s_addr);
1038 return((net1 & nmask) == (net2 & nmask));
1042 /****************************************************************************
1043 check if a process exists. Does this work on all unixes?
1044 ****************************************************************************/
1046 BOOL process_exists(pid_t pid)
1048 /* Doing kill with a non-positive pid causes messages to be
1049 * sent to places we don't want. */
1050 SMB_ASSERT(pid > 0);
1051 return(kill(pid,0) == 0 || errno != ESRCH);
1055 /*******************************************************************
1056 Convert a uid into a user name.
1057 ********************************************************************/
1059 char *uidtoname(uid_t uid)
1061 static fstring name;
1062 struct passwd *pass;
1064 pass = sys_getpwuid(uid);
1065 if (pass) return(pass->pw_name);
1066 slprintf(name, sizeof(name) - 1, "%d",(int)uid);
1071 /*******************************************************************
1072 Convert a gid into a group name.
1073 ********************************************************************/
1075 char *gidtoname(gid_t gid)
1077 static fstring name;
1080 grp = getgrgid(gid);
1081 if (grp) return(grp->gr_name);
1082 slprintf(name,sizeof(name) - 1, "%d",(int)gid);
1086 /*******************************************************************
1087 Convert a user name into a uid. If winbindd is present uses this.
1088 ********************************************************************/
1090 uid_t nametouid(char *name)
1092 struct passwd *pass;
1096 u = (uid_t)strtol(name, &p, 0);
1097 if ((p != name) && (*p == '\0'))
1100 pass = getpwnam_alloc(name);
1102 return(pass->pw_uid);
1108 /*******************************************************************
1109 Convert a name to a gid_t if possible. Return -1 if not a group. If winbindd
1110 is present does a shortcut lookup...
1111 ********************************************************************/
1113 gid_t nametogid(const char *name)
1119 g = (gid_t)strtol(name, &p, 0);
1120 if ((p != name) && (*p == '\0'))
1123 grp = getgrnam(name);
1125 return(grp->gr_gid);
1129 /*******************************************************************
1130 something really nasty happened - panic!
1131 ********************************************************************/
1132 void smb_panic(char *why)
1134 char *cmd = lp_panic_action();
1138 DEBUG(0,("PANIC: %s\n", why));
1144 /*******************************************************************
1145 a readdir wrapper which just returns the file name
1146 ********************************************************************/
1147 char *readdirname(DIR *p)
1149 SMB_STRUCT_DIRENT *ptr;
1152 if (!p) return(NULL);
1154 ptr = (SMB_STRUCT_DIRENT *)sys_readdir(p);
1155 if (!ptr) return(NULL);
1157 dname = ptr->d_name;
1160 if (telldir(p) < 0) return(NULL);
1163 #ifdef HAVE_BROKEN_READDIR
1164 /* using /usr/ucb/cc is BAD */
1170 int len = NAMLEN(ptr);
1171 memcpy(buf, dname, len);
1179 /*******************************************************************
1180 Utility function used to decide if the last component
1181 of a path matches a (possibly wildcarded) entry in a namelist.
1182 ********************************************************************/
1184 BOOL is_in_path(char *name, name_compare_entry *namelist)
1186 pstring last_component;
1189 DEBUG(8, ("is_in_path: %s\n", name));
1191 /* if we have no list it's obviously not in the path */
1192 if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL)))
1194 DEBUG(8,("is_in_path: no name list.\n"));
1198 /* Get the last component of the unix name. */
1199 p = strrchr_m(name, '/');
1200 strncpy(last_component, p ? ++p : name, sizeof(last_component)-1);
1201 last_component[sizeof(last_component)-1] = '\0';
1203 for(; namelist->name != NULL; namelist++)
1205 if(namelist->is_wild)
1207 if (mask_match(last_component, namelist->name, case_sensitive))
1209 DEBUG(8,("is_in_path: mask match succeeded\n"));
1215 if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
1216 (!case_sensitive && (StrCaseCmp(last_component, namelist->name) == 0)))
1218 DEBUG(8,("is_in_path: match succeeded\n"));
1223 DEBUG(8,("is_in_path: match not found\n"));
1228 /*******************************************************************
1229 Strip a '/' separated list into an array of
1230 name_compare_enties structures suitable for
1231 passing to is_in_path(). We do this for
1232 speed so we can pre-parse all the names in the list
1233 and don't do it for each call to is_in_path().
1234 namelist is modified here and is assumed to be
1235 a copy owned by the caller.
1236 We also check if the entry contains a wildcard to
1237 remove a potentially expensive call to mask_match
1239 ********************************************************************/
1241 void set_namearray(name_compare_entry **ppname_array, char *namelist)
1244 char *nameptr = namelist;
1245 int num_entries = 0;
1248 (*ppname_array) = NULL;
1250 if((nameptr == NULL ) || ((nameptr != NULL) && (*nameptr == '\0')))
1253 /* We need to make two passes over the string. The
1254 first to count the number of elements, the second
1259 if ( *nameptr == '/' )
1261 /* cope with multiple (useless) /s) */
1265 /* find the next / */
1266 name_end = strchr_m(nameptr, '/');
1268 /* oops - the last check for a / didn't find one. */
1269 if (name_end == NULL)
1272 /* next segment please */
1273 nameptr = name_end + 1;
1277 if(num_entries == 0)
1280 if(( (*ppname_array) = (name_compare_entry *)malloc(
1281 (num_entries + 1) * sizeof(name_compare_entry))) == NULL)
1283 DEBUG(0,("set_namearray: malloc fail\n"));
1287 /* Now copy out the names */
1292 if ( *nameptr == '/' )
1294 /* cope with multiple (useless) /s) */
1298 /* find the next / */
1299 if ((name_end = strchr_m(nameptr, '/')) != NULL)
1304 /* oops - the last check for a / didn't find one. */
1305 if(name_end == NULL)
1308 (*ppname_array)[i].is_wild = ms_has_wild(nameptr);
1309 if(((*ppname_array)[i].name = strdup(nameptr)) == NULL)
1311 DEBUG(0,("set_namearray: malloc fail (1)\n"));
1315 /* next segment please */
1316 nameptr = name_end + 1;
1320 (*ppname_array)[i].name = NULL;
1325 /****************************************************************************
1326 routine to free a namearray.
1327 ****************************************************************************/
1329 void free_namearray(name_compare_entry *name_array)
1331 if(name_array == NULL)
1334 SAFE_FREE(name_array->name);
1335 SAFE_FREE(name_array);
1338 /****************************************************************************
1339 Simple routine to do POSIX file locking. Cruft in NFS and 64->32 bit mapping
1340 is dealt with in posix.c
1341 ****************************************************************************/
1343 BOOL fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
1345 SMB_STRUCT_FLOCK lock;
1348 DEBUG(8,("fcntl_lock %d %d %.0f %.0f %d\n",fd,op,(double)offset,(double)count,type));
1351 lock.l_whence = SEEK_SET;
1352 lock.l_start = offset;
1356 ret = sys_fcntl_ptr(fd,op,&lock);
1358 if (ret == -1 && errno != 0)
1359 DEBUG(3,("fcntl_lock: fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
1362 if (op == SMB_F_GETLK)
1365 (lock.l_type != F_UNLCK) &&
1366 (lock.l_pid != 0) &&
1367 (lock.l_pid != sys_getpid()))
1369 DEBUG(3,("fcntl_lock: fd %d is locked by pid %d\n",fd,(int)lock.l_pid));
1373 /* it must be not locked or locked by me */
1377 /* a lock set or unset */
1380 DEBUG(3,("fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
1381 (double)offset,(double)count,op,type,strerror(errno)));
1385 /* everything went OK */
1386 DEBUG(8,("fcntl_lock: Lock call successful\n"));
1391 /*******************************************************************
1392 Is the name specified one of my netbios names.
1393 Returns true if it is equal, false otherwise.
1394 ********************************************************************/
1396 BOOL is_myname(char *s)
1401 for (n=0; my_netbios_names[n]; n++) {
1402 if (strequal(my_netbios_names[n], s))
1405 DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret));
1409 /********************************************************************
1410 Return only the first IP address of our configured interfaces
1412 ********************************************************************/
1414 const char *get_my_primary_ip (void)
1416 static fstring ip_string;
1418 struct iface_struct nics[MAX_INTERFACES];
1420 if ((n=get_interfaces(nics, MAX_INTERFACES)) <= 0)
1423 fstrcpy(ip_string, inet_ntoa(nics[0].ip));
1427 BOOL is_myname_or_ipaddr(char *s)
1431 /* optimize for the common case */
1432 if (strequal(s, global_myname))
1435 /* maybe its an IP address? */
1436 if (is_ipaddress(s)) {
1437 struct iface_struct nics[MAX_INTERFACES];
1441 ip = interpret_addr(s);
1442 if ((ip==0) || (ip==0xffffffff))
1445 n = get_interfaces(nics, MAX_INTERFACES);
1446 for (i=0; i<n; i++) {
1447 if (ip == nics[i].ip.s_addr)
1452 /* check for an alias */
1453 ptr = lp_netbios_aliases();
1454 for ( ; *ptr; ptr++ ) {
1455 if (StrCaseCmp(s, *ptr) == 0)
1463 /*******************************************************************
1464 Set the horrid remote_arch string based on an enum.
1465 ********************************************************************/
1467 void set_remote_arch(enum remote_arch_types type)
1469 extern fstring remote_arch;
1473 fstrcpy(remote_arch, "WfWg");
1476 fstrcpy(remote_arch, "OS2");
1479 fstrcpy(remote_arch, "Win95");
1482 fstrcpy(remote_arch, "WinNT");
1485 fstrcpy(remote_arch, "Win2K");
1488 fstrcpy(remote_arch,"Samba");
1491 ra_type = RA_UNKNOWN;
1492 fstrcpy(remote_arch, "UNKNOWN");
1497 /*******************************************************************
1498 Get the remote_arch type.
1499 ********************************************************************/
1501 enum remote_arch_types get_remote_arch(void)
1507 void out_ascii(FILE *f, unsigned char *buf,int len)
1511 fprintf(f, "%c", isprint(buf[i])?buf[i]:'.');
1514 void out_data(FILE *f,char *buf1,int len, int per_line)
1516 unsigned char *buf = (unsigned char *)buf1;
1522 fprintf(f, "[%03X] ",i);
1524 fprintf(f, "%02X ",(int)buf[i]);
1526 if (i%(per_line/2) == 0) fprintf(f, " ");
1527 if (i%per_line == 0) {
1528 out_ascii(f,&buf[i-per_line ],per_line/2); fprintf(f, " ");
1529 out_ascii(f,&buf[i-per_line/2],per_line/2); fprintf(f, "\n");
1530 if (i<len) fprintf(f, "[%03X] ",i);
1533 if ((i%per_line) != 0) {
1536 n = per_line - (i%per_line);
1538 if (n>(per_line/2)) fprintf(f, " ");
1542 n = MIN(per_line/2,i%per_line);
1543 out_ascii(f,&buf[i-(i%per_line)],n); fprintf(f, " ");
1544 n = (i%per_line) - n;
1545 if (n>0) out_ascii(f,&buf[i-n],n);
1550 void print_asc(int level, const unsigned char *buf,int len)
1554 DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.'));
1557 void dump_data(int level, const char *buf1,int len)
1559 const unsigned char *buf = (const unsigned char *)buf1;
1563 DEBUG(level,("[%03X] ",i));
1565 DEBUG(level,("%02X ",(int)buf[i]));
1567 if (i%8 == 0) DEBUG(level,(" "));
1569 print_asc(level,&buf[i-16],8); DEBUG(level,(" "));
1570 print_asc(level,&buf[i-8],8); DEBUG(level,("\n"));
1571 if (i<len) DEBUG(level,("[%03X] ",i));
1579 if (n>8) DEBUG(level,(" "));
1580 while (n--) DEBUG(level,(" "));
1583 print_asc(level,&buf[i-(i%16)],n); DEBUG(level,(" "));
1585 if (n>0) print_asc(level,&buf[i-n],n);
1586 DEBUG(level,("\n"));
1590 char *tab_depth(int depth)
1592 static pstring spaces;
1593 memset(spaces, ' ', depth * 4);
1594 spaces[depth * 4] = 0;
1598 /*****************************************************************************
1599 * Provide a checksum on a string
1601 * Input: s - the null-terminated character string for which the checksum
1602 * will be calculated.
1604 * Output: The checksum value calculated for s.
1606 * ****************************************************************************
1608 int str_checksum(const char *s)
1616 res ^= (c << (i % 15)) ^ (c >> (15-(i%15)));
1621 } /* str_checksum */
1625 /*****************************************************************
1626 zero a memory area then free it. Used to catch bugs faster
1627 *****************************************************************/
1628 void zero_free(void *p, size_t size)
1635 /*****************************************************************
1636 set our open file limit to a requested max and return the limit
1637 *****************************************************************/
1638 int set_maxfiles(int requested_max)
1640 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
1642 int saved_current_limit;
1644 if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1645 DEBUG(0,("set_maxfiles: getrlimit (1) for RLIMIT_NOFILE failed with error %s\n",
1648 return requested_max;
1652 * Set the fd limit to be real_max_open_files + MAX_OPEN_FUDGEFACTOR to
1653 * account for the extra fd we need
1654 * as well as the log files and standard
1655 * handles etc. Save the limit we want to set in case
1656 * we are running on an OS that doesn't support this limit (AIX)
1657 * which always returns RLIM_INFINITY for rlp.rlim_max.
1660 /* Try raising the hard (max) limit to the requested amount. */
1662 #if defined(RLIM_INFINITY)
1663 if (rlp.rlim_max != RLIM_INFINITY) {
1664 int orig_max = rlp.rlim_max;
1666 if ( rlp.rlim_max < requested_max )
1667 rlp.rlim_max = requested_max;
1669 /* This failing is not an error - many systems (Linux) don't
1670 support our default request of 10,000 open files. JRA. */
1672 if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1673 DEBUG(3,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d max files failed with error %s\n",
1674 (int)rlp.rlim_max, strerror(errno) ));
1676 /* Set failed - restore original value from get. */
1677 rlp.rlim_max = orig_max;
1682 /* Now try setting the soft (current) limit. */
1684 saved_current_limit = rlp.rlim_cur = MIN(requested_max,rlp.rlim_max);
1686 if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1687 DEBUG(0,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d files failed with error %s\n",
1688 (int)rlp.rlim_cur, strerror(errno) ));
1690 return saved_current_limit;
1693 if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1694 DEBUG(0,("set_maxfiles: getrlimit (2) for RLIMIT_NOFILE failed with error %s\n",
1697 return saved_current_limit;
1700 #if defined(RLIM_INFINITY)
1701 if(rlp.rlim_cur == RLIM_INFINITY)
1702 return saved_current_limit;
1705 if((int)rlp.rlim_cur > saved_current_limit)
1706 return saved_current_limit;
1708 return rlp.rlim_cur;
1709 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
1711 * No way to know - just guess...
1713 return requested_max;
1717 /*****************************************************************
1718 splits out the start of the key (HKLM or HKU) and the rest of the key
1719 *****************************************************************/
1720 BOOL reg_split_key(char *full_keyname, uint32 *reg_type, char *key_name)
1724 if (!next_token(&full_keyname, tmp, "\\", sizeof(tmp)))
1731 DEBUG(10, ("reg_split_key: hive %s\n", tmp));
1733 if (strequal(tmp, "HKLM") || strequal(tmp, "HKEY_LOCAL_MACHINE"))
1735 (*reg_type) = HKEY_LOCAL_MACHINE;
1737 else if (strequal(tmp, "HKU") || strequal(tmp, "HKEY_USERS"))
1739 (*reg_type) = HKEY_USERS;
1743 DEBUG(10,("reg_split_key: unrecognised hive key %s\n", tmp));
1747 if (next_token(&full_keyname, tmp, "\n\r", sizeof(tmp)))
1749 fstrcpy(key_name, tmp);
1756 DEBUG(10, ("reg_split_key: name %s\n", key_name));
1762 /*****************************************************************
1763 possibly replace mkstemp if it is broken
1764 *****************************************************************/
1765 int smb_mkstemp(char *template)
1767 #if HAVE_SECURE_MKSTEMP
1768 return mkstemp(template);
1770 /* have a reasonable go at emulating it. Hope that
1771 the system mktemp() isn't completly hopeless */
1772 char *p = mktemp(template);
1774 return open(p, O_CREAT|O_EXCL|O_RDWR, 0600);
1780 malloc that aborts with smb_panic on fail or zero size.
1782 void *smb_xmalloc(size_t size)
1786 smb_panic("smb_xmalloc: called with zero size.\n");
1787 if ((p = malloc(size)) == NULL)
1788 smb_panic("smb_xmalloc: malloc fail.\n");
1793 Memdup with smb_panic on fail.
1795 void *smb_xmemdup(const void *p, size_t size)
1798 p2 = smb_xmalloc(size);
1799 memcpy(p2, p, size);
1804 strdup that aborts on malloc fail.
1806 char *smb_xstrdup(const char *s)
1808 char *s1 = strdup(s);
1810 smb_panic("smb_xstrdup: malloc fail\n");
1815 vasprintf that aborts on malloc fail
1817 int smb_xvasprintf(char **ptr, const char *format, va_list ap)
1820 n = vasprintf(ptr, format, ap);
1821 if (n == -1 || ! *ptr) {
1822 smb_panic("smb_xvasprintf: out of memory");
1827 /*****************************************************************
1828 like strdup but for memory
1829 *****************************************************************/
1830 void *memdup(const void *p, size_t size)
1833 if (size == 0) return NULL;
1835 if (!p2) return NULL;
1836 memcpy(p2, p, size);
1840 /*****************************************************************
1841 get local hostname and cache result
1842 *****************************************************************/
1843 char *myhostname(void)
1853 /*****************************************************************
1854 a useful function for returning a path in the Samba lock directory
1855 *****************************************************************/
1856 char *lock_path(char *name)
1858 static pstring fname;
1860 pstrcpy(fname,lp_lockdir());
1861 trim_string(fname,"","/");
1863 if (!directory_exist(fname,NULL)) {
1868 pstrcat(fname,name);
1875 * @brief Returns an absolute path to a file in the Samba lib directory.
1877 * @param name File to find, relative to LIBDIR.
1879 * @retval Pointer to a static #pstring containing the full path.
1881 char *lib_path(char *name)
1883 static pstring fname;
1884 snprintf(fname, sizeof(fname), "%s/%s", dyn_LIBDIR, name);
1888 /*******************************************************************
1889 Given a filename - get its directory name
1890 NB: Returned in static storage. Caveats:
1891 o Not safe in thread environment.
1892 o Caller must not free.
1893 o If caller wishes to preserve, they should copy.
1894 ********************************************************************/
1896 char *parent_dirname(const char *path)
1898 static pstring dirpath;
1904 pstrcpy(dirpath, path);
1905 p = strrchr_m(dirpath, '/'); /* Find final '/', if any */
1907 pstrcpy(dirpath, "."); /* No final "/", so dir is "." */
1910 ++p; /* For root "/", leave "/" in place */
1917 /*******************************************************************
1918 determine if a pattern contains any Microsoft wildcard characters
1919 *******************************************************************/
1920 BOOL ms_has_wild(char *s)
1923 while ((c = *s++)) {
1936 BOOL ms_has_wild_w(const smb_ucs2_t *s)
1939 if (!s) return False;
1940 while ((c = *s++)) {
1942 case UCS2_CHAR('*'):
1943 case UCS2_CHAR('?'):
1944 case UCS2_CHAR('<'):
1945 case UCS2_CHAR('>'):
1946 case UCS2_CHAR('"'):
1953 /*******************************************************************
1954 a wrapper that handles case sensitivity and the special handling
1956 *******************************************************************/
1957 BOOL mask_match(char *string, char *pattern, BOOL is_case_sensitive)
1961 if (strcmp(string,"..") == 0) string = ".";
1962 if (strcmp(pattern,".") == 0) return False;
1964 if (is_case_sensitive) {
1965 return ms_fnmatch(pattern, string, Protocol) == 0;
1968 fstrcpy(p2, pattern);
1969 fstrcpy(s2, string);
1972 return ms_fnmatch(p2, s2, Protocol) == 0;
1975 /*********************************************************
1976 Recursive routine that is called by unix_wild_match.
1977 *********************************************************/
1979 static BOOL unix_do_match(char *regexp, char *str)
1983 for( p = regexp; *p && *str; ) {
1994 * Look for a character matching
1995 * the one after the '*'.
1999 return True; /* Automatic match */
2002 while(*str && (*p != *str))
2006 * Patch from weidel@multichart.de. In the case of the regexp
2007 * '*XX*' we want to ensure there are at least 2 'X' characters
2008 * in the string after the '*' for a match to be made.
2015 * Eat all the characters that match, but count how many there were.
2018 while(*str && (*p == *str)) {
2024 * Now check that if the regexp had n identical characters that
2025 * matchcount had at least that many matches.
2028 while ( *(p+1) && (*(p+1) == *p)) {
2033 if ( matchcount <= 0 )
2037 str--; /* We've eaten the match char after the '*' */
2039 if(unix_do_match(p, str))
2061 if (!*p && str[0] == '.' && str[1] == 0)
2064 if (!*str && *p == '?') {
2070 if(!*str && (*p == '*' && p[1] == '\0'))
2076 /*******************************************************************
2077 Simple case insensitive interface to a UNIX wildcard matcher.
2078 *******************************************************************/
2080 BOOL unix_wild_match(char *pattern, char *string)
2085 pstrcpy(p2, pattern);
2086 pstrcpy(s2, string);
2090 /* Remove any *? and ** from the pattern as they are meaningless */
2091 for(p = p2; *p; p++)
2092 while( *p == '*' && (p[1] == '?' ||p[1] == '*'))
2093 pstrcpy( &p[1], &p[2]);
2095 if (strequal(p2,"*"))
2098 return unix_do_match(p2, s2) == 0;
2101 /*******************************************************************
2103 *******************************************************************/
2104 static void free_data_blob(DATA_BLOB *d)
2106 if ((d) && (d->free)) {
2111 /*******************************************************************
2112 construct a data blob, must be freed with data_blob_free()
2113 you can pass NULL for p and get a blank data blob
2114 *******************************************************************/
2115 DATA_BLOB data_blob(const void *p, size_t length)
2125 ret.data = smb_xmemdup(p, length);
2127 ret.data = smb_xmalloc(length);
2129 ret.length = length;
2130 ret.free = free_data_blob;
2134 /*******************************************************************
2135 construct a data blob, using supplied TALLOC_CTX
2136 *******************************************************************/
2137 DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length)
2141 if (!p || !length) {
2146 ret.data = talloc_memdup(mem_ctx, p, length);
2147 if (ret.data == NULL)
2148 smb_panic("data_blob_talloc: talloc_memdup failed.\n");
2150 ret.length = length;
2155 /*******************************************************************
2157 *******************************************************************/
2158 void data_blob_free(DATA_BLOB *d)
2168 /*******************************************************************
2169 clear a DATA_BLOB's contents
2170 *******************************************************************/
2171 void data_blob_clear(DATA_BLOB *d)
2174 memset(d->data, 0, d->length);
2178 /*******************************************************************
2179 free a data blob and clear its contents
2180 *******************************************************************/
2181 void data_blob_clear_free(DATA_BLOB *d)
2189 /*******************************************************************
2190 This routine is a trick to immediately catch errors when debugging
2191 with insure. A xterm with a gdb is popped up when insure catches
2192 a error. It is Linux specific.
2193 ********************************************************************/
2194 int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6)
2199 /* you can get /usr/bin/backtrace from
2200 http://samba.org/ftp/unpacked/junkcode/backtrace */
2201 pstring cmd = "/usr/bin/backtrace %d";
2203 slprintf(pidstr, sizeof(pidstr)-1, "%d", sys_getpid());
2204 pstring_sub(cmd, "%d", pidstr);
2208 h = dlopen("/usr/local/parasoft/insure++lite/lib.linux2/libinsure.so", RTLD_LAZY);
2209 fn = dlsym(h, "_Insure_trap_error");
2211 if (!h || h == _Insure_trap_error) {
2212 h = dlopen("/usr/local/parasoft/lib.linux2/libinsure.so", RTLD_LAZY);
2213 fn = dlsym(h, "_Insure_trap_error");
2217 ret = fn(a1, a2, a3, a4, a5, a6);