Setting UID to User Other Than Root in C++ - c++

I am developing in C++ on NetBeans 6.9 on Ubuntu 11.04. I am making a system() call which I would like to be called as user "peter" rather than as root. I understand that
setuid(0);
sets the user to root but how does one set the user to peter?
Thanks,
Peter.

You perhaps want to search the password file for the correct user id via, e.g. getpwnam(). Something like:
// look up peter's uid
uid_t peter_uid=getpwnam("peter")->pw_uid;
// Become peter
setuid(peter_uid);
seteuid(peter_uid);

Related

How to find user name from the session ID?

I have an user win32 application . That enumerates all the process details along with ProcessId. I need to print the User name along with the session ID. SessionID is enumerated using ProcessIdToSessionId() method. I want to print the User name of corresponding SessionID. This application is specially designed for WindowsXP , so the API should support WindowsXP. If anyone knows the solution please share it.
DWORD dwSessionId,dwPid,dwErr;
char* Uname;
ProcessIdToSessionId(dwPid,&dwSessionId);
WTSQuerySessionInformation() with the WTSInfoClass parameter set to WTSUserName:
A null-terminated string that contains the name of the user associated with the session.

C++ Change Print Queue Owner

I want to know if it is possible to change the “Owner” name that is visible when you bring up the print queue to view the queued printer documents. I have a Windows Service that receives a document from a user and sends it a Windows printer, and the Owner is always the name of the user that logged into the workstation where the Service is running. I would like to change the “Owner” to something else, and this would be done in a C++ Windows DLL that the Windows Service loads and uses.
Added 8/30/16#9:14am ET
Thanks for the suggestion, Thomas. I should have mentioned my research, but it was long and empty. MSDN has a SetJob function which can take 1 of 4 structures (pJob = JOB_INFO_1…JOB_INFO_4). JOB_INFO_1, 2, and 4 have an LPSTR pUserName that appears to be what can be used to change the owner of the print job (Owner?). However, in the remarks section of SetJob it says:
“The following members of a JOB_INFO_1, JOB_INFO_2, or JOB_INFO_4 structure are ignored on a call to SetJob: JobId, pPrinterName, pMachineName, pUserName, pDrivername, Size, Submitted, Time, and TotalPages.”
And JOB_INFO_3 does not have this field in it. Nice.
I did not see any other functions that could maybe do this. Can you point me to something specific that I can research more? Or that is known to work? Thanks.
I'm not sure why MSDN says those fields are ignored. I change pUserName, pDocument, pStatus using SetJob with JOB_INFO_1 and it works perfectly.
Just be sure to set Position to JOB_POSITION_UNSPECIFIED.

How to change Fennec application name

I need to change Fennec application name.
I build Fennec using next instruction https://wiki.mozilla.org/Mobile/Fennec/Android. Them when after apk installation application name is "Fennec Misha". Instruction tells that
The name of the app that shows up on your phone will be "Fennec $USER"
(where $USER is the username under which you built the code).
And when I change $USER using export USER=app
$USER variable is changed but application name is still the same "Fennec Misha"
Solution to create local user with required name doesn't fits to me.
What I'm doing wrong?
Override MOZ_APP_DISPLAYNAME. If it's not overridable edit /mobile/android/branding/unofficial/configure.sh

Can I programmatically use the OneNote printer driver?

I have a system service that handles print requests, and given a printer name from the user, attaches a DC to that printer. It starts a document, ends it, and detatches.
m_PrinterDC.CreateDC (L"WINSPOOL", _printerName.c_str(), NULL, NULL)
m_DC.Attach(m_hprinter)
m_DC.StartDoc(...)
...
mDc.TextOut(...)
...
m_DC.EndDoc()
m_DC.Detatch()
This works fine for normal printers, but when using the "Print to OneNote" functionality (driver name 'Send To OneNote 2010') it doesn't seem to work. I would like to avoid custom logic just for this feature; ideally all printers would work regardless. Any thoughts what might be going wrong? I've tried updating the printer security settings to include Print rights for group everyone; not sure what else to try.
Unfortunately, I have to guess some points, because you seem to avoid detailed description of error condition.
First, if you check all return values are success, just it may be a problem about onenote itself. Check the condition of onenote by printing using other programmes.
Second, did you check if _printerName is exact? If some of users are using other language OS, the driver name, 'Send To OneNote 2010' will be different or depending on version. Of course, if you check all return values of function calls, it recorded in your log file. However, I'm worrying about you used exact printer name by using 'EnumPrinters'.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd162931(v=vs.85).aspx
I hope this helps you a little.

Which winapi function will allow me to change logged in user's password?

I'm looking for a winapi function that will allow me to change current logged in user's password. In my case, I know current password of logged in user. I use Windows 7 Ultimate.
Thanks.
Background
The background will look weird, but I'm going to describe it for clarification. My personal home PC is used by several users (dad, sister, cousins etc.). I'm the only administrator of the PC. All others are standard user. All user accounts are password protected.
I don't like that other people use my account as they mess with anything. I always install all softwares from my account and also troubleshoot PC. When I'm away from my PC for several weeks, may be some other user need to install a software, or do something that require administrative right. For this reason they may need to use my account. There may be emergency cases, and I must allow the user to log into my account by giving account password.
I recently faced such a situation. Before leaving my PC for long time, I changed my regular password to something else. I'll change that again when I reach my PC.
So I'm thinking to write a tiny program that will run each time someone logs into my account. The program will only change current password to something else that I only know. In this case, if anyone logs into my account, installs something, logs out, and cannot logged back in as the password changes.
Suppose I set my account's password as abcd. When someone logs in, the program will change it to abcde. Next time may be abc123 and so on.
You're looking for NetUserChangePassword(). Check this MSDN link for sample code:
http://support.microsoft.com/kb/151546
You could use ADSI to connect to the local machine, get the user object and call the change password action.
MSDN doc on IADsUser::ChangePassword has the following example.
The following "C++" code example shows how to change a user password.
HRESULT ChangePassword(
IADsUser *pUser,
LPWSTR oldPasswd,
LPWSTR newPasswd)
{
HRESULT hr=S_OK;
if(!pUser) { return E_FAIL;}
hr = pUser->ChangePassword(oldPasswd, newPasswd);
printf("User password has been changed");
return hr;
}
The following VB code example shows how to get the user and change a user password.
Dim usr As IADsUser
Dim szOldPass As String
Dim szNewPass As String
On Error GoTo Cleanup
' Fabrikam is the MSDN fictional domain - us '.' for localhost
Set usr = GetObject("WinNT://Fabrikam/JeffSmith,user")
' Add code to securely retrieve the old and new password.
usr.ChangePassword szOldPass, szNewPass
Cleanup:
If (Err.Number<>0) Then
MsgBox("An error has occurred. " & Err.Number)
End If
Set usr = Nothing