Solved

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

Posted on 2010-09-24
3
593 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
3 Comments
 
LVL 76

Assisted Solution

by:arnold
arnold earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
The comments were satisfactory.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now