[OpenAFS] OpenAFS API / Documentation?

Jeffrey B. Woodward jeff.woodward@catchdata.com
Fri, 08 Dec 2006 13:43:11 -0500


This is a multi-part message in MIME format.
--------------040109050401010805010901
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Kirk,

This reminds me of a project that a colleague, Jed Dobson, and I worked
on at Dartmouth College approximately 2.5 yrs ago. Unfortunately, I
don't remember all the details, but essentially all we did was enable
the AFS feature in SGE and wrote some simple code to write the token to
stdout and then another small program to read it in. Attached are said
programs.

Your mileage may vary as this is oldish code, but I hope this helps (and
special thanks to Jed for digging up our old code from the archives).

-Woody


Kirk Patton wrote:
> Hello,
> 
> I am working on a small C program that needs to klog on behalf of a user.
> We are running Sun Grid Engine and AFS.  In order for a SGE job to access
> AFS space, SGE needs to be able to switch to the users ID and authenticate 
> to AFS.  
> 
> I was wondering if there are any tutorials/howto's or references that 
> could be of help.
> 
> Thanks,
> Kirk
> 
> 


--------------040109050401010805010901
Content-Type: text/plain;
 name="GetToken.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="GetToken.c"

/*
 * GetToken.c: read a token from the Cache Manager and write to a file.
 *
 * $Id: GetToken.c,v 1.1 2003/04/15 13:41:15 jed Exp jed $
 *
 */

#include <unistd.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <strings.h> /* bzero lives in either string.h or strings.h...sigh */
#include <string.h>
#include <netinet/in.h>
#include <errno.h>

#include <afs/param.h>

#include <afs/stds.h>
#include <afs/afsutil.h>
#include <afs/auth.h>


extern int ktc_GetToken(struct ktc_principal *aserver,
			struct ktc_token *atoken,
			int atokenLen,
			struct ktc_principal *aclient);

extern int ktc_ListTokens(int aprevIndex,
			  int* aindex, struct ktc_principal *aserver);


int main(int argc, char* argv[])
{
  int cellIndex, newIndex;
  for (cellIndex = 0 ; ; cellIndex = newIndex) {
    struct ktc_principal server;
    struct ktc_principal client;
    struct ktc_token token;
    int rc;

    bzero(&server, sizeof(struct ktc_principal));
    bzero(&client, sizeof(struct ktc_principal));
    bzero(&token, sizeof(struct ktc_token));

    /* fetch server principal */
    rc = ktc_ListTokens(cellIndex, &newIndex, &server);

    /*
      fprintf(stderr, "cellIndex = %d, newIndex = %d, rc = %d  (%d)\n",
                      cellIndex,
		      newIndex,
		      rc,
		      KTC_NOENT);
    */

    if (rc) {
      if (rc == KTC_NOENT) {
	/* no more tokens */
	break;
      } else {
	/* some error occured */
	perror("ktc_ListTokens failed fetching original tokens");
	exit(1);
      }
    }
    
    rc = ktc_GetToken(&server,
		      &token, sizeof(struct ktc_token),
		      &client);

    if (rc) {
      printf("error: couldn't get token!\n");
      exit(255);
    }


    /*
     * Fix up endian issues. Only the ktc_token structure has
     * problems...we will make all field network byte-order...
     */

    token.startTime = htonl(token.startTime);
    token.endTime = htonl(token.endTime);
    token.kvno = htons(token.kvno);
    token.ticketLen = htonl(token.ticketLen);


    /*
     * Puke out the token
     */

    write(STDOUT_FILENO, &server, sizeof(struct ktc_principal));
    write(STDOUT_FILENO, &client, sizeof(struct ktc_principal));
    write(STDOUT_FILENO, &token, sizeof(struct ktc_token));
  }

  exit(0);
}

--------------040109050401010805010901
Content-Type: text/plain;
 name="SetToken.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="SetToken.c"


/*
 * SetToken.c: read a token from a file and load into Cache Manager. 
 *
 * $Id: SetToken.c,v 1.1 2003/04/15 13:41:38 jed Exp jed $
 *
 */

#include <unistd.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>   /* bzero lives in either string.h or strings.h...sigh */
#include <strings.h>
#include <netinet/in.h>
#include <errno.h>

#include <afs/param.h>

#include <afs/stds.h>
#include <afs/afsutil.h>
#include <afs/auth.h>


extern int ktc_SetToken(struct ktc_principal *aserver,
			struct ktc_token *atoken,
			struct ktc_principal *aclient,
			int flags);



int readn(int fd, void *vptr, size_t n)
{
  size_t  nleft;
  ssize_t nread;
  char    *ptr;
  
  ptr = vptr;
  nleft = n;
  while (nleft > 0) {
    if ( (nread = read(fd, ptr, nleft)) < 0) {
      if (errno == EINTR)
	nread = 0;              /* and call read() again */
      else
	return(-1);
    } else if (nread == 0)
      break;                          /* EOF */
    
    nleft -= nread;
    ptr   += nread;
  }
  return(n - nleft);              /* return >= 0 */
}



int main(int argc, char* argv[])
{
  struct ktc_principal server;
  struct ktc_principal client;
  struct ktc_token token;

  
  for ( ; ; ) {
    int nbytes, rc;

    nbytes = readn(STDIN_FILENO, &server, sizeof(struct ktc_principal));
    if (nbytes == 0) break;  /* EOF */
    if (nbytes < 0) {
      perror("truncated nread getting server struct");
      exit(1);
    }

    nbytes = readn(STDIN_FILENO, &client, sizeof(struct ktc_principal));
    if (nbytes <= 0) {
      perror("truncated nread getting client struct");
      exit(1);
    }

    nbytes = readn(STDIN_FILENO, &token, sizeof(struct ktc_token));
    if (nbytes <= 0) {
      perror("truncated nread getting token struct");
      exit(1);
    }


    /*
     * Fix up endian issues. Only the ktc_token structure has
     * problems...we will receive everything in network byte-order,
     * so we convert back to host convention now...
     */

    token.startTime = ntohl(token.startTime);
    token.endTime = ntohl(token.endTime);
    token.kvno = ntohs(token.kvno);
    token.ticketLen = ntohl(token.ticketLen);


    /*
     * Set the token
     */
    rc = ktc_SetToken(&server, &token, &client, 0);

    if (rc) {
      perror("ktc_SetToken failed reinstalling tokens");
      exit(1);
    }
  }

  exit(0);
}

--------------040109050401010805010901--