[prev in list] [next in list] [prev in thread] [next in thread] 

List:       hpux-devtools
Subject:    RE: HPUX-DEVTOOLS: seteuid under HPUX11.0
From:       "BENNETT,ANDY (HP-Unitedkingdom,ex1)" <andy_bennett () hp ! com>
Date:       2001-02-28 12:05:35
[Download RAW message or body]

Check out "man setresuid".

> -----Original Message-----
> From: Sriram Natarajan [mailto:sriram.natarajan@sun.com]
> Sent: 28 February 2001 05:33
> To: hpux-devtools@cxx.cup.hp.com
> Subject: HPUX-DEVTOOLS: seteuid under HPUX11.0
> 
> 
> Hi,
>  I am porting an application from Solaris to HP-UX 11.0 where we use
> seteuid and  setegid to change the effective user/group id to 
> a non root
> user id and then later revert it back to the original uid/gid values.
> 
> It seems to me that under HP-UX 11.0 we do not have seteuid 
> system call.
> So I was using setuid and setgid to do this job under HP-UX 11.0
> Presently the problem I am encountering is once I became a 
> non root user
> ( for eg sriramn) I am not able to revert myself back as a root user.
> But the same code works fine under Solaris/Linux platforms . Our
> application will be always started as a root user id.
> 
> Any suggestions are very much appreciated. I am attaching a 
> sample code
> for your kind reference.
> 
> Thanks,
> Sriram.
> 
> 
> --- sample source code --
> #include <sys/types.h>
> #include <stdio.h>
> #include <unistd.h>
> #include <pwd.h>
> #include <grp.h>
> 
> /* basic pre macros */
> #define  OK  0
> #define  FAIL -1
> 
> /* OS dependent macros */
> #ifdef __hpux
> #define seteuid setuid
> #define setegid setgid
> #endif
> 
> uid_t get_user_id()
> {
>         return geteuid();
> }
> 
> gid_t get_group_id()
> {
>         return getegid();
> }
> 
> static int gname2id(const char *Name, gid_t *pId)
> {
>     struct group *ent;
> 
>     if (Name[0] == '#')
>         {
>                 *pId = atoi(&Name[1]);
>                 return OK;
>         }
> 
>     if (!(ent = getgrnam(Name)))
>         {
>                 return FAIL;
>     }
>         *pId = ent->gr_gid;
> 
>     return OK;
> }
> 
> static int uname2id(const char *Name, uid_t *pId)
> {
>     struct passwd *ent;
> 
>     if (Name[0] == '#')
>         {
>                 *pId = atoi(&Name[1]);
>                 return OK;
>         }
> 
>     if (!(ent = getpwnam(Name)))
>         {
>                 return FAIL;
>     }
>     *pId = ent->pw_uid;
> 
>         return OK;
> }
> 
> int set_user(char *User_name)
> {
>  uid_t user_id;
> 
>         if (!User_name)
>                 return FAIL;
> 
>         if (uname2id(User_name, &user_id) != OK)
>                 return FAIL;
> 
>         if (seteuid(user_id))
>  {
>   perror("\nError in seting the effective uid");
>                 return FAIL;
>  }
>         return OK;
> }
> 
> int set_group(char *Group_name)
> {
>         gid_t group_id;
> 
>         if (!Group_name)
>                 return FAIL;
> 
>         if (gname2id(Group_name, &group_id) != OK)
>                 return FAIL;
> 
>         if (setegid(group_id))
>  {
>   perror("\nError in seting the effective group id");
>                 return FAIL;
>  }
>         return OK;
> }
> 
> int main()
> {
> 
>  gid_t group_id;
>  if (set_group("users") == OK)
>   fprintf(stderr,"group set successfully \n");
> 
>  if (set_user("sriramn") == OK)
>   fprintf(stderr,"user set successfully \n");
> 
>  fprintf(stderr,"now reverting back \n");
> 
>  if (set_user("root") == OK)
>   fprintf(stderr,"user set successfully \n");
> 
>  if (set_group("sys") == OK)
>   fprintf(stderr,"group set successfully \n");
> 
>  return 0;
> }
> --------
> 
>  _________________________________________________________________
>  To leave this mailing list, send mail to majordomo@cxx.cup.hp.com
>     with the message UNSUBSCRIBE hpux-devtools
>  _________________________________________________________________
> 
 _________________________________________________________________
 To leave this mailing list, send mail to majordomo@cxx.cup.hp.com
    with the message UNSUBSCRIBE hpux-devtools
 _________________________________________________________________

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic