(file) Return to SystemWindows.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.32 //%2003////////////////////////////////////////////////////////////////////////
  2 mike  1.13 //
  3 karl  1.32 // Copyright (c) 2000, 2001, 2002  BMC Software, Hewlett-Packard Development
  4            // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
  5            // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
  6            // IBM Corp.; EMC Corporation, The Open Group.
  7 mike  1.13 //
  8            // Permission is hereby granted, free of charge, to any person obtaining a copy
  9 mike  1.14 // of this software and associated documentation files (the "Software"), to
 10            // deal in the Software without restriction, including without limitation the
 11            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 12 mike  1.13 // sell copies of the Software, and to permit persons to whom the Software is
 13            // furnished to do so, subject to the following conditions:
 14 kumpf 1.25 // 
 15 mike  1.14 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 16 mike  1.13 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 17            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 18 mike  1.14 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 19            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 20            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 21 mike  1.13 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 22            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23            //
 24            //==============================================================================
 25            //
 26            // Author: Mike Brasher (mbrasher@bmc.com)
 27            //
 28 mike  1.14 // Modified By: Sushma Fernandes (sushma_fernandes@hp.com)
 29            //
 30            //              Nag Boranna, Hewlett-Packard Company (nagaraja_boranna@hp.com)
 31 mike  1.13 //
 32 david 1.29 // Modified By: Dave Rosckes (rosckes@us.ibm.com)
 33            //
 34 mike  1.13 //%/////////////////////////////////////////////////////////////////////////////
 35            
 36            #include "System.h"
 37            
 38            PEGASUS_NAMESPACE_BEGIN
 39            
 40            #include <windows.h>
 41 tony  1.31 #ifndef _WINSOCKAPI_
 42            #include <winsock2.h>
 43            #endif
 44 mike  1.21 #include <fcntl.h>
 45 mike  1.13 #include <sys/types.h>
 46            #include <time.h>
 47            #include <sys/timeb.h>
 48            #include <io.h>
 49 tony  1.33 #include <conio.h>
 50 mike  1.13 #include <direct.h>
 51            #include <sys/stat.h>
 52            #include <sys/types.h>
 53 mday  1.19 #include <windows.h>
 54 kumpf 1.18 #include <process.h>
 55 mike  1.13 
 56            #define ACCESS_EXISTS 0
 57            #define ACCESS_WRITE 2
 58            #define ACCESS_READ 4
 59            #define ACCESS_READ_AND_WRITE 6
 60            
 61 tony  1.33 #define PW_BUFF_LEN 65
 62            
 63 mike  1.13 void System::getCurrentTime(Uint32& seconds, Uint32& milliseconds)
 64            {
 65                FILETIME ft;
 66                GetSystemTimeAsFileTime(&ft);
 67                ULARGE_INTEGER largeInt = { ft.dwLowDateTime, ft.dwHighDateTime };
 68                largeInt.QuadPart -= 0x19db1ded53e8000;
 69                seconds = long(largeInt.QuadPart / (10000 * 1000));
 70                milliseconds = long((largeInt.QuadPart % (10000 * 1000)) / 10);
 71 karl  1.23     // This is a real hack. Added the following line after timevalue was
 72                // corrected and this apparently wrong. ks 7 apri 2002
 73                milliseconds = milliseconds / 1000;
 74 mike  1.13 }
 75            
 76            String System::getCurrentASCIITime()
 77            {
 78                char tmpbuf[128];
 79 kumpf 1.28     _strdate( tmpbuf );
 80                String date = tmpbuf;
 81 mike  1.13     _strtime( tmpbuf );
 82 kumpf 1.28     date.append("-");
 83                date.append(tmpbuf);
 84                return date;
 85 mike  1.13 }
 86            
 87            void System::sleep(Uint32 seconds)
 88            {
 89                Sleep(seconds * 1000);
 90            }
 91            
 92            Boolean System::exists(const char* path)
 93            {
 94                return _access(path, ACCESS_EXISTS) == 0;
 95            }
 96            
 97            Boolean System::canRead(const char* path)
 98            {
 99                return _access(path, ACCESS_READ) == 0;
100            }
101            
102            Boolean System::canWrite(const char* path)
103            {
104                return _access(path, ACCESS_WRITE) == 0;
105            }
106 mike  1.13 
107            Boolean System::getCurrentDirectory(char* path, Uint32 size)
108            {
109                return GetCurrentDirectory(size, path) != 0;
110            }
111            
112            Boolean System::isDirectory(const char* path)
113            {
114                struct stat st;
115            
116                if (stat(path, &st) != 0)
117            	return false;
118            
119                return (st.st_mode & _S_IFDIR) != 0;
120            }
121            
122            Boolean System::changeDirectory(const char* path)
123            {
124                return chdir(path) == 0;
125            }
126            
127 mike  1.13 Boolean System::makeDirectory(const char* path)
128            {
129                return _mkdir(path) == 0;
130            }
131            
132            Boolean System::getFileSize(const char* path, Uint32& size)
133            {
134                struct stat st;
135            
136                if (stat(path, &st) != 0)
137            	return false;
138            
139                size = st.st_size;
140                return true;
141            }
142            
143            Boolean System::removeDirectory(const char* path)
144            {
145                return rmdir(path) == 0;	
146            }
147            
148 mike  1.13 Boolean System::removeFile(const char* path)
149            {
150                return unlink(path) == 0;	
151            }
152            
153            Boolean System::renameFile(const char* oldPath, const char* newPath)
154            {
155                return rename(oldPath, newPath) == 0;
156            }
157            
158            DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName)
159            {
160                return DynamicLibraryHandle(LoadLibrary(fileName));
161            }
162            
163 mike  1.14 void System::unloadDynamicLibrary(DynamicLibraryHandle libraryHandle)
164            {
165            	FreeLibrary(HINSTANCE(libraryHandle));
166            }
167            
168 mike  1.13 String System::dynamicLoadError(void) {
169            return String();
170            }
171            
172            DynamicSymbolHandle System::loadDynamicSymbol(
173                DynamicLibraryHandle libraryHandle,
174                const char* symbolName)
175            {
176                return DynamicSymbolHandle(GetProcAddress(
177            	(HINSTANCE)libraryHandle, symbolName));
178            }
179            
180            String System::getHostName()
181            {
182                static char hostname[64];
183            
184                if (!*hostname)
185                    gethostname(hostname, sizeof(hostname));
186            
187                return hostname;
188 kumpf 1.16 }
189            
190 kumpf 1.22 String System::getFullyQualifiedHostName ()
191            {
192                //
193                //  ATTN: Implement this method to return the fully qualified host name
194                //
195                return String::EMPTY;
196            }
197            
198            String System::getSystemCreationClassName ()
199            {
200                //
201                //  ATTN: Implement this method to return the system creation class name
202                //
203                return String::EMPTY;
204            }
205            
206 kumpf 1.16 Uint32 System::lookupPort(
207                const char * serviceName,
208                Uint32 defaultPort)
209            {
210                Uint32 localPort;
211            
212                struct servent *serv;
213            
214                //
215                // Get wbem-local port from /etc/services
216                //
217                if (  (serv = getservbyname(serviceName, TCP)) != NULL )
218                {
219                    localPort = serv->s_port;
220                }
221                else
222                {
223                    localPort = defaultPort;
224                }
225            
226                return localPort;
227 mike  1.13 }
228            
229 mike  1.14 String System::getPassword(const char* prompt)
230            {
231 tony  1.33   char password[PW_BUFF_LEN] = {0};
232              int num_chars = 0;
233              int ch;
234            
235              fputs(prompt, stderr);
236            
237              while ((ch = _getch()) != '\r' &&
238                     num_chars < PW_BUFF_LEN)
239                {
240                  // EOF
241                  if (ch == EOF)
242                    {
243                       fputs("[EOF]\n", stderr);
244                       return String::EMPTY;
245                    }
246                  // Backspace or Delete
247                  else if ((ch == '\b' || ch == 127) &&
248                           num_chars > 0) 
249                    {
250                      password[--num_chars] = '\0';
251                      fputs("\b \b", stderr);
252 tony  1.33         }
253                  // CTRL+C
254                  else if (ch == 3)
255                    {
256                      // _getch() does not catch CTRL+C
257                      fputs("^C\n", stderr);
258                      exit(-1);
259                    }
260                  // CTRL+Z
261                  else if (ch == 26)
262                    {
263                      fputs("^Z\n", stderr);
264                      return String::EMPTY;
265                    }
266                  // Esc
267                  else if (ch == 27)
268                    {
269                      fputc('\n', stderr);
270                      fputs(prompt, stderr);
271                      num_chars = 0;
272                    }
273 tony  1.33       // Function keys (0 or E0) are a guards for a Function key codes
274                  else if (ch == 0 || ch == 0xE0)
275                    {
276                      ch = (ch << 4) | _getch();
277                      // Handle DELETE, left arrow, keypad DEL, and keypad left arrow
278                      if ((ch == 0xE53 || ch == 0xE4B || ch == 0x053 || ch == 0x04b) &&
279                          num_chars > 0)
280                        {
281                          password[--num_chars] = '\0';
282                          fputs("\b \b", stderr);
283                        }
284                      else
285                        {
286                          fputc('\a', stderr);
287                        }
288                    }
289                  else if ((num_chars < sizeof(password) - 1) &&
290                           !iscntrl(((unsigned char)(ch))))
291                    {
292                      password[num_chars++] = ch;
293                      fputc('*', stderr);
294 tony  1.33         }
295                  else
296                    {
297                      fputc('\a', stderr);
298                    }
299                }
300 mike  1.14 
301 tony  1.33   fputc('\n', stderr);
302              password[num_chars] = '\0';
303            
304              return String(password);
305 mike  1.14 }
306            
307 kumpf 1.24 String System::getEffectiveUserName()
308 mike  1.14 {
309 tony  1.33   int retcode = 0;
310            
311              // UNLEN (256) is the limit, not including null
312              char pUserName[256+1] = {0};
313              DWORD nSize = sizeof(pUserName);
314 mike  1.14 
315 tony  1.33   retcode = GetUserName(pUserName, &nSize);
316              if (retcode == 0)
317                {
318                  // zero is failure
319                  return String();
320                }
321              
322              return String(pUserName);
323 mike  1.14 }
324            
325            String System::encryptPassword(const char* password, const char* salt)
326            {
327 tony  1.33   BYTE pbBuffer[PW_BUFF_LEN] = {0};
328              DWORD dwByteCount;
329              char pcSalt[3] = {0};
330            
331              strncpy(pcSalt, salt, 2);
332              dwByteCount = strlen(password);
333              memcpy(pbBuffer, password, dwByteCount);
334              for (DWORD i=0; (i<dwByteCount) || (i>=PW_BUFF_LEN); i++)
335                (i%2 == 0) ? pbBuffer[i] ^= pcSalt[1] : pbBuffer[i] ^= pcSalt[0];
336              
337              return String(pcSalt) + String((char *)pbBuffer);
338 mike  1.14 }
339            
340 kumpf 1.26 Boolean System::isSystemUser(const char* userName)
341 mike  1.14 {
342                //ATTN: Implement this method to verify if user is vaild on the local system
343                //      This is used in User Manager
344                return true;
345            }
346            
347 kumpf 1.17 Boolean System::isPrivilegedUser(const String userName)
348 mike  1.14 {
349                // ATTN: Implement this method to verify if user executing the current
350 kumpf 1.17     //       command is a priviliged user, when user name is not passed as
351                //       as argument. If user name is passed the function checks 
352                //       whether the given user is a priviliged user.
353                //       This is used in cimuser CLI and CIMOperationRequestAuthorizer
354 mike  1.14     return true;
355            }
356 kumpf 1.20 
357            String System::getPrivilegedUserName()
358            {
359                // ATTN-NB-03-20000304: Implement better way to get the privileged
360                // user on the system.
361            
362                return (String("Administrator"));
363            }
364 mike  1.14     
365 kumpf 1.15 Uint32 System::getPID()
366            {
367 kumpf 1.18     return _getpid();
368 mike  1.21 }
369            
370            Boolean System::truncateFile(
371                const char* path, 
372                size_t newSize)
373            {
374                int fd = open(path, O_RDWR);
375            
376                if (fd == -1)
377                    return false;
378            
379                if (chsize(fd, newSize) != 0)
380            	return false;
381            
382                close(fd);
383                return true;
384 kumpf 1.15 }
385            
386 tony  1.27 // Is absolute path?
387            Boolean System::is_absolute_path(const char *path)
388            {
389              char full[_MAX_PATH];
390 tony  1.30   char path_slash[_MAX_PATH];
391              char *p;
392            
393              strncpy(path_slash, path, _MAX_PATH);
394              path_slash[_MAX_PATH-1] = '\0';
395            
396              for(p = path_slash; p < path_slash + strlen(path_slash); p++)
397                if (*p == '/') *p = '\\';
398              
399              return (strcasecmp(_fullpath( full, path_slash, _MAX_PATH ), path_slash) == 0) ? true : false;
400 tony  1.27 }
401 david 1.29 
402            void System::openlog(const String ident)
403            {
404                return;
405            }
406            
407            void System::syslog(Uint32 severity, const char *data)
408            {
409                return;
410            }
411            
412            void System::closelog()
413            {
414                return;
415            }
416            
417            // System ID constants for Logger::put and Logger::trace
418            const String System::CIMSERVER = "cimserver";  // Server system ID
419 tony  1.27 
420 mike  1.13 PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2