fixed warnings (and potential errors) due to integer overflow when
[samba.git] / source / lib / util.c
index 8bc75e1137ea8d05b25ebfc575817c421ac0cb70..e5ddda18916b096f344e15ed1956dce1833b7eea 100644 (file)
@@ -121,15 +121,15 @@ BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups)
 /****************************************************************************
 gets either a hex number (0xNNN) or decimal integer (NNN).
 ****************************************************************************/
-int get_number(const char *tmp)
+uint32 get_number(const char *tmp)
 {
        if (strnequal(tmp, "0x", 2))
        {
-               return strtol(tmp, (char**)NULL, 16);
+               return strtoul(tmp, (char**)NULL, 16);
        }
        else
        {
-               return strtol(tmp, (char**)NULL, 10);
+               return strtoul(tmp, (char**)NULL, 10);
        }
 }
 
@@ -144,7 +144,7 @@ char *Atoic(char *p, int *n, char *c)
                return NULL;
        }
 
-       (*n) = get_number(p);
+       (*n) = (int)get_number(p);
 
        if (strnequal(p, "0x", 2))
        {
@@ -366,6 +366,21 @@ BOOL file_exist(char *fname,SMB_STRUCT_STAT *sbuf)
   return(S_ISREG(sbuf->st_mode));
 }
 
+/*******************************************************************
+  rename a unix file
+********************************************************************/
+int file_rename(char *from, char *to)
+{
+       int rcode = rename (from, to);
+
+       if (errno == EXDEV) 
+       {
+               /* Rename across filesystems needed. */
+               rcode = copy_reg (from, to);        
+       }
+       return rcode;
+}
+
 /*******************************************************************
 check a files mod time
 ********************************************************************/
@@ -2306,6 +2321,55 @@ BOOL process_exists(int pid)
 }
 
 
+/****************************************************************************
+Setup the groups a user belongs to.
+****************************************************************************/
+int get_unixgroups(char *user, uid_t uid, gid_t gid, int *p_ngroups, gid_t **p_groups)
+{
+       int i,ngroups;
+       gid_t grp = 0;
+       gid_t *groups = NULL;
+
+       if (-1 == initgroups(user,gid))
+       {
+               if (getuid() == 0)
+               {
+                       DEBUG(0,("Unable to initgroups!\n"));
+                       if (gid < 0 || gid > 16000 || uid < 0 || uid > 16000)
+                       {
+                               DEBUG(0,("This is probably a problem with the account %s\n", user));
+                       }
+               }
+               return -1;
+       }
+
+       ngroups = sys_getgroups(0,&grp);
+       if (ngroups <= 0)
+       {
+               ngroups = 32;
+       }
+
+       if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL)
+       {
+               DEBUG(0,("get_unixgroups malloc fail !\n"));
+               return -1;
+       }
+
+       ngroups = sys_getgroups(ngroups,groups);
+
+       (*p_ngroups) = ngroups;
+       (*p_groups) = groups;
+
+       DEBUG( 3, ( "%s is in %d groups: ", user, ngroups ) );
+       for (i = 0; i < ngroups; i++ )
+       {
+               DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
+       }
+       DEBUG( 3, ( "\n" ) );
+
+       return 0;
+}
+
 /*******************************************************************
 turn a uid into a user name
 ********************************************************************/
@@ -2602,8 +2666,8 @@ BOOL fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
   int ret;
 
   if(lp_ole_locking_compat()) {
-    SMB_OFF_T mask = ((SMB_OFF_T)0xC) << (SMB_OFF_T_BITS-4);
     SMB_OFF_T mask2= ((SMB_OFF_T)0x3) << (SMB_OFF_T_BITS-4);
+    SMB_OFF_T mask = (mask2<<2);
 
     /* make sure the count is reasonable, we might kill the lockd otherwise */
     count &= ~mask;
@@ -2615,7 +2679,8 @@ BOOL fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
     if ((offset & mask) != 0)
       offset = (offset & ~mask) | (((offset & mask) >> 2) & mask2);
   } else {
-    SMB_OFF_T mask = ((SMB_OFF_T)0x8) << (SMB_OFF_T_BITS-4);
+    SMB_OFF_T mask2 = ((SMB_OFF_T)0x4) << (SMB_OFF_T_BITS-4);
+    SMB_OFF_T mask = (mask2<<1);
     SMB_OFF_T neg_mask = ~mask;
 
     /* interpret negative counts as large numbers */