2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1997
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.
30 int Protocol = PROTOCOL_COREPLUS;
32 /* a default finfo structure to ensure all fields are sensible */
33 file_info def_finfo = {-1,0,0,0,0,0,0,""};
35 /* these are some file handles where debug info will be stored */
38 /* the client file descriptor */
41 /* the last IP received from */
42 struct in_addr lastip;
44 /* the last port received from */
47 /* this is used by the chaining code */
53 case handling on filenames
55 int case_default = CASE_LOWER;
60 /* the following control case operations - they are put here so the
61 client can link easily */
64 BOOL use_mangled_map = False;
65 BOOL short_case_preserve;
68 fstring remote_machine="";
69 fstring local_machine="";
70 fstring remote_arch="UNKNOWN";
71 static enum remote_arch_types ra_type = RA_UNKNOWN;
72 fstring remote_proto="UNKNOWN";
73 pstring myhostname="";
74 pstring user_socket_options="";
75 pstring sesssetup_user="";
77 fstring myworkgroup = "";
78 char **my_netbios_names;
80 int smb_read_error = 0;
82 static BOOL stdout_logging = False;
84 static char *filename_dos(char *path,char *buf);
86 /*******************************************************************
87 get ready for syslog stuff
88 ******************************************************************/
89 void setup_logging(char *pname,BOOL interactive)
93 char *p = strrchr(pname,'/');
96 openlog(pname, LOG_PID, LOG_DAEMON);
97 #else /* LOG_DAEMON - for old systems that have no facility codes. */
98 openlog(pname, LOG_PID);
99 #endif /* LOG_DAEMON */
103 stdout_logging = True;
109 BOOL append_log=False;
112 /****************************************************************************
114 ****************************************************************************/
115 void reopen_logs(void)
122 strcpy(fname,debugf);
123 if (lp_loaded() && (*lp_logfile()))
124 strcpy(fname,lp_logfile());
126 if (!strcsequal(fname,debugf) || !dbf || !file_exist(debugf,NULL))
128 int oldumask = umask(022);
129 strcpy(debugf,fname);
130 if (dbf) fclose(dbf);
132 dbf = fopen(debugf,"a");
134 dbf = fopen(debugf,"w");
135 if (dbf) setbuf(dbf,NULL);
150 /*******************************************************************
151 check if the log has grown too big
152 ********************************************************************/
153 static void check_log_size(void)
155 static int debug_count=0;
159 if (debug_count++ < 100 || getuid() != 0) return;
161 maxlog = lp_max_log_size() * 1024;
162 if (!dbf || maxlog <= 0) return;
164 if (fstat(fileno(dbf),&st) == 0 && st.st_size > maxlog) {
165 fclose(dbf); dbf = NULL;
167 if (dbf && file_size(debugf) > maxlog) {
169 fclose(dbf); dbf = NULL;
170 sprintf(name,"%s.old",debugf);
171 sys_rename(debugf,name);
179 /*******************************************************************
180 write an debug message on the debugfile. This is called by the DEBUG
182 ********************************************************************/
184 int Debug1(char *format_str, ...)
193 int old_errno = errno;
195 if (stdout_logging) {
197 va_start(ap, format_str);
200 format_str = va_arg(ap,char *);
202 vfprintf(dbf,format_str,ap);
209 if (!lp_syslog_only())
213 int oldumask = umask(022);
214 dbf = fopen(debugf,"w");
226 if (syslog_level < lp_syslog())
229 * map debug levels to syslog() priorities
230 * note that not all DEBUG(0, ...) calls are
233 static int priority_map[] = {
242 if (syslog_level >= sizeof(priority_map) / sizeof(priority_map[0]) ||
244 priority = LOG_DEBUG;
246 priority = priority_map[syslog_level];
249 va_start(ap, format_str);
252 format_str = va_arg(ap,char *);
254 vsprintf(msgbuf, format_str, ap);
258 syslog(priority, "%s", msgbuf);
263 if (!lp_syslog_only())
267 va_start(ap, format_str);
270 format_str = va_arg(ap,char *);
272 vfprintf(dbf,format_str,ap);
284 /****************************************************************************
285 find a suitable temporary directory. The result should be copied immediately
286 as it may be overwritten by a subsequent call
287 ****************************************************************************/
291 if ((p = getenv("TMPDIR"))) {
299 /****************************************************************************
300 determine if a file descriptor is in fact a socket
301 ****************************************************************************/
302 BOOL is_a_socket(int fd)
306 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
310 static char *last_ptr=NULL;
312 /****************************************************************************
313 Get the next token from a string, return False if none found
314 handles double-quotes.
315 Based on a routine by GJC@VILLAGE.COM.
316 Extensively modified by Andrew.Tridgell@anu.edu.au
317 ****************************************************************************/
318 BOOL next_token(char **ptr,char *buff,char *sep)
323 if (!ptr) ptr = &last_ptr;
324 if (!ptr) return(False);
328 /* default to simple separators */
329 if (!sep) sep = " \t\n\r";
331 /* find the first non sep char */
332 while(*s && strchr(sep,*s)) s++;
335 if (! *s) return(False);
337 /* copy over the token */
338 for (quoted = False; *s && (quoted || !strchr(sep,*s)); s++)
346 *ptr = (*s) ? s+1 : s;
353 /****************************************************************************
354 Convert list of tokens to array; dependent on above routine.
355 Uses last_ptr from above - bit of a hack.
356 ****************************************************************************/
357 char **toktocliplist(int *ctok, char *sep)
363 if (!sep) sep = " \t\n\r";
365 while(*s && strchr(sep,*s)) s++;
368 if (!*s) return(NULL);
372 while(*s && (!strchr(sep,*s))) s++;
373 while(*s && strchr(sep,*s)) *s++=0;
379 if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL;
391 /*******************************************************************
392 safely copies memory, ensuring no overlap problems.
393 this is only used if the machine does not have it's own memmove().
394 this is not the fastest algorithm in town, but it will do for our
396 ********************************************************************/
397 void *MemMove(void *dest,void *src,int size)
401 if (dest==src || !size) return(dest);
403 d = (unsigned long)dest;
404 s = (unsigned long)src;
406 if ((d >= (s+size)) || (s >= (d+size))) {
408 memcpy(dest,src,size);
414 /* we can forward copy */
415 if (s-d >= sizeof(int) &&
416 !(s%sizeof(int)) && !(d%sizeof(int)) && !(size%sizeof(int))) {
417 /* do it all as words */
418 int *idest = (int *)dest;
419 int *isrc = (int *)src;
421 for (i=0;i<size;i++) idest[i] = isrc[i];
424 char *cdest = (char *)dest;
425 char *csrc = (char *)src;
426 for (i=0;i<size;i++) cdest[i] = csrc[i];
431 /* must backward copy */
432 if (d-s >= sizeof(int) &&
433 !(s%sizeof(int)) && !(d%sizeof(int)) && !(size%sizeof(int))) {
434 /* do it all as words */
435 int *idest = (int *)dest;
436 int *isrc = (int *)src;
438 for (i=size-1;i>=0;i--) idest[i] = isrc[i];
441 char *cdest = (char *)dest;
442 char *csrc = (char *)src;
443 for (i=size-1;i>=0;i--) cdest[i] = csrc[i];
451 /****************************************************************************
452 prompte a dptr (to make it recently used)
453 ****************************************************************************/
454 void array_promote(char *array,int elsize,int element)
460 p = (char *)malloc(elsize);
464 DEBUG(5,("Ahh! Can't malloc\n"));
467 memcpy(p,array + element * elsize, elsize);
468 memmove(array + elsize,array,elsize*element);
469 memcpy(array,p,elsize);
473 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
482 } socket_options[] = {
483 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
484 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
485 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
487 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
489 #ifdef IPTOS_LOWDELAY
490 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
492 #ifdef IPTOS_THROUGHPUT
493 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
496 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
499 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
502 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
505 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
508 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
511 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
517 /****************************************************************************
518 set user socket options
519 ****************************************************************************/
520 void set_socket_options(int fd, char *options)
524 while (next_token(&options,tok," \t,"))
529 BOOL got_value = False;
531 if ((p = strchr(tok,'=')))
538 for (i=0;socket_options[i].name;i++)
539 if (strequal(socket_options[i].name,tok))
542 if (!socket_options[i].name)
544 DEBUG(0,("Unknown socket option %s\n",tok));
548 switch (socket_options[i].opttype)
552 ret = setsockopt(fd,socket_options[i].level,
553 socket_options[i].option,(char *)&value,sizeof(int));
558 DEBUG(0,("syntax error - %s does not take a value\n",tok));
561 int on = socket_options[i].value;
562 ret = setsockopt(fd,socket_options[i].level,
563 socket_options[i].option,(char *)&on,sizeof(int));
569 DEBUG(0,("Failed to set socket option %s\n",tok));
575 /****************************************************************************
576 close the socket communication
577 ****************************************************************************/
578 void close_sockets(void )
584 /****************************************************************************
585 determine whether we are in the specified group
586 ****************************************************************************/
587 BOOL in_group(gid_t group, int current_gid, int ngroups, int *groups)
591 if (group == current_gid) return(True);
593 for (i=0;i<ngroups;i++)
594 if (group == groups[i])
600 /****************************************************************************
601 this is a safer strcpy(), meant to prevent core dumps when nasty things happen
602 ****************************************************************************/
603 char *StrCpy(char *dest,char *src)
608 /* I don't want to get lazy with these ... */
610 DEBUG(0,("ERROR: NULL StrCpy() called!\n"));
615 if (!dest) return(NULL);
620 while ((*d++ = *src++)) ;
624 /****************************************************************************
625 line strncpy but always null terminates. Make sure there is room!
626 ****************************************************************************/
627 char *StrnCpy(char *dest,char *src,int n)
630 if (!dest) return(NULL);
635 while (n-- && (*d++ = *src++)) ;
641 /*******************************************************************
642 copy an IP address from one buffer to another
643 ********************************************************************/
644 void putip(void *dest,void *src)
650 /****************************************************************************
651 interpret the weird netbios "name". Return the name type
652 ****************************************************************************/
653 static int name_interpret(char *in,char *out)
656 int len = (*in++) / 2;
660 if (len > 30 || len<1) return(0);
664 if (in[0] < 'A' || in[0] > 'P' || in[1] < 'A' || in[1] > 'P') {
668 *out = ((in[0]-'A')<<4) + (in[1]-'A');
676 /* Handle any scope names */
679 *out++ = '.'; /* Scope names are separated by periods */
680 len = *(unsigned char *)in++;
681 StrnCpy(out, in, len);
690 /****************************************************************************
691 mangle a name into netbios format
693 Note: <Out> must be (33 + strlen(scope) + 2) bytes long, at minimum.
694 ****************************************************************************/
695 int name_mangle( char *In, char *Out, char name_type )
703 /* Safely copy the input string, In, into buf[]. */
704 (void)memset( buf, 0, 20 );
708 (void)sprintf( buf, "%-15.15s%c", In, name_type );
710 /* Place the length of the first field into the output buffer. */
714 /* Now convert the name to the rfc1001/1002 format. */
715 for( i = 0; i < 16; i++ )
717 c = toupper( buf[i] );
718 p[i*2] = ( (c >> 4) & 0x000F ) + 'A';
719 p[(i*2)+1] = (c & 0x000F) + 'A';
724 /* Add the scope string. */
725 for( i = 0, len = 0; NULL != scope; i++, len++ )
733 return( name_len(Out) );
745 return( name_len(Out) );
748 /*******************************************************************
749 check if a file exists
750 ********************************************************************/
751 BOOL file_exist(char *fname,struct stat *sbuf)
754 if (!sbuf) sbuf = &st;
756 if (sys_stat(fname,sbuf) != 0)
759 return(S_ISREG(sbuf->st_mode));
762 /*******************************************************************
763 check a files mod time
764 ********************************************************************/
765 time_t file_modtime(char *fname)
769 if (sys_stat(fname,&st) != 0)
775 /*******************************************************************
776 check if a directory exists
777 ********************************************************************/
778 BOOL directory_exist(char *dname,struct stat *st)
785 if (sys_stat(dname,st) != 0)
788 ret = S_ISDIR(st->st_mode);
794 /*******************************************************************
795 returns the size in bytes of the named file
796 ********************************************************************/
797 uint32 file_size(char *file_name)
801 sys_stat(file_name,&buf);
805 /*******************************************************************
806 return a string representing an attribute for a file
807 ********************************************************************/
808 char *attrib_string(int mode)
810 static char attrstr[10];
814 if (mode & aVOLID) strcat(attrstr,"V");
815 if (mode & aDIR) strcat(attrstr,"D");
816 if (mode & aARCH) strcat(attrstr,"A");
817 if (mode & aHIDDEN) strcat(attrstr,"H");
818 if (mode & aSYSTEM) strcat(attrstr,"S");
819 if (mode & aRONLY) strcat(attrstr,"R");
825 /*******************************************************************
826 case insensitive string compararison
827 ********************************************************************/
828 int StrCaseCmp(char *s, char *t)
830 /* compare until we run out of string, either t or s, or find a difference */
831 /* We *must* use toupper rather than tolower here due to the
832 asynchronous upper to lower mapping.
834 #if !defined(KANJI_WIN95_COMPATIBILITY)
835 if(lp_client_code_page() == KANJI_CODEPAGE)
837 /* Win95 treats full width ascii characters as case sensitive. */
842 return toupper (*s) - toupper (*t);
843 else if (is_sj_alph (*s) && is_sj_alph (*t))
845 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
851 else if (is_shift_jis (*s) && is_shift_jis (*t))
853 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
856 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
862 else if (is_shift_jis (*s))
864 else if (is_shift_jis (*t))
868 diff = toupper (*s) - toupper (*t);
877 #endif /* KANJI_WIN95_COMPATIBILITY */
879 while (*s && *t && toupper(*s) == toupper(*t))
885 return(toupper(*s) - toupper(*t));
889 /*******************************************************************
890 case insensitive string compararison, length limited
891 ********************************************************************/
892 int StrnCaseCmp(char *s, char *t, int n)
894 /* compare until we run out of string, either t or s, or chars */
895 /* We *must* use toupper rather than tolower here due to the
896 asynchronous upper to lower mapping.
898 #if !defined(KANJI_WIN95_COMPATIBILITY)
899 if(lp_client_code_page() == KANJI_CODEPAGE)
901 /* Win95 treats full width ascii characters as case sensitive. */
906 return toupper (*s) - toupper (*t);
907 else if (is_sj_alph (*s) && is_sj_alph (*t))
909 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
916 else if (is_shift_jis (*s) && is_shift_jis (*t))
918 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
921 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
928 else if (is_shift_jis (*s))
930 else if (is_shift_jis (*t))
934 diff = toupper (*s) - toupper (*t);
945 #endif /* KANJI_WIN95_COMPATIBILITY */
947 while (n-- && *s && *t && toupper(*s) == toupper(*t))
953 /* not run out of chars - strings are different lengths */
955 return(toupper(*s) - toupper(*t));
957 /* identical up to where we run out of chars,
958 and strings are same length */
963 /*******************************************************************
965 ********************************************************************/
966 BOOL strequal(char *s1, char *s2)
968 if (s1 == s2) return(True);
969 if (!s1 || !s2) return(False);
971 return(StrCaseCmp(s1,s2)==0);
974 /*******************************************************************
975 compare 2 strings up to and including the nth char.
976 ******************************************************************/
977 BOOL strnequal(char *s1,char *s2,int n)
979 if (s1 == s2) return(True);
980 if (!s1 || !s2 || !n) return(False);
982 return(StrnCaseCmp(s1,s2,n)==0);
985 /*******************************************************************
986 compare 2 strings (case sensitive)
987 ********************************************************************/
988 BOOL strcsequal(char *s1,char *s2)
990 if (s1 == s2) return(True);
991 if (!s1 || !s2) return(False);
993 return(strcmp(s1,s2)==0);
997 /*******************************************************************
998 convert a string to lower case
999 ********************************************************************/
1000 void strlower(char *s)
1004 #if !defined(KANJI_WIN95_COMPATIBILITY)
1005 if(lp_client_code_page() == KANJI_CODEPAGE)
1007 /* Win95 treats full width ascii characters as case sensitive. */
1008 if (is_shift_jis (*s))
1010 if (is_sj_upper (s[0], s[1]))
1011 s[1] = sj_tolower2 (s[1]);
1014 else if (is_kana (*s))
1026 #endif /* KANJI_WIN95_COMPATIBILITY */
1035 /*******************************************************************
1036 convert a string to upper case
1037 ********************************************************************/
1038 void strupper(char *s)
1042 #if !defined(KANJI_WIN95_COMPATIBILITY)
1043 if(lp_client_code_page() == KANJI_CODEPAGE)
1045 /* Win95 treats full width ascii characters as case sensitive. */
1046 if (is_shift_jis (*s))
1048 if (is_sj_lower (s[0], s[1]))
1049 s[1] = sj_toupper2 (s[1]);
1052 else if (is_kana (*s))
1064 #endif /* KANJI_WIN95_COMPATIBILITY */
1073 /*******************************************************************
1074 convert a string to "normal" form
1075 ********************************************************************/
1076 void strnorm(char *s)
1078 if (case_default == CASE_UPPER)
1084 /*******************************************************************
1085 check if a string is in "normal" case
1086 ********************************************************************/
1087 BOOL strisnormal(char *s)
1089 if (case_default == CASE_UPPER)
1090 return(!strhaslower(s));
1092 return(!strhasupper(s));
1096 /****************************************************************************
1098 ****************************************************************************/
1099 void string_replace(char *s,char oldc,char newc)
1103 #if !defined(KANJI_WIN95_COMPATIBILITY)
1104 if(lp_client_code_page() == KANJI_CODEPAGE)
1106 /* Win95 treats full width ascii characters as case sensitive. */
1107 if (is_shift_jis (*s))
1109 else if (is_kana (*s))
1119 #endif /* KANJI_WIN95_COMPATIBILITY */
1128 /****************************************************************************
1129 make a file into unix format
1130 ****************************************************************************/
1131 void unix_format(char *fname)
1134 string_replace(fname,'\\','/');
1138 pstrcpy(namecopy,fname);
1140 strcat(fname,namecopy);
1144 /****************************************************************************
1145 make a file into dos format
1146 ****************************************************************************/
1147 void dos_format(char *fname)
1149 string_replace(fname,'/','\\');
1153 /*******************************************************************
1154 show a smb message structure
1155 ********************************************************************/
1156 void show_msg(char *buf)
1164 DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
1166 (int)CVAL(buf,smb_com),
1167 (int)CVAL(buf,smb_rcls),
1168 (int)CVAL(buf,smb_reh),
1169 (int)SVAL(buf,smb_err),
1170 (int)CVAL(buf,smb_flg),
1171 (int)SVAL(buf,smb_flg2)));
1172 DEBUG(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\nsmt_wct=%d\n",
1173 (int)SVAL(buf,smb_tid),
1174 (int)SVAL(buf,smb_pid),
1175 (int)SVAL(buf,smb_uid),
1176 (int)SVAL(buf,smb_mid),
1177 (int)CVAL(buf,smb_wct)));
1178 for (i=0;i<(int)CVAL(buf,smb_wct);i++)
1179 DEBUG(5,("smb_vwv[%d]=%d (0x%X)\n",i,
1180 SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
1181 bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
1182 DEBUG(5,("smb_bcc=%d\n",bcc));
1183 if (DEBUGLEVEL < 10)
1185 for (i = 0; i < MIN(bcc, 512); i += 16)
1187 for (j = 0; j < 16 && i+j < MIN(bcc,512); j++)
1190 DEBUG(10,("%2X ",CVAL(smb_buf(buf),i+j)));
1191 if (j == 7) DEBUG(10, (" "));
1196 for (j = 0; j < 16 && i+j < MIN(bcc,512); j++)
1198 unsigned char c = CVAL(smb_buf(buf),i+j);
1199 if (c < 32 || c > 128) c = '.';
1202 if (j == 7) DEBUG(10, (" "));
1209 /*******************************************************************
1210 return the length of an smb packet
1211 ********************************************************************/
1212 int smb_len(char *buf)
1214 return( PVAL(buf,3) | (PVAL(buf,2)<<8) | ((PVAL(buf,1)&1)<<16) );
1217 /*******************************************************************
1218 set the length of an smb packet
1219 ********************************************************************/
1220 void _smb_setlen(char *buf,int len)
1223 buf[1] = (len&0x10000)>>16;
1224 buf[2] = (len&0xFF00)>>8;
1228 /*******************************************************************
1229 set the length and marker of an smb packet
1230 ********************************************************************/
1231 void smb_setlen(char *buf,int len)
1233 _smb_setlen(buf,len);
1241 /*******************************************************************
1242 setup the word count and byte count for a smb message
1243 ********************************************************************/
1244 int set_message(char *buf,int num_words,int num_bytes,BOOL zero)
1247 bzero(buf + smb_size,num_words*2 + num_bytes);
1248 CVAL(buf,smb_wct) = num_words;
1249 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
1250 smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
1251 return (smb_size + num_words*2 + num_bytes);
1254 /*******************************************************************
1255 return the number of smb words
1256 ********************************************************************/
1257 int smb_numwords(char *buf)
1259 return (CVAL(buf,smb_wct));
1262 /*******************************************************************
1263 return the size of the smb_buf region of a message
1264 ********************************************************************/
1265 int smb_buflen(char *buf)
1267 return(SVAL(buf,smb_vwv0 + smb_numwords(buf)*2));
1270 /*******************************************************************
1271 return a pointer to the smb_buf data area
1272 ********************************************************************/
1273 int smb_buf_ofs(char *buf)
1275 return (smb_size + CVAL(buf,smb_wct)*2);
1278 /*******************************************************************
1279 return a pointer to the smb_buf data area
1280 ********************************************************************/
1281 char *smb_buf(char *buf)
1283 return (buf + smb_buf_ofs(buf));
1286 /*******************************************************************
1287 return the SMB offset into an SMB buffer
1288 ********************************************************************/
1289 int smb_offset(char *p,char *buf)
1291 return(PTR_DIFF(p,buf+4) + chain_size);
1295 /*******************************************************************
1296 skip past some strings in a buffer
1297 ********************************************************************/
1298 char *skip_string(char *buf,int n)
1301 buf += strlen(buf) + 1;
1305 /*******************************************************************
1306 trim the specified elements off the front and back of a string
1307 ********************************************************************/
1308 BOOL trim_string(char *s,char *front,char *back)
1311 while (front && *front && strncmp(s,front,strlen(front)) == 0)
1317 if (!(*p = p[strlen(front)]))
1322 while (back && *back && strlen(s) >= strlen(back) &&
1323 (strncmp(s+strlen(s)-strlen(back),back,strlen(back))==0))
1326 s[strlen(s)-strlen(back)] = 0;
1332 /*******************************************************************
1333 reduce a file name, removing .. elements.
1334 ********************************************************************/
1335 void dos_clean_name(char *s)
1339 DEBUG(3,("dos_clean_name [%s]\n",s));
1341 /* remove any double slashes */
1342 string_sub(s, "\\\\", "\\");
1344 while ((p = strstr(s,"\\..\\")) != NULL)
1351 if ((p=strrchr(s,'\\')) != NULL)
1358 trim_string(s,NULL,"\\..");
1360 string_sub(s, "\\.\\", "\\");
1363 /*******************************************************************
1364 reduce a file name, removing .. elements.
1365 ********************************************************************/
1366 void unix_clean_name(char *s)
1370 DEBUG(3,("unix_clean_name [%s]\n",s));
1372 /* remove any double slashes */
1373 string_sub(s, "//","/");
1375 /* Remove leading ./ characters */
1376 if(strncmp(s, "./", 2) == 0) {
1377 trim_string(s, "./", NULL);
1382 while ((p = strstr(s,"/../")) != NULL)
1389 if ((p=strrchr(s,'/')) != NULL)
1396 trim_string(s,NULL,"/..");
1400 /*******************************************************************
1401 a wrapper for the normal chdir() function
1402 ********************************************************************/
1403 int ChDir(char *path)
1406 static pstring LastDir="";
1408 if (strcsequal(path,".")) return(0);
1410 if (*path == '/' && strcsequal(LastDir,path)) return(0);
1411 DEBUG(3,("chdir to %s\n",path));
1412 res = sys_chdir(path);
1414 pstrcpy(LastDir,path);
1418 /* number of list structures for a caching GetWd function. */
1419 #define MAX_GETWDCACHE (50)
1427 } ino_list[MAX_GETWDCACHE];
1429 BOOL use_getwd_cache=True;
1431 /*******************************************************************
1432 return the absolute current directory path
1433 ********************************************************************/
1434 char *GetWd(char *str)
1437 static BOOL getwd_cache_init = False;
1438 struct stat st, st2;
1443 if (!use_getwd_cache)
1444 return(sys_getwd(str));
1446 /* init the cache */
1447 if (!getwd_cache_init)
1449 getwd_cache_init = True;
1450 for (i=0;i<MAX_GETWDCACHE;i++)
1452 string_init(&ino_list[i].text,"");
1453 ino_list[i].valid = False;
1457 /* Get the inode of the current directory, if this doesn't work we're
1460 if (stat(".",&st) == -1)
1462 DEBUG(0,("Very strange, couldn't stat \".\"\n"));
1463 return(sys_getwd(str));
1467 for (i=0; i<MAX_GETWDCACHE; i++)
1468 if (ino_list[i].valid)
1471 /* If we have found an entry with a matching inode and dev number
1472 then find the inode number for the directory in the cached string.
1473 If this agrees with that returned by the stat for the current
1474 directory then all is o.k. (but make sure it is a directory all
1477 if (st.st_ino == ino_list[i].inode &&
1478 st.st_dev == ino_list[i].dev)
1480 if (stat(ino_list[i].text,&st2) == 0)
1482 if (st.st_ino == st2.st_ino &&
1483 st.st_dev == st2.st_dev &&
1484 (st2.st_mode & S_IFMT) == S_IFDIR)
1486 strcpy (str, ino_list[i].text);
1488 /* promote it for future use */
1489 array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
1494 /* If the inode is different then something's changed,
1495 scrub the entry and start from scratch. */
1496 ino_list[i].valid = False;
1503 /* We don't have the information to hand so rely on traditional methods.
1504 The very slow getcwd, which spawns a process on some systems, or the
1505 not quite so bad getwd. */
1509 DEBUG(0,("Getwd failed, errno %s\n",strerror(errno)));
1515 DEBUG(5,("GetWd %s, inode %d, dev %x\n",s,(int)st.st_ino,(int)st.st_dev));
1517 /* add it to the cache */
1518 i = MAX_GETWDCACHE - 1;
1519 string_set(&ino_list[i].text,s);
1520 ino_list[i].dev = st.st_dev;
1521 ino_list[i].inode = st.st_ino;
1522 ino_list[i].valid = True;
1524 /* put it at the top of the list */
1525 array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
1532 /*******************************************************************
1533 reduce a file name, removing .. elements and checking that
1534 it is below dir in the heirachy. This uses GetWd() and so must be run
1535 on the system that has the referenced file system.
1537 widelinks are allowed if widelinks is true
1538 ********************************************************************/
1539 BOOL reduce_name(char *s,char *dir,BOOL widelinks)
1541 #ifndef REDUCE_PATHS
1549 BOOL relative = (*s != '/');
1551 *dir2 = *wd = *basename = *newname = 0;
1556 /* can't have a leading .. */
1557 if (strncmp(s,"..",2) == 0 && (s[2]==0 || s[2]=='/'))
1559 DEBUG(3,("Illegal file name? (%s)\n",s));
1569 DEBUG(3,("reduce_name [%s] [%s]\n",s,dir));
1571 /* remove any double slashes */
1572 string_sub(s,"//","/");
1574 pstrcpy(basename,s);
1575 p = strrchr(basename,'/');
1582 DEBUG(0,("couldn't getwd for %s %s\n",s,dir));
1586 if (ChDir(dir) != 0)
1588 DEBUG(0,("couldn't chdir to %s\n",dir));
1594 DEBUG(0,("couldn't getwd for %s\n",dir));
1600 if (p && (p != basename))
1603 if (strcmp(p+1,".")==0)
1605 if (strcmp(p+1,"..")==0)
1609 if (ChDir(basename) != 0)
1612 DEBUG(3,("couldn't chdir for %s %s basename=%s\n",s,dir,basename));
1616 if (!GetWd(newname))
1619 DEBUG(2,("couldn't get wd for %s %s\n",s,dir2));
1623 if (p && (p != basename))
1625 strcat(newname,"/");
1626 strcat(newname,p+1);
1630 int l = strlen(dir2);
1631 if (dir2[l-1] == '/')
1634 if (strncmp(newname,dir2,l) != 0)
1637 DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n",s,dir2,newname,l));
1643 if (newname[l] == '/')
1644 pstrcpy(s,newname + l + 1);
1646 pstrcpy(s,newname+l);
1657 DEBUG(3,("reduced to %s\n",s));
1662 /****************************************************************************
1664 ****************************************************************************/
1665 static void expand_one(char *Mask,int len)
1668 while ((p1 = strchr(Mask,'*')) != NULL)
1670 int lfill = (len+1) - strlen(Mask);
1671 int l1= (p1 - Mask);
1674 memset(tmp+l1,'?',lfill);
1675 pstrcpy(tmp + l1 + lfill,Mask + l1 + 1);
1680 /****************************************************************************
1681 expand a wildcard expression, replacing *s with ?s
1682 ****************************************************************************/
1683 void expand_mask(char *Mask,BOOL doext)
1688 BOOL hasdot = False;
1690 BOOL absolute = (*Mask == '\\');
1692 *mbeg = *mext = *dirpart = *filepart = 0;
1694 /* parse the directory and filename */
1695 if (strchr(Mask,'\\'))
1696 dirname_dos(Mask,dirpart);
1698 filename_dos(Mask,filepart);
1700 pstrcpy(mbeg,filepart);
1701 if ((p1 = strchr(mbeg,'.')) != NULL)
1711 if (strlen(mbeg) > 8)
1713 pstrcpy(mext,mbeg + 8);
1719 strcpy(mbeg,"????????");
1720 if ((*mext == 0) && doext && !hasdot)
1723 if (strequal(mbeg,"*") && *mext==0)
1731 pstrcpy(Mask,dirpart);
1732 if (*dirpart || absolute) strcat(Mask,"\\");
1737 DEBUG(6,("Mask expanded to [%s]\n",Mask));
1741 /****************************************************************************
1742 does a string have any uppercase chars in it?
1743 ****************************************************************************/
1744 BOOL strhasupper(char *s)
1748 #if !defined(KANJI_WIN95_COMPATIBILITY)
1749 if(lp_client_code_page() == KANJI_CODEPAGE)
1751 /* Win95 treats full width ascii characters as case sensitive. */
1752 if (is_shift_jis (*s))
1754 else if (is_kana (*s))
1764 #endif /* KANJI_WIN95_COMPATIBILITY */
1774 /****************************************************************************
1775 does a string have any lowercase chars in it?
1776 ****************************************************************************/
1777 BOOL strhaslower(char *s)
1781 #if !defined(KANJI_WIN95_COMPATIBILITY)
1782 if(lp_client_code_page() == KANJI_CODEPAGE)
1784 /* Win95 treats full width ascii characters as case sensitive. */
1785 if (is_shift_jis (*s))
1787 if (is_sj_upper (s[0], s[1]))
1789 if (is_sj_lower (s[0], s[1]))
1793 else if (is_kana (*s))
1805 #endif /* KANJI_WIN95_COMPATIBILITY */
1815 /****************************************************************************
1816 find the number of chars in a string
1817 ****************************************************************************/
1818 int count_chars(char *s,char c)
1822 #if !defined(KANJI_WIN95_COMPATIBILITY)
1823 if(lp_client_code_page() == KANJI_CODEPAGE)
1825 /* Win95 treats full width ascii characters as case sensitive. */
1828 if (is_shift_jis (*s))
1839 #endif /* KANJI_WIN95_COMPATIBILITY */
1852 /****************************************************************************
1854 ****************************************************************************/
1855 void make_dir_struct(char *buf,char *mask,char *fname,unsigned int size,int mode,time_t date)
1860 pstrcpy(mask2,mask);
1862 if ((mode & aDIR) != 0)
1865 memset(buf+1,' ',11);
1866 if ((p = strchr(mask2,'.')) != NULL)
1869 memcpy(buf+1,mask2,MIN(strlen(mask2),8));
1870 memcpy(buf+9,p+1,MIN(strlen(p+1),3));
1874 memcpy(buf+1,mask2,MIN(strlen(mask2),11));
1876 bzero(buf+21,DIR_STRUCT_SIZE-21);
1877 CVAL(buf,21) = mode;
1878 put_dos_date(buf,22,date);
1879 SSVAL(buf,26,size & 0xFFFF);
1880 SSVAL(buf,28,size >> 16);
1881 StrnCpy(buf+30,fname,12);
1882 if (!case_sensitive)
1884 DEBUG(8,("put name [%s] into dir struct\n",buf+30));
1888 /*******************************************************************
1889 close the low 3 fd's and open dev/null in their place
1890 ********************************************************************/
1891 void close_low_fds(void)
1895 close(0); close(1); close(2);
1896 /* try and use up these file descriptors, so silly
1897 library routines writing to stdout etc won't cause havoc */
1899 fd = open("/dev/null",O_RDWR,0);
1900 if (fd < 0) fd = open("/dev/null",O_WRONLY,0);
1902 DEBUG(0,("Can't open /dev/null\n"));
1906 DEBUG(0,("Didn't get file descriptor %d\n",i));
1912 /****************************************************************************
1913 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
1915 if SYSV use O_NDELAY
1917 ****************************************************************************/
1918 int set_blocking(int fd, BOOL set)
1922 #define FLAG_TO_SET O_NONBLOCK
1925 #define FLAG_TO_SET O_NDELAY
1927 #define FLAG_TO_SET FNDELAY
1931 if((val = fcntl(fd, F_GETFL, 0)) == -1)
1933 if(set) /* Turn blocking on - ie. clear nonblock flag */
1934 val &= ~FLAG_TO_SET;
1937 return fcntl( fd, F_SETFL, val);
1942 /****************************************************************************
1944 ****************************************************************************/
1945 int write_socket(int fd,char *buf,int len)
1951 DEBUG(6,("write_socket(%d,%d)\n",fd,len));
1952 ret = write_data(fd,buf,len);
1954 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,len,ret));
1958 /****************************************************************************
1960 ****************************************************************************/
1961 int read_udp_socket(int fd,char *buf,int len)
1964 struct sockaddr sock;
1967 socklen = sizeof(sock);
1968 bzero((char *)&sock,socklen);
1969 bzero((char *)&lastip,sizeof(lastip));
1970 ret = recvfrom(fd,buf,len,0,&sock,&socklen);
1972 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
1976 lastip = *(struct in_addr *) &sock.sa_data[2];
1977 lastport = ntohs(((struct sockaddr_in *)&sock)->sin_port);
1982 /****************************************************************************
1983 read data from a device with a timout in msec.
1984 mincount = if timeout, minimum to read before returning
1985 maxcount = number to be read.
1986 ****************************************************************************/
1987 int read_with_timeout(int fd,char *buf,int mincnt,int maxcnt,long time_out)
1993 struct timeval timeout;
1995 /* just checking .... */
1996 if (maxcnt <= 0) return(0);
2001 if (time_out <= 0) {
2002 if (mincnt == 0) mincnt = maxcnt;
2004 while (nread < mincnt) {
2005 readret = read(fd, buf + nread, maxcnt - nread);
2007 smb_read_error = READ_EOF;
2011 if (readret == -1) {
2012 smb_read_error = READ_ERROR;
2020 /* Most difficult - timeout read */
2021 /* If this is ever called on a disk file and
2022 mincnt is greater then the filesize then
2023 system performance will suffer severely as
2024 select always return true on disk files */
2026 /* Set initial timeout */
2027 timeout.tv_sec = time_out / 1000;
2028 timeout.tv_usec = 1000 * (time_out % 1000);
2030 for (nread=0; nread<mincnt; )
2035 selrtn = sys_select(&fds,&timeout);
2037 /* Check if error */
2039 /* something is wrong. Maybe the socket is dead? */
2040 smb_read_error = READ_ERROR;
2044 /* Did we timeout ? */
2046 smb_read_error = READ_TIMEOUT;
2050 readret = read(fd, buf+nread, maxcnt-nread);
2052 /* we got EOF on the file descriptor */
2053 smb_read_error = READ_EOF;
2057 if (readret == -1) {
2058 /* the descriptor is probably dead */
2059 smb_read_error = READ_ERROR;
2066 /* Return the number we got */
2070 /****************************************************************************
2071 read data from the client. Maxtime is in milliseconds
2072 ****************************************************************************/
2073 int read_max_udp(int fd,char *buffer,int bufsize,int maxtime)
2078 struct timeval timeout;
2083 timeout.tv_sec = maxtime / 1000;
2084 timeout.tv_usec = (maxtime % 1000) * 1000;
2086 selrtn = sys_select(&fds,maxtime>0?&timeout:NULL);
2088 if (!FD_ISSET(fd,&fds))
2091 nread = read_udp_socket(fd, buffer, bufsize);
2093 /* return the number got */
2097 /*******************************************************************
2098 find the difference in milliseconds between two struct timeval
2100 ********************************************************************/
2101 int TvalDiff(struct timeval *tvalold,struct timeval *tvalnew)
2103 return((tvalnew->tv_sec - tvalold->tv_sec)*1000 +
2104 ((int)tvalnew->tv_usec - (int)tvalold->tv_usec)/1000);
2107 /****************************************************************************
2108 send a keepalive packet (rfc1002)
2109 ****************************************************************************/
2110 BOOL send_keepalive(int client)
2112 unsigned char buf[4];
2115 buf[1] = buf[2] = buf[3] = 0;
2117 return(write_data(client,(char *)buf,4) == 4);
2122 /****************************************************************************
2123 read data from the client, reading exactly N bytes.
2124 ****************************************************************************/
2125 int read_data(int fd,char *buffer,int N)
2134 ret = read(fd,buffer + total,N - total);
2136 smb_read_error = READ_EOF;
2140 smb_read_error = READ_ERROR;
2149 /****************************************************************************
2151 ****************************************************************************/
2152 int write_data(int fd,char *buffer,int N)
2159 ret = write(fd,buffer + total,N - total);
2161 if (ret == -1) return -1;
2162 if (ret == 0) return total;
2170 /****************************************************************************
2171 transfer some data between two fd's
2172 ****************************************************************************/
2173 int transfer_file(int infd,int outfd,int n,char *header,int headlen,int align)
2175 static char *buf=NULL;
2180 DEBUG(4,("transfer_file %d (head=%d) called\n",n,headlen));
2183 size = lp_readsize();
2184 size = MAX(size,1024);
2187 while (!buf && size>0) {
2188 buf = (char *)Realloc(buf,size+8);
2189 if (!buf) size /= 2;
2193 DEBUG(0,("Can't allocate transfer buffer!\n"));
2197 abuf = buf + (align%8);
2204 int s = MIN(n,size);
2209 if (header && (headlen >= MIN(s,1024))) {
2219 if (header && headlen > 0)
2221 ret = MIN(headlen,size);
2222 memcpy(buf1,header,ret);
2225 if (headlen <= 0) header = NULL;
2229 ret += read(infd,buf1+ret,s-ret);
2233 ret2 = (outfd>=0?write_data(outfd,buf1,ret):ret);
2234 if (ret2 > 0) total += ret2;
2235 /* if we can't write then dump excess data */
2237 transfer_file(infd,-1,n-(ret+headlen),NULL,0,0);
2239 if (ret <= 0 || ret2 != ret)
2247 /****************************************************************************
2248 read 4 bytes of a smb packet and return the smb length of the packet
2249 possibly store the result in the buffer
2250 ****************************************************************************/
2251 int read_smb_length(int fd,char *inbuf,int timeout)
2255 int len=0, msg_type;
2266 ok = (read_with_timeout(fd,buffer,4,4,timeout) == 4);
2268 ok = (read_data(fd,buffer,4) == 4);
2273 len = smb_len(buffer);
2274 msg_type = CVAL(buffer,0);
2276 if (msg_type == 0x85)
2278 DEBUG(5,("Got keepalive packet\n"));
2283 DEBUG(10,("got smb length of %d\n",len));
2290 /****************************************************************************
2291 read an smb from a fd. Note that the buffer *MUST* be of size
2292 BUFFER_SIZE+SAFETY_MARGIN.
2293 The timeout is in milli seconds
2294 ****************************************************************************/
2295 BOOL receive_smb(int fd,char *buffer, int timeout)
2301 bzero(buffer,smb_size + 100);
2303 len = read_smb_length(fd,buffer,timeout);
2307 if (len > BUFFER_SIZE) {
2308 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
2309 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
2313 ret = read_data(fd,buffer+4,len);
2315 smb_read_error = READ_ERROR;
2322 /****************************************************************************
2323 read a message from a udp fd.
2324 The timeout is in milli seconds
2325 ****************************************************************************/
2326 BOOL receive_local_message(int fd, char *buffer, int buffer_len, int timeout)
2328 struct sockaddr_in from;
2329 int fromlen = sizeof(from);
2341 to.tv_sec = timeout / 1000;
2342 to.tv_usec = (timeout % 1000) * 1000;
2344 selrtn = sys_select(&fds,&to);
2346 /* Check if error */
2349 /* something is wrong. Maybe the socket is dead? */
2350 smb_read_error = READ_ERROR;
2354 /* Did we timeout ? */
2357 smb_read_error = READ_TIMEOUT;
2363 * Read a loopback udp message.
2365 msg_len = recvfrom(fd, &buffer[UDP_CMD_HEADER_LEN],
2366 buffer_len - UDP_CMD_HEADER_LEN, 0,
2367 (struct sockaddr *)&from, &fromlen);
2371 DEBUG(0,("receive_local_message. Error in recvfrom. (%s).\n",strerror(errno)));
2375 /* Validate message length. */
2376 if(msg_len > (buffer_len - UDP_CMD_HEADER_LEN))
2378 DEBUG(0,("receive_local_message: invalid msg_len (%d) max can be %d\n",
2380 buffer_len - UDP_CMD_HEADER_LEN));
2384 /* Validate message from address (must be localhost). */
2385 if(from.sin_addr.s_addr != htonl(INADDR_LOOPBACK))
2387 DEBUG(0,("receive_local_message: invalid 'from' address \
2388 (was %x should be 127.0.0.1\n", from.sin_addr.s_addr));
2392 /* Setup the message header */
2393 SIVAL(buffer,UDP_CMD_LEN_OFFSET,msg_len);
2394 SSVAL(buffer,UDP_CMD_PORT_OFFSET,ntohs(from.sin_port));
2399 /****************************************************************************
2400 structure to hold a linked list of local udp messages.
2402 ****************************************************************************/
2404 typedef struct _udp_message_list {
2405 struct _udp_message_list *msg_next;
2410 static udp_message_list *udp_msg_head = NULL;
2412 /****************************************************************************
2413 Function to push a linked list of local udp messages ready
2415 ****************************************************************************/
2416 BOOL push_local_message(char *buf, int msg_len)
2418 udp_message_list *msg = (udp_message_list *)malloc(sizeof(udp_message_list));
2422 DEBUG(0,("push_local_message: malloc fail (1)\n"));
2426 msg->msg_buf = (char *)malloc(msg_len);
2427 if(msg->msg_buf == NULL)
2429 DEBUG(0,("push_local_message: malloc fail (2)\n"));
2434 memcpy(msg->msg_buf, buf, msg_len);
2435 msg->msg_len = msg_len;
2437 msg->msg_next = udp_msg_head;
2443 /****************************************************************************
2444 Do a select on an two fd's - with timeout.
2446 If a local udp message has been pushed onto the
2447 queue (this can only happen during oplock break
2448 processing) return this first.
2450 If the first smbfd is ready then read an smb from it.
2451 if the second (loopback UDP) fd is ready then read a message
2452 from it and setup the buffer header to identify the length
2454 Returns False on timeout or error.
2457 The timeout is in milli seconds
2458 ****************************************************************************/
2459 BOOL receive_message_or_smb(int smbfd, int oplock_fd,
2460 char *buffer, int buffer_len,
2461 int timeout, BOOL *got_smb)
2470 * Check to see if we already have a message on the udp queue.
2471 * If so - copy and return it.
2476 udp_message_list *msg = udp_msg_head;
2477 memcpy(buffer, msg->msg_buf, MIN(buffer_len, msg->msg_len));
2478 udp_msg_head = msg->msg_next;
2480 /* Free the message we just copied. */
2481 free((char *)msg->msg_buf);
2488 FD_SET(oplock_fd,&fds);
2490 to.tv_sec = timeout / 1000;
2491 to.tv_usec = (timeout % 1000) * 1000;
2493 selrtn = sys_select(&fds,timeout>0?&to:NULL);
2495 /* Check if error */
2497 /* something is wrong. Maybe the socket is dead? */
2498 smb_read_error = READ_ERROR;
2502 /* Did we timeout ? */
2504 smb_read_error = READ_TIMEOUT;
2508 if (FD_ISSET(smbfd,&fds))
2511 return receive_smb(smbfd, buffer, 0);
2515 return receive_local_message(oplock_fd, buffer, buffer_len, 0);
2519 /****************************************************************************
2521 ****************************************************************************/
2522 BOOL send_smb(int fd,char *buffer)
2526 len = smb_len(buffer) + 4;
2528 while (nwritten < len)
2530 ret = write_socket(fd,buffer+nwritten,len - nwritten);
2533 DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
2545 /****************************************************************************
2546 find a pointer to a netbios name
2547 ****************************************************************************/
2548 char *name_ptr(char *buf,int ofs)
2550 unsigned char c = *(unsigned char *)(buf+ofs);
2552 if ((c & 0xC0) == 0xC0)
2556 memcpy(p,buf+ofs,2);
2559 DEBUG(5,("name ptr to pos %d from %d is %s\n",l,ofs,buf+l));
2566 /****************************************************************************
2567 extract a netbios name from a buf
2568 ****************************************************************************/
2569 int name_extract(char *buf,int ofs,char *name)
2571 char *p = name_ptr(buf,ofs);
2572 int d = PTR_DIFF(p,buf+ofs);
2574 if (d < -50 || d > 50) return(0);
2575 return(name_interpret(p,name));
2578 /****************************************************************************
2579 return the total storage length of a mangled name
2580 ****************************************************************************/
2581 int name_len( char *s )
2585 /* If the two high bits of the byte are set, return 2. */
2586 if( 0xC0 == (*(unsigned char *)s & 0xC0) )
2589 /* Add up the length bytes. */
2590 for( len = 1; (*s); s += (*s) + 1 )
2598 /****************************************************************************
2599 send a single packet to a port on another machine
2600 ****************************************************************************/
2601 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
2605 struct sockaddr_in sock_out;
2610 /* create a socket to write to */
2611 out_fd = socket(AF_INET, type, 0);
2614 DEBUG(0,("socket failed"));
2618 /* set the address and port */
2619 bzero((char *)&sock_out,sizeof(sock_out));
2620 putip((char *)&sock_out.sin_addr,(char *)&ip);
2621 sock_out.sin_port = htons( port );
2622 sock_out.sin_family = AF_INET;
2625 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
2626 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
2629 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
2632 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
2633 inet_ntoa(ip),port,strerror(errno)));
2639 /*******************************************************************
2640 sleep for a specified number of milliseconds
2641 ********************************************************************/
2645 struct timeval tval,t1,t2;
2652 tval.tv_sec = (t-tdiff)/1000;
2653 tval.tv_usec = 1000*((t-tdiff)%1000);
2657 sys_select(&fds,&tval);
2660 tdiff = TvalDiff(&t1,&t2);
2664 /****************************************************************************
2665 check if a string is part of a list
2666 ****************************************************************************/
2667 BOOL in_list(char *s,char *list,BOOL casesensitive)
2672 if (!list) return(False);
2674 while (next_token(&p,tok,LIST_SEP))
2676 if (casesensitive) {
2677 if (strcmp(tok,s) == 0)
2680 if (StrCaseCmp(tok,s) == 0)
2687 /* this is used to prevent lots of mallocs of size 1 */
2688 static char *null_string = NULL;
2690 /****************************************************************************
2691 set a string value, allocing the space for the string
2692 ****************************************************************************/
2693 BOOL string_init(char **dest,char *src)
2704 null_string = (char *)malloc(1);
2707 *dest = null_string;
2711 (*dest) = (char *)malloc(l+1);
2712 if ((*dest) == NULL) {
2713 DEBUG(0,("Out of memory in string_init\n"));
2722 /****************************************************************************
2724 ****************************************************************************/
2725 void string_free(char **s)
2727 if (!s || !(*s)) return;
2728 if (*s == null_string)
2734 /****************************************************************************
2735 set a string value, allocing the space for the string, and deallocating any
2737 ****************************************************************************/
2738 BOOL string_set(char **dest,char *src)
2742 return(string_init(dest,src));
2745 /****************************************************************************
2746 substitute a string for a pattern in another string. Make sure there is
2749 This routine looks for pattern in s and replaces it with
2750 insert. It may do multiple replacements.
2752 return True if a substitution was done.
2753 ****************************************************************************/
2754 BOOL string_sub(char *s,char *pattern,char *insert)
2760 if (!insert || !pattern || !s) return(False);
2763 lp = strlen(pattern);
2764 li = strlen(insert);
2766 if (!*pattern) return(False);
2768 while (lp <= ls && (p = strstr(s,pattern)))
2771 memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
2772 memcpy(p,insert,li);
2781 /*********************************************************
2782 * Recursive routine that is called by mask_match.
2783 * Does the actual matching.
2784 *********************************************************/
2785 BOOL do_match(char *str, char *regexp, int case_sig)
2789 for( p = regexp; *p && *str; ) {
2796 /* Look for a character matching
2797 the one after the '*' */
2800 return True; /* Automatic match */
2802 while(*str && (case_sig ? (*p != *str) : (toupper(*p)!=toupper(*str))))
2804 if(do_match(str,p,case_sig))
2818 if(toupper(*str) != toupper(*p))
2828 if (!*p && str[0] == '.' && str[1] == 0)
2831 if (!*str && *p == '?')
2833 while (*p == '?') p++;
2837 if(!*str && (*p == '*' && p[1] == '\0'))
2843 /*********************************************************
2844 * Routine to match a given string with a regexp - uses
2845 * simplified regexp that takes * and ? only. Case can be
2846 * significant or not.
2847 *********************************************************/
2848 BOOL mask_match(char *str, char *regexp, int case_sig,BOOL trans2)
2852 fstring ebase,eext,sbase,sext;
2856 /* Make local copies of str and regexp */
2857 StrnCpy(p1,regexp,sizeof(pstring)-1);
2858 StrnCpy(p2,str,sizeof(pstring)-1);
2860 if (!strchr(p2,'.')) {
2865 if (!strchr(p1,'.')) {
2873 string_sub(p1,"*.*","*");
2874 string_sub(p1,".*","*");
2878 /* Remove any *? and ** as they are meaningless */
2879 for(p = p1; *p; p++)
2880 while( *p == '*' && (p[1] == '?' ||p[1] == '*'))
2881 (void)strcpy( &p[1], &p[2]);
2883 if (strequal(p1,"*")) return(True);
2885 DEBUG(8,("mask_match str=<%s> regexp=<%s>, case_sig = %d\n", p2, p1, case_sig));
2891 if ((p=strrchr(p1,'.'))) {
2900 if (!strequal(p2,".") && !strequal(p2,"..") && (p=strrchr(p2,'.'))) {
2910 matched = do_match(sbase,ebase,case_sig) &&
2911 (trans2 || do_match(sext,eext,case_sig));
2913 DEBUG(8,("mask_match returning %d\n", matched));
2920 /****************************************************************************
2921 become a daemon, discarding the controlling terminal
2922 ****************************************************************************/
2923 void become_daemon(void)
2925 #ifndef NO_FORK_DEBUG
2929 /* detach from the terminal */
2932 #else /* USE_SETSID */
2935 int i = open("/dev/tty", O_RDWR);
2938 ioctl(i, (int) TIOCNOTTY, (char *)0);
2942 #endif /* TIOCNOTTY */
2943 #endif /* USE_SETSID */
2944 /* Close fd's 0,1,2. Needed if started by rsh */
2946 #endif /* NO_FORK_DEBUG */
2950 /****************************************************************************
2951 put up a yes/no prompt
2952 ****************************************************************************/
2958 if (!fgets(ans,sizeof(ans)-1,stdin))
2961 if (*ans == 'y' || *ans == 'Y')
2967 /****************************************************************************
2968 read a line from a file with possible \ continuation chars.
2969 Blanks at the start or end of a line are stripped.
2970 The string will be allocated if s2 is NULL
2971 ****************************************************************************/
2972 char *fgets_slash(char *s2,int maxlen,FILE *f)
2977 BOOL start_of_line = True;
2984 maxlen = MIN(maxlen,8);
2985 s = (char *)Realloc(s,maxlen);
2988 if (!s || maxlen < 2) return(NULL);
2992 while (len < maxlen-1)
3000 while (len > 0 && s[len-1] == ' ')
3004 if (len > 0 && s[len-1] == '\\')
3007 start_of_line = True;
3012 if (len <= 0 && !s2)
3014 return(len>0?s:NULL);
3019 start_of_line = False;
3023 if (!s2 && len > maxlen-3)
3026 s = (char *)Realloc(s,maxlen);
3027 if (!s) return(NULL);
3035 /****************************************************************************
3036 set the length of a file from a filedescriptor.
3037 Returns 0 on success, -1 on failure.
3038 ****************************************************************************/
3039 int set_filelen(int fd, long len)
3041 /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
3042 extend a file with ftruncate. Provide alternate implementation
3045 #if FTRUNCATE_CAN_EXTEND
3046 return ftruncate(fd, len);
3050 long currpos = lseek(fd, 0L, SEEK_CUR);
3054 /* Do an fstat to see if the file is longer than
3055 the requested size (call ftruncate),
3056 or shorter, in which case seek to len - 1 and write 1
3058 if(fstat(fd, &st)<0)
3062 if (S_ISFIFO(st.st_mode)) return 0;
3065 if(st.st_size == len)
3067 if(st.st_size > len)
3068 return ftruncate(fd, len);
3070 if(lseek(fd, len-1, SEEK_SET) != len -1)
3072 if(write(fd, &c, 1)!=1)
3074 /* Seek to where we were */
3075 lseek(fd, currpos, SEEK_SET);
3081 /****************************************************************************
3082 return the byte checksum of some data
3083 ****************************************************************************/
3084 int byte_checksum(char *buf,int len)
3086 unsigned char *p = (unsigned char *)buf;
3096 /****************************************************************************
3097 this is a version of setbuffer() for those machines that only have setvbuf
3098 ****************************************************************************/
3099 void setbuffer(FILE *f,char *buf,int bufsize)
3101 setvbuf(f,buf,_IOFBF,bufsize);
3106 /****************************************************************************
3107 parse out a directory name from a path name. Assumes dos style filenames.
3108 ****************************************************************************/
3109 char *dirname_dos(char *path,char *buf)
3111 char *p = strrchr(path,'\\');
3126 /****************************************************************************
3127 parse out a filename from a path name. Assumes dos style filenames.
3128 ****************************************************************************/
3129 static char *filename_dos(char *path,char *buf)
3131 char *p = strrchr(path,'\\');
3143 /****************************************************************************
3144 expand a pointer to be a particular size
3145 ****************************************************************************/
3146 void *Realloc(void *p,int size)
3152 DEBUG(5,("Realloc asked for 0 bytes\n"));
3157 ret = (void *)malloc(size);
3159 ret = (void *)realloc(p,size);
3162 DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",size));
3168 /****************************************************************************
3170 ****************************************************************************/
3171 char *strdup(char *s)
3174 if (!s) return(NULL);
3175 ret = (char *)malloc(strlen(s)+1);
3176 if (!ret) return(NULL);
3183 /****************************************************************************
3184 Signal handler for SIGPIPE (write on a disconnected socket)
3185 ****************************************************************************/
3188 DEBUG(0,("Probably got SIGPIPE\nExiting\n"));
3192 /****************************************************************************
3193 get my own name and IP
3194 ****************************************************************************/
3195 BOOL get_myname(char *my_name,struct in_addr *ip)
3202 /* get my host name */
3203 if (gethostname(hostname, MAXHOSTNAMELEN) == -1)
3205 DEBUG(0,("gethostname failed\n"));
3210 if ((hp = Get_Hostbyname(hostname)) == 0)
3212 DEBUG(0,( "Get_Hostbyname: Unknown host %s.\n",hostname));
3218 /* split off any parts after an initial . */
3219 char *p = strchr(hostname,'.');
3222 fstrcpy(my_name,hostname);
3226 putip((char *)ip,(char *)hp->h_addr);
3232 /****************************************************************************
3233 true if two IP addresses are equal
3234 ****************************************************************************/
3235 BOOL ip_equal(struct in_addr ip1,struct in_addr ip2)
3238 a1 = ntohl(ip1.s_addr);
3239 a2 = ntohl(ip2.s_addr);
3244 /****************************************************************************
3245 open a socket of the specified type, port and address for incoming data
3246 ****************************************************************************/
3247 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
3250 struct sockaddr_in sock;
3254 /* get my host name */
3255 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
3256 { DEBUG(0,("gethostname failed\n")); return -1; }
3259 if ((hp = Get_Hostbyname(host_name)) == 0)
3261 DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",host_name));
3265 bzero((char *)&sock,sizeof(sock));
3266 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
3267 #if defined(__FreeBSD__) || defined(NETBSD) /* XXX not the right ifdef */
3268 sock.sin_len = sizeof(sock);
3270 sock.sin_port = htons( port );
3271 sock.sin_family = hp->h_addrtype;
3272 sock.sin_addr.s_addr = socket_addr;
3273 res = socket(hp->h_addrtype, type, 0);
3275 { DEBUG(0,("socket failed\n")); return -1; }
3279 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
3282 /* now we've got a socket - we need to bind it */
3283 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
3286 if (port == SMB_PORT || port == NMB_PORT)
3287 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
3288 port,inet_ntoa(sock.sin_addr),strerror(errno)));
3291 if (dlevel > 0 && port < 1000)
3294 if (port >= 1000 && port < 9000)
3295 return(open_socket_in(type,port+1,dlevel,socket_addr));
3300 DEBUG(3,("bind succeeded on port %d\n",port));
3306 /****************************************************************************
3307 create an outgoing socket
3308 **************************************************************************/
3309 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
3311 struct sockaddr_in sock_out;
3313 int connect_loop = 250; /* 250 milliseconds */
3314 int loops = (timeout * 1000) / connect_loop;
3316 /* create a socket to write to */
3317 res = socket(PF_INET, type, 0);
3319 { DEBUG(0,("socket error\n")); return -1; }
3321 if (type != SOCK_STREAM) return(res);
3323 bzero((char *)&sock_out,sizeof(sock_out));
3324 putip((char *)&sock_out.sin_addr,(char *)addr);
3326 sock_out.sin_port = htons( port );
3327 sock_out.sin_family = PF_INET;
3329 /* set it non-blocking */
3330 set_blocking(res,False);
3332 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
3334 /* and connect it to the destination */
3336 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
3338 /* Some systems return EAGAIN when they mean EINPROGRESS */
3339 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
3340 errno == EAGAIN) && loops--) {
3341 msleep(connect_loop);
3345 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
3347 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
3353 if (ret < 0 && errno == EISCONN) {
3360 DEBUG(1,("error connecting to %s:%d (%s)\n",
3361 inet_ntoa(*addr),port,strerror(errno)));
3365 /* set it blocking again */
3366 set_blocking(res,True);
3372 /****************************************************************************
3373 interpret a protocol description string, with a default
3374 ****************************************************************************/
3375 int interpret_protocol(char *str,int def)
3377 if (strequal(str,"NT1"))
3378 return(PROTOCOL_NT1);
3379 if (strequal(str,"LANMAN2"))
3380 return(PROTOCOL_LANMAN2);
3381 if (strequal(str,"LANMAN1"))
3382 return(PROTOCOL_LANMAN1);
3383 if (strequal(str,"CORE"))
3384 return(PROTOCOL_CORE);
3385 if (strequal(str,"COREPLUS"))
3386 return(PROTOCOL_COREPLUS);
3387 if (strequal(str,"CORE+"))
3388 return(PROTOCOL_COREPLUS);
3390 DEBUG(0,("Unrecognised protocol level %s\n",str));
3395 /****************************************************************************
3396 interpret a security level
3397 ****************************************************************************/
3398 int interpret_security(char *str,int def)
3400 if (strequal(str,"SERVER"))
3402 if (strequal(str,"USER"))
3404 if (strequal(str,"SHARE"))
3407 DEBUG(0,("Unrecognised security level %s\n",str));
3413 /****************************************************************************
3414 interpret an internet address or name into an IP address in 4 byte form
3415 ****************************************************************************/
3416 uint32 interpret_addr(char *str)
3421 BOOL pure_address = True;
3423 if (strcmp(str,"0.0.0.0") == 0) return(0);
3424 if (strcmp(str,"255.255.255.255") == 0) return(0xFFFFFFFF);
3426 for (i=0; pure_address && str[i]; i++)
3427 if (!(isdigit(str[i]) || str[i] == '.'))
3428 pure_address = False;
3430 /* if it's in the form of an IP address then get the lib to interpret it */
3432 res = inet_addr(str);
3434 /* otherwise assume it's a network name of some sort and use
3436 if ((hp = Get_Hostbyname(str)) == 0) {
3437 DEBUG(3,("Get_Hostbyname: Unknown host. %s\n",str));
3440 if(hp->h_addr == NULL) {
3441 DEBUG(3,("Get_Hostbyname: host address is invalid for host %s.\n",str));
3444 putip((char *)&res,(char *)hp->h_addr);
3447 if (res == (uint32)-1) return(0);
3452 /*******************************************************************
3453 a convenient addition to interpret_addr()
3454 ******************************************************************/
3455 struct in_addr *interpret_addr2(char *str)
3457 static struct in_addr ret;
3458 uint32 a = interpret_addr(str);
3463 /*******************************************************************
3464 check if an IP is the 0.0.0.0
3465 ******************************************************************/
3466 BOOL zero_ip(struct in_addr ip)
3469 putip((char *)&a,(char *)&ip);
3474 /*******************************************************************
3475 matchname - determine if host name matches IP address
3476 ******************************************************************/
3477 static BOOL matchname(char *remotehost,struct in_addr addr)
3482 if ((hp = Get_Hostbyname(remotehost)) == 0) {
3483 DEBUG(0,("Get_Hostbyname(%s): lookup failure", remotehost));
3488 * Make sure that gethostbyname() returns the "correct" host name.
3489 * Unfortunately, gethostbyname("localhost") sometimes yields
3490 * "localhost.domain". Since the latter host name comes from the
3491 * local DNS, we just have to trust it (all bets are off if the local
3492 * DNS is perverted). We always check the address list, though.
3495 if (strcasecmp(remotehost, hp->h_name)
3496 && strcasecmp(remotehost, "localhost")) {
3497 DEBUG(0,("host name/name mismatch: %s != %s",
3498 remotehost, hp->h_name));
3502 /* Look up the host address in the address list we just got. */
3503 for (i = 0; hp->h_addr_list[i]; i++) {
3504 if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
3509 * The host name does not map to the original host address. Perhaps
3510 * someone has compromised a name server. More likely someone botched
3511 * it, but that could be dangerous, too.
3514 DEBUG(0,("host name/address mismatch: %s != %s",
3515 inet_ntoa(addr), hp->h_name));
3519 /*******************************************************************
3520 Reset the 'done' variables so after a client process is created
3521 from a fork call these calls will be re-done. This should be
3522 expanded if more variables need reseting.
3523 ******************************************************************/
3525 static BOOL global_client_name_done = False;
3526 static BOOL global_client_addr_done = False;
3528 void reset_globals_after_fork()
3530 global_client_name_done = False;
3531 global_client_addr_done = False;
3534 /*******************************************************************
3535 return the DNS name of the client
3536 ******************************************************************/
3537 char *client_name(void)
3541 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
3542 int length = sizeof(sa);
3543 static pstring name_buf;
3546 if (global_client_name_done)
3549 strcpy(name_buf,"UNKNOWN");
3551 if (getpeername(Client, &sa, &length) < 0) {
3552 DEBUG(0,("getpeername failed\n"));
3556 /* Look up the remote host name. */
3557 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
3558 sizeof(sockin->sin_addr),
3560 DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr()));
3561 StrnCpy(name_buf,client_addr(),sizeof(name_buf) - 1);
3563 StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
3564 if (!matchname(name_buf, sockin->sin_addr)) {
3565 DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr()));
3566 strcpy(name_buf,"UNKNOWN");
3569 global_client_name_done = True;
3573 /*******************************************************************
3574 return the IP addr of the client as a string
3575 ******************************************************************/
3576 char *client_addr(void)
3580 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
3581 int length = sizeof(sa);
3582 static fstring addr_buf;
3584 if (global_client_addr_done)
3587 strcpy(addr_buf,"0.0.0.0");
3589 if (getpeername(Client, &sa, &length) < 0) {
3590 DEBUG(0,("getpeername failed\n"));
3594 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
3596 global_client_addr_done = True;
3600 /*******************************************************************
3601 sub strings with useful parameters
3602 Rewritten by Stefaan A Eeckels <Stefaan.Eeckels@ecc.lu> and
3603 Paul Rippin <pr3245@nopc.eurostat.cec.be>
3604 ********************************************************************/
3605 void standard_sub_basic(char *str)
3609 struct passwd *pass;
3611 for (s = str ; (p = strchr(s,'%')) != NULL ; s = p )
3615 case 'G' : if ((pass = Get_Pwnam(sesssetup_user,False))!=NULL)
3616 string_sub(p,"%G",gidtoname(pass->pw_gid));
3620 case 'I' : string_sub(p,"%I",client_addr()); break;
3621 case 'L' : string_sub(p,"%L",local_machine); break;
3622 case 'M' : string_sub(p,"%M",client_name()); break;
3623 case 'R' : string_sub(p,"%R",remote_proto); break;
3624 case 'T' : string_sub(p,"%T",timestring()); break;
3625 case 'U' : string_sub(p,"%U",sesssetup_user); break;
3626 case 'a' : string_sub(p,"%a",remote_arch); break;
3627 case 'd' : sprintf(pidstr,"%d",(int)getpid());
3628 string_sub(p,"%d",pidstr);
3630 case 'h' : string_sub(p,"%h",myhostname); break;
3631 case 'm' : string_sub(p,"%m",remote_machine); break;
3632 case 'v' : string_sub(p,"%v",VERSION); break;
3633 case '\0' : p++; break; /* don't run off end if last character is % */
3634 default : p+=2; break;
3640 /*******************************************************************
3641 are two IPs on the same subnet?
3642 ********************************************************************/
3643 BOOL same_net(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
3645 uint32 net1,net2,nmask;
3647 nmask = ntohl(mask.s_addr);
3648 net1 = ntohl(ip1.s_addr);
3649 net2 = ntohl(ip2.s_addr);
3651 return((net1 & nmask) == (net2 & nmask));
3655 /*******************************************************************
3656 write a string in unicoode format
3657 ********************************************************************/
3658 int PutUniCode(char *dst,char *src)
3662 dst[ret++] = src[0];
3671 /****************************************************************************
3672 a wrapper for gethostbyname() that tries with all lower and all upper case
3673 if the initial name fails
3674 ****************************************************************************/
3675 struct hostent *Get_Hostbyname(char *name)
3677 char *name2 = strdup(name);
3678 struct hostent *ret;
3682 DEBUG(0,("Memory allocation error in Get_Hostbyname! panic\n"));
3686 if (!isalnum(*name2))
3692 ret = sys_gethostbyname(name2);
3699 /* try with all lowercase */
3701 ret = sys_gethostbyname(name2);
3708 /* try with all uppercase */
3710 ret = sys_gethostbyname(name2);
3717 /* nothing works :-( */
3723 /****************************************************************************
3724 check if a process exists. Does this work on all unixes?
3725 ****************************************************************************/
3726 BOOL process_exists(int pid)
3730 sprintf(s,"/proc/%d",pid);
3731 return(directory_exist(s,NULL));
3734 static BOOL tested=False;
3735 static BOOL ok=False;
3739 sprintf(s,"/proc/%05d",(int)getpid());
3740 ok = file_exist(s,NULL);
3743 sprintf(s,"/proc/%05d",pid);
3744 return(file_exist(s,NULL));
3748 /* CGH 8/16/96 - added ESRCH test */
3749 return(pid == getpid() || kill(pid,0) == 0 || errno != ESRCH);
3754 /*******************************************************************
3755 turn a uid into a user name
3756 ********************************************************************/
3757 char *uidtoname(int uid)
3759 static char name[40];
3760 struct passwd *pass = getpwuid(uid);
3761 if (pass) return(pass->pw_name);
3762 sprintf(name,"%d",uid);
3766 /*******************************************************************
3767 turn a gid into a group name
3768 ********************************************************************/
3769 char *gidtoname(int gid)
3771 static char name[40];
3772 struct group *grp = getgrgid(gid);
3773 if (grp) return(grp->gr_name);
3774 sprintf(name,"%d",gid);
3778 /*******************************************************************
3780 ********************************************************************/
3781 void BlockSignals(BOOL block,int signum)
3784 int block_mask = sigmask(signum);
3785 static int oldmask = 0;
3787 oldmask = sigblock(block_mask);
3789 sigsetmask(oldmask);
3790 #elif defined(USE_SIGPROCMASK)
3793 sigaddset(&set,signum);
3794 sigprocmask(block?SIG_BLOCK:SIG_UNBLOCK,&set,NULL);
3799 /*******************************************************************
3800 my own panic function - not suitable for general use
3801 ********************************************************************/
3802 void ajt_panic(void)
3804 system("/usr/bin/X11/xedit -display ljus:0 /tmp/ERROR_FAULT");
3809 #define DIRECT direct
3811 #define DIRECT dirent
3814 /*******************************************************************
3815 a readdir wrapper which just returns the file name
3816 also return the inode number if requested
3817 ********************************************************************/
3818 char *readdirname(void *p)
3823 if (!p) return(NULL);
3825 ptr = (struct DIRECT *)readdir(p);
3826 if (!ptr) return(NULL);
3828 dname = ptr->d_name;
3831 if (telldir(p) < 0) return(NULL);
3835 /* this handles a broken compiler setup, causing a mixture
3836 of BSD and SYSV headers and libraries */
3838 static BOOL broken_readdir = False;
3839 if (!broken_readdir && !(*(dname)) && strequal("..",dname-2))
3841 DEBUG(0,("Your readdir() is broken. You have somehow mixed SYSV and BSD headers and libraries\n"));
3842 broken_readdir = True;
3851 pstrcpy(buf, dname);
3852 unix_to_dos(buf, True);
3859 /*******************************************************************
3860 Utility function used to decide if the last component
3861 of a path matches a (possibly wildcarded) entry in a namelist.
3862 ********************************************************************/
3864 BOOL is_in_path(char *name, name_compare_entry *namelist)
3866 pstring last_component;
3869 DEBUG(8, ("is_in_path: %s\n", name));
3871 /* if we have no list it's obviously not in the path */
3872 if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL)))
3874 DEBUG(8,("is_in_path: no name list.\n"));
3878 /* Get the last component of the unix name. */
3879 p = strrchr(name, '/');
3880 strncpy(last_component, p ? p : name, sizeof(last_component)-1);
3881 last_component[sizeof(last_component)-1] = '\0';
3883 for(; namelist->name != NULL; namelist++)
3885 if(namelist->is_wild)
3887 /* look for a wildcard match. */
3888 if (mask_match(last_component, namelist->name, case_sensitive, False))
3890 DEBUG(8,("is_in_path: mask match succeeded\n"));
3896 if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
3897 (!case_sensitive && (StrCaseCmp(last_component, namelist->name) == 0)))
3899 DEBUG(8,("is_in_path: match succeeded\n"));
3904 DEBUG(8,("is_in_path: match not found\n"));
3909 /*******************************************************************
3910 Strip a '/' separated list into an array of
3911 name_compare_enties structures suitable for
3912 passing to is_in_path(). We do this for
3913 speed so we can pre-parse all the names in the list
3914 and don't do it for each call to is_in_path().
3915 namelist is modified here and is assumed to be
3916 a copy owned by the caller.
3917 We also check if the entry contains a wildcard to
3918 remove a potentially expensive call to mask_match
3920 ********************************************************************/
3922 void set_namearray(name_compare_entry **ppname_array, char *namelist)
3925 char *nameptr = namelist;
3926 int num_entries = 0;
3929 (*ppname_array) = NULL;
3931 if((nameptr == NULL ) || ((nameptr != NULL) && (*nameptr == '\0')))
3934 /* We need to make two passes over the string. The
3935 first to count the number of elements, the second
3940 if ( *nameptr == '/' )
3942 /* cope with multiple (useless) /s) */
3946 /* find the next / */
3947 name_end = strchr(nameptr, '/');
3949 /* oops - the last check for a / didn't find one. */
3950 if (name_end == NULL)
3953 /* next segment please */
3954 nameptr = name_end + 1;
3958 if(num_entries == 0)
3961 if(( (*ppname_array) = (name_compare_entry *)malloc(
3962 (num_entries + 1) * sizeof(name_compare_entry))) == NULL)
3964 DEBUG(0,("set_namearray: malloc fail\n"));
3968 /* Now copy out the names */
3973 if ( *nameptr == '/' )
3975 /* cope with multiple (useless) /s) */
3979 /* find the next / */
3980 if ((name_end = strchr(nameptr, '/')) != NULL)
3985 /* oops - the last check for a / didn't find one. */
3986 if(name_end == NULL)
3989 (*ppname_array)[i].is_wild = ((strchr( nameptr, '?')!=NULL) ||
3990 (strchr( nameptr, '*')!=NULL));
3991 if(((*ppname_array)[i].name = strdup(nameptr)) == NULL)
3993 DEBUG(0,("set_namearray: malloc fail (1)\n"));
3997 /* next segment please */
3998 nameptr = name_end + 1;
4002 (*ppname_array)[i].name = NULL;
4007 /****************************************************************************
4008 routine to free a namearray.
4009 ****************************************************************************/
4011 void free_namearray(name_compare_entry *name_array)
4016 if(name_array->name != NULL)
4017 free(name_array->name);
4019 free((char *)name_array);
4022 /****************************************************************************
4023 routine to do file locking
4024 ****************************************************************************/
4025 BOOL fcntl_lock(int fd,int op,uint32 offset,uint32 count,int type)
4032 uint32 mask = 0xC0000000;
4034 /* make sure the count is reasonable, we might kill the lockd otherwise */
4037 /* the offset is often strange - remove 2 of its bits if either of
4038 the top two bits are set. Shift the top ones by two bits. This
4039 still allows OLE2 apps to operate, but should stop lockd from
4041 if ((offset & mask) != 0)
4042 offset = (offset & ~mask) | ((offset & mask) >> 2);
4044 uint32 mask = ((unsigned)1<<31);
4046 /* interpret negative counts as large numbers */
4050 /* no negative offsets */
4053 /* count + offset must be in range */
4054 while ((offset < 0 || (offset + count < 0)) && mask)
4062 DEBUG(8,("fcntl_lock %d %d %d %d %d\n",fd,op,(int)offset,(int)count,type));
4065 lock.l_whence = SEEK_SET;
4066 lock.l_start = (int)offset;
4067 lock.l_len = (int)count;
4072 ret = fcntl(fd,op,&lock);
4075 DEBUG(3,("fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
4081 (lock.l_type != F_UNLCK) &&
4082 (lock.l_pid != 0) &&
4083 (lock.l_pid != getpid()))
4085 DEBUG(3,("fd %d is locked by pid %d\n",fd,lock.l_pid));
4089 /* it must be not locked or locked by me */
4093 /* a lock set or unset */
4096 DEBUG(3,("lock failed at offset %d count %d op %d type %d (%s)\n",
4097 offset,count,op,type,strerror(errno)));
4099 /* perhaps it doesn't support this sort of locking?? */
4100 if (errno == EINVAL)
4102 DEBUG(3,("locking not supported? returning True\n"));
4109 /* everything went OK */
4110 DEBUG(8,("Lock call successful\n"));
4118 /*******************************************************************
4119 lock a file - returning a open file descriptor or -1 on failure
4120 The timeout is in seconds. 0 means no timeout
4121 ********************************************************************/
4122 int file_lock(char *name,int timeout)
4124 int fd = open(name,O_RDWR|O_CREAT,0666);
4126 if (fd < 0) return(-1);
4129 if (timeout) t = time(NULL);
4130 while (!timeout || (time(NULL)-t < timeout)) {
4131 if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)) return(fd);
4132 msleep(LOCK_RETRY_TIMEOUT);
4140 /*******************************************************************
4141 unlock a file locked by file_lock
4142 ********************************************************************/
4143 void file_unlock(int fd)
4147 fcntl_lock(fd,F_SETLK,0,1,F_UNLCK);
4152 /*******************************************************************
4153 is the name specified one of my netbios names
4154 returns true is it is equal, false otherwise
4155 ********************************************************************/
4156 BOOL is_myname(char *s)
4161 for (n=0; my_netbios_names[n]; n++) {
4162 if (strequal(my_netbios_names[n], s))
4165 DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret));
4169 /*******************************************************************
4170 set the horrid remote_arch string based on an enum.
4171 ********************************************************************/
4172 void set_remote_arch(enum remote_arch_types type)
4178 strcpy(remote_arch, "WfWg");
4181 strcpy(remote_arch, "OS2");
4184 strcpy(remote_arch, "Win95");
4187 strcpy(remote_arch, "WinNT");
4190 strcpy(remote_arch,"Samba");
4193 ra_type = RA_UNKNOWN;
4194 strcpy(remote_arch, "UNKNOWN");
4199 /*******************************************************************
4200 Get the remote_arch type.
4201 ********************************************************************/
4202 enum remote_arch_types get_remote_arch()
4208 /*******************************************************************
4209 skip past some unicode strings in a buffer
4210 ********************************************************************/
4211 char *skip_unicode_string(char *buf,int n)
4222 /*******************************************************************
4223 Return a ascii version of a unicode string
4224 Hack alert: uses fixed buffer(s) and only handles ascii strings
4225 ********************************************************************/
4227 char *unistr2(uint16 *buf)
4229 static char lbufs[8][MAXUNI];
4231 char *lbuf = lbufs[nexti];
4233 nexti = (nexti+1)%8;
4234 for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf++)
4242 /*******************************************************************
4243 Return a ascii version of a unicode string
4244 Hack alert: uses fixed buffer(s) and only handles ascii strings
4245 ********************************************************************/
4247 char *unistr(char *buf)
4249 static char lbufs[8][MAXUNI];
4251 char *lbuf = lbufs[nexti];
4254 nexti = (nexti+1)%8;
4256 for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf += 2)
4264 /*******************************************************************
4265 strncpy for unicode strings
4266 ********************************************************************/
4267 int unistrncpy(char *dst, char *src, int len)
4271 while (*src && len > 0)
4285 /*******************************************************************
4286 strcpy for unicode strings. returns length (in num of wide chars)
4287 ********************************************************************/
4288 int unistrcpy(char *dst, char *src)
4305 /*******************************************************************
4306 safe string copy into a fstring
4307 ********************************************************************/
4308 void fstrcpy(char *dest, char *src)
4310 int maxlength = sizeof(fstring) - 1;
4312 DEBUG(0,("ERROR: NULL dest in fstrcpy\n"));
4321 while (maxlength-- && *src)
4325 DEBUG(0,("ERROR: string overflow by %d in fstrcpy\n",
4330 /*******************************************************************
4331 safe string copy into a pstring
4332 ********************************************************************/
4333 void pstrcpy(char *dest, char *src)
4335 int maxlength = sizeof(pstring) - 1;
4337 DEBUG(0,("ERROR: NULL dest in pstrcpy\n"));
4346 while (maxlength-- && *src)
4350 DEBUG(0,("ERROR: string overflow by %d in pstrcpy\n",
4356 /*******************************************************************
4357 align a pointer to a multiple of 4 bytes
4358 ********************************************************************/
4359 char *align4(char *q, char *base)
4363 q += 4 - ((q - base) & 3);
4368 /*******************************************************************
4369 align a pointer to a multiple of 2 bytes
4370 ********************************************************************/
4371 char *align2(char *q, char *base)
4380 /*******************************************************************
4381 align a pointer to a multiple of align_offset bytes. looks like it
4382 will work for offsets of 0, 2 and 4...
4383 ********************************************************************/
4384 char *align_offset(char *q, char *base, int align_offset_len)
4386 int mod = ((q - base) & (align_offset_len-1));
4387 if (align_offset_len != 0 && mod != 0)
4389 q += align_offset_len - mod;
4394 static void print_asc(int level, unsigned char *buf,int len)
4398 DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.'));
4401 void dump_data(int level,unsigned char *buf,int len)
4406 DEBUG(level,("[%03X] ",i));
4408 DEBUG(level,("%02X ",(int)buf[i]));
4410 if (i%8 == 0) DEBUG(level,(" "));
4412 print_asc(level,&buf[i-16],8); DEBUG(level,(" "));
4413 print_asc(level,&buf[i-8],8); DEBUG(level,("\n"));
4414 if (i<len) DEBUG(level,("[%03X] ",i));
4422 if (n>8) DEBUG(level,(" "));
4423 while (n--) DEBUG(level,(" "));
4426 print_asc(level,&buf[i-(i%16)],n); DEBUG(level,(" "));
4428 if (n>0) print_asc(level,&buf[i-n],n);
4429 DEBUG(level,("\n"));
4433 char *tab_depth(int depth)
4435 static pstring spaces;
4436 memset(spaces, ' ', depth * 4);
4437 spaces[depth * 4] = 0;