setting time under NT from non Administrator Accounting

i need to set the sistem time without using NT Security (because my program will run on NT under non Admin login and it must have the possibility to set the system time!)

how can i do this?
and it's possible to do it?
(samples of source code are needed)

thanks

Cristian Bertoldi

i've posted the question under C++ section too. I will assign 500 points to the first correct answer.....
crisbertAsked:
Who is Participating?
 
jhanceConnect With a Mentor Commented:
The SetSystemTime() function is the only way to set the time on an NT system.  Unfortunately for your situation, this is a privileged function and, as the SDK says, the account calling it must have the SE_SYSTEMTIME_NAME (a.k.a "Change the system time" in the User Manager)

You have two options:

1) Assign this privilege to the user account(s) in question so that your program can request this privilege (using the AdjustTokenPrivileges call) when it needs to set the time.

2) Implement an NT service which runs under the Local System account to modify the system time on your program's behalf.

There are no shortcuts here since we are dealing with Windows NT security.  If things like this were able to be easily hacked, NT security would be useless.

I'll be happy to post some source code for either one of the above options if they make sense for your situation.  Of course, there are drawbacks to either one.

Giving the user(s) the privilege might not be possible since you may need to run/install this application on a system where you cannot give users this privilege.

Installing a server must be done using an administrative account and again, this might not be possible in your situation.  
0
 
crisbertAuthor Commented:
i can't give the admin login to the user, so i'll try to create a service if you send me a sample....

thanks
0
 
jhanceCommented:
I'll have that for you shortly.

0
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
jhanceCommented:
OK, here you go...

Please download:

ftp://www.jwhance.com/pub/TimeSetSvc.zip

This VC++ Workspace has 2 projects in it:

1) TimeSvc.  This is an NT service project that permits setting the time from a user application.  Compile it and then install the service from a command prompt:

TimeSvc -install

This will install but will not start the service.  Open the Services Control Panel and start the service. NOTE: You must be administrator to install a service.  Once this starts running you should hear an annoying beeping to let you know it's working.

2) TimeSetClient.  A Windows GUI application that when started, reads the current time.  If you change the time in the window and click OK, it will transact with the service and change the time.  If it "takes" you will hear a different (i.e. lower) beep.  It it fails (like from getting an invalid time input) the service will make a high-pitched beep.

This GUI app can be run by anyone.

WARNING:  Little attempt has been made to ensure graceful handling of errors and no consideration has been given to security.  In other words, please take time to understand what this thing is doing and how it might be abused by a hostile user.

Please let me know if you have any questions.
0
 
crisbertAuthor Commented:
OK, can i set the system time with this service from a visual basic application??? (the vb project get the date from an OS/390 Host and pass it to the service that.....)


(if this is not possible i'll try to get the Host's date from this service.)

thanks

For some strange reason (proxy or i don't know ...) i can't download your sample. May you send it at the address bertoldi.cristian@libero.it

Thanks
0
 
jhanceCommented:
I've gone ahead and sent it to you at your email address.

>OK, can i set the system time with
>this service from a visual basic
>application??? (the vb project get the
>date from an OS/390 Host and pass it
>to the service that.....)

Yes, of course.  This sample uses a Windows NT named pipe to communicate with the server which can be done from a VB application.  Of course, there are any number of other communication methods that could be used as well.

0
 
crisbertAuthor Commented:
this is the correct solution for my problem; i'll compile the sample that you gave to me cutting visual reference for make a DLL (for vb calling)

i can't still download the sample (can't access to ftp site....)

can you send it to bertoldi.cristian@libero.it or bertoldi.cristian@tiscalinet.it ?

thanks for your patience.

crisbert
0
 
crisbertAuthor Commented:
excuse me, if I've asked twice times to send me the files...

i've received all...
now i'm testing...

wait a little time and i'll give you your points.
0
 
crisbertAuthor Commented:
it worked!!!!

thanks for all.
0
 
jhanceCommented:
Let me know if you need any help talking to the service using VB as a client.  I'm not a VB guru but it's pretty straightforward.  The biggest trick is calling Windows API functions from the VB application.  Once you know how to do that, the process is virtually identical to the VC++ client program.

(I still can't figure out why you cannot connect to the FTP site....)
0
 
crisbertAuthor Commented:
if i log in with an userid of the USER NT's Group, the settime client app stop when it try to connect to the pipe... Why?

(I hear a double thick)

0
 
jhanceCommented:
>USER NT's Group

I don't understand what you mean by this.

Where is the client app stopping?  Run it in the debugger.

The only place where I see a potential problem is at the ConnectNamedPipe() call.  You might modify it to read:

      if(hPipe == INVALID_HANDLE_VALUE){
            AfxMessageBox("Error in CreateNamedPipe", MB_OK);
            return;
      }
      else{
            if(ConnectNamedPipe(hPipe, NULL)){
                  DWORD dwBytesWritten;
                  WriteFile(hPipe, (LPCVOID)&t, sizeof(SYSTEMTIME), &dwBytesWritten, NULL);
            }
            else{
                  AfxMessageBox("Error in ConnectNamedPipe", MB_OK);
            }
            CloseHandle(hPipe);
      }

This shouldn't fail unless the pipe has been closed by the service.
0
 
crisbertAuthor Commented:
excuse me, when i've installed the service i set to it an other acconnt!

now i've edited and set the sistem account and it work!

i'm testing the vb code, i'll send you a sample if you want...

Thanks
0
 
jhanceCommented:
>excuse me, when i've installed the service i set to it an other acconnt!

You probably want to leave it as the Local System account.  Unless there is a good reason for setting it to another user, this wastes system resources as NT must create an "NT Window Station" for every user on the system, including those running as services.  This consumes resources.
0
 
jhanceCommented:
Just in case anyone is interested in downloading this code, I've put it somewhere else where it should be more accessible:


http://www.jwhance.elosoft.com/TimeSetSvc.zip
0
 
crisbertAuthor Commented:
thanks jhance,
it work perfectly under VB6 (i've found some trouble setting the pointer to the security structure in CREATENAMEDPIPE, but nothing unresolvable....)

now i can set the sistem time from my VB & C programs directly....

if someone is intrested in the vb code please aksit to me at bertoldi.cristian@libero.it


0
All Courses

From novice to tech pro — start learning today.