static int spade_setxattr(const char *path, const char *name, const char *value, size_t size, int flags, uint32_t position) { int res; if (!strncmp(name, XATTR_APPLE_PREFIX, sizeof (XATTR_APPLE_PREFIX) - 1)) { flags &= ~(XATTR_NOSECURITY); } if (!strcmp(name, A_KAUTH_FILESEC_XATTR)) { char new_name[MAXPATHLEN]; memcpy(new_name, A_KAUTH_FILESEC_XATTR, sizeof (A_KAUTH_FILESEC_XATTR)); memcpy(new_name, G_PREFIX, sizeof (G_PREFIX) - 1); res = setxattr(path, new_name, value, size, position, flags); } else { res = setxattr(path, name, value, size, position, flags); } if (res == -1) { return -errno; } return 0; }
static int local_set_xattr(const char *path, FsCred *credp) { int err; if (credp->fc_uid != -1) { uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0); if (err) { return err; } } if (credp->fc_gid != -1) { uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0); if (err) { return err; } } if (credp->fc_mode != -1) { uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0); if (err) { return err; } } if (credp->fc_rdev != -1) { uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0); if (err) { return err; } } return 0; }
static int local_set_xattr(const char *path, FsCred *credp) { int err; if (credp->fc_uid != -1) { err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t), 0); if (err) { return err; } } if (credp->fc_gid != -1) { err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t), 0); if (err) { return err; } } if (credp->fc_mode != -1) { err = setxattr(path, "user.virtfs.mode", &credp->fc_mode, sizeof(mode_t), 0); if (err) { return err; } } if (credp->fc_rdev != -1) { err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev, sizeof(dev_t), 0); if (err) { return err; } } return 0; }
static INT64_T chirp_fs_local_setxattr(const char *path, const char *name, const void *data, size_t size, int flags) { #ifdef CCTOOLS_OPSYS_DARWIN return setxattr(path, name, data, size, 0, flags); #else return setxattr(path, name, data, size, flags); #endif }
int mark_pos(const char *path, char *pos, const char *ts) { if(strlen(pos) == 0) { *pos = '0'; } setxattr(path, XATTR_ANALYZER_POS, pos, strlen(pos), 0); setxattr(path, XATTR_ANALYZER_POS_TS, ts, strlen(ts), 0); return; }
static int _setxattr(const char* path, const char* name, void* value, size_t size) { #if defined(__LWI_DARWIN__) return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW); #else return setxattr(path, name, value, size, 0); #endif }
static INT64_T chirp_fs_local_setxattr(const char *path, const char *name, const void *data, size_t size, int flags) { PREAMBLE("setxattr(`%s', `%s', %p, %zu, %d)", path, name, data, size, flags); RESOLVE(path) #ifdef CCTOOLS_OPSYS_DARWIN rc = setxattr(path, name, data, size, 0, flags); #else rc = setxattr(path, name, data, size, flags); #endif PROLOGUE }
void setxattrs(const std::string &path) { setxattr(path.c_str(), kQuarantineKey.c_str(), (void *)kQuarantineValue, sizeof(kQuarantineValue), 0, XATTR_NOFOLLOW); setxattr(path.c_str(), kMetedataKey.c_str(), (void *)kMetadataWhereFromValue, sizeof(kMetadataWhereFromValue), 0, XATTR_NOFOLLOW); setxattr(path.c_str(), kFsckKey.c_str(), (void *)kFsckValue, sizeof(kFsckValue), 0, XATTR_NOFOLLOW); // insert arbitrary xattr const std::string key = "foobar"; const unsigned char val[] = {0x62, 0x61, 0x7A}; // baz setxattr(path.c_str(), key.c_str(), (void *)val, sizeof(val), 0, XATTR_NOFOLLOW); }
static int xsetxattr(const char *path, const char *name, const void *value, size_t size, int flags, int symlink) { #ifdef XATTR_EXTRA_ARGS return setxattr(path, name, value, size, 0, flags | symlink ? XATTR_NOFOLLOW : 0); #else switch (symlink) { case 0: return setxattr(path, name, value, size, flags); default: return lsetxattr(path, name, value, size, flags); } #endif }
int nfs4_set_acl(struct nfs4_acl *acl, const char *path) { int res = 0; char *xdrbuf = NULL; res = acl_nfs4_xattr_pack(acl, &xdrbuf); if (res <= 0) { fprintf(stderr, "Failed to populate xattr from nfs4acl\n"); goto out_free; } res = setxattr(path, ACL_NFS4_XATTR, (char *)xdrbuf, res, XATTR_REPLACE); if (res < -10000) { fprintf(stderr,"An internal NFS server error code (%d) was returned; this should never happen.\n",res); goto out_free; } else if (res < 0) { if (errno == EOPNOTSUPP) fprintf(stderr,"Operation to set ACL not supported.\n"); else if (errno == ENOATTR) fprintf(stderr,"ACL Attribute not found on file.\n"); else if (errno == EREMOTEIO) fprintf(stderr,"An NFS server error occurred.\n"); else perror("Failed setxattr operation"); } out_free: free(xdrbuf); return res; }
static void setup(void) { int fd; tst_require_root(NULL); tst_tmpdir(); /* Test for xattr support and set attr value */ fd = creat(TESTFILE, 0644); if (fd == -1) tst_brkm(TBROK | TERRNO, cleanup, "Create %s failed", TESTFILE); close(fd); if (setxattr(TESTFILE, XATTR_TEST_KEY, XATTR_TEST_VALUE, XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == -1) { if (errno == ENOTSUP) tst_brkm(TCONF, cleanup, "No xattr support in fs or " "fs mounted without user_xattr option"); else tst_brkm(TBROK | TERRNO, cleanup, "setxattr %s failed", TESTFILE); } TEST_PAUSE; }
static int put_sha1_file(char *name) { uint32_t count; if (getxattr(name, CNAME, &count, CSIZE) < 0) { if (errno == ENOENT) { dprintf("sha1 file doesn't exist\n"); return -1; } else { panic("%m\n"); } } count--; if (count == 0) { if (unlink(name) < 0) { dprintf("%m\n"); return -1; } dprintf("%s deleted\n", name); } else { if (setxattr(name, CNAME, &count, CSIZE, 0) < 0) panic("%m\n"); } return 0; }
static void get_sha1_file(char *name) { uint32_t count; if (getxattr(name, CNAME, &count, CSIZE) < 0) { if (errno == ENODATA) { count = 1; if (setxattr(name, CNAME, &count, CSIZE, 0) < 0) panic("%m\n"); return; } else panic("%m\n"); } count++; if (setxattr(name, CNAME, &count, CSIZE, 0) < 0) panic("%m\n"); }
/* 23.4.22 */ int acl_set_file(const char *path_p, acl_type_t type, acl_t acl) { acl_obj *acl_obj_p = ext2int(acl, acl); char *ext_acl_p; const char *name; size_t size; int error; if (!acl_obj_p) return -1; switch (type) { case ACL_TYPE_ACCESS: name = ACL_EA_ACCESS; break; case ACL_TYPE_DEFAULT: name = ACL_EA_DEFAULT; break; default: errno = EINVAL; return -1; } ext_acl_p = __acl_to_xattr(acl_obj_p, &size); if (!ext_acl_p) return -1; error = setxattr(path_p, name, (char *)ext_acl_p, size, 0); free(ext_acl_p); return error; }
static int xmp_create(const char* path, mode_t mode, struct fuse_file_info* fi) { char fpath[PATH_MAX]; xmp_getfullpath(fpath, path); (void) fi; (void) mode; FILE *fd = fopen(fpath, "wb+"); fprintf(stderr, "CREATE: fpath: %s\n", fpath); if(!do_crypt(fd, fd, ENCRYPT, ENCFS_DATA->passkey)){ fprintf(stderr, "Create: do_crypt failed\n"); } fprintf(stderr, "Create: encryption done correctly\n"); fclose(fd); if(setxattr(fpath, XATRR_ENCRYPTED_FLAG, "true", 4, 0)){ fprintf(stderr, "error setting xattr of file %s\n", fpath); return -errno; } fprintf(stderr, "Create: file xatrr correctly set %s\n", fpath); return 0; }
static int enc_create(const char* path, mode_t mode, struct fuse_file_info* fi) { char fpath[PATH_MAX]; enc_fullpath(fpath, path); (void) fi; (void) mode; FILE *f = fopen(fpath, "wb+"); fprintf(stderr, "CREATE: fpath: %s\n", fpath); /* It is okay to encrypt a file into itself as long as it's empty * otherwise the contents of the file would be erased. */ if(!do_crypt(f, f, ENCRYPT, ENC_DATA->password)){ fprintf(stderr, "Create: do_crypt failed\n"); } fprintf(stderr, "Create: encryption done correctly\n"); fclose(f); if(setxattr(fpath, XATRR_ENCRYPTED_FLAG, ENCRYPTED, 4, 0)){ fprintf(stderr, "error setting xattr of file %s\n", fpath); return -errno; } fprintf(stderr, "Create: file xatrr correctly set %s\n", fpath); return 0; }
static int rm_xattr_set(RmFile *file, const char *key, const char *value, size_t value_size) { RM_DEFINE_PATH(file); return rm_xattr_is_fail("setxattr", setxattr(file_path, key, value, value_size, 0)); }
int sys_lsetxattr (const char *path, const char *uname, const void *value, size_t size, int flags) { const char *name = prefix(uname); #if defined(HAVE_LSETXATTR) return lsetxattr(path, name, value, size, flags); #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT) int options = XATTR_NOFOLLOW; return setxattr(path, name, value, size, 0, options); #elif defined(LSETEA) return lsetea(path, name, value, size, flags); #elif defined(HAVE_EXTATTR_SET_LINK) int retval = 0; if (flags) { /* Check attribute existence */ retval = extattr_get_link(path, EXTATTR_NAMESPACE_USER, uname, NULL, 0); if (retval < 0) { /* REPLACE attribute, that doesn't exist */ if (flags & XATTR_REPLACE && errno == ENOATTR) { errno = ENOATTR; return -1; } /* Ignore other errors */ } else { /* CREATE attribute, that already exists */ if (flags & XATTR_CREATE) { errno = EEXIST; return -1; } } } retval = extattr_set_link(path, EXTATTR_NAMESPACE_USER, uname, value, size); return (retval < 0) ? -1 : 0; #elif defined(HAVE_ATTR_SET) int myflags = ATTR_DONTFOLLOW; char *attrname = strchr(name,'.') + 1; if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT; if (flags & XATTR_CREATE) myflags |= ATTR_CREATE; if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE; return attr_set(path, attrname, (const char *)value, size, myflags); #elif defined(HAVE_ATTROPEN) int ret = -1; int myflags = O_RDWR | AT_SYMLINK_NOFOLLOW; int attrfd; if (flags & XATTR_CREATE) myflags |= O_EXCL; if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT; attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE); if (attrfd >= 0) { ret = solaris_write_xattr(attrfd, value, size); close(attrfd); } return ret; #else errno = ENOSYS; return -1; #endif }
int cap_set_file(const char *filename, cap_t cap_d) { struct vfs_cap_data rawvfscap; int sizeofcaps; struct stat buf; if (lstat(filename, &buf) != 0) { _cap_debug("unable to stat file [%s]", filename); return -1; } if (S_ISLNK(buf.st_mode) || !S_ISREG(buf.st_mode)) { _cap_debug("file [%s] is not a regular file", filename); errno = EINVAL; return -1; } if (cap_d == NULL) { _cap_debug("removing filename capabilities"); return removexattr(filename, XATTR_NAME_CAPS); } else if (_fcaps_save(&rawvfscap, cap_d, &sizeofcaps) != 0) { return -1; } _cap_debug("setting filename capabilities"); return setxattr(filename, XATTR_NAME_CAPS, &rawvfscap, sizeofcaps, 0); }
static int testUserXattrEnabled(void) { int ret = -1; ssize_t len; const char *con_value = "system_u:object_r:svirt_image_t:s0:c41,c264"; char *path = NULL; if (virAsprintf(&path, "%s/securityselinuxlabeldata/testxattr", abs_builddir) < 0) goto cleanup; if (virFileMakePath(abs_builddir "/securityselinuxlabeldata") < 0 || virFileTouch(path, 0600) < 0) goto cleanup; len = setxattr(path, "user.libvirt.selinux", con_value, strlen(con_value), 0); if (len < 0) { if (errno == EOPNOTSUPP) ret = 0; goto cleanup; } ret = 1; cleanup: unlink(path); rmdir(abs_builddir "/securityselinuxlabeldata"); VIR_FREE(path); return ret; }
static int pifs_setxattr(const char *path, const char *name, const char *value, size_t size, int flags) { FULL_PATH(path); int ret = setxattr(full_path, name, value, size, flags); return ret == -1 ? -errno : ret; }
//attach file at <source> to attributes of <target> int attach_file(char * const source, char * const target) { FILE *inf; const char attr_name[] = ATTR_NAME; int size = DEFAULT_MAX_FILE_SIZE; byte *buffer; int bytes_read; if (!(buffer = (byte *) malloc(sizeof(byte) * DEFAULT_MAX_FILE_SIZE))) { error_exit(ATTRACH_ERR_FILE); } if (!(inf = fopen(source, "r+"))) { error_exit(ATTRACH_ERR_FILE); } bytes_read = fread(buffer, sizeof(byte), size, inf); if (verbose_flag) printf("%d\n", bytes_read); if (setxattr(target, attr_name, buffer, bytes_read, 0, XATTR_NOFOLLOW)) { free(buffer); error_exit(ATTRACH_ERR_ATTACH); } free(buffer); fclose(inf); return 0; }
void xainc(wchar_t *file, char *an, off_t inc) { char buf[32]; ssize_t al; off_t val; char *fn; if(file[0] != L'/') return; if((fn = icswcstombs(file, NULL, NULL)) == NULL) { flog(LOG_WARNING, "could not convert filename %ls into local charset: %s", file, strerror(errno)); return; } if((al = getxattr(fn, an, buf, sizeof(buf) - 1)) < 0) { if(errno != ENOATTR) { flog(LOG_WARNING, "could not get xattr %s on %s: %s", an, fn, strerror(errno)); return; } val = 0; } else { buf[al] = 0; val = strtoll(buf, NULL, 10); } val += inc; al = snprintf(buf, sizeof(buf), "%ji", (intmax_t)val); if(setxattr(fn, an, buf, al, 0) < 0) flog(LOG_WARNING, "could not set xattr %s on %s: %s", an, fn, strerror(errno)); }
void basic_xattr_test(const char *mountpt) { char name[100]; int h; int result; int val1; int valread; strcpy(name,mountpt); strcat(name,"/"); strcat(name,"xfile"); unlink(name); h = open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); close(h); printf("Start\n"); list_xattr(name); printf("Add an attribute\n"); val1 = 0x123456; result = setxattr(name,"foo",&val1,sizeof(val1),0); printf("wrote attribute foo: result %d\n",result); list_xattr(name); printf("Add an attribute\n"); val1 = 0x7890; result = setxattr(name,"bar",&val1,sizeof(val1),0); printf("wrote attribute bar: result %d\n",result); list_xattr(name); printf("Get non-existanrt attribute\n"); print_xattrib_val(name,"not here"); printf("Delete non existing attribute\n"); removexattr(name,"not here"); list_xattr(name); printf("Remove foo\n"); removexattr(name,"foo"); list_xattr(name); printf("Remove bar\n"); removexattr(name,"bar"); list_xattr(name); }
/* xattr operations are optional and can safely be left unimplemented */ static int xmp_setxattr(const char *path, const char *name, const char *value, size_t size, int flags, uint32_t position) { int res = setxattr(path, name, value, size, position, flags | XATTR_NOFOLLOW); if (res == -1) return -errno; return 0; }
static ssize_t xsetxattr(const char *path, char *list, char *value, size_t size) { ssize_t ret = setxattr(path, list, value, size, 0); if (ret < 0) err(1, "setxattr() failed"); return ret; }
static int set_erasure_index(const char *path, uint8_t idx) { if (setxattr(path, ECNAME, &idx, ECSIZE, 0) < 0) { sd_err("failed to setxattr %s, %m", path); return -1; } return 0; }
int bproc_nodesetstatus(int node, char *status) { char *path; get_node_path(path, node); if (!path) return -1; return setxattr(path, BPROC_STATE_XATTR, status, strlen(status), 0); }
int bproc_setnodeattr(int node, char *name, void *value, int size) { char *p; char *nametmp; get_node_path(p, node); nametmp = alloca(strlen(name) + 7); sprintf(nametmp, "bproc.%s", name); return setxattr(p, nametmp, value, size, 0); }
static int event_set_timestamp(const gchar *path, time_t when) { gchar str[128]; gsize str_len; str_len = g_snprintf(str, sizeof(str), "%ld", when); return 0 == setxattr(path, xattr_event_timestamp, str, str_len, 0); }