I have a function I call runas which is used to switch a program to another user. It is having trouble because it needs to be thread safe and reentrant but the reentrant version of getpwnam "getpwnam_r" is giving me trouble. It returns a user id but it is always wrong and I am not sure why.
I appreciate any help you can give me.
Thank you.
Code:int runas ( char *user ) { uid_t uid = 0; if(user == NULL) { return EINVAL; } if(isdigit((int) *user)) { uid = myatoi(user); if (uid < 0 || uid >= INT_MAX) { return ERANGE; } } else { struct passwd pwd,*result; char pwdbuffer[200]; int pwdlinelen = 200; if((getpwnam_r(user,&pwd,pwdbuffer,pwdlinelen,&result)) != 0) { syslog(LOG_ERR,"Error with getpwnam_r(%s)",user); return EINVAL; } uid = pwd.pw_uid; fprintf(stderr,"uid=%i\n",uid); } if(uid == 0) { return EACCES; } if(setgroups ( 0,NULL ) == -1) { return errno; } if(setuid(uid) < 0 || seteuid(uid) < 0) { return errno; } return 0; }



LinkBack URL
About LinkBacks


