pam_conv_y.y 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. %{
  2. /*
  3. * $Id$
  4. *
  5. * Copyright (c) Andrew G. Morgan 1997 <morgan@parc.power.net>
  6. *
  7. * This file is covered by the Linux-PAM License (which should be
  8. * distributed with this file.)
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdarg.h>
  16. #include <stdlib.h>
  17. #include <ctype.h>
  18. #include <sys/stat.h>
  19. #include <security/_pam_types.h>
  20. extern int yylex(void);
  21. int current_line=1;
  22. extern char *yytext;
  23. /* XXX - later we'll change this to be the specific conf file(s) */
  24. #define newpamf stderr
  25. #define PAM_D "./pam.d"
  26. #define PAM_D_MODE 0755
  27. #define PAM_D_MAGIC_HEADER \
  28. "#%%PAM-1.0\n" \
  29. "#[For version 1.0 syntax, the above header is optional]\n"
  30. #define PAM_D_FILE_FMT PAM_D "/%s"
  31. const char *old_to_new_ctrl_flag(const char *old);
  32. void yyerror(const char *format, ...);
  33. %}
  34. %union {
  35. int def;
  36. char *string;
  37. }
  38. %token NL EOFILE TOK
  39. %type <string> tok path tokenls
  40. %start complete
  41. %%
  42. complete
  43. :
  44. | complete NL
  45. | complete line
  46. | complete EOFILE {
  47. return 0;
  48. }
  49. ;
  50. line
  51. : tok tok tok path tokenls NL {
  52. char *filename;
  53. FILE *conf;
  54. int i;
  55. /* make sure we have lower case */
  56. for (i=0; $1[i]; ++i) {
  57. $1[i] = tolower($1[i]);
  58. }
  59. /* $1 = service-name */
  60. yyerror("Appending to " PAM_D "/%s", $1);
  61. filename = malloc(strlen($1) + sizeof(PAM_D) + 6);
  62. sprintf(filename, PAM_D_FILE_FMT, $1);
  63. conf = fopen(filename, "r");
  64. if (conf == NULL) {
  65. /* new file */
  66. conf = fopen(filename, "w");
  67. if (conf != NULL) {
  68. fprintf(conf, PAM_D_MAGIC_HEADER);
  69. fprintf(conf,
  70. "#\n"
  71. "# The PAM configuration file for the `%s' service\n"
  72. "#\n", $1);
  73. }
  74. } else {
  75. fclose(conf);
  76. conf = fopen(filename, "a");
  77. }
  78. if (conf == NULL) {
  79. yyerror("trouble opening %s - aborting", filename);
  80. exit(1);
  81. }
  82. free(filename);
  83. /* $2 = module-type */
  84. fprintf(conf, "%-10s", $2);
  85. free($2);
  86. /* $3 = required etc. */
  87. {
  88. const char *trans;
  89. trans = old_to_new_ctrl_flag($3);
  90. free($3);
  91. fprintf(conf, " %-10s", trans);
  92. }
  93. /* $4 = module-path */
  94. fprintf(conf, " %s", $4);
  95. free($4);
  96. /* $5 = arguments */
  97. if ($5 != NULL) {
  98. fprintf(conf, " \\\n\t\t%s", $5);
  99. free($5);
  100. }
  101. /* end line */
  102. fprintf(conf, "\n");
  103. fclose(conf);
  104. }
  105. | error NL {
  106. yyerror("malformed line");
  107. }
  108. ;
  109. tokenls
  110. : {
  111. $$=NULL;
  112. }
  113. | tokenls tok {
  114. int len;
  115. if ($1) {
  116. len = strlen($1) + strlen($2) + 2;
  117. $$ = malloc(len);
  118. sprintf($$,"%s %s",$1,$2);
  119. free($1);
  120. free($2);
  121. } else {
  122. $$ = $2;
  123. }
  124. }
  125. ;
  126. path
  127. : TOK {
  128. /* XXX - this could be used to check if file present */
  129. $$ = strdup(yytext);
  130. }
  131. tok
  132. : TOK {
  133. $$ = strdup(yytext);
  134. }
  135. %%
  136. const char *old_to_new_ctrl_flag(const char *old)
  137. {
  138. static const char *clist[] = {
  139. "requisite",
  140. "required",
  141. "sufficient",
  142. "optional",
  143. NULL,
  144. };
  145. int i;
  146. for (i=0; clist[i]; ++i) {
  147. if (strcasecmp(clist[i], old) == 0) {
  148. break;
  149. }
  150. }
  151. return clist[i];
  152. }
  153. PAM_FORMAT((printf, 1, 2))
  154. void yyerror(const char *format, ...)
  155. {
  156. va_list args;
  157. fprintf(stderr, "line %d: ", current_line);
  158. va_start(args, format);
  159. vfprintf(stderr, format, args);
  160. va_end(args);
  161. fprintf(stderr, "\n");
  162. }
  163. int main(void)
  164. {
  165. if (mkdir(PAM_D, PAM_D_MODE) != 0) {
  166. yyerror(PAM_D " already exists.. aborting");
  167. exit(1);
  168. }
  169. yyparse();
  170. exit(0);
  171. }