The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
C function to test existence of a login xavier054 High Level Programming 2 03-04-2008 10:51 AM
Csh to check for existence of file Raynon Shell Programming and Scripting 9 12-05-2007 06:20 PM
How to check the file existence using shell scripting in Solaris-10 krevathi1912 SUN Solaris 2 11-26-2007 02:07 AM
Variable check for existence ? samit_9999 UNIX for Dummies Questions & Answers 2 12-05-2006 02:15 PM
check for FILES existence mpang_ Shell Programming and Scripting 3 06-28-2006 03:51 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-01-2008
Registered User
 

Join Date: Mar 2008
Posts: 11
Check existence of a login

Hi everybody,

I need to check in C program wether a given login is known on the system. Is there any system function that could do this ?

So far, all I could find is getpwnam(), which answers my problem by parsing the local password database. But won't work if a user is authenticated by other means (PAM, Kerberos, ...).

I am suprised not to have found anything on that topic, because any shell is able to resolve a user's HOME while doing the tilde expansion, but how is it done? I had a look at the shell C sources, but I am a bit lost. Also, I don't have a system with network authentication handy, so I can't ktrace /bin/sh while doing a tilde expansion to see what function is called...

Thanks in advance, any help much appreciated.

Xavier
Reply With Quote
Forum Sponsor
  #2  
Old 03-02-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
When a user logs in using login(1) login sets the $HOME environmental variable. The shell uses that environmental variable to find a users home directory.

AFAIK, there is no single API which will check whether a login is known to all authentication methods (local, NIS, PAM, etc.) used on a particular system.
Reply With Quote
  #3  
Old 03-02-2008
Registered User
 

Join Date: Mar 2008
Posts: 11
Quote:
Originally Posted by fpmurphy View Post
When a user logs in using login(1) login sets the $HOME environmental variable. The shell uses that environmental variable to find a users home directory.

AFAIK, there is no single API which will check whether a login is known to all authentication methods (local, NIS, PAM, etc.) used on a particular system.
Thanks for your answer.
That is true for the current user, who is obviously logged in. However, if I have an existing user, say bob with HOME=/home/bob, on a system, then anyone will see ~bob expanded to /home/bob in their shell. Without that bob has ever logged in (and so called login(1)), and regardless of the authentication method as far as I could experiment...

On the other hand, if bob doesn't exist, then ~bob won't expand.
So the shell knows whether a user exists or not... the question is how? which I should find in the sources
Reply With Quote
  #4  
Old 03-02-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
If the system is PAM based then you can use pam_authenticate to verify user access (and probable existence).
Linux example: Chapter*8.*An example application
Reply With Quote
  #5  
Old 03-02-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
Quote:
So the shell knows whether a user exists or not... the question is how?
No, in general, a shell does not know whether a particular user exists or not. The operating system may know that information.

As for tilde expansion, here is the relevant section from ksh93 s+.
Code:
/*
 * This routine is used to resolve ~ expansion.
 * A ~ by itself is replaced with the users login directory.
 * A ~- is replaced by the previous working directory in shell.
 * A ~+ is replaced by the present working directory in shell.
 * If ~name  is replaced with login directory of name.
 * If string doesn't start with ~ or ~... not found then 0 returned.
 */

static char *sh_tilde(register const char *string)
{
        register char           *cp;
        register int            c;
        register struct passwd  *pw;
        register Namval_t *np=0;
        static Dt_t *logins_tree;
        if(*string++!='~')
                return(NIL(char*));
        if((c = *string)==0)
        {
                if(!(cp=nv_getval(nv_scoped(HOME))))
                        cp = getlogin();
                return(cp);
        }
        if((c=='-' || c=='+') && string[1]==0)
        {
                if(c=='+')
                        cp = nv_getval(nv_scoped(PWDNOD));
                else
                        cp = nv_getval(nv_scoped(OLDPWDNOD));
                return(cp);
        }
        if(logins_tree && (np=nv_search(string,logins_tree,0)))
                return(nv_getval(np));
        if(!(pw = getpwnam(string)))
                return(NIL(char*));
        if(!logins_tree)
                logins_tree = dtopen(&_Nvdisc,Dtbag);
        if(np=nv_search(string,logins_tree,NV_ADD))
                nv_putval(np, pw->pw_dir,0);
        return(pw->pw_dir);
}
Reply With Quote
  #6  
Old 03-03-2008
Registered User
 

Join Date: Mar 2008
Posts: 11
Thumbs up

Quote:
Originally Posted by fpmurphy View Post
No, in general, a shell does not know whether a particular user exists or not. The operating system may know that information.

As for tilde expansion, here is the relevant section from ksh93 s+.
Thanks, this answers my question. Furthermore I was wrong, and you are right: the shell knows only for users already logged at least once on the system. I could test getpwnam() on a set of PCs running Linux with PAM authentication, it failed for PAM logins of users that never logged on the machine. Same occurred with tidlde expansion.

BTW, I understand that getpwnam() manpage is confusing : it claims that /etc/passwd is parsed. But seen the output of last(1), and the outcome of my test, I understand it is rather /var/run/wtmp...

X.
Reply With Quote
  #7  
Old 03-04-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
Quote:
BTW, I understand that getpwnam() manpage is confusing : it claims that /etc/passwd is parsed. But seen the output of last(1), and the outcome of my test, I understand it is rather /var/run/wtmp...
Wrong. The getpwnam() searches the user database for an entry with a matching name.
Note, that I did not say searches /etc/passwd.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0