2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #if (defined(NETGROUP) && defined (AUTOMOUNT))
26 #include <rpcsvc/nis.h>
28 #include "rpcsvc/ypclnt.h"
34 #undef Realloc /* SSLeay defines this and samba has a function of this name */
45 int Protocol = PROTOCOL_COREPLUS;
47 /* a default finfo structure to ensure all fields are sensible */
48 file_info def_finfo = {-1,0,0,0,0,0,0,""};
50 /* these are some file handles where debug info will be stored */
53 /* the client file descriptor */
56 /* the last IP received from */
57 struct in_addr lastip;
59 /* the last port received from */
62 /* this is used by the chaining code */
68 case handling on filenames
70 int case_default = CASE_LOWER;
75 /* the following control case operations - they are put here so the
76 client can link easily */
79 BOOL use_mangled_map = False;
80 BOOL short_case_preserve;
83 fstring remote_machine="";
84 fstring local_machine="";
85 fstring remote_arch="UNKNOWN";
86 static enum remote_arch_types ra_type = RA_UNKNOWN;
87 fstring remote_proto="UNKNOWN";
88 pstring myhostname="";
89 pstring user_socket_options="";
91 pstring sesssetup_user="";
92 pstring samlogon_user="";
94 BOOL sam_logon_in_ssb = False;
96 pstring global_myname = "";
97 fstring global_myworkgroup = "";
98 char **my_netbios_names;
100 int smb_read_error = 0;
102 static BOOL stdout_logging = False;
104 static char *filename_dos(char *path,char *buf);
107 /******************************************************************************
108 catch a sigusr2 - decrease the debug log level.
109 *****************************************************************************/
112 BlockSignals( True, SIGUSR2);
119 DEBUG( 0, ( "Got SIGUSR2 set debug level to %d.\n", DEBUGLEVEL ) );
121 BlockSignals( False, SIGUSR2);
122 #ifndef DONT_REINSTALL_SIG
123 signal(SIGUSR2, SIGNAL_CAST sig_usr2);
130 /**************************************************************************** **
131 catch a sigusr1 - increase the debug log level.
132 **************************************************************************** */
135 BlockSignals( True, SIGUSR1);
142 DEBUG( 0, ( "Got SIGUSR1 set debug level to %d.\n", DEBUGLEVEL ) );
144 BlockSignals( False, SIGUSR1);
145 #ifndef DONT_REINSTALL_SIG
146 signal(SIGUSR1, SIGNAL_CAST sig_usr1);
153 /*******************************************************************
154 get ready for syslog stuff
155 ******************************************************************/
156 void setup_logging(char *pname,BOOL interactive)
160 char *p = strrchr(pname,'/');
163 openlog(pname, LOG_PID, SYSLOG_FACILITY);
164 #else /* for old systems that have no facility codes. */
165 openlog(pname, LOG_PID);
170 stdout_logging = True;
176 BOOL append_log=False;
179 /****************************************************************************
181 ****************************************************************************/
182 void reopen_logs(void)
188 pstrcpy(fname,debugf);
189 if (lp_loaded() && (*lp_logfile()))
190 pstrcpy(fname,lp_logfile());
192 if (!strcsequal(fname,debugf) || !dbf || !file_exist(debugf,NULL))
194 int oldumask = umask(022);
195 pstrcpy(debugf,fname);
196 if (dbf) fclose(dbf);
198 dbf = fopen(debugf,"a");
200 dbf = fopen(debugf,"w");
201 if (dbf) setbuf(dbf,NULL);
216 /*******************************************************************
217 check if the log has grown too big
218 ********************************************************************/
219 static void check_log_size(void)
221 static int debug_count=0;
225 if (debug_count++ < 100 || getuid() != 0) return;
227 maxlog = lp_max_log_size() * 1024;
228 if (!dbf || maxlog <= 0) return;
230 if (fstat(fileno(dbf),&st) == 0 && st.st_size > maxlog) {
231 fclose(dbf); dbf = NULL;
233 if (dbf && file_size(debugf) > maxlog) {
235 fclose(dbf); dbf = NULL;
236 slprintf(name,sizeof(name)-1,"%s.old",debugf);
245 /*******************************************************************
246 write an debug message on the debugfile. This is called by the DEBUG
248 ********************************************************************/
250 int Debug1(char *format_str, ...)
259 int old_errno = errno;
261 if (stdout_logging) {
263 va_start(ap, format_str);
266 format_str = va_arg(ap,char *);
268 vfprintf(dbf,format_str,ap);
275 if (!lp_syslog_only())
279 int oldumask = umask(022);
281 dbf = fopen(debugf,"a");
283 dbf = fopen(debugf,"w");
295 if (syslog_level < lp_syslog())
298 * map debug levels to syslog() priorities
299 * note that not all DEBUG(0, ...) calls are
302 static int priority_map[] = {
311 if (syslog_level >= sizeof(priority_map) / sizeof(priority_map[0]) ||
313 priority = LOG_DEBUG;
315 priority = priority_map[syslog_level];
318 va_start(ap, format_str);
321 format_str = va_arg(ap,char *);
323 vslprintf(msgbuf, sizeof(msgbuf)-1,format_str, ap);
327 syslog(priority, "%s", msgbuf);
332 if (!lp_syslog_only())
336 va_start(ap, format_str);
339 format_str = va_arg(ap,char *);
341 vfprintf(dbf,format_str,ap);
353 /****************************************************************************
354 find a suitable temporary directory. The result should be copied immediately
355 as it may be overwritten by a subsequent call
356 ****************************************************************************/
360 if ((p = getenv("TMPDIR"))) {
368 /****************************************************************************
369 determine if a file descriptor is in fact a socket
370 ****************************************************************************/
371 BOOL is_a_socket(int fd)
375 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
379 static char *last_ptr=NULL;
381 /****************************************************************************
382 Get the next token from a string, return False if none found
383 handles double-quotes.
384 Based on a routine by GJC@VILLAGE.COM.
385 Extensively modified by Andrew.Tridgell@anu.edu.au
386 ****************************************************************************/
387 BOOL next_token(char **ptr,char *buff,char *sep)
392 if (!ptr) ptr = &last_ptr;
393 if (!ptr) return(False);
397 /* default to simple separators */
398 if (!sep) sep = " \t\n\r";
400 /* find the first non sep char */
401 while(*s && strchr(sep,*s)) s++;
404 if (! *s) return(False);
406 /* copy over the token */
407 for (quoted = False; *s && (quoted || !strchr(sep,*s)); s++)
415 *ptr = (*s) ? s+1 : s;
422 /****************************************************************************
423 Convert list of tokens to array; dependent on above routine.
424 Uses last_ptr from above - bit of a hack.
425 ****************************************************************************/
426 char **toktocliplist(int *ctok, char *sep)
432 if (!sep) sep = " \t\n\r";
434 while(*s && strchr(sep,*s)) s++;
437 if (!*s) return(NULL);
441 while(*s && (!strchr(sep,*s))) s++;
442 while(*s && strchr(sep,*s)) *s++=0;
448 if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL;
460 /*******************************************************************
461 safely copies memory, ensuring no overlap problems.
462 this is only used if the machine does not have it's own memmove().
463 this is not the fastest algorithm in town, but it will do for our
465 ********************************************************************/
466 void *MemMove(void *dest,void *src,int size)
470 if (dest==src || !size) return(dest);
472 d = (unsigned long)dest;
473 s = (unsigned long)src;
475 if ((d >= (s+size)) || (s >= (d+size))) {
477 memcpy(dest,src,size);
483 /* we can forward copy */
484 if (s-d >= sizeof(int) &&
485 !(s%sizeof(int)) && !(d%sizeof(int)) && !(size%sizeof(int))) {
486 /* do it all as words */
487 int *idest = (int *)dest;
488 int *isrc = (int *)src;
490 for (i=0;i<size;i++) idest[i] = isrc[i];
493 char *cdest = (char *)dest;
494 char *csrc = (char *)src;
495 for (i=0;i<size;i++) cdest[i] = csrc[i];
500 /* must backward copy */
501 if (d-s >= sizeof(int) &&
502 !(s%sizeof(int)) && !(d%sizeof(int)) && !(size%sizeof(int))) {
503 /* do it all as words */
504 int *idest = (int *)dest;
505 int *isrc = (int *)src;
507 for (i=size-1;i>=0;i--) idest[i] = isrc[i];
510 char *cdest = (char *)dest;
511 char *csrc = (char *)src;
512 for (i=size-1;i>=0;i--) cdest[i] = csrc[i];
520 /****************************************************************************
521 prompte a dptr (to make it recently used)
522 ****************************************************************************/
523 void array_promote(char *array,int elsize,int element)
529 p = (char *)malloc(elsize);
533 DEBUG(5,("Ahh! Can't malloc\n"));
536 memcpy(p,array + element * elsize, elsize);
537 memmove(array + elsize,array,elsize*element);
538 memcpy(array,p,elsize);
542 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
551 } socket_options[] = {
552 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
553 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
554 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
556 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
558 #ifdef IPTOS_LOWDELAY
559 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
561 #ifdef IPTOS_THROUGHPUT
562 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
565 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
568 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
571 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
574 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
577 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
580 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
586 /****************************************************************************
587 set user socket options
588 ****************************************************************************/
589 void set_socket_options(int fd, char *options)
593 while (next_token(&options,tok," \t,"))
598 BOOL got_value = False;
600 if ((p = strchr(tok,'=')))
607 for (i=0;socket_options[i].name;i++)
608 if (strequal(socket_options[i].name,tok))
611 if (!socket_options[i].name)
613 DEBUG(0,("Unknown socket option %s\n",tok));
617 switch (socket_options[i].opttype)
621 ret = setsockopt(fd,socket_options[i].level,
622 socket_options[i].option,(char *)&value,sizeof(int));
627 DEBUG(0,("syntax error - %s does not take a value\n",tok));
630 int on = socket_options[i].value;
631 ret = setsockopt(fd,socket_options[i].level,
632 socket_options[i].option,(char *)&on,sizeof(int));
638 DEBUG(0,("Failed to set socket option %s\n",tok));
644 /****************************************************************************
645 close the socket communication
646 ****************************************************************************/
647 void close_sockets(void )
650 sslutil_disconnect(Client);
657 /****************************************************************************
658 determine whether we are in the specified group
659 ****************************************************************************/
660 BOOL in_group(gid_t group, int current_gid, int ngroups, int *groups)
664 if (group == current_gid) return(True);
666 for (i=0;i<ngroups;i++)
667 if (group == groups[i])
673 /****************************************************************************
674 this is a safer strcpy(), meant to prevent core dumps when nasty things happen
675 ****************************************************************************/
676 char *StrCpy(char *dest,char *src)
681 /* I don't want to get lazy with these ... */
683 DEBUG(0,("ERROR: NULL StrCpy() called!\n"));
688 if (!dest) return(NULL);
693 while ((*d++ = *src++)) ;
697 /****************************************************************************
698 line strncpy but always null terminates. Make sure there is room!
699 ****************************************************************************/
700 char *StrnCpy(char *dest,char *src,int n)
703 if (!dest) return(NULL);
708 while (n-- && (*d++ = *src++)) ;
714 /*******************************************************************
715 copy an IP address from one buffer to another
716 ********************************************************************/
717 void putip(void *dest,void *src)
723 /****************************************************************************
724 interpret the weird netbios "name". Return the name type
725 ****************************************************************************/
726 static int name_interpret(char *in,char *out)
729 int len = (*in++) / 2;
733 if (len > 30 || len<1) return(0);
737 if (in[0] < 'A' || in[0] > 'P' || in[1] < 'A' || in[1] > 'P') {
741 *out = ((in[0]-'A')<<4) + (in[1]-'A');
749 /* Handle any scope names */
752 *out++ = '.'; /* Scope names are separated by periods */
753 len = *(unsigned char *)in++;
754 StrnCpy(out, in, len);
763 /****************************************************************************
764 mangle a name into netbios format
766 Note: <Out> must be (33 + strlen(scope) + 2) bytes long, at minimum.
767 ****************************************************************************/
768 int name_mangle( char *In, char *Out, char name_type )
776 /* Safely copy the input string, In, into buf[]. */
777 (void)memset( buf, 0, 20 );
781 (void)slprintf( buf, sizeof(buf) - 1, "%-15.15s%c", In, name_type );
783 /* Place the length of the first field into the output buffer. */
787 /* Now convert the name to the rfc1001/1002 format. */
788 for( i = 0; i < 16; i++ )
790 c = toupper( buf[i] );
791 p[i*2] = ( (c >> 4) & 0x000F ) + 'A';
792 p[(i*2)+1] = (c & 0x000F) + 'A';
797 /* Add the scope string. */
798 for( i = 0, len = 0; NULL != scope; i++, len++ )
806 return( name_len(Out) );
818 return( name_len(Out) );
821 /*******************************************************************
822 check if a file exists
823 ********************************************************************/
824 BOOL file_exist(char *fname,struct stat *sbuf)
827 if (!sbuf) sbuf = &st;
829 if (sys_stat(fname,sbuf) != 0)
832 return(S_ISREG(sbuf->st_mode));
835 /*******************************************************************
836 check a files mod time
837 ********************************************************************/
838 time_t file_modtime(char *fname)
842 if (sys_stat(fname,&st) != 0)
848 /*******************************************************************
849 check if a directory exists
850 ********************************************************************/
851 BOOL directory_exist(char *dname,struct stat *st)
858 if (sys_stat(dname,st) != 0)
861 ret = S_ISDIR(st->st_mode);
867 /*******************************************************************
868 returns the size in bytes of the named file
869 ********************************************************************/
870 uint32 file_size(char *file_name)
874 sys_stat(file_name,&buf);
878 /*******************************************************************
879 return a string representing an attribute for a file
880 ********************************************************************/
881 char *attrib_string(int mode)
883 static fstring attrstr;
887 if (mode & aVOLID) fstrcat(attrstr,"V");
888 if (mode & aDIR) fstrcat(attrstr,"D");
889 if (mode & aARCH) fstrcat(attrstr,"A");
890 if (mode & aHIDDEN) fstrcat(attrstr,"H");
891 if (mode & aSYSTEM) fstrcat(attrstr,"S");
892 if (mode & aRONLY) fstrcat(attrstr,"R");
898 /*******************************************************************
899 case insensitive string compararison
900 ********************************************************************/
901 int StrCaseCmp(char *s, char *t)
903 /* compare until we run out of string, either t or s, or find a difference */
904 /* We *must* use toupper rather than tolower here due to the
905 asynchronous upper to lower mapping.
907 #if !defined(KANJI_WIN95_COMPATIBILITY)
909 * For completeness we should put in equivalent code for code pages
910 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
911 * doubt anyone wants Samba to behave differently from Win95 and WinNT
912 * here. They both treat full width ascii characters as case senstive
913 * filenames (ie. they don't do the work we do here).
917 if(lp_client_code_page() == KANJI_CODEPAGE)
919 /* Win95 treats full width ascii characters as case sensitive. */
924 return toupper (*s) - toupper (*t);
925 else if (is_sj_alph (*s) && is_sj_alph (*t))
927 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
933 else if (is_shift_jis (*s) && is_shift_jis (*t))
935 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
938 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
944 else if (is_shift_jis (*s))
946 else if (is_shift_jis (*t))
950 diff = toupper (*s) - toupper (*t);
959 #endif /* KANJI_WIN95_COMPATIBILITY */
961 while (*s && *t && toupper(*s) == toupper(*t))
967 return(toupper(*s) - toupper(*t));
971 /*******************************************************************
972 case insensitive string compararison, length limited
973 ********************************************************************/
974 int StrnCaseCmp(char *s, char *t, int n)
976 /* compare until we run out of string, either t or s, or chars */
977 /* We *must* use toupper rather than tolower here due to the
978 asynchronous upper to lower mapping.
980 #if !defined(KANJI_WIN95_COMPATIBILITY)
982 * For completeness we should put in equivalent code for code pages
983 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
984 * doubt anyone wants Samba to behave differently from Win95 and WinNT
985 * here. They both treat full width ascii characters as case senstive
986 * filenames (ie. they don't do the work we do here).
990 if(lp_client_code_page() == KANJI_CODEPAGE)
992 /* Win95 treats full width ascii characters as case sensitive. */
997 return toupper (*s) - toupper (*t);
998 else if (is_sj_alph (*s) && is_sj_alph (*t))
1000 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
1007 else if (is_shift_jis (*s) && is_shift_jis (*t))
1009 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
1012 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
1019 else if (is_shift_jis (*s))
1021 else if (is_shift_jis (*t))
1025 diff = toupper (*s) - toupper (*t);
1036 #endif /* KANJI_WIN95_COMPATIBILITY */
1038 while (n && *s && *t && toupper(*s) == toupper(*t))
1045 /* not run out of chars - strings are different lengths */
1047 return(toupper(*s) - toupper(*t));
1049 /* identical up to where we run out of chars,
1050 and strings are same length */
1055 /*******************************************************************
1057 ********************************************************************/
1058 BOOL strequal(char *s1, char *s2)
1060 if (s1 == s2) return(True);
1061 if (!s1 || !s2) return(False);
1063 return(StrCaseCmp(s1,s2)==0);
1066 /*******************************************************************
1067 compare 2 strings up to and including the nth char.
1068 ******************************************************************/
1069 BOOL strnequal(char *s1,char *s2,int n)
1071 if (s1 == s2) return(True);
1072 if (!s1 || !s2 || !n) return(False);
1074 return(StrnCaseCmp(s1,s2,n)==0);
1077 /*******************************************************************
1078 compare 2 strings (case sensitive)
1079 ********************************************************************/
1080 BOOL strcsequal(char *s1,char *s2)
1082 if (s1 == s2) return(True);
1083 if (!s1 || !s2) return(False);
1085 return(strcmp(s1,s2)==0);
1089 /*******************************************************************
1090 convert a string to lower case
1091 ********************************************************************/
1092 void strlower(char *s)
1096 #if !defined(KANJI_WIN95_COMPATIBILITY)
1098 * For completeness we should put in equivalent code for code pages
1099 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
1100 * doubt anyone wants Samba to behave differently from Win95 and WinNT
1101 * here. They both treat full width ascii characters as case senstive
1102 * filenames (ie. they don't do the work we do here).
1106 if(lp_client_code_page() == KANJI_CODEPAGE)
1108 /* Win95 treats full width ascii characters as case sensitive. */
1109 if (is_shift_jis (*s))
1111 if (is_sj_upper (s[0], s[1]))
1112 s[1] = sj_tolower2 (s[1]);
1115 else if (is_kana (*s))
1127 #endif /* KANJI_WIN95_COMPATIBILITY */
1129 int skip = skip_multibyte_char( *s );
1142 /*******************************************************************
1143 convert a string to upper case
1144 ********************************************************************/
1145 void strupper(char *s)
1149 #if !defined(KANJI_WIN95_COMPATIBILITY)
1151 * For completeness we should put in equivalent code for code pages
1152 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
1153 * doubt anyone wants Samba to behave differently from Win95 and WinNT
1154 * here. They both treat full width ascii characters as case senstive
1155 * filenames (ie. they don't do the work we do here).
1159 if(lp_client_code_page() == KANJI_CODEPAGE)
1161 /* Win95 treats full width ascii characters as case sensitive. */
1162 if (is_shift_jis (*s))
1164 if (is_sj_lower (s[0], s[1]))
1165 s[1] = sj_toupper2 (s[1]);
1168 else if (is_kana (*s))
1180 #endif /* KANJI_WIN95_COMPATIBILITY */
1182 int skip = skip_multibyte_char( *s );
1195 /*******************************************************************
1196 convert a string to "normal" form
1197 ********************************************************************/
1198 void strnorm(char *s)
1200 if (case_default == CASE_UPPER)
1206 /*******************************************************************
1207 check if a string is in "normal" case
1208 ********************************************************************/
1209 BOOL strisnormal(char *s)
1211 if (case_default == CASE_UPPER)
1212 return(!strhaslower(s));
1214 return(!strhasupper(s));
1218 /****************************************************************************
1220 ****************************************************************************/
1221 void string_replace(char *s,char oldc,char newc)
1226 skip = skip_multibyte_char( *s );
1238 /****************************************************************************
1239 make a file into unix format
1240 ****************************************************************************/
1241 void unix_format(char *fname)
1244 string_replace(fname,'\\','/');
1248 pstrcpy(namecopy,fname);
1250 pstrcat(fname,namecopy);
1254 /****************************************************************************
1255 make a file into dos format
1256 ****************************************************************************/
1257 void dos_format(char *fname)
1259 string_replace(fname,'/','\\');
1262 /*******************************************************************
1263 show a smb message structure
1264 ********************************************************************/
1265 void show_msg(char *buf)
1270 if (DEBUGLEVEL < 5) return;
1272 DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
1274 (int)CVAL(buf,smb_com),
1275 (int)CVAL(buf,smb_rcls),
1276 (int)CVAL(buf,smb_reh),
1277 (int)SVAL(buf,smb_err),
1278 (int)CVAL(buf,smb_flg),
1279 (int)SVAL(buf,smb_flg2)));
1280 DEBUG(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\nsmt_wct=%d\n",
1281 (int)SVAL(buf,smb_tid),
1282 (int)SVAL(buf,smb_pid),
1283 (int)SVAL(buf,smb_uid),
1284 (int)SVAL(buf,smb_mid),
1285 (int)CVAL(buf,smb_wct)));
1287 for (i=0;i<(int)CVAL(buf,smb_wct);i++)
1289 DEBUG(5,("smb_vwv[%d]=%d (0x%X)\n",i,
1290 SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
1293 bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
1295 DEBUG(5,("smb_bcc=%d\n",bcc));
1297 if (DEBUGLEVEL < 10) return;
1299 if (DEBUGLEVEL < 50)
1301 bcc = MIN(bcc, 512);
1304 dump_data(10, smb_buf(buf), bcc);
1306 /*******************************************************************
1307 return the length of an smb packet
1308 ********************************************************************/
1309 int smb_len(char *buf)
1311 return( PVAL(buf,3) | (PVAL(buf,2)<<8) | ((PVAL(buf,1)&1)<<16) );
1314 /*******************************************************************
1315 set the length of an smb packet
1316 ********************************************************************/
1317 void _smb_setlen(char *buf,int len)
1320 buf[1] = (len&0x10000)>>16;
1321 buf[2] = (len&0xFF00)>>8;
1325 /*******************************************************************
1326 set the length and marker of an smb packet
1327 ********************************************************************/
1328 void smb_setlen(char *buf,int len)
1330 _smb_setlen(buf,len);
1338 /*******************************************************************
1339 setup the word count and byte count for a smb message
1340 ********************************************************************/
1341 int set_message(char *buf,int num_words,int num_bytes,BOOL zero)
1344 bzero(buf + smb_size,num_words*2 + num_bytes);
1345 CVAL(buf,smb_wct) = num_words;
1346 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
1347 smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
1348 return (smb_size + num_words*2 + num_bytes);
1351 /*******************************************************************
1352 return the number of smb words
1353 ********************************************************************/
1354 int smb_numwords(char *buf)
1356 return (CVAL(buf,smb_wct));
1359 /*******************************************************************
1360 return the size of the smb_buf region of a message
1361 ********************************************************************/
1362 int smb_buflen(char *buf)
1364 return(SVAL(buf,smb_vwv0 + smb_numwords(buf)*2));
1367 /*******************************************************************
1368 return a pointer to the smb_buf data area
1369 ********************************************************************/
1370 int smb_buf_ofs(char *buf)
1372 return (smb_size + CVAL(buf,smb_wct)*2);
1375 /*******************************************************************
1376 return a pointer to the smb_buf data area
1377 ********************************************************************/
1378 char *smb_buf(char *buf)
1380 return (buf + smb_buf_ofs(buf));
1383 /*******************************************************************
1384 return the SMB offset into an SMB buffer
1385 ********************************************************************/
1386 int smb_offset(char *p,char *buf)
1388 return(PTR_DIFF(p,buf+4) + chain_size);
1392 /*******************************************************************
1393 skip past some strings in a buffer
1394 ********************************************************************/
1395 char *skip_string(char *buf,int n)
1398 buf += strlen(buf) + 1;
1402 /*******************************************************************
1403 trim the specified elements off the front and back of a string
1404 ********************************************************************/
1405 BOOL trim_string(char *s,char *front,char *back)
1408 while (front && *front && strncmp(s,front,strlen(front)) == 0)
1414 if (!(*p = p[strlen(front)]))
1419 while (back && *back && strlen(s) >= strlen(back) &&
1420 (strncmp(s+strlen(s)-strlen(back),back,strlen(back))==0))
1423 s[strlen(s)-strlen(back)] = 0;
1429 /*******************************************************************
1430 reduce a file name, removing .. elements.
1431 ********************************************************************/
1432 void dos_clean_name(char *s)
1436 DEBUG(3,("dos_clean_name [%s]\n",s));
1438 /* remove any double slashes */
1439 string_sub(s, "\\\\", "\\");
1441 while ((p = strstr(s,"\\..\\")) != NULL)
1448 if ((p=strrchr(s,'\\')) != NULL)
1455 trim_string(s,NULL,"\\..");
1457 string_sub(s, "\\.\\", "\\");
1460 /*******************************************************************
1461 reduce a file name, removing .. elements.
1462 ********************************************************************/
1463 void unix_clean_name(char *s)
1467 DEBUG(3,("unix_clean_name [%s]\n",s));
1469 /* remove any double slashes */
1470 string_sub(s, "//","/");
1472 /* Remove leading ./ characters */
1473 if(strncmp(s, "./", 2) == 0) {
1474 trim_string(s, "./", NULL);
1479 while ((p = strstr(s,"/../")) != NULL)
1486 if ((p=strrchr(s,'/')) != NULL)
1493 trim_string(s,NULL,"/..");
1497 /*******************************************************************
1498 a wrapper for the normal chdir() function
1499 ********************************************************************/
1500 int ChDir(char *path)
1503 static pstring LastDir="";
1505 if (strcsequal(path,".")) return(0);
1507 if (*path == '/' && strcsequal(LastDir,path)) return(0);
1508 DEBUG(3,("chdir to %s\n",path));
1509 res = sys_chdir(path);
1511 pstrcpy(LastDir,path);
1515 /* number of list structures for a caching GetWd function. */
1516 #define MAX_GETWDCACHE (50)
1524 } ino_list[MAX_GETWDCACHE];
1526 BOOL use_getwd_cache=True;
1528 /*******************************************************************
1529 return the absolute current directory path
1530 ********************************************************************/
1531 char *GetWd(char *str)
1534 static BOOL getwd_cache_init = False;
1535 struct stat st, st2;
1540 if (!use_getwd_cache)
1541 return(sys_getwd(str));
1543 /* init the cache */
1544 if (!getwd_cache_init)
1546 getwd_cache_init = True;
1547 for (i=0;i<MAX_GETWDCACHE;i++)
1549 string_init(&ino_list[i].text,"");
1550 ino_list[i].valid = False;
1554 /* Get the inode of the current directory, if this doesn't work we're
1557 if (stat(".",&st) == -1)
1559 DEBUG(0,("Very strange, couldn't stat \".\"\n"));
1560 return(sys_getwd(str));
1564 for (i=0; i<MAX_GETWDCACHE; i++)
1565 if (ino_list[i].valid)
1568 /* If we have found an entry with a matching inode and dev number
1569 then find the inode number for the directory in the cached string.
1570 If this agrees with that returned by the stat for the current
1571 directory then all is o.k. (but make sure it is a directory all
1574 if (st.st_ino == ino_list[i].inode &&
1575 st.st_dev == ino_list[i].dev)
1577 if (stat(ino_list[i].text,&st2) == 0)
1579 if (st.st_ino == st2.st_ino &&
1580 st.st_dev == st2.st_dev &&
1581 (st2.st_mode & S_IFMT) == S_IFDIR)
1583 pstrcpy (str, ino_list[i].text);
1585 /* promote it for future use */
1586 array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
1591 /* If the inode is different then something's changed,
1592 scrub the entry and start from scratch. */
1593 ino_list[i].valid = False;
1600 /* We don't have the information to hand so rely on traditional methods.
1601 The very slow getcwd, which spawns a process on some systems, or the
1602 not quite so bad getwd. */
1606 DEBUG(0,("Getwd failed, errno %s\n",strerror(errno)));
1612 DEBUG(5,("GetWd %s, inode %d, dev %x\n",s,(int)st.st_ino,(int)st.st_dev));
1614 /* add it to the cache */
1615 i = MAX_GETWDCACHE - 1;
1616 string_set(&ino_list[i].text,s);
1617 ino_list[i].dev = st.st_dev;
1618 ino_list[i].inode = st.st_ino;
1619 ino_list[i].valid = True;
1621 /* put it at the top of the list */
1622 array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
1629 /*******************************************************************
1630 reduce a file name, removing .. elements and checking that
1631 it is below dir in the heirachy. This uses GetWd() and so must be run
1632 on the system that has the referenced file system.
1634 widelinks are allowed if widelinks is true
1635 ********************************************************************/
1636 BOOL reduce_name(char *s,char *dir,BOOL widelinks)
1638 #ifndef REDUCE_PATHS
1646 BOOL relative = (*s != '/');
1648 *dir2 = *wd = *base_name = *newname = 0;
1653 /* can't have a leading .. */
1654 if (strncmp(s,"..",2) == 0 && (s[2]==0 || s[2]=='/'))
1656 DEBUG(3,("Illegal file name? (%s)\n",s));
1666 DEBUG(3,("reduce_name [%s] [%s]\n",s,dir));
1668 /* remove any double slashes */
1669 string_sub(s,"//","/");
1671 pstrcpy(base_name,s);
1672 p = strrchr(base_name,'/');
1679 DEBUG(0,("couldn't getwd for %s %s\n",s,dir));
1683 if (ChDir(dir) != 0)
1685 DEBUG(0,("couldn't chdir to %s\n",dir));
1691 DEBUG(0,("couldn't getwd for %s\n",dir));
1697 if (p && (p != base_name))
1700 if (strcmp(p+1,".")==0)
1702 if (strcmp(p+1,"..")==0)
1706 if (ChDir(base_name) != 0)
1709 DEBUG(3,("couldn't chdir for %s %s basename=%s\n",s,dir,base_name));
1713 if (!GetWd(newname))
1716 DEBUG(2,("couldn't get wd for %s %s\n",s,dir2));
1720 if (p && (p != base_name))
1722 pstrcat(newname,"/");
1723 pstrcat(newname,p+1);
1727 int l = strlen(dir2);
1728 if (dir2[l-1] == '/')
1731 if (strncmp(newname,dir2,l) != 0)
1734 DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n",s,dir2,newname,l));
1740 if (newname[l] == '/')
1741 pstrcpy(s,newname + l + 1);
1743 pstrcpy(s,newname+l);
1754 DEBUG(3,("reduced to %s\n",s));
1759 /****************************************************************************
1761 ****************************************************************************/
1762 static void expand_one(char *Mask,int len)
1765 while ((p1 = strchr(Mask,'*')) != NULL)
1767 int lfill = (len+1) - strlen(Mask);
1768 int l1= (p1 - Mask);
1771 memset(tmp+l1,'?',lfill);
1772 pstrcpy(tmp + l1 + lfill,Mask + l1 + 1);
1777 /****************************************************************************
1778 expand a wildcard expression, replacing *s with ?s
1779 ****************************************************************************/
1780 void expand_mask(char *Mask,BOOL doext)
1785 BOOL hasdot = False;
1787 BOOL absolute = (*Mask == '\\');
1789 *mbeg = *mext = *dirpart = *filepart = 0;
1791 /* parse the directory and filename */
1792 if (strchr(Mask,'\\'))
1793 dirname_dos(Mask,dirpart);
1795 filename_dos(Mask,filepart);
1797 pstrcpy(mbeg,filepart);
1798 if ((p1 = strchr(mbeg,'.')) != NULL)
1808 if (strlen(mbeg) > 8)
1810 pstrcpy(mext,mbeg + 8);
1816 pstrcpy(mbeg,"????????");
1817 if ((*mext == 0) && doext && !hasdot)
1818 pstrcpy(mext,"???");
1820 if (strequal(mbeg,"*") && *mext==0)
1828 pstrcpy(Mask,dirpart);
1829 if (*dirpart || absolute) pstrcat(Mask,"\\");
1834 DEBUG(6,("Mask expanded to [%s]\n",Mask));
1838 /****************************************************************************
1839 does a string have any uppercase chars in it?
1840 ****************************************************************************/
1841 BOOL strhasupper(char *s)
1845 #if !defined(KANJI_WIN95_COMPATIBILITY)
1847 * For completeness we should put in equivalent code for code pages
1848 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
1849 * doubt anyone wants Samba to behave differently from Win95 and WinNT
1850 * here. They both treat full width ascii characters as case senstive
1851 * filenames (ie. they don't do the work we do here).
1855 if(lp_client_code_page() == KANJI_CODEPAGE)
1857 /* Win95 treats full width ascii characters as case sensitive. */
1858 if (is_shift_jis (*s))
1860 else if (is_kana (*s))
1870 #endif /* KANJI_WIN95_COMPATIBILITY */
1872 int skip = skip_multibyte_char( *s );
1885 /****************************************************************************
1886 does a string have any lowercase chars in it?
1887 ****************************************************************************/
1888 BOOL strhaslower(char *s)
1892 #if !defined(KANJI_WIN95_COMPATIBILITY)
1894 * For completeness we should put in equivalent code for code pages
1895 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
1896 * doubt anyone wants Samba to behave differently from Win95 and WinNT
1897 * here. They both treat full width ascii characters as case senstive
1898 * filenames (ie. they don't do the work we do here).
1902 if(lp_client_code_page() == KANJI_CODEPAGE)
1904 /* Win95 treats full width ascii characters as case sensitive. */
1905 if (is_shift_jis (*s))
1907 if (is_sj_upper (s[0], s[1]))
1909 if (is_sj_lower (s[0], s[1]))
1913 else if (is_kana (*s))
1925 #endif /* KANJI_WIN95_COMPATIBILITY */
1927 int skip = skip_multibyte_char( *s );
1940 /****************************************************************************
1941 find the number of chars in a string
1942 ****************************************************************************/
1943 int count_chars(char *s,char c)
1947 #if !defined(KANJI_WIN95_COMPATIBILITY)
1949 * For completeness we should put in equivalent code for code pages
1950 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
1951 * doubt anyone wants Samba to behave differently from Win95 and WinNT
1952 * here. They both treat full width ascii characters as case senstive
1953 * filenames (ie. they don't do the work we do here).
1957 if(lp_client_code_page() == KANJI_CODEPAGE)
1959 /* Win95 treats full width ascii characters as case sensitive. */
1962 if (is_shift_jis (*s))
1973 #endif /* KANJI_WIN95_COMPATIBILITY */
1977 int skip = skip_multibyte_char( *s );
1991 /****************************************************************************
1993 ****************************************************************************/
1994 void make_dir_struct(char *buf,char *mask,char *fname,unsigned int size,int mode,time_t date)
1999 pstrcpy(mask2,mask);
2001 if ((mode & aDIR) != 0)
2004 memset(buf+1,' ',11);
2005 if ((p = strchr(mask2,'.')) != NULL)
2008 memcpy(buf+1,mask2,MIN(strlen(mask2),8));
2009 memcpy(buf+9,p+1,MIN(strlen(p+1),3));
2013 memcpy(buf+1,mask2,MIN(strlen(mask2),11));
2015 bzero(buf+21,DIR_STRUCT_SIZE-21);
2016 CVAL(buf,21) = mode;
2017 put_dos_date(buf,22,date);
2018 SSVAL(buf,26,size & 0xFFFF);
2019 SSVAL(buf,28,size >> 16);
2020 StrnCpy(buf+30,fname,12);
2021 if (!case_sensitive)
2023 DEBUG(8,("put name [%s] into dir struct\n",buf+30));
2027 /*******************************************************************
2028 close the low 3 fd's and open dev/null in their place
2029 ********************************************************************/
2030 void close_low_fds(void)
2034 close(0); close(1); close(2);
2035 /* try and use up these file descriptors, so silly
2036 library routines writing to stdout etc won't cause havoc */
2038 fd = open("/dev/null",O_RDWR,0);
2039 if (fd < 0) fd = open("/dev/null",O_WRONLY,0);
2041 DEBUG(0,("Can't open /dev/null\n"));
2045 DEBUG(0,("Didn't get file descriptor %d\n",i));
2051 /****************************************************************************
2052 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
2054 if SYSV use O_NDELAY
2056 ****************************************************************************/
2057 int set_blocking(int fd, BOOL set)
2061 #define FLAG_TO_SET O_NONBLOCK
2064 #define FLAG_TO_SET O_NDELAY
2066 #define FLAG_TO_SET FNDELAY
2070 if((val = fcntl(fd, F_GETFL, 0)) == -1)
2072 if(set) /* Turn blocking on - ie. clear nonblock flag */
2073 val &= ~FLAG_TO_SET;
2076 return fcntl( fd, F_SETFL, val);
2081 /****************************************************************************
2083 ****************************************************************************/
2084 int write_socket(int fd,char *buf,int len)
2090 DEBUG(6,("write_socket(%d,%d)\n",fd,len));
2091 ret = write_data(fd,buf,len);
2093 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,len,ret));
2095 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
2096 len, fd, strerror(errno) ));
2101 /****************************************************************************
2103 ****************************************************************************/
2104 int read_udp_socket(int fd,char *buf,int len)
2107 struct sockaddr_in sock;
2110 socklen = sizeof(sock);
2111 bzero((char *)&sock,socklen);
2112 bzero((char *)&lastip,sizeof(lastip));
2113 ret = recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
2115 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
2119 lastip = sock.sin_addr;
2120 lastport = ntohs(sock.sin_port);
2122 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
2123 inet_ntoa(lastip), lastport, ret));
2128 /****************************************************************************
2129 read data from a device with a timout in msec.
2130 mincount = if timeout, minimum to read before returning
2131 maxcount = number to be read.
2132 ****************************************************************************/
2133 int read_with_timeout(int fd,char *buf,int mincnt,int maxcnt,long time_out)
2139 struct timeval timeout;
2141 /* just checking .... */
2142 if (maxcnt <= 0) return(0);
2147 if (time_out <= 0) {
2148 if (mincnt == 0) mincnt = maxcnt;
2150 while (nread < mincnt) {
2153 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
2155 readret = read(fd, buf + nread, maxcnt - nread);
2158 readret = read(fd, buf + nread, maxcnt - nread);
2159 #endif /* USE_SSL */
2162 smb_read_error = READ_EOF;
2166 if (readret == -1) {
2167 smb_read_error = READ_ERROR;
2175 /* Most difficult - timeout read */
2176 /* If this is ever called on a disk file and
2177 mincnt is greater then the filesize then
2178 system performance will suffer severely as
2179 select always return true on disk files */
2181 /* Set initial timeout */
2182 timeout.tv_sec = time_out / 1000;
2183 timeout.tv_usec = 1000 * (time_out % 1000);
2185 for (nread=0; nread<mincnt; )
2190 selrtn = sys_select(&fds,&timeout);
2192 /* Check if error */
2194 /* something is wrong. Maybe the socket is dead? */
2195 smb_read_error = READ_ERROR;
2199 /* Did we timeout ? */
2201 smb_read_error = READ_TIMEOUT;
2207 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
2209 readret = read(fd, buf + nread, maxcnt - nread);
2212 readret = read(fd, buf+nread, maxcnt-nread);
2213 #endif /* USE_SSL */
2216 /* we got EOF on the file descriptor */
2217 smb_read_error = READ_EOF;
2221 if (readret == -1) {
2222 /* the descriptor is probably dead */
2223 smb_read_error = READ_ERROR;
2230 /* Return the number we got */
2234 /****************************************************************************
2235 read data from the client. Maxtime is in milliseconds
2236 ****************************************************************************/
2237 int read_max_udp(int fd,char *buffer,int bufsize,int maxtime)
2242 struct timeval timeout;
2247 timeout.tv_sec = maxtime / 1000;
2248 timeout.tv_usec = (maxtime % 1000) * 1000;
2250 selrtn = sys_select(&fds,maxtime>0?&timeout:NULL);
2252 if (!FD_ISSET(fd,&fds))
2255 nread = read_udp_socket(fd, buffer, bufsize);
2257 /* return the number got */
2261 /*******************************************************************
2262 find the difference in milliseconds between two struct timeval
2264 ********************************************************************/
2265 int TvalDiff(struct timeval *tvalold,struct timeval *tvalnew)
2267 return((tvalnew->tv_sec - tvalold->tv_sec)*1000 +
2268 ((int)tvalnew->tv_usec - (int)tvalold->tv_usec)/1000);
2271 /****************************************************************************
2272 send a keepalive packet (rfc1002)
2273 ****************************************************************************/
2274 BOOL send_keepalive(int client)
2276 unsigned char buf[4];
2279 buf[1] = buf[2] = buf[3] = 0;
2281 return(write_data(client,(char *)buf,4) == 4);
2286 /****************************************************************************
2287 read data from the client, reading exactly N bytes.
2288 ****************************************************************************/
2289 int read_data(int fd,char *buffer,int N)
2300 ret = SSL_read(ssl, buffer + total, N - total);
2302 ret = read(fd,buffer + total,N - total);
2305 ret = read(fd,buffer + total,N - total);
2306 #endif /* USE_SSL */
2310 smb_read_error = READ_EOF;
2315 smb_read_error = READ_ERROR;
2324 /****************************************************************************
2326 ****************************************************************************/
2327 int write_data(int fd,char *buffer,int N)
2336 ret = SSL_write(ssl,buffer + total,N - total);
2338 ret = write(fd,buffer + total,N - total);
2341 ret = write(fd,buffer + total,N - total);
2342 #endif /* USE_SSL */
2344 if (ret == -1) return -1;
2345 if (ret == 0) return total;
2353 /****************************************************************************
2354 transfer some data between two fd's
2355 ****************************************************************************/
2356 int transfer_file(int infd,int outfd,int n,char *header,int headlen,int align)
2358 static char *buf=NULL;
2363 DEBUG(4,("transfer_file %d (head=%d) called\n",n,headlen));
2366 size = lp_readsize();
2367 size = MAX(size,1024);
2370 while (!buf && size>0) {
2371 buf = (char *)Realloc(buf,size+8);
2372 if (!buf) size /= 2;
2376 DEBUG(0,("Can't allocate transfer buffer!\n"));
2380 abuf = buf + (align%8);
2387 int s = MIN(n,size);
2392 if (header && (headlen >= MIN(s,1024))) {
2402 if (header && headlen > 0)
2404 ret = MIN(headlen,size);
2405 memcpy(buf1,header,ret);
2408 if (headlen <= 0) header = NULL;
2412 ret += read(infd,buf1+ret,s-ret);
2416 ret2 = (outfd>=0?write_data(outfd,buf1,ret):ret);
2417 if (ret2 > 0) total += ret2;
2418 /* if we can't write then dump excess data */
2420 transfer_file(infd,-1,n-(ret+headlen),NULL,0,0);
2422 if (ret <= 0 || ret2 != ret)
2430 /****************************************************************************
2431 read 4 bytes of a smb packet and return the smb length of the packet
2432 store the result in the buffer
2433 This version of the function will return a length of zero on receiving
2435 ****************************************************************************/
2436 static int read_smb_length_return_keepalive(int fd,char *inbuf,int timeout)
2438 int len=0, msg_type;
2444 ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
2446 ok = (read_data(fd,inbuf,4) == 4);
2451 len = smb_len(inbuf);
2452 msg_type = CVAL(inbuf,0);
2454 if (msg_type == 0x85)
2455 DEBUG(5,("Got keepalive packet\n"));
2458 DEBUG(10,("got smb length of %d\n",len));
2463 /****************************************************************************
2464 read 4 bytes of a smb packet and return the smb length of the packet
2465 store the result in the buffer. This version of the function will
2466 never return a session keepalive (length of zero).
2467 ****************************************************************************/
2468 int read_smb_length(int fd,char *inbuf,int timeout)
2474 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
2479 /* Ignore session keepalives. */
2480 if(CVAL(inbuf,0) != 0x85)
2487 /****************************************************************************
2488 read an smb from a fd. Note that the buffer *MUST* be of size
2489 BUFFER_SIZE+SAFETY_MARGIN.
2490 The timeout is in milli seconds.
2492 This function will return on a
2493 receipt of a session keepalive packet.
2494 ****************************************************************************/
2495 BOOL receive_smb(int fd,char *buffer, int timeout)
2501 bzero(buffer,smb_size + 100);
2503 len = read_smb_length_return_keepalive(fd,buffer,timeout);
2507 if (len > BUFFER_SIZE) {
2508 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
2509 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
2514 ret = read_data(fd,buffer+4,len);
2516 smb_read_error = READ_ERROR;
2523 /****************************************************************************
2524 read an smb from a fd ignoring all keepalive packets. Note that the buffer
2525 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
2526 The timeout is in milli seconds
2528 This is exactly the same as receive_smb except that it never returns
2529 a session keepalive packet (just as receive_smb used to do).
2530 receive_smb was changed to return keepalives as the oplock processing means this call
2531 should never go into a blocking read.
2532 ****************************************************************************/
2534 BOOL client_receive_smb(int fd,char *buffer, int timeout)
2540 ret = receive_smb(fd, buffer, timeout);
2545 /* Ignore session keepalive packets. */
2546 if(CVAL(buffer,0) != 0x85)
2552 /****************************************************************************
2553 read a message from a udp fd.
2554 The timeout is in milli seconds
2555 ****************************************************************************/
2556 BOOL receive_local_message(int fd, char *buffer, int buffer_len, int timeout)
2558 struct sockaddr_in from;
2559 int fromlen = sizeof(from);
2573 to.tv_sec = timeout / 1000;
2574 to.tv_usec = (timeout % 1000) * 1000;
2576 selrtn = sys_select(&fds,&to);
2578 /* Check if error */
2581 /* something is wrong. Maybe the socket is dead? */
2582 smb_read_error = READ_ERROR;
2586 /* Did we timeout ? */
2589 smb_read_error = READ_TIMEOUT;
2595 * Read a loopback udp message.
2597 msg_len = recvfrom(fd, &buffer[UDP_CMD_HEADER_LEN],
2598 buffer_len - UDP_CMD_HEADER_LEN, 0,
2599 (struct sockaddr *)&from, &fromlen);
2603 DEBUG(0,("receive_local_message. Error in recvfrom. (%s).\n",strerror(errno)));
2607 /* Validate message length. */
2608 if(msg_len > (buffer_len - UDP_CMD_HEADER_LEN))
2610 DEBUG(0,("receive_local_message: invalid msg_len (%d) max can be %d\n",
2612 buffer_len - UDP_CMD_HEADER_LEN));
2616 /* Validate message from address (must be localhost). */
2617 if(from.sin_addr.s_addr != htonl(INADDR_LOOPBACK))
2619 DEBUG(0,("receive_local_message: invalid 'from' address \
2620 (was %x should be 127.0.0.1\n", from.sin_addr.s_addr));
2624 /* Setup the message header */
2625 SIVAL(buffer,UDP_CMD_LEN_OFFSET,msg_len);
2626 SSVAL(buffer,UDP_CMD_PORT_OFFSET,ntohs(from.sin_port));
2631 /****************************************************************************
2632 structure to hold a linked list of local messages.
2634 ****************************************************************************/
2636 typedef struct _message_list {
2637 struct _message_list *msg_next;
2640 } pending_message_list;
2642 static pending_message_list *smb_msg_head = NULL;
2644 /****************************************************************************
2645 Function to push a linked list of local messages ready
2647 ****************************************************************************/
2649 static BOOL push_local_message(pending_message_list **pml, char *buf, int msg_len)
2651 pending_message_list *msg = (pending_message_list *)
2652 malloc(sizeof(pending_message_list));
2656 DEBUG(0,("push_message: malloc fail (1)\n"));
2660 msg->msg_buf = (char *)malloc(msg_len);
2661 if(msg->msg_buf == NULL)
2663 DEBUG(0,("push_local_message: malloc fail (2)\n"));
2668 memcpy(msg->msg_buf, buf, msg_len);
2669 msg->msg_len = msg_len;
2671 msg->msg_next = *pml;
2677 /****************************************************************************
2678 Function to push a linked list of local smb messages ready
2680 ****************************************************************************/
2682 BOOL push_smb_message(char *buf, int msg_len)
2684 return push_local_message(&smb_msg_head, buf, msg_len);
2687 /****************************************************************************
2688 Do a select on an two fd's - with timeout.
2690 If a local udp message has been pushed onto the
2691 queue (this can only happen during oplock break
2692 processing) return this first.
2694 If a pending smb message has been pushed onto the
2695 queue (this can only happen during oplock break
2696 processing) return this next.
2698 If the first smbfd is ready then read an smb from it.
2699 if the second (loopback UDP) fd is ready then read a message
2700 from it and setup the buffer header to identify the length
2702 Returns False on timeout or error.
2705 The timeout is in milli seconds
2706 ****************************************************************************/
2707 BOOL receive_message_or_smb(int smbfd, int oplock_fd,
2708 char *buffer, int buffer_len,
2709 int timeout, BOOL *got_smb)
2720 * Check to see if we already have a message on the smb queue.
2721 * If so - copy and return it.
2726 pending_message_list *msg = smb_msg_head;
2727 memcpy(buffer, msg->msg_buf, MIN(buffer_len, msg->msg_len));
2728 smb_msg_head = msg->msg_next;
2730 /* Free the message we just copied. */
2731 free((char *)msg->msg_buf);
2735 DEBUG(5,("receive_message_or_smb: returning queued smb message.\n"));
2741 FD_SET(oplock_fd,&fds);
2743 to.tv_sec = timeout / 1000;
2744 to.tv_usec = (timeout % 1000) * 1000;
2746 selrtn = sys_select(&fds,timeout>0?&to:NULL);
2748 /* Check if error */
2750 /* something is wrong. Maybe the socket is dead? */
2751 smb_read_error = READ_ERROR;
2755 /* Did we timeout ? */
2757 smb_read_error = READ_TIMEOUT;
2761 if (FD_ISSET(smbfd,&fds))
2764 return receive_smb(smbfd, buffer, 0);
2768 return receive_local_message(oplock_fd, buffer, buffer_len, 0);
2772 /****************************************************************************
2774 ****************************************************************************/
2775 BOOL send_smb(int fd,char *buffer)
2779 len = smb_len(buffer) + 4;
2781 while (nwritten < len)
2783 ret = write_socket(fd,buffer+nwritten,len - nwritten);
2786 DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
2798 /****************************************************************************
2799 find a pointer to a netbios name
2800 ****************************************************************************/
2801 char *name_ptr(char *buf,int ofs)
2803 unsigned char c = *(unsigned char *)(buf+ofs);
2805 if ((c & 0xC0) == 0xC0)
2809 memcpy(p,buf+ofs,2);
2812 DEBUG(5,("name ptr to pos %d from %d is %s\n",l,ofs,buf+l));
2819 /****************************************************************************
2820 extract a netbios name from a buf
2821 ****************************************************************************/
2822 int name_extract(char *buf,int ofs,char *name)
2824 char *p = name_ptr(buf,ofs);
2825 int d = PTR_DIFF(p,buf+ofs);
2827 if (d < -50 || d > 50) return(0);
2828 return(name_interpret(p,name));
2831 /****************************************************************************
2832 return the total storage length of a mangled name
2833 ****************************************************************************/
2834 int name_len( char *s )
2838 /* If the two high bits of the byte are set, return 2. */
2839 if( 0xC0 == (*(unsigned char *)s & 0xC0) )
2842 /* Add up the length bytes. */
2843 for( len = 1; (*s); s += (*s) + 1 )
2851 /****************************************************************************
2852 send a single packet to a port on another machine
2853 ****************************************************************************/
2854 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
2858 struct sockaddr_in sock_out;
2863 /* create a socket to write to */
2864 out_fd = socket(AF_INET, type, 0);
2867 DEBUG(0,("socket failed"));
2871 /* set the address and port */
2872 bzero((char *)&sock_out,sizeof(sock_out));
2873 putip((char *)&sock_out.sin_addr,(char *)&ip);
2874 sock_out.sin_port = htons( port );
2875 sock_out.sin_family = AF_INET;
2878 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
2879 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
2882 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
2885 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
2886 inet_ntoa(ip),port,strerror(errno)));
2892 /*******************************************************************
2893 sleep for a specified number of milliseconds
2894 ********************************************************************/
2898 struct timeval tval,t1,t2;
2905 tval.tv_sec = (t-tdiff)/1000;
2906 tval.tv_usec = 1000*((t-tdiff)%1000);
2910 sys_select(&fds,&tval);
2913 tdiff = TvalDiff(&t1,&t2);
2917 /****************************************************************************
2918 check if a string is part of a list
2919 ****************************************************************************/
2920 BOOL in_list(char *s,char *list,BOOL casesensitive)
2925 if (!list) return(False);
2927 while (next_token(&p,tok,LIST_SEP))
2929 if (casesensitive) {
2930 if (strcmp(tok,s) == 0)
2933 if (StrCaseCmp(tok,s) == 0)
2940 /* this is used to prevent lots of mallocs of size 1 */
2941 static char *null_string = NULL;
2943 /****************************************************************************
2944 set a string value, allocing the space for the string
2945 ****************************************************************************/
2946 BOOL string_init(char **dest,char *src)
2957 null_string = (char *)malloc(1);
2960 *dest = null_string;
2964 (*dest) = (char *)malloc(l+1);
2965 if ((*dest) == NULL) {
2966 DEBUG(0,("Out of memory in string_init\n"));
2975 /****************************************************************************
2977 ****************************************************************************/
2978 void string_free(char **s)
2980 if (!s || !(*s)) return;
2981 if (*s == null_string)
2987 /****************************************************************************
2988 set a string value, allocing the space for the string, and deallocating any
2990 ****************************************************************************/
2991 BOOL string_set(char **dest,char *src)
2995 return(string_init(dest,src));
2998 /****************************************************************************
2999 substitute a string for a pattern in another string. Make sure there is
3002 This routine looks for pattern in s and replaces it with
3003 insert. It may do multiple replacements.
3005 return True if a substitution was done.
3006 ****************************************************************************/
3007 BOOL string_sub(char *s,char *pattern,char *insert)
3013 if (!insert || !pattern || !s) return(False);
3016 lp = strlen(pattern);
3017 li = strlen(insert);
3019 if (!*pattern) return(False);
3021 while (lp <= ls && (p = strstr(s,pattern)))
3024 memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
3025 memcpy(p,insert,li);
3032 /*********************************************************
3033 * Recursive routine that is called by mask_match.
3034 * Does the actual matching. Returns True if matched,
3036 *********************************************************/
3038 BOOL do_match(char *str, char *regexp, int case_sig)
3042 for( p = regexp; *p && *str; ) {
3049 /* Look for a character matching
3050 the one after the '*' */
3053 return True; /* Automatic match */
3055 while(*str && (case_sig ? (*p != *str) : (toupper(*p)!=toupper(*str))))
3057 /* Now eat all characters that match, as
3058 we want the *last* character to match. */
3059 while(*str && (case_sig ? (*p == *str) : (toupper(*p)==toupper(*str))))
3061 str--; /* We've eaten the match char after the '*' */
3062 if(do_match(str,p,case_sig)) {
3079 if(toupper(*str) != toupper(*p)) {
3091 if (!*p && str[0] == '.' && str[1] == 0) {
3095 if (!*str && *p == '?') {
3101 if(!*str && (*p == '*' && p[1] == '\0')) {
3109 /*********************************************************
3110 * Routine to match a given string with a regexp - uses
3111 * simplified regexp that takes * and ? only. Case can be
3112 * significant or not.
3113 * The 8.3 handling was rewritten by Ums Harald <Harald.Ums@pro-sieben.de>
3114 *********************************************************/
3116 BOOL mask_match(char *str, char *regexp, int case_sig,BOOL trans2)
3119 pstring t_pattern, t_filename, te_pattern, te_filename;
3120 fstring ebase,eext,sbase,sext;
3122 BOOL matched = False;
3124 /* Make local copies of str and regexp */
3125 pstrcpy(t_pattern,regexp);
3126 pstrcpy(t_filename,str);
3130 * Not sure if this is a good idea. JRA.
3132 if(trans2 && is_8_3(t_pattern,False) && is_8_3(t_filename,False))
3137 if (!strchr(t_filename,'.')) {
3138 pstrcat(t_filename,".");
3142 /* Remove any *? and ** as they are meaningless */
3143 string_sub(t_pattern, "*?", "*");
3144 string_sub(t_pattern, "**", "*");
3146 if (strequal(t_pattern,"*"))
3149 DEBUG(8,("mask_match str=<%s> regexp=<%s>, case_sig = %d\n", t_filename, t_pattern, case_sig));
3153 * Match each component of the regexp, split up by '.'
3156 char *fp, *rp, *cp2, *cp1;
3157 BOOL last_wcard_was_star = False;
3158 int num_path_components, num_regexp_components;
3160 pstrcpy(te_pattern,t_pattern);
3161 pstrcpy(te_filename,t_filename);
3163 * Remove multiple "*." patterns.
3165 string_sub(te_pattern, "*.*.", "*.");
3166 num_regexp_components = count_chars(te_pattern, '.');
3167 num_path_components = count_chars(te_filename, '.');
3170 * Check for special 'hack' case of "DIR a*z". - needs to match a.b.c...z
3172 if(num_regexp_components == 0)
3173 matched = do_match( te_filename, te_pattern, case_sig);
3175 for( cp1 = te_pattern, cp2 = te_filename; cp1;) {
3176 fp = strchr(cp2, '.');
3179 rp = strchr(cp1, '.');
3183 if(cp1[strlen(cp1)-1] == '*')
3184 last_wcard_was_star = True;
3186 last_wcard_was_star = False;
3188 if(!do_match(cp2, cp1, case_sig))
3191 cp1 = rp ? rp + 1 : NULL;
3192 cp2 = fp ? fp + 1 : "";
3194 if(last_wcard_was_star || ((cp1 != NULL) && (*cp1 == '*'))) {
3195 /* Eat the extra path components. */
3198 for(i = 0; i < num_path_components - num_regexp_components; i++) {
3199 fp = strchr(cp2, '.');
3203 if((cp1 != NULL) && do_match( cp2, cp1, case_sig)) {
3204 cp2 = fp ? fp + 1 : "";
3207 cp2 = fp ? fp + 1 : "";
3209 num_path_components -= i;
3212 if(cp1 == NULL && ((*cp2 == '\0') || last_wcard_was_star))
3217 /* -------------------------------------------------
3218 * Behaviour of Win95
3219 * for 8.3 filenames and 8.3 Wildcards
3220 * -------------------------------------------------
3222 if (strequal (t_filename, ".")) {
3224 * Patterns: *.* *. ?. ? are valid
3227 if(strequal(t_pattern, "*.*") || strequal(t_pattern, "*.") ||
3228 strequal(t_pattern, "?.") || strequal(t_pattern, "?"))
3230 } else if (strequal (t_filename, "..")) {
3232 * Patterns: *.* *. ?. ? *.? are valid
3235 if(strequal(t_pattern, "*.*") || strequal(t_pattern, "*.") ||
3236 strequal(t_pattern, "?.") || strequal(t_pattern, "?") ||
3237 strequal(t_pattern, "*.?") || strequal(t_pattern, "?.*"))
3241 if ((p = strrchr (t_pattern, '.'))) {
3243 * Wildcard has a suffix.
3246 fstrcpy (ebase, t_pattern);
3248 fstrcpy (eext, p + 1);
3250 /* pattern ends in DOT: treat as if there is no DOT */
3252 if (strequal (ebase, "*"))
3257 * No suffix for wildcard.
3259 fstrcpy (ebase, t_pattern);
3263 p = strrchr (t_filename, '.');
3264 if (p && (p[1] == 0) ) {
3266 * Filename has an extension of '.' only.
3268 *p = 0; /* nuke dot at end of string */
3269 p = 0; /* and treat it as if there is no extension */
3274 * Filename has an extension.
3277 fstrcpy (sbase, t_filename);
3278 fstrcpy (sext, p + 1);
3280 matched = do_match(sbase, ebase, case_sig)
3281 && do_match(sext, eext, case_sig);
3283 /* pattern has no extension */
3284 /* Really: match complete filename with pattern ??? means exactly 3 chars */
3285 matched = do_match(str, ebase, case_sig);
3289 * Filename has no extension.
3291 fstrcpy (sbase, t_filename);
3294 /* pattern has extension */
3295 matched = do_match(sbase, ebase, case_sig)
3296 && do_match(sext, eext, case_sig);
3298 matched = do_match(sbase, ebase, case_sig);
3299 #ifdef EMULATE_WEIRD_W95_MATCHING
3301 * Even Microsoft has some problems
3302 * Behaviour Win95 -> local disk
3303 * is different from Win95 -> smb drive from Nt 4.0
3304 * This branch would reflect the Win95 local disk behaviour
3307 /* a? matches aa and a in w95 */
3308 fstrcat (sbase, ".");
3309 matched = do_match(sbase, ebase, case_sig);
3317 DEBUG(8,("mask_match returning %d\n", matched));
3322 /****************************************************************************
3323 become a daemon, discarding the controlling terminal
3324 ****************************************************************************/
3325 void become_daemon(void)
3327 #ifndef NO_FORK_DEBUG
3331 /* detach from the terminal */
3334 #else /* USE_SETSID */
3337 int i = open("/dev/tty", O_RDWR);
3340 ioctl(i, (int) TIOCNOTTY, (char *)0);
3344 #endif /* TIOCNOTTY */
3345 #endif /* USE_SETSID */
3346 /* Close fd's 0,1,2. Needed if started by rsh */
3348 #endif /* NO_FORK_DEBUG */
3352 /****************************************************************************
3353 put up a yes/no prompt
3354 ****************************************************************************/
3360 if (!fgets(ans,sizeof(ans)-1,stdin))
3363 if (*ans == 'y' || *ans == 'Y')
3369 /****************************************************************************
3370 read a line from a file with possible \ continuation chars.
3371 Blanks at the start or end of a line are stripped.
3372 The string will be allocated if s2 is NULL
3373 ****************************************************************************/
3374 char *fgets_slash(char *s2,int maxlen,FILE *f)
3379 BOOL start_of_line = True;
3386 maxlen = MIN(maxlen,8);
3387 s = (char *)Realloc(s,maxlen);
3390 if (!s || maxlen < 2) return(NULL);
3394 while (len < maxlen-1)
3402 while (len > 0 && s[len-1] == ' ')
3406 if (len > 0 && s[len-1] == '\\')
3409 start_of_line = True;
3414 if (len <= 0 && !s2)
3416 return(len>0?s:NULL);
3421 start_of_line = False;
3425 if (!s2 && len > maxlen-3)
3428 s = (char *)Realloc(s,maxlen);
3429 if (!s) return(NULL);
3437 /****************************************************************************
3438 set the length of a file from a filedescriptor.
3439 Returns 0 on success, -1 on failure.
3440 ****************************************************************************/
3441 int set_filelen(int fd, long len)
3443 /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
3444 extend a file with ftruncate. Provide alternate implementation
3447 #if FTRUNCATE_CAN_EXTEND
3448 return ftruncate(fd, len);
3452 long currpos = lseek(fd, 0L, SEEK_CUR);
3456 /* Do an fstat to see if the file is longer than
3457 the requested size (call ftruncate),
3458 or shorter, in which case seek to len - 1 and write 1
3460 if(fstat(fd, &st)<0)
3464 if (S_ISFIFO(st.st_mode)) return 0;
3467 if(st.st_size == len)
3469 if(st.st_size > len)
3470 return ftruncate(fd, len);
3472 if(lseek(fd, len-1, SEEK_SET) != len -1)
3474 if(write(fd, &c, 1)!=1)
3476 /* Seek to where we were */
3477 lseek(fd, currpos, SEEK_SET);
3483 /****************************************************************************
3484 return the byte checksum of some data
3485 ****************************************************************************/
3486 int byte_checksum(char *buf,int len)
3488 unsigned char *p = (unsigned char *)buf;
3498 /****************************************************************************
3499 this is a version of setbuffer() for those machines that only have setvbuf
3500 ****************************************************************************/
3501 void setbuffer(FILE *f,char *buf,int bufsize)
3503 setvbuf(f,buf,_IOFBF,bufsize);
3508 /****************************************************************************
3509 parse out a directory name from a path name. Assumes dos style filenames.
3510 ****************************************************************************/
3511 char *dirname_dos(char *path,char *buf)
3513 char *p = strrchr(path,'\\');
3528 /****************************************************************************
3529 parse out a filename from a path name. Assumes dos style filenames.
3530 ****************************************************************************/
3531 static char *filename_dos(char *path,char *buf)
3533 char *p = strrchr(path,'\\');
3545 /****************************************************************************
3546 expand a pointer to be a particular size
3547 ****************************************************************************/
3548 void *Realloc(void *p,int size)
3554 DEBUG(5,("Realloc asked for 0 bytes\n"));
3559 ret = (void *)malloc(size);
3561 ret = (void *)realloc(p,size);
3564 DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",size));
3570 /****************************************************************************
3572 ****************************************************************************/
3573 char *strdup(char *s)
3577 if (!s) return(NULL);
3578 ret = (char *)malloc((len = strlen(s))+1);
3579 if (!ret) return(NULL);
3580 safe_strcpy(ret,s,len);
3586 /****************************************************************************
3587 Signal handler for SIGPIPE (write on a disconnected socket)
3588 ****************************************************************************/
3591 DEBUG(0,("Probably got SIGPIPE\nExiting\n"));
3595 /****************************************************************************
3596 get my own name and IP
3597 ****************************************************************************/
3598 BOOL get_myname(char *my_name,struct in_addr *ip)
3605 /* get my host name */
3606 if (gethostname(hostname, MAXHOSTNAMELEN) == -1)
3608 DEBUG(0,("gethostname failed\n"));
3613 if ((hp = Get_Hostbyname(hostname)) == 0)
3615 DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",hostname));
3621 /* split off any parts after an initial . */
3622 char *p = strchr(hostname,'.');
3625 fstrcpy(my_name,hostname);
3629 putip((char *)ip,(char *)hp->h_addr);
3635 /****************************************************************************
3636 true if two IP addresses are equal
3637 ****************************************************************************/
3638 BOOL ip_equal(struct in_addr ip1,struct in_addr ip2)
3641 a1 = ntohl(ip1.s_addr);
3642 a2 = ntohl(ip2.s_addr);
3647 /****************************************************************************
3648 open a socket of the specified type, port and address for incoming data
3649 ****************************************************************************/
3650 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
3653 struct sockaddr_in sock;
3657 /* get my host name */
3658 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
3659 { DEBUG(0,("gethostname failed\n")); return -1; }
3662 if ((hp = Get_Hostbyname(host_name)) == 0)
3664 DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
3668 bzero((char *)&sock,sizeof(sock));
3669 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
3670 #if defined(__FreeBSD__) || defined(NETBSD) || defined(__OpenBSD__) /* XXX not the right ifdef */
3671 sock.sin_len = sizeof(sock);
3673 sock.sin_port = htons( port );
3674 sock.sin_family = hp->h_addrtype;
3675 sock.sin_addr.s_addr = socket_addr;
3676 res = socket(hp->h_addrtype, type, 0);
3678 { DEBUG(0,("socket failed\n")); return -1; }
3682 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
3685 /* now we've got a socket - we need to bind it */
3686 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
3689 if (port == SMB_PORT || port == NMB_PORT)
3690 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
3691 port,inet_ntoa(sock.sin_addr),strerror(errno)));
3694 if (dlevel > 0 && port < 1000)
3697 if (port >= 1000 && port < 9000)
3698 return(open_socket_in(type,port+1,dlevel,socket_addr));
3703 DEBUG(3,("bind succeeded on port %d\n",port));
3709 /****************************************************************************
3710 create an outgoing socket
3711 **************************************************************************/
3712 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
3714 struct sockaddr_in sock_out;
3716 int connect_loop = 250; /* 250 milliseconds */
3717 int loops = (timeout * 1000) / connect_loop;
3719 /* create a socket to write to */
3720 res = socket(PF_INET, type, 0);
3722 { DEBUG(0,("socket error\n")); return -1; }
3724 if (type != SOCK_STREAM) return(res);
3726 bzero((char *)&sock_out,sizeof(sock_out));
3727 putip((char *)&sock_out.sin_addr,(char *)addr);
3729 sock_out.sin_port = htons( port );
3730 sock_out.sin_family = PF_INET;
3732 /* set it non-blocking */
3733 set_blocking(res,False);
3735 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
3737 /* and connect it to the destination */
3739 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
3741 /* Some systems return EAGAIN when they mean EINPROGRESS */
3742 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
3743 errno == EAGAIN) && loops--) {
3744 msleep(connect_loop);
3748 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
3750 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
3756 if (ret < 0 && errno == EISCONN) {
3763 DEBUG(1,("error connecting to %s:%d (%s)\n",
3764 inet_ntoa(*addr),port,strerror(errno)));
3769 /* set it blocking again */
3770 set_blocking(res,True);
3776 /****************************************************************************
3777 interpret a protocol description string, with a default
3778 ****************************************************************************/
3779 int interpret_protocol(char *str,int def)
3781 if (strequal(str,"NT1"))
3782 return(PROTOCOL_NT1);
3783 if (strequal(str,"LANMAN2"))
3784 return(PROTOCOL_LANMAN2);
3785 if (strequal(str,"LANMAN1"))
3786 return(PROTOCOL_LANMAN1);
3787 if (strequal(str,"CORE"))
3788 return(PROTOCOL_CORE);
3789 if (strequal(str,"COREPLUS"))
3790 return(PROTOCOL_COREPLUS);
3791 if (strequal(str,"CORE+"))
3792 return(PROTOCOL_COREPLUS);
3794 DEBUG(0,("Unrecognised protocol level %s\n",str));
3799 /****************************************************************************
3800 interpret a security level
3801 ****************************************************************************/
3802 int interpret_security(char *str,int def)
3804 if (strequal(str,"SERVER"))
3806 if (strequal(str,"USER"))
3808 if (strequal(str,"SHARE"))
3811 DEBUG(0,("Unrecognised security level %s\n",str));
3817 /****************************************************************************
3818 interpret an internet address or name into an IP address in 4 byte form
3819 ****************************************************************************/
3820 uint32 interpret_addr(char *str)
3825 BOOL pure_address = True;
3827 if (strcmp(str,"0.0.0.0") == 0) return(0);
3828 if (strcmp(str,"255.255.255.255") == 0) return(0xFFFFFFFF);
3830 for (i=0; pure_address && str[i]; i++)
3831 if (!(isdigit(str[i]) || str[i] == '.'))
3832 pure_address = False;
3834 /* if it's in the form of an IP address then get the lib to interpret it */
3836 res = inet_addr(str);
3838 /* otherwise assume it's a network name of some sort and use
3840 if ((hp = Get_Hostbyname(str)) == 0) {
3841 DEBUG(3,("Get_Hostbyname: Unknown host. %s\n",str));
3844 if(hp->h_addr == NULL) {
3845 DEBUG(3,("Get_Hostbyname: host address is invalid for host %s\n",str));
3848 putip((char *)&res,(char *)hp->h_addr);
3851 if (res == (uint32)-1) return(0);
3856 /*******************************************************************
3857 a convenient addition to interpret_addr()
3858 ******************************************************************/
3859 struct in_addr *interpret_addr2(char *str)
3861 static struct in_addr ret;
3862 uint32 a = interpret_addr(str);
3867 /*******************************************************************
3868 check if an IP is the 0.0.0.0
3869 ******************************************************************/
3870 BOOL zero_ip(struct in_addr ip)
3873 putip((char *)&a,(char *)&ip);
3878 /*******************************************************************
3879 matchname - determine if host name matches IP address
3880 ******************************************************************/
3881 static BOOL matchname(char *remotehost,struct in_addr addr)
3886 if ((hp = Get_Hostbyname(remotehost)) == 0) {
3887 DEBUG(0,("Get_Hostbyname(%s): lookup failure", remotehost));
3892 * Make sure that gethostbyname() returns the "correct" host name.
3893 * Unfortunately, gethostbyname("localhost") sometimes yields
3894 * "localhost.domain". Since the latter host name comes from the
3895 * local DNS, we just have to trust it (all bets are off if the local
3896 * DNS is perverted). We always check the address list, though.
3899 if (strcasecmp(remotehost, hp->h_name)
3900 && strcasecmp(remotehost, "localhost")) {
3901 DEBUG(0,("host name/name mismatch: %s != %s",
3902 remotehost, hp->h_name));
3906 /* Look up the host address in the address list we just got. */
3907 for (i = 0; hp->h_addr_list[i]; i++) {
3908 if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
3913 * The host name does not map to the original host address. Perhaps
3914 * someone has compromised a name server. More likely someone botched
3915 * it, but that could be dangerous, too.
3918 DEBUG(0,("host name/address mismatch: %s != %s",
3919 inet_ntoa(addr), hp->h_name));
3923 /*******************************************************************
3924 Reset the 'done' variables so after a client process is created
3925 from a fork call these calls will be re-done. This should be
3926 expanded if more variables need reseting.
3927 ******************************************************************/
3929 static BOOL global_client_name_done = False;
3930 static BOOL global_client_addr_done = False;
3932 void reset_globals_after_fork(void)
3934 global_client_name_done = False;
3935 global_client_addr_done = False;
3938 * Re-seed the random crypto generator, so all smbd's
3939 * started from the same parent won't generate the same
3943 unsigned char dummy;
3944 generate_random_buffer( &dummy, 1, True);
3948 /*******************************************************************
3949 return the DNS name of the client
3950 ******************************************************************/
3951 char *client_name(int fd)
3954 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
3955 int length = sizeof(sa);
3956 static pstring name_buf;
3958 static int last_fd=-1;
3960 if (global_client_name_done && last_fd == fd)
3964 global_client_name_done = False;
3966 pstrcpy(name_buf,"UNKNOWN");
3972 if (getpeername(fd, &sa, &length) < 0) {
3973 DEBUG(0,("getpeername failed\n"));
3977 /* Look up the remote host name. */
3978 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
3979 sizeof(sockin->sin_addr),
3981 DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr(fd)));
3982 StrnCpy(name_buf,client_addr(fd),sizeof(name_buf) - 1);
3984 StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
3985 if (!matchname(name_buf, sockin->sin_addr)) {
3986 DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr(fd)));
3987 pstrcpy(name_buf,"UNKNOWN");
3990 global_client_name_done = True;
3994 /*******************************************************************
3995 return the IP addr of the client as a string
3996 ******************************************************************/
3997 char *client_addr(int fd)
4000 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
4001 int length = sizeof(sa);
4002 static fstring addr_buf;
4003 static int last_fd = -1;
4005 if (global_client_addr_done && fd == last_fd)
4009 global_client_addr_done = False;
4011 fstrcpy(addr_buf,"0.0.0.0");
4017 if (getpeername(fd, &sa, &length) < 0) {
4018 DEBUG(0,("getpeername failed\n"));
4022 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
4024 global_client_addr_done = True;
4028 /*******************************************************************
4029 Patch from jkf@soton.ac.uk
4030 Split Luke's automount_server into YP lookup and string splitter
4031 so can easily implement automount_path().
4032 As we may end up doing both, cache the last YP result.
4033 *******************************************************************/
4035 #if (defined(NETGROUP) && defined(AUTOMOUNT))
4037 static char *automount_lookup(char *user_name)
4039 static fstring last_key = "";
4040 static pstring last_value = "";
4042 char *nis_map = (char *)lp_nis_home_map_name();
4044 char nis_domain[NIS_MAXNAMELEN + 1];
4045 char buffer[NIS_MAXATTRVAL + 1];
4050 strncpy(nis_domain, (char *)nis_local_directory(), NIS_MAXNAMELEN);
4051 nis_domain[NIS_MAXNAMELEN] = '\0';
4053 DEBUG(5, ("NIS+ Domain: %s\n", nis_domain));
4055 if (strcmp(user_name, last_key))
4057 slprintf(buffer, sizeof(buffer)-1, "[%s=%s]%s.%s", "key", user_name, nis_map, nis_domain);
4058 DEBUG(5, ("NIS+ querystring: %s\n", buffer));
4060 if (result = nis_list(buffer, RETURN_RESULT, NULL, NULL))
4062 if (result->status != NIS_SUCCESS)
4064 DEBUG(3, ("NIS+ query failed: %s\n", nis_sperrno(result->status)));
4065 fstrcpy(last_key, ""); pstrcpy(last_value, "");
4069 object = result->objects.objects_val;
4070 if (object->zo_data.zo_type == ENTRY_OBJ)
4072 entry = &object->zo_data.objdata_u.en_data;
4073 DEBUG(5, ("NIS+ entry type: %s\n", entry->en_type));
4074 DEBUG(3, ("NIS+ result: %s\n", entry->en_cols.en_cols_val[1].ec_value.ec_value_val));
4076 pstrcpy(last_value, entry->en_cols.en_cols_val[1].ec_value.ec_value_val);
4077 string_sub(last_value, "&", user_name);
4078 fstrcpy(last_key, user_name);
4082 nis_freeresult(result);
4084 DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n", user_name, last_value));
4087 #else /* NISPLUS_HOME */
4088 static char *automount_lookup(char *user_name)
4090 static fstring last_key = "";
4091 static pstring last_value = "";
4093 int nis_error; /* returned by yp all functions */
4094 char *nis_result; /* yp_match inits this */
4095 int nis_result_len; /* and set this */
4096 char *nis_domain; /* yp_get_default_domain inits this */
4097 char *nis_map = (char *)lp_nis_home_map_name();
4099 if ((nis_error = yp_get_default_domain(&nis_domain)) != 0)
4101 DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
4105 DEBUG(5, ("NIS Domain: %s\n", nis_domain));
4107 if (!strcmp(user_name, last_key))
4109 nis_result = last_value;
4110 nis_result_len = strlen(last_value);
4115 if ((nis_error = yp_match(nis_domain, nis_map,
4116 user_name, strlen(user_name),
4117 &nis_result, &nis_result_len)) != 0)
4119 DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n",
4120 yperr_string(nis_error), user_name, nis_map));
4122 if (!nis_error && nis_result_len >= sizeof(pstring))
4124 nis_result_len = sizeof(pstring)-1;
4126 fstrcpy(last_key, user_name);
4127 strncpy(last_value, nis_result, nis_result_len);
4128 last_value[nis_result_len] = '\0';
4131 DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name, last_value));
4134 #endif /* NISPLUS_HOME */
4137 /*******************************************************************
4138 Patch from jkf@soton.ac.uk
4139 This is Luke's original function with the NIS lookup code
4140 moved out to a separate function.
4141 *******************************************************************/
4143 char *automount_server(char *user_name)
4145 static pstring server_name;
4147 /* use the local machine name as the default */
4148 /* this will be the default if AUTOMOUNT is not used or fails */
4149 pstrcpy(server_name, local_machine);
4151 #if (defined(NETGROUP) && defined (AUTOMOUNT))
4153 if (lp_nis_home_map())
4155 int home_server_len;
4156 char *automount_value = automount_lookup(user_name);
4157 home_server_len = strcspn(automount_value,":");
4158 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
4159 if (home_server_len > sizeof(pstring))
4161 home_server_len = sizeof(pstring);
4163 strncpy(server_name, automount_value, home_server_len);
4164 server_name[home_server_len] = '\0';
4168 DEBUG(4,("Home server: %s\n", server_name));
4173 /*******************************************************************
4174 Patch from jkf@soton.ac.uk
4175 Added this to implement %p (NIS auto-map version of %H)
4176 *******************************************************************/
4178 char *automount_path(char *user_name)
4180 static pstring server_path;
4182 /* use the passwd entry as the default */
4183 /* this will be the default if AUTOMOUNT is not used or fails */
4184 /* pstrcpy() copes with get_home_dir() returning NULL */
4185 pstrcpy(server_path, get_home_dir(user_name));
4187 #if (defined(NETGROUP) && defined (AUTOMOUNT))
4189 if (lp_nis_home_map())
4191 char *home_path_start;
4192 char *automount_value = automount_lookup(user_name);
4193 home_path_start = strchr(automount_value,':');
4194 if (home_path_start != NULL)
4196 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
4197 home_path_start?(home_path_start+1):""));
4198 pstrcpy(server_path, home_path_start+1);
4203 DEBUG(4,("Home server path: %s\n", server_path));
4209 /*******************************************************************
4210 sub strings with useful parameters
4211 Rewritten by Stefaan A Eeckels <Stefaan.Eeckels@ecc.lu> and
4212 Paul Rippin <pr3245@nopc.eurostat.cec.be>
4213 ********************************************************************/
4214 void standard_sub_basic(char *str)
4218 struct passwd *pass;
4219 char *username = sam_logon_in_ssb ? samlogon_user : sesssetup_user;
4221 for (s = str ; s && *s && (p = strchr(s,'%')); s = p )
4227 if ((pass = Get_Pwnam(username,False))!=NULL)
4229 string_sub(p,"%G",gidtoname(pass->pw_gid));
4237 case 'N' : string_sub(p,"%N", automount_server(username)); break;
4238 case 'I' : string_sub(p,"%I", client_addr(Client)); break;
4239 case 'L' : string_sub(p,"%L", local_machine); break;
4240 case 'M' : string_sub(p,"%M", client_name(Client)); break;
4241 case 'R' : string_sub(p,"%R", remote_proto); break;
4242 case 'T' : string_sub(p,"%T", timestring()); break;
4243 case 'U' : string_sub(p,"%U", username); break;
4244 case 'a' : string_sub(p,"%a", remote_arch); break;
4247 slprintf(pidstr,sizeof(pidstr) - 1, "%d",(int)getpid());
4248 string_sub(p,"%d", pidstr);
4251 case 'h' : string_sub(p,"%h", myhostname); break;
4252 case 'm' : string_sub(p,"%m", remote_machine); break;
4253 case 'v' : string_sub(p,"%v", VERSION); break;
4254 case '$' : /* Expand environment variables */
4256 /* Contributed by Branko Cibej <branko.cibej@hermes.si> */
4267 if ((q = strchr(p,')')) == NULL)
4269 DEBUG(0,("standard_sub_basic: Unterminated environment \
4270 variable [%s]\n", p));
4276 copylen = MIN((q-r),(sizeof(envname)-1));
4277 strncpy(envname,r,copylen);
4278 envname[copylen] = '\0';
4280 if ((envval = getenv(envname)) == NULL)
4282 DEBUG(0,("standard_sub_basic: Environment variable [%s] not set\n",
4288 copylen = MIN((q+1-p),(sizeof(envname)-1));
4289 strncpy(envname,p,copylen);
4290 envname[copylen] = '\0';
4291 string_sub(p,envname,envval);
4294 case '\0': p++; break; /* don't run off end if last character is % */
4295 default : p+=2; break;
4301 /*******************************************************************
4302 are two IPs on the same subnet?
4303 ********************************************************************/
4304 BOOL same_net(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
4306 uint32 net1,net2,nmask;
4308 nmask = ntohl(mask.s_addr);
4309 net1 = ntohl(ip1.s_addr);
4310 net2 = ntohl(ip2.s_addr);
4312 return((net1 & nmask) == (net2 & nmask));
4316 /*******************************************************************
4317 write a string in unicoode format
4318 ********************************************************************/
4319 int PutUniCode(char *dst,char *src)
4323 dst[ret++] = src[0];
4332 /****************************************************************************
4333 a wrapper for gethostbyname() that tries with all lower and all upper case
4334 if the initial name fails
4335 ****************************************************************************/
4336 struct hostent *Get_Hostbyname(char *name)
4338 char *name2 = strdup(name);
4339 struct hostent *ret;
4343 DEBUG(0,("Memory allocation error in Get_Hostbyname! panic\n"));
4349 * This next test is redundent and causes some systems (with
4350 * broken isalnum() calls) problems.
4355 if (!isalnum(*name2))
4362 ret = sys_gethostbyname(name2);
4369 /* try with all lowercase */
4371 ret = sys_gethostbyname(name2);
4378 /* try with all uppercase */
4380 ret = sys_gethostbyname(name2);
4387 /* nothing works :-( */
4393 /****************************************************************************
4394 check if a process exists. Does this work on all unixes?
4395 ****************************************************************************/
4396 BOOL process_exists(int pid)
4398 return(kill(pid,0) == 0 || errno != ESRCH);
4402 /*******************************************************************
4403 turn a uid into a user name
4404 ********************************************************************/
4405 char *uidtoname(int uid)
4407 static char name[40];
4408 struct passwd *pass = getpwuid(uid);
4409 if (pass) return(pass->pw_name);
4410 slprintf(name, sizeof(name) - 1, "%d",uid);
4414 /*******************************************************************
4415 turn a gid into a group name
4416 ********************************************************************/
4417 char *gidtoname(int gid)
4419 static char name[40];
4420 struct group *grp = getgrgid(gid);
4421 if (grp) return(grp->gr_name);
4422 slprintf(name,sizeof(name) - 1, "%d",gid);
4426 /*******************************************************************
4428 ********************************************************************/
4429 void BlockSignals(BOOL block,int signum)
4432 int block_mask = sigmask(signum);
4433 static int oldmask = 0;
4435 oldmask = sigblock(block_mask);
4437 sigsetmask(oldmask);
4438 #elif defined(USE_SIGPROCMASK)
4441 sigaddset(&set,signum);
4442 sigprocmask(block?SIG_BLOCK:SIG_UNBLOCK,&set,NULL);
4447 /*******************************************************************
4448 my own panic function - not suitable for general use
4449 ********************************************************************/
4450 void ajt_panic(void)
4452 system("/usr/bin/X11/xedit -display solen:0 /tmp/ERROR_FAULT");
4457 #define DIRECT direct
4459 #define DIRECT dirent
4462 /*******************************************************************
4463 a readdir wrapper which just returns the file name
4464 also return the inode number if requested
4465 ********************************************************************/
4466 char *readdirname(void *p)
4471 if (!p) return(NULL);
4473 ptr = (struct DIRECT *)readdir(p);
4474 if (!ptr) return(NULL);
4476 dname = ptr->d_name;
4479 if (telldir(p) < 0) return(NULL);
4483 /* this handles a broken compiler setup, causing a mixture
4484 of BSD and SYSV headers and libraries */
4486 static BOOL broken_readdir = False;
4487 if (!broken_readdir && !(*(dname)) && strequal("..",dname-2))
4489 DEBUG(0,("Your readdir() is broken. You have somehow mixed SYSV and BSD headers and libraries\n"));
4490 broken_readdir = True;
4499 pstrcpy(buf, dname);
4500 unix_to_dos(buf, True);
4507 /*******************************************************************
4508 Utility function used to decide if the last component
4509 of a path matches a (possibly wildcarded) entry in a namelist.
4510 ********************************************************************/
4512 BOOL is_in_path(char *name, name_compare_entry *namelist)
4514 pstring last_component;
4517 DEBUG(8, ("is_in_path: %s\n", name));
4519 /* if we have no list it's obviously not in the path */
4520 if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL)))
4522 DEBUG(8,("is_in_path: no name list.\n"));
4526 /* Get the last component of the unix name. */
4527 p = strrchr(name, '/');
4528 strncpy(last_component, p ? ++p : name, sizeof(last_component)-1);
4529 last_component[sizeof(last_component)-1] = '\0';
4531 for(; namelist->name != NULL; namelist++)
4533 if(namelist->is_wild)
4535 /* look for a wildcard match. */
4536 if (mask_match(last_component, namelist->name, case_sensitive, False))
4538 DEBUG(8,("is_in_path: mask match succeeded\n"));
4544 if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
4545 (!case_sensitive && (StrCaseCmp(last_component, namelist->name) == 0)))
4547 DEBUG(8,("is_in_path: match succeeded\n"));
4552 DEBUG(8,("is_in_path: match not found\n"));
4557 /*******************************************************************
4558 Strip a '/' separated list into an array of
4559 name_compare_enties structures suitable for
4560 passing to is_in_path(). We do this for
4561 speed so we can pre-parse all the names in the list
4562 and don't do it for each call to is_in_path().
4563 namelist is modified here and is assumed to be
4564 a copy owned by the caller.
4565 We also check if the entry contains a wildcard to
4566 remove a potentially expensive call to mask_match
4568 ********************************************************************/
4570 void set_namearray(name_compare_entry **ppname_array, char *namelist)
4573 char *nameptr = namelist;
4574 int num_entries = 0;
4577 (*ppname_array) = NULL;
4579 if((nameptr == NULL ) || ((nameptr != NULL) && (*nameptr == '\0')))
4582 /* We need to make two passes over the string. The
4583 first to count the number of elements, the second
4588 if ( *nameptr == '/' )
4590 /* cope with multiple (useless) /s) */
4594 /* find the next / */
4595 name_end = strchr(nameptr, '/');
4597 /* oops - the last check for a / didn't find one. */
4598 if (name_end == NULL)
4601 /* next segment please */
4602 nameptr = name_end + 1;
4606 if(num_entries == 0)
4609 if(( (*ppname_array) = (name_compare_entry *)malloc(
4610 (num_entries + 1) * sizeof(name_compare_entry))) == NULL)
4612 DEBUG(0,("set_namearray: malloc fail\n"));
4616 /* Now copy out the names */
4621 if ( *nameptr == '/' )
4623 /* cope with multiple (useless) /s) */
4627 /* find the next / */
4628 if ((name_end = strchr(nameptr, '/')) != NULL)
4633 /* oops - the last check for a / didn't find one. */
4634 if(name_end == NULL)
4637 (*ppname_array)[i].is_wild = ((strchr( nameptr, '?')!=NULL) ||
4638 (strchr( nameptr, '*')!=NULL));
4639 if(((*ppname_array)[i].name = strdup(nameptr)) == NULL)
4641 DEBUG(0,("set_namearray: malloc fail (1)\n"));
4645 /* next segment please */
4646 nameptr = name_end + 1;
4650 (*ppname_array)[i].name = NULL;
4655 /****************************************************************************
4656 routine to free a namearray.
4657 ****************************************************************************/
4659 void free_namearray(name_compare_entry *name_array)
4664 if(name_array->name != NULL)
4665 free(name_array->name);
4667 free((char *)name_array);
4670 /****************************************************************************
4671 routine to do file locking
4672 ****************************************************************************/
4673 BOOL fcntl_lock(int fd,int op,uint32 offset,uint32 count,int type)
4680 uint32 mask = 0xC0000000;
4682 /* make sure the count is reasonable, we might kill the lockd otherwise */
4685 /* the offset is often strange - remove 2 of its bits if either of
4686 the top two bits are set. Shift the top ones by two bits. This
4687 still allows OLE2 apps to operate, but should stop lockd from
4689 if ((offset & mask) != 0)
4690 offset = (offset & ~mask) | ((offset & mask) >> 2);
4692 uint32 mask = ((unsigned)1<<31);
4694 /* interpret negative counts as large numbers */
4698 /* no negative offsets */
4701 /* count + offset must be in range */
4702 while ((offset < 0 || (offset + count < 0)) && mask)
4710 DEBUG(8,("fcntl_lock %d %d %d %d %d\n",fd,op,(int)offset,(int)count,type));
4713 lock.l_whence = SEEK_SET;
4714 lock.l_start = (int)offset;
4715 lock.l_len = (int)count;
4720 ret = fcntl(fd,op,&lock);
4723 DEBUG(3,("fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
4729 (lock.l_type != F_UNLCK) &&
4730 (lock.l_pid != 0) &&
4731 (lock.l_pid != getpid()))
4733 DEBUG(3,("fd %d is locked by pid %d\n",fd,lock.l_pid));
4737 /* it must be not locked or locked by me */
4741 /* a lock set or unset */
4744 DEBUG(3,("lock failed at offset %d count %d op %d type %d (%s)\n",
4745 offset,count,op,type,strerror(errno)));
4747 /* perhaps it doesn't support this sort of locking?? */
4748 if (errno == EINVAL)
4750 DEBUG(3,("locking not supported? returning True\n"));
4757 /* everything went OK */
4758 DEBUG(8,("Lock call successful\n"));
4766 /*******************************************************************
4767 lock a file - returning a open file descriptor or -1 on failure
4768 The timeout is in seconds. 0 means no timeout
4769 ********************************************************************/
4770 int file_lock(char *name,int timeout)
4772 int fd = open(name,O_RDWR|O_CREAT,0666);
4774 if (fd < 0) return(-1);
4777 if (timeout) t = time(NULL);
4778 while (!timeout || (time(NULL)-t < timeout)) {
4779 if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)) return(fd);
4780 msleep(LOCK_RETRY_TIMEOUT);
4788 /*******************************************************************
4789 unlock a file locked by file_lock
4790 ********************************************************************/
4791 void file_unlock(int fd)
4795 fcntl_lock(fd,F_SETLK,0,1,F_UNLCK);
4800 /*******************************************************************
4801 is the name specified one of my netbios names
4802 returns true is it is equal, false otherwise
4803 ********************************************************************/
4804 BOOL is_myname(char *s)
4809 for (n=0; my_netbios_names[n]; n++) {
4810 if (strequal(my_netbios_names[n], s))
4813 DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret));
4817 /*******************************************************************
4818 set the horrid remote_arch string based on an enum.
4819 ********************************************************************/
4820 void set_remote_arch(enum remote_arch_types type)
4826 fstrcpy(remote_arch, "WfWg");
4829 fstrcpy(remote_arch, "OS2");
4832 fstrcpy(remote_arch, "Win95");
4835 fstrcpy(remote_arch, "WinNT");
4838 fstrcpy(remote_arch,"Samba");
4841 ra_type = RA_UNKNOWN;
4842 fstrcpy(remote_arch, "UNKNOWN");
4847 /*******************************************************************
4848 Get the remote_arch type.
4849 ********************************************************************/
4850 enum remote_arch_types get_remote_arch(void)
4856 /*******************************************************************
4857 skip past some unicode strings in a buffer
4858 ********************************************************************/
4859 char *skip_unicode_string(char *buf,int n)
4870 /*******************************************************************
4871 Return a ascii version of a unicode string
4872 Hack alert: uses fixed buffer(s) and only handles ascii strings
4873 ********************************************************************/
4875 char *unistrn2(uint16 *buf, int len)
4877 static char lbufs[8][MAXUNI];
4879 char *lbuf = lbufs[nexti];
4882 nexti = (nexti+1)%8;
4884 DEBUG(10, ("unistrn2: "));
4886 for (p = lbuf; *buf && p-lbuf < MAXUNI-2 && len > 0; len--, p++, buf++)
4888 DEBUG(10, ("%4x ", *buf));
4898 /*******************************************************************
4899 Return a ascii version of a unicode string
4900 Hack alert: uses fixed buffer(s) and only handles ascii strings
4901 ********************************************************************/
4903 char *unistr2(uint16 *buf)
4905 static char lbufs[8][MAXUNI];
4907 char *lbuf = lbufs[nexti];
4910 nexti = (nexti+1)%8;
4912 DEBUG(10, ("unistr2: "));
4914 for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf++)
4916 DEBUG(10, ("%4x ", *buf));
4926 /*******************************************************************
4927 create a null-terminated unicode string from a null-terminated ascii string.
4928 return number of unicode chars copied, excluding the null character.
4930 only handles ascii strings
4931 ********************************************************************/
4933 int struni2(uint16 *p, char *buf)
4937 if (p == NULL) return 0;
4939 DEBUG(10, ("struni2: "));
4943 for (; *buf && len < MAXUNI-2; len++, p++, buf++)
4945 DEBUG(10, ("%2x ", *buf));
4957 /*******************************************************************
4958 Return a ascii version of a unicode string
4959 Hack alert: uses fixed buffer(s) and only handles ascii strings
4960 ********************************************************************/
4962 char *unistr(char *buf)
4964 static char lbufs[8][MAXUNI];
4966 char *lbuf = lbufs[nexti];
4969 nexti = (nexti+1)%8;
4971 for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf += 2)
4979 /*******************************************************************
4980 strncpy for unicode strings
4981 ********************************************************************/
4982 int unistrncpy(char *dst, char *src, int len)
4986 while (*src && len > 0)
5000 /*******************************************************************
5001 strcpy for unicode strings. returns length (in num of wide chars)
5002 ********************************************************************/
5003 int unistrcpy(char *dst, char *src)
5019 /*******************************************************************
5020 safe string copy into a known length string. maxlength does not
5021 include the terminating zero.
5022 ********************************************************************/
5023 char *safe_strcpy(char *dest, char *src, int maxlength)
5028 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
5039 if (len > maxlength) {
5040 DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
5041 len-maxlength, src));
5045 memcpy(dest, src, len);
5050 /*******************************************************************
5051 safe string cat into a string. maxlength does not
5052 include the terminating zero.
5053 ********************************************************************/
5054 char *safe_strcat(char *dest, char *src, int maxlength)
5056 int src_len, dest_len;
5059 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
5067 src_len = strlen(src);
5068 dest_len = strlen(dest);
5070 if (src_len + dest_len > maxlength) {
5071 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
5072 src_len + dest_len - maxlength, src));
5073 src_len = maxlength - dest_len;
5076 memcpy(&dest[dest_len], src, src_len);
5077 dest[dest_len + src_len] = 0;
5081 /*******************************************************************
5082 align a pointer to a multiple of 4 bytes
5083 ********************************************************************/
5084 char *align4(char *q, char *base)
5088 q += 4 - ((q - base) & 3);
5093 /*******************************************************************
5094 align a pointer to a multiple of 2 bytes
5095 ********************************************************************/
5096 char *align2(char *q, char *base)
5105 /*******************************************************************
5106 align a pointer to a multiple of align_offset bytes. looks like it
5107 will work for offsets of 0, 2 and 4...
5108 ********************************************************************/
5109 char *align_offset(char *q, char *base, int align_offset_len)
5111 int mod = ((q - base) & (align_offset_len-1));
5112 if (align_offset_len != 0 && mod != 0)
5114 q += align_offset_len - mod;
5119 void print_asc(int level, unsigned char *buf,int len)
5123 DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.'));
5126 void dump_data(int level,char *buf1,int len)
5128 unsigned char *buf = (unsigned char *)buf1;
5132 DEBUG(level,("[%03X] ",i));
5134 DEBUG(level,("%02X ",(int)buf[i]));
5136 if (i%8 == 0) DEBUG(level,(" "));
5138 print_asc(level,&buf[i-16],8); DEBUG(level,(" "));
5139 print_asc(level,&buf[i-8],8); DEBUG(level,("\n"));
5140 if (i<len) DEBUG(level,("[%03X] ",i));
5148 if (n>8) DEBUG(level,(" "));
5149 while (n--) DEBUG(level,(" "));
5152 print_asc(level,&buf[i-(i%16)],n); DEBUG(level,(" "));
5154 if (n>0) print_asc(level,&buf[i-n],n);
5155 DEBUG(level,("\n"));
5159 char *tab_depth(int depth)
5161 static pstring spaces;
5162 memset(spaces, ' ', depth * 4);
5163 spaces[depth * 4] = 0;
5167 /*****************************************************************
5168 Convert a SID to an ascii string.
5169 *****************************************************************/
5171 char *sid_to_string(pstring sidstr_out, DOM_SID *sid)
5175 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
5176 uint32 ia = (sid->id_auth[5]) +
5177 (sid->id_auth[4] << 8 ) +
5178 (sid->id_auth[3] << 16) +
5179 (sid->id_auth[2] << 24);
5181 slprintf(sidstr_out, sizeof(pstring) - 1, "S-%d-%d", sid->sid_rev_num, ia);
5183 for (i = 0; i < sid->num_auths; i++)
5185 slprintf(subauth, sizeof(subauth)-1, "-%d", sid->sub_auths[i]);
5186 pstrcat(sidstr_out, subauth);
5189 DEBUG(7,("sid_to_string returning %s\n", sidstr_out));
5193 /*****************************************************************
5194 Convert a string to a SID. Returns True on success, False on fail.
5195 *****************************************************************/
5197 BOOL string_to_sid(DOM_SID *sidout, char *sidstr)
5201 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
5204 memset((char *)sidout, '\0', sizeof(DOM_SID));
5206 if(StrnCaseCmp( sidstr, "S-", 2)) {
5207 DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
5212 if(!next_token(&p, tok, "-")) {
5213 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
5217 /* Get the revision number. */
5218 sidout->sid_rev_num = atoi(tok);
5220 if(!next_token(&p, tok, "-")) {
5221 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
5225 /* identauth in decimal should be < 2^32 */
5228 /* NOTE - the ia value is in big-endian format. */
5229 sidout->id_auth[0] = 0;
5230 sidout->id_auth[1] = 0;
5231 sidout->id_auth[2] = (ia & 0xff000000) >> 24;
5232 sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
5233 sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
5234 sidout->id_auth[5] = (ia & 0x000000ff);
5236 sidout->num_auths = 0;
5238 while(next_token(&p, tok, "-") && sidout->num_auths < MAXSUBAUTHS) {
5240 * NOTE - the subauths are in native machine-endian format. They
5241 * are converted to little-endian when linearized onto the wire.
5243 sidout->sub_auths[sidout->num_auths++] = atoi(tok);
5246 DEBUG(7,("string_to_sid: converted SID %s ok\n", sidstr));