merge with master
[external/tizen-coreutils.git] / packaging / coreutils-6.9-smack.patch
1 diff -Nuarp tizen-coreutils/src/copy.c tizen-coreutils-smack/src/copy.c
2 --- tizen-coreutils/src/copy.c  2012-11-30 11:18:57.473521424 +0200
3 +++ tizen-coreutils-smack/src/copy.c    2012-11-30 11:26:01.601502515 +0200
4 @@ -53,6 +53,7 @@
5  #include "utimens.h"
6  #include "xreadlink.h"
7  #include "yesno.h"
8 +#include "smack.h"
9  
10  #ifndef HAVE_FCHOWN
11  # define HAVE_FCHOWN false
12 @@ -570,6 +574,28 @@ copy_reg (char const *src_name, char con
13         }
14      }
15  
16 +  if (x->preserve_context)
17 +    {
18 +      char src_context[SMACK_LABELLEN];
19 +      char dst_context[SMACK_LABELLEN];
20 +
21 +      if (smack_of_fd (source_desc, src_context, SMACK_LABELLEN) < 0)
22 +        {
23 +          if (x->require_preserve)
24 +            ; //fail silently now // return_val = false;
25 +        }
26 +      else if (smack_of_fd (dest_desc, dst_context, SMACK_LABELLEN) < 0)
27 +        {
28 +          if (x->require_preserve)
29 +            ; //fail silently now // return_val = false;
30 +        }
31 +      else if (strcmp(src_context, dst_context))
32 +        {
33 +          if (smack_to_fd(dest_desc, src_context) < 0 && x->require_preserve)
34 +            ; //fail silently now // return_val = false;
35 +        }
36 +    }
37 +
38  close_src_and_dst_desc:
39    if (close (dest_desc) < 0)
40      {
41 diff -Nuarp tizen-coreutils/src/copy.h tizen-coreutils-smack/src/copy.h
42 --- tizen-coreutils/src/copy.h  2012-11-30 10:11:38.000000000 +0200
43 +++ tizen-coreutils-smack/src/copy.h    2012-11-30 11:20:39.837516860 +0200
44 @@ -128,6 +128,10 @@ struct cp_options
45    bool preserve_mode;
46    bool preserve_timestamps;
47  
48 +  /* If true, attempt to give the copies the original files'
49 +     security context. */
50 +  bool preserve_context;
51 +
52    /* Enabled for mv, and for cp by the --preserve=links option.
53       If true, attempt to preserve in the destination files any
54       logical hard links between the source files.  If used with cp's
55 diff -Nuarp tizen-coreutils/src/cp.c tizen-coreutils-smack/src/cp.c
56 --- tizen-coreutils/src/cp.c    2012-11-30 10:11:38.000000000 +0200
57 +++ tizen-coreutils-smack/src/cp.c      2012-11-30 11:20:39.837516860 +0200
58 @@ -35,6 +35,7 @@
59  #include "stat-time.h"
60  #include "utimens.h"
61  #include "acl.h"
62 +#include "smack.h"
63  
64  #define ASSIGN_BASENAME_STRDUPA(Dest, File_name)       \
65    do                                                   \
66 @@ -191,7 +192,7 @@ Mandatory arguments to long options are
67    -p                           same as --preserve=mode,ownership,timestamps\n\
68        --preserve[=ATTR_LIST]   preserve the specified attributes (default:\n\
69                                   mode,ownership,timestamps), if possible\n\
70 -                                 additional attributes: links, all\n\
71 +                                 additional attributes: links, context, all\n\
72  "), stdout);
73        fputs (_("\
74        --no-preserve=ATTR_LIST  don't preserve the specified attributes\n\
75 @@ -317,6 +318,27 @@ re_protect (char const *const_dst_name,
76             }
77         }
78  
79 +      if (x->preserve_context)
80 +        {
81 +          char src_context[SMACK_LABELLEN];
82 +          char dst_context[SMACK_LABELLEN];
83 +
84 +          if (smack_of_file (src_name, src_context, SMACK_LABELLEN) < 0 ||
85 +              smack_of_file (dst_name, dst_context, SMACK_LABELLEN) < 0)
86 +            {
87 +             ; //error (0, errno, _("failed to preserve context for %s"),
88 +               //     quote (dst_name));
89 +             //return false;
90 +            }
91 +          if (strcmp (src_context, dst_context) &&
92 +              smack_to_file (dst_name, src_context) < 0)
93 +            {
94 +             ; //error (0, errno, _("failed to preserve context for %s"),
95 +               //     quote (dst_name));
96 +             //return false;
97 +            }
98 +        }
99 +
100        if (x->preserve_ownership)
101         {
102           if (chown (dst_name, src_sb.st_uid, src_sb.st_gid) != 0
103 @@ -749,6 +771,7 @@ cp_option_init (struct cp_options *x)
104    x->preserve_links = false;
105    x->preserve_mode = false;
106    x->preserve_timestamps = false;
107 +  x->preserve_context = false;
108  
109    x->require_preserve = false;
110    x->recursive = false;
111 @@ -777,18 +800,19 @@ decode_preserve_arg (char const *arg, st
112        PRESERVE_TIMESTAMPS,
113        PRESERVE_OWNERSHIP,
114        PRESERVE_LINK,
115 +      PRESERVE_CONTEXT,
116        PRESERVE_ALL
117      };
118    static enum File_attribute const preserve_vals[] =
119      {
120        PRESERVE_MODE, PRESERVE_TIMESTAMPS,
121 -      PRESERVE_OWNERSHIP, PRESERVE_LINK, PRESERVE_ALL
122 +      PRESERVE_OWNERSHIP, PRESERVE_LINK, PRESERVE_CONTEXT, PRESERVE_ALL
123      };
124    /* Valid arguments to the `--preserve' option. */
125    static char const* const preserve_args[] =
126      {
127        "mode", "timestamps",
128 -      "ownership", "links", "all", NULL
129 +      "ownership", "links", "context", "all", NULL
130      };
131    ARGMATCH_VERIFY (preserve_args, preserve_vals);
132  
133 @@ -824,11 +848,16 @@ decode_preserve_arg (char const *arg, st
134           x->preserve_links = on_off;
135           break;
136  
137 +       case PRESERVE_CONTEXT:
138 +         x->preserve_context = on_off;
139 +         break;
140 +
141         case PRESERVE_ALL:
142           x->preserve_mode = on_off;
143           x->preserve_timestamps = on_off;
144           x->preserve_ownership = on_off;
145           x->preserve_links = on_off;
146 +         x->preserve_context = on_off;
147           break;
148  
149         default:
150 @@ -885,6 +914,8 @@ main (int argc, char **argv)
151           x.preserve_ownership = true;
152           x.preserve_mode = true;
153           x.preserve_timestamps = true;
154 +          /* Context preservation may be draconian */
155 +         x.preserve_context = true;
156           x.require_preserve = true;
157           x.recursive = true;
158           break;
159 diff -Nuarp tizen-coreutils/src/id.c tizen-coreutils-smack/src/id.c
160 --- tizen-coreutils/src/id.c    2012-11-30 10:11:38.000000000 +0200
161 +++ tizen-coreutils-smack/src/id.c      2012-11-30 11:20:39.837516860 +0200
162 @@ -29,6 +29,7 @@
163  #include "system.h"
164  #include "error.h"
165  #include "quote.h"
166 +#include "smack.h"
167  
168  /* The official name of this program (e.g., no `g' prefix).  */
169  #define PROGRAM_NAME "id"
170 @@ -40,6 +41,7 @@ int getugroups ();
171  static void print_user (uid_t uid);
172  static void print_group (gid_t gid);
173  static void print_group_list (const char *username);
174 +static void print_context (void);
175  static void print_full_info (const char *username);
176  
177  /* The name this program was run with. */
178 @@ -52,11 +54,15 @@ static bool use_name = false;
179  static uid_t ruid, euid;
180  static gid_t rgid, egid;
181  
182 +/* The security "context" to print. */
183 +static char context[SMACK_LABELLEN];
184 +
185  /* True unless errors have been encountered.  */
186  static bool ok = true;
187  
188  static struct option const longopts[] =
189  {
190 +  {"context", no_argument, NULL, 'Z'},
191    {"group", no_argument, NULL, 'g'},
192    {"groups", no_argument, NULL, 'G'},
193    {"name", no_argument, NULL, 'n'},
194 @@ -80,6 +86,7 @@ usage (int status)
195  Print information for USERNAME, or the current user.\n\
196  \n\
197    -a              ignore, for compatibility with other versions\n\
198 +  -Z, --context   print only the security context\n\
199    -g, --group     print only the effective group ID\n\
200    -G, --groups    print all group IDs\n\
201    -n, --name      print a name instead of a number, for -ugG\n\
202 @@ -102,6 +109,8 @@ main (int argc, char **argv)
203  {
204    int optc;
205  
206 +  /* If true, output only the security context. -Z */
207 +  bool just_context = false;
208    /* If true, output the list of all group IDs. -G */
209    bool just_group_list = false;
210    /* If true, output only the group ID(s). -g */
211 @@ -119,13 +128,16 @@ main (int argc, char **argv)
212  
213    atexit (close_stdout);
214  
215 -  while ((optc = getopt_long (argc, argv, "agnruG", longopts, NULL)) != -1)
216 +  while ((optc = getopt_long (argc, argv, "agnruGZ", longopts, NULL)) != -1)
217      {
218        switch (optc)
219         {
220         case 'a':
221           /* Ignore -a, for compatibility with SVR4.  */
222           break;
223 +       case 'Z':
224 +         just_context = true;
225 +         break;
226         case 'g':
227           just_group = true;
228           break;
229 @@ -148,8 +160,8 @@ main (int argc, char **argv)
230         }
231      }
232  
233 -  if (just_user + just_group + just_group_list > 1)
234 -    error (EXIT_FAILURE, 0, _("cannot print only user and only group"));
235 +  if (just_user + just_group + just_group_list + just_context > 1)
236 +    error (EXIT_FAILURE, 0, _("cannot print multiple exclusive fields"));
237  
238    if (just_user + just_group + just_group_list == 0 && (use_real | use_name))
239      error (EXIT_FAILURE, 0,
240 @@ -183,6 +195,8 @@ main (int argc, char **argv)
241      print_group (use_real ? rgid : egid);
242    else if (just_group_list)
243      print_group_list (argv[optind]);
244 +  else if (just_context)
245 +    print_context ();
246    else
247      print_full_info (argv[optind]);
248    putchar ('\n');
249 @@ -324,6 +338,18 @@ print_group_list (const char *username)
250  #endif /* HAVE_GETGROUPS */
251  }
252  
253 +/* Print the security context. */
254 +
255 +static void
256 +print_context (void)
257 +{
258 +
259 +  if (smack_of_proc(-1, context, sizeof (context)) < 1 || strlen(context) < 1)
260 +    printf ("<secuirty context is unavailable>");
261 +  else
262 +    printf ("%s", context);
263 +}
264 +
265  /* Print all of the info about the user's user and group IDs. */
266  
267  static void
268 @@ -385,4 +411,7 @@ print_full_info (const char *username)
269      free (groups);
270    }
271  #endif /* HAVE_GETGROUPS */
272 +
273 +  if (smack_of_proc(-1, context, sizeof (context)) > 0 && strlen(context) > 0)
274 +    printf (" context=\"%s\"", context);
275  }
276 diff -Nuarp tizen-coreutils/src/install.c tizen-coreutils-smack/src/install.c
277 --- tizen-coreutils/src/install.c       2012-11-30 10:11:38.000000000 +0200
278 +++ tizen-coreutils-smack/src/install.c 2012-11-30 11:20:39.837516860 +0200
279 @@ -39,6 +39,7 @@
280  #include "stat-time.h"
281  #include "utimens.h"
282  #include "xstrtol.h"
283 +#include "smack.h"
284  
285  /* The official name of this program (e.g., no `g' prefix).  */
286  #define PROGRAM_NAME "install"
287 @@ -115,6 +116,9 @@ static mode_t dir_mode = DEFAULT_MODE;
288     or S_ISGID bits.  */
289  static mode_t dir_mode_bits = CHMOD_MODE_BITS;
290  
291 +/* The security context to give all files. */
292 +static char *context;
293 +
294  /* If true, strip executable files after copying them. */
295  static bool strip_files;
296  
297 @@ -124,6 +128,7 @@ static bool dir_arg;
298  static struct option const long_options[] =
299  {
300    {"backup", optional_argument, NULL, 'b'},
301 +  {"context", required_argument, NULL, 'Z'},
302    {"directory", no_argument, NULL, 'd'},
303    {"group", required_argument, NULL, 'g'},
304    {"mode", required_argument, NULL, 'm'},
305 @@ -155,6 +160,7 @@ cp_option_init (struct cp_options *x)
306    x->preserve_links = false;
307    x->preserve_mode = false;
308    x->preserve_timestamps = false;
309 +  x->preserve_context = false;
310    x->require_preserve = false;
311    x->recursive = false;
312    x->sparse_mode = SPARSE_AUTO;
313 @@ -243,7 +249,7 @@ main (int argc, char **argv)
314       we'll actually use backup_suffix_string.  */
315    backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
316  
317 -  while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pt:TvS:", long_options,
318 +  while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pt:TvS:Z:", long_options,
319                               NULL)) != -1)
320      {
321        switch (optc)
322 @@ -305,6 +311,9 @@ main (int argc, char **argv)
323         case 'T':
324           no_target_directory = true;
325           break;
326 +       case 'Z':
327 +         context = optarg;
328 +         break;
329         case_GETOPT_HELP_CHAR;
330         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
331         default:
332 @@ -520,6 +529,8 @@ change_attributes (char const *name)
333      error (0, errno, _("cannot change ownership of %s"), quote (name));
334    else if (chmod (name, mode) != 0)
335      error (0, errno, _("cannot change permissions of %s"), quote (name));
336 +  else if (context && smack_to_file (name, context) < 0)
337 +   ; // error (0, errno, _("cannot change security context of %s"), quote (name));
338    else
339      return true;
340  
341 @@ -686,6 +697,7 @@ Mandatory arguments to long options are
342    -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY\n\
343    -T, --no-target-directory  treat DEST as a normal file\n\
344    -v, --verbose       print the name of each directory as it is created\n\
345 +  -Z, --context=CONTEXT  set the security context on all destination files\n\
346  "), stdout);
347        fputs (HELP_OPTION_DESCRIPTION, stdout);
348        fputs (VERSION_OPTION_DESCRIPTION, stdout);
349 diff -Nuarp tizen-coreutils/src/ls.c tizen-coreutils-smack/src/ls.c
350 --- tizen-coreutils/src/ls.c    2012-11-30 10:11:38.000000000 +0200
351 +++ tizen-coreutils-smack/src/ls.c      2012-11-30 11:20:39.841516860 +0200
352 @@ -104,6 +104,7 @@
353  #include "wcwidth.h"
354  #include "xstrtol.h"
355  #include "xreadlink.h"
356 +#include "smack.h"
357  
358  #define PROGRAM_NAME (ls_mode == LS_LS ? "ls" \
359                       : (ls_mode == LS_MULTI_COL \
360 @@ -177,6 +178,9 @@ struct fileinfo
361      /* For long listings, true if the file has an access control list.  */
362      bool have_acl;
363  #endif
364 +
365 +    /* Security context */
366 +    char context[SMACK_LABELLEN];
367    };
368  
369  #if USE_ACL
370 @@ -339,6 +343,7 @@ static int nlink_width;
371  static int owner_width;
372  static int group_width;
373  static int author_width;
374 +static int context_width;
375  static int major_device_number_width;
376  static int minor_device_number_width;
377  static int file_size_width;
378 @@ -434,6 +439,10 @@ static bool print_owner = true;
379  
380  static bool print_author;
381  
382 +/* True means to display the security context.  */
383 +
384 +static bool print_context;
385 +
386  /* True means to display group information.  -G and -o turn this off.  */
387  
388  static bool print_group = true;
389 @@ -1514,7 +1523,7 @@ decode_switches (int argc, char **argv)
390    }
391  
392    while ((c = getopt_long (argc, argv,
393 -                          "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UX1",
394 +                          "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UXZ1",
395                            long_options, NULL)) != -1)
396      {
397        switch (c)
398 @@ -1717,6 +1726,10 @@ decode_switches (int argc, char **argv)
399           sort_type_specified = true;
400           break;
401  
402 +       case 'Z':
403 +          print_context = true;
404 +         break;
405 +
406         case '1':
407           /* -1 has no effect after -l.  */
408           if (format != long_format)
409 @@ -2574,6 +2587,7 @@ gobble_file (char const *name, enum file
410        /* Command line dereferences are already taken care of by the above
411          assertion that the inode number is not yet known.  */
412        || (print_inode && inode == NOT_AN_INODE_NUMBER)
413 +      || print_context
414        || (format_needs_type
415           && (type == unknown || command_line_arg
416               /* --indicator-style=classify (aka -F)
417 @@ -2605,6 +2619,7 @@ gobble_file (char const *name, enum file
418        switch (dereference)
419         {
420         case DEREF_ALWAYS:
421 +          err = smack_of_file_follow(absolute_name, f->context, SMACK_LABELLEN);
422           err = stat (absolute_name, &f->stat);
423           break;
424  
425 @@ -2613,6 +2628,8 @@ gobble_file (char const *name, enum file
426           if (command_line_arg)
427             {
428               bool need_lstat;
429 +              err = smack_of_file_follow(absolute_name, f->context,
430 +                                         SMACK_LABELLEN);
431               err = stat (absolute_name, &f->stat);
432  
433               if (dereference == DEREF_COMMAND_LINE_ARGUMENTS)
434 @@ -2631,6 +2648,7 @@ gobble_file (char const *name, enum file
435             }
436  
437         default: /* DEREF_NEVER */
438 +          err = smack_of_file(absolute_name, f->context, SMACK_LABELLEN);
439           err = lstat (absolute_name, &f->stat);
440           break;
441         }
442 @@ -2738,6 +2756,13 @@ gobble_file (char const *name, enum file
443             author_width = len;
444         }
445  
446 +      if (print_context)
447 +       {
448 +         int len = strlen (f->context);
449 +         if (context_width < len)
450 +           context_width = len;
451 +       }
452 +
453        {
454         char buf[INT_BUFSIZE_BOUND (uintmax_t)];
455         int len = strlen (umaxtostr (f->stat.st_nlink, buf));
456 @@ -3463,7 +3488,7 @@ print_long_format (const struct fileinfo
457  
458    DIRED_INDENT ();
459  
460 -  if (print_owner | print_group | print_author)
461 +  if (print_owner | print_group | print_author | print_context)
462      {
463        DIRED_FPUTS (buf, stdout, p - buf);
464  
465 @@ -3476,6 +3501,9 @@ print_long_format (const struct fileinfo
466        if (print_author)
467         format_user (f->stat.st_author, author_width, f->stat_ok);
468  
469 +      if (print_context)
470 +        format_user_or_group(f->context, 0, context_width);
471 +
472        p = buf;
473      }
474  
475 @@ -3812,6 +3840,9 @@ print_file_name_and_frills (const struct
476             human_readable (ST_NBLOCKS (f->stat), buf, human_output_opts,
477                             ST_NBLOCKSIZE, output_block_size));
478  
479 +  if (print_context)
480 +    printf ("%*s ", format == with_commas ? 0 : context_width, f->context);
481 +
482    print_name_with_quoting (f->name, FILE_OR_LINK_MODE (f), f->linkok,
483                            f->stat_ok, f->filetype, NULL);
484  
485 @@ -3975,6 +4006,9 @@ length_of_file_name_and_frills (const st
486                                           output_block_size))
487                 : block_size_width);
488  
489 +  if (print_context)
490 +    len += 1 + (format == with_commas ? strlen (f->context) : context_width);
491 +
492    quote_name (NULL, f->name, filename_quoting_options, &name_width);
493    len += name_width;
494  
495 @@ -4403,6 +4437,7 @@ Mandatory arguments to long options are
496    -w, --width=COLS           assume screen width instead of current value\n\
497    -x                         list entries by lines instead of by columns\n\
498    -X                         sort alphabetically by entry extension\n\
499 +  -Z                         print the security context\n\
500    -1                         list one file per line\n\
501  "), stdout);
502        fputs (HELP_OPTION_DESCRIPTION, stdout);
503 diff -Nuarp tizen-coreutils/src/mkdir.c tizen-coreutils-smack/src/mkdir.c
504 --- tizen-coreutils/src/mkdir.c 2012-11-30 10:11:38.000000000 +0200
505 +++ tizen-coreutils-smack/src/mkdir.c   2012-11-30 11:20:39.841516860 +0200
506 @@ -29,6 +29,7 @@
507  #include "modechange.h"
508  #include "quote.h"
509  #include "savewd.h"
510 +#include "smack.h"
511  
512  /* The official name of this program (e.g., no `g' prefix).  */
513  #define PROGRAM_NAME "mkdir"
514 @@ -40,6 +41,7 @@ char *program_name;
515  
516  static struct option const longopts[] =
517  {
518 +  {"context", required_argument, NULL, 'Z'},
519    {"mode", required_argument, NULL, 'm'},
520    {"parents", no_argument, NULL, 'p'},
521    {"verbose", no_argument, NULL, 'v'},
522 @@ -65,9 +67,10 @@ Create the DIRECTORY(ies), if they do no
523  Mandatory arguments to long options are mandatory for short options too.\n\
524  "), stdout);
525        fputs (_("\
526 -  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask\n\
527 -  -p, --parents     no error if existing, make parent directories as needed\n\
528 -  -v, --verbose     print a message for each created directory\n\
529 +  -Z, --context=CONTEXT  set security context\n\
530 +  -m, --mode=MODE      set file mode (as in chmod), not a=rwx - umask\n\
531 +  -p, --parents        no error if existing, make parent directories as needed\n\
532 +  -v, --verbose        print a message for each created directory\n\
533  "), stdout);
534        fputs (HELP_OPTION_DESCRIPTION, stdout);
535        fputs (VERSION_OPTION_DESCRIPTION, stdout);
536 @@ -92,6 +95,9 @@ struct mkdir_options
537    /* File mode bits affected by MODE.  */
538    mode_t mode_bits;
539  
540 +  /* Security context.  */
541 +  char *context;
542 +
543    /* If not null, format to use when reporting newly made directories.  */
544    char const *created_directory_format;
545  };
546 @@ -101,6 +107,17 @@ static void
547  announce_mkdir (char const *dir, void *options)
548  {
549    struct mkdir_options const *o = options;
550 +
551 +  if (o->context) {
552 +  char *sep = strrchr(dir, '/');
553 +  int res = 0;
554 +  if ((sep != NULL) && (strlen(sep) != strlen(dir)))
555 +    res = smack_to_file (sep + 1, o->context);
556 +  else 
557 +    res = smack_to_file (dir, o->context);
558 +  if (res < 0)
559 +    ; // error (0, errno, _("setting directory context failed"));
560 +  }
561    if (o->created_directory_format)
562      error (0, 0, o->created_directory_format, quote (dir));
563  }
564 @@ -144,6 +155,7 @@ main (int argc, char **argv)
565    options.make_ancestor_function = NULL;
566    options.mode = S_IRWXUGO;
567    options.mode_bits = 0;
568 +  options.context = NULL;
569    options.created_directory_format = NULL;
570  
571    initialize_main (&argc, &argv);
572 @@ -154,10 +166,13 @@ main (int argc, char **argv)
573  
574    atexit (close_stdout);
575  
576 -  while ((optc = getopt_long (argc, argv, "pm:v", longopts, NULL)) != -1)
577 +  while ((optc = getopt_long (argc, argv, "Z:pm:v", longopts, NULL)) != -1)
578      {
579        switch (optc)
580         {
581 +       case 'Z':
582 +         options.context = optarg;
583 +         break;
584         case 'p':
585           options.make_ancestor_function = make_ancestor;
586           break;
587 diff -Nuarp tizen-coreutils/src/mkfifo.c tizen-coreutils-smack/src/mkfifo.c
588 --- tizen-coreutils/src/mkfifo.c        2012-11-30 10:11:38.000000000 +0200
589 +++ tizen-coreutils-smack/src/mkfifo.c  2012-11-30 11:20:39.841516860 +0200
590 @@ -26,6 +26,7 @@
591  #include "error.h"
592  #include "modechange.h"
593  #include "quote.h"
594 +#include "smack.h"
595  
596  /* The official name of this program (e.g., no `g' prefix).  */
597  #define PROGRAM_NAME "mkfifo"
598 @@ -37,6 +38,7 @@ char *program_name;
599  
600  static struct option const longopts[] =
601  {
602 +  {"context", required_argument, NULL, 'Z'},
603    {"mode", required_argument, NULL, 'm'},
604    {GETOPT_HELP_OPTION_DECL},
605    {GETOPT_VERSION_OPTION_DECL},
606 @@ -60,7 +62,10 @@ Create named pipes (FIFOs) with the give
607  Mandatory arguments to long options are mandatory for short options too.\n\
608  "), stdout);
609        fputs (_("\
610 -  -m, --mode=MODE   set file permission bits to MODE, not a=rw - umask\n\
611 +  -Z, --context=CONTEXT   set security context to CONTEXT\n\
612 +"), stdout);
613 +      fputs (_("\
614 +  -m, --mode=MODE         set file permission bits to MODE, not a=rw - umask\n\
615  "), stdout);
616        fputs (HELP_OPTION_DESCRIPTION, stdout);
617        fputs (VERSION_OPTION_DESCRIPTION, stdout);
618 @@ -73,6 +78,7 @@ int
619  main (int argc, char **argv)
620  {
621    mode_t newmode;
622 +  char const *specified_context = NULL;
623    char const *specified_mode = NULL;
624    int exit_status = EXIT_SUCCESS;
625    int optc;
626 @@ -85,10 +91,13 @@ main (int argc, char **argv)
627  
628    atexit (close_stdout);
629  
630 -  while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
631 +  while ((optc = getopt_long (argc, argv, "Z:m:", longopts, NULL)) != -1)
632      {
633        switch (optc)
634         {
635 +       case 'Z':
636 +         specified_context = optarg;
637 +         break;
638         case 'm':
639           specified_mode = optarg;
640           break;
641 @@ -119,11 +128,17 @@ main (int argc, char **argv)
642      }
643  
644    for (; optind < argc; ++optind)
645 -    if (mkfifo (argv[optind], newmode) != 0)
646 -      {
647 -       error (0, errno, _("cannot create fifo %s"), quote (argv[optind]));
648 -       exit_status = EXIT_FAILURE;
649 -      }
650 +    {
651 +      if (mkfifo (argv[optind], newmode) != 0)
652 +        {
653 +         error (0, errno, _("cannot create fifo %s"), quote (argv[optind]));
654 +         exit_status = EXIT_FAILURE;
655 +        }
656 +      if (specified_context &&
657 +          smack_to_file(argv[optind], specified_context) < 0)
658 +       ;// error (0, errno, _("context assignment of %s to %s failed"),
659 +       //        argv[optind], quote (specified_context));
660 +    }
661  
662    exit (exit_status);
663  }
664 diff -Nuarp tizen-coreutils/src/mknod.c tizen-coreutils-smack/src/mknod.c
665 --- tizen-coreutils/src/mknod.c 2012-11-30 10:11:38.000000000 +0200
666 +++ tizen-coreutils-smack/src/mknod.c   2012-11-30 11:20:39.841516860 +0200
667 @@ -27,6 +27,7 @@
668  #include "modechange.h"
669  #include "quote.h"
670  #include "xstrtol.h"
671 +#include "smack.h"
672  
673  /* The official name of this program (e.g., no `g' prefix).  */
674  #define PROGRAM_NAME "mknod"
675 @@ -38,6 +39,7 @@ char *program_name;
676  
677  static struct option const longopts[] =
678  {
679 +  {"context", required_argument, NULL, 'Z'},
680    {"mode", required_argument, NULL, 'm'},
681    {GETOPT_HELP_OPTION_DECL},
682    {GETOPT_VERSION_OPTION_DECL},
683 @@ -62,7 +64,10 @@ Create the special file NAME of the give
684  Mandatory arguments to long options are mandatory for short options too.\n\
685  "), stdout);
686        fputs (_("\
687 -  -m, --mode=MODE   set file permission bits to MODE, not a=rw - umask\n\
688 +  -Z, --context=CONTEXT set file security context to CONTEXT\n\
689 +"), stdout);
690 +      fputs (_("\
691 +  -m, --mode=MODE       set file permission bits to MODE, not a=rw - umask\n\
692  "), stdout);
693        fputs (HELP_OPTION_DESCRIPTION, stdout);
694        fputs (VERSION_OPTION_DESCRIPTION, stdout);
695 @@ -88,6 +93,7 @@ int
696  main (int argc, char **argv)
697  {
698    mode_t newmode;
699 +  char const *specified_context = NULL;
700    char const *specified_mode = NULL;
701    int optc;
702    int expected_operands;
703 @@ -101,10 +107,13 @@ main (int argc, char **argv)
704  
705    atexit (close_stdout);
706  
707 -  while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
708 +  while ((optc = getopt_long (argc, argv, "Z:m:", longopts, NULL)) != -1)
709      {
710        switch (optc)
711         {
712 +       case 'Z':
713 +         specified_context = optarg;
714 +         break;
715         case 'm':
716           specified_mode = optarg;
717           break;
718 @@ -217,5 +226,9 @@ main (int argc, char **argv)
719        usage (EXIT_FAILURE);
720      }
721  
722 +  if (specified_context && smack_to_file (argv[optind], specified_context) < 0)
723 +    ; //error (0, errno, _("context assignment to %s failed"),
724 +    //       quote (specified_context));
725 +
726    exit (EXIT_SUCCESS);
727  }
728 diff -Nuarp tizen-coreutils/src/mv.c tizen-coreutils-smack/src/mv.c
729 --- tizen-coreutils/src/mv.c    2012-11-30 10:11:38.000000000 +0200
730 +++ tizen-coreutils-smack/src/mv.c      2012-11-30 11:20:39.841516860 +0200
731 @@ -32,6 +32,7 @@
732  #include "filenamecat.h"
733  #include "quote.h"
734  #include "remove.h"
735 +#include "smack.h"
736  
737  /* The official name of this program (e.g., no `g' prefix).  */
738  #define PROGRAM_NAME "mv"
739 @@ -126,6 +127,7 @@ cp_option_init (struct cp_options *x)
740    x->preserve_links = true;
741    x->preserve_mode = true;
742    x->preserve_timestamps = true;
743 +  x->preserve_context = true;
744    x->require_preserve = false;  /* FIXME: maybe make this an option */
745    x->recursive = true;
746    x->sparse_mode = SPARSE_AUTO;  /* FIXME: maybe make this an option */
747 diff -Nuarp tizen-coreutils/src/smack.h tizen-coreutils-smack/src/smack.h
748 --- tizen-coreutils/src/smack.h 1970-01-01 02:00:00.000000000 +0200
749 +++ tizen-coreutils-smack/src/smack.h   2012-11-30 11:20:39.841516860 +0200
750 @@ -0,0 +1,134 @@
751 +/* smack.h - Simplified Mandatory Access Control Kernel
752 +
753 +   Copyright (C) 2010 Free Software Foundation, Inc.
754 +
755 +   This program is free software: you can redistribute it and/or modify
756 +   it under the terms of the GNU General Public License as published by
757 +   the Free Software Foundation; either version 3 of the License, or
758 +   (at your option) any later version.
759 +
760 +   This program is distributed in the hope that it will be useful,
761 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
762 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
763 +   GNU General Public License for more details.
764 +
765 +   You should have received a copy of the GNU General Public License
766 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.
767 +
768 +   Written by Casey Schaufler.  */
769 +
770 +#include <sys/types.h>
771 +#include <sys/stat.h>
772 +#include <sys/xattr.h>
773 +#include <unistd.h>
774 +
775 +#define SMACK_PATHTEXTSIZE 80
776 +#define SMACK_LABELLEN 256
777 +#define SMACK_PROC_FMT "/proc/%s/attr/current"
778 +#define SMACK_CHECK_PATH "/smack/load"
779 +#define SMACK_ATTRNAME "security.SMACK64"
780 +
781 +static inline int
782 +smack_enabled(void)
783 +{
784 +  int i;
785 +  struct stat buf;
786 +
787 +  i = stat(SMACK_CHECK_PATH, &buf);
788 +
789 +  if (i < 0)
790 +    return 0;
791 +
792 +  return 1;
793 +}
794 +
795 +static inline int
796 +smack_of_file(const char *path, char *result, int rlen)
797 +{
798 +  int i;
799 +
800 +  i = lgetxattr(path, SMACK_ATTRNAME, result, rlen);
801 +  if (i < 0)
802 +    return i;
803 +
804 +  if (i < rlen)
805 +    result[i] = '\0';
806 +
807 +  return i;
808 +}
809 +
810 +static inline int
811 +smack_of_file_follow(const char *path, char *result, int rlen)
812 +{
813 +  int i;
814 +
815 +  i = getxattr(path, SMACK_ATTRNAME, result, rlen);
816 +  if (i < 0)
817 +    return i;
818 +
819 +  if (i < rlen)
820 +    result[i] = '\0';
821 +
822 +  return i;
823 +}
824 +
825 +static inline int
826 +smack_of_fd(int fd, char *result, int rlen)
827 +{
828 +  int i;
829 +
830 +  i = fgetxattr(fd, SMACK_ATTRNAME, result, rlen);
831 +  if (i < 0)
832 +    return i;
833 +
834 +  if (i < rlen)
835 +    result[i] = '\0';
836 +
837 +  return i;
838 +}
839 +
840 +static inline int
841 +smack_to_fd(int fd, char *smack)
842 +{
843 +  return fsetxattr(fd, SMACK_ATTRNAME, smack, strlen(smack), 0);
844 +}
845 +
846 +static inline int
847 +smack_to_file(const char *path, char *smack)
848 +{
849 +  return lsetxattr(path, SMACK_ATTRNAME, smack, strlen(smack), 0);
850 +}
851 +
852 +static inline int
853 +smack_of_proc(pid_t pid, char *result, int rlen)
854 +{
855 +  int fd;
856 +  int red;
857 +  char *cp = "self";
858 +  char pidtext[SMACK_PATHTEXTSIZE];
859 +  char path[SMACK_PATHTEXTSIZE];
860 +
861 +  if (pid > 0)
862 +    {
863 +      sprintf(pidtext, "%d", pid);
864 +      cp = pidtext;
865 +    }
866 +
867 +  if (strlen(cp) + strlen(SMACK_PROC_FMT) >= SMACK_PATHTEXTSIZE)
868 +    return -1;
869 +
870 +  sprintf(path, SMACK_PROC_FMT, cp);
871 +  fd = open(path, O_RDONLY);
872 +  if (fd < 0)
873 +    return fd;
874 +
875 +  red = read(fd, result, rlen);
876 +  close(fd);
877 +
878 +  if (red >= 0 && red < rlen)
879 +    result[red] = '\0';
880 +  if ((cp = index (result, '\n')) != NULL)
881 +    *cp = '\0';
882 +
883 +  return strlen (result);
884 +}
885 diff -Nuarp tizen-coreutils/src/stat.c tizen-coreutils-smack/src/stat.c
886 --- tizen-coreutils/src/stat.c  2012-11-30 10:11:38.000000000 +0200
887 +++ tizen-coreutils-smack/src/stat.c    2012-11-30 11:20:39.845516860 +0200
888 @@ -68,6 +68,7 @@
889  #include "stat-time.h"
890  #include "strftime.h"
891  #include "xreadlink.h"
892 +#include "smack.h"
893  
894  #define alignof(type) offsetof (struct { char c; type x; }, x)
895  
896 @@ -270,6 +271,8 @@ human_fstype (STRUCT_STATVFS const *stat
897        return "squashfs";
898      case S_MAGIC_SYSFS: /* 0x62656572 */
899        return "sysfs";
900 +    case S_MAGIC_SMACK: /* 0x43415D53 */
901 +      return "smack";
902  # elif __GNU__
903      case FSTYPE_UFS:
904        return "ufs";
905 @@ -595,6 +598,14 @@ print_stat (char *pformat, size_t prefix
906        else
907         out_uint (pformat, prefix_len, statbuf->st_ctime);
908        break;
909 +    case 'C':
910 +      {
911 +        char context[SMACK_LABELLEN];
912 +
913 +        if (smack_of_file(filename, context, SMACK_LABELLEN) > 0)
914 +          out_string(pformat, prefix_len, context);
915 +      }
916 +      break;
917      default:
918        fputc ('?', stdout);
919        break;
920 @@ -855,6 +866,7 @@ The valid format sequences for files (wi
921    %B   The size in bytes of each block reported by %b\n\
922  "), stdout);
923        fputs (_("\
924 +  %C   Security context\n\
925    %d   Device number in decimal\n\
926    %D   Device number in hex\n\
927    %f   Raw mode in hex\n\