help_env.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * $Id$
  3. *
  4. * This file was written by Andrew G. Morgan <morgan@parc.power.net>
  5. *
  6. */
  7. #include "config.h"
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <security/pam_misc.h>
  12. /*
  13. * This function should be used to carefully dispose of the copied
  14. * environment.
  15. *
  16. * usage: env = pam_misc_drop_env(env);
  17. */
  18. char **pam_misc_drop_env(char **dump)
  19. {
  20. int i;
  21. for (i=0; dump[i] != NULL; ++i) {
  22. D(("dump[%d]=`%s'", i, dump[i]));
  23. _pam_overwrite(dump[i]);
  24. _pam_drop(dump[i]);
  25. }
  26. _pam_drop(dump);
  27. return NULL;
  28. }
  29. /*
  30. * This function takes the supplied environment and uploads it to be
  31. * the PAM one.
  32. */
  33. int pam_misc_paste_env(pam_handle_t *pamh, const char * const * user_env)
  34. {
  35. for (; user_env && *user_env; ++user_env) {
  36. int retval;
  37. D(("uploading: %s", *user_env));
  38. retval = pam_putenv(pamh, *user_env);
  39. if (retval != PAM_SUCCESS) {
  40. D(("error setting %s: %s", *user_env, pam_strerror(pamh,retval)));
  41. return retval;
  42. }
  43. }
  44. D(("done."));
  45. return PAM_SUCCESS;
  46. }
  47. /*
  48. * This is a wrapper to make pam behave in the way that setenv() does.
  49. */
  50. int pam_misc_setenv(pam_handle_t *pamh, const char *name
  51. , const char *value, int readonly)
  52. {
  53. char *tmp;
  54. int retval;
  55. if (readonly) {
  56. const char *etmp;
  57. /* we check if the variable is there already */
  58. etmp = pam_getenv(pamh, name);
  59. if (etmp != NULL) {
  60. D(("failed to set readonly variable: %s", name));
  61. return PAM_PERM_DENIED; /* not allowed to overwrite */
  62. }
  63. }
  64. if (asprintf(&tmp, "%s=%s", name, value) >= 0) {
  65. D(("pam_putt()ing: %s", tmp));
  66. retval = pam_putenv(pamh, tmp);
  67. _pam_overwrite(tmp); /* purge */
  68. _pam_drop(tmp); /* forget */
  69. } else {
  70. D(("malloc failure"));
  71. retval = PAM_BUF_ERR;
  72. }
  73. return retval;
  74. }