static char * lctx_label (pid_t lcid) { mac_t lctxlabel; char *string; int error; string = NULL; error = mac_prepare_process_label(&lctxlabel); if (error == -1) { warn("mac_prepare_process_label"); return (NULL); } error = mac_get_lcid(lcid, lctxlabel); if (error) { warn("mac_get_lcid"); return (NULL); } error = mac_to_text(lctxlabel, &string); if (error == -1) { mac_free(lctxlabel); return (NULL); } mac_free(lctxlabel); return (string); }
static void maclabel_status(int s) { struct ifreq ifr; mac_t label; char *label_text; memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); if (mac_prepare_ifnet_label(&label) == -1) return; ifr.ifr_ifru.ifru_data = (void *)label; if (ioctl(s, SIOCGIFMAC, &ifr) == -1) goto mac_free; if (mac_to_text(label, &label_text) == -1) goto mac_free; if (strlen(label_text) != 0) printf("\tmaclabel %s\n", label_text); free(label_text); mac_free: mac_free(label); }
int main(int argc, char *argv[]) { char ch, *labellist, *string; mac_t label; int hflag; int error, i; labellist = NULL; hflag = 0; while ((ch = getopt(argc, argv, "hl:")) != -1) { switch (ch) { case 'h': hflag = 1; break; case 'l': if (labellist != NULL) usage(); labellist = argv[optind - 1]; break; default: usage(); } } for (i = optind; i < argc; i++) { if (labellist != NULL) error = mac_prepare(&label, labellist); else error = mac_prepare_file_label(&label); if (error != 0) { perror("mac_prepare"); return (-1); } if (hflag) error = mac_get_link (argv[i], label); else error = mac_get_file (argv[i], label); if (error) { perror(argv[i]); mac_free(label); continue; } error = mac_to_text(label, &string); if (error != 0) perror("mac_to_text"); else { printf("%s: %s\n", argv[i], string); free(string); } mac_free(label); } exit(EX_OK); }
static int get_file_maclabel(char *filename, char *buf, size_t buflen) { mac_t mac = NULL; char *labeltext = NULL; int ret = 0; if (mac_prepare_file_label(&mac)) return 0; if (mac_get_file(filename, mac)) goto done; if (mac_to_text(mac, &labeltext) == 0) { strncpy(buf, labeltext, buflen - 1); buf[buflen - 1] = '\0'; ret = 1; } done: if (mac) mac_free(mac); if (labeltext) free(labeltext); return ret; }
static void maclabel(void) { char *string; mac_t label; int error; error = mac_prepare_process_label(&label); if (error == -1) errx(1, "mac_prepare_type: %s", strerror(errno)); error = mac_get_proc(label); if (error == -1) errx(1, "mac_get_proc: %s", strerror(errno)); error = mac_to_text(label, &string); if (error == -1) errx(1, "mac_to_text: %s", strerror(errno)); (void)printf("%s\n", string); mac_free(label); free(string); }
int main(int argc, char *argv[]) { char *labellist, *string; mac_t label; pid_t pid; int ch, error, pid_set; pid_set = 0; pid = 0; labellist = NULL; while ((ch = getopt(argc, argv, "l:p:")) != -1) { switch (ch) { case 'l': if (labellist != NULL) usage(); labellist = argv[optind - 1]; break; case 'p': if (pid_set) usage(); pid = atoi(argv[optind - 1]); pid_set = 1; break; default: usage(); } } argc -= optind; argv += optind; if (argc != 0) usage(); if (labellist != NULL) error = mac_prepare(&label, labellist); else error = mac_prepare_process_label(&label); if (error != 0) { perror("mac_prepare"); return (-1); } if (pid_set) { error = mac_get_pid(pid, label); if (error) perror("mac_get_pid"); } else { error = mac_get_proc(label); if (error) perror("mac_get_proc"); } if (error) { mac_free(label); exit (-1); } error = mac_to_text(label, &string); if (error != 0) { perror("mac_to_text"); exit(EX_DATAERR); } if (strlen(string) > 0) printf("%s\n", string); mac_free(label); free(string); exit(EX_OK); }
/* * Display() takes a linked list of FTSENT structures and passes the list * along with any other necessary information to the print function. P * points to the parent directory of the display list. */ static void display(const FTSENT *p, FTSENT *list, int options) { struct stat *sp; DISPLAY d; FTSENT *cur; NAMES *np; off_t maxsize; long maxblock; uintmax_t maxinode; u_long btotal, labelstrlen, maxlen, maxnlink; u_long maxlabelstr; u_int sizelen; int maxflags; gid_t maxgroup; uid_t maxuser; size_t flen, ulen, glen; char *initmax; int entries, needstats; const char *user, *group; char *flags, *labelstr = NULL; char ngroup[STRBUF_SIZEOF(uid_t) + 1]; char nuser[STRBUF_SIZEOF(gid_t) + 1]; needstats = f_inode || f_longform || f_size; flen = 0; btotal = 0; initmax = getenv("LS_COLWIDTHS"); /* Fields match -lios order. New ones should be added at the end. */ maxlabelstr = maxblock = maxlen = maxnlink = 0; maxuser = maxgroup = maxflags = maxsize = 0; maxinode = 0; if (initmax != NULL && *initmax != '\0') { char *initmax2, *jinitmax; int ninitmax; /* Fill-in "::" as "0:0:0" for the sake of scanf. */ jinitmax = malloc(strlen(initmax) * 2 + 2); if (jinitmax == NULL) err(1, "malloc"); initmax2 = jinitmax; if (*initmax == ':') strcpy(initmax2, "0:"), initmax2 += 2; else *initmax2++ = *initmax, *initmax2 = '\0'; for (initmax++; *initmax != '\0'; initmax++) { if (initmax[-1] == ':' && initmax[0] == ':') { *initmax2++ = '0'; *initmax2++ = initmax[0]; initmax2[1] = '\0'; } else { *initmax2++ = initmax[0]; initmax2[1] = '\0'; } } if (initmax2[-1] == ':') strcpy(initmax2, "0"); ninitmax = sscanf(jinitmax, " %ju : %ld : %lu : %u : %u : %i : %jd : %lu : %lu ", &maxinode, &maxblock, &maxnlink, &maxuser, &maxgroup, &maxflags, &maxsize, &maxlen, &maxlabelstr); f_notabs = 1; switch (ninitmax) { case 0: maxinode = 0; /* FALLTHROUGH */ case 1: maxblock = 0; /* FALLTHROUGH */ case 2: maxnlink = 0; /* FALLTHROUGH */ case 3: maxuser = 0; /* FALLTHROUGH */ case 4: maxgroup = 0; /* FALLTHROUGH */ case 5: maxflags = 0; /* FALLTHROUGH */ case 6: maxsize = 0; /* FALLTHROUGH */ case 7: maxlen = 0; /* FALLTHROUGH */ case 8: maxlabelstr = 0; /* FALLTHROUGH */ #ifdef COLORLS if (!f_color) #endif f_notabs = 0; /* FALLTHROUGH */ default: break; } MAKENINES(maxinode); MAKENINES(maxblock); MAKENINES(maxnlink); MAKENINES(maxsize); free(jinitmax); } d.s_size = 0; sizelen = 0; flags = NULL; for (cur = list, entries = 0; cur; cur = cur->fts_link) { if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { warnx("%s: %s", cur->fts_name, strerror(cur->fts_errno)); cur->fts_number = NO_PRINT; rval = 1; continue; } /* * P is NULL if list is the argv list, to which different rules * apply. */ if (p == NULL) { /* Directories will be displayed later. */ if (cur->fts_info == FTS_D && !f_listdir) { cur->fts_number = NO_PRINT; continue; } } else { /* Only display dot file if -a/-A set. */ if (cur->fts_name[0] == '.' && !f_listdot) { cur->fts_number = NO_PRINT; continue; } } if (cur->fts_namelen > maxlen) maxlen = cur->fts_namelen; if (f_octal || f_octal_escape) { u_long t = len_octal(cur->fts_name, cur->fts_namelen); if (t > maxlen) maxlen = t; } if (needstats) { sp = cur->fts_statp; if (sp->st_blocks > maxblock) maxblock = sp->st_blocks; if (sp->st_ino > maxinode) maxinode = sp->st_ino; if (sp->st_nlink > maxnlink) maxnlink = sp->st_nlink; if (sp->st_size > maxsize) maxsize = sp->st_size; btotal += sp->st_blocks; if (f_longform) { if (f_numericonly) { (void)snprintf(nuser, sizeof(nuser), "%u", sp->st_uid); (void)snprintf(ngroup, sizeof(ngroup), "%u", sp->st_gid); user = nuser; group = ngroup; } else { user = user_from_uid(sp->st_uid, 0); group = group_from_gid(sp->st_gid, 0); } if ((ulen = strlen(user)) > maxuser) maxuser = ulen; if ((glen = strlen(group)) > maxgroup) maxgroup = glen; if (f_flags) { flags = fflagstostr(sp->st_flags); if (flags != NULL && *flags == '\0') { free(flags); flags = strdup("-"); } if (flags == NULL) err(1, "fflagstostr"); flen = strlen(flags); if (flen > (size_t)maxflags) maxflags = flen; } else flen = 0; labelstr = NULL; if (f_label) { char name[PATH_MAX + 1]; mac_t label; int error; error = mac_prepare_file_label(&label); if (error == -1) { warn("MAC label for %s/%s", cur->fts_parent->fts_path, cur->fts_name); goto label_out; } if (cur->fts_level == FTS_ROOTLEVEL) snprintf(name, sizeof(name), "%s", cur->fts_name); else snprintf(name, sizeof(name), "%s/%s", cur->fts_parent-> fts_accpath, cur->fts_name); if (options & FTS_LOGICAL) error = mac_get_file(name, label); else error = mac_get_link(name, label); if (error == -1) { warn("MAC label for %s/%s", cur->fts_parent->fts_path, cur->fts_name); mac_free(label); goto label_out; } error = mac_to_text(label, &labelstr); if (error == -1) { warn("MAC label for %s/%s", cur->fts_parent->fts_path, cur->fts_name); mac_free(label); goto label_out; } mac_free(label); label_out: if (labelstr == NULL) labelstr = strdup("-"); labelstrlen = strlen(labelstr); if (labelstrlen > maxlabelstr) maxlabelstr = labelstrlen; } else labelstrlen = 0; if ((np = malloc(sizeof(NAMES) + labelstrlen + ulen + glen + flen + 4)) == NULL) err(1, "malloc"); np->user = &np->data[0]; (void)strcpy(np->user, user); np->group = &np->data[ulen + 1]; (void)strcpy(np->group, group); if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) { sizelen = snprintf(NULL, 0, "%#jx", (uintmax_t)sp->st_rdev); if (d.s_size < sizelen) d.s_size = sizelen; } if (f_flags) { np->flags = &np->data[ulen + glen + 2]; (void)strcpy(np->flags, flags); free(flags); } if (f_label) { np->label = &np->data[ulen + glen + 2 + (f_flags ? flen + 1 : 0)]; (void)strcpy(np->label, labelstr); free(labelstr); } cur->fts_pointer = np; } } ++entries; } /* * If there are no entries to display, we normally stop right * here. However, we must continue if we have to display the * total block count. In this case, we display the total only * on the second (p != NULL) pass. */ if (!entries && (!(f_longform || f_size) || p == NULL)) return; d.list = list; d.entries = entries; d.maxlen = maxlen; if (needstats) { d.btotal = btotal; d.s_block = snprintf(NULL, 0, "%lu", howmany(maxblock, blocksize)); d.s_flags = maxflags; d.s_label = maxlabelstr; d.s_group = maxgroup; d.s_inode = snprintf(NULL, 0, "%ju", maxinode); d.s_nlink = snprintf(NULL, 0, "%lu", maxnlink); sizelen = f_humanval ? HUMANVALSTR_LEN : snprintf(NULL, 0, "%ju", maxsize); if (d.s_size < sizelen) d.s_size = sizelen; d.s_user = maxuser; } if (f_thousands) /* make space for commas */ d.s_size += (d.s_size - 1) / 3; printfcn(&d); output = 1; if (f_longform) for (cur = list; cur; cur = cur->fts_link) free(cur->fts_pointer); }