Actual source code: globus.c

  1: #include <petscwebclient.h>
  2: #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  3: #pragma gcc diagnostic ignored "-Wdeprecated-declarations"

  5: /*
  6:     Encodes and decodes from MIME Base64
  7: */
  8: static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  9:                                 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
 10:                                 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
 11:                                 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
 12:                                 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
 13:                                 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
 14:                                 'w', 'x', 'y', 'z', '0', '1', '2', '3',
 15:                                 '4', '5', '6', '7', '8', '9', '+', '/'};

 17: static PetscErrorCode base64_encode(const unsigned char *data,unsigned char *encoded_data,size_t len)
 18: {
 19:   static size_t  mod_table[] = {0, 2, 1};
 20:   size_t         i,j;
 21:   size_t         input_length,output_length;

 23:   PetscStrlen((const char*)data,&input_length);
 24:   output_length = 4 * ((input_length + 2) / 3);

 27:   for (i = 0, j = 0; i < input_length;) {
 28:      uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
 29:      uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
 30:      uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
 31:      uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

 33:      encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
 34:      encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
 35:      encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
 36:      encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
 37:   }
 38:   encoded_data[j] = 0;
 39:   for (i = 0; i < mod_table[input_length % 3]; i++) encoded_data[output_length - 1 - i] = '=';
 40:   return 0;
 41: }

 43: PETSC_UNUSED static PetscErrorCode base64_decode(const unsigned char *data,unsigned char* decoded_data, size_t length)
 44: {
 45:   static char    decoding_table[257];
 46:   static int     decode_table_built = 0;
 47:   size_t         i,j;
 48:   size_t         input_length,output_length;

 50:   if (!decode_table_built) {
 51:     for (i = 0; i < 64; i++) decoding_table[(unsigned char) encoding_table[i]] = i;
 52:     decode_table_built = 1;
 53:   }

 55:   PetscStrlen((const char*)data,&input_length);

 58:   output_length = input_length / 4 * 3;
 59:   if (data[input_length - 1] == '=') (output_length)--;
 60:   if (data[input_length - 2] == '=') (output_length)--;

 63:   for (i = 0, j = 0; i < input_length;) {
 64:     uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]];
 65:     uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]];
 66:     uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]];
 67:     uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]];
 68:     uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);

 70:     if (j < output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
 71:     if (j < output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
 72:     if (j < output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
 73:   }
 74:   decoded_data[j] = 0;
 75:   return 0;
 76: }

 78: #if defined(PETSC_HAVE_UNISTD_H)
 79: #include <unistd.h>
 80: #endif

 82: /*@C
 83:      PetscGlobusAuthorize - Get an access token allowing PETSc applications to make Globus file transfer requests

 85:    Not collective, only the first process in MPI_Comm does anything

 87:    Input Parameters:
 88: +  comm - the MPI communicator
 89: -  tokensize - size of the token array

 91:    Output Parameters:
 92: .  access_token - can be used with PetscGlobusUpLoad() for 30 days

 94:    Notes:
 95:     This call requires stdout and stdin access from process 0 on the MPI communicator

 97:    You can run src/sys/webclient/tutorials/globusobtainaccesstoken to get an access token

 99:    Level: intermediate

101: .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscURLShorten(), PetscGlobusUpload()

103: @*/
104: PetscErrorCode PetscGlobusAuthorize(MPI_Comm comm,char access_token[],size_t tokensize)
105: {
106:   SSL_CTX        *ctx;
107:   SSL            *ssl;
108:   int            sock;
109:   char           buff[8*1024],*ptr,head[1024];
110:   PetscMPIInt    rank;
111:   size_t         len;
112:   PetscBool      found;

114:   MPI_Comm_rank(comm,&rank);
115:   if (rank == 0) {
117:     PetscPrintf(comm,"Enter globus username:");
118:     ptr  = fgets(buff, 1024, stdin);
120:     PetscStrlen(buff,&len);
121:     buff[len-1] = ':'; /* remove carriage return at end of line */

123:     PetscPrintf(comm,"Enter globus password:");
124:     ptr  = fgets(buff+len, 1024-len, stdin);
126:     PetscStrlen(buff,&len);
127:     buff[len-1] = '\0'; /* remove carriage return at end of line */
128:     PetscStrcpy(head,"Authorization: Basic ");
129:     base64_encode((const unsigned char*)buff,(unsigned char*)(head+21),sizeof(head)-21);
130:     PetscStrcat(head,"\r\n");

132:     PetscSSLInitializeContext(&ctx);
133:     PetscHTTPSConnect("nexus.api.globusonline.org",443,ctx,&sock,&ssl);
134:     PetscHTTPSRequest("GET","nexus.api.globusonline.org/goauth/token?grant_type=client_credentials",head,"application/x-www-form-urlencoded",NULL,ssl,buff,sizeof(buff));
135:     PetscSSLDestroyContext(ctx);
136:     close(sock);

138:     PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);

141:     PetscPrintf(comm,"Here is your Globus access token, save it in a save place, in the future you can run PETSc\n");
142:     PetscPrintf(comm,"programs with the option -globus_access_token %s\n",access_token);
143:     PetscPrintf(comm,"to access Globus automatically\n");
144:   }
145:   return 0;
146: }

148: /*@C
149:      PetscGlobusGetTransfers - Get a record of current transfers requested from Globus

151:    Not collective, only the first process in MPI_Comm does anything

153:    Input Parameters:
154: +  comm - the MPI communicator
155: .  access_token - Globus access token, if NULL will check in options database for -globus_access_token XXX otherwise
156:                   will call PetscGlobusAuthorize().
157: -  buffsize - size of the buffer

159:    Output Parameters:
160: .  buff - location to put Globus information

162:    Level: intermediate

164: .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscURLShorten(), PetscGlobusUpload(), PetscGlobusAuthorize()

166: @*/
167: PetscErrorCode PetscGlobusGetTransfers(MPI_Comm comm,const char access_token[],char buff[],size_t buffsize)
168: {
169:   SSL_CTX        *ctx;
170:   SSL            *ssl;
171:   int            sock;
172:   char           head[4096];
173:   PetscMPIInt    rank;

175:   MPI_Comm_rank(comm,&rank);
176:   if (rank == 0) {
177:     PetscStrcpy(head,"Authorization : Globus-Goauthtoken ");
178:     if (access_token) {
179:       PetscStrcat(head,access_token);
180:     } else {
181:       PetscBool set;
182:       char      accesstoken[4096];
183:       PetscOptionsGetString(NULL,NULL,"-globus_access_token",accesstoken,sizeof(accesstoken),&set);
185:       PetscStrcat(head,accesstoken);
186:     }
187:     PetscStrcat(head,"\r\n");

189:     PetscSSLInitializeContext(&ctx);
190:     PetscHTTPSConnect("transfer.api.globusonline.org",443,ctx,&sock,&ssl);
191:     PetscHTTPSRequest("GET","transfer.api.globusonline.org/v0.10/tasksummary",head,"application/json",NULL,ssl,buff,buffsize);
192:     PetscSSLDestroyContext(ctx);
193:     close(sock);
194:   }
195:   return 0;
196: }

198: /*@C
199:      PetscGlobusUpload - Loads a file to Globus

201:      Not collective, only the first process in the MPI_Comm uploads the file

203:   Input Parameters:
204: +   comm - MPI communicator
205: .   access_token - obtained with PetscGlobusAuthorize(), pass NULL to use -globus_access_token XXX from the PETSc database
206: -   filename - file to upload

208:   Options Database:
209: .  -globus_access_token XXX - the Globus token

211:    Level: intermediate

213: .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveRefresh(), PetscGlobusAuthorize()

215: @*/
216: PetscErrorCode PetscGlobusUpload(MPI_Comm comm,const char access_token[],const char filename[])
217: {
218:   SSL_CTX        *ctx;
219:   SSL            *ssl;
220:   int            sock;
221:   char           head[4096],buff[8*1024],body[4096],submission_id[4096];
222:   PetscMPIInt    rank;
223:   PetscBool      flg,found;

225:   MPI_Comm_rank(comm,&rank);
226:   if (rank == 0) {
227:     PetscTestFile(filename,'r',&flg);

230:     PetscStrcpy(head,"Authorization : Globus-Goauthtoken ");
231:     if (access_token) {
232:       PetscStrcat(head,access_token);
233:     } else {
234:       PetscBool set;
235:       char      accesstoken[4096];
236:       PetscOptionsGetString(NULL,NULL,"-globus_access_token",accesstoken,sizeof(accesstoken),&set);
238:       PetscStrcat(head,accesstoken);
239:     }
240:     PetscStrcat(head,"\r\n");

242:     /* Get Globus submission id */
243:     PetscSSLInitializeContext(&ctx);
244:     PetscHTTPSConnect("transfer.api.globusonline.org",443,ctx,&sock,&ssl);
245:     PetscHTTPSRequest("GET","transfer.api.globusonline.org/v0.10/submission_id",head,"application/json",NULL,ssl,buff,sizeof(buff));
246:     PetscSSLDestroyContext(ctx);
247:     close(sock);
248:     PetscPullJSONValue(buff,"value",submission_id,sizeof(submission_id),&found);

251:     /* build JSON body of transfer request */
252:     PetscStrcpy(body,"{");
253:     PetscPushJSONValue(body,"submission_id",submission_id,sizeof(body)));                 PetscCall(PetscStrcat(body,",");
254:     PetscPushJSONValue(body,"DATA_TYPE","transfer",sizeof(body)));                        PetscCall(PetscStrcat(body,",");
255:     PetscPushJSONValue(body,"sync_level","null",sizeof(body)));                           PetscCall(PetscStrcat(body,",");
256:     PetscPushJSONValue(body,"source_endpoint","barryfsmith#MacBookPro",sizeof(body)));    PetscCall(PetscStrcat(body,",");
257:     PetscPushJSONValue(body,"label","PETSc transfer label",sizeof(body)));                PetscCall(PetscStrcat(body,",");
258:     PetscPushJSONValue(body,"length","1",sizeof(body)));                                  PetscCall(PetscStrcat(body,",");
259:     PetscPushJSONValue(body,"destination_endpoint","mcs#home",sizeof(body)));             PetscCall(PetscStrcat(body,",");

261:     PetscStrcat(body,"\"DATA\": [ {");
262:     PetscPushJSONValue(body,"source_path","/~/FEM_GPU.pdf",sizeof(body)));                PetscCall(PetscStrcat(body,",");
263:     PetscPushJSONValue(body,"destination_path","/~/FEM_GPU.pdf",sizeof(body)));           PetscCall(PetscStrcat(body,",");
264:     PetscPushJSONValue(body,"verify_size","null",sizeof(body)));                          PetscCall(PetscStrcat(body,",");
265:     PetscPushJSONValue(body,"recursive","false",sizeof(body)));                           PetscCall(PetscStrcat(body,",");
266:     PetscPushJSONValue(body,"DATA_TYPE","transfer_item",sizeof(body));
267:     PetscStrcat(body,"} ] }");

269:     PetscSSLInitializeContext(&ctx);
270:     PetscHTTPSConnect("transfer.api.globusonline.org",443,ctx,&sock,&ssl);
271:     PetscHTTPSRequest("POST","transfer.api.globusonline.org/v0.10/transfer",head,"application/json",body,ssl,buff,sizeof(buff));
272:     PetscSSLDestroyContext(ctx);
273:     close(sock);
274:     PetscPullJSONValue(buff,"code",submission_id,sizeof(submission_id),&found);
276:     PetscStrcmp(submission_id,"Accepted",&found);
278:   }
279:   return 0;
280: }