/**********************************************************************
 * ti library's routine for authentication
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/auth.c,v $
 * $Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/auth.c,v 0.2 92/04/01 16:19:54 brlewis Exp $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/

#include <mit-copyright.h>

#ifndef lint
static char rcsid_ti_auth_c[] = "$Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/auth.c,v 0.2 92/04/01 16:19:54 brlewis Exp $";
#endif /* lint */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ti/memory.h>
#include <ti/ti.h>

/**********************************************************************
 * ti_auth(TI_H hti, char *user, char *password)
 *	NULL user/password will eventually mean user kerberos
 *
 * - returns error code
 **********************************************************************/

/* jms 7/9/92 -- windows-izing begun */

long
ti_auth(hti, user, password)
     TI_H hti;
     char *user, *password;
{
  long code;
  static char torecv[BUFSIZ];

  TI_LP lpti;

  lpti = (TI_LP)TI_DEREFERENCE(hti);

  /* send command and receive reply */
  if (code = _ti_talkf(hti, torecv, BUFSIZ - 1, "p:%s:%s\n",
	              user, password))
    CLEANUP_AND_RETURN(code)

  /* success? */
  if (torecv[0] == '0' && torecv[1] == ':')
    {
      /* set username */
      if (!ti_user(lpti) || strcmp(ti_user(lpti), user))
	{
	  Delete(ti_user(lpti));
	  TI_MEM(ti_user(lpti) = ti_dupstring(user));
	}
      /* set password */
      if (!ti_password(lpti) || strcmp(ti_password(lpti), password))
	{
	  Delete(ti_password(lpti));
	  TI_MEM(ti_password(lpti) = ti_dupstring(password));
	}
      /* set primary source */
      if (!ti_source1(lpti) || strcmp(ti_source1(lpti), torecv+2))
	{
	  Delete(ti_source1(lpti));
	  TI_MEM(ti_source1(lpti) = ti_dupstring(torecv+2));
	}
      CLEANUP_AND_RETURN(0L)
    }

  /* failure */
  strcpy(ti_context, user);
  code = (_ti_srverr(torecv));

CLEANUP:
  TI_REMOVE_DEREFERENCE(hti, lpti);
  return (code);
}
