Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: Private temporal files on Windows

by salva (Canon)
on Dec 19, 2014 at 10:36 UTC ( #1110833=note: print w/replies, xml ) Need Help??


in reply to Re^3: Private temporal files on Windows
in thread Private temporal files on Windows

I already have a solution using extprog that mostly works, the problem with that approach is that, as sshg3 reuses the connections, you never kwnow if it is actually going to run the helper program or not and when it doesn't, the parent just get stalled at the $pipe->Connect.

The option I am considering now is opening an anonymous pipe on the parent (just using pipe), mark the read side as inheritable and then recover and use it from the helper. The problem is reconstructing a Perl level fh from the windows handler... probably, the easiest solution would be to move away from Perl there, program the helper in C and just bundle it precompiled.

Anyway, I still have to check that the file handle survives the full chain of program calls (perl -> sshg3 -> cmd -> helper).

Also, a minor issue I have found with extprog is that a console window pops up briefly. Besides the visual ugliness what really worries me is if that could cause the helper invocation to fail on contexts lacking a GUI environment. For instance, when called from the task scheduler, a web server, or any other program not started inside an user session.

Replies are listed 'Best First'.
Re^5: Private temporal files on Windows
by Corion (Patriarch) on Dec 19, 2014 at 10:39 UTC

    If you want to avoid the console windows popping up, use wperl.exe instead of perl.exe for the helper process. Note that STDIN etc. will be unavailable.

      But the way to pass the password back to the caller is via STDOUT!
        You can use also some win32 freak-trick to hide the window as found in this post
        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^5: Private temporal files on Windows
by BrowserUk (Patriarch) on Dec 19, 2014 at 11:08 UTC
    the parent just get stalled at the $pipe->Connect.

    Um..?

    async{ my $pipe = Win32::Pipe->new( 'MyPipe' ); while( 1 ) { $pipe->Connect; $pipe->Write( 'The quick brown fox' ); $pipe->Disconnect; } }->detach;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^5: Private temporal files on Windows
by BrowserUk (Patriarch) on Dec 19, 2014 at 12:37 UTC
    Also, a minor issue I have found with extprog is that a console window pops up briefly. Besides the visual ugliness what really worries me is if that could cause the helper invocation to fail on contexts lacking a GUI environment. For instance, when called from the task scheduler, a web server, or any other program not started inside an user session.

    Hm. Given that you aren't spawning the program, none of the usual tricks to prevent the window popping will work.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      But what really bothers is not the windows popping but if it will always be able to pop...

        If you have to go the private file route, this may help. It creates a file that can only be read by the current userid, and writes the password to it. It then waits for keyboard input and deletes the file.

        I've verified that other users, including administrators cannot read or delete the file, nor even inspect its permissions.

        Also, in theory, by using FILE_ATTRIBUTE_TEMPORARY, the contents may never actually be written to the disk, and (assuming a small file and a system file cache that is not overrun) only ever exist in cache. (In theory!)

        #include <windows.h> #include <sddl.h> #include <stdio.h> #include <conio.h> #include <Lmcons.h> #include "debug.h" #define TESTFILENAME "PermissionsTest.txt" int main( int argc, char **argv ) { char userName[ UNLEN+1 ]; char sid[ 400 ]; char *stringSID; char domainName[ 256 ]; SID_NAME_USE sidType; char pswd[] = "The quick brown fox"; char ssdTemplate[] = "O:%sD:P(A;;FA;;;%s)"; char ssd[1024]; SECURITY_DESCRIPTOR *psd = NULL; SECURITY_ATTRIBUTES sa = { sizeof( SECURITY_ATTRIBUTES ), NULL, 0 +}; ULONG sdSize, unSize = sizeof( userName ), sidSize = sizeof( sid ) +, dnSize = sizeof( domainName ), written; HANDLE h; DIEIF( !GetUserName( userName, &unSize ), NULL ); DIEIF( !LookupAccountName( NULL, userName, sid, &sidSize, domainNa +me, &dnSize, &sidType ), NULL ); printf( "Got sid\n" ); DIEIF( !ConvertSidToStringSid( sid, &stringSID ), NULL ); printf( "SID (as string): '%s'\n", stringSID ); sprintf_s( ssd, sizeof(ssd), ssdTemplate, stringSID, stringSID ); printf( "SSD: '%s'\n", ssd ); DIEIF( !ConvertStringSecurityDescriptorToSecurityDescriptor( ssd, +SDDL_REVISION_1, &psd, &sdSize ), NULL ); printf( "psd:%x sdSize: %d\n", psd, sdSize ); sa.lpSecurityDescriptor = psd; DIEIF( ( h = CreateFile( TESTFILENAME, GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ +ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL )) == INVALID_HANDLE_VALUE, NULL ); printf("File created\n" ); DIEIF( !WriteFile( h, pswd, sizeof( pswd ), &written, NULL ), NULL + ); printf( "File written '%s'\n", pswd ); while( !_kbhit() ) Sleep( 1 ); printf( "Closing file\n" ); DIEIF( !CloseHandle( h ), NULL ); DIEIF( !DeleteFile( TESTFILENAME ), NULL ); return 0; }

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^5: Private temporal files on Windows
by salva (Canon) on Dec 20, 2014 at 08:57 UTC
    Anyway, I still have to check that the file handle survives the full chain of program calls (perl -> sshg3 -> cmd -> helper)

    It doesn't, the file handle is not inherited by sshg3 children.

      You could try to use the technique from FDPasser to pass the filehandle as a string in %ENV to the helper process... But then I guess you could pass the information via %ENV already :-/

        Well, that led me into using OpenProcess+DuplicateHandler to get the handler from the perl process into the helper and it works!

        On the other hand, how secure is that? I guess only processes from the same user would be able to reopen handlers.

        I will post a link to the code once I clean it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1110833]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2022-05-19 01:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?