Solved

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

Posted on 2010-09-24
3
619 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 77

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
computer science syllabus 3 88
Exception thrown at 0x00007FFD5BC81F28 7 49
unix example issues 18 98
Writing a UNIX script menu allowing for multiple options. 5 87
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

840 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