[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Protecting Text Files in Windows 2000 Server using Mutex for CGI and Normal application

Hi,
I have a CGI web service runing on Windows 2000 server, users  are using IE to read data from and modify data in a text file, to protect the text file, I uesd a Mutex which worked perfect as follow:

FLockMutex:=CreateMutex(nil, False, 'MUTEX_LOCK_DATA_TEXT_FILE');
if (FLockMutex=0)  then exit;
try
  if (WaitForSingleObject(FLockMutex, 90000)<>WAIT_OBJECT_0)
     then exit; // Time out
  // now the file is protected
  // open it for read or write
  // then close the file

finally
  ReleaseMutex(FLockMutex);
  CloseHandle(FLockMutex);
end;

Now if more than one users tries to use this web service, the Mutex will organize them to access the file one by one.

I wrote a small Delphi  program which read and modify the same Text file, so I used the same code above to protect the file, same Mutex name.

I run this small program on the same server using (Remote Desktop connection) but I found by experiment that Mutex techniques is not working between this small program and CGI.

In other words, this small program can access the text file even if the CGI is locking it !!! And if this small program is loacking the text file the CGI will be able access it also!!.

If I run the CGI in local machine (XP pro)  the behavour is different, createMutex gives handle 0 (FLockMutex is zero) which is good, so I now that someone is using the text file then I will try again later.

But I need to run the file in Windows 2000 server please help me in finding the solution, I used Semaphore instead of Mutex which gives exactly same behavior.

Thank you for the help.

Monir.
0
monir
Asked:
monir
  • 5
  • 3
1 Solution
 
kretzschmarCommented:
why not open the file exclusive?
0
 
monirAuthor Commented:

I gave a file as an example only, I need to protect a portion of program code, which deals with too many files and database.
0
 
Russell LibbySoftware Engineer, Advisory Commented:
I have a feeling it has to do with the global/local session namespaces... from the MSDN:

A Terminal Services environment has a global name space for events, semaphores, mutexes, waitable timers, file-mapping objects, and job objects. In addition, each Terminal Services client session has its own separate name space for these objects. Terminal Services client processes can use object names with a "Global\" or "Local\" prefix to explicitly create an object in the global or session name space.

Windows 2000 or later: If Terminal Services is not running, the "Global\" and "Local\" prefixes are ignored. The remainder of the name can contain any character except the backslash character.

---

So, you might try the following mutex name to see if it corrects the issue:

'Global\MUTEX_LOCK_DATA_TEXT_FILE'

---

Regards,
Russell



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
monirAuthor Commented:
Thank you Mr. Russell
I used the name that you suggested 'Global\MUTEX_LOCK_DATA_TEXT_FILE'. When a user tries to connect to CGI using IE, he get "You are not authorized to view this page
You do not have permission to view this directory or page using the credentials you supplied......".
Buy inside the server IE prompts a dialog asking for user name/Passord, when I give it a valid information it wokrs fine, I mean CGI works with the small program and they are waiting each others. same effects if I used 'Local\MUTEX_LOCK_DATA_TEXT_FILE'

So how can solve this for my users?

Monir
0
 
monirAuthor Commented:
ok, 'Global\MUTEX_LOCK_DATA_TEXT_FILE' is good, so to solve the users issue, I wrote  a client program, I used TIdHTTP (Indy10) to connect to CGI so I can now give it the user name and password.

When I used 'MUTEX_LOCK_DATA_TEXT_FILE' inside CGI,  I successfully connected to CGI and passed the info using parameter string and get back the results.

But when  I used 'Global\MUTEX_LOCK_DATA_TEXT_FILE' inside CGI, I got exception, so my question now is how to use pass the user name & password to TIdHTTP?

 I notes that the event OnSelectAuthorization is called but (I did not write a handler for it), then an excetion is raised (EAccessViolation).

Thank you for your help

0
 
Russell LibbySoftware Engineer, Advisory Commented:

Is the call to CreateMutex actually raising the exception? (not likely), or is it the fact that the program logic has now changed, and thus the code is raising an exception further in?

Russell
0
 
monirAuthor Commented:
IdHTTP is raising the exception.
0
 
Russell LibbySoftware Engineer, Advisory Commented:

Sorry, wish I could help you with that one, but I was only attempting to assist with the Mutex problem.

Russell
0
 
monirAuthor Commented:
Mr. Russell, I must thank you for your help and give you points for your help, I am trying to get the latest version of Indy maybe this will solve the exception, or maybe I can play with the IIS security settings on server for my CGI prgram, or what if I switch user at the begining of CGI program to a user with admin rights instead of I_USER?

Thank you for your help.

Mnoir
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now