diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 3b005c1d8..b35da5cba 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,15 @@
+2002-11-12  Pierre Humblet <pierre.humblet@ieee.org>
+
+	* syscalls.cc (chmod): Simplify conditional.
+	* sec_helper (cygsid::getfromstr): Reorganize to remove
+	calls to strcpy and strtok_r.
+	(cygsid::getfromgr): Change type to __uid32_t instead of int.
+	Keep only the allow_ntsec branch. Never call LookupAccountSid 
+	which calls PDCs, simply return -1 in case of failure.
+	Use cygsid == instead of calling EqualSid and remove test 
+	for NULL psid.
+	* security.h: Declare cygsid::getfromgr as __uid32_t.
+
 2002-11-10  Corinna Vinschen  <corinna@vinschen.de>
 
 	* net.cc: Run indent.
diff --git a/winsup/cygwin/sec_helper.cc b/winsup/cygwin/sec_helper.cc
index 8e06810b9..3b5e162cd 100644
--- a/winsup/cygwin/sec_helper.cc
+++ b/winsup/cygwin/sec_helper.cc
@@ -99,29 +99,19 @@ cygsid::get_sid (DWORD s, DWORD cnt, DWORD *r)
 const PSID
 cygsid::getfromstr (const char *nsidstr)
 {
-  char sid_buf[256];
-  char *t, *lasts;
-  DWORD cnt = 0;
-  DWORD s = 0;
-  DWORD i, r[8];
+  char *lasts;
+  DWORD s, cnt = 0;
+  DWORD r[8];
 
-  if (!nsidstr || strncmp (nsidstr, "S-1-", 4))
+  if (nsidstr && !strncmp (nsidstr, "S-1-", 4))
     {
-      psid = NO_SID;
-      return NULL;
+      s = strtoul (nsidstr + 4, &lasts, 10);
+      while ( cnt < 8 && *lasts == '-')
+	r[cnt++] = strtoul (lasts + 1, &lasts, 10);
+      if (!*lasts)
+	return get_sid (s, cnt, r);
     }
-
-  strcpy (sid_buf, nsidstr);
-
-  for (t = sid_buf + 4, i = 0;
-       cnt < 8 && (t = strtok_r (t, "-", &lasts));
-       t = NULL, ++i)
-    if (i == 0)
-      s = strtoul (t, NULL, 10);
-    else
-      r[cnt++] = strtoul (t, NULL, 10);
-
-  return get_sid (s, cnt, r);
+  return psid = NO_SID;
 }
 
 BOOL
@@ -138,124 +128,54 @@ cygsid::getfromgr (const struct __group32 *gr)
   return (*this = sp ?: "") != NULL;
 }
 
-int
+__uid32_t
 cygsid::get_id (BOOL search_grp, int *type)
 {
-  if (!psid)
-    {
-      set_errno (EINVAL);
-      return -1;
-    }
-  if (!IsValidSid (psid))
-    {
-      __seterrno ();
-      system_printf ("IsValidSid failed with %E");
-      return -1;
-    }
-
   /* First try to get SID from passwd or group entry */
-  if (allow_ntsec)
+  cygsid sid;
+  __uid32_t id = ILLEGAL_UID;
+
+  if (!search_grp)
     {
-      cygsid sid;
-      int id = -1;
-
-      if (!search_grp)
+      struct passwd *pw;
+      if (*this == cygheap->user.sid ())
+	id = myself->uid;
+      else
+	for (int pidx = 0; (pw = internal_getpwent (pidx)); ++pidx)
+          {
+	    if (sid.getfrompw (pw) && sid == psid)
+	      {
+		id = pw->pw_uid;
+		break;
+	      }
+	  }
+      if (id != ILLEGAL_UID)
 	{
-	  struct passwd *pw;
-	 if (EqualSid(psid, cygheap->user.sid ()))
-	   id = myself->uid;
-	 else
-	   for (int pidx = 0; (pw = internal_getpwent (pidx)); ++pidx)
-	     {
-	       if (sid.getfrompw (pw) && sid == psid)
-		 {
-		   id = pw->pw_uid;
-		   break;
-		 }
-	     }
-	  if (id >= 0)
-	    {
-	      if (type)
-		*type = USER;
-	      return id;
-	    }
-	}
-      if (search_grp || type)
-	{
-	  struct __group32 *gr;
-	 if (cygheap->user.groups.pgsid == psid)
-	   id = myself->gid;
-	 else
-	   for (int gidx = 0; (gr = internal_getgrent (gidx)); ++gidx)
-	     {
-	       if (sid.getfromgr (gr) && sid == psid)
-		 {
-		   id = gr->gr_gid;
-		   break;
-		 }
-	     }
-	  if (id >= 0)
-	    {
-	      if (type)
-		*type = GROUP;
-	      return id;
-	    }
-	}
+	  if (type)
+	    *type = USER;
+	   return id;
+	 }
     }
-
-  /* We use the RID as default UID/GID */
-  int id = *GetSidSubAuthority (psid, *GetSidSubAuthorityCount (psid) - 1);
-
-  /*
-   * The RID maybe -1 if accountname == computername.
-   * In this case we search for the accountname in the passwd and group files.
-   * If type is needed, we search in each case.
-   */
-  if (id == -1 || type)
+  if (search_grp || type)
     {
-      char account[UNLEN + 1];
-      char domain[INTERNET_MAX_HOST_NAME_LENGTH + 1];
-      DWORD acc_len = UNLEN + 1;
-      DWORD dom_len = INTERNET_MAX_HOST_NAME_LENGTH + 1;
-      SID_NAME_USE acc_type;
-
-      if (!LookupAccountSid (NULL, psid, account, &acc_len,
-			     domain, &dom_len, &acc_type))
-	{
-	  __seterrno ();
-	  return -1;
-	}
-
-      switch (acc_type)
-	{
-	  case SidTypeGroup:
-	  case SidTypeAlias:
-	  case SidTypeWellKnownGroup:
-	    if (type)
-	      *type = GROUP;
-	    if (id == -1)
+      struct __group32 *gr;
+      if (cygheap->user.groups.pgsid == psid)
+	id = myself->gid;
+      else
+	for (int gidx = 0; (gr = internal_getgrent (gidx)); ++gidx)
+	  {
+	    if (sid.getfromgr (gr) && sid == psid)
 	      {
-		struct __group32 *gr = getgrnam32 (account);
-		if (gr)
-		  id = gr->gr_gid;
+		id = gr->gr_gid;
+		break;
 	      }
-	    break;
-	  case SidTypeUser:
-	    if (type)
-	      *type = USER;
-	    if (id == -1)
-	      {
-		struct passwd *pw = getpwnam (account);
-		if (pw)
-		  id = pw->pw_uid;
-	      }
-	    break;
-	  default:
-	    break;
+	  }
+      if (id != ILLEGAL_UID)
+	{
+	  if (type)
+	    *type = GROUP;
 	}
-    }
-  if (id == -1)
-    id = getuid32 ();
+     }
   return id;
 }
 
diff --git a/winsup/cygwin/security.h b/winsup/cygwin/security.h
index d9bd691f3..1bc498b99 100644
--- a/winsup/cygwin/security.h
+++ b/winsup/cygwin/security.h
@@ -57,7 +57,7 @@ public:
   BOOL getfrompw (const struct passwd *pw);
   BOOL getfromgr (const struct __group32 *gr);
 
-  int get_id (BOOL search_grp, int *type = NULL);
+  __uid32_t get_id (BOOL search_grp, int *type = NULL);
   inline int get_uid () { return get_id (FALSE); }
   inline int get_gid () { return get_id (TRUE); }
 
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index bfc45e722..1c2a18ba9 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -964,12 +964,9 @@ chmod (const char *path, mode_t mode)
 
       if (!SetFileAttributes (win32_path, win32_path))
 	__seterrno ();
-      else
-	{
-	  /* Correct NTFS security attributes have higher priority */
-	  if (res == 0 || !allow_ntsec)
-	    res = 0;
-	}
+      else if (!allow_ntsec)
+	/* Correct NTFS security attributes have higher priority */
+	res = 0;
     }
 
 done: