pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

C++ pastebin - collaborative debugging tool View Help


Posted by dUkk on Fri 19 Jun 18:16
report abuse | download | new post

  1. // autoshare-2k8.cpp : watch for new folders in specified path and create shares with default permissions
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <Lm.h>
  7. #include <Aclapi.h>
  8.  
  9. void MakeDirShared(TCHAR *dir);
  10. void DeleteShare(TCHAR *dir);
  11.  
  12. TCHAR pathprefix[MAX_PATH];
  13.  
  14. int _tmain(int argc, _TCHAR* argv[])
  15. {
  16.         BYTE buf[4096];
  17.         DWORD buff_len=0;
  18.         DWORD outSize = sizeof(buf);
  19.        
  20.         if(argc < 2)
  21.         {
  22.                 wprintf(_T("Usage: autoshare <path>\n"));
  23.                 return -1;
  24.         }
  25.  
  26.         wsprintf(pathprefix, _T("%s"), argv[1]);
  27.  
  28.         HANDLE hDir = CreateFile(pathprefix, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE , NULL,    OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  29.         if(hDir == INVALID_HANDLE_VALUE)
  30.         {
  31.                 wprintf(_T("Error can't open path!\n"));
  32.                 return -1;
  33.         }
  34.  
  35.         while(true)
  36.         {
  37.         if(ReadDirectoryChangesW(hDir, &buf, outSize, FALSE, FILE_NOTIFY_CHANGE_DIR_NAME, &buff_len, NULL, NULL) != 0)
  38.         {
  39.                 PFILE_NOTIFY_INFORMATION fni = (FILE_NOTIFY_INFORMATION*)&buf;
  40.                 DWORD offset=0;
  41.                 do
  42.                 {
  43.                         TCHAR filename[MAX_PATH];
  44.                         offset=fni->NextEntryOffset;
  45.                         lstrcpynW(filename, fni->FileName, fni->FileNameLength/sizeof(WCHAR)+1);
  46.                         wprintf(_T("dir: %s\nAction: %d\nNext offset: %d\n"), filename, fni->Action, offset);
  47.                         switch(fni->Action)
  48.                         {
  49.                         case FILE_ACTION_RENAMED_NEW_NAME:
  50.                         case FILE_ACTION_ADDED:
  51.                                 MakeDirShared(filename);
  52.                                 break;
  53.                         case FILE_ACTION_RENAMED_OLD_NAME:
  54.                         case FILE_ACTION_REMOVED:
  55.                                 DeleteShare(filename);
  56.                                 break;
  57.                         }
  58.                         fni = (PFILE_NOTIFY_INFORMATION)((LPBYTE)buf + fni->NextEntryOffset);
  59.                 }
  60.                 while(offset != 0L);
  61.         }
  62.         }
  63.  
  64.         CloseHandle(hDir);
  65.  
  66.         return 0;
  67. }
  68.  
  69.  
  70. void MakeDirShared(TCHAR *dir)
  71. {
  72.  
  73.     DWORD dwRes;
  74.     PSID pEveryoneSID = NULL;
  75.     PACL pACL = NULL;
  76.     PSECURITY_DESCRIPTOR pSD = NULL;
  77.     EXPLICIT_ACCESS ea[1];
  78.     SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
  79.  
  80.     if(!AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID))
  81.     {
  82.         wprintf(_T("AllocateAndInitializeSid Error %u\n"), GetLastError());
  83.         goto Cleanup;
  84.     }
  85.  
  86.     ZeroMemory(&ea, 1 * sizeof(EXPLICIT_ACCESS));
  87.     ea[0].grfAccessPermissions = GENERIC_ALL;
  88.     ea[0].grfAccessMode = SET_ACCESS;
  89.     ea[0].grfInheritance= NO_INHERITANCE;
  90.     ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  91.     ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
  92.     ea[0].Trustee.ptstrName  = (LPTSTR) pEveryoneSID;
  93.  
  94.     dwRes = SetEntriesInAcl(1, ea, NULL, &pACL);
  95.     if (ERROR_SUCCESS != dwRes)
  96.     {
  97.         wprintf(_T("SetEntriesInAcl Error %u\n"), GetLastError());
  98.         goto Cleanup;
  99.     }
  100.  
  101.     pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  102.     if (NULL == pSD)
  103.     {
  104.         wprintf(_T("LocalAlloc Error %u\n"), GetLastError());
  105.         goto Cleanup;
  106.     }
  107.  
  108.     if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
  109.     {  
  110.         wprintf(_T("InitializeSecurityDescriptor Error %u\n"), GetLastError());
  111.         goto Cleanup;
  112.     }
  113.  
  114.     if (!SetSecurityDescriptorDacl(pSD, TRUE, pACL, FALSE))
  115.     {  
  116.         wprintf(_T("SetSecurityDescriptorDacl Error %u\n"), GetLastError());
  117.         goto Cleanup;
  118.     }
  119.  
  120.         SHARE_INFO_502 info;
  121.         TCHAR fullpath[MAX_PATH];
  122.         TCHAR sharename[MAX_PATH];
  123.        
  124.         wsprintf(fullpath, _T("%s\\%s"), pathprefix, dir);
  125.         wsprintf(sharename, _T("%s$"), dir);
  126.  
  127.         info.shi502_netname = sharename;
  128.         info.shi502_type = STYPE_DISKTREE;
  129.         info.shi502_remark = (LPTSTR)L"";
  130.         info.shi502_permissions = ACCESS_ALL;
  131.         info.shi502_max_uses = -1;
  132.         info.shi502_current_uses = 0;
  133.         info.shi502_path = fullpath;
  134.         info.shi502_passwd = (LPTSTR)L"";
  135.         info.shi502_reserved = 0;
  136.         info.shi502_security_descriptor = pSD;
  137.         DWORD parm_err;
  138.         dwRes=NetShareAdd(NULL, 502, (LPBYTE) &info, &parm_err);
  139.         if(dwRes==0)
  140.                 wprintf(_T("Share created.\n"));
  141.         else
  142.                 wprintf(_T("NetShareAdd() error: %u\tparmerr=%u\n"), dwRes, parm_err);
  143.        
  144.  
  145. Cleanup:
  146.     if (pEveryoneSID)
  147.         FreeSid(pEveryoneSID);
  148.     if (pACL)
  149.         LocalFree(pACL);
  150.     if (pSD)
  151.         LocalFree(pSD);
  152.  
  153.     return;
  154.  
  155. }
  156.  
  157. void DeleteShare(TCHAR *dir)
  158. {
  159.         NET_API_STATUS res;
  160.         TCHAR sharename[MAX_PATH];
  161.        
  162.         wprintf(_T("%s\n"), dir);
  163.         wsprintf(sharename, _T("%s$"), dir);
  164.         res=NetShareDel(NULL, sharename, 0);
  165.         if(res==0)
  166.                 wprintf(_T("Share Removed.\n"));
  167.         else
  168.                 wprintf(_T("NetShareDel() error: %u\n"), res);
  169.         return;
  170. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post