⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sec_acl.cc

📁 cygwin, 著名的在win32下模拟unix操作系统的东东
💻 CC
📖 第 1 页 / 共 2 页
字号:
	  ret = 4;	  break;	}      syscall_printf ("%d = acl (%s)", ret, path);      return ret;    }  switch (cmd)    {      case SETACL:	if (!aclsort (nentries, 0, aclbufp))	  return setacl (real_path.get_win32 (),			 nentries, aclbufp);	break;      case GETACL:	if (nentries < 1)	  break;	return getacl (real_path.get_win32 (),		       real_path.file_attributes (),		       nentries, aclbufp);      case GETACLCNT:	return getacl (real_path.get_win32 (),		       real_path.file_attributes (),		       0, NULL);      default:	break;    }  set_errno (EINVAL);  syscall_printf ("-1 = acl (%s)", path);  return -1;}extern "C"intacl (const char *path, int cmd, int nentries, __aclent16_t *aclbufp){  return acl_worker (path, cmd, nentries, aclbufp, 0);}extern "C"intlacl (const char *path, int cmd, int nentries, __aclent16_t *aclbufp){  return acl_worker (path, cmd, nentries, aclbufp, 1);}extern "C"intfacl (int fd, int cmd, int nentries, __aclent16_t *aclbufp){  cygheap_fdget cfd (fd);  if (cfd < 0)    {      syscall_printf ("-1 = facl (%d)", fd);      return -1;    }  const char *path = cfd->get_name ();  if (path == NULL)    {      syscall_printf ("-1 = facl (%d) (no name)", fd);      set_errno (ENOSYS);      return -1;    }  syscall_printf ("facl (%d): calling acl (%s)", fd, path);  return acl_worker (path, cmd, nentries, aclbufp, 0);}extern "C"intaclcheck (__aclent16_t *aclbufp, int nentries, int *which){  BOOL has_user_obj = FALSE;  BOOL has_group_obj = FALSE;  BOOL has_other_obj = FALSE;  BOOL has_class_obj = FALSE;  BOOL has_ug_objs = FALSE;  BOOL has_def_user_obj = FALSE;  BOOL has_def_group_obj = FALSE;  BOOL has_def_other_obj = FALSE;  BOOL has_def_class_obj = FALSE;  BOOL has_def_ug_objs = FALSE;  int pos2;  for (int pos = 0; pos < nentries; ++pos)    switch (aclbufp[pos].a_type)      {      case USER_OBJ:	if (has_user_obj)	  {	    if (which)	      *which = pos;	    return USER_ERROR;	  }	has_user_obj = TRUE;	break;      case GROUP_OBJ:	if (has_group_obj)	  {	    if (which)	      *which = pos;	    return GRP_ERROR;	  }	has_group_obj = TRUE;	break;      case OTHER_OBJ:	if (has_other_obj)	  {	    if (which)	      *which = pos;	    return OTHER_ERROR;	  }	has_other_obj = TRUE;	break;      case CLASS_OBJ:	if (has_class_obj)	  {	    if (which)	      *which = pos;	    return CLASS_ERROR;	  }	has_class_obj = TRUE;	break;      case USER:      case GROUP:	if ((pos2 = searchace (aclbufp + pos + 1, nentries - pos - 1,			       aclbufp[pos].a_type, aclbufp[pos].a_id)) >= 0)	  {	    if (which)	      *which = pos2;	    return DUPLICATE_ERROR;	  }	has_ug_objs = TRUE;	break;      case DEF_USER_OBJ:	if (has_def_user_obj)	  {	    if (which)	      *which = pos;	    return USER_ERROR;	  }	has_def_user_obj = TRUE;	break;      case DEF_GROUP_OBJ:	if (has_def_group_obj)	  {	    if (which)	      *which = pos;	    return GRP_ERROR;	  }	has_def_group_obj = TRUE;	break;      case DEF_OTHER_OBJ:	if (has_def_other_obj)	  {	    if (which)	      *which = pos;	    return OTHER_ERROR;	  }	has_def_other_obj = TRUE;	break;      case DEF_CLASS_OBJ:	if (has_def_class_obj)	  {	    if (which)	      *which = pos;	    return CLASS_ERROR;	  }	has_def_class_obj = TRUE;	break;      case DEF_USER:      case DEF_GROUP:	if ((pos2 = searchace (aclbufp + pos + 1, nentries - pos - 1,			       aclbufp[pos].a_type, aclbufp[pos].a_id)) >= 0)	  {	    if (which)	      *which = pos2;	    return DUPLICATE_ERROR;	  }	has_def_ug_objs = TRUE;	break;      default:	return ENTRY_ERROR;      }  if (!has_user_obj      || !has_group_obj      || !has_other_obj#if 0      /* These checks are not ok yet since CLASS_OBJ isn't fully implemented. */      || (has_ug_objs && !has_class_obj)      || (has_def_ug_objs && !has_def_class_obj)#endif     )    {      if (which)	*which = -1;      return MISS_ERROR;    }  return 0;}extern "C"int acecmp (const void *a1, const void *a2){#define ace(i) ((const __aclent16_t *) a##i)  int ret = ace (1)->a_type - ace (2)->a_type;  if (!ret)    ret = ace (1)->a_id - ace (2)->a_id;  return ret;#undef ace}extern "C"intaclsort (int nentries, int, __aclent16_t *aclbufp){  if (aclcheck (aclbufp, nentries, NULL))    return -1;  if (!aclbufp || nentries < 1)    {      set_errno (EINVAL);      return -1;    }  qsort ((void *) aclbufp, nentries, sizeof (__aclent16_t), acecmp);  return 0;}extern "C"intacltomode (__aclent16_t *aclbufp, int nentries, mode_t *modep){  int pos;  if (!aclbufp || nentries < 1 || !modep)    {      set_errno (EINVAL);      return -1;    }  *modep = 0;  if ((pos = searchace (aclbufp, nentries, USER_OBJ)) < 0      || !aclbufp[pos].a_type)    {      set_errno (EINVAL);      return -1;    }  *modep |= (aclbufp[pos].a_perm & S_IRWXO) << 6;  if ((pos = searchace (aclbufp, nentries, GROUP_OBJ)) < 0      || !aclbufp[pos].a_type)    {      set_errno (EINVAL);      return -1;    }  *modep |= (aclbufp[pos].a_perm & S_IRWXO) << 3;  int cpos;  if ((cpos = searchace (aclbufp, nentries, CLASS_OBJ)) >= 0      && aclbufp[cpos].a_type == CLASS_OBJ)    *modep |= ((aclbufp[pos].a_perm & S_IRWXO) & aclbufp[cpos].a_perm) << 3;  if ((pos = searchace (aclbufp, nentries, OTHER_OBJ)) < 0      || !aclbufp[pos].a_type)    {      set_errno (EINVAL);      return -1;    }  *modep |= aclbufp[pos].a_perm & S_IRWXO;  return 0;}extern "C"intaclfrommode (__aclent16_t *aclbufp, int nentries, mode_t *modep){  int pos;  if (!aclbufp || nentries < 1 || !modep)    {      set_errno (EINVAL);      return -1;    }  if ((pos = searchace (aclbufp, nentries, USER_OBJ)) < 0      || !aclbufp[pos].a_type)    {      set_errno (EINVAL);      return -1;    }  aclbufp[pos].a_perm = (*modep & S_IRWXU) >> 6;  if ((pos = searchace (aclbufp, nentries, GROUP_OBJ)) < 0      || !aclbufp[pos].a_type)    {      set_errno (EINVAL);      return -1;    }  aclbufp[pos].a_perm = (*modep & S_IRWXG) >> 3;  if ((pos = searchace (aclbufp, nentries, CLASS_OBJ)) >= 0      && aclbufp[pos].a_type == CLASS_OBJ)    aclbufp[pos].a_perm = (*modep & S_IRWXG) >> 3;  if ((pos = searchace (aclbufp, nentries, OTHER_OBJ)) < 0      || !aclbufp[pos].a_type)    {      set_errno (EINVAL);      return -1;    }  aclbufp[pos].a_perm = (*modep & S_IRWXO);  return 0;}extern "C"intacltopbits (__aclent16_t *aclbufp, int nentries, mode_t *pbitsp){  return acltomode (aclbufp, nentries, pbitsp);}extern "C"intaclfrompbits (__aclent16_t *aclbufp, int nentries, mode_t *pbitsp){  return aclfrommode (aclbufp, nentries, pbitsp);}static char *permtostr (mode_t perm){  static char pbuf[4];  pbuf[0] = (perm & S_IROTH) ? 'r' : '-';  pbuf[1] = (perm & S_IWOTH) ? 'w' : '-';  pbuf[2] = (perm & S_IXOTH) ? 'x' : '-';  pbuf[3] = '\0';  return pbuf;}extern "C"char *acltotext (__aclent16_t *aclbufp, int aclcnt){  if (!aclbufp || aclcnt < 1 || aclcnt > MAX_ACL_ENTRIES      || aclcheck (aclbufp, aclcnt, NULL))    {      set_errno (EINVAL);      return NULL;    }  char buf[32000];  buf[0] = '\0';  BOOL first = TRUE;  for (int pos = 0; pos < aclcnt; ++pos)    {      if (!first)	strcat (buf, ",");      first = FALSE;      if (aclbufp[pos].a_type & ACL_DEFAULT)	strcat (buf, "default");      switch (aclbufp[pos].a_type)	{	case USER_OBJ:	  __small_sprintf (buf + strlen (buf), "user::%s",		   permtostr (aclbufp[pos].a_perm));	  break;	case USER:	  __small_sprintf (buf + strlen (buf), "user:%d:%s",		   aclbufp[pos].a_id, permtostr (aclbufp[pos].a_perm));	  break;	case GROUP_OBJ:	  __small_sprintf (buf + strlen (buf), "group::%s",		   permtostr (aclbufp[pos].a_perm));	  break;	case GROUP:	  __small_sprintf (buf + strlen (buf), "group:%d:%s",		   aclbufp[pos].a_id, permtostr (aclbufp[pos].a_perm));	  break;	case CLASS_OBJ:	  __small_sprintf (buf + strlen (buf), "mask::%s",		   permtostr (aclbufp[pos].a_perm));	  break;	case OTHER_OBJ:	  __small_sprintf (buf + strlen (buf), "other::%s",		   permtostr (aclbufp[pos].a_perm));	  break;	default:	  set_errno (EINVAL);	  return NULL;	}    }  return strdup (buf);}static mode_tpermfromstr (char *perm){  mode_t mode = 0;  if (strlen (perm) != 3)    return 01000;  if (perm[0] == 'r')    mode |= S_IROTH;  else if (perm[0] != '-')    return 01000;  if (perm[1] == 'w')    mode |= S_IWOTH;  else if (perm[1] != '-')    return 01000;  if (perm[2] == 'x')    mode |= S_IXOTH;  else if (perm[2] != '-')    return 01000;  return mode;}extern "C"__aclent16_t *aclfromtext (char *acltextp, int *){  if (!acltextp)    {      set_errno (EINVAL);      return NULL;    }  char buf[strlen (acltextp) + 1];  __aclent16_t lacl[MAX_ACL_ENTRIES];  memset (lacl, 0, sizeof lacl);  int pos = 0;  strcpy (buf, acltextp);  char *lasts;  for (char *c = strtok_r (buf, ",", &lasts);       c;       c = strtok_r (NULL, ",", &lasts))    {      if (!strncmp (c, "default", 7))	{	  lacl[pos].a_type |= ACL_DEFAULT;	  c += 7;	}      if (!strncmp (c, "user:", 5))	{	  if (c[5] == ':')	    lacl[pos].a_type |= USER_OBJ;	  else	    {	      lacl[pos].a_type |= USER;	      c += 5;	      if (isalpha (*c))		{		  struct passwd *pw = getpwnam (c);		  if (!pw)		    {		      set_errno (EINVAL);		      return NULL;		    }		  lacl[pos].a_id = pw->pw_uid;		  c = strchr (c, ':');		}	      else if (isdigit (*c))		lacl[pos].a_id = strtol (c, &c, 10);	      if (!c || *c != ':')		{		  set_errno (EINVAL);		  return NULL;		}	    }	}      else if (!strncmp (c, "group:", 6))	{	  if (c[5] == ':')	    lacl[pos].a_type |= GROUP_OBJ;	  else	    {	      lacl[pos].a_type |= GROUP;	      c += 5;	      if (isalpha (*c))		{		  struct __group32 *gr = getgrnam32 (c);		  if (!gr)		    {		      set_errno (EINVAL);		      return NULL;		    }		  lacl[pos].a_id = gr->gr_gid;		  c = strchr (c, ':');		}	      else if (isdigit (*c))		lacl[pos].a_id = strtol (c, &c, 10);	      if (!c || *c != ':')		{		  set_errno (EINVAL);		  return NULL;		}	    }	}      else if (!strncmp (c, "mask:", 5))	{	  if (c[5] == ':')	    lacl[pos].a_type |= CLASS_OBJ;	  else	    {	      set_errno (EINVAL);	      return NULL;	    }	}      else if (!strncmp (c, "other:", 6))	{	  if (c[5] == ':')	    lacl[pos].a_type |= OTHER_OBJ;	  else	    {	      set_errno (EINVAL);	      return NULL;	    }	}      if ((lacl[pos].a_perm = permfromstr (c)) == 01000)	{	  set_errno (EINVAL);	  return NULL;	}      ++pos;    }  __aclent16_t *aclp = (__aclent16_t *) malloc (pos * sizeof (__aclent16_t));  if (aclp)    memcpy (aclp, lacl, pos * sizeof (__aclent16_t));  return aclp;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -