Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / SMESHUtils / SMESH_MGLicenseKeyGen.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File      : SMESHUtils_MGLicenseKeyGen.cxx
20 // Created   : Sat Jul 31 18:54:16 2021
21 // Author    : Edward AGAPOV (OCC)
22
23 #include "SMESH_MGLicenseKeyGen.hxx"
24
25 #include "SMESH_Comment.hxx"
26 #include "SMESH_File.hxx"
27 #include "SMESH_TryCatch.hxx"
28
29 #include <Basics_DirUtils.hxx>
30 #include <Basics_Utils.hxx>
31
32 #include <cstdlib> // getenv, system
33
34 #include <boost/filesystem.hpp>
35 #include <boost/regex.hpp>
36 namespace boofs = boost::filesystem;
37
38 #ifdef WIN32
39
40 #  include <windows.h>
41 #  include <process.h>
42
43 #  define LibHandle HMODULE
44 #  define LoadLib( name ) LoadLibrary( name )
45 #  define GetProc GetProcAddress
46 #  define UnLoadLib( handle ) FreeLibrary( handle );
47
48 #else // WIN32
49
50 #  include <dlfcn.h>
51
52 #  define LibHandle void*
53 #  define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_LOCAL )
54 #  define GetProc dlsym
55 #  define UnLoadLib( handle ) dlclose( handle );
56
57 #endif // WIN32
58
59 // to retrieve description of exception caught by SMESH_TRY
60 #undef SMESH_CAUGHT
61 #define SMESH_CAUGHT error =
62
63 constexpr char MESHGEMS_OLD_STYLE[] = "MESHGEMS_OLD_STYLE";
64
65
66 namespace
67 {
68   static LibHandle theLibraryHandle = nullptr; //!< handle of a loaded library
69
70   const char* theEnvVar = "SALOME_MG_KEYGEN_LIB_PATH"; /* var specifies either full file name
71                                                           of libSalomeMeshGemsKeyGenerator or
72                                                           URL to download the library from */
73
74   const char* theTmpEnvVar = "SALOME_TMP_DIR"; // directory to download the library to
75
76   //-----------------------------------------------------------------------------------
77   /*!
78    * \brief Remove library file at destruction in case if it was downloaded from server
79    */
80   //-----------------------------------------------------------------------------------
81
82   struct LibraryFile
83   {
84     std::string _name; // full file name
85     bool        _isURL;
86
87     LibraryFile(): _isURL( false ) {}
88
89     ~LibraryFile()
90     {
91       if ( _isURL )
92       {
93         if ( theLibraryHandle )
94         {
95           UnLoadLib( theLibraryHandle );
96           theLibraryHandle = nullptr;
97         }
98
99         std::string tmpDir; // tmp dir that should not be removed
100         if ( const char* libPath = getenv( theTmpEnvVar ))
101         {
102           tmpDir = libPath;
103           while (( !tmpDir.empty() ) &&
104                  ( tmpDir.back() == '/' || tmpDir.back() == '\\' ))
105             tmpDir.pop_back();
106         }
107
108         while ( SMESH_File( _name ).remove() )
109         {
110           size_t length = _name.size();
111           _name = boofs::path( _name ).parent_path().string(); // goto parent directory
112           if ( _name.size() == length )
113             break; // no more parents
114
115           if ( _name == tmpDir )
116             break; // don't remove tmp dir
117
118           if ( !Kernel_Utils::IsEmptyDir( _name ))
119             break;
120         }
121       }
122     }
123   };
124
125
126   //================================================================================
127   /*!
128    * \brief Retrieve description of the last error
129    *  \param [out] error - return the description
130    *  \return bool - true if the description found
131    */
132   //================================================================================
133
134   bool getLastError( std::string& error )
135   {
136 #ifndef WIN32
137
138     if ( const char* text = dlerror() )
139     {
140       error = text;
141       return true;
142     }
143     return false;
144
145 #else
146
147     DWORD dw = GetLastError();
148     void* cstr;
149     DWORD msgLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
150                                  NULL,
151                                  dw,
152                                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
153                                  (LPTSTR) &cstr,
154                                  0,
155                                  NULL
156                                  );
157     if ( msgLen > 0 ) {
158 #  if defined( UNICODE )
159       error = Kernel_Utils::encode_s((wchar_t*)cstr);
160 #  else
161       error = (char*)cstr;
162 #  endif
163       LocalFree(cstr);
164     }
165
166     return (bool)msgLen;
167
168 #endif
169   }
170
171   //================================================================================
172   /*!
173    * \brief Adjust file extension according to the platform
174    */
175   //================================================================================
176
177   bool setExtension( std::string& fileName, std::string& error )
178   {
179     if ( fileName.empty() )
180     {
181       error = "Library file name is empty";
182       return false;
183     }
184 #if defined(WIN32)
185     std::string ext = ".dll";
186 #elif defined(__APPLE__)
187     std::string ext = ".dylib";
188 #else
189     std::string ext = ".so";
190 #endif
191
192     fileName = fileName.substr( 0, fileName.find_last_of('.')) + ext;
193     return true;
194   }
195
196   //================================================================================
197   /*!
198    * \brief Check if library file name looks like an URL
199    *  \param [in,out] libraryFile - holds file name and returns result in _isURL member field
200    *  \return bool - true if the file name looks like an URL
201    */
202   //================================================================================
203
204   bool isURL( LibraryFile & libraryFile )
205   {
206     {// round1
207       enum { SCHEME = 2, AUTHORITY = 4, PATH = 5 }; // sub-strings
208       boost::regex urlRegex ( R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)",
209                               boost::regex::extended );
210       boost::smatch matchResult;
211
212       libraryFile._isURL = false;
213       if ( boost::regex_match( libraryFile._name, matchResult, urlRegex ))
214         libraryFile._isURL = ( !matchResult.str( SCHEME    ).empty() &&
215                               !matchResult.str( AUTHORITY ).empty() &&
216                               !matchResult.str( PATH      ).empty() );
217     }
218     if(libraryFile._isURL)
219       return true;
220     {// round2
221       enum { HOST = 2, PORT = 3, PATH = 4 }; // sub-strings
222       boost::regex urlRegex ( R"(^(([^:\/?#]+):)?([^/]+)?(/[^#]*))",
223                               boost::regex::extended );
224       boost::smatch matchResult;
225
226       libraryFile._isURL = false;
227       if ( boost::regex_match( libraryFile._name, matchResult, urlRegex ))
228         libraryFile._isURL = ( !matchResult.str( HOST ).empty() &&
229                               !matchResult.str( PORT ).empty() &&
230                               !matchResult.str( PATH ).empty() );
231     }
232     return libraryFile._isURL;
233   }
234
235   //================================================================================
236   /*!
237    * \brief Download libraryFile._name URL to SALOME_TMP_DIR
238    *  \param [in,out] libraryFile - holds the URL and returns name of a downloaded file
239    *  \param [out] error - return error description
240    *  \return bool - is a success
241    */
242   //================================================================================
243
244   bool downloadLib( LibraryFile& libraryFile, std::string & error )
245   {
246     // check if can write into SALOME_TMP_DIR
247
248     std::string tmpDir = Kernel_Utils::GetTmpDirByEnv( theTmpEnvVar );
249     if ( tmpDir.empty() ||
250          !Kernel_Utils::IsExists( tmpDir ))
251     {
252       error = "Can't download " + libraryFile._name + " as SALOME_TMP_DIR is not correctly set";
253       return false;
254     }
255     if ( !Kernel_Utils::IsWritable( tmpDir ))
256     {
257       error = "Can't download " + libraryFile._name + " as '" + tmpDir + "' is not writable. "
258         "Check SALOME_TMP_DIR environment variable";
259       return false;
260     }
261
262     // Download
263
264     std::string url = libraryFile._name;
265
266 #ifdef WIN32
267
268     std::string outFile = tmpDir + "MeshGemsKeyGenerator.dll";
269
270     // use wget (== Invoke-WebRequest) PowerShell command available since Windows 7
271     std::string psCmd = "wget -Uri " + url + " -OutFile " + outFile;
272     std::string   cmd = "powershell.exe " + psCmd;
273
274 #else
275
276     std::string outFile = tmpDir + "libMeshGemsKeyGenerator.so";
277
278     std::string cmd = "wget " + url + " -O " + outFile;
279
280 #endif
281
282     if ( Kernel_Utils::IsExists( outFile )) // remove existing file
283     {
284       SMESH_File lib( outFile, /*open=*/false );
285       if ( !lib.remove() )
286       {
287         error = lib.error();
288         return false;
289       }
290     }
291
292     system( cmd.c_str() ); // download
293
294     SMESH_File resultFile( outFile, /*open=*/false );
295     bool ok = ( resultFile.exists() && resultFile.size() > 0 );
296
297     if ( ok )
298       libraryFile._name = outFile;
299     else
300       error = "Can't download file " + url;
301
302     return ok;
303   }
304
305   //================================================================================
306   /*!
307    * \brief Load libMeshGemsKeyGenerator.so
308    *  \param [out] error - return error description
309    *  \param [out] libraryFile - return library file name and _isURL flag
310    *  \return bool - is a success
311    */
312   //================================================================================
313
314   bool loadLibrary( std::string& error, LibraryFile& libraryFile )
315   {
316     if ( theLibraryHandle )
317       return true;
318
319     const char* libPath = getenv( theEnvVar );
320     if ( !libPath )
321     {
322       error = SMESH_Comment( "Environment variable ") <<  theEnvVar << " is not set";
323       return false;
324     }
325
326     libraryFile._name = libPath;
327     // if ( !setExtension( libraryFile._name, error )) // is it necessary?
328     //   return false;
329
330     if ( isURL( libraryFile ))
331     {
332       if ( !downloadLib( libraryFile, error ))
333       {
334         // try to fix extension
335         std::string url = libraryFile._name;
336         if ( !setExtension( libraryFile._name, error ))
337           return false;
338         if ( url == libraryFile._name )
339           return false; // extension not changed
340
341         if ( !downloadLib( libraryFile, error ))
342           return false;
343       }
344     }
345
346 #if defined( WIN32 ) && defined( UNICODE )
347     std::wstring encodePath = Kernel_Utils::utf8_decode_s( libraryFile._name );
348     const wchar_t*     path = encodePath.c_str();
349 #else
350     const char*        path = libraryFile._name.c_str();
351 #endif
352
353     theLibraryHandle = LoadLib( path );
354     if ( !theLibraryHandle )
355     {
356       if ( ! getLastError( error ))
357         error = "Can't load library '" + libraryFile._name + "'";
358     }
359
360     return theLibraryHandle;
361   }
362
363 } // anonymous namespace
364
365
366 namespace SMESHUtils_MGLicenseKeyGen // API implementation
367 {
368   //================================================================================
369   /*!
370    * \brief Sign a CAD
371    *  \param [in] meshgems_cad - pointer to a MG CAD object (meshgems_cad_t)
372    *  \param [out] error - return error description
373    *  \return bool - is a success
374    */
375   //================================================================================
376
377   bool SignCAD_After( void* meshgems_cad, std::string& error )
378   {
379     LibraryFile libraryFile;
380     if ( !loadLibrary( error, libraryFile ))
381       return false;
382
383     bool ok = false;
384     typedef bool (*SignFun)(void* );
385     SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignCAD" );
386     if ( !signFun )
387     {
388       if ( ! getLastError( error ))
389         error = SMESH_Comment( "Can't find symbol 'SignCAD' in '") << getenv( theEnvVar ) << "'";
390     }
391     else
392     {
393       SMESH_TRY;
394
395       ok = signFun( meshgems_cad );
396
397       SMESH_CATCH( SMESH::returnError );
398
399       if ( !error.empty() )
400         ok = false;
401       else if ( !ok )
402         error = "SignCAD() failed (located in '" + libraryFile._name + "')";
403     }
404     return ok;
405   }
406   
407   bool SignCAD( void* meshgems_cad, std::string& error )
408   {
409     const char *meshGemsOldStyleEnvVar( getenv( MESHGEMS_OLD_STYLE ) );
410     if ( !meshGemsOldStyleEnvVar || strlen(meshGemsOldStyleEnvVar) == 0 )
411       return SignCAD_After(meshgems_cad, error);
412     else
413       return true;
414   }
415
416   //================================================================================
417   /*!
418    * \brief Sign a mesh
419    *  \param [in] meshgems_mesh - pointer to a MG mesh (meshgems_mesh_t)
420    *  \param [out] error - return error description
421    *  \return bool - is a success
422    */
423   //================================================================================
424
425   bool SignMesh_After( void* meshgems_mesh, std::string& error )
426   {
427     LibraryFile libraryFile;
428     if ( !loadLibrary( error, libraryFile ))
429       return false;
430
431     bool ok = false;
432     typedef bool (*SignFun)(void* );
433     SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignMesh" );
434     if ( !signFun )
435     {
436       if ( ! getLastError( error ))
437         error = SMESH_Comment( "Can't find symbol 'SignMesh' in '") << getenv( theEnvVar ) << "'";
438     }
439     else
440     {
441       SMESH_TRY;
442
443       ok = signFun( meshgems_mesh );
444
445       SMESH_CATCH( SMESH::returnError );
446
447       if ( !error.empty() )
448         ok = false;
449       else if ( !ok )
450         error = "SignMesh() failed (located in '" + libraryFile._name + "')";
451     }
452     return ok;
453   }
454   
455   bool SignMesh( void* meshgems_mesh, std::string& error )
456   {
457     const char *meshGemsOldStyleEnvVar( getenv( MESHGEMS_OLD_STYLE ) );
458     if ( !meshGemsOldStyleEnvVar || strlen(meshGemsOldStyleEnvVar) == 0 )
459       return SignMesh_After(meshgems_mesh, error);
460     else
461       return true;
462   }
463
464   //================================================================================
465   /*!
466    * \brief Return a license key to pass as argument to a MG mesher executable
467    *  \param [in] gmfFile - path to an input mesh file
468    *  \param [in] nb* - nb of entities in the input mesh
469    *  \param [out] error - return error description
470    *  \return std::string - the key
471    */
472   //================================================================================
473
474   std::string GetKey_After(const std::string& gmfFile,
475                             int                nbVertex,
476                             int                nbEdge,
477                             int                nbFace,
478                             int                nbVol,
479                             std::string&       error)
480   {
481     std::string key;
482     LibraryFile libraryFile;
483     if ( !loadLibrary( error, libraryFile ))
484       return key;
485
486     typedef std::string (*GetKeyFun)(std::string const &, int, int, int, int );
487     GetKeyFun keyFun = (GetKeyFun) GetProc( theLibraryHandle, "GetKey" );
488     if ( !keyFun )
489     {
490       if ( ! getLastError( error ))
491         error = SMESH_Comment( "Can't find symbol 'GetKey' in '") << getenv( theEnvVar ) << "'";
492     }
493     else
494     {
495       key = keyFun( gmfFile, nbVertex, nbEdge, nbFace, nbVol );
496     }
497     if ( key.empty() )
498       error = "GetKey() failed (located in '" + libraryFile._name + "')";
499
500     return key;
501   }
502
503   std::string GetKey(const std::string& gmfFile,
504                     int                nbVertex,
505                     int                nbEdge,
506                     int                nbFace,
507                     int                nbVol,
508                     std::string&       error)
509   {
510     const char *meshGemsOldStyleEnvVar( getenv( MESHGEMS_OLD_STYLE ) );
511     if ( !meshGemsOldStyleEnvVar || strlen(meshGemsOldStyleEnvVar) == 0 )
512       return GetKey_After(gmfFile,nbVertex,nbEdge,nbFace,nbVol,error);
513     else
514       return std::string("0");
515   }
516
517   //================================================================================
518   /*!
519    * \brief Return false if libMeshGemsKeyGenerator.so is not functional
520    *  \param [out] error - return error description
521    *  \return bool - is a success
522    */
523   //================================================================================
524
525   bool CheckKeyGenLibrary( std::string& error )
526   {
527     return !GetKey("",4,0,2,0,error ).empty();
528   }
529
530   //================================================================================
531   /*!
532    * \brief Return KeyGenerator library name
533    */
534   //================================================================================
535
536   std::string GetLibraryName()
537   {
538     std::string libName, error;
539     if ( const char* libPath = getenv( theEnvVar ))
540     {
541       libName = Kernel_Utils::GetBaseName( libPath );
542     }
543     else
544     {
545       libName = "libSalomeMeshGemsKeyGenerator";
546     }
547     setExtension( libName, error );
548     return libName;
549   }
550 }