![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
Quote:
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? |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
Quote:
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);
}
|
|
#6
|
|||
|
|||
|
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... X. |
|
#7
|
|||
|
|||
|
Quote:
Note, that I did not say searches /etc/passwd. |
|||
| Google The UNIX and Linux Forums |