Print this page
NEX-1767 ls is unable to display SIDs
Reviewed by: Gordon Ross <gordon.ross@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/ls/ls.c
          +++ new/usr/src/cmd/ls/ls.c
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  25   25   * Copyright 2015 Gary Mills
       26 + *
       27 + * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
  26   28   */
  27   29  
  28   30  /*
  29   31   * Copyright 2017 Jason King.  All rights reserved.
  30   32   * Use is subject to license terms.
  31   33   */
  32   34  
  33   35  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  34   36  /*        All Rights Reserved   */
  35   37  
↓ open down ↓ 187 lines elided ↑ open up ↑
 223  225  static int      first = 1;      /* true if first line is not yet printed */
 224  226  static int      nfiles = 0;     /* number of flist entries in current use */
 225  227  static int      nargs = 0;      /* number of flist entries used for arguments */
 226  228  static int      maxfils = 0;    /* number of flist/lbuf entries allocated */
 227  229  static int      maxn = 0;       /* number of flist entries with lbufs asigned */
 228  230  static int      quantn = 64;    /* allocation growth quantum */
 229  231  
 230  232  static struct lbuf      *nxtlbf;        /* ptr to next lbuf to be assigned */
 231  233  static struct lbuf      **flist;        /* ptr to list of lbuf pointers */
 232  234  static struct lbuf      *gstat(char *, int, struct ditem *);
      235 +static char             *get_sid_name(uid_t, boolean_t, boolean_t);
 233  236  static char             *getname(uid_t);
 234  237  static char             *getgroup(gid_t);
 235  238  static char             *makename(char *, char *);
 236  239  static void             pentry(struct lbuf *);
 237  240  static void             column(void);
 238  241  static void             pmode(mode_t aflag);
 239  242  static void             selection(int *);
 240  243  static void             new_line(void);
 241  244  static void             rddir(char *, struct ditem *);
 242  245  static int              strcol(unsigned char *);
↓ open down ↓ 526 lines elided ↑ open up ↑
 769  772                          continue;
 770  773                  case 'm':
 771  774                          Cflg = 0;
 772  775                          mflg = 1;
 773  776  #ifdef XPG4
 774  777                          lflg = 0;
 775  778  #endif
 776  779                          continue;
 777  780                  case 'n':
 778  781                          nflg++;
 779      -                        lflg++;
 780      -                        statreq++;
 781      -                        Cflg = 0;
 782      -                        xflg = 0;
 783      -                        mflg = 0;
 784      -                        atflg = 0;
      782 +                        if (nflg == 1) {
      783 +                                lflg++;
      784 +                                statreq++;
      785 +                                Cflg = 0;
      786 +                                xflg = 0;
      787 +                                mflg = 0;
      788 +                                atflg = 0;
      789 +                        }
 785  790                          continue;
 786  791                  case 'o':
 787  792                          oflg++;
 788  793                          lflg++;
 789  794                          statreq++;
 790  795                          continue;
 791  796                  case 'p':
 792  797                          pflg++;
 793  798                          statreq++;
 794  799                          continue;
↓ open down ↓ 439 lines elided ↑ open up ↑
1234 1239                  new_line();
1235 1240          }
1236 1241  }
1237 1242  
1238 1243  /*
1239 1244   * print one output entry;
1240 1245   * if uid/gid is not found in the appropriate
1241 1246   * file(passwd/group), then print uid/gid instead of
1242 1247   * user/group name;
1243 1248   */
     1249 +#define         DUMP_EPHEMERAL(x)       printf("%-8lu ", (ulong_t)(x))
1244 1250  static void
1245 1251  pentry(struct lbuf *ap)
1246 1252  {
1247 1253          struct lbuf *p;
1248 1254          char *dmark = "";       /* Used if -p or -F option active */
1249 1255          char *cp;
1250 1256          char *str;
1251 1257  
1252 1258          if (noflist) {
1253 1259                  (void) printf("%s\n", (ap->lflags & ISARG) ? ap->ln.namep :
↓ open down ↓ 9 lines elided ↑ open up ↑
1263 1269                  else
1264 1270                          curcol += printf("%10llu ", (long long)p->lnum);
1265 1271          }
1266 1272          if (sflg) {
1267 1273                  curcol += printf((mflg && !lflg) ? "%lld " :
1268 1274                      (p->lblocks < 10000) ? "%4lld " : "%lld ",
1269 1275                      (p->ltype != 'b' && p->ltype != 'c') ?
1270 1276                      p->lblocks : 0LL);
1271 1277          }
1272 1278          if (lflg) {
     1279 +                boolean_t       res;
     1280 +
1273 1281                  (void) putchar(p->ltype);
1274 1282                  curcol++;
1275 1283                  pmode(p->lflags);
1276 1284  
1277 1285                  /* ACL: additional access mode flag */
1278 1286                  (void) putchar(p->acl);
1279 1287                  curcol++;
1280 1288  
1281 1289                  curcol += printf("%3lu ", (ulong_t)p->lnl);
1282 1290                  if (oflg) {
1283      -                        if (!nflg) {
1284      -                                cp = getname(p->luid);
     1291 +                        boolean_t       usr = B_TRUE;
     1292 +
     1293 +                        if (nflg == 0) {        /* -n not specified; resolve */
     1294 +                                if (p->luid > MAXUID) {
     1295 +                                        res = B_FALSE;
     1296 +                                        cp = get_sid_name(p->luid, usr, res);
     1297 +                                } else {
     1298 +                                        cp = getname(p->luid);
     1299 +                                }
1285 1300                                  curcol += printf("%-8s ", cp);
1286      -                        } else
1287      -                                curcol += printf("%-8lu ", (ulong_t)p->luid);
     1301 +
     1302 +                        } else if (nflg == 1) { /* -n specified; force SID's */
     1303 +                                if (p->luid > MAXUID) {
     1304 +                                        res = B_TRUE;
     1305 +
     1306 +                                        cp = get_sid_name(p->luid, usr, res);
     1307 +                                        curcol += printf("%-8s ", cp);
     1308 +                                } else {
     1309 +                                        curcol += DUMP_EPHEMERAL(p->luid);
     1310 +                                }
     1311 +                        } else          /* -nn specified; force ephemerals */
     1312 +                                curcol += DUMP_EPHEMERAL(p->luid);
1288 1313                  }
1289 1314                  if (gflg) {
1290      -                        if (!nflg) {
1291      -                                cp = getgroup(p->lgid);
     1315 +                        boolean_t       usr = B_FALSE;
     1316 +
     1317 +                        if (nflg == 0) {        /* -n not specified; resolve */
     1318 +                                if (p->lgid > MAXUID) {
     1319 +                                        res = B_FALSE;
     1320 +                                        cp = get_sid_name(p->lgid, usr, res);
     1321 +                                } else {
     1322 +                                        cp = getgroup(p->lgid);
     1323 +                                }
1292 1324                                  curcol += printf("%-8s ", cp);
1293      -                        } else
1294      -                                curcol += printf("%-8lu ", (ulong_t)p->lgid);
     1325 +
     1326 +                        } else if (nflg == 1) { /* -n specified; force SID's */
     1327 +                                if (p->lgid > MAXUID) {
     1328 +                                        res = B_TRUE;
     1329 +
     1330 +                                        cp = get_sid_name(p->lgid, usr, res);
     1331 +                                        curcol += printf("%-8s ", cp);
     1332 +                                } else {
     1333 +                                        curcol += DUMP_EPHEMERAL(p->lgid);
     1334 +                                }
     1335 +                        } else          /* -nn specified; force ephemerals */
     1336 +                                curcol += DUMP_EPHEMERAL(p->lgid);
1295 1337                  }
1296 1338                  if (p->ltype == 'b' || p->ltype == 'c') {
1297 1339                          curcol += printf("%3u, %2u",
1298 1340                              (uint_t)major((dev_t)p->lsize),
1299 1341                              (uint_t)minor((dev_t)p->lsize));
1300 1342                  } else if (hflg) {
1301 1343                          char numbuf[NN_NUMBUF_SZ];
1302 1344  
1303 1345                          nicenum_scale(p->lsize, 1, numbuf, sizeof (numbuf),
1304 1346                              nicenum_flags);
↓ open down ↓ 114 lines elided ↑ open up ↑
1419 1461          }
1420 1462          /* Display file timestamps and extended system attribute timestamps */
1421 1463          if (tmflg && alltm) {
1422 1464                  new_line();
1423 1465                  print_time(p);
1424 1466                  new_line();
1425 1467          }
1426 1468          if (vflg) {
1427 1469                  new_line();
1428 1470                  if (p->aclp) {
1429      -                        acl_printacl(p->aclp, num_cols, Vflg);
     1471 +                        int flgs = ACL_SID_FMT;
     1472 +
     1473 +                        flgs |= Vflg ? ACL_COMPACT_FMT : 0;
     1474 +                        flgs |= (nflg == 1) ? ACL_NORESOLVE : 0;
     1475 +                        flgs |= (nflg >= 2) ? ACL_EPHEMERAL : 0;
     1476 +
     1477 +                        acl_printacl(p->aclp, num_cols, flgs);
1430 1478                  }
1431 1479          }
1432 1480          /* Free extended system attribute lists */
1433 1481          if (saflg || tmflg)
1434 1482                  free_sysattr(p);
1435 1483  }
1436 1484  
1437 1485  /* print various r,w,x permissions */
1438 1486  static void
1439 1487  pmode(mode_t aflag)
↓ open down ↓ 767 lines elided ↑ open up ↑
2207 2255          c = calloc(1, sizeof (struct cachenode));
2208 2256          if (c == NULL) {
2209 2257                  perror("ls");
2210 2258                  exit(2);
2211 2259          }
2212 2260          *parent = c;
2213 2261          c->val = val;
2214 2262          return (c);
2215 2263  }
2216 2264  
     2265 +/*
     2266 + * SID MAX String Length: "http://stackoverflow.com/questions/1140528/"
     2267 + */
     2268 +#define SID_STR_MAX     185                     /* +1 for null char */
     2269 +static char *
     2270 +get_sid_name(uid_t id, boolean_t user, boolean_t res)
     2271 +{
     2272 +        static char     *sid = NULL;
     2273 +        char            *p = NULL;
     2274 +
     2275 +        if (sid_string_by_id(id, user, &sid, res)) {
     2276 +                if ((p = getname(id)) == NULL) {
     2277 +                        /*
     2278 +                         * getname() already converts to ephemeral if id
     2279 +                         * is not found in passwd or group file(s). This
     2280 +                         * should be an extreme case.
     2281 +                         */
     2282 +                        static char      buf[SID_STR_MAX] = {'\0'};
     2283 +
     2284 +                        (void) sprintf(buf, "%-8u", (int)id);
     2285 +                        p = buf;
     2286 +                }
     2287 +        } else
     2288 +                p = sid;
     2289 +
     2290 +        return (p);
     2291 +}
     2292 +
2217 2293  /*
2218 2294   * get name from cache, or passwd file for a given uid;
2219 2295   * lastuid is set to uid.
2220 2296   */
2221 2297  static char *
2222 2298  getname(uid_t uid)
2223 2299  {
2224 2300          struct passwd *pwent;
2225 2301          struct cachenode *c;
2226 2302  
↓ open down ↓ 895 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX