IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Lancer process user a partir d'un service system


Sujet :

C

  1. #1
    Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    151
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 151
    Points : 49
    Points
    49
    Par défaut Lancer process user a partir d'un service system
    Bonjour à toutes et à tous

    je cherche a lancer un process en tant qu'utilisateur local

    ce process est lancé à partir d'un service lancé lui en tant qu'utilisateur system

    voici mon prog

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    #define _WIN32_WINNT 0x0500
     
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <gtk/gtk.h>
     
    /* Real name of the service */
    char *ServiceName = "Service";
    char *ServiceDescription = "Description";
     
    /* Startup type */
    long ServiceStartFlag = SERVICE_AUTO_START;
     
    /* The service should start as soon as it is installed or not */
    long ServiceStartRightNow = TRUE;
     
    /*  Type of service */
    long ServiceTypeFlag = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
     
    /* --------------- Service datas */
    STARTUPINFO         si;
    PROCESS_INFORMATION pi;
    SC_HANDLE ServiceHandle;
    SC_HANDLE ServicesDatabase;
    OSVERSIONINFO OsVer;
    DWORD ServiceCurrentStatus;
    HANDLE ServiceEvent;
    HANDLE hServiceThread;
    SERVICE_DESCRIPTION ServiceDesc;
    SERVICE_STATUS_HANDLE ServiceStatus;
    SERVICE_TABLE_ENTRY ServiceTable[2];
    SERVICE_STATUS ServiceStatusTable;
    char FileName[MAX_PATH + 1];
    char *ErrStartMsg = "Can't initialize control dispatcher.";
    char *ErrServiceDBMsg = "Can't open services database.";
    char *ErrCreateServiceMsg = "Impossible de créer le service.";
    char *ErrOpenServiceMsg = "Impossible d'ouvrir le service.";
    char *ErrRemoveServiceMsg = "Impossible de désinstaller le service.";
    char *ServiceInstalledMsg = "Service installé.";
    char *ServiceRemovedMsg	= "Service désinstallé.";
     
    /* Note: the produced file will auto-install or remove itself when run.
    To start it go to the service manager of Windows NT/2000/XP. */
     
    /* --------------- Perform tasks right before service effective creation
    Out: 0 = Stop install process
         1 = Proceed with install
    ---------------
    ServicesDatabase variable is initialized
    --------------- */
    long ServiceStart(void) {
    	return(1);
    }
     
    /* --------------- Perform tasks right before service effective removal
    Out: 0 = Stop removal process
         1 = Proceed with removal
    ---------------
    ServicesDatabase variable is initialized
    ServiceHandle variable is initialized
    --------------- */
    long ServiceRemove(void) {
    	return(1);
    }
     
    /* --------------- Background Thread (infinite) of the service */
    DWORD __stdcall ServiceThread(LPVOID Param) {
     
                 STARTUPINFO         siStartupInfo;
                 PROCESS_INFORMATION piProcessInfo;
                 memset(&si, 0, sizeof(si));
                 memset(&pi, 0, sizeof(pi));
                 siStartupInfo.dwFlags = STARTF_USESTDHANDLES || STARTF_USESHOWWINDOW;
                 siStartupInfo.wShowWindow = SW_MAXIMIZE;      
                 siStartupInfo.cb = sizeof(si);
     
     
     
                 HANDLE hUserToken;
                 LogonUser("user", "domain", "pass", LOGON32_LOGON_SERVICE, LOGON32_PROVIDER_DEFAULT, &hUserToken);
     
                 char commande[2100]={0};
                 sprintf(commande, "C:\\test\\notepad.exe");
     
                 CreateProcessAsUser(hUserToken, commande, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    }
     
    /* --------------- Functions Declarations */
    void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv);
     
    /* --------------- Display an error and terminate process */
    void RaiseError(char *ErrorMsg) {
    	MessageBox(NULL, ErrorMsg, ServiceName, MB_OK | MB_ICONERROR);
    	return;
    }
     
    /* --------------- Display an informative message */
    void RaiseInformation(char *InfoMsg) {
    	MessageBox(NULL, InfoMsg, ServiceName, MB_OK | MB_ICONINFORMATION);
    	return;
    }
     
    /* --------------- Service entry point
    Must run 3 in ways with the same procedure:
    1. Install
    2. Start
    3. Remove */
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    	ServicesDatabase = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
    	if(!ServicesDatabase) {
    		RaiseError(ErrServiceDBMsg);
    		return(0);
    	}
    	/* Check if the service is in starting state */
    	ServiceHandle = OpenService(ServicesDatabase, ServiceName, SERVICE_ALL_ACCESS);
    	if(!ServiceHandle) {
    		/* Call user procedure */
    		if(ServiceStart()) goto InstallProceed;
    		CloseServiceHandle(ServicesDatabase);
    		return(0);
    	}
    	if(QueryServiceStatus(ServiceHandle, &ServiceStatusTable)) {
    		/* Feed service manager with our thread if starting state */
    		if(ServiceStatusTable.dwCurrentState == SERVICE_START_PENDING) {
    			CloseServiceHandle(ServiceHandle);
    			CloseServiceHandle(ServicesDatabase);
    			ServiceTable[0].lpServiceName = ServiceName;
    			ServiceTable[0].lpServiceProc = &ServiceMain;
    			if(!StartServiceCtrlDispatcher(&ServiceTable[0])) RaiseError(ErrStartMsg);
    			return(0);
    		}
    	}
    	CloseServiceHandle(ServiceHandle);
    InstallProceed:	
    	GetModuleFileName(0, FileName, MAX_PATH);
    	/* Try to install */
    	ServiceHandle = CreateService(ServicesDatabase, ServiceName, ServiceName, SERVICE_ALL_ACCESS, ServiceTypeFlag, ServiceStartFlag, SERVICE_ERROR_NORMAL, FileName, NULL, NULL, NULL, NULL, NULL);
    	if(ServiceHandle) {
    		OsVer.dwOSVersionInfoSize = sizeof(OsVer);
    		if(GetVersionEx(&OsVer) != 0) {
            		if(OsVer.dwMajorVersion >= 5) {
    				// Add a description if OS >= Win2k
    				if(OsVer.dwPlatformId == VER_PLATFORM_WIN32_NT) {
    					ServiceDesc.lpDescription = ServiceDescription;
    					ChangeServiceConfig2(ServiceHandle, SERVICE_CONFIG_DESCRIPTION, &ServiceDesc);
    				}
    			}
    		}
    		if(ServiceStartRightNow) StartService(ServiceHandle, 0, 0);
    		CloseServiceHandle(ServiceHandle);
    		CloseServiceHandle(ServicesDatabase);
    		RaiseInformation(ServiceInstalledMsg);
    		return(0);
    	}
    	if(GetLastError() != ERROR_SERVICE_EXISTS) {
    		CloseServiceHandle(ServicesDatabase);
    		RaiseError(ErrCreateServiceMsg);
    		return(0);
    	}
    	/* Perform removal */
    	ServiceHandle = OpenService(ServicesDatabase, ServiceName, SERVICE_ALL_ACCESS | DELETE);
    	if(!ServiceHandle) {
    		CloseServiceHandle(ServicesDatabase);
    		RaiseError(ErrOpenServiceMsg);
    		return(0);
    	}
    	QueryServiceStatus(ServiceHandle, &ServiceStatusTable);
    	if(ServiceStatusTable.dwCurrentState != SERVICE_STOPPED) {
    		ControlService(ServiceHandle, SERVICE_CONTROL_STOP, &ServiceStatusTable);
    		Sleep(500);
    	}
    	/* Call user procedure */
    	if(!ServiceRemove()) {
    		CloseServiceHandle(ServiceHandle);
    		CloseServiceHandle(ServicesDatabase);
    		return(0);
    	}
    	if(DeleteService(ServiceHandle)) {
    		CloseServiceHandle(ServiceHandle);
    		CloseServiceHandle(ServicesDatabase);
    		RaiseInformation(ServiceRemovedMsg);
    		return(0);
    	}
    	CloseServiceHandle(ServiceHandle);
    	CloseServiceHandle(ServicesDatabase);
    	RaiseError(ErrRemoveServiceMsg);
    	return(0);
    }
     
    /* --------------- Initialize service thread */
    long InitServiceThread(void) {
    	DWORD ThreadID;
    	hServiceThread = CreateThread(0, 0, &ServiceThread, 0, 0, &ThreadID);
    	if(hServiceThread) {
    		ServiceCurrentStatus |= 1;
    		return(1);
    	}
    	return(0);
    }
     
    /* --------------- Resume service */
    void ResumeService(void) {
    	ServiceCurrentStatus &= 0xfffffffd;
    	ResumeThread(hServiceThread);
    	return;
    }
     
    /* --------------- Pause service */
    void PauseService(void) {
    	ServiceCurrentStatus |= 2;
    	SuspendThread(hServiceThread);
    	return;
    }
     
    /* --------------- Stop service */
    void StopService(void) {
    	ServiceCurrentStatus &= 0xfffffffe;
    	SetEvent(ServiceEvent);
    	return;
    }
     
    /* --------------- Send message to system */
    long SendStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint) {
    	ServiceStatusTable.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    	ServiceStatusTable.dwCurrentState = dwCurrentState;
    	if(dwCurrentState == SERVICE_START_PENDING) ServiceStatusTable.dwControlsAccepted = 0;
    	else ServiceStatusTable.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN;
    	if(dwServiceSpecificExitCode == 0) ServiceStatusTable.dwWin32ExitCode = dwWin32ExitCode;
    	else ServiceStatusTable.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
    	ServiceStatusTable.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
    	ServiceStatusTable.dwCheckPoint = dwCheckPoint;
    	ServiceStatusTable.dwWaitHint = dwWaitHint;
    	SetServiceStatus(ServiceStatus, &ServiceStatusTable);
    	return(1);
    }
     
    /* --------------- Terminate service */
    long TerminateService(DWORD ProvidedErr) {
    	if(ServiceEvent) CloseHandle(ServiceEvent);
    	if(ServiceStatus) SendStatus(SERVICE_STOPPED, ProvidedErr, 0, 0, 0);
    	if(hServiceThread) CloseHandle(hServiceThread);
    	return(0);
    }
     
    /* --------------- Answer to system messages */
    void __stdcall CtrlHandler(DWORD CtrlCode) {
    	DWORD StatetoSend = 0;
    	switch(CtrlCode) {
    		case SERVICE_CONTROL_STOP:
    			SendStatus(SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000);
    			StopService();
    			StatetoSend = SERVICE_STOPPED;
    			break;
    		case SERVICE_CONTROL_PAUSE:
    			if(ServiceCurrentStatus == 1) {
    				SendStatus(SERVICE_PAUSE_PENDING, NO_ERROR, 0, 1, 1000);
    				PauseService();
    				StatetoSend = SERVICE_PAUSED;
    			}
    			break;
    		case SERVICE_CONTROL_CONTINUE:
    			if(ServiceCurrentStatus == 3) {
    				SendStatus(SERVICE_CONTINUE_PENDING, NO_ERROR, 0, 1, 1000);
    				ResumeService();
    				StatetoSend = SERVICE_RUNNING;
    			}
    			break;
    		case SERVICE_CONTROL_SHUTDOWN:
    			return;
    	}
    	SendStatus(StatetoSend, NO_ERROR, 0, 0, 0);
    }
     
    /* --------------- Service main handler */
    void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
    	ServiceStatus = RegisterServiceCtrlHandler(ServiceName, &CtrlHandler);
    	if(!ServiceStatus) {
    		TerminateService(GetLastError());
    		return;
    	}
    	SendStatus(SERVICE_START_PENDING, NO_ERROR, 0, 1, 5000);
    	ServiceEvent = CreateEvent(0, TRUE, FALSE, 0);
    	if(!ServiceEvent) {
    		TerminateService(GetLastError());
    		return;
    	}
    	SendStatus(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000);
    	SendStatus(SERVICE_START_PENDING, NO_ERROR, 0, 3, 5000);
    	if(!InitServiceThread()) {
    		TerminateService(GetLastError());
    		return;
    	}
    	SendStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
    	/* Endless loop */
    	WaitForSingleObject(ServiceEvent, INFINITE);
    	TerminateService(0);
    	return;
    }
    et voici la partie concernant le lancement du process

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
       STARTUPINFO         siStartupInfo;
                 PROCESS_INFORMATION piProcessInfo;
                 memset(&si, 0, sizeof(si));
                 memset(&pi, 0, sizeof(pi));
                 siStartupInfo.dwFlags = STARTF_USESTDHANDLES || STARTF_USESHOWWINDOW;
                 siStartupInfo.wShowWindow = SW_MAXIMIZE;      
                 siStartupInfo.cb = sizeof(si);
     
     
     
                 HANDLE hUserToken;
                 LogonUser("user", "domain", "pass", LOGON32_LOGON_SERVICE, LOGON32_PROVIDER_DEFAULT, &hUserToken);
     
                 char commande[2100]={0};
                 sprintf(commande, "C:\\test\\notepad.exe");
     
                 CreateProcessAsUser(hUserToken, commande, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    Le service s'installe correctement, il est présent dans la liste des processus (en tant que compte system) mais notepad ne se lance pas...

    Quelqu'un aurait t'il une idée, une piste qui pourrait m'aider?!?!

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 381
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 381
    Points : 41 582
    Points
    41 582
    Par défaut
    Sans doute un problème de Desktop et/ou Window Station...

  3. #3
    Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    151
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 151
    Points : 49
    Points
    49
    Par défaut
    C'est à dire???

  4. #4
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 381
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 381
    Points : 41 582
    Points
    41 582

  5. #5
    Rédacteur
    Avatar de Vincent Rogier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 373
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 373
    Points : 5 306
    Points
    5 306
    Par défaut
    sprintf(commande, "C:\\test\\notepad.exe");
    Tu as un notepad dans c:\test ??

  6. #6
    Rédacteur

    Avatar de ram-0000
    Homme Profil pro
    Consultant en sécurité
    Inscrit en
    Mai 2007
    Messages
    11 517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Consultant en sécurité
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mai 2007
    Messages : 11 517
    Points : 50 367
    Points
    50 367
    Par défaut
    Les code de retour de LogonUser() et de CreateProcessAsUser() ne sont pas testés dans ton code. Tu es sûr que tout se passe bien dedans ?

    Autre chose, la commande "C:\test\notepad.exe" fonctionne correctement ?

  7. #7
    Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    151
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 151
    Points : 49
    Points
    49
    Par défaut
    Merci de ton aide vicenzo, ta réponse m'a été grandement utile...

    [CODE]HANDLE hUserToken;
    LogonUser("user", "domain", "pass", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &hUserToken);[CODE]

    La fonction me retourne cette fois ci un 1 (au lieu d'un 0 avec "LOGON32_LOGON_SERVICE")

    Notepad est bien lancé, en tant que compte user et non system, mais la fenetre n'est pas affichée correctement, je vais me pencher sur la question.

    Merci pour votre aide, surtout toi vicenzo, j'espère pouvoir te rendre l'appareil un jour!!!!

Discussions similaires

  1. Lancer un exe à partir d'un service windows
    Par Eldaorn dans le forum Windows Forms
    Réponses: 1
    Dernier message: 02/04/2009, 13h37
  2. Lancer un programme à partir d'un service
    Par edam dans le forum Langage
    Réponses: 5
    Dernier message: 21/02/2008, 19h04
  3. Lancer un executable a partir d'un windows service
    Par barbar dans le forum VB.NET
    Réponses: 1
    Dernier message: 21/06/2007, 11h42
  4. lancer une page asp à partir du shell dos
    Par sqlnet dans le forum ASP
    Réponses: 3
    Dernier message: 19/11/2003, 15h20
  5. [reseaux] Comment creer un compte user à partir d'un formul avec perl
    Par oulai_evado dans le forum Programmation et administration système
    Réponses: 4
    Dernier message: 01/10/2002, 19h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo