tst-pam_group1.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Redistribution and use in source and binary forms, with or without
  3. * modification, are permitted provided that the following conditions
  4. * are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, and the entire permission notice in its entirety,
  7. * including the disclaimer of warranties.
  8. * 2. Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * 3. The name of the author may not be used to endorse or promote
  12. * products derived from this software without specific prior
  13. * written permission.
  14. *
  15. * ALTERNATIVELY, this product may be distributed under the terms of
  16. * the GNU Public License, in which case the provisions of the GPL are
  17. * required INSTEAD OF the above restrictions. (This clause is
  18. * necessary due to a potential bad interaction between the GPL and
  19. * the restrictions contained in a BSD-style copyright.)
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  29. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  31. * OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /*
  34. test case:
  35. Check the following line in group.conf:
  36. tst-pam_group1;*;tstpamgrp;Al0000-2400;tstpamgrpg
  37. pam_group should add group tstpamgrpg to user tstpamgrp, but not
  38. to tstpamgrp2.
  39. */
  40. #ifdef HAVE_CONFIG_H
  41. #include <config.h>
  42. #endif
  43. #include <grp.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #include <sys/types.h>
  49. #include <security/pam_appl.h>
  50. #define GROUP_BLK 10
  51. #define blk_size(len) (((len-1 + GROUP_BLK)/GROUP_BLK)*GROUP_BLK)
  52. /* A conversation function which uses an internally-stored value for
  53. the responses. */
  54. static int
  55. fake_conv (int num_msg, const struct pam_message **msgm UNUSED,
  56. struct pam_response **response, void *appdata_ptr UNUSED)
  57. {
  58. struct pam_response *reply;
  59. int count;
  60. /* Sanity test. */
  61. if (num_msg <= 0)
  62. return PAM_CONV_ERR;
  63. /* Allocate memory for the responses. */
  64. reply = calloc (num_msg, sizeof (struct pam_response));
  65. if (reply == NULL)
  66. return PAM_CONV_ERR;
  67. /* Each prompt elicits the same response. */
  68. for (count = 0; count < num_msg; ++count)
  69. {
  70. reply[count].resp_retcode = 0;
  71. reply[count].resp = strdup ("!!");
  72. }
  73. /* Set the pointers in the response structure and return. */
  74. *response = reply;
  75. return PAM_SUCCESS;
  76. }
  77. static struct pam_conv conv = {
  78. fake_conv,
  79. NULL
  80. };
  81. static int debug = 0;
  82. static int
  83. run_test (const char *user, gid_t groupid, int needit)
  84. {
  85. pam_handle_t *pamh = NULL;
  86. int retval;
  87. int no_grps;
  88. retval = pam_start("tst-pam_group1", user, &conv, &pamh);
  89. if (retval != PAM_SUCCESS)
  90. {
  91. if (debug)
  92. fprintf (stderr, "pam_group1: pam_start returned %d\n", retval);
  93. return 1;
  94. }
  95. retval = pam_set_item (pamh, PAM_TTY, "/dev/tty1");
  96. if (retval != PAM_SUCCESS)
  97. {
  98. if (debug)
  99. fprintf (stderr,
  100. "pam_group1: pam_set_item(PAM_TTY) returned %d\n",
  101. retval);
  102. return 1;
  103. }
  104. retval = pam_authenticate (pamh, 0);
  105. if (retval != PAM_SUCCESS)
  106. {
  107. if (debug)
  108. fprintf (stderr, "pam_group1: pam_authenticate returned %d\n", retval);
  109. return 1;
  110. }
  111. retval = pam_setcred (pamh, PAM_ESTABLISH_CRED);
  112. if (retval != PAM_SUCCESS)
  113. {
  114. if (debug)
  115. fprintf (stderr, "pam_group1: pam_setcred returned %d\n", retval);
  116. return 1;
  117. }
  118. no_grps = getgroups(0, NULL); /* find the current number of groups */
  119. if (no_grps > 0)
  120. {
  121. int i, found;
  122. gid_t *grps = calloc (blk_size (no_grps), sizeof(gid_t));
  123. if (getgroups(no_grps, grps) < 0)
  124. {
  125. if (debug)
  126. fprintf (stderr, "pam_group1: getroups returned error: %m\n");
  127. pam_end (pamh, PAM_SYSTEM_ERR);
  128. return 1;
  129. }
  130. found = 0;
  131. for (i = 0; i < no_grps; ++i)
  132. {
  133. #if 0
  134. if (debug)
  135. fprintf (stderr, "gid[%d]=%d\n", i, grps[i]);
  136. #endif
  137. if (grps[i] == groupid)
  138. found = 1;
  139. }
  140. if ((needit && found) || (!needit && !found))
  141. {
  142. /* everything is ok */
  143. }
  144. else
  145. {
  146. pam_end (pamh, PAM_SYSTEM_ERR);
  147. if (debug)
  148. fprintf (stderr,
  149. "pam_group1: unexpected result for %s: needit=%d, found=%d\n",
  150. user, needit, found);
  151. return 1;
  152. }
  153. }
  154. retval = pam_end (pamh,retval);
  155. if (retval != PAM_SUCCESS)
  156. {
  157. if (debug)
  158. fprintf (stderr, "pam_group1: pam_end returned %d\n", retval);
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. int
  164. main(int argc, char *argv[])
  165. {
  166. struct group *grp;
  167. gid_t grpid;
  168. if (argc > 1 && strcmp (argv[1], "-d") == 0)
  169. debug = 1;
  170. grp = getgrnam ("tstpamgrpg");
  171. if (grp == NULL)
  172. return 1;
  173. grpid = grp->gr_gid;
  174. if (run_test ("root", grpid, 0) != 0 ||
  175. run_test ("tstpamgrp2", grpid, 0) != 0 ||
  176. run_test ("tstpamgrp", grpid, 1) != 0)
  177. return 1;
  178. return 0;
  179. }