Add Book to My BookshelfPurchase This Book Online

Chapter 8 - Users and Groups

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Login Names
When a user's account is created, the user is assigned a unique login name. The login name is used by user-level and system-level programs to identify individuals. The login name consists of from one to eight characters (some systems require a minimum of two; a few systems have been modified to allow more than eight). Usually, only lowercase letters and numbers are allowed in login names, although some systems will also allow some special characters such as a hyphen or underscore.
Most importantly, the login name is used when logging in to identify yourself to the system. When presented with a “login:” prompt, you enter your login name, followed by your password to gain access. Another important use for the login name is in addressing electronic mail. At some point, all electronic mail is identified by the login name of the person who sent it, and by the login name(s) of the intended recipient(s). Although it has recently become popular to allow mail to be addressed as “Firstname.Lastname@host.domain” (or something similar), this is almost universally handled by mapping the “Firstname.Lastname” strings (e.g., “Robert M. Smith,” “Robert Smith,” “Bob Smith”) to the login name (e.g., “bmsmith”) internally. Other uses for the login name include identifying output on the printer, granting or removing privileges in permissions files, and so forth.
An important part of the UNIX system that does not use the login name is the operating system kernel. The kernel instead uses your user ID number (described in the next section) to keep track of who you are and what you may do. The reason for this is quite simply that the underlying hardware makes it easier to deal with numbers than with character strings. Numbers may be tested for equality, copied from memory location to memory location, and so forth, with individual machine instructions. Character strings (login names) on the other hand, must be handled in subroutines. Since the kernel checks every request you make for permission to make such a request (e.g., if this file is readable only by the owner, you cannot open it for reading unless you own it), it is vital that these checks be as efficient as possible.
To obtain the login name of the user executing a program, all versions of UNIX provide the getlogin function:
    #include <unistd.h>
    char *getlogin(void);
This function searches the /var/adm/utmp file (described later in this chapter) for the entry for the terminal line to which the program is attached, and returns the login name contained in that entry. This method is prone to error: if the user has logged off, or is running the program without a terminal (for example, with the rsh command), getlogin will return a null pointer, indicating that it could not find the information.
The creators of System V UNIX recognized this problem, and added the routine cuserid in an attempt to avoid it.
    #include <stdio.h>
    char *cuserid(char *buf);
Like getlogin, cuserid examines the /var/adm/utmp file. However, if nothing is found, cuserid obtains the user ID number of the executing process, looks it up in the password file (how to do this is described later in this chapter), and returns the login name that way. If buf is a non-null pointer, the login name is copied into the array it points to. Otherwise, a pointer is returned to a static area that is overwritten with each call. If the login name cannot be found, a null pointer is returned.
Be aware that neither getlogin or cuserid should be trusted by programs that must know the name of the user executing a program. These include any program that uses this information to perform permissions or authorization checking. The problem with both of these functions is that they rely on the contents of the utmp file first: whatever is written there is assumed to be correct. Unfortunately, the utmp file is world-writable on many systems. This means that an unscrupulous user could change his entry in the file to the name of an authorized user, and then run your program, and you would be none the wiser. Programs that must know the true identity of the executing user should only use the user ID number to identify that user. If they also need to know the user's login name, this information can be obtained from the password file. The method for doing this is described later in this chapter.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback