genheaders.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <ctype.h>
  7. struct security_class_mapping {
  8. const char *name;
  9. const char *perms[sizeof(unsigned) * 8 + 1];
  10. };
  11. #include "classmap.h"
  12. #include "initial_sid_to_string.h"
  13. #define max(x, y) (((int)(x) > (int)(y)) ? x : y)
  14. const char *progname;
  15. static void usage(void)
  16. {
  17. printf("usage: %s flask.h av_permissions.h\n", progname);
  18. exit(1);
  19. }
  20. static char *stoupperx(const char *s)
  21. {
  22. char *s2 = strdup(s);
  23. char *p;
  24. if (!s2) {
  25. fprintf(stderr, "%s: out of memory\n", progname);
  26. exit(3);
  27. }
  28. for (p = s2; *p; p++)
  29. *p = toupper(*p);
  30. return s2;
  31. }
  32. int main(int argc, char *argv[])
  33. {
  34. int i, j, k;
  35. int isids_len;
  36. FILE *fout;
  37. progname = argv[0];
  38. if (argc < 3)
  39. usage();
  40. fout = fopen(argv[1], "w");
  41. if (!fout) {
  42. fprintf(stderr, "Could not open %s for writing: %s\n",
  43. argv[1], strerror(errno));
  44. exit(2);
  45. }
  46. for (i = 0; secclass_map[i].name; i++) {
  47. struct security_class_mapping *map = &secclass_map[i];
  48. map->name = stoupperx(map->name);
  49. for (j = 0; map->perms[j]; j++)
  50. map->perms[j] = stoupperx(map->perms[j]);
  51. }
  52. isids_len = sizeof(initial_sid_to_string) / sizeof (char *);
  53. for (i = 1; i < isids_len; i++)
  54. initial_sid_to_string[i] = stoupperx(initial_sid_to_string[i]);
  55. fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
  56. fprintf(fout, "#ifndef _SELINUX_FLASK_H_\n#define _SELINUX_FLASK_H_\n\n");
  57. for (i = 0; secclass_map[i].name; i++) {
  58. struct security_class_mapping *map = &secclass_map[i];
  59. fprintf(fout, "#define SECCLASS_%s", map->name);
  60. for (j = 0; j < max(1, 40 - strlen(map->name)); j++)
  61. fprintf(fout, " ");
  62. fprintf(fout, "%2d\n", i+1);
  63. }
  64. fprintf(fout, "\n");
  65. for (i = 1; i < isids_len; i++) {
  66. char *s = initial_sid_to_string[i];
  67. fprintf(fout, "#define SECINITSID_%s", s);
  68. for (j = 0; j < max(1, 40 - strlen(s)); j++)
  69. fprintf(fout, " ");
  70. fprintf(fout, "%2d\n", i);
  71. }
  72. fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
  73. fprintf(fout, "\n#endif\n");
  74. fclose(fout);
  75. fout = fopen(argv[2], "w");
  76. if (!fout) {
  77. fprintf(stderr, "Could not open %s for writing: %s\n",
  78. argv[2], strerror(errno));
  79. exit(4);
  80. }
  81. fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
  82. fprintf(fout, "#ifndef _SELINUX_AV_PERMISSIONS_H_\n#define _SELINUX_AV_PERMISSIONS_H_\n\n");
  83. for (i = 0; secclass_map[i].name; i++) {
  84. struct security_class_mapping *map = &secclass_map[i];
  85. for (j = 0; map->perms[j]; j++) {
  86. fprintf(fout, "#define %s__%s", map->name,
  87. map->perms[j]);
  88. for (k = 0; k < max(1, 40 - strlen(map->name) - strlen(map->perms[j])); k++)
  89. fprintf(fout, " ");
  90. fprintf(fout, "0x%08xUL\n", (1<<j));
  91. }
  92. }
  93. fprintf(fout, "\n#endif\n");
  94. fclose(fout);
  95. exit(0);
  96. }