|
-
Jul 29th, 2003, 01:55 AM
#1
Thread Starter
Fanatic Member
Redirect output of CreateProcessAsUser
Hi,
I want to start a process on WinNt and then redirect the output to a pipe and then send it back to the socket..
Please see the code below.. It gets stuck at ReadFile().
Code:
void RunOnNT(char *User, char *Pwd, char *Command, SOCKET aCon)
{
STARTUPINFO startUpInfo;
PROCESS_INFORMATION procInfo;
SECURITY_ATTRIBUTES pa;
HANDLE hRootsid;
HANDLE hdTk;
TOKEN_USER* pTk = NULL;
UCHAR tmpName[NAME_BUF_SZ], domName[NAME_BUF_SZ];
DWORD dwSize, tmpSize=NAME_BUF_SZ, domSize=NAME_BUF_SZ;
SID_NAME_USE snu;
HANDLE hRead, hWrite;
DWORD bRead;
char lpBuffer[MAX_SIZE];
char AppName[NAME_BUF_SZ];
OpenProcessToken(GetCurrentProcess() ,TOKEN_READ, &hdTk);
GetTokenInformation(hdTk, TokenUser, pTk, 0, &dwSize);
pTk = (TOKEN_USER*)malloc(dwSize);
if(pTk == NULL)
{
return;
}
GetTokenInformation(hdTk, TokenUser, pTk, dwSize, &dwSize);
if(FALSE == LookupAccountSid(NULL, pTk->User.Sid, (char *)tmpName,
&tmpSize, (char *)domName, &domSize, &snu))
{
return;
}
free(pTk);
if (!LogonUser (User,
NULL,
Pwd,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
&hRootsid) )
{
sprintf(lpBuffer,"LogonUser failed error= %ld\n",GetLastError());
int nw = send(aCon ,lpBuffer,sizeof(lpBuffer),0);
return;
}
memset((void *)&pa,0,sizeof(SECURITY_ATTRIBUTES));
pa.nLength = sizeof(SECURITY_ATTRIBUTES);
pa.lpSecurityDescriptor = NULL;
pa.bInheritHandle = TRUE;
memset(&procInfo, 0, sizeof(PROCESS_INFORMATION));
if ( CreatePipe(&hRead,&hWrite,&pa,0) != 0)
{
memset(&startUpInfo, 0, sizeof(STARTUPINFO));
startUpInfo.cb = sizeof(STARTUPINFO);
startUpInfo.dwFlags = STARTF_USESTDHANDLES;
startUpInfo.hStdOutput = hWrite;
startUpInfo.hStdError = hWrite;
memset(AppName,0,sizeof(AppName));
strcat(AppName,"RevEXAgent.exe ");
strcat(AppName,Command);
printf("\nApp -%s\n",AppName);
// Create the child process
if (CreateProcessAsUser(hRootsid,
0,
AppName,
0,
0,
FALSE,
0,
0,
NULL,
&startUpInfo,
&procInfo))
{
//WaitForSingleObject(procInfo.hProcess, INFINITE);
while(1)
{
memset(lpBuffer,0,sizeof(lpBuffer));
printf("Hello \n");
long bResult = ReadFile(hRead, lpBuffer, MAX_SIZE - 1, &bRead, NULL);
printf("Hello %ld\n",bResult);
if (bResult)
{
LogMessage(lpBuffer);
int nw = send(aCon ,lpBuffer,sizeof(lpBuffer),0);
if( nw < 0 )
{
perror("Error at write\n");
CloseHandle(hRootsid);
CloseHandle(procInfo.hThread);
CloseHandle(procInfo.hProcess);
closesocket(aCon );
CloseHandle(hRead);
return;
}
}
else
{
if(GetLastError() == ERROR_IO_PENDING)
{
printf("Sleep\n");
Sleep(100);
}
else
{
printf("\nError %ld\n",GetLastError());
CloseHandle(hRootsid);
CloseHandle(procInfo.hThread);
CloseHandle(procInfo.hProcess);
closesocket(aCon );
CloseHandle(hRead);
break;
}
}
}
}
else
{
CloseHandle(hWrite);
sprintf(lpBuffer,"Create Process as user failed error= %ld\n",GetLastError());
printf(lpBuffer);
int nw = send(aCon ,lpBuffer,sizeof(lpBuffer),0);
return;
}
}
else
{
sprintf(lpBuffer,"Create Pipe failed error= %ld\n",GetLastError());
int nw = send(aCon ,lpBuffer,sizeof(lpBuffer),0);
return;
}
CloseHandle(hWrite);
printf("Hello\n");
}
Any help is appreciated..
Pradeep
-
Jul 29th, 2003, 02:00 AM
#2
ReadFile returns only when it has read enough characters or EOF. Can the problem come from there?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 29th, 2003, 02:03 AM
#3
Thread Starter
Fanatic Member
Hi
Hae CornedBee.. u got to help me out now...
I can see the process getting executed at the server its not more that say 100 bytes.. but my ReadFile Gets stuck.. it doesnt exit at all.. If i run using CreateProcess its fine.. but not CreateProcessAsUser.. is there any limitation... Or else how to do it..
Please help me out..
Thanks,
Pradeep
-
Jul 29th, 2003, 02:35 AM
#4
Thread Starter
Fanatic Member
Hae Solved
.... 
I had to use TRUE in create process as user..
Code:
if (CreateProcessAsUser(hRootsid,
0,
AppName,
0,
0,
TRUE,
CREATE_NEW_CONSOLE,
0,
NULL,
&startUpInfo,
&procInfo))
{
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|