home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Networking / SambaManager / samba-1.9.17p4 / source / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-30  |  97.5 KB  |  4,110 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    Samba utility functions
  5.    Copyright (C) Andrew Tridgell 1992-1997
  6.    
  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.
  11.    
  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.
  16.    
  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.
  20. */
  21.  
  22. #include "includes.h"
  23.  
  24. pstring scope = "";
  25.  
  26. int DEBUGLEVEL = 1;
  27.  
  28. BOOL passive = False;
  29.  
  30. int Protocol = PROTOCOL_COREPLUS;
  31.  
  32. /* a default finfo structure to ensure all fields are sensible */
  33. file_info def_finfo = {-1,0,0,0,0,0,0,""};
  34.  
  35. /* these are some file handles where debug info will be stored */
  36. FILE *dbf = NULL;
  37.  
  38. /* the client file descriptor */
  39. int Client = -1;
  40.  
  41. /* the last IP received from */
  42. struct in_addr lastip;
  43.  
  44. /* the last port received from */
  45. int lastport=0;
  46.  
  47. /* this is used by the chaining code */
  48. int chain_size = 0;
  49.  
  50. int trans_num = 0;
  51.  
  52. /*
  53.    case handling on filenames 
  54. */
  55. int case_default = CASE_LOWER;
  56.  
  57. pstring debugf = "";
  58. int syslog_level;
  59.  
  60. /* the following control case operations - they are put here so the
  61.    client can link easily */
  62. BOOL case_sensitive;
  63. BOOL case_preserve;
  64. BOOL use_mangled_map = False;
  65. BOOL short_case_preserve;
  66. BOOL case_mangle;
  67.  
  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="";
  76. pstring myname = "";
  77. fstring myworkgroup = "";
  78. char **my_netbios_names;
  79.  
  80. int smb_read_error = 0;
  81.  
  82. static BOOL stdout_logging = False;
  83.  
  84. static char *filename_dos(char *path,char *buf);
  85.  
  86. /*******************************************************************
  87.   get ready for syslog stuff
  88.   ******************************************************************/
  89. void setup_logging(char *pname,BOOL interactive)
  90. {
  91. #ifdef SYSLOG
  92.   if (!interactive) {
  93.     char *p = strrchr(pname,'/');
  94.     if (p) pname = p+1;
  95. #ifdef LOG_DAEMON
  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 */
  100.   }
  101. #endif
  102.   if (interactive) {
  103.     stdout_logging = True;
  104.     dbf = stdout;
  105.   }
  106. }
  107.  
  108.  
  109. BOOL append_log=False;
  110.  
  111.  
  112. /****************************************************************************
  113. reopen the log files
  114. ****************************************************************************/
  115. void reopen_logs(void)
  116. {
  117.   extern FILE *dbf;
  118.   pstring fname;
  119.   
  120.   if (DEBUGLEVEL > 0)
  121.     {
  122.       strcpy(fname,debugf);
  123.       if (lp_loaded() && (*lp_logfile()))
  124.     strcpy(fname,lp_logfile());
  125.  
  126.       if (!strcsequal(fname,debugf) || !dbf || !file_exist(debugf,NULL))
  127.     {
  128.       int oldumask = umask(022);
  129.       strcpy(debugf,fname);
  130.       if (dbf) fclose(dbf);
  131.       if (append_log)
  132.         dbf = fopen(debugf,"a");
  133.       else
  134.         dbf = fopen(debugf,"w");
  135.       if (dbf) setbuf(dbf,NULL);
  136.       umask(oldumask);
  137.     }
  138.     }
  139.   else
  140.     {
  141.       if (dbf)
  142.     {
  143.       fclose(dbf);
  144.       dbf = NULL;
  145.     }
  146.     }
  147. }
  148.  
  149.  
  150. /*******************************************************************
  151. check if the log has grown too big
  152. ********************************************************************/
  153. static void check_log_size(void)
  154. {
  155.   static int debug_count=0;
  156.   int maxlog;
  157.   struct stat st;
  158.  
  159.   if (debug_count++ < 100 || getuid() != 0) return;
  160.  
  161.   maxlog = lp_max_log_size() * 1024;
  162.   if (!dbf || maxlog <= 0) return;
  163.  
  164.   if (fstat(fileno(dbf),&st) == 0 && st.st_size > maxlog) {
  165.     fclose(dbf); dbf = NULL;
  166.     reopen_logs();
  167.     if (dbf && file_size(debugf) > maxlog) {
  168.       pstring name;
  169.       fclose(dbf); dbf = NULL;
  170.       sprintf(name,"%s.old",debugf);
  171.       sys_rename(debugf,name);
  172.       reopen_logs();
  173.     }
  174.   }
  175.   debug_count=0;
  176. }
  177.  
  178. /*******************************************************************
  179. write an debug message on the debugfile. This is called by the DEBUG
  180. macro
  181. ********************************************************************/
  182. #ifdef __STDC__
  183.  int Debug1(char *format_str, ...)
  184. {
  185. #else
  186.  int Debug1(va_alist)
  187. va_dcl
  188. {  
  189.   char *format_str;
  190. #endif
  191.   va_list ap;  
  192.   int old_errno = errno;
  193.  
  194.   if (stdout_logging) {
  195. #ifdef __STDC__
  196.     va_start(ap, format_str);
  197. #else
  198.     va_start(ap);
  199.     format_str = va_arg(ap,char *);
  200. #endif
  201.     vfprintf(dbf,format_str,ap);
  202.     va_end(ap);
  203.     errno = old_errno;
  204.     return(0);
  205.   }
  206.   
  207. #ifdef SYSLOG
  208.   if (!lp_syslog_only())
  209. #endif  
  210.     {
  211.       if (!dbf) {
  212.           int oldumask = umask(022);
  213.           dbf = fopen(debugf,"w");
  214.           umask(oldumask);
  215.           if (dbf) {
  216.               setbuf(dbf,NULL);
  217.           } else {
  218.               errno = old_errno;
  219.               return(0);
  220.           }
  221.       }
  222.     }
  223.  
  224. #ifdef SYSLOG
  225.   if (syslog_level < lp_syslog())
  226.     {
  227.       /* 
  228.        * map debug levels to syslog() priorities
  229.        * note that not all DEBUG(0, ...) calls are
  230.        * necessarily errors
  231.        */
  232.       static int priority_map[] = { 
  233.     LOG_ERR,     /* 0 */
  234.     LOG_WARNING, /* 1 */
  235.     LOG_NOTICE,  /* 2 */
  236.     LOG_INFO,    /* 3 */
  237.       };
  238.       int priority;
  239.       pstring msgbuf;
  240.       
  241.       if (syslog_level >= sizeof(priority_map) / sizeof(priority_map[0]) ||
  242.       syslog_level < 0)
  243.     priority = LOG_DEBUG;
  244.       else
  245.     priority = priority_map[syslog_level];
  246.       
  247. #ifdef __STDC__
  248.       va_start(ap, format_str);
  249. #else
  250.       va_start(ap);
  251.       format_str = va_arg(ap,char *);
  252. #endif
  253.       vsprintf(msgbuf, format_str, ap);
  254.       va_end(ap);
  255.       
  256.       msgbuf[255] = '\0';
  257.       syslog(priority, "%s", msgbuf);
  258.     }
  259. #endif
  260.   
  261. #ifdef SYSLOG
  262.   if (!lp_syslog_only())
  263. #endif
  264.     {
  265. #ifdef __STDC__
  266.       va_start(ap, format_str);
  267. #else
  268.       va_start(ap);
  269.       format_str = va_arg(ap,char *);
  270. #endif
  271.       vfprintf(dbf,format_str,ap);
  272.       va_end(ap);
  273.       fflush(dbf);
  274.     }
  275.  
  276.   check_log_size();
  277.  
  278.   errno = old_errno;
  279.  
  280.   return(0);
  281. }
  282.  
  283. /****************************************************************************
  284.   find a suitable temporary directory. The result should be copied immediately
  285.   as it may be overwritten by a subsequent call
  286.   ****************************************************************************/
  287. char *tmpdir(void)
  288. {
  289.   char *p;
  290.   if ((p = getenv("TMPDIR"))) {
  291.     return p;
  292.   }
  293.   return "/tmp";
  294. }
  295.  
  296.  
  297.  
  298. /****************************************************************************
  299. determine if a file descriptor is in fact a socket
  300. ****************************************************************************/
  301. BOOL is_a_socket(int fd)
  302. {
  303.   int v,l;
  304.   l = sizeof(int);
  305.   return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
  306. }
  307.  
  308.  
  309. static char *last_ptr=NULL;
  310.  
  311. /****************************************************************************
  312.   Get the next token from a string, return False if none found
  313.   handles double-quotes. 
  314. Based on a routine by GJC@VILLAGE.COM. 
  315. Extensively modified by Andrew.Tridgell@anu.edu.au
  316. ****************************************************************************/
  317. BOOL next_token(char **ptr,char *buff,char *sep)
  318. {
  319.   char *s;
  320.   BOOL quoted;
  321.  
  322.   if (!ptr) ptr = &last_ptr;
  323.   if (!ptr) return(False);
  324.  
  325.   s = *ptr;
  326.  
  327.   /* default to simple separators */
  328.   if (!sep) sep = " \t\n\r";
  329.  
  330.   /* find the first non sep char */
  331.   while(*s && strchr(sep,*s)) s++;
  332.  
  333.   /* nothing left? */
  334.   if (! *s) return(False);
  335.  
  336.   /* copy over the token */
  337.   for (quoted = False; *s && (quoted || !strchr(sep,*s)); s++)
  338.     {
  339.       if (*s == '\"') 
  340.     quoted = !quoted;
  341.       else
  342.     *buff++ = *s;
  343.     }
  344.  
  345.   *ptr = (*s) ? s+1 : s;  
  346.   *buff = 0;
  347.   last_ptr = *ptr;
  348.  
  349.   return(True);
  350. }
  351.  
  352. /****************************************************************************
  353. Convert list of tokens to array; dependent on above routine.
  354. Uses last_ptr from above - bit of a hack.
  355. ****************************************************************************/
  356. char **toktocliplist(int *ctok, char *sep)
  357. {
  358.   char *s=last_ptr;
  359.   int ictok=0;
  360.   char **ret, **iret;
  361.  
  362.   if (!sep) sep = " \t\n\r";
  363.  
  364.   while(*s && strchr(sep,*s)) s++;
  365.  
  366.   /* nothing left? */
  367.   if (!*s) return(NULL);
  368.  
  369.   do {
  370.     ictok++;
  371.     while(*s && (!strchr(sep,*s))) s++;
  372.     while(*s && strchr(sep,*s)) *s++=0;
  373.   } while(*s);
  374.  
  375.   *ctok=ictok;
  376.   s=last_ptr;
  377.  
  378.   if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL;
  379.   
  380.   while(ictok--) {    
  381.     *iret++=s;
  382.     while(*s++);
  383.     while(!*s) s++;
  384.   }
  385.  
  386.   return ret;
  387. }
  388.  
  389. #ifndef HAVE_MEMMOVE
  390. /*******************************************************************
  391. safely copies memory, ensuring no overlap problems.
  392. this is only used if the machine does not have it's own memmove().
  393. this is not the fastest algorithm in town, but it will do for our
  394. needs.
  395. ********************************************************************/
  396. void *MemMove(void *dest,void *src,int size)
  397. {
  398.   unsigned long d,s;
  399.   int i;
  400.   if (dest==src || !size) return(dest);
  401.  
  402.   d = (unsigned long)dest;
  403.   s = (unsigned long)src;
  404.  
  405.   if ((d >= (s+size)) || (s >= (d+size))) {
  406.     /* no overlap */
  407.     memcpy(dest,src,size);
  408.     return(dest);
  409.   }
  410.  
  411.   if (d < s)
  412.     {
  413.       /* we can forward copy */
  414.       if (s-d >= sizeof(int) && 
  415.       !(s%sizeof(int)) && !(d%sizeof(int)) && !(size%sizeof(int))) {
  416.     /* do it all as words */
  417.     int *idest = (int *)dest;
  418.     int *isrc = (int *)src;
  419.     size /= sizeof(int);
  420.     for (i=0;i<size;i++) idest[i] = isrc[i];
  421.       } else {
  422.     /* simplest */
  423.     char *cdest = (char *)dest;
  424.     char *csrc = (char *)src;
  425.     for (i=0;i<size;i++) cdest[i] = csrc[i];
  426.       }
  427.     }
  428.   else
  429.     {
  430.       /* must backward copy */
  431.       if (d-s >= sizeof(int) && 
  432.       !(s%sizeof(int)) && !(d%sizeof(int)) && !(size%sizeof(int))) {
  433.     /* do it all as words */
  434.     int *idest = (int *)dest;
  435.     int *isrc = (int *)src;
  436.     size /= sizeof(int);
  437.     for (i=size-1;i>=0;i--) idest[i] = isrc[i];
  438.       } else {
  439.     /* simplest */
  440.     char *cdest = (char *)dest;
  441.     char *csrc = (char *)src;
  442.     for (i=size-1;i>=0;i--) cdest[i] = csrc[i];
  443.       }      
  444.     }
  445.   return(dest);
  446. }
  447. #endif
  448.  
  449.  
  450. /****************************************************************************
  451. prompte a dptr (to make it recently used)
  452. ****************************************************************************/
  453. void array_promote(char *array,int elsize,int element)
  454. {
  455.   char *p;
  456.   if (element == 0)
  457.     return;
  458.  
  459.   p = (char *)malloc(elsize);
  460.  
  461.   if (!p)
  462.     {
  463.       DEBUG(5,("Ahh! Can't malloc\n"));
  464.       return;
  465.     }
  466.   memcpy(p,array + element * elsize, elsize);
  467.   memmove(array + elsize,array,elsize*element);
  468.   memcpy(array,p,elsize);
  469.   free(p);
  470. }
  471.  
  472. enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
  473.  
  474. struct
  475. {
  476.   char *name;
  477.   int level;
  478.   int option;
  479.   int value;
  480.   int opttype;
  481. } socket_options[] = {
  482.   {"SO_KEEPALIVE",      SOL_SOCKET,    SO_KEEPALIVE,    0,                 OPT_BOOL},
  483.   {"SO_REUSEADDR",      SOL_SOCKET,    SO_REUSEADDR,    0,                 OPT_BOOL},
  484.   {"SO_BROADCAST",      SOL_SOCKET,    SO_BROADCAST,    0,                 OPT_BOOL},
  485. #ifdef TCP_NODELAY
  486.   {"TCP_NODELAY",       IPPROTO_TCP,   TCP_NODELAY,     0,                 OPT_BOOL},
  487. #endif
  488. #ifdef IPTOS_LOWDELAY
  489.   {"IPTOS_LOWDELAY",    IPPROTO_IP,    IP_TOS,          IPTOS_LOWDELAY,    OPT_ON},
  490. #endif
  491. #ifdef IPTOS_THROUGHPUT
  492.   {"IPTOS_THROUGHPUT",  IPPROTO_IP,    IP_TOS,          IPTOS_THROUGHPUT,  OPT_ON},
  493. #endif
  494. #ifdef SO_SNDBUF
  495.   {"SO_SNDBUF",         SOL_SOCKET,    SO_SNDBUF,       0,                 OPT_INT},
  496. #endif
  497. #ifdef SO_RCVBUF
  498.   {"SO_RCVBUF",         SOL_SOCKET,    SO_RCVBUF,       0,                 OPT_INT},
  499. #endif
  500. #ifdef SO_SNDLOWAT
  501.   {"SO_SNDLOWAT",       SOL_SOCKET,    SO_SNDLOWAT,     0,                 OPT_INT},
  502. #endif
  503. #ifdef SO_RCVLOWAT
  504.   {"SO_RCVLOWAT",       SOL_SOCKET,    SO_RCVLOWAT,     0,                 OPT_INT},
  505. #endif
  506. #ifdef SO_SNDTIMEO
  507.   {"SO_SNDTIMEO",       SOL_SOCKET,    SO_SNDTIMEO,     0,                 OPT_INT},
  508. #endif
  509. #ifdef SO_RCVTIMEO
  510.   {"SO_RCVTIMEO",       SOL_SOCKET,    SO_RCVTIMEO,     0,                 OPT_INT},
  511. #endif
  512.   {NULL,0,0,0,0}};
  513.  
  514.     
  515.  
  516. /****************************************************************************
  517. set user socket options
  518. ****************************************************************************/
  519. void set_socket_options(int fd, char *options)
  520. {
  521.   string tok;
  522.  
  523.   while (next_token(&options,tok," \t,"))
  524.     {
  525.       int ret=0,i;
  526.       int value = 1;
  527.       char *p;
  528.       BOOL got_value = False;
  529.  
  530.       if ((p = strchr(tok,'=')))
  531.     {
  532.       *p = 0;
  533.       value = atoi(p+1);
  534.       got_value = True;
  535.     }
  536.  
  537.       for (i=0;socket_options[i].name;i++)
  538.     if (strequal(socket_options[i].name,tok))
  539.       break;
  540.  
  541.       if (!socket_options[i].name)
  542.     {
  543.       DEBUG(0,("Unknown socket option %s\n",tok));
  544.       continue;
  545.     }
  546.  
  547.       switch (socket_options[i].opttype)
  548.     {
  549.     case OPT_BOOL:
  550.     case OPT_INT:
  551.       ret = setsockopt(fd,socket_options[i].level,
  552.                socket_options[i].option,(char *)&value,sizeof(int));
  553.       break;
  554.  
  555.     case OPT_ON:
  556.       if (got_value)
  557.         DEBUG(0,("syntax error - %s does not take a value\n",tok));
  558.  
  559.       {
  560.         int on = socket_options[i].value;
  561.         ret = setsockopt(fd,socket_options[i].level,
  562.                  socket_options[i].option,(char *)&on,sizeof(int));
  563.       }
  564.       break;      
  565.     }
  566.       
  567.       if (ret != 0)
  568.     DEBUG(0,("Failed to set socket option %s\n",tok));
  569.     }
  570. }
  571.  
  572.  
  573.  
  574. /****************************************************************************
  575.   close the socket communication
  576. ****************************************************************************/
  577. void close_sockets(void )
  578. {
  579.   close(Client);
  580.   Client = 0;
  581. }
  582.  
  583. /****************************************************************************
  584. determine whether we are in the specified group
  585. ****************************************************************************/
  586. BOOL in_group(gid_t group, int current_gid, int ngroups, int *groups)
  587. {
  588.   int i;
  589.  
  590.   if (group == current_gid) return(True);
  591.  
  592.   for (i=0;i<ngroups;i++)
  593.     if (group == groups[i])
  594.       return(True);
  595.  
  596.   return(False);
  597. }
  598.  
  599. /****************************************************************************
  600. this is a safer strcpy(), meant to prevent core dumps when nasty things happen
  601. ****************************************************************************/
  602. char *StrCpy(char *dest,char *src)
  603. {
  604.   char *d = dest;
  605.  
  606. #if AJT
  607.   /* I don't want to get lazy with these ... */
  608.   if (!dest || !src) {
  609.     DEBUG(0,("ERROR: NULL StrCpy() called!\n"));
  610.     ajt_panic();
  611.   }
  612. #endif
  613.  
  614.   if (!dest) return(NULL);
  615.   if (!src) {
  616.     *dest = 0;
  617.     return(dest);
  618.   }
  619.   while ((*d++ = *src++)) ;
  620.   return(dest);
  621. }
  622.  
  623. /****************************************************************************
  624. line strncpy but always null terminates. Make sure there is room!
  625. ****************************************************************************/
  626. char *StrnCpy(char *dest,char *src,int n)
  627. {
  628.   char *d = dest;
  629.   if (!dest) return(NULL);
  630.   if (!src) {
  631.     *dest = 0;
  632.     return(dest);
  633.   }
  634.   while (n-- && (*d++ = *src++)) ;
  635.   *d = 0;
  636.   return(dest);
  637. }
  638.  
  639.  
  640. /*******************************************************************
  641. copy an IP address from one buffer to another
  642. ********************************************************************/
  643. void putip(void *dest,void *src)
  644. {
  645.   memcpy(dest,src,4);
  646. }
  647.  
  648.  
  649. /****************************************************************************
  650. interpret the weird netbios "name". Return the name type
  651. ****************************************************************************/
  652. static int name_interpret(char *in,char *out)
  653. {
  654.   int ret;
  655.   int len = (*in++) / 2;
  656.  
  657.   *out=0;
  658.  
  659.   if (len > 30 || len<1) return(0);
  660.  
  661.   while (len--)
  662.     {
  663.       if (in[0] < 'A' || in[0] > 'P' || in[1] < 'A' || in[1] > 'P') {
  664.     *out = 0;
  665.     return(0);
  666.       }
  667.       *out = ((in[0]-'A')<<4) + (in[1]-'A');
  668.       in += 2;
  669.       out++;
  670.     }
  671.   *out = 0;
  672.   ret = out[-1];
  673.  
  674. #ifdef NETBIOS_SCOPE
  675.   /* Handle any scope names */
  676.   while(*in) 
  677.     {
  678.       *out++ = '.'; /* Scope names are separated by periods */
  679.       len = *(unsigned char *)in++;
  680.       StrnCpy(out, in, len);
  681.       out += len;
  682.       *out=0;
  683.       in += len;
  684.     }
  685. #endif
  686.   return(ret);
  687. }
  688.  
  689. /****************************************************************************
  690. mangle a name into netbios format
  691. ****************************************************************************/
  692. int name_mangle(char *In,char *Out,char name_type)
  693. {
  694.   fstring name;
  695.   char buf[20];
  696.   char *in = (char *)&buf[0];
  697.   char *out = (char *)Out;
  698.   char *p, *label;
  699.   int i;
  700.  
  701.   if (In[0] != '*') {
  702.     StrnCpy(name,In,sizeof(name)-1);
  703.     sprintf(buf,"%-15.15s%c",name,name_type);
  704.   } else {
  705.     buf[0]='*';
  706.     memset(&buf[1],0,16);
  707.   }
  708.  
  709.   *out++ = 32;
  710.   for (i=0;i<16;i++) {
  711.     char c = toupper(in[i]);
  712.     out[i*2] = (c>>4) + 'A';
  713.     out[i*2+1] = (c & 0xF) + 'A';
  714.   }
  715.   out[32]=0;
  716.   out += 32;
  717.   
  718.   label = scope;
  719.   while (*label)
  720.     {
  721.       p = strchr(label, '.');
  722.       if (p == 0)
  723.     p = label + strlen(label);
  724.       *out++ = p - label;
  725.       memcpy(out, label, p - label);
  726.       out += p - label;
  727.       label += p - label + (*p == '.');
  728.     }
  729.   *out = 0;
  730.   return(name_len(Out));
  731. }
  732.  
  733.  
  734. /*******************************************************************
  735.   check if a file exists
  736. ********************************************************************/
  737. BOOL file_exist(char *fname,struct stat *sbuf)
  738. {
  739.   struct stat st;
  740.   if (!sbuf) sbuf = &st;
  741.  
  742. #ifdef USE_NETINFO
  743.     if (!strcmp(fname, "netinfo"))
  744.         return True;
  745.     else
  746. #endif
  747.   if (sys_stat(fname,sbuf) != 0) 
  748.     return(False);
  749.  
  750.   return(S_ISREG(sbuf->st_mode));
  751. }
  752.  
  753. /*******************************************************************
  754. check a files mod time
  755. ********************************************************************/
  756. time_t file_modtime(char *fname)
  757. {
  758.   struct stat st;
  759.   
  760.   if (sys_stat(fname,&st) != 0) 
  761.     return(0);
  762.  
  763.   return(st.st_mtime);
  764. }
  765.  
  766. /*******************************************************************
  767.   check if a directory exists
  768. ********************************************************************/
  769. BOOL directory_exist(char *dname,struct stat *st)
  770. {
  771.   struct stat st2;
  772.   BOOL ret;
  773.  
  774.   if (!st) st = &st2;
  775.  
  776.   if (sys_stat(dname,st) != 0) 
  777.     return(False);
  778.  
  779.   ret = S_ISDIR(st->st_mode);
  780.   if(!ret)
  781.     errno = ENOTDIR;
  782.   return ret;
  783. }
  784.  
  785. /*******************************************************************
  786. returns the size in bytes of the named file
  787. ********************************************************************/
  788. uint32 file_size(char *file_name)
  789. {
  790.   struct stat buf;
  791.   buf.st_size = 0;
  792.   sys_stat(file_name,&buf);
  793.   return(buf.st_size);
  794. }
  795.  
  796. /*******************************************************************
  797. return a string representing an attribute for a file
  798. ********************************************************************/
  799. char *attrib_string(int mode)
  800. {
  801.   static char attrstr[10];
  802.  
  803.   attrstr[0] = 0;
  804.  
  805.   if (mode & aVOLID) strcat(attrstr,"V");
  806.   if (mode & aDIR) strcat(attrstr,"D");
  807.   if (mode & aARCH) strcat(attrstr,"A");
  808.   if (mode & aHIDDEN) strcat(attrstr,"H");
  809.   if (mode & aSYSTEM) strcat(attrstr,"S");
  810.   if (mode & aRONLY) strcat(attrstr,"R");      
  811.  
  812.   return(attrstr);
  813. }
  814.  
  815.  
  816. /*******************************************************************
  817.   case insensitive string compararison
  818. ********************************************************************/
  819. int StrCaseCmp(const char *s, const char *t)
  820. {
  821.   /* compare until we run out of string, either t or s, or find a difference */
  822.   /* We *must* use toupper rather than tolower here due to the
  823.      asynchronous upper to lower mapping.
  824.    */
  825. #if defined(KANJI) && !defined(KANJI_WIN95_COMPATIBILITY)
  826.   /* Win95 treats full width ascii characters as case sensitive. */
  827.   int diff;
  828.   for (;;)
  829.   {
  830.     if (!*s || !*t)
  831.       return toupper (*s) - toupper (*t);
  832.     else if (is_sj_alph (*s) && is_sj_alph (*t))
  833.     {
  834.       diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
  835.       if (diff)
  836.         return diff;
  837.       s += 2;
  838.       t += 2;
  839.     }
  840.     else if (is_shift_jis (*s) && is_shift_jis (*t))
  841.     {
  842.       diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
  843.       if (diff)
  844.         return diff;
  845.       diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
  846.       if (diff)
  847.         return diff;
  848.       s += 2;
  849.       t += 2;
  850.     }
  851.     else if (is_shift_jis (*s))
  852.       return 1;
  853.     else if (is_shift_jis (*t))
  854.       return -1;
  855.     else 
  856.     {
  857.       diff = toupper (*s) - toupper (*t);
  858.       if (diff)
  859.         return diff;
  860.       s++;
  861.       t++;
  862.     }
  863.   }
  864. #else /* KANJI */
  865.   while (*s && *t && toupper(*s) == toupper(*t))
  866.   {
  867.     s++; t++;
  868.   }
  869.  
  870.   return(toupper(*s) - toupper(*t));
  871. #endif /* KANJI */
  872. }
  873.  
  874. /*******************************************************************
  875.   case insensitive string compararison, length limited
  876. ********************************************************************/
  877. int StrnCaseCmp(const char *s, const char *t, int n)
  878. {
  879.   /* compare until we run out of string, either t or s, or chars */
  880.   /* We *must* use toupper rather than tolower here due to the
  881.      asynchronous upper to lower mapping.
  882.    */
  883. #if defined(KANJI) && !defined(KANJI_WIN95_COMPATIBILITY)
  884.   /* Win95 treats full width ascii characters as case sensitive. */
  885.   int diff;
  886.   for (;n > 0;)
  887.   {
  888.     if (!*s || !*t)
  889.       return toupper (*s) - toupper (*t);
  890.     else if (is_sj_alph (*s) && is_sj_alph (*t))
  891.     {
  892.       diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
  893.       if (diff)
  894.         return diff;
  895.       s += 2;
  896.       t += 2;
  897.       n -= 2;
  898.     }
  899.     else if (is_shift_jis (*s) && is_shift_jis (*t))
  900.     {
  901.       diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
  902.       if (diff)
  903.         return diff;
  904.       diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
  905.       if (diff)
  906.         return diff;
  907.       s += 2;
  908.       t += 2;
  909.       n -= 2;
  910.     }
  911.     else if (is_shift_jis (*s))
  912.       return 1;
  913.     else if (is_shift_jis (*t))
  914.       return -1;
  915.     else 
  916.     {
  917.       diff = toupper (*s) - toupper (*t);
  918.       if (diff)
  919.         return diff;
  920.       s++;
  921.       t++;
  922.       n--;
  923.     }
  924.   }
  925.   return 0;
  926. #else /* KANJI */
  927.   while (n-- && *s && *t && toupper(*s) == toupper(*t))
  928.   {
  929.     s++; t++;
  930.   }
  931.  
  932.   /* not run out of chars - strings are different lengths */
  933.   if (n) return(toupper(*s) - toupper(*t));
  934.  
  935.   /* identical up to where we run out of chars, and strings are same length */
  936.   return(0);
  937. #endif /* KANJI */
  938. }
  939.  
  940. /*******************************************************************
  941.   compare 2 strings 
  942. ********************************************************************/
  943. BOOL strequal(const char *s1, const char *s2)
  944. {
  945.   if (s1 == s2) return(True);
  946.   if (!s1 || !s2) return(False);
  947.   
  948.   return(StrCaseCmp(s1,s2)==0);
  949. }
  950.  
  951. /*******************************************************************
  952.   compare 2 strings up to and including the nth char.
  953.   ******************************************************************/
  954. BOOL strnequal(const char *s1,const char *s2,int n)
  955. {
  956.   if (s1 == s2) return(True);
  957.   if (!s1 || !s2 || !n) return(False);
  958.   
  959.   return(StrnCaseCmp(s1,s2,n)==0);
  960. }
  961.  
  962. /*******************************************************************
  963.   compare 2 strings (case sensitive)
  964. ********************************************************************/
  965. BOOL strcsequal(char *s1,char *s2)
  966. {
  967.   if (s1 == s2) return(True);
  968.   if (!s1 || !s2) return(False);
  969.   
  970.   return(strcmp(s1,s2)==0);
  971. }
  972.  
  973.  
  974. /*******************************************************************
  975.   convert a string to lower case
  976. ********************************************************************/
  977. void strlower(char *s)
  978. {
  979.   while (*s)
  980.     {
  981. #if defined(KANJI) && !defined(KANJI_WIN95_COMPATIBILITY)
  982.   /* Win95 treats full width ascii characters as case sensitive. */
  983.     if (is_shift_jis (*s)) {
  984.         if (is_sj_upper (s[0], s[1])) {
  985.             s[1] = sj_tolower2 (s[1]);
  986.         }
  987.         s += 2;
  988.     } else if (is_kana (*s)) {
  989.         s++;
  990.     } else {
  991.         if (isupper(*s))
  992.         *s = tolower(*s);
  993.         s++;
  994.     }
  995. #else /* KANJI */
  996.       if (isupper(*s))
  997.       *s = tolower(*s);
  998.       s++;
  999. #endif /* KANJI */
  1000.     }
  1001. }
  1002.  
  1003. /*******************************************************************
  1004.   convert a string to upper case
  1005. ********************************************************************/
  1006. void strupper(char *s)
  1007. {
  1008.   while (*s)
  1009.     {
  1010. #if defined(KANJI) && !defined(KANJI_WIN95_COMPATIBILITY)
  1011.   /* Win95 treats full width ascii characters as case sensitive. */
  1012.     if (is_shift_jis (*s)) {
  1013.         if (is_sj_lower (s[0], s[1])) {
  1014.           s[1] = sj_toupper2 (s[1]);
  1015.         }
  1016.         s += 2;
  1017.     } else if (is_kana (*s)) {
  1018.         s++;
  1019.     } else {
  1020.         if (islower(*s))
  1021.         *s = toupper(*s);
  1022.         s++;
  1023.     }
  1024. #else /* KANJI */
  1025.       if (islower(*s))
  1026.     *s = toupper(*s);
  1027.       s++;
  1028. #endif /* KANJI */
  1029.     }
  1030. }
  1031.  
  1032. /*******************************************************************
  1033.   convert a string to "normal" form
  1034. ********************************************************************/
  1035. void strnorm(char *s)
  1036. {
  1037.   if (case_default == CASE_UPPER)
  1038.     strupper(s);
  1039.   else
  1040.     strlower(s);
  1041. }
  1042.  
  1043. /*******************************************************************
  1044. check if a string is in "normal" case
  1045. ********************************************************************/
  1046. BOOL strisnormal(char *s)
  1047. {
  1048.   if (case_default == CASE_UPPER)
  1049.     return(!strhaslower(s));
  1050.  
  1051.   return(!strhasupper(s));
  1052. }
  1053.  
  1054.  
  1055. /****************************************************************************
  1056.   string replace
  1057. ****************************************************************************/
  1058. void string_replace(char *s,char oldc,char newc)
  1059. {
  1060.   while (*s)
  1061.     {
  1062. #if defined(KANJI) && !defined(KANJI_WIN95_COMPATIBILITY)
  1063.   /* Win95 treats full width ascii characters as case sensitive. */
  1064.     if (is_shift_jis (*s)) {
  1065.         s += 2;
  1066.     } else if (is_kana (*s)) {
  1067.         s++;
  1068.     } else {
  1069.         if (oldc == *s)
  1070.         *s = newc;
  1071.         s++;
  1072.     }
  1073. #else /* KANJI */
  1074.       if (oldc == *s)
  1075.     *s = newc;
  1076.       s++;
  1077. #endif /* KANJI */
  1078.     }
  1079. }
  1080.  
  1081. /****************************************************************************
  1082.   make a file into unix format
  1083. ****************************************************************************/
  1084. void unix_format(char *fname)
  1085. {
  1086.   pstring namecopy;
  1087.   string_replace(fname,'\\','/');
  1088.  
  1089.   if (*fname == '/')
  1090.     {
  1091.       pstrcpy(namecopy,fname);
  1092.       strcpy(fname,".");
  1093.       strcat(fname,namecopy);
  1094.     }  
  1095. }
  1096.  
  1097. /****************************************************************************
  1098.   make a file into dos format
  1099. ****************************************************************************/
  1100. void dos_format(char *fname)
  1101. {
  1102.   string_replace(fname,'/','\\');
  1103. }
  1104.  
  1105.  
  1106. /*******************************************************************
  1107.   show a smb message structure
  1108. ********************************************************************/
  1109. void show_msg(char *buf)
  1110. {
  1111.   int i;
  1112.   int j;
  1113.   int bcc=0;
  1114.   if (DEBUGLEVEL < 5)
  1115.     return;
  1116.  
  1117.   DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
  1118.       smb_len(buf),
  1119.       (int)CVAL(buf,smb_com),
  1120.       (int)CVAL(buf,smb_rcls),
  1121.       (int)CVAL(buf,smb_reh),
  1122.       (int)SVAL(buf,smb_err),
  1123.       (int)CVAL(buf,smb_flg),
  1124.       (int)SVAL(buf,smb_flg2)));
  1125.   DEBUG(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\nsmt_wct=%d\n",
  1126.       (int)SVAL(buf,smb_tid),
  1127.       (int)SVAL(buf,smb_pid),
  1128.       (int)SVAL(buf,smb_uid),
  1129.       (int)SVAL(buf,smb_mid),
  1130.       (int)CVAL(buf,smb_wct)));
  1131.   for (i=0;i<(int)CVAL(buf,smb_wct);i++)
  1132.     DEBUG(5,("smb_vwv[%d]=%d (0x%X)\n",i,
  1133.       SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
  1134.   bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
  1135.   DEBUG(5,("smb_bcc=%d\n",bcc));
  1136.   if (DEBUGLEVEL < 10)
  1137.     return;
  1138.   for (i = 0; i < MIN(bcc, 256); i += 16)
  1139.   {
  1140.     for (j = 0; j < 16 && i+j < MIN(bcc,256); j++)
  1141.     {
  1142.  
  1143.       DEBUG(10,("%2X ",CVAL(smb_buf(buf),i+j)));
  1144.       if (j == 7) DEBUG(10, ("  "));
  1145.  
  1146.     }
  1147.     DEBUG(10,("  "));  
  1148.  
  1149.     for (j = 0; j < 16 && i+j < MIN(bcc,256); j++)
  1150.     {
  1151.       unsigned char c = CVAL(smb_buf(buf),i+j);
  1152.       if (c < 32 || c > 128) c = '.';
  1153.       DEBUG(10,("%c",c));
  1154.  
  1155.       if (j == 7) DEBUG(10, ("  "));
  1156.     }
  1157.  
  1158.   DEBUG(10,("\n"));  
  1159. }
  1160. }
  1161.  
  1162. /*******************************************************************
  1163.   return the length of an smb packet
  1164. ********************************************************************/
  1165. int smb_len(char *buf)
  1166. {
  1167.   return( PVAL(buf,3) | (PVAL(buf,2)<<8) | ((PVAL(buf,1)&1)<<16) );
  1168. }
  1169.  
  1170. /*******************************************************************
  1171.   set the length of an smb packet
  1172. ********************************************************************/
  1173. void _smb_setlen(char *buf,int len)
  1174. {
  1175.   buf[0] = 0;
  1176.   buf[1] = (len&0x10000)>>16;
  1177.   buf[2] = (len&0xFF00)>>8;
  1178.   buf[3] = len&0xFF;
  1179. }
  1180.  
  1181. /*******************************************************************
  1182.   set the length and marker of an smb packet
  1183. ********************************************************************/
  1184. void smb_setlen(char *buf,int len)
  1185. {
  1186.   _smb_setlen(buf,len);
  1187.  
  1188.   CVAL(buf,4) = 0xFF;
  1189.   CVAL(buf,5) = 'S';
  1190.   CVAL(buf,6) = 'M';
  1191.   CVAL(buf,7) = 'B';
  1192. }
  1193.  
  1194. /*******************************************************************
  1195.   setup the word count and byte count for a smb message
  1196. ********************************************************************/
  1197. int set_message(char *buf,int num_words,int num_bytes,BOOL zero)
  1198. {
  1199.   if (zero)
  1200.     bzero(buf + smb_size,num_words*2 + num_bytes);
  1201.   CVAL(buf,smb_wct) = num_words;
  1202.   SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);  
  1203.   smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
  1204.   return (smb_size + num_words*2 + num_bytes);
  1205. }
  1206.  
  1207. /*******************************************************************
  1208. return the number of smb words
  1209. ********************************************************************/
  1210. int smb_numwords(char *buf)
  1211. {
  1212.   return (CVAL(buf,smb_wct));
  1213. }
  1214.  
  1215. /*******************************************************************
  1216. return the size of the smb_buf region of a message
  1217. ********************************************************************/
  1218. int smb_buflen(char *buf)
  1219. {
  1220.   return(SVAL(buf,smb_vwv0 + smb_numwords(buf)*2));
  1221. }
  1222.  
  1223. /*******************************************************************
  1224.   return a pointer to the smb_buf data area
  1225. ********************************************************************/
  1226. int smb_buf_ofs(char *buf)
  1227. {
  1228.   return (smb_size + CVAL(buf,smb_wct)*2);
  1229. }
  1230.  
  1231. /*******************************************************************
  1232.   return a pointer to the smb_buf data area
  1233. ********************************************************************/
  1234. char *smb_buf(char *buf)
  1235. {
  1236.   return (buf + smb_buf_ofs(buf));
  1237. }
  1238.  
  1239. /*******************************************************************
  1240. return the SMB offset into an SMB buffer
  1241. ********************************************************************/
  1242. int smb_offset(char *p,char *buf)
  1243. {
  1244.   return(PTR_DIFF(p,buf+4) + chain_size);
  1245. }
  1246.  
  1247.  
  1248. /*******************************************************************
  1249. skip past some strings in a buffer
  1250. ********************************************************************/
  1251. char *skip_string(char *buf,int n)
  1252. {
  1253.   while (n--)
  1254.     buf += strlen(buf) + 1;
  1255.   return(buf);
  1256. }
  1257.  
  1258. /*******************************************************************
  1259. trim the specified elements off the front and back of a string
  1260. ********************************************************************/
  1261. BOOL trim_string(char *s,char *front,char *back)
  1262. {
  1263.   BOOL ret = False;
  1264.   while (front && *front && strncmp(s,front,strlen(front)) == 0)
  1265.     {
  1266.       char *p = s;
  1267.       ret = True;
  1268.       while (1)
  1269.     {
  1270.       if (!(*p = p[strlen(front)]))
  1271.         break;
  1272.       p++;
  1273.     }
  1274.     }
  1275.   while (back && *back && strlen(s) >= strlen(back) && 
  1276.      (strncmp(s+strlen(s)-strlen(back),back,strlen(back))==0))  
  1277.     {
  1278.       ret = True;
  1279.       s[strlen(s)-strlen(back)] = 0;
  1280.     }
  1281.   return(ret);
  1282. }
  1283.  
  1284.  
  1285. /*******************************************************************
  1286. reduce a file name, removing .. elements.
  1287. ********************************************************************/
  1288. void dos_clean_name(char *s)
  1289. {
  1290.   char *p=NULL;
  1291.  
  1292.   DEBUG(3,("dos_clean_name [%s]\n",s));
  1293.  
  1294.   /* remove any double slashes */
  1295.   string_sub(s, "\\\\", "\\");
  1296.  
  1297.   while ((p = strstr(s,"\\..\\")) != NULL)
  1298.     {
  1299.       pstring s1;
  1300.  
  1301.       *p = 0;
  1302.       pstrcpy(s1,p+3);
  1303.  
  1304.       if ((p=strrchr(s,'\\')) != NULL)
  1305.     *p = 0;
  1306.       else
  1307.     *s = 0;
  1308.       strcat(s,s1);
  1309.     }  
  1310.  
  1311.   trim_string(s,NULL,"\\..");
  1312.  
  1313.   string_sub(s, "\\.\\", "\\");
  1314. }
  1315.  
  1316. /*******************************************************************
  1317. reduce a file name, removing .. elements. 
  1318. ********************************************************************/
  1319. void unix_clean_name(char *s)
  1320. {
  1321.   char *p=NULL;
  1322.  
  1323.   DEBUG(3,("unix_clean_name [%s]\n",s));
  1324.  
  1325.   /* remove any double slashes */
  1326.   string_sub(s, "//","/");
  1327.  
  1328.   /* Remove leading ./ characters */
  1329.   if(strncmp(s, "./", 2) == 0) {
  1330.     trim_string(s, "./", NULL);
  1331.     if(*s == 0)
  1332.       strcpy(s,"./");
  1333.   }
  1334.  
  1335.   while ((p = strstr(s,"/../")) != NULL)
  1336.     {
  1337.       pstring s1;
  1338.  
  1339.       *p = 0;
  1340.       pstrcpy(s1,p+3);
  1341.  
  1342.       if ((p=strrchr(s,'/')) != NULL)
  1343.     *p = 0;
  1344.       else
  1345.     *s = 0;
  1346.       strcat(s,s1);
  1347.     }  
  1348.  
  1349.   trim_string(s,NULL,"/..");
  1350. }
  1351.  
  1352.  
  1353. /*******************************************************************
  1354. a wrapper for the normal chdir() function
  1355. ********************************************************************/
  1356. int ChDir(char *path)
  1357. {
  1358.   int res;
  1359.   static pstring LastDir="";
  1360.  
  1361.   if (strcsequal(path,".")) return(0);
  1362.  
  1363.   if (*path == '/' && strcsequal(LastDir,path)) return(0);
  1364.   DEBUG(3,("chdir to %s\n",path));
  1365.   res = sys_chdir(path);
  1366.   if (!res)
  1367.     pstrcpy(LastDir,path);
  1368.   return(res);
  1369. }
  1370.  
  1371. /* number of list structures for a caching GetWd function. */
  1372. #define MAX_GETWDCACHE (50)
  1373.  
  1374. struct
  1375. {
  1376.   ino_t inode;
  1377.   dev_t dev;
  1378.   char *text;
  1379.   BOOL valid;
  1380. } ino_list[MAX_GETWDCACHE];
  1381.  
  1382. BOOL use_getwd_cache=True;
  1383.  
  1384. /*******************************************************************
  1385.   return the absolute current directory path
  1386. ********************************************************************/
  1387. char *GetWd(char *str)
  1388. {
  1389.   pstring s;
  1390.   static BOOL getwd_cache_init = False;
  1391.   struct stat st, st2;
  1392.   int i;
  1393.  
  1394.   *s = 0;
  1395.  
  1396.   if (!use_getwd_cache)
  1397.     return(sys_getwd(str));
  1398.  
  1399.   /* init the cache */
  1400.   if (!getwd_cache_init)
  1401.     {
  1402.       getwd_cache_init = True;
  1403.       for (i=0;i<MAX_GETWDCACHE;i++)
  1404.     {
  1405.       string_init(&ino_list[i].text,"");
  1406.       ino_list[i].valid = False;
  1407.     }
  1408.     }
  1409.  
  1410.   /*  Get the inode of the current directory, if this doesn't work we're
  1411.       in trouble :-) */
  1412.  
  1413.   if (stat(".",&st) == -1) 
  1414.     {
  1415.       DEBUG(0,("Very strange, couldn't stat \".\"\n"));
  1416.       return(sys_getwd(str));
  1417.     }
  1418.  
  1419.  
  1420.   for (i=0; i<MAX_GETWDCACHE; i++)
  1421.     if (ino_list[i].valid)
  1422.       {
  1423.  
  1424.     /*  If we have found an entry with a matching inode and dev number
  1425.         then find the inode number for the directory in the cached string.
  1426.         If this agrees with that returned by the stat for the current
  1427.         directory then all is o.k. (but make sure it is a directory all
  1428.         the same...) */
  1429.       
  1430.     if (st.st_ino == ino_list[i].inode &&
  1431.         st.st_dev == ino_list[i].dev)
  1432.       {
  1433.         if (stat(ino_list[i].text,&st2) == 0)
  1434.           {
  1435.         if (st.st_ino == st2.st_ino &&
  1436.             st.st_dev == st2.st_dev &&
  1437.             (st2.st_mode & S_IFMT) == S_IFDIR)
  1438.           {
  1439.             strcpy (str, ino_list[i].text);
  1440.  
  1441.             /* promote it for future use */
  1442.             array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
  1443.             return (str);
  1444.           }
  1445.         else
  1446.           {
  1447.             /*  If the inode is different then something's changed, 
  1448.             scrub the entry and start from scratch. */
  1449.             ino_list[i].valid = False;
  1450.           }
  1451.           }
  1452.       }
  1453.       }
  1454.  
  1455.  
  1456.   /*  We don't have the information to hand so rely on traditional methods.
  1457.       The very slow getcwd, which spawns a process on some systems, or the
  1458.       not quite so bad getwd. */
  1459.  
  1460.   if (!sys_getwd(s))
  1461.     {
  1462.       DEBUG(0,("Getwd failed, errno %d\n",errno));
  1463.       return (NULL);
  1464.     }
  1465.  
  1466.   strcpy(str,s);
  1467.  
  1468.   DEBUG(5,("GetWd %s, inode %d, dev %x\n",s,(int)st.st_ino,(int)st.st_dev));
  1469.  
  1470.   /* add it to the cache */
  1471.   i = MAX_GETWDCACHE - 1;
  1472.   string_set(&ino_list[i].text,s);
  1473.   ino_list[i].dev = st.st_dev;
  1474.   ino_list[i].inode = st.st_ino;
  1475.   ino_list[i].valid = True;
  1476.  
  1477.   /* put it at the top of the list */
  1478.   array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
  1479.  
  1480.   return (str);
  1481. }
  1482.  
  1483.  
  1484.  
  1485. /*******************************************************************
  1486. reduce a file name, removing .. elements and checking that 
  1487. it is below dir in the heirachy. This uses GetWd() and so must be run
  1488. on the system that has the referenced file system.
  1489.  
  1490. widelinks are allowed if widelinks is true
  1491. ********************************************************************/
  1492. BOOL reduce_name(char *s,char *dir,BOOL widelinks)
  1493. {
  1494. #ifndef REDUCE_PATHS
  1495.   return True;
  1496. #else
  1497.   pstring dir2;
  1498.   pstring wd;
  1499.   pstring basename;
  1500.   pstring newname;
  1501.   char *p=NULL;
  1502.   BOOL relative = (*s != '/');
  1503.  
  1504.   *dir2 = *wd = *basename = *newname = 0;
  1505.  
  1506.   if (widelinks)
  1507.     {
  1508.       unix_clean_name(s);
  1509.       /* can't have a leading .. */
  1510.       if (strncmp(s,"..",2) == 0 && (s[2]==0 || s[2]=='/'))
  1511.     {
  1512.       DEBUG(3,("Illegal file name? (%s)\n",s));
  1513.       return(False);
  1514.     }
  1515.  
  1516.       if (strlen(s) == 0)
  1517.         strcpy(s,"./");
  1518.  
  1519.       return(True);
  1520.     }
  1521.   
  1522.   DEBUG(3,("reduce_name [%s] [%s]\n",s,dir));
  1523.  
  1524.   /* remove any double slashes */
  1525.   string_sub(s,"//","/");
  1526.  
  1527.   pstrcpy(basename,s);
  1528.   p = strrchr(basename,'/');
  1529.  
  1530.   if (!p)
  1531.     return(True);
  1532.  
  1533.   if (!GetWd(wd))
  1534.     {
  1535.       DEBUG(0,("couldn't getwd for %s %s\n",s,dir));
  1536.       return(False);
  1537.     }
  1538.  
  1539.   if (ChDir(dir) != 0)
  1540.     {
  1541.       DEBUG(0,("couldn't chdir to %s\n",dir));
  1542.       return(False);
  1543.     }
  1544.  
  1545.   if (!GetWd(dir2))
  1546.     {
  1547.       DEBUG(0,("couldn't getwd for %s\n",dir));
  1548.       ChDir(wd);
  1549.       return(False);
  1550.     }
  1551.  
  1552.  
  1553.     if (p && (p != basename))
  1554.       {
  1555.     *p = 0;
  1556.     if (strcmp(p+1,".")==0)
  1557.       p[1]=0;
  1558.     if (strcmp(p+1,"..")==0)
  1559.       *p = '/';
  1560.       }
  1561.  
  1562.   if (ChDir(basename) != 0)
  1563.     {
  1564.       ChDir(wd);
  1565.       DEBUG(3,("couldn't chdir for %s %s basename=%s\n",s,dir,basename));
  1566.       return(False);
  1567.     }
  1568.  
  1569.   if (!GetWd(newname))
  1570.     {
  1571.       ChDir(wd);
  1572.       DEBUG(2,("couldn't get wd for %s %s\n",s,dir2));
  1573.       return(False);
  1574.     }
  1575.  
  1576.   if (p && (p != basename))
  1577.     {
  1578.       strcat(newname,"/");
  1579.       strcat(newname,p+1);
  1580.     }
  1581.  
  1582.   {
  1583.     int l = strlen(dir2);    
  1584.     if (dir2[l-1] == '/')
  1585.       l--;
  1586.  
  1587.     if (strncmp(newname,dir2,l) != 0)
  1588.       {
  1589.     ChDir(wd);
  1590.     DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n",s,dir2,newname,l));
  1591.     return(False);
  1592.       }
  1593.  
  1594.     if (relative)
  1595.       {
  1596.     if (newname[l] == '/')
  1597.       pstrcpy(s,newname + l + 1);
  1598.     else
  1599.       pstrcpy(s,newname+l);
  1600.       }
  1601.     else
  1602.       pstrcpy(s,newname);
  1603.   }
  1604.  
  1605.   ChDir(wd);
  1606.  
  1607.   if (strlen(s) == 0)
  1608.     strcpy(s,"./");
  1609.  
  1610.   DEBUG(3,("reduced to %s\n",s));
  1611.   return(True);
  1612. #endif
  1613. }
  1614.  
  1615. /****************************************************************************
  1616. expand some *s 
  1617. ****************************************************************************/
  1618. static void expand_one(char *Mask,int len)
  1619. {
  1620.   char *p1;
  1621.   while ((p1 = strchr(Mask,'*')) != NULL)
  1622.     {
  1623.       int lfill = (len+1) - strlen(Mask);
  1624.       int l1= (p1 - Mask);
  1625.       pstring tmp;
  1626.       pstrcpy(tmp,Mask);  
  1627.       memset(tmp+l1,'?',lfill);
  1628.       pstrcpy(tmp + l1 + lfill,Mask + l1 + 1);    
  1629.       pstrcpy(Mask,tmp);      
  1630.     }
  1631. }
  1632.  
  1633. /****************************************************************************
  1634. expand a wildcard expression, replacing *s with ?s
  1635. ****************************************************************************/
  1636. void expand_mask(char *Mask,BOOL doext)
  1637. {
  1638.   pstring mbeg,mext;
  1639.   pstring dirpart;
  1640.   pstring filepart;
  1641.   BOOL hasdot = False;
  1642.   char *p1;
  1643.   BOOL absolute = (*Mask == '\\');
  1644.  
  1645.   *mbeg = *mext = *dirpart = *filepart = 0;
  1646.  
  1647.   /* parse the directory and filename */
  1648.   if (strchr(Mask,'\\'))
  1649.     dirname_dos(Mask,dirpart);
  1650.  
  1651.   filename_dos(Mask,filepart);
  1652.  
  1653.   pstrcpy(mbeg,filepart);
  1654.   if ((p1 = strchr(mbeg,'.')) != NULL)
  1655.     {
  1656.       hasdot = True;
  1657.       *p1 = 0;
  1658.       p1++;
  1659.       pstrcpy(mext,p1);
  1660.     }
  1661.   else
  1662.     {
  1663.       strcpy(mext,"");
  1664.       if (strlen(mbeg) > 8)
  1665.     {
  1666.       pstrcpy(mext,mbeg + 8);
  1667.       mbeg[8] = 0;
  1668.     }
  1669.     }
  1670.  
  1671.   if (*mbeg == 0)
  1672.     strcpy(mbeg,"????????");
  1673.   if ((*mext == 0) && doext && !hasdot)
  1674.     strcpy(mext,"???");
  1675.  
  1676.   if (strequal(mbeg,"*") && *mext==0) 
  1677.     strcpy(mext,"*");
  1678.  
  1679.   /* expand *'s */
  1680.   expand_one(mbeg,8);
  1681.   if (*mext)
  1682.     expand_one(mext,3);
  1683.  
  1684.   pstrcpy(Mask,dirpart);
  1685.   if (*dirpart || absolute) strcat(Mask,"\\");
  1686.   strcat(Mask,mbeg);
  1687.   strcat(Mask,".");
  1688.   strcat(Mask,mext);
  1689.  
  1690.   DEBUG(6,("Mask expanded to [%s]\n",Mask));
  1691. }  
  1692.  
  1693.  
  1694. /****************************************************************************
  1695. does a string have any uppercase chars in it?
  1696. ****************************************************************************/
  1697. BOOL strhasupper(char *s)
  1698. {
  1699.   while (*s) 
  1700.     {
  1701. #if defined(KANJI) && !defined(KANJI_WIN95_COMPATIBILITY)
  1702.   /* Win95 treats full width ascii characters as case sensitive. */
  1703.     if (is_shift_jis (*s)) {
  1704.         s += 2;
  1705.     } else if (is_kana (*s)) {
  1706.         s++;
  1707.     } else {
  1708.         if (isupper(*s)) return(True);
  1709.         s++;
  1710.     }
  1711. #else /* KANJI */
  1712.       if (isupper(*s)) return(True);
  1713.       s++;
  1714. #endif /* KANJI */
  1715.     }
  1716.   return(False);
  1717. }
  1718.  
  1719. /****************************************************************************
  1720. does a string have any lowercase chars in it?
  1721. ****************************************************************************/
  1722. BOOL strhaslower(char *s)
  1723. {
  1724.   while (*s) 
  1725.     {
  1726. #if defined(KANJI) && !defined(KANJI_WIN95_COMPATIBILITY)
  1727.   /* Win95 treats full width ascii characters as case sensitive. */
  1728.     if (is_shift_jis (*s)) {
  1729.         if (is_sj_upper (s[0], s[1])) return(True);
  1730.         if (is_sj_lower (s[0], s[1])) return (True);
  1731.         s += 2;
  1732.     } else if (is_kana (*s)) {
  1733.         s++;
  1734.     } else {
  1735.         if (islower(*s)) return(True);
  1736.         s++;
  1737.     }
  1738. #else /* KANJI */
  1739.       if (islower(*s)) return(True);
  1740.       s++;
  1741. #endif /* KANJI */
  1742.     }
  1743.   return(False);
  1744. }
  1745.  
  1746. /****************************************************************************
  1747. find the number of chars in a string
  1748. ****************************************************************************/
  1749. int count_chars(char *s,char c)
  1750. {
  1751.   int count=0;
  1752. #if defined(KANJI) && !defined(KANJI_WIN95_COMPATIBILITY)
  1753.   /* Win95 treats full width ascii characters as case sensitive. */
  1754.   while (*s) 
  1755.     {
  1756.     if (is_shift_jis (*s))
  1757.       s += 2;
  1758.     else 
  1759.     {
  1760.       if (*s == c)
  1761.     count++;
  1762.       s++;
  1763.     }
  1764.   }
  1765. #else /* KANJI */
  1766.   while (*s) 
  1767.     {
  1768.       if (*s == c)
  1769.     count++;
  1770.       s++;
  1771.     }
  1772. #endif /* KANJI */
  1773.   return(count);
  1774. }
  1775.  
  1776.  
  1777. /****************************************************************************
  1778.   make a dir struct
  1779. ****************************************************************************/
  1780. void make_dir_struct(char *buf,char *mask,char *fname,unsigned int size,int mode,time_t date)
  1781. {  
  1782.   char *p;
  1783.   pstring mask2;
  1784.  
  1785.   pstrcpy(mask2,mask);
  1786.  
  1787.   if ((mode & aDIR) != 0)
  1788.     size = 0;
  1789.  
  1790.   memset(buf+1,' ',11);
  1791.   if ((p = strchr(mask2,'.')) != NULL)
  1792.     {
  1793.       *p = 0;
  1794.       memcpy(buf+1,mask2,MIN(strlen(mask2),8));
  1795.       memcpy(buf+9,p+1,MIN(strlen(p+1),3));
  1796.       *p = '.';
  1797.     }
  1798.   else
  1799.     memcpy(buf+1,mask2,MIN(strlen(mask2),11));
  1800.  
  1801.   bzero(buf+21,DIR_STRUCT_SIZE-21);
  1802.   CVAL(buf,21) = mode;
  1803.   put_dos_date(buf,22,date);
  1804.   SSVAL(buf,26,size & 0xFFFF);
  1805.   SSVAL(buf,28,size >> 16);
  1806.   StrnCpy(buf+30,fname,12);
  1807.   if (!case_sensitive)
  1808.     strupper(buf+30);
  1809.   DEBUG(8,("put name [%s] into dir struct\n",buf+30));
  1810. }
  1811.  
  1812.  
  1813. /*******************************************************************
  1814. close the low 3 fd's and open dev/null in their place
  1815. ********************************************************************/
  1816. void close_low_fds(void)
  1817. {
  1818.   int fd;
  1819.   int i;
  1820.   close(0); close(1); close(2);
  1821.   /* try and use up these file descriptors, so silly
  1822.      library routines writing to stdout etc won't cause havoc */
  1823.   for (i=0;i<3;i++) {
  1824.     fd = open("/dev/null",O_RDWR,0);
  1825.     if (fd < 0) fd = open("/dev/null",O_WRONLY,0);
  1826.     if (fd < 0) {
  1827.       DEBUG(0,("Can't open /dev/null\n"));
  1828.       return;
  1829.     }
  1830.     if (fd != i) {
  1831.       DEBUG(0,("Didn't get file descriptor %d\n",i));
  1832.       return;
  1833.     }
  1834.   }
  1835. }
  1836.  
  1837. /****************************************************************************
  1838. Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
  1839. else
  1840. if SYSV use O_NDELAY
  1841. if BSD use FNDELAY
  1842. ****************************************************************************/
  1843. int set_blocking(int fd, BOOL set)
  1844. {
  1845.   int val;
  1846. #ifdef O_NONBLOCK
  1847. #define FLAG_TO_SET O_NONBLOCK
  1848. #else
  1849. #ifdef SYSV
  1850. #define FLAG_TO_SET O_NDELAY
  1851. #else /* BSD */
  1852. #define FLAG_TO_SET FNDELAY
  1853. #endif
  1854. #endif
  1855.  
  1856.   if((val = fcntl(fd, F_GETFL, 0)) == -1)
  1857.     return -1;
  1858.   if(set) /* Turn blocking on - ie. clear nonblock flag */
  1859.     val &= ~FLAG_TO_SET;
  1860.   else
  1861.     val |= FLAG_TO_SET;
  1862.   return fcntl( fd, F_SETFL, val);
  1863. #undef FLAG_TO_SET
  1864. }
  1865.  
  1866.  
  1867. /****************************************************************************
  1868. write to a socket
  1869. ****************************************************************************/
  1870. int write_socket(int fd,char *buf,int len)
  1871. {
  1872.   int ret=0;
  1873.  
  1874.   if (passive)
  1875.     return(len);
  1876.   DEBUG(6,("write_socket(%d,%d)\n",fd,len));
  1877.   ret = write_data(fd,buf,len);
  1878.       
  1879.   DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,len,ret));
  1880.   return(ret);
  1881. }
  1882.  
  1883. /****************************************************************************
  1884. read from a socket
  1885. ****************************************************************************/
  1886. int read_udp_socket(int fd,char *buf,int len)
  1887. {
  1888.   int ret;
  1889.   struct sockaddr sock;
  1890.   int socklen;
  1891.   
  1892.   socklen = sizeof(sock);
  1893.   bzero((char *)&sock,socklen);
  1894.   bzero((char *)&lastip,sizeof(lastip));
  1895.   ret = recvfrom(fd,buf,len,0,&sock,&socklen);
  1896.   if (ret <= 0) {
  1897.     DEBUG(2,("read socket failed. ERRNO=%d\n",errno));
  1898.     return(0);
  1899.   }
  1900.  
  1901.   lastip = *(struct in_addr *) &sock.sa_data[2];
  1902.   lastport = ntohs(((struct sockaddr_in *)&sock)->sin_port);
  1903.  
  1904.   return(ret);
  1905. }
  1906.  
  1907. /****************************************************************************
  1908. read data from a device with a timout in msec.
  1909. mincount = if timeout, minimum to read before returning
  1910. maxcount = number to be read.
  1911. ****************************************************************************/
  1912. int read_with_timeout(int fd,char *buf,int mincnt,int maxcnt,long time_out)
  1913. {
  1914.   fd_set fds;
  1915.   int selrtn;
  1916.   int readret;
  1917.   int nread = 0;
  1918.   struct timeval timeout;
  1919.  
  1920.   /* just checking .... */
  1921.   if (maxcnt <= 0) return(0);
  1922.  
  1923.   smb_read_error = 0;
  1924.  
  1925.   /* Blocking read */
  1926.   if (time_out <= 0) {
  1927.     if (mincnt == 0) mincnt = maxcnt;
  1928.  
  1929.     while (nread < mincnt) {
  1930.       readret = read(fd, buf + nread, maxcnt - nread);
  1931.       if (readret == 0) {
  1932.     smb_read_error = READ_EOF;
  1933.     return -1;
  1934.       }
  1935.  
  1936.       if (readret == -1) {
  1937.     smb_read_error = READ_ERROR;
  1938.     return -1;
  1939.       }
  1940.       nread += readret;
  1941.     }
  1942.     return(nread);
  1943.   }
  1944.   
  1945.   /* Most difficult - timeout read */
  1946.   /* If this is ever called on a disk file and 
  1947.      mincnt is greater then the filesize then
  1948.      system performance will suffer severely as 
  1949.      select always return true on disk files */
  1950.  
  1951.   /* Set initial timeout */
  1952.   timeout.tv_sec = time_out / 1000;
  1953.   timeout.tv_usec = 1000 * (time_out % 1000);
  1954.  
  1955.   for (nread=0; nread<mincnt; ) 
  1956.     {      
  1957.       FD_ZERO(&fds);
  1958.       FD_SET(fd,&fds);
  1959.       
  1960.       selrtn = sys_select(&fds,&timeout);
  1961.  
  1962.       /* Check if error */
  1963.       if(selrtn == -1) {
  1964.     /* something is wrong. Maybe the socket is dead? */
  1965.     smb_read_error = READ_ERROR;
  1966.     return -1;
  1967.       }
  1968.       
  1969.       /* Did we timeout ? */
  1970.       if (selrtn == 0) {
  1971.     smb_read_error = READ_TIMEOUT;
  1972.     return -1;
  1973.       }
  1974.       
  1975.       readret = read(fd, buf+nread, maxcnt-nread);
  1976.       if (readret == 0) {
  1977.     /* we got EOF on the file descriptor */
  1978.     smb_read_error = READ_EOF;
  1979.     return -1;
  1980.       }
  1981.  
  1982.       if (readret == -1) {
  1983.     /* the descriptor is probably dead */
  1984.     smb_read_error = READ_ERROR;
  1985.     return -1;
  1986.       }
  1987.       
  1988.       nread += readret;
  1989.     }
  1990.  
  1991.   /* Return the number we got */
  1992.   return(nread);
  1993. }
  1994.  
  1995. /****************************************************************************
  1996. read data from the client. Maxtime is in milliseconds
  1997. ****************************************************************************/
  1998. int read_max_udp(int fd,char *buffer,int bufsize,int maxtime)
  1999. {
  2000.   fd_set fds;
  2001.   int selrtn;
  2002.   int nread;
  2003.   struct timeval timeout;
  2004.  
  2005.   FD_ZERO(&fds);
  2006.   FD_SET(fd,&fds);
  2007.  
  2008.   timeout.tv_sec = maxtime / 1000;
  2009.   timeout.tv_usec = (maxtime % 1000) * 1000;
  2010.  
  2011.   selrtn = sys_select(&fds,maxtime>0?&timeout:NULL);
  2012.  
  2013.   if (!FD_ISSET(fd,&fds))
  2014.     return 0;
  2015.  
  2016.   nread = read_udp_socket(fd, buffer, bufsize);
  2017.  
  2018.   /* return the number got */
  2019.   return(nread);
  2020. }
  2021.  
  2022. /*******************************************************************
  2023. find the difference in milliseconds between two struct timeval
  2024. values
  2025. ********************************************************************/
  2026. int TvalDiff(struct timeval *tvalold,struct timeval *tvalnew)
  2027. {
  2028.   return((tvalnew->tv_sec - tvalold->tv_sec)*1000 + 
  2029.      ((int)tvalnew->tv_usec - (int)tvalold->tv_usec)/1000);     
  2030. }
  2031.  
  2032. /****************************************************************************
  2033. send a keepalive packet (rfc1002)
  2034. ****************************************************************************/
  2035. BOOL send_keepalive(int client)
  2036. {
  2037.   unsigned char buf[4];
  2038.  
  2039.   buf[0] = 0x85;
  2040.   buf[1] = buf[2] = buf[3] = 0;
  2041.  
  2042.   return(write_data(client,(char *)buf,4) == 4);
  2043. }
  2044.  
  2045.  
  2046.  
  2047. /****************************************************************************
  2048.   read data from the client, reading exactly N bytes. 
  2049. ****************************************************************************/
  2050. int read_data(int fd,char *buffer,int N)
  2051. {
  2052.   int  ret;
  2053.   int total=0;  
  2054.  
  2055.   smb_read_error = 0;
  2056.  
  2057.   while (total < N)
  2058.     {
  2059.       ret = read(fd,buffer + total,N - total);
  2060.       if (ret == 0) {
  2061.     smb_read_error = READ_EOF;
  2062.     return 0;
  2063.       }
  2064.       if (ret == -1) {
  2065.     smb_read_error = READ_ERROR;
  2066.     return -1;
  2067.       }
  2068.       total += ret;
  2069.     }
  2070.   return total;
  2071. }
  2072.  
  2073.  
  2074. /****************************************************************************
  2075.   write data to a fd 
  2076. ****************************************************************************/
  2077. int write_data(int fd,char *buffer,int N)
  2078. {
  2079.   int total=0;
  2080.   int ret;
  2081.  
  2082.   while (total < N)
  2083.     {
  2084.       ret = write(fd,buffer + total,N - total);
  2085.  
  2086.       if (ret == -1) return -1;
  2087.       if (ret == 0) return total;
  2088.  
  2089.       total += ret;
  2090.     }
  2091.   return total;
  2092. }
  2093.  
  2094.  
  2095. /****************************************************************************
  2096. transfer some data between two fd's
  2097. ****************************************************************************/
  2098. int transfer_file(int infd,int outfd,int n,char *header,int headlen,int align)
  2099. {
  2100.   static char *buf=NULL;  
  2101.   static int size=0;
  2102.   char *buf1,*abuf;
  2103.   int total = 0;
  2104.  
  2105.   DEBUG(4,("transfer_file %d  (head=%d) called\n",n,headlen));
  2106.  
  2107.   if (size == 0) {
  2108.     size = lp_readsize();
  2109.     size = MAX(size,1024);
  2110.   }
  2111.  
  2112.   while (!buf && size>0) {
  2113.     buf = (char *)Realloc(buf,size+8);
  2114.     if (!buf) size /= 2;
  2115.   }
  2116.  
  2117.   if (!buf) {
  2118.     DEBUG(0,("Can't allocate transfer buffer!\n"));
  2119.     exit(1);
  2120.   }
  2121.  
  2122.   abuf = buf + (align%8);
  2123.  
  2124.   if (header)
  2125.     n += headlen;
  2126.  
  2127.   while (n > 0)
  2128.     {
  2129.       int s = MIN(n,size);
  2130.       int ret,ret2=0;
  2131.  
  2132.       ret = 0;
  2133.  
  2134.       if (header && (headlen >= MIN(s,1024))) {
  2135.     buf1 = header;
  2136.     s = headlen;
  2137.     ret = headlen;
  2138.     headlen = 0;
  2139.     header = NULL;
  2140.       } else {
  2141.     buf1 = abuf;
  2142.       }
  2143.  
  2144.       if (header && headlen > 0)
  2145.     {
  2146.       ret = MIN(headlen,size);
  2147.       memcpy(buf1,header,ret);
  2148.       headlen -= ret;
  2149.       header += ret;
  2150.       if (headlen <= 0) header = NULL;
  2151.     }
  2152.  
  2153.       if (s > ret)
  2154.     ret += read(infd,buf1+ret,s-ret);
  2155.  
  2156.       if (ret > 0)
  2157.     {
  2158.       ret2 = (outfd>=0?write_data(outfd,buf1,ret):ret);
  2159.       if (ret2 > 0) total += ret2;
  2160.       /* if we can't write then dump excess data */
  2161.       if (ret2 != ret)
  2162.         transfer_file(infd,-1,n-(ret+headlen),NULL,0,0);
  2163.     }
  2164.       if (ret <= 0 || ret2 != ret)
  2165.     return(total);
  2166.       n -= ret;
  2167.     }
  2168.   return(total);
  2169. }
  2170.  
  2171.  
  2172. /****************************************************************************
  2173. read 4 bytes of a smb packet and return the smb length of the packet
  2174. possibly store the result in the buffer
  2175. ****************************************************************************/
  2176. int read_smb_length(int fd,char *inbuf,int timeout)
  2177. {
  2178.   char *buffer;
  2179.   char buf[4];
  2180.   int len=0, msg_type;
  2181.   BOOL ok=False;
  2182.  
  2183.   if (inbuf)
  2184.     buffer = inbuf;
  2185.   else
  2186.     buffer = buf;
  2187.  
  2188.   while (!ok)
  2189.     {
  2190.       if (timeout > 0)
  2191.     ok = (read_with_timeout(fd,buffer,4,4,timeout) == 4);
  2192.       else 
  2193.     ok = (read_data(fd,buffer,4) == 4);
  2194.  
  2195.       if (!ok)
  2196.     return(-1);
  2197.  
  2198.       len = smb_len(buffer);
  2199.       msg_type = CVAL(buffer,0);
  2200.  
  2201.       if (msg_type == 0x85) 
  2202.     {
  2203.       DEBUG(5,("Got keepalive packet\n"));
  2204.       ok = False;
  2205.     }
  2206.     }
  2207.  
  2208.   DEBUG(10,("got smb length of %d\n",len));
  2209.  
  2210.   return(len);
  2211. }
  2212.  
  2213.  
  2214.  
  2215. /****************************************************************************
  2216.   read an smb from a fd and return it's length
  2217. The timeout is in milli seconds
  2218. ****************************************************************************/
  2219. BOOL receive_smb(int fd,char *buffer,int timeout)
  2220. {
  2221.   int len,ret;
  2222.  
  2223.   smb_read_error = 0;
  2224.  
  2225.   bzero(buffer,smb_size + 100);
  2226.  
  2227.   len = read_smb_length(fd,buffer,timeout);
  2228.   if (len == -1)
  2229.     return(False);
  2230.  
  2231.   if (len > BUFFER_SIZE) {
  2232.     DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
  2233.     if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
  2234.       exit(1);
  2235.   }
  2236.  
  2237.   ret = read_data(fd,buffer+4,len);
  2238.   if (ret != len) {
  2239.     smb_read_error = READ_ERROR;
  2240.     return False;
  2241.   }
  2242.  
  2243.   return(True);
  2244. }
  2245.  
  2246.  
  2247. /****************************************************************************
  2248.   send an smb to a fd 
  2249. ****************************************************************************/
  2250. BOOL send_smb(int fd,char *buffer)
  2251. {
  2252.   int len;
  2253.   int ret,nwritten=0;
  2254.   len = smb_len(buffer) + 4;
  2255.  
  2256.   while (nwritten < len)
  2257.     {
  2258.       ret = write_socket(fd,buffer+nwritten,len - nwritten);
  2259.       if (ret <= 0)
  2260.     {
  2261.       DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
  2262.           close_sockets();
  2263.       exit(1);
  2264.     }
  2265.       nwritten += ret;
  2266.     }
  2267.  
  2268.  
  2269.   return True;
  2270. }
  2271.  
  2272.  
  2273. /****************************************************************************
  2274. find a pointer to a netbios name
  2275. ****************************************************************************/
  2276. char *name_ptr(char *buf,int ofs)
  2277. {
  2278.   unsigned char c = *(unsigned char *)(buf+ofs);
  2279.  
  2280.   if ((c & 0xC0) == 0xC0)
  2281.     {
  2282.       uint16 l;
  2283.       char p[2];
  2284.       memcpy(p,buf+ofs,2);
  2285.       p[0] &= ~0xC0;
  2286.       l = RSVAL(p,0);
  2287.       DEBUG(5,("name ptr to pos %d from %d is %s\n",l,ofs,buf+l));
  2288.       return(buf + l);
  2289.     }
  2290.   else
  2291.     return(buf+ofs);
  2292. }  
  2293.  
  2294. /****************************************************************************
  2295. extract a netbios name from a buf
  2296. ****************************************************************************/
  2297. int name_extract(char *buf,int ofs,char *name)
  2298. {
  2299.   char *p = name_ptr(buf,ofs);
  2300.   int d = PTR_DIFF(p,buf+ofs);
  2301.   strcpy(name,"");
  2302.   if (d < -50 || d > 50) return(0);
  2303.   return(name_interpret(p,name));
  2304. }  
  2305.   
  2306.  
  2307. /****************************************************************************
  2308. return the total storage length of a mangled name
  2309. ****************************************************************************/
  2310. int name_len(char *s)
  2311. {
  2312.   char *s0=s;
  2313.   unsigned char c = *(unsigned char *)s;
  2314.   if ((c & 0xC0) == 0xC0)
  2315.     return(2);
  2316.   while (*s) s += (*s)+1;
  2317.   return(PTR_DIFF(s,s0)+1);
  2318. }
  2319.  
  2320. /****************************************************************************
  2321. send a single packet to a port on another machine
  2322. ****************************************************************************/
  2323. BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
  2324. {
  2325.   BOOL ret;
  2326.   int out_fd;
  2327.   struct sockaddr_in sock_out;
  2328.  
  2329.   if (passive)
  2330.     return(True);
  2331.  
  2332.   /* create a socket to write to */
  2333.   out_fd = socket(AF_INET, type, 0);
  2334.   if (out_fd == -1) 
  2335.     {
  2336.       DEBUG(0,("socket failed"));
  2337.       return False;
  2338.     }
  2339.  
  2340.   /* set the address and port */
  2341.   bzero((char *)&sock_out,sizeof(sock_out));
  2342.   putip((char *)&sock_out.sin_addr,(char *)&ip);
  2343.   sock_out.sin_port = htons( port );
  2344.   sock_out.sin_family = AF_INET;
  2345.   
  2346.   if (DEBUGLEVEL > 0)
  2347.     DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
  2348.          len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
  2349.     
  2350.   /* send it */
  2351.   ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
  2352.  
  2353.   if (!ret)
  2354.     DEBUG(0,("Packet send to %s(%d) failed ERRNO=%d\n",
  2355.          inet_ntoa(ip),port,errno));
  2356.  
  2357.   close(out_fd);
  2358.   return(ret);
  2359. }
  2360.  
  2361. /*******************************************************************
  2362. sleep for a specified number of milliseconds
  2363. ********************************************************************/
  2364. void msleep(int t)
  2365. {
  2366.   int tdiff=0;
  2367.   struct timeval tval,t1,t2;  
  2368.   fd_set fds;
  2369.  
  2370.   GetTimeOfDay(&t1);
  2371.   GetTimeOfDay(&t2);
  2372.   
  2373.   while (tdiff < t) {
  2374.     tval.tv_sec = (t-tdiff)/1000;
  2375.     tval.tv_usec = 1000*((t-tdiff)%1000);
  2376.  
  2377.     FD_ZERO(&fds);
  2378.     errno = 0;
  2379.     sys_select(&fds,&tval);
  2380.  
  2381.     GetTimeOfDay(&t2);
  2382.     tdiff = TvalDiff(&t1,&t2);
  2383.   }
  2384. }
  2385.  
  2386. /****************************************************************************
  2387. check if a string is part of a list
  2388. ****************************************************************************/
  2389. BOOL in_list(char *s,char *list,BOOL casesensitive)
  2390. {
  2391.   pstring tok;
  2392.   char *p=list;
  2393.  
  2394.   if (!list) return(False);
  2395.  
  2396.   while (next_token(&p,tok,LIST_SEP))
  2397.     {
  2398.       if (casesensitive) {
  2399.     if (strcmp(tok,s) == 0)
  2400.       return(True);
  2401.       } else {
  2402.     if (StrCaseCmp(tok,s) == 0)
  2403.       return(True);
  2404.       }
  2405.     }
  2406.   return(False);
  2407. }
  2408.  
  2409. /* this is used to prevent lots of mallocs of size 1 */
  2410. static char *null_string = NULL;
  2411.  
  2412. /****************************************************************************
  2413. set a string value, allocing the space for the string
  2414. ****************************************************************************/
  2415. BOOL string_init(char **dest,char *src)
  2416. {
  2417.   int l;
  2418.   if (!src)     
  2419.     src = "";
  2420.  
  2421.   l = strlen(src);
  2422.  
  2423.   if (l == 0)
  2424.     {
  2425.       if (!null_string)
  2426.     null_string = (char *)malloc(1);
  2427.  
  2428.       *null_string = 0;
  2429.       *dest = null_string;
  2430.     }
  2431.   else
  2432.     {
  2433.       (*dest) = (char *)malloc(l+1);
  2434.       if ((*dest) == NULL) {
  2435.           DEBUG(0,("Out of memory in string_init\n"));
  2436.           return False;
  2437.       }
  2438.  
  2439.       strcpy(*dest,src);
  2440.     }
  2441.   return(True);
  2442. }
  2443.  
  2444. /****************************************************************************
  2445. free a string value
  2446. ****************************************************************************/
  2447. void string_free(char **s)
  2448. {
  2449.   if (!s || !(*s)) return;
  2450.   if (*s == null_string)
  2451.     *s = NULL;
  2452.   if (*s) free(*s);
  2453.   *s = NULL;
  2454. }
  2455.  
  2456. /****************************************************************************
  2457. set a string value, allocing the space for the string, and deallocating any 
  2458. existing space
  2459. ****************************************************************************/
  2460. BOOL string_set(char **dest,char *src)
  2461. {
  2462.   string_free(dest);
  2463.  
  2464.   return(string_init(dest,src));
  2465. }
  2466.  
  2467. /****************************************************************************
  2468. substitute a string for a pattern in another string. Make sure there is 
  2469. enough room!
  2470.  
  2471. This routine looks for pattern in s and replaces it with 
  2472. insert. It may do multiple replacements.
  2473.  
  2474. return True if a substitution was done.
  2475. ****************************************************************************/
  2476. BOOL string_sub(char *s,char *pattern,char *insert)
  2477. {
  2478.   BOOL ret = False;
  2479.   char *p;
  2480.   int ls,lp,li;
  2481.  
  2482.   if (!insert || !pattern || !s) return(False);
  2483.  
  2484.   ls = strlen(s);
  2485.   lp = strlen(pattern);
  2486.   li = strlen(insert);
  2487.  
  2488.   if (!*pattern) return(False);
  2489.  
  2490.   while (lp <= ls && (p = strstr(s,pattern)))
  2491.     {
  2492.       ret = True;
  2493.       memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
  2494.       memcpy(p,insert,li);
  2495.       s = p + li;
  2496.       ls = strlen(s);
  2497.     }
  2498.   return(ret);
  2499. }
  2500.  
  2501.  
  2502.  
  2503. /*********************************************************
  2504. * Recursive routine that is called by mask_match.
  2505. * Does the actual matching.
  2506. *********************************************************/
  2507. BOOL do_match(char *str, char *regexp, int case_sig)
  2508. {
  2509.   char *p;
  2510.  
  2511.   for( p = regexp; *p && *str; ) {
  2512.     switch(*p) {
  2513.     case '?':
  2514.       str++; p++;
  2515.       break;
  2516.  
  2517.     case '*':
  2518.       /* Look for a character matching 
  2519.      the one after the '*' */
  2520.       p++;
  2521.       if(!*p)
  2522.     return True; /* Automatic match */
  2523.       while(*str) {
  2524.     while(*str && (case_sig ? (*p != *str) : (toupper(*p)!=toupper(*str))))
  2525.       str++;
  2526.     if(do_match(str,p,case_sig))
  2527.       return True;
  2528.     if(!*str)
  2529.       return False;
  2530.     else
  2531.       str++;
  2532.       }
  2533.       return False;
  2534.  
  2535.     default:
  2536.       if(case_sig) {
  2537.     if(*str != *p)
  2538.       return False;
  2539.       } else {
  2540.     if(toupper(*str) != toupper(*p))
  2541.       return False;
  2542.       }
  2543.       str++, p++;
  2544.       break;
  2545.     }
  2546.   }
  2547.   if(!*p && !*str)
  2548.     return True;
  2549.  
  2550.   if (!*p && str[0] == '.' && str[1] == 0)
  2551.     return(True);
  2552.   
  2553.   if (!*str && *p == '?')
  2554.     {
  2555.       while (*p == '?') p++;
  2556.       return(!*p);
  2557.     }
  2558.  
  2559.   if(!*str && (*p == '*' && p[1] == '\0'))
  2560.     return True;
  2561.   return False;
  2562. }
  2563.  
  2564.  
  2565. /*********************************************************
  2566. * Routine to match a given string with a regexp - uses
  2567. * simplified regexp that takes * and ? only. Case can be
  2568. * significant or not.
  2569. *********************************************************/
  2570. BOOL mask_match(char *str, char *regexp, int case_sig,BOOL trans2)
  2571. {
  2572.   char *p;
  2573.   pstring p1, p2;
  2574.   fstring ebase,eext,sbase,sext;
  2575.  
  2576.   BOOL matched;
  2577.  
  2578.   /* Make local copies of str and regexp */
  2579.   StrnCpy(p1,regexp,sizeof(pstring)-1);
  2580.   StrnCpy(p2,str,sizeof(pstring)-1);
  2581.  
  2582.   if (!strchr(p2,'.')) {
  2583.     strcat(p2,".");
  2584.   }
  2585.  
  2586. /*
  2587.   if (!strchr(p1,'.')) {
  2588.     strcat(p1,".");
  2589.   }
  2590. */
  2591.  
  2592. #if 0
  2593.   if (strchr(p1,'.'))
  2594.     {
  2595.       string_sub(p1,"*.*","*");
  2596.       string_sub(p1,".*","*");
  2597.     }
  2598. #endif
  2599.  
  2600.   /* Remove any *? and ** as they are meaningless */
  2601.   for(p = p1; *p; p++)
  2602.     while( *p == '*' && (p[1] == '?' ||p[1] == '*'))
  2603.       (void)strcpy( &p[1], &p[2]);
  2604.  
  2605.   if (strequal(p1,"*")) return(True);
  2606.  
  2607.   DEBUG(5,("mask_match str=<%s> regexp=<%s>, case_sig = %d\n", p2, p1, case_sig));
  2608.  
  2609.   if (trans2) {
  2610.     fstrcpy(ebase,p1);
  2611.     fstrcpy(sbase,p2);
  2612.   } else {
  2613.     if ((p=strrchr(p1,'.'))) {
  2614.       *p = 0;
  2615.       fstrcpy(ebase,p1);
  2616.       fstrcpy(eext,p+1);
  2617.     } else {
  2618.       fstrcpy(ebase,p1);
  2619.       eext[0] = 0;
  2620.     }
  2621.  
  2622.   if (!strequal(p2,".") && !strequal(p2,"..") && (p=strrchr(p2,'.'))) {
  2623.     *p = 0;
  2624.     fstrcpy(sbase,p2);
  2625.     fstrcpy(sext,p+1);
  2626.   } else {
  2627.     fstrcpy(sbase,p2);
  2628.     fstrcpy(sext,"");
  2629.   }
  2630.   }
  2631.  
  2632.   matched = do_match(sbase,ebase,case_sig) && 
  2633.     (trans2 || do_match(sext,eext,case_sig));
  2634.  
  2635.   DEBUG(5,("mask_match returning %d\n", matched));
  2636.  
  2637.   return matched;
  2638. }
  2639.  
  2640.  
  2641.  
  2642. /****************************************************************************
  2643. become a daemon, discarding the controlling terminal
  2644. ****************************************************************************/
  2645. void become_daemon(void)
  2646. {
  2647. #ifndef NO_FORK_DEBUG
  2648.   if (fork())
  2649.     exit(0);
  2650.  
  2651.   /* detach from the terminal */
  2652. #ifdef USE_SETSID
  2653.   setsid();
  2654. #else /* USE_SETSID */
  2655. #ifdef TIOCNOTTY
  2656.   {
  2657.     int i = open("/dev/tty", O_RDWR);
  2658.     if (i >= 0) 
  2659.       {
  2660.     ioctl(i, (int) TIOCNOTTY, (char *)0);      
  2661.     close(i);
  2662.       }
  2663.   }
  2664. #endif /* TIOCNOTTY */
  2665. #endif /* USE_SETSID */
  2666.   /* Close fd's 0,1,2. Needed if started by rsh */
  2667.   close_low_fds();
  2668. #endif /* NO_FORK_DEBUG */
  2669. }
  2670.  
  2671.  
  2672. /****************************************************************************
  2673. put up a yes/no prompt
  2674. ****************************************************************************/
  2675. BOOL yesno(char *p)
  2676. {
  2677.   pstring ans;
  2678.   printf("%s",p);
  2679.  
  2680.   if (!fgets(ans,sizeof(ans)-1,stdin))
  2681.     return(False);
  2682.  
  2683.   if (*ans == 'y' || *ans == 'Y')
  2684.     return(True);
  2685.  
  2686.   return(False);
  2687. }
  2688.  
  2689. /****************************************************************************
  2690. read a line from a file with possible \ continuation chars. 
  2691. Blanks at the start or end of a line are stripped.
  2692. The string will be allocated if s2 is NULL
  2693. ****************************************************************************/
  2694. char *fgets_slash(char *s2,int maxlen,FILE *f)
  2695. {
  2696.   char *s=s2;
  2697.   int len = 0;
  2698.   int c;
  2699.   BOOL start_of_line = True;
  2700.  
  2701.   if (feof(f))
  2702.     return(NULL);
  2703.  
  2704.   if (!s2)
  2705.     {
  2706.       maxlen = MIN(maxlen,8);
  2707.       s = (char *)Realloc(s,maxlen);
  2708.     }
  2709.  
  2710.   if (!s || maxlen < 2) return(NULL);
  2711.  
  2712.   *s = 0;
  2713.  
  2714.   while (len < maxlen-1)
  2715.     {
  2716.       c = getc(f);
  2717.       switch (c)
  2718.     {
  2719.     case '\r':
  2720.       break;
  2721.     case '\n':
  2722.       while (len > 0 && s[len-1] == ' ')
  2723.         {
  2724.           s[--len] = 0;
  2725.         }
  2726.       if (len > 0 && s[len-1] == '\\')
  2727.         {
  2728.           s[--len] = 0;
  2729.           start_of_line = True;
  2730.           break;
  2731.         }
  2732.       return(s);
  2733.     case EOF:
  2734.       if (len <= 0 && !s2) 
  2735.         free(s);
  2736.       return(len>0?s:NULL);
  2737.     case ' ':
  2738.       if (start_of_line)
  2739.         break;
  2740.     default:
  2741.       start_of_line = False;
  2742.       s[len++] = c;
  2743.       s[len] = 0;
  2744.     }
  2745.       if (!s2 && len > maxlen-3)
  2746.     {
  2747.       maxlen *= 2;
  2748.       s = (char *)Realloc(s,maxlen);
  2749.       if (!s) return(NULL);
  2750.     }
  2751.     }
  2752.   return(s);
  2753. }
  2754.  
  2755.  
  2756.  
  2757. /****************************************************************************
  2758. set the length of a file from a filedescriptor.
  2759. Returns 0 on success, -1 on failure.
  2760. ****************************************************************************/
  2761. int set_filelen(int fd, long len)
  2762. {
  2763. /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
  2764.    extend a file with ftruncate. Provide alternate implementation
  2765.    for this */
  2766.  
  2767. #if FTRUNCATE_CAN_EXTEND
  2768.   return ftruncate(fd, len);
  2769. #else
  2770.   struct stat st;
  2771.   char c = 0;
  2772.   long currpos = lseek(fd, 0L, SEEK_CUR);
  2773.  
  2774.   if(currpos < 0)
  2775.     return -1;
  2776.   /* Do an fstat to see if the file is longer than
  2777.      the requested size (call ftruncate),
  2778.      or shorter, in which case seek to len - 1 and write 1
  2779.      byte of zero */
  2780.   if(fstat(fd, &st)<0)
  2781.     return -1;
  2782.  
  2783. #ifdef S_ISFIFO
  2784.   if (S_ISFIFO(st.st_mode)) return 0;
  2785. #endif
  2786.  
  2787.   if(st.st_size == len)
  2788.     return 0;
  2789.   if(st.st_size > len)
  2790.     return ftruncate(fd, len);
  2791.  
  2792.   if(lseek(fd, len-1, SEEK_SET) != len -1)
  2793.     return -1;
  2794.   if(write(fd, &c, 1)!=1)
  2795.     return -1;
  2796.   /* Seek to where we were */
  2797.   lseek(fd, currpos, SEEK_SET);
  2798.   return 0;
  2799. #endif
  2800. }
  2801.  
  2802.  
  2803. /****************************************************************************
  2804. return the byte checksum of some data
  2805. ****************************************************************************/
  2806. int byte_checksum(char *buf,int len)
  2807. {
  2808.   unsigned char *p = (unsigned char *)buf;
  2809.   int ret = 0;
  2810.   while (len--)
  2811.     ret += *p++;
  2812.   return(ret);
  2813. }
  2814.  
  2815.  
  2816.  
  2817. #ifdef HPUX
  2818. /****************************************************************************
  2819. this is a version of setbuffer() for those machines that only have setvbuf
  2820. ****************************************************************************/
  2821.  void setbuffer(FILE *f,char *buf,int bufsize)
  2822. {
  2823.   setvbuf(f,buf,_IOFBF,bufsize);
  2824. }
  2825. #endif
  2826.  
  2827.  
  2828. /****************************************************************************
  2829. parse out a directory name from a path name. Assumes dos style filenames.
  2830. ****************************************************************************/
  2831. char *dirname_dos(char *path,char *buf)
  2832. {
  2833.   char *p = strrchr(path,'\\');
  2834.  
  2835.   if (!p)
  2836.     strcpy(buf,path);
  2837.   else
  2838.     {
  2839.       *p = 0;
  2840.       strcpy(buf,path);
  2841.       *p = '\\';
  2842.     }
  2843.  
  2844.   return(buf);
  2845. }
  2846.  
  2847.  
  2848. /****************************************************************************
  2849. parse out a filename from a path name. Assumes dos style filenames.
  2850. ****************************************************************************/
  2851. static char *filename_dos(char *path,char *buf)
  2852. {
  2853.   char *p = strrchr(path,'\\');
  2854.  
  2855.   if (!p)
  2856.     strcpy(buf,path);
  2857.   else
  2858.     strcpy(buf,p+1);
  2859.  
  2860.   return(buf);
  2861. }
  2862.  
  2863.  
  2864.  
  2865. /****************************************************************************
  2866. expand a pointer to be a particular size
  2867. ****************************************************************************/
  2868. void *Realloc(void *p,int size)
  2869. {
  2870.   void *ret=NULL;
  2871.  
  2872.   if (size == 0) {
  2873.     if (p) free(p);
  2874.     DEBUG(5,("Realloc asked for 0 bytes\n"));
  2875.     return NULL;
  2876.   }
  2877.  
  2878.   if (!p)
  2879.     ret = (void *)malloc(size);
  2880.   else
  2881.     ret = (void *)realloc(p,size);
  2882.  
  2883.   if (!ret)
  2884.     DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",size));
  2885.  
  2886.   return(ret);
  2887. }
  2888.  
  2889. #ifdef NOSTRDUP
  2890. /****************************************************************************
  2891. duplicate a string
  2892. ****************************************************************************/
  2893.  char *strdup(char *s)
  2894. {
  2895.   char *ret = NULL;
  2896.   if (!s) return(NULL);
  2897.   ret = (char *)malloc(strlen(s)+1);
  2898.   if (!ret) return(NULL);
  2899.   strcpy(ret,s);
  2900.   return(ret);
  2901. }
  2902. #endif
  2903.  
  2904.  
  2905. /****************************************************************************
  2906.   Signal handler for SIGPIPE (write on a disconnected socket) 
  2907. ****************************************************************************/
  2908. void Abort(void )
  2909. {
  2910.   DEBUG(0,("Probably got SIGPIPE\nExiting\n"));
  2911.   exit(2);
  2912. }
  2913.  
  2914. /****************************************************************************
  2915. get my own name and IP
  2916. ****************************************************************************/
  2917. BOOL get_myname(char *my_name,struct in_addr *ip)
  2918. {
  2919.   struct hostent *hp;
  2920.   pstring hostname;
  2921.  
  2922.   *hostname = 0;
  2923.  
  2924.   /* get my host name */
  2925.   if (gethostname(hostname, MAXHOSTNAMELEN) == -1) 
  2926.     {
  2927.       DEBUG(0,("gethostname failed\n"));
  2928.       return False;
  2929.     } 
  2930.  
  2931.   /* get host info */
  2932.   if ((hp = Get_Hostbyname(hostname)) == 0) 
  2933.     {
  2934.       DEBUG(0,( "Get_Hostbyname: Unknown host %s.\n",hostname));
  2935.       return False;
  2936.     }
  2937.  
  2938.   if (my_name)
  2939.     {
  2940.       /* split off any parts after an initial . */
  2941.       char *p = strchr(hostname,'.');
  2942.       if (p) *p = 0;
  2943.  
  2944.       fstrcpy(my_name,hostname);
  2945.     }
  2946.  
  2947.   if (ip)
  2948.     putip((char *)ip,(char *)hp->h_addr);
  2949.  
  2950.   return(True);
  2951. }
  2952.  
  2953.  
  2954. /****************************************************************************
  2955. true if two IP addresses are equal
  2956. ****************************************************************************/
  2957. BOOL ip_equal(struct in_addr ip1,struct in_addr ip2)
  2958. {
  2959.   uint32 a1,a2;
  2960.   a1 = ntohl(ip1.s_addr);
  2961.   a2 = ntohl(ip2.s_addr);
  2962.   return(a1 == a2);
  2963. }
  2964.  
  2965.  
  2966. /****************************************************************************
  2967. open a socket of the specified type, port and address for incoming data
  2968. ****************************************************************************/
  2969. int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
  2970. {
  2971.   struct hostent *hp;
  2972.   struct sockaddr_in sock;
  2973.   pstring host_name;
  2974.   int res;
  2975.  
  2976.   /* get my host name */
  2977.   if (gethostname(host_name, MAXHOSTNAMELEN) == -1) 
  2978.     { DEBUG(0,("gethostname failed\n")); return -1; } 
  2979.  
  2980.   /* get host info */
  2981.   if ((hp = Get_Hostbyname(host_name)) == 0) 
  2982.     {
  2983.       DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",host_name));
  2984.       return -1;
  2985.     }
  2986.   
  2987.   bzero((char *)&sock,sizeof(sock));
  2988.   memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
  2989. #if defined(__FreeBSD__) || defined(NETBSD) /* XXX not the right ifdef */
  2990.   sock.sin_len = sizeof(sock);
  2991. #endif
  2992.   sock.sin_port = htons( port );
  2993.   sock.sin_family = hp->h_addrtype;
  2994.   sock.sin_addr.s_addr = socket_addr;
  2995.   res = socket(hp->h_addrtype, type, 0);
  2996.   if (res == -1) 
  2997.     { DEBUG(0,("socket failed\n")); return -1; }
  2998.  
  2999.   {
  3000.     int one=1;
  3001.     setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
  3002.   }
  3003.  
  3004.   /* now we've got a socket - we need to bind it */
  3005.   if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) 
  3006.     { 
  3007.       if (port) {
  3008.     if (port == SMB_PORT || port == NMB_PORT)
  3009.       DEBUG(dlevel,("bind failed on port %d socket_addr=%x (%s)\n",
  3010.             port,socket_addr,strerror(errno))); 
  3011.     close(res); 
  3012.  
  3013.     if (dlevel > 0 && port < 1000)
  3014.       port = 7999;
  3015.  
  3016.     if (port >= 1000 && port < 9000)
  3017.       return(open_socket_in(type,port+1,dlevel,socket_addr));
  3018.       }
  3019.  
  3020.       return(-1); 
  3021.     }
  3022.   DEBUG(3,("bind succeeded on port %d\n",port));
  3023.  
  3024.   return res;
  3025. }
  3026.  
  3027.  
  3028. /****************************************************************************
  3029.   create an outgoing socket
  3030.   **************************************************************************/
  3031. int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
  3032. {
  3033.   struct sockaddr_in sock_out;
  3034.   int res,ret;
  3035.   int connect_loop = 250; /* 250 milliseconds */
  3036.   int loops = (timeout * 1000) / connect_loop;
  3037.  
  3038.   /* create a socket to write to */
  3039.   res = socket(PF_INET, type, 0);
  3040.   if (res == -1) 
  3041.     { DEBUG(0,("socket error\n")); return -1; }
  3042.  
  3043. #ifdef BIND_LOCAL_OUTPUT_SOCKET
  3044.   {
  3045.   struct sockaddr_in sock_in;
  3046.   /* Bind the local part of this socket to the address
  3047.      given in the socket address parameter. */
  3048.  
  3049.   bzero((char *)&sock_in,sizeof(sock_in));
  3050.   putip((char *)&sock_in.sin_addr,(char *)interpret_addr2(lp_socket_address()));
  3051.   
  3052.   sock_in.sin_port = 0;
  3053.   sock_in.sin_family = PF_INET;
  3054.  
  3055.   if (bind(res, (struct sockaddr * ) &sock_in,sizeof(sock_in)) < 0)
  3056.     DEBUG(0,("Failed to bind local socket address for output socket. Error was %s\n",
  3057.           strerror(errno)));
  3058.   }
  3059. #endif /* BIND_LOCAL_OUTPUT_SOCKET */
  3060.  
  3061.   if (type != SOCK_STREAM) return(res);
  3062.  
  3063.   bzero((char *)&sock_out,sizeof(sock_out));
  3064.   putip((char *)&sock_out.sin_addr,(char *)addr);
  3065.   
  3066.   sock_out.sin_port = htons( port );
  3067.   sock_out.sin_family = PF_INET;
  3068.  
  3069.   /* set it non-blocking */
  3070.   set_blocking(res,False);
  3071.  
  3072.   DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
  3073.   
  3074.   /* and connect it to the destination */
  3075. connect_again:
  3076.   ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
  3077.  
  3078.   /* Some systems return EAGAIN when they mean EINPROGRESS */
  3079.   if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
  3080.         errno == EAGAIN) && loops--) {
  3081.     msleep(connect_loop);
  3082.     goto connect_again;
  3083.   }
  3084.  
  3085.   if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
  3086.          errno == EAGAIN)) {
  3087.       DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
  3088.       close(res);
  3089.       return -1;
  3090.   }
  3091.  
  3092. #ifdef EISCONN
  3093.   if (ret < 0 && errno == EISCONN) {
  3094.     errno = 0;
  3095.     ret = 0;
  3096.   }
  3097. #endif
  3098.  
  3099.   if (ret < 0) {
  3100.     DEBUG(1,("error connecting to %s:%d (%s)\n",
  3101.          inet_ntoa(*addr),port,strerror(errno)));
  3102.     return -1;
  3103.   }
  3104.  
  3105.   /* set it blocking again */
  3106.   set_blocking(res,True);
  3107.  
  3108.   return res;
  3109. }
  3110.  
  3111.  
  3112. /****************************************************************************
  3113. interpret a protocol description string, with a default
  3114. ****************************************************************************/
  3115. int interpret_protocol(char *str,int def)
  3116. {
  3117.   if (strequal(str,"NT1"))
  3118.     return(PROTOCOL_NT1);
  3119.   if (strequal(str,"LANMAN2"))
  3120.     return(PROTOCOL_LANMAN2);
  3121.   if (strequal(str,"LANMAN1"))
  3122.     return(PROTOCOL_LANMAN1);
  3123.   if (strequal(str,"CORE"))
  3124.     return(PROTOCOL_CORE);
  3125.   if (strequal(str,"COREPLUS"))
  3126.     return(PROTOCOL_COREPLUS);
  3127.   if (strequal(str,"CORE+"))
  3128.     return(PROTOCOL_COREPLUS);
  3129.   
  3130.   DEBUG(0,("Unrecognised protocol level %s\n",str));
  3131.   
  3132.   return(def);
  3133. }
  3134.  
  3135. /****************************************************************************
  3136. interpret a security level
  3137. ****************************************************************************/
  3138. int interpret_security(char *str,int def)
  3139. {
  3140.   if (strequal(str,"SERVER"))
  3141.     return(SEC_SERVER);
  3142.   if (strequal(str,"USER"))
  3143.     return(SEC_USER);
  3144.   if (strequal(str,"SHARE"))
  3145.     return(SEC_SHARE);
  3146.   
  3147.   DEBUG(0,("Unrecognised security level %s\n",str));
  3148.   
  3149.   return(def);
  3150. }
  3151.  
  3152.  
  3153. /****************************************************************************
  3154. interpret an internet address or name into an IP address in 4 byte form
  3155. ****************************************************************************/
  3156. uint32 interpret_addr(char *str)
  3157. {
  3158.   struct hostent *hp;
  3159.   uint32 res;
  3160.   int i;
  3161.   BOOL pure_address = True;
  3162.  
  3163.   if (strcmp(str,"0.0.0.0") == 0) return(0);
  3164.   if (strcmp(str,"255.255.255.255") == 0) return(0xFFFFFFFF);
  3165.  
  3166.   for (i=0; pure_address && str[i]; i++)
  3167.     if (!(isdigit(str[i]) || str[i] == '.')) 
  3168.       pure_address = False;
  3169.  
  3170.   /* if it's in the form of an IP address then get the lib to interpret it */
  3171.   if (pure_address) {
  3172.     res = inet_addr(str);
  3173.   } else {
  3174.     /* otherwise assume it's a network name of some sort and use 
  3175.        Get_Hostbyname */
  3176.     if ((hp = Get_Hostbyname(str)) == 0) {
  3177.       DEBUG(3,("Get_Hostbyname: Unknown host. %s\n",str));
  3178.       return 0;
  3179.     }
  3180.     if(hp->h_addr == NULL) {
  3181.       DEBUG(3,("Get_Hostbyname: host address is invalid for host %s.\n",str));
  3182.       return 0;
  3183.     }
  3184.     putip((char *)&res,(char *)hp->h_addr);
  3185.   }
  3186.  
  3187.   if (res == (uint32)-1) return(0);
  3188.  
  3189.   return(res);
  3190. }
  3191.  
  3192. /*******************************************************************
  3193.   a convenient addition to interpret_addr()
  3194.   ******************************************************************/
  3195. struct in_addr *interpret_addr2(char *str)
  3196. {
  3197.   static struct in_addr ret;
  3198.   uint32 a = interpret_addr(str);
  3199.   ret.s_addr = a;
  3200.   return(&ret);
  3201. }
  3202.  
  3203. /*******************************************************************
  3204.   check if an IP is the 0.0.0.0
  3205.   ******************************************************************/
  3206. BOOL zero_ip(struct in_addr ip)
  3207. {
  3208.   uint32 a;
  3209.   putip((char *)&a,(char *)&ip);
  3210.   return(a == 0);
  3211. }
  3212.  
  3213.  
  3214. /*******************************************************************
  3215.  matchname - determine if host name matches IP address 
  3216.  ******************************************************************/
  3217. static BOOL matchname(char *remotehost,struct in_addr  addr)
  3218. {
  3219.   struct hostent *hp;
  3220.   int     i;
  3221.   
  3222.   if ((hp = Get_Hostbyname(remotehost)) == 0) {
  3223.     DEBUG(0,("Get_Hostbyname(%s): lookup failure", remotehost));
  3224.     return False;
  3225.   } 
  3226.  
  3227.   /*
  3228.    * Make sure that gethostbyname() returns the "correct" host name.
  3229.    * Unfortunately, gethostbyname("localhost") sometimes yields
  3230.    * "localhost.domain". Since the latter host name comes from the
  3231.    * local DNS, we just have to trust it (all bets are off if the local
  3232.    * DNS is perverted). We always check the address list, though.
  3233.    */
  3234.   
  3235.   if (strcasecmp(remotehost, hp->h_name)
  3236.       && strcasecmp(remotehost, "localhost")) {
  3237.     DEBUG(0,("host name/name mismatch: %s != %s",
  3238.          remotehost, hp->h_name));
  3239.     return False;
  3240.   }
  3241.     
  3242.   /* Look up the host address in the address list we just got. */
  3243.   for (i = 0; hp->h_addr_list[i]; i++) {
  3244.     if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
  3245.       return True;
  3246.   }
  3247.  
  3248.   /*
  3249.    * The host name does not map to the original host address. Perhaps
  3250.    * someone has compromised a name server. More likely someone botched
  3251.    * it, but that could be dangerous, too.
  3252.    */
  3253.   
  3254.   DEBUG(0,("host name/address mismatch: %s != %s",
  3255.        inet_ntoa(addr), hp->h_name));
  3256.   return False;
  3257. }
  3258.  
  3259. /*******************************************************************
  3260.  Reset the 'done' variables so after a client process is created
  3261.  from a fork call these calls will be re-done. This should be
  3262.  expanded if more variables need reseting.
  3263.  ******************************************************************/
  3264.  
  3265. static BOOL global_client_name_done = False;
  3266. static BOOL global_client_addr_done = False;
  3267.  
  3268. void reset_globals_after_fork()
  3269. {
  3270.   global_client_name_done = False;
  3271.   global_client_addr_done = False;
  3272. }
  3273.  
  3274. /*******************************************************************
  3275.  return the DNS name of the client 
  3276.  ******************************************************************/
  3277. char *client_name(void)
  3278. {
  3279.   extern int Client;
  3280.   struct sockaddr sa;
  3281.   struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
  3282.   int     length = sizeof(sa);
  3283.   static pstring name_buf;
  3284.   struct hostent *hp;
  3285.  
  3286.   if (global_client_name_done) 
  3287.     return name_buf;
  3288.  
  3289.   strcpy(name_buf,"UNKNOWN");
  3290.  
  3291.   if (getpeername(Client, &sa, &length) < 0) {
  3292.     DEBUG(0,("getpeername failed\n"));
  3293.     return name_buf;
  3294.   }
  3295.  
  3296.   /* Look up the remote host name. */
  3297.   if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
  3298.               sizeof(sockin->sin_addr),
  3299.               AF_INET)) == 0) {
  3300.     DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr()));
  3301.     StrnCpy(name_buf,client_addr(),sizeof(name_buf) - 1);
  3302.   } else {
  3303.     StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
  3304.     if (!matchname(name_buf, sockin->sin_addr)) {
  3305.       DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr()));
  3306.       strcpy(name_buf,"UNKNOWN");
  3307.     }
  3308.   }
  3309.   global_client_name_done = True;
  3310.   return name_buf;
  3311. }
  3312.  
  3313. /*******************************************************************
  3314.  return the IP addr of the client as a string 
  3315.  ******************************************************************/
  3316. char *client_addr(void)
  3317. {
  3318.   extern int Client;
  3319.   struct sockaddr sa;
  3320.   struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
  3321.   int     length = sizeof(sa);
  3322.   static fstring addr_buf;
  3323.  
  3324.   if (global_client_addr_done) 
  3325.     return addr_buf;
  3326.  
  3327.   strcpy(addr_buf,"0.0.0.0");
  3328.  
  3329.   if (getpeername(Client, &sa, &length) < 0) {
  3330.     DEBUG(0,("getpeername failed\n"));
  3331.     return addr_buf;
  3332.   }
  3333.  
  3334.   fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
  3335.  
  3336.   global_client_addr_done = True;
  3337.   return addr_buf;
  3338. }
  3339.  
  3340. /*******************************************************************
  3341. sub strings with useful parameters
  3342. Rewritten by Stefaan A Eeckels <Stefaan.Eeckels@ecc.lu> and
  3343. Paul Rippin <pr3245@nopc.eurostat.cec.be>
  3344. ********************************************************************/
  3345. void standard_sub_basic(char *str)
  3346.   {
  3347.   char *s, *p;
  3348.     char pidstr[10];
  3349.   struct passwd *pass;
  3350.  
  3351.   for (s = str ; (p = strchr(s,'%')) != NULL ; s = p )
  3352.   {
  3353.     switch (*(p+1))
  3354.   {
  3355.       case 'G' : if ((pass = Get_Pwnam(sesssetup_user,False))!=NULL)
  3356.                    string_sub(p,"%G",gidtoname(pass->pw_gid));
  3357.                  else
  3358.                    p += 2;
  3359.                  break;
  3360.       case 'I' : string_sub(p,"%I",client_addr()); break;
  3361.       case 'L' : string_sub(p,"%L",local_machine); break;
  3362.       case 'M' : string_sub(p,"%M",client_name()); break;
  3363.       case 'R' : string_sub(p,"%R",remote_proto); break;
  3364.       case 'T' : string_sub(p,"%T",timestring()); break;
  3365.       case 'U' : string_sub(p,"%U",sesssetup_user); break;
  3366.       case 'a' : string_sub(p,"%a",remote_arch); break;
  3367.       case 'd' : sprintf(pidstr,"%d",(int)getpid());
  3368.                  string_sub(p,"%d",pidstr);
  3369.                  break;
  3370.       case 'h' : string_sub(p,"%h",myhostname); break;
  3371.       case 'm' : string_sub(p,"%m",remote_machine); break;
  3372.       case 'v' : string_sub(p,"%v",VERSION); break;
  3373.       case '\0' : p++; break; /* don't run off end if last character is % */
  3374.       default  : p+=2; break;
  3375.     }
  3376.   }
  3377.   return;
  3378. }
  3379.  
  3380. /*******************************************************************
  3381. are two IPs on the same subnet?
  3382. ********************************************************************/
  3383. BOOL same_net(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
  3384. {
  3385.   uint32 net1,net2,nmask;
  3386.  
  3387.   nmask = ntohl(mask.s_addr);
  3388.   net1  = ntohl(ip1.s_addr);
  3389.   net2  = ntohl(ip2.s_addr);
  3390.             
  3391.   return((net1 & nmask) == (net2 & nmask));
  3392. }
  3393.  
  3394.  
  3395. /*******************************************************************
  3396. write a string in unicoode format
  3397. ********************************************************************/
  3398. int PutUniCode(char *dst,char *src)
  3399. {
  3400.   int ret = 0;
  3401.   while (*src) {
  3402.     dst[ret++] = src[0];
  3403.     dst[ret++] = 0;    
  3404.     src++;
  3405.   }
  3406.   dst[ret++]=0;
  3407.   dst[ret++]=0;
  3408.   return(ret);
  3409. }
  3410.  
  3411. /****************************************************************************
  3412. a wrapper for gethostbyname() that tries with all lower and all upper case 
  3413. if the initial name fails
  3414. ****************************************************************************/
  3415. struct hostent *Get_Hostbyname(char *name)
  3416. {
  3417.   char *name2 = strdup(name);
  3418.   struct hostent *ret;
  3419.  
  3420.   if (!name2)
  3421.     {
  3422.       DEBUG(0,("Memory allocation error in Get_Hostbyname! panic\n"));
  3423.       exit(0);
  3424.     }
  3425.  
  3426.   if (!isalnum(*name2))
  3427.     {
  3428.       free(name2);
  3429.       return(NULL);
  3430.     }
  3431.  
  3432.   ret = sys_gethostbyname(name2);
  3433.   if (ret != NULL)
  3434.     {
  3435.       free(name2);
  3436.       return(ret);
  3437.     }
  3438.  
  3439.   /* try with all lowercase */
  3440.   strlower(name2);
  3441.   ret = sys_gethostbyname(name2);
  3442.   if (ret != NULL)
  3443.     {
  3444.       free(name2);
  3445.       return(ret);
  3446.     }
  3447.  
  3448.   /* try with all uppercase */
  3449.   strupper(name2);
  3450.   ret = sys_gethostbyname(name2);
  3451.   if (ret != NULL)
  3452.     {
  3453.       free(name2);
  3454.       return(ret);
  3455.     }
  3456.   
  3457.   /* nothing works :-( */
  3458.   free(name2);
  3459.   return(NULL);
  3460. }
  3461.  
  3462.  
  3463. /****************************************************************************
  3464. check if a process exists. Does this work on all unixes?
  3465. ****************************************************************************/
  3466. BOOL process_exists(int pid)
  3467. {
  3468. #ifdef LINUX
  3469.   fstring s;
  3470.   sprintf(s,"/proc/%d",pid);
  3471.   return(directory_exist(s,NULL));
  3472. #else
  3473.   {
  3474.     static BOOL tested=False;
  3475.     static BOOL ok=False;
  3476.     fstring s;
  3477.     if (!tested) {
  3478.       tested = True;
  3479.       sprintf(s,"/proc/%05d",(int)getpid());
  3480.       ok = file_exist(s,NULL);
  3481.     }
  3482.     if (ok) {
  3483.       sprintf(s,"/proc/%05d",pid);
  3484.       return(file_exist(s,NULL));
  3485.     }
  3486.   }
  3487.  
  3488.   /* CGH 8/16/96 - added ESRCH test */
  3489.   return(pid == getpid() || kill(pid,0) == 0 || errno != ESRCH);
  3490. #endif
  3491. }
  3492.  
  3493.  
  3494. /*******************************************************************
  3495. turn a uid into a user name
  3496. ********************************************************************/
  3497. char *uidtoname(int uid)
  3498. {
  3499.   static char name[40];
  3500.   struct passwd *pass = getpwuid(uid);
  3501.   if (pass) return(pass->pw_name);
  3502.   sprintf(name,"%d",uid);
  3503.   return(name);
  3504. }
  3505.  
  3506. /*******************************************************************
  3507. turn a gid into a group name
  3508. ********************************************************************/
  3509. char *gidtoname(int gid)
  3510. {
  3511.   static char name[40];
  3512.   struct group *grp = getgrgid(gid);
  3513.   if (grp) return(grp->gr_name);
  3514.   sprintf(name,"%d",gid);
  3515.   return(name);
  3516. }
  3517.  
  3518. /*******************************************************************
  3519. block sigs
  3520. ********************************************************************/
  3521. void BlockSignals(BOOL block,int signum)
  3522. {
  3523. #ifdef USE_SIGBLOCK
  3524.   int block_mask = sigmask(signum);
  3525.   static int oldmask = 0;
  3526.   if (block) 
  3527.     oldmask = sigblock(block_mask);
  3528.   else
  3529.     sigsetmask(oldmask);
  3530. #elif defined(USE_SIGPROCMASK)
  3531.   sigset_t set;
  3532.   sigemptyset(&set);
  3533.   sigaddset(&set,signum);
  3534.   sigprocmask(block?SIG_BLOCK:SIG_UNBLOCK,&set,NULL);
  3535. #endif
  3536. }
  3537.  
  3538. #if AJT
  3539. /*******************************************************************
  3540. my own panic function - not suitable for general use
  3541. ********************************************************************/
  3542. void ajt_panic(void)
  3543. {
  3544.   system("/usr/bin/X11/xedit -display ljus:0 /tmp/ERROR_FAULT");
  3545. }
  3546. #endif
  3547.  
  3548. #ifdef USE_DIRECT
  3549. #define DIRECT direct
  3550. #else
  3551. #define DIRECT dirent
  3552. #endif
  3553.  
  3554. /*******************************************************************
  3555. a readdir wrapper which just returns the file name
  3556. also return the inode number if requested
  3557. ********************************************************************/
  3558. char *readdirname(void *p)
  3559. {
  3560.   struct DIRECT *ptr;
  3561.   char *dname;
  3562.  
  3563.   if (!p) return(NULL);
  3564.   
  3565.   ptr = (struct DIRECT *)readdir(p);
  3566.   if (!ptr) return(NULL);
  3567.  
  3568.   dname = ptr->d_name;
  3569.  
  3570. #ifdef NEXT2
  3571.   if (telldir(p) < 0) return(NULL);
  3572. #endif
  3573.  
  3574. #ifdef SUNOS5
  3575.   /* this handles a broken compiler setup, causing a mixture
  3576.    of BSD and SYSV headers and libraries */
  3577.   {
  3578.     static BOOL broken_readdir = False;
  3579.     if (!broken_readdir && !(*(dname)) && strequal("..",dname-2))
  3580.       {
  3581.     DEBUG(0,("Your readdir() is broken. You have somehow mixed SYSV and BSD headers and libraries\n"));
  3582.     broken_readdir = True;
  3583.       }
  3584.     if (broken_readdir)
  3585.       dname = dname - 2;
  3586.   }
  3587. #endif
  3588.  
  3589.   {
  3590.     static pstring buf;
  3591.     pstrcpy(buf, dname);
  3592.     unix_to_dos(buf, True);
  3593.     dname = buf;
  3594.   }
  3595.  
  3596.   return(dname);
  3597. }
  3598.  
  3599. /*
  3600.  * Utility function used to decide if the last component 
  3601.  * of a path matches a (possibly wildcarded) entry in a namelist.
  3602.  */
  3603.  
  3604. BOOL is_in_path(char *name, name_compare_entry *namelist)
  3605. {
  3606.   pstring last_component;
  3607.   char *p;
  3608.  
  3609.   DEBUG(5, ("is_in_path: %s\n", name));
  3610.  
  3611.   /* if we have no list it's obviously not in the path */
  3612.   if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL))) 
  3613.         {
  3614.     DEBUG(5,("is_in_path: no name list.\n"));
  3615.     return False;
  3616. }
  3617.  
  3618.   /* Get the last component of the unix name. */
  3619.   p = strrchr(name, '/');
  3620.   strncpy(last_component, p ? p : name, sizeof(last_component)-1);
  3621.   last_component[sizeof(last_component)-1] = '\0'; 
  3622.  
  3623.   for(; namelist->name != NULL; namelist++)
  3624.   {
  3625.     if(namelist->is_wild)
  3626.     {
  3627.       /* look for a wildcard match. */
  3628.       if (mask_match(last_component, namelist->name, case_sensitive, False))
  3629.       {
  3630.          DEBUG(5,("is_in_path: mask match succeeded\n"));
  3631.          return True;
  3632.       }
  3633.     }
  3634.     else
  3635.     {
  3636.       if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
  3637.        (!case_sensitive && (StrCaseCmp(last_component, namelist->name) == 0)))
  3638.         {
  3639.          DEBUG(5,("is_in_path: match succeeded\n"));
  3640.          return True;
  3641.         }
  3642.     }
  3643.   }
  3644.   DEBUG(5,("is_in_path: match not found\n"));
  3645.  
  3646.   return False;
  3647. }
  3648.  
  3649. /*
  3650.  * Strip a '/' separated list into an array of 
  3651.  * name_compare_enties structures suitable for 
  3652.  * passing to is_in_path(). We do this for
  3653.  * speed so we can pre-parse all the names in the list 
  3654.  * and don't do it for each call to is_in_path().
  3655.  * namelist is modified here and is assumed to be 
  3656.  * a copy owned by the caller.
  3657.  * We also check if the entry contains a wildcard to
  3658.  * remove a potentially expensive call to mask_match
  3659.  * if possible.
  3660.    */
  3661.  
  3662. void set_namearray(name_compare_entry **ppname_array, char *namelist)
  3663. {
  3664.   char *name_end;
  3665.   char *nameptr = namelist;
  3666.   int num_entries = 0;
  3667.   int i;
  3668.  
  3669.   (*ppname_array) = NULL;
  3670.  
  3671.   if((nameptr == NULL ) || ((nameptr != NULL) && (*nameptr == '\0'))) 
  3672.     return;
  3673.  
  3674.   /* We need to make two passes over the string. The
  3675.      first to count the number of elements, the second
  3676.      to split it.
  3677.    */
  3678.   while(*nameptr) 
  3679.     {
  3680.       if ( *nameptr == '/' ) 
  3681.         {
  3682.           /* cope with multiple (useless) /s) */
  3683.           nameptr++;
  3684.           continue;
  3685.         }
  3686.       /* find the next / */
  3687.       name_end = strchr(nameptr, '/');
  3688.  
  3689.       /* oops - the last check for a / didn't find one. */
  3690.       if (name_end == NULL)
  3691.         break;
  3692.  
  3693.       /* next segment please */
  3694.       nameptr = name_end + 1;
  3695.       num_entries++;
  3696.     }
  3697.  
  3698.   if(num_entries == 0)
  3699.     return;
  3700.  
  3701.   if(( (*ppname_array) = (name_compare_entry *)malloc( 
  3702.            (num_entries + 1) * sizeof(name_compare_entry))) == NULL)
  3703.         {
  3704.     DEBUG(0,("set_namearray: malloc fail\n"));
  3705.     return;
  3706.         }
  3707.  
  3708.   /* Now copy out the names */
  3709.   nameptr = namelist;
  3710.   i = 0;
  3711.   while(*nameptr)
  3712.              {
  3713.       if ( *nameptr == '/' ) 
  3714.       {
  3715.           /* cope with multiple (useless) /s) */
  3716.           nameptr++;
  3717.           continue;
  3718.       }
  3719.       /* find the next / */
  3720.       if ((name_end = strchr(nameptr, '/')) != NULL) 
  3721.       {
  3722.           *name_end = 0;
  3723.          }
  3724.  
  3725.       /* oops - the last check for a / didn't find one. */
  3726.       if(name_end == NULL) 
  3727.         break;
  3728.  
  3729.       (*ppname_array)[i].is_wild = ((strchr( nameptr, '?')!=NULL) ||
  3730.                                 (strchr( nameptr, '*')!=NULL));
  3731.       if(((*ppname_array)[i].name = strdup(nameptr)) == NULL)
  3732.       {
  3733.         DEBUG(0,("set_namearray: malloc fail (1)\n"));
  3734.         return;
  3735.       }
  3736.  
  3737.       /* next segment please */
  3738.       nameptr = name_end + 1;
  3739.       i++;
  3740.     }
  3741.   
  3742.   (*ppname_array)[i].name = NULL;
  3743.  
  3744.   return;
  3745. }
  3746.  
  3747. /****************************************************************************
  3748. routine to free a namearray.
  3749. ****************************************************************************/
  3750.  
  3751. void free_namearray(name_compare_entry *name_array)
  3752. {
  3753.   if(name_array == 0)
  3754.     return;
  3755.  
  3756.   if(name_array->name != NULL)
  3757.     free(name_array->name);
  3758.  
  3759.   free((char *)name_array);
  3760. }
  3761.  
  3762. /****************************************************************************
  3763. routine to do file locking
  3764. ****************************************************************************/
  3765. BOOL fcntl_lock(int fd,int op,uint32 offset,uint32 count,int type)
  3766. {
  3767. #if HAVE_FCNTL_LOCK
  3768.   struct flock lock;
  3769.   int ret;
  3770.  
  3771. #if 1
  3772.   uint32 mask = 0xC0000000;
  3773.  
  3774.   /* make sure the count is reasonable, we might kill the lockd otherwise */
  3775.   count &= ~mask;
  3776.  
  3777.   /* the offset is often strange - remove 2 of its bits if either of
  3778.      the top two bits are set. Shift the top ones by two bits. This
  3779.      still allows OLE2 apps to operate, but should stop lockd from
  3780.      dieing */
  3781.   if ((offset & mask) != 0)
  3782.     offset = (offset & ~mask) | ((offset & mask) >> 2);
  3783. #else
  3784.   uint32 mask = ((unsigned)1<<31);
  3785.  
  3786.   /* interpret negative counts as large numbers */
  3787.   if (count < 0)
  3788.     count &= ~mask;
  3789.  
  3790.   /* no negative offsets */
  3791.   offset &= ~mask;
  3792.  
  3793.   /* count + offset must be in range */
  3794.   while ((offset < 0 || (offset + count < 0)) && mask)
  3795.     {
  3796.       offset &= ~mask;
  3797.       mask = mask >> 1;
  3798.     }
  3799. #endif
  3800.  
  3801.  
  3802.   DEBUG(5,("fcntl_lock %d %d %d %d %d\n",fd,op,(int)offset,(int)count,type));
  3803.  
  3804.   lock.l_type = type;
  3805.   lock.l_whence = SEEK_SET;
  3806.   lock.l_start = (int)offset;
  3807.   lock.l_len = (int)count;
  3808.   lock.l_pid = 0;
  3809.  
  3810.   errno = 0;
  3811.  
  3812.   ret = fcntl(fd,op,&lock);
  3813.  
  3814.   if (errno != 0)
  3815.     DEBUG(3,("fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
  3816.  
  3817.   /* a lock query */
  3818.   if (op == F_GETLK)
  3819.     {
  3820.       if ((ret != -1) &&
  3821.       (lock.l_type != F_UNLCK) && 
  3822.       (lock.l_pid != 0) && 
  3823.       (lock.l_pid != getpid()))
  3824.     {
  3825.       DEBUG(3,("fd %d is locked by pid %d\n",fd,lock.l_pid));
  3826.       return(True);
  3827.     }
  3828.  
  3829.       /* it must be not locked or locked by me */
  3830.       return(False);
  3831.     }
  3832.  
  3833.   /* a lock set or unset */
  3834.   if (ret == -1)
  3835.     {
  3836.       DEBUG(3,("lock failed at offset %d count %d op %d type %d (%s)\n",
  3837.            offset,count,op,type,strerror(errno)));
  3838.  
  3839.       /* perhaps it doesn't support this sort of locking?? */
  3840.       if (errno == EINVAL)
  3841.     {
  3842.       DEBUG(3,("locking not supported? returning True\n"));
  3843.       return(True);
  3844.     }
  3845.  
  3846.       return(False);
  3847.     }
  3848.  
  3849.   /* everything went OK */
  3850.   DEBUG(5,("Lock call successful\n"));
  3851.  
  3852.   return(True);
  3853. #else
  3854.   return(False);
  3855. #endif
  3856. }
  3857.  
  3858. /*******************************************************************
  3859. lock a file - returning a open file descriptor or -1 on failure
  3860. The timeout is in seconds. 0 means no timeout
  3861. ********************************************************************/
  3862. int file_lock(char *name,int timeout)
  3863. {  
  3864.   int fd = open(name,O_RDWR|O_CREAT,0666);
  3865.   time_t t=0;
  3866.   if (fd < 0) return(-1);
  3867.  
  3868. #if HAVE_FCNTL_LOCK
  3869.   if (timeout) t = time(NULL);
  3870.   while (!timeout || (time(NULL)-t < timeout)) {
  3871.     if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)) return(fd);    
  3872.     msleep(LOCK_RETRY_TIMEOUT);
  3873.   }
  3874.   return(-1);
  3875. #else
  3876.   return(fd);
  3877. #endif
  3878. }
  3879.  
  3880. /*******************************************************************
  3881. unlock a file locked by file_lock
  3882. ********************************************************************/
  3883. void file_unlock(int fd)
  3884. {
  3885.   if (fd<0) return;
  3886. #if HAVE_FCNTL_LOCK
  3887.   fcntl_lock(fd,F_SETLK,0,1,F_UNLCK);
  3888. #endif
  3889.   close(fd);
  3890. }
  3891.  
  3892. /*******************************************************************
  3893. is the name specified one of my netbios names
  3894. returns true is it is equal, false otherwise
  3895. ********************************************************************/
  3896. BOOL is_myname(const char *s)
  3897. {
  3898.   int n;
  3899.   BOOL ret = False;
  3900.  
  3901.   for (n=0; my_netbios_names[n]; n++) {
  3902.     if (strequal(my_netbios_names[n], s))
  3903.       ret=True;
  3904.   }
  3905.   DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret));
  3906.   return(ret);
  3907. }
  3908.  
  3909. /*******************************************************************
  3910. set the horrid remote_arch string based on an enum.
  3911. ********************************************************************/
  3912. void set_remote_arch(enum remote_arch_types type)
  3913. {
  3914.   ra_type = type;
  3915.   switch( type )
  3916.   {
  3917.   case RA_WFWG:
  3918.     strcpy(remote_arch, "WfWg");
  3919.     return;
  3920.   case RA_OS2:
  3921.     strcpy(remote_arch, "OS2");
  3922.     return;
  3923.   case RA_WIN95:
  3924.     strcpy(remote_arch, "Win95");
  3925.     return;
  3926.   case RA_WINNT:
  3927.     strcpy(remote_arch, "WinNT");
  3928.     return;
  3929.   case RA_SAMBA:
  3930.     strcpy(remote_arch,"Samba");
  3931.     return;
  3932.   default:
  3933.     ra_type = RA_UNKNOWN;
  3934.     strcpy(remote_arch, "UNKNOWN");
  3935.     break;
  3936.   }
  3937. }
  3938.  
  3939. /*******************************************************************
  3940.  Get the remote_arch type.
  3941. ********************************************************************/
  3942. enum remote_arch_types get_remote_arch()
  3943. {
  3944.   return ra_type;
  3945. }
  3946.  
  3947.  
  3948. /*******************************************************************
  3949. safe string copy into a fstring
  3950. ********************************************************************/
  3951. void fstrcpy(char *dest, char *src)
  3952. {
  3953.     int maxlength = sizeof(fstring) - 1;
  3954.     if (!dest) {
  3955.         DEBUG(0,("ERROR: NULL dest in fstrcpy\n"));
  3956.         return;
  3957.     }
  3958.  
  3959.     if (!src) {
  3960.         *dest = 0;
  3961.         return;
  3962.     }
  3963.  
  3964.     while (maxlength-- && *src)
  3965.         *dest++ = *src++;
  3966.     *dest = 0;
  3967.     if (*src) {
  3968.         DEBUG(0,("ERROR: string overflow by %d in fstrcpy\n",
  3969.              strlen(src)));
  3970.     }
  3971. }
  3972.  
  3973. /*******************************************************************
  3974. safe string copy into a pstring
  3975. ********************************************************************/
  3976. void pstrcpy(char *dest, char *src)
  3977. {
  3978.     int maxlength = sizeof(pstring) - 1; 
  3979.     if (!dest) {
  3980.         DEBUG(0,("ERROR: NULL dest in pstrcpy\n"));
  3981.         return;
  3982.     }
  3983.  
  3984.     if (!src) {
  3985.         *dest = 0;
  3986.         return;
  3987.     }
  3988.  
  3989.     while (maxlength-- && *src)
  3990.         *dest++ = *src++;
  3991.     *dest = 0;
  3992.     if (*src) {
  3993.         DEBUG(0,("ERROR: string overflow by %d in pstrcpy\n",
  3994.              strlen(src)));
  3995.     }
  3996. }
  3997.  
  3998. #ifdef USE_NETINFO
  3999. /*******************************************************************
  4000. NetInfo routines for the NEXTSTEP/OPENSTEP and Rhapsody systems.
  4001.  
  4002. Robert Frank, Department of Computer Science, University of Basel 1997.
  4003. ********************************************************************/
  4004. /*
  4005.     Search for a specified directory (dir) in the NetInfo hierarchy, starting
  4006.     at the directory given in baseDomain and relative to the handle passed
  4007.     in (may be NULL). Climb the hierarchy until a match is found or the
  4008.     top level has been left. If a handle was passed in the old handle will be freed
  4009.     if freeold is true.
  4010.     Return NI_OK if a directory is found and place the directory's id into id.
  4011. */
  4012. ni_status ni_search_for_dir(char *dir, char *baseDomain, void **handle, ni_id *id, int timeout, int willwrite, int freeold)
  4013. {
  4014. ni_fancyopenargs    openArgs;
  4015. ni_status                    status;
  4016. char                            *domain;
  4017. void                            *oldDomain, *lastDomain;
  4018.  
  4019.     domain = baseDomain;
  4020.     oldDomain = *handle;    /* Do not free unless freeold is true. */
  4021.     lastDomain = *handle; /* do free intermediate references. */
  4022.     
  4023.     do {
  4024.  
  4025.         openArgs.rtimeout = timeout;
  4026.         openArgs.wtimeout = 0;
  4027.         openArgs.abort = 1;
  4028.         openArgs.needwrite = willwrite;
  4029.         status = ni_fancyopen(lastDomain, domain, handle, &openArgs);
  4030.         if (status != NI_OK) {
  4031.             if (oldDomain && freeold)
  4032.                 ni_free(oldDomain);
  4033.             if (oldDomain != lastDomain)
  4034.                 ni_free(lastDomain);
  4035.             return status;
  4036.         }
  4037.  
  4038.         /*
  4039.          * Set the directory to the root for relative addressing if dir doesn't start 
  4040.          * with a `/'.
  4041.          */
  4042.         status = (*dir != '/') ? ni_root(*handle, id) : NI_OK;
  4043.  
  4044.         if (status == NI_OK)
  4045.             status = ni_pathsearch(*handle, id, dir);
  4046.         if (status == NI_NODIR) {    /* Try next higher level, if any. */
  4047.             domain = "..";
  4048.             lastDomain = *handle;
  4049.         } else if (status != NI_OK) {
  4050.             if (oldDomain != lastDomain)
  4051.                 ni_free(lastDomain);
  4052.             ni_free(*handle);
  4053.             *handle = NULL;
  4054.         }
  4055.     } while (status == NI_NODIR);
  4056.  
  4057.     if (oldDomain && freeold)
  4058.         ni_free(oldDomain);
  4059.     return status;
  4060. } /* ni_search_for_dir */
  4061.  
  4062.  
  4063. /*
  4064.     Search for the netbios name of the given host. If any is found, return the value of the netbiosname property, otherwise just return the string passed.
  4065. */
  4066. char *ni_get_netbios_name(char *unixname)
  4067. {
  4068.   static pstring    realname;
  4069.     pstring                    hostDir;
  4070.     ni_namelist            realnames;
  4071.  
  4072.     ni_status        status;
  4073.     ni_id                ndir;
  4074.     void                *handle;
  4075.  
  4076.  
  4077.     strncpy(realname, unixname, sizeof(pstring));
  4078.     strncpy(hostDir, S_HOSTDIR, sizeof(pstring));
  4079.     strncat(hostDir, "/", sizeof(pstring));
  4080.     strncat(hostDir, unixname, sizeof(pstring));
  4081.     
  4082.     DEBUG(10, ("ni_get_netbios_name: opening netinfo %s .\n", hostDir));
  4083.  
  4084.     /*
  4085.      * Scan the NetInfo hierarchy, starting from the local level,
  4086.      * until either an entry is found or we are past the top level.
  4087.      */
  4088.     handle = NULL;
  4089.     status = ni_search_for_dir(hostDir, ".", &handle, &ndir, 5, 0, 1);
  4090.  
  4091.     while (status == NI_OK) {
  4092.  
  4093.         status = ni_lookupprop(handle, &ndir, S_NETBIOSNAME, &realnames);
  4094.         if ((status == NI_OK) && (realnames.ni_namelist_len > 0)) {
  4095.             strncpy(realname, realnames.ni_namelist_val[0], sizeof(pstring));
  4096.             DEBUG(10, ("ni_get_netbios_name: found netbios name %s .\n", realname));
  4097.             ni_namelist_free(&realnames);
  4098.             ni_free(handle);
  4099.             break;
  4100.         }
  4101.  
  4102.         status = ni_search_for_dir(hostDir, "..", &handle, &ndir, 5, 0, 1);
  4103.     } 
  4104.  
  4105.   return realname;
  4106. }
  4107. #endif
  4108.  
  4109.  
  4110.