/**********************************************************************
 * ti library's routine for adding a new provider
 *
 * $Author: brlewis $
 * $Source: /mit/techinfodev/src/libti/RCS/newprov.c,v $
 * $Header: /mit/techinfodev/src/libti/RCS/newprov.c,v 4.1 1995/04/10 20:21:57 brlewis Exp brlewis $
 *
 * Copyright 1993 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_newprov_c[] = "$Header: /mit/techinfodev/src/libti/RCS/newprov.c,v 4.1 1995/04/10 20:21:57 brlewis Exp brlewis $";
#endif /* lint */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <ti/ti.h>
#ifdef USE_KERBEROS
#include <krb.h>
#endif /* USE_KERBEROS */

/**********************************************************************
 * ti_newprov(TI *tp, char *source, char *username, char *password)
 *
 * - adds "username" to "source" with "password"
 * - if password is TI_KERBEROS, add realm if appropriate
 * - returns error code
 **********************************************************************/

long
ti_newprov(tp, source, username, password)
     TI *tp;
     char *source, *username, *password;
{
  long code;
  char tosend[1024];
  char torecv[256];
#ifdef USE_KERBEROS
  char realm[REALM_SZ];
#endif /* USE_KERBEROS */

  sprintf(tosend, "N:%s:%s", source, username);

#ifdef USE_KERBEROS
  if (!strcmp(password, TI_KERBEROS))
    {
      /* Convert "brlewis" to "brlewis.@ATHENA.MIT.EDU", but
	 don't convert "group=apo" to "group=apo.@ATHENA.MIT.EDU" */
      if (!strchr(username, '@') && !strchr(username, '='))
	/* add realm */
	{
	  if (!strchr(username, '.')) strcat(tosend, ".");
	  krb_get_lrealm(realm, 1);
	  strcat(strcat(tosend, "@"), realm);
	}
    }
#endif /* USE_KERBEROS */

  strcat(strcat(tosend, ":"), password);

  /* send command and receive reply */
  if (code = _ti_talk(tp, tosend, strlen(tosend), torecv, 255))
    return(code);

  /* failure? */
  if (isdigit(torecv[0]))
    {
      strncpy(ti_context, tosend, 126);
      ti_context[127] = '\0';
      return(_ti_srverr(torecv));
    }

  return(0L);
}
