(root)/
Linux-PAM-1.5.3/
tests/
tst-pam_getenvlist.c
       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  #ifdef HAVE_CONFIG_H
      35  #include <config.h>
      36  #endif
      37  
      38  #include <stdio.h>
      39  #include <unistd.h>
      40  #include <stdlib.h>
      41  #include <string.h>
      42  #include <security/pam_appl.h>
      43  
      44  const char *envvals[] = {"VAL1=1", "VAL2=2", "VAL3=3"};
      45  
      46  int
      47  main (void)
      48  {
      49    const char *service = "dummy";
      50    const char *user = "root";
      51    struct pam_conv conv = { NULL, NULL };
      52    pam_handle_t *pamh;
      53    int retval;
      54    char **ptr;
      55    char *temp;
      56    int var, i;
      57  
      58    /* 1: Call with NULL as pam handle */
      59    ptr = pam_getenvlist (NULL);
      60    if (ptr != NULL)
      61      {
      62        fprintf (stderr, "pam_getenvlist (NULL) does not return NULL\n");
      63        return 1;
      64      }
      65  
      66    /* setup pam handle */
      67    retval = pam_start (service, user, &conv, &pamh);
      68    if (retval != PAM_SUCCESS)
      69      {
      70        fprintf (stderr, "pam_start (%s, %s, &conv, &pamh) returned %d\n",
      71                 service, user, retval);
      72        return 1;
      73      }
      74  
      75    /* 2: Call with pam handle, but no environment set */
      76    ptr = pam_getenvlist (pamh);
      77    if (ptr == NULL || *ptr != NULL)
      78      {
      79        fprintf (stderr,
      80  	       "pam_getenvlist (pamh) does not return pointer to NULL\n");
      81        temp = *ptr;
      82        var = 0;
      83        while (temp)
      84  	{
      85  	  printf ("%s\n", temp);
      86  	  var++;
      87  	  temp = *(ptr + var);
      88  	}
      89        return 1;
      90      }
      91    free (ptr);
      92  
      93    /* set environment variable */
      94    for (i = 0; i < 3; i++)
      95      {
      96        retval = pam_putenv (pamh, envvals[i]);
      97        if (retval != PAM_SUCCESS)
      98  	{
      99  	  fprintf (stderr, "pam_putenv (pamh, \"%s\") returned %d\n",
     100  		   envvals[i], retval);
     101  	  return 1;
     102  	}
     103      }
     104  
     105    /* 3: Call with pam handle and environment set */
     106    ptr = pam_getenvlist (pamh);
     107    if (ptr == NULL)
     108      {
     109        fprintf (stderr, "pam_getenvlist (pamh) returned NULL\n");
     110        return 1;
     111      }
     112    else
     113      {
     114        temp = *ptr;
     115        var = 0;
     116        while (temp)
     117  	{
     118  	  if (strcmp (temp, envvals[var]) != 0)
     119  	    {
     120  	      fprintf (stderr,
     121  		       "pam_getenvlist returns wrong value:\n"
     122  		       "expected: %s\n"
     123  		       "got: %s\n", envvals[var], temp);
     124  	      return 1;
     125  	    }
     126  	  free (temp);
     127  	  var++;
     128  	  temp = *(ptr + var);
     129  	}
     130        free (ptr);
     131      }
     132  
     133    return 0;
     134  }