Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^6: Private temporal files on Windows

by salva (Canon)
on Dec 20, 2014 at 09:12 UTC ( [id://1110907]=note: print w/replies, xml ) Need Help??


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

But what really bothers is not the windows popping but if it will always be able to pop...

Replies are listed 'Best First'.
Re^7: Private temporal files on Windows
by BrowserUk (Patriarch) on Dec 20, 2014 at 12:44 UTC

    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.

        When I tried to build Win32::SecretFile I got the following errors:

        C:\perl64\packages\p5-Win32-SecretFile-master>nmake Microsoft (R) Program Maintenance Utility Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. C:\Perl64\bin\perl.exe C:\perl64\site\lib\ExtUtils\xsubpp -ty +pemap C:\Perl64\lib\ExtUtils\typemap SecretFile.xs > SecretFile.xsc +&& C:\Perl64\bin\perl.exe -MExtUtils::Command -e mv -- SecretFile.xsc + SecretFile.c Please specify prototyping behavior for SecretFile.xs (see perlxs manu +al) cl -c -I. -nologo -GF -W3 -MD -Zi -Ox -GL -fp:precise -DWIN3 +2 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DWIN64 -DUSE_SITECUSTOMIZ +E -DPRIVLIB_LAST_IN_INC -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS - +DUSE_PERLIO -DPERL_MSVCRT_READFIX -MD -Zi -Ox -GL -fp:precise -DVE +RSION=\"0.01\" -DXS_VERSION=\"0.01\" "-IC:\Perl64\lib\CORE" Secre +tFile.c SecretFile.c SecretFile.xs(55) : warning C4267: 'function' : conversion from 'size_ +t' to 'int', possible loss of data SecretFile.xs(58) : warning C4267: 'function' : conversion from 'size_ +t' to 'int', possible loss of data SecretFile.xs(58) : warning C4267: 'function' : conversion from 'size_ +t' to 'int', possible loss of data SecretFile.xs(82) : warning C4013: 'ConvertSidToStringSid' undefined; +assuming extern returning int SecretFile.xs(87) : warning C4013: 'ConvertStringSecurityDescriptorToS +ecurityDescriptor' undefined; assuming extern returning int SecretFile.xs(99) : warning C4267: 'function' : conversion from 'size_ +t' to 'DWORD', possible loss of data link -out:blib\arch\auto\Win32\SecretFile\SecretFile.dll -dll +-nologo -nodefaultlib -debug -opt:ref,icf -ltcg -libpath:"C:\Perl64\ +lib\CORE" -machine:AMD64 SecretFile.obj C:\Perl64\lib\CORE\perl510 +.lib oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib com +dlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netapi32.l +ib uuid.lib ws2_32.lib mpr.lib winmm.lib version.lib odbc32.lib odbc +cp32.lib comctl32.lib bufferoverflowU.lib msvcrt.lib -def:SecretFile. +def Creating library blib\arch\auto\Win32\SecretFile\SecretFile.lib and + object blib\arch\auto\Win32\SecretFile\SecretFile.exp SecretFile.obj : error LNK2001: unresolved external symbol ConvertStri +ngSecurityDescriptorToSecurityDescriptor SecretFile.obj : error LNK2001: unresolved external symbol ConvertSidT +oStringSid blib\arch\auto\Win32\SecretFile\SecretFile.dll : fatal error LNK1120: +2 unresolved externals NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual S +tudio 9.0\VC\Bin\amd64\link.EXE"' : return code '0x460' Stop.

        Which confuses me because in the exact same environment and using the same includes I build my code without errors:

        C:\test>cl /W3 Permissions.c Advapi32.lib Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64 Copyright (C) Microsoft Corporation. All rights reserved. Permissions.c Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. /out:Permissions.exe Permissions.obj Advapi32.lib

        I have a work around that fixes the unresolved externals (but not the 'ConvertSidToStringSid' undefined; assuming extern returning int?); and that is to explicitly use the A versions of the two functions:

        if (ConvertSidToStringSidA(sid, &sid_as_string)) { PSECURITY_DESCRIPTOR sd = NULL; DWORD sd_size; SV *ssd_as_sv = sv_2mortal(newSVpvf(ssd_template, sid_ +as_string, sid_as_string)); LocalFree(sid_as_string); if (ConvertStringSecurityDescriptorToSecurityDescripto +rA(SvPV_nolen(ssd_as_sv), + SDDL_REVISION_1, + &sd, &sd_size)) {

        The the build completes:

        C:\test>pushd \perl64\packages\p5-Win32-SecretFile-master C:\perl64\packages\p5-Win32-SecretFile-master>nmake install Microsoft (R) Program Maintenance Utility Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. C:\Perl64\bin\perl.exe C:\perl64\site\lib\ExtUtils\xsubpp -ty +pemap C:\Perl64\lib\ExtUtils\typemap SecretFile.xs > SecretFile.xsc +&& C:\Perl64\bin\perl.exe -MExtUtils::Command -e mv -- SecretFile.xsc + SecretFile.c Please specify prototyping behavior for SecretFile.xs (see perlxs manu +al) cl -c -I. -nologo -GF -W3 -MD -Zi -Ox -GL -fp:precise -DWIN3 +2 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DWIN64 -DUSE_SITECUSTOMIZ +E -DPRIVLIB_LAST_IN_INC -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS - +DUSE_PERLIO -DPERL_MSVCRT_READFIX -MD -Zi -Ox -GL -fp:precise -DVE +RSION=\"0.01\" -DXS_VERSION=\"0.01\" "-IC:\Perl64\lib\CORE" Secre +tFile.c SecretFile.c SecretFile.xs(55) : warning C4267: 'function' : conversion from 'size_ +t' to 'int', possible loss of data SecretFile.xs(58) : warning C4267: 'function' : conversion from 'size_ +t' to 'int', possible loss of data SecretFile.xs(58) : warning C4267: 'function' : conversion from 'size_ +t' to 'int', possible loss of data SecretFile.xs(82) : warning C4013: 'ConvertSidToStringSidA' undefined; + assuming extern returning int SecretFile.xs(87) : warning C4013: 'ConvertStringSecurityDescriptorToS +ecurityDescriptorA' undefined; assuming extern returning int SecretFile.xs(99) : warning C4267: 'function' : conversion from 'size_ +t' to 'DWORD', possible loss of data link -out:blib\arch\auto\Win32\SecretFile\SecretFile.dll -dll +-nologo -nodefaultlib -debug -opt:ref,icf -ltcg -libpath:"C:\Perl64\ +lib\CORE" -machine:AMD64 SecretFile.obj C:\Perl64\lib\CORE\perl510 +.lib oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib com +dlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netapi32.l +ib uuid.lib ws2_32.lib mpr.lib winmm.lib version.lib odbc32.lib odbc +cp32.lib comctl32.lib bufferoverflowU.lib msvcrt.lib -def:SecretFile. +def Creating library blib\arch\auto\Win32\SecretFile\SecretFile.lib and + object blib\arch\auto\Win32\SecretFile\SecretFile.exp Generating code Finished generating code if exist blib\arch\auto\Win32\SecretFile\SecretFile.dll.manife +st mt -nologo -manifest blib\arch\auto\Win32\SecretFile\SecretFile.dl +l.manifest -outputresource:blib\arch\auto\Win32\SecretFile\SecretFile +.dll;2 if exist blib\arch\auto\Win32\SecretFile\SecretFile.dll.manife +st del blib\arch\auto\Win32\SecretFile\SecretFile.dll.manifest C:\Perl64\bin\perl.exe -MExtUtils::Command -e chmod -- 755 bli +b\arch\auto\Win32\SecretFile\SecretFile.dll Files found in blib\arch: installing files in blib\lib into architectu +re dependent library tree Installing C:\Perl64\site\lib\auto\Win32\SecretFile\SecretFile.dll Installing C:\Perl64\site\lib\auto\Win32\SecretFile\SecretFile.exp Installing C:\Perl64\site\lib\auto\Win32\SecretFile\SecretFile.lib Installing C:\Perl64\site\lib\auto\Win32\SecretFile\SecretFile.pdb Appending installation info to C:\Perl64\lib/perllocal.pod C:\perl64\packages\p5-Win32-SecretFile-master>

        And the code the runs:

        C:\perl64\packages\p5-Win32-SecretFile-master>popd C:\test>perl -MWin32::SecretFile -E"say Win32::SecretFile::_create_sec +ret_file( 'mysecretfile.txt', 'Now is the time for all good men.', 0 +);" 1 C:\test>type mysecretfile.txt Now is the time for all good men. C:\test>

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-29 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found