예제 #1
0
PetscErrorCode  PetscOpenHistoryFile(const char filename[],FILE **fd)
{
    PetscErrorCode ierr;
    PetscMPIInt    rank,size;
    char           pfile[PETSC_MAX_PATH_LEN],pname[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],date[64];
    char           version[256];

    PetscFunctionBegin;
    ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
    CHKERRQ(ierr);
    if (!rank) {
        char        arch[10];
        int         err;

        ierr = PetscGetArchType(arch,10);
        CHKERRQ(ierr);
        ierr = PetscGetDate(date,64);
        CHKERRQ(ierr);
        ierr = PetscGetVersion(version,256);
        CHKERRQ(ierr);
        ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);
        CHKERRQ(ierr);
        if (filename) {
            ierr = PetscFixFilename(filename,fname);
            CHKERRQ(ierr);
        } else {
            ierr = PetscGetHomeDirectory(pfile,240);
            CHKERRQ(ierr);
            ierr = PetscStrcat(pfile,"/.petschistory");
            CHKERRQ(ierr);
            ierr = PetscFixFilename(pfile,fname);
            CHKERRQ(ierr);
        }

        *fd = fopen(fname,"a");
        if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file: %s",fname);

        ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
        CHKERRQ(ierr);
        ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"%s %s\n",version,date);
        CHKERRQ(ierr);
        ierr = PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);
        CHKERRQ(ierr);
        ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"%s on a %s, %d proc. with options:\n",pname,arch,size);
        CHKERRQ(ierr);
        ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
        CHKERRQ(ierr);

        err = fflush(*fd);
        if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
    }
    PetscFunctionReturn(0);
}
예제 #2
0
/*@C
    PetscSSLInitializeContext - Set up an SSL context suitable for initiating HTTPS requests.

    Output Parameter:
.   octx - the SSL_CTX to be passed to PetscHTTPSConnect

    Level: advanced

    If PETSc was ./configure -with-ssl-certificate requires the user have created a self-signed certificate with
$    saws/CA.pl  -newcert  (using the passphrase of password)
$    cat newkey.pem newcert.pem > sslclient.pem

    and put the resulting file in either the current directory (with the application) or in the home directory. This seems kind of
    silly but it was all I could figure out.

.seealso: PetscSSLDestroyContext(), PetscHTTPSConnect(), PetscHTTPSRequest()

@*/
PetscErrorCode PetscSSLInitializeContext(SSL_CTX **octx)
{
    SSL_CTX        *ctx;
#if defined(PETSC_USE_SSL_CERTIFICATE)
    char           keyfile[PETSC_MAX_PATH_LEN];
    PetscBool      exists;
    PetscErrorCode ierr;
#endif

    PetscFunctionBegin;
    if (!bio_err){
      SSL_library_init();
      SSL_load_error_strings();
      bio_err = BIO_new_fp(stderr,BIO_NOCLOSE);
    }

    /* Set up a SIGPIPE handler */
    signal(SIGPIPE,sigpipe_handle);

/* suggested at https://mta.openssl.org/pipermail/openssl-dev/2015-May/001449.html */
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
    ctx  = SSL_CTX_new(TLS_client_method());
#else
    ctx  = SSL_CTX_new(SSLv23_client_method());
#endif
    SSL_CTX_set_mode(ctx,SSL_MODE_AUTO_RETRY);

#if defined(PETSC_USE_SSL_CERTIFICATE)
    /* Locate keyfile */
    ierr = PetscStrcpy(keyfile,"sslclient.pem");CHKERRQ(ierr);
    ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr);
    if (!exists) {
      ierr = PetscGetHomeDirectory(keyfile,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
      ierr = PetscStrcat(keyfile,"/");CHKERRQ(ierr);
      ierr = PetscStrcat(keyfile,"sslclient.pem");CHKERRQ(ierr);
      ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr);
      if (!exists) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate sslclient.pem file in current directory or home directory");
    }

    /* Load our keys and certificates*/
    if (!(SSL_CTX_use_certificate_chain_file(ctx,keyfile))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot read certificate file");

    SSL_CTX_set_default_passwd_cb(ctx,password_cb);
    if (!(SSL_CTX_use_PrivateKey_file(ctx,keyfile,SSL_FILETYPE_PEM))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot read key file");
#endif

    *octx = ctx;
    PetscFunctionReturn(0);
}
예제 #3
0
/*
    Box can only return an authorization code to a Webserver, hence we need to start one up and wait for
    the authorization code to arrive from Box
*/
static PetscErrorCode PetscBoxStartWebServer_Private(void)
{
  PetscErrorCode      ierr;
  int                 optionsLen = 5;
  const char          *options[optionsLen];
  struct mg_callbacks callbacks;
  struct mg_context   *ctx;
  char                keyfile[PETSC_MAX_PATH_LEN];
  PetscBool           exists;

  PetscFunctionBegin;
  options[0] = "listening_ports";
  options[1] = "8081s";

  ierr = PetscStrcpy(keyfile,"sslclient.pem");CHKERRQ(ierr);
  ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr);
  if (!exists) {
    ierr = PetscGetHomeDirectory(keyfile,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
    ierr = PetscStrcat(keyfile,"/");CHKERRQ(ierr);
    ierr = PetscStrcat(keyfile,"sslclient.pem");CHKERRQ(ierr);
    ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr);
    if (!exists) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate sslclient.pem file in current directory or home directory");
  }

  options[2] = "ssl_certificate";
  options[3] = keyfile;
  options[4] = NULL;

  /* Prepare callbacks structure. We have only one callback, the rest are NULL. */
  ierr = PetscMemzero(&callbacks, sizeof(callbacks));CHKERRQ(ierr);
  callbacks.begin_request = PetscBoxWebServer_Private;
  ctx = mg_start(&callbacks, NULL, options);
  if (!ctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to start up webserver");
  while (!result) {};
  PetscFunctionReturn(0);
}
예제 #4
0
파일: fpath.c 프로젝트: 00liujj/petsc
/*@C
   PetscGetFullPath - Given a filename, returns the fully qualified file name.

   Not Collective

   Input Parameters:
+  path     - pathname to qualify
.  fullpath - pointer to buffer to hold full pathname
-  flen     - size of fullpath

   Level: developer

   Concepts: full path
   Concepts: path^full

.seealso: PetscGetRelativePath()
@*/
PetscErrorCode  PetscGetFullPath(const char path[],char fullpath[],size_t flen)
{
  PetscErrorCode ierr;
  size_t         ln;
  PetscBool      flg;

  PetscFunctionBegin;
  if (path[0] == '/') {
    ierr = PetscStrncmp("/tmp_mnt/",path,9,&flg);CHKERRQ(ierr);
    if (flg) {ierr = PetscStrncpy(fullpath,path + 8,flen);CHKERRQ(ierr);}
    else     {ierr = PetscStrncpy(fullpath,path,flen);CHKERRQ(ierr);}
    fullpath[flen-1] = 0;
    PetscFunctionReturn(0);
  }

  ierr = PetscStrncpy(fullpath,path,flen);CHKERRQ(ierr);
  fullpath[flen-1] = 0;
  /* Remove the various "special" forms (~username/ and ~/) */
  if (fullpath[0] == '~') {
    char tmppath[PETSC_MAX_PATH_LEN],*rest;
    if (fullpath[1] == '/') {
      ierr = PetscGetHomeDirectory(tmppath,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
      rest = fullpath + 2;
    } else {
#if defined(PETSC_HAVE_PWD_H)
      struct passwd  *pwde;
      char *p,*name;

      /* Find username */
      name = fullpath + 1;
      p    = name;
      while (*p && *p != '/') p++;
      *p   = 0;
      rest = p + 1;
      pwde = getpwnam(name);
      if (!pwde) PetscFunctionReturn(0);

      ierr = PetscStrcpy(tmppath,pwde->pw_dir);CHKERRQ(ierr);
#else
      PetscFunctionReturn(0);
#endif
    }
    ierr = PetscStrlen(tmppath,&ln);CHKERRQ(ierr);
    if (tmppath[ln-1] != '/') {ierr = PetscStrcat(tmppath+ln-1,"/");CHKERRQ(ierr);}
    ierr = PetscStrcat(tmppath,rest);CHKERRQ(ierr);
    ierr = PetscStrncpy(fullpath,tmppath,flen);CHKERRQ(ierr);
    fullpath[flen-1] = 0;
  } else {
    ierr = PetscGetWorkingDirectory(fullpath,flen);CHKERRQ(ierr);
    ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr);
    ierr = PetscStrncpy(fullpath+ln,"/",flen - ln);CHKERRQ(ierr);
    fullpath[flen-1] = 0;
    ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr);
    if (path[0] == '.' && path[1] == '/') {
      ierr = PetscStrncat(fullpath,path+2,flen - ln - 1);CHKERRQ(ierr);
    } else {
      ierr = PetscStrncat(fullpath,path,flen - ln - 1);CHKERRQ(ierr);
    }
    fullpath[flen-1] = 0;
  }

  /* Remove the automounter part of the path */
  ierr = PetscStrncmp(fullpath,"/tmp_mnt/",9,&flg);CHKERRQ(ierr);
  if (flg) {
    char tmppath[PETSC_MAX_PATH_LEN];
    ierr = PetscStrcpy(tmppath,fullpath + 8);CHKERRQ(ierr);
    ierr = PetscStrcpy(fullpath,tmppath);CHKERRQ(ierr);
  }
  /* We could try to handle things like the removal of .. etc */
  PetscFunctionReturn(0);
}