C++. Get logged in user name from Linux daemon - c++

is there a way to get logged in user name from linux daemon?
I tried
seteuid(1000);
std::string userName = getlogin();
But seems that this call fails and my application is terminated after.
The general situation is following: some script that runs my daemon process. Inside of this daemon I start another UI process (lets call it A). Then in process A I'm trying to get logged in user name with way, described earlier. And my process A is terminated after getlogin call. Is there any reliable way to get logged in user name from process A?

getlogin() reads the login information from the utmp file, so it won't work if the process isn't associated with a terminal.
I assume from the seteuid call in your example that process A is running with the effective user ID of the original user.
If you don't have a terminal, but have the uid you need to use the "passwd" database (often, but not always, backed by the /etc/passwd file):
struct passwd *pw = getpwuid(geteuid());
string userName = pw->name;

Related

How can I get the username in the task script?

I have a task within a workflow that invokes a service on the back end that, along with other data sent from the task, waits for the name of the user who executed that task.
How can I get the user or the username in the task script which is written in Groovy?
You mean a script task following a user task? Assuming you would like to access the name of the user who completed the previous user task from your script, you can store the user name on completion in a process data as shown here:
https://github.com/rob2universe/bpmrun-add-to-dockerimg/blob/e2dd62122b7d006a8a719be96b0a3d043efc9851/configuration/resources/groovyprocess.bpmn#L23
You can then subsequently access the process data in the groovy script task like this:
https://github.com/rob2universe/bpmrun-add-to-dockerimg/blob/e2dd62122b7d006a8a719be96b0a3d043efc9851/configuration/resources/groovyprocess.bpmn#L18
You could either get the task assignee as mentioned by #rob2universe, or make use of the execution object, which is always accessible from the script (see Camunda Docs), as follows:
user = execution.getProcessEngineServices().getIdentityService().getCurrentAuthentication().getUserId()
See IdentityService.
The username can be then saved to a process variable:
execution.setVariable('username', user)
And accessed:
username = execution.getVariable('username')

Assigning JobObject to process with non-zero session id launched from a SYSTEM service

I have a Windows SYSTEM service which needs to launch a new process in the context of the logged-in user. Along with this, I need to create a Job for the new process with certain limits.
I am extracting the process token of explorer.exe and duplicating it to create a primary token. I use this token in CreateProcessAsUser to create the new process running in the context of the user with a session id which is non-zero. When I assign the Job to this process, AssignProcessToJobObject function fails with Access denied error. Specifically, I am not able to set JOBOBJECT_BASIC_UI_RESTRICTIONS limits (JOBOBJECT_EXTENDED_LIMIT_INFORMATION works though).
The process is created as suspended and after assigning the job, I am resuming the thread.
When I use the token of the current process (i.e. SYSTEM service with session id 0) instead of explorer.exe, everything works fine.
I am testing this on Windows 10

Start pty asking for login credentials

I've been working on a program in c++ that fork a pty. Everything goes well except for one thing: when the root run the program, the pty logs-in as the root user. In the same way, if a user 'x' runs the program, the new pty logs in as 'x' user.
How can it start a pty asking for the user credentias and login in? i know that ssh or pty1(ctr + alt + 1) does.
EDIT: Here is like i fork the pty
http://pastebin.com/3vLQynz2
To be allowed to run something as a different user, you have to have to right to change to uid (man setuid). Normally you can only do this as user 'root'.
Therefore if you want to implement something like this either your program has to run as suid root or you have to use some other executeable that is suid root. For example you could ask the user which user it wants to be. Then run /bin/su to ask the user for his password.
BTW: the mentioned binary /bin/login will only work if you already run as user 'root'.

Trying to interpret user session states on Windows OS

If I call the following API from a local service running on Windows 7:
WTS_SESSION_INFO* pWSI;
DWORD nCntWSI;
WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, NULL, 1, &pWSI, &nCntWSI);
and then go through all returned WTS_SESSION_INFO structs in pWSI and check WTS_CONNECTSTATE_CLASS State members, can someone explain what is the difference between WTSActive and WTSConnected?
Connected means the user has connected and has been (or soon will be) presented with a login screen but hasn't completed it and been verified yet. He might be typing his password, for example.
If the user has locked the workstation, it's been locked by a screensaver, or he has switched to another user account, it doesn't end his session. The user remains logged in and his session would remain marked active. So being connected but not active means there are no processes running under that user's account. (The one caveat being there could be a service or other process running in a separate session under that user's credentials, but that's a different matter.)

How to get the logged on user from a process running as root?

I have a program running as root and call another program to run(A).
I want A to run when the user logs on. I've used command: su - 'username' -c A,
or in the A main function, I've called: setuid(current_uid_logged).
But I don't know the way to get the user-name of the logged on user, or the uid in the root process.
Ways that I've tried: getenv("USERNAME") or getlogin() always return root account.
I've confused with getlogin(), my program is running when kernel start and wait for user login (I have a thread to wait a Finder process (Mac OSX) running to detect user logged on), wait 10 seconds and call getlogin() but sometime, it returned root but can return user login. I think Finder process is running when user logged on.
But when I call my app to run with sudo command, getlogin() always returns current user logged on.
How do I do this?
getlogin(3) returns the name of the user who owns the process's controlling terminal. This has nothing to do with the username of whoever might log in to the GUI of the operating system. Instead, getlogin(3) and getuid(2) will only ever return the name / uid of the user account that started the program -- they have more to do with the process history than with any user sitting in front of the computer.
There are similar stories with the environment variables USER and LOGNAME -- if either one was set in a process, it was by a process higher in the process's call tree. It too cannot be influenced by whichever user eventually sits in front of the computer.
I'm sure there is some mechanism to discover the currently-logged-on user on an OS X machine, but it won't be a traditional Unix API that provides it to you.
how about uid_t getuid()?
more details at http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/getuid.2.html