Professional Web Applications Themes

Several questions about uid - UNIX Programming

Greetings, In my program, I want to do this: char *username; // suppose this var. has value. if ((pid = fork() == 0) { get uid from username // Question 1: how to do? setuid(uid); get the uid's home directory // Question 2: how to do? execute the uid's .profile //Question 3: how to do? setenv("MY_VAR", "aa"); // Question 4: how to handle memory? exec("my_program"); } else if (pid > 0) { waitpid(pid); } else { perror("fork error"); } So there are totally four questions. Please help. Thanks in advance! Evan...

  1. #1

    Default Several questions about uid

    Greetings,

    In my program, I want to do this:

    char *username; // suppose this var. has value.

    if ((pid = fork() == 0)
    {
    get uid from username // Question 1: how to do?

    setuid(uid);

    get the uid's home directory // Question 2: how to do?

    execute the uid's .profile //Question 3: how to do?

    setenv("MY_VAR", "aa"); // Question 4: how to handle memory?

    exec("my_program");
    }
    else if (pid > 0)
    {
    waitpid(pid);
    }
    else
    {
    perror("fork error");
    }

    So there are totally four questions. Please help. Thanks in advance!

    Evan


    music4 Guest

  2. #2

    Default Re: Several questions about uid

    "music4" <net> writes:
     

    If your environment is POSIX-compliant, you may check getpwnam(); that
    function returns a pointer to a structure containing the broken out
    fields of the /etc/passwd entry for a given user name.
     

    Build a shell command file containing

    source /path_to_user_home/.profile
    MY_VAR=aa
    export MY_VAR
    my_program

    and exec "/bin/sh that_command_file".

    --
    Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
    Dept. of Physics, Univ. of Padova, Italy ROT13: vasa.vg
    Maurizio Guest

  3. #3

    Default Re: Several questions about uid



    music4 wrote: 

    All of this is pointless unless you are running as root. Only root can
    successfully execute setuid(). However, as has been pointed out,
    getpwnam() will the the passwd entry from the name.

    You also may have some difficulty executing the user's .profile file as
    this is a shell script source doent.

    Perhaps the easiest way to accomplish your goal is (while running as
    root), execute "su - username -c my_program" in a system() call.

    --

    Fletcher Glenn

    Fletcher Guest

  4. #4

    Default Re: Several questions about uid

    "music4" <net> schrieb 
    man -k uid | grep "(3)"
    ....
    getpwuid (3) - get password file entry
    ....
     
    man -k uid | grep "(3)"
    ....
    getpwuid (3) - get password file entry
    ....
     
    man -k execute | grep "(3)"
    ....
    system (3) - execute a shell command
    ....
     
    Huh?
    man setenv

    Your man pages are broken. Install the man pages
    (or better yet, info pages).

    HTH
    Martin



    Martin Guest

Similar Threads

  1. AMD-64 Questions
    By Ed in forum Ubuntu
    Replies: 1
    Last Post: May 7th, 09:50 AM
  2. questions
    By Ben in forum Mac Networking
    Replies: 1
    Last Post: October 27th, 06:30 PM
  3. 2 questions :) - portal questions
    By John Weinshel in forum FileMaker
    Replies: 3
    Last Post: July 14th, 04:37 AM
  4. MSN & XP Questions
    By GT in forum Windows XP/2000/ME
    Replies: 1
    Last Post: July 12th, 11:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139