mknod.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* mknod -- make special files
  2. Copyright (C) 1990-2018 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by David MacKenzie <djm@ai.mit.edu> */
  14. #include <config.h>
  15. #include <stdio.h>
  16. #include <getopt.h>
  17. #include <sys/types.h>
  18. #include <selinux/selinux.h>
  19. #include "system.h"
  20. #include "die.h"
  21. #include "error.h"
  22. #include "modechange.h"
  23. #include "quote.h"
  24. #include "selinux.h"
  25. #include "smack.h"
  26. #include "xstrtol.h"
  27. /* The official name of this program (e.g., no 'g' prefix). */
  28. #define PROGRAM_NAME "mknod"
  29. #define AUTHORS proper_name ("David MacKenzie")
  30. static struct option const longopts[] =
  31. {
  32. {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
  33. {"mode", required_argument, NULL, 'm'},
  34. {GETOPT_HELP_OPTION_DECL},
  35. {GETOPT_VERSION_OPTION_DECL},
  36. {NULL, 0, NULL, 0}
  37. };
  38. void
  39. usage (int status)
  40. {
  41. if (status != EXIT_SUCCESS)
  42. emit_try_help ();
  43. else
  44. {
  45. printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"),
  46. program_name);
  47. fputs (_("\
  48. Create the special file NAME of the given TYPE.\n\
  49. "), stdout);
  50. emit_mandatory_arg_note ();
  51. fputs (_("\
  52. -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
  53. "), stdout);
  54. fputs (_("\
  55. -Z set the SELinux security context to default type\n\
  56. --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n\
  57. or SMACK security context to CTX\n\
  58. "), stdout);
  59. fputs (HELP_OPTION_DESCRIPTION, stdout);
  60. fputs (VERSION_OPTION_DESCRIPTION, stdout);
  61. fputs (_("\
  62. \n\
  63. Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they\n\
  64. must be omitted when TYPE is p. If MAJOR or MINOR begins with 0x or 0X,\n\
  65. it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal;\n\
  66. otherwise, as decimal. TYPE may be:\n\
  67. "), stdout);
  68. fputs (_("\
  69. \n\
  70. b create a block (buffered) special file\n\
  71. c, u create a character (unbuffered) special file\n\
  72. p create a FIFO\n\
  73. "), stdout);
  74. printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
  75. emit_ancillary_info (PROGRAM_NAME);
  76. }
  77. exit (status);
  78. }
  79. int
  80. main (int argc, char **argv)
  81. {
  82. mode_t newmode;
  83. char const *specified_mode = NULL;
  84. int optc;
  85. size_t expected_operands;
  86. mode_t node_type;
  87. char const *scontext = NULL;
  88. bool set_security_context = false;
  89. initialize_main (&argc, &argv);
  90. set_program_name (argv[0]);
  91. setlocale (LC_ALL, "");
  92. bindtextdomain (PACKAGE, LOCALEDIR);
  93. textdomain (PACKAGE);
  94. atexit (close_stdout);
  95. while ((optc = getopt_long (argc, argv, "m:Z", longopts, NULL)) != -1)
  96. {
  97. switch (optc)
  98. {
  99. case 'm':
  100. specified_mode = optarg;
  101. break;
  102. case 'Z':
  103. if (is_smack_enabled ())
  104. {
  105. /* We don't yet support -Z to restore context with SMACK. */
  106. scontext = optarg;
  107. }
  108. else if (is_selinux_enabled () > 0)
  109. {
  110. if (optarg)
  111. scontext = optarg;
  112. else
  113. set_security_context = true;
  114. }
  115. else if (optarg)
  116. {
  117. error (0, 0,
  118. _("warning: ignoring --context; "
  119. "it requires an SELinux/SMACK-enabled kernel"));
  120. }
  121. break;
  122. case_GETOPT_HELP_CHAR;
  123. case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
  124. default:
  125. usage (EXIT_FAILURE);
  126. }
  127. }
  128. newmode = MODE_RW_UGO;
  129. if (specified_mode)
  130. {
  131. mode_t umask_value;
  132. struct mode_change *change = mode_compile (specified_mode);
  133. if (!change)
  134. die (EXIT_FAILURE, 0, _("invalid mode"));
  135. umask_value = umask (0);
  136. umask (umask_value);
  137. newmode = mode_adjust (newmode, false, umask_value, change, NULL);
  138. free (change);
  139. if (newmode & ~S_IRWXUGO)
  140. die (EXIT_FAILURE, 0,
  141. _("mode must specify only file permission bits"));
  142. }
  143. /* If the number of arguments is 0 or 1,
  144. or (if it's 2 or more and the second one starts with 'p'), then there
  145. must be exactly two operands. Otherwise, there must be four. */
  146. expected_operands = (argc <= optind
  147. || (optind + 1 < argc && argv[optind + 1][0] == 'p')
  148. ? 2 : 4);
  149. if (argc - optind < expected_operands)
  150. {
  151. if (argc <= optind)
  152. error (0, 0, _("missing operand"));
  153. else
  154. error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
  155. if (expected_operands == 4 && argc - optind == 2)
  156. fprintf (stderr, "%s\n",
  157. _("Special files require major and minor device numbers."));
  158. usage (EXIT_FAILURE);
  159. }
  160. if (expected_operands < argc - optind)
  161. {
  162. error (0, 0, _("extra operand %s"),
  163. quote (argv[optind + expected_operands]));
  164. if (expected_operands == 2 && argc - optind == 4)
  165. fprintf (stderr, "%s\n",
  166. _("Fifos do not have major and minor device numbers."));
  167. usage (EXIT_FAILURE);
  168. }
  169. if (scontext)
  170. {
  171. int ret = 0;
  172. if (is_smack_enabled ())
  173. ret = smack_set_label_for_self (scontext);
  174. else
  175. ret = setfscreatecon (se_const (scontext));
  176. if (ret < 0)
  177. die (EXIT_FAILURE, errno,
  178. _("failed to set default file creation context to %s"),
  179. quote (scontext));
  180. }
  181. /* Only check the first character, to allow mnemonic usage like
  182. 'mknod /dev/rst0 character 18 0'. */
  183. switch (argv[optind + 1][0])
  184. {
  185. case 'b': /* 'block' or 'buffered' */
  186. #ifndef S_IFBLK
  187. die (EXIT_FAILURE, 0, _("block special files not supported"));
  188. #else
  189. node_type = S_IFBLK;
  190. #endif
  191. goto block_or_character;
  192. case 'c': /* 'character' */
  193. case 'u': /* 'unbuffered' */
  194. #ifndef S_IFCHR
  195. die (EXIT_FAILURE, 0, _("character special files not supported"));
  196. #else
  197. node_type = S_IFCHR;
  198. #endif
  199. goto block_or_character;
  200. block_or_character:
  201. {
  202. char const *s_major = argv[optind + 2];
  203. char const *s_minor = argv[optind + 3];
  204. uintmax_t i_major, i_minor;
  205. dev_t device;
  206. if (xstrtoumax (s_major, NULL, 0, &i_major, NULL) != LONGINT_OK
  207. || i_major != (major_t) i_major)
  208. die (EXIT_FAILURE, 0,
  209. _("invalid major device number %s"), quote (s_major));
  210. if (xstrtoumax (s_minor, NULL, 0, &i_minor, NULL) != LONGINT_OK
  211. || i_minor != (minor_t) i_minor)
  212. die (EXIT_FAILURE, 0,
  213. _("invalid minor device number %s"), quote (s_minor));
  214. device = makedev (i_major, i_minor);
  215. #ifdef NODEV
  216. if (device == NODEV)
  217. die (EXIT_FAILURE, 0, _("invalid device %s %s"),
  218. s_major, s_minor);
  219. #endif
  220. if (set_security_context)
  221. defaultcon (argv[optind], node_type);
  222. if (mknod (argv[optind], newmode | node_type, device) != 0)
  223. die (EXIT_FAILURE, errno, "%s", quotef (argv[optind]));
  224. }
  225. break;
  226. case 'p': /* 'pipe' */
  227. if (set_security_context)
  228. defaultcon (argv[optind], S_IFIFO);
  229. if (mkfifo (argv[optind], newmode) != 0)
  230. die (EXIT_FAILURE, errno, "%s", quotef (argv[optind]));
  231. break;
  232. default:
  233. error (0, 0, _("invalid device type %s"), quote (argv[optind + 1]));
  234. usage (EXIT_FAILURE);
  235. }
  236. if (specified_mode && lchmod (argv[optind], newmode) != 0)
  237. die (EXIT_FAILURE, errno, _("cannot set permissions of %s"),
  238. quoteaf (argv[optind]));
  239. return EXIT_SUCCESS;
  240. }