Solved

Getting Environment Variables of Another User in C++ in Unix?

Posted on 2010-09-24
3
630 Views
Last Modified: 2012-08-13
Dear experts,

I have a process running in UNIX. I want the process to get the environment variables of a user other than the user in which it is running. Then I will run some of its threads with the environment of that another user. How can I achieve this?

BR,
0
Comment
Question by:GurcanK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 79

Assisted Solution

by:arnold
arnold earned 200 total points
ID: 33755397
If you want to run an under the effective rights of another user, you have to switch to the effective user's settings. The environment variables are definable and vary depending on what the user has configured under their shell.

Are you spawning the thread using fork?

Have a look at any daemon application, http, etc. and you see that while the process starts running as root it then continues to run under a user with lower level rights.

I.e. you can use execv with fork etc.
http://www.google.com/url?sa=t&source=web&cd=6&ved=0CC0QFjAF&url=https%3A%2F%2Fwww.securecoding.cert.org%2Fconfluence%2Fdownload%2Fattachments%2F26017980%2F07%2BUNIX%2BPermissions%2Band%2BPrivileges.pdf%3Fversion%3D1%26modificationDate%3D1238607959000&rct=j&q=unix%20programming%20fork%20drop%20privileges&ei=w8-cTI3eKIWKlwemhvnVCg&usg=AFQjCNF4mU4EeSAzTfDSBTdkFtRF_7QMXA&cad=rja

http%3A%2F%2Fwww.cs.uga.edu%2F~eileen%2F1730%2FNotes%2Fprocesses-UNIX.ppt&rct=j&q=unix%20programming%20fork%20drop%20privileges&ei=w8-cTI3eKIWKlwemhvnVCg&usg=AFQjCNFHbm7rVc-AW71bJ6okEqEeOJV3sA&cad=rja

etc.
get yourself an account on www.informit.com and you may be able to use one of the C++ programming references.

There are other similar resources where you can access various books to get an example.
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 33755665
The easiest way would be to launch a shell to execute an 'export' command that list all environment variables. From your other Q I understand that your process is running as 'root', so you could use
#include <iostream>
#include <vector>
using namespace std;

#include <stdlib.h>

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int run_cmd ( const char* pCmd, vector<string>& vOutput) {

  FILE *fp;
  int status;
  char path[PATH_MAX];

  fp = popen(pCmd, "r");
 // if (fp == NULL)
    /* Handle error */;

  while (fgets(path, PATH_MAX, fp) != NULL)
      printf("%s", path);

  status = pclose(fp);
  if (status == -1) {
    /* Error reported by pclose() */

  } else {

    /* Use macros described under wait() to inspect `status' in order
       to determine success/failure of command executed by popen() */
  }
return 0;
}

int main () {

  vector<string> vOutput;

  run_cmd("su -l OtherUser csh -c export",vOutput);

  // now vOutput contains all environment variables

  return 0;
}

Open in new window

0
 

Author Closing Comment

by:GurcanK
ID: 33763287
The comments were satisfactory.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question