/*
 *
 * htocf.c
 *
 * obtain entries for NFS credentials file using hesiod
 *
 * mhpower@athena.mit.edu, 28 May 1993
 *
 */

#include <stdio.h>
#include <hesiod.h>

main()
 {
   char **hr;
   char a[32], thisg[32], uid_s[32];
   int g[512], i, j, k, n;

   while (fgets(a, 10, stdin) != NULL)
     {
       a[strlen(a) - 1] = '\0';    /* delete newline */
#ifdef DEBUG
       fprintf(stderr, "doing %s\n", a);
#endif
       hr = hes_resolve(a, "passwd");
       if (!hr)
         {
           hes_error();
           fprintf(stderr, "passwd entry for %s is NULL\n", a);
           exit(1);
         }
       for (i = 0; hr[0][i] != '*'; ++i)
         {
           ;
         }
       k = 0;
       for (j = i + 2; hr[0][j] != ':'; ++j)
         {
           if (hr[0][j] == '\0')
             {
               fprintf(stderr, "bad passwd entry %s\n", hr[0]);
               exit(1);
             }
           uid_s[k++] = hr[0][j];
         }
       uid_s[k] = '\0';
       hr = hes_resolve(a, "grplist");
       if (!hr)
         {
           hes_error();
           fprintf(stderr, "grplist entry for %s is NULL\n", a);
           exit(1);
         }
       n = 0;  /* not reading numerical portion of grplist */
       j = 0;  /* no groups found yet */
       for (i = 0; hr[0][i] != '\0'; ++i)
         {
           if (n)
             {
               thisg[k++] = hr[0][i];
             }
           if (hr[0][i] == ':')
             {
               if (n)
                 {
                   thisg[k] = '\0';
                   g[j++] = atoi(thisg);
                   n = 0;
                 }
               else
                 {
                   n = 1;
                 }
               k = 0;
             }
         }
       /* last group, or maybe just a trailing : */
       if (k)
         {
           thisg[k] = '\0';
           g[j++] = atoi(thisg);
         }
#ifdef DEBUG       
       fprintf(stderr, "%s has UID %s\n", a, uid_s);
       for (i = 0; i < j; ++i)
         {
           fprintf(stderr, "%s is in GROUP %ld\n", a, g[i]);
         }
       fflush(stderr);
#endif
       fputs(a, stdout);
       fputc(':', stdout);
       fputs(uid_s, stdout);
       for (i = 0; i < j; ++i)
         {
           printf(":%ld", g[i]);
         }
       fputc('\n', stdout);
       fflush(stdout);
     }
 }
