RCS_ID_C="$Id: loginname.c,v 2.1 1994/02/17 02:21:58 ppessi Exp $";
/*
 * loginname.c - Login name handling
 *
 * Author: ppessi <Pekka.Pessi@hut.fi>
 *
 * This file is part of the AmiTCP/IP User Library.
 *
 * Copyright © 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
 *                  Helsinki University of Technology, Finland.
 *
 * Created      : Sun Nov 28 17:45:55 1993 ppessi
 * Last modified: Thu Jan 27 08:39:51 1994 ppessi
 *
 * $Log: loginname.c,v $
 * Revision 2.1  1994/02/17  02:21:58  ppessi
 * *** empty log message ***
 *
 * Revision 1.2  1994/01/21  08:13:47  ppessi
 * Updated documentation
 *
 * Revision 1.1  1994/01/19  10:05:48  ppessi
 * Initial revision
 *
 */

#include "base.h"
#include "credential.h"
#include "libfunc.h"
#include <string.h>
#include <exec/memory.h>

/****** usergroup.library/getlogin *******************************************

    NAME
        getlogin - get login name

    SYNOPSIS
        name = getlogin()
        D0

        char *getlogin(void)

    FUNCTION
        The getlogin() routine returns the login name of the user associated
        with the current session, as previously set by setlogin().  The name
        is normally associated with a console at the time a session is
        created, and is inherited by all processes descended from the login
        process.  (This is true even if some of those processes assume
        another user ID, for example when su is used.)

    INPUTS

    RESULT
        name   - pointer to login name

    SEE ALSO
        setlogin()

****************************************************************************
*/

SAVEDS ASM char *R_getlogin(void)
{
  static char buffer[MAXLOGNAME];

  struct proc *p;

  lock(proc_list);
  p = procfind(NULL);
  memcpy(buffer, p->p_session->s_login, MAXLOGNAME);
  unlock(proc_list);

  return buffer;
}

/****** usergroup.library/setlogin *******************************************

    NAME
        setlogin - set login name

    SYNOPSIS
        success = setlogin(name)
        D0                 A1

        int setlogin(const char *);

    FUNCTION
        The function setlogin() sets the login name of the user associated
        with the current session to name. This call is restricted to the
        super-user, and is normally used only when a new session is being
        created on behalf of the named user (for example, at login time, or
        when a remote shell is invoked).

    INPUTS
        name     - Buffer to hold login name

    RESULT
        If a call to setlogin() succeeds, a value of 0 is returned.  If
        setlogin() fails, a value of -1 is returned and an error code is
        placed into global errno location.

    ERRORS
        [EPERM]  - The caller has got no necessary privileges.
        [EFAULT] - The name parameter gave an invalid address.
        [EINVAL] - The name parameter pointed to a string that was too long.
                   Login names are limited to MAXLOGNAME (from <sys/param.h>)
                   characters, currently 16.

    BUGS

    SEE ALSO
        getlogin()

**********************************************************************
*/

SAVEDS ASM int R_setlogin(REG(a1) const char *buffer)
{
  struct proc *p;
  int error;

  if (buffer == NULL) {
    error = EFAULT;
  } else if (strlen(buffer) > MAXLOGNAME - 1) {
    error = EINVAL;
  } else {
    p = procfind(NULL);

    error = suser(p->p_cred->pc_ucred);

    if (!error) {
      strncpy(p->p_session->s_login, buffer, MAXLOGNAME);
      p->p_session->s_login[MAXLOGNAME - 1] = '\0';
      return 0;
    }
  }

  SetErrno(error);
  return -1;
}
