Ejemplo n.º 1
0
void
unregister_proxy(glite_renewal_core_context ctx, edg_wlpr_Request *request, edg_wlpr_Response *response)
{
   proxy_record record;
   int ret, i, index;
   FILE *meta_fd = NULL;
   char *basename = NULL;
   char *p;
   struct stat stat_buf;

   memset(&record, 0, sizeof(record));

   if (request->jobid == NULL) {
      glite_renewal_core_set_err(ctx, "Request doesn't specify jobid");
      ret = EINVAL;
      goto end;
   }

   edg_wlpr_Log(ctx, LOG_DEBUG, "Unregistrating proxy of job %s", 
		request->jobid);

   if (request->proxy_filename == NULL) {
      ret = find_proxyname(ctx, request->jobid, &request->proxy_filename);
      if (ret)
	 goto end;
   }

   ret = get_base_filename(ctx, request->proxy_filename, &basename);
   if (ret) {
      goto end;
   }

   if (strncmp(request->proxy_filename, basename, strlen(basename) != 0)) {
      edg_wlpr_Log(ctx, LOG_DEBUG, "Requested proxy %s is not from repository",
	           request->proxy_filename);
      ret = EDG_WLPR_PROXY_NOT_REGISTERED;
      goto end;
   }

   p = strrchr(request->proxy_filename, '.');
   if (p == NULL) {
      edg_wlpr_Log(ctx, LOG_DEBUG, "Requested proxy %s is not from repository",
	           request->proxy_filename);
      ret = EDG_WLPR_PROXY_NOT_REGISTERED;
      goto end;
   }

   ret = edg_wlpr_DecodeInt(p+1, &record.suffix);
   if (ret) {
      edg_wlpr_Log(ctx, LOG_DEBUG, "Requested proxy %s is not from repository",
	          request->proxy_filename);
      ret = EDG_WLPR_PROXY_NOT_REGISTERED;
      goto end;
   }

   ret = open_metafile(ctx, basename, &meta_fd);
   if (ret) {
      /* fill in error response */
      return;
   }

   ret = get_record(ctx, meta_fd, basename, &record);
   if (ret)
      goto end;

   ret = EDG_WLPR_PROXY_NOT_REGISTERED;
   for (i = 0; i < record.jobids.len; i++)
      if (strcmp(request->jobid, record.jobids.val[i]) == 0) {
	 ret = 0;
	 break;
      }
   if (ret) {
      edg_wlpr_Log(ctx, LOG_DEBUG, "Requested proxy %s is not registered",
	           request->proxy_filename);
      goto end;
   }

   /* remove jobid from the list */
   index = i;
   free(record.jobids.val[i]);
   record.jobids.len--;
   for (i = index; i < record.jobids.len; i++)
      record.jobids.val[i] = record.jobids.val[i+1];

   if (record.jobids.len == 0) {
      record.unique = 0;
      record.voms_exts = 0;
      record.end_time = 0;
      record.next_renewal = 0;
      if (record.fqans) {
	  free(record.fqans);
	  record.fqans = NULL;
      }
   }

   ret = stat(request->proxy_filename, &stat_buf);
   if (ret) {
      edg_wlpr_Log(ctx, LOG_DEBUG, "Cannot stat file %s: (%s)",
	           request->proxy_filename, strerror(errno));
      ret = errno;
      goto end;
   }

   ret = store_record(ctx, basename, &record);
   if (ret)
      goto end;

   if (record.jobids.len == 0)
      unlink(request->proxy_filename);

end:
   if (meta_fd) {
      fclose(meta_fd);
   }
   if (basename)
      free(basename);

   if (ret == 0) {
      ret = filename_to_response(ctx, request->proxy_filename, response);
      edg_wlpr_Log(ctx, LOG_NOTICE,
                   "Proxy %s of job %s has been unregistered",
		   request->proxy_filename, request->jobid);
   }
   else
      edg_wlpr_Log(ctx, LOG_ERR,
         "Failed to unregister proxy %s of job %s: %s",
         (request->proxy_filename) ? request->proxy_filename : "'(null)'",
	 (request->jobid) ? request->jobid : "'(null)'",
	 glite_renewal_core_get_err(ctx));

   record_to_response(ctx, ret, &record, response);
   free_record(ctx, &record);
}
Ejemplo n.º 2
0
void
register_proxy(glite_renewal_core_context ctx, edg_wlpr_Request *request, edg_wlpr_Response *response)
{
   proxy_record record;
   int ret;
   FILE *meta_fd = NULL;
   int last_suffix;
   char *basename = NULL;
   char filename[FILENAME_MAX];
   time_t end_time_x509, end_time_voms;

   assert(request != NULL);
   assert(response != NULL);

   memset(&record, 0, sizeof(record));
   memset(response, 0, sizeof(*response));

   if (request->proxy_filename == NULL || request->jobid == NULL) {
      edg_wlpr_Log(ctx, LOG_ERR, "Registration request doesn't contain registration information");
      return; /*  EINVAL; */
   }

   edg_wlpr_Log(ctx, LOG_DEBUG,
		"Registering proxy from %s belonging to job %s",
		request->proxy_filename, request->jobid);

   ret = get_base_filename(ctx, request->proxy_filename, &basename);
   if (ret)
      goto end;

   ret = open_metafile(ctx, basename, &meta_fd);
   if (ret)
      goto end;

   if (voms_enabled) {
       record.fqans = get_voms_fqans(ctx, request->proxy_filename);
       record.voms_exts = (record.fqans != NULL);
   }

   /* Find first free record */
   record.suffix = -1;
   record.myproxy_server = strdup(request->myproxy_server);
   ret = get_record_ext(ctx, meta_fd, basename, &record, &last_suffix);
   fclose(meta_fd); meta_fd = NULL;
   if (ret && ret != EDG_WLPR_ERROR_PROTO_PARSE_NOT_FOUND)
      goto end;

   if (ret == EDG_WLPR_ERROR_PROTO_PARSE_NOT_FOUND || record.jobids.len == 0 || request->unique) {
      /* create a new proxy file in the repository */
      int suffix;

      suffix = (record.jobids.len == 0 && record.suffix >= 0) ? 
	         record.suffix : last_suffix + 1;
      snprintf(filename, sizeof(filename), "%s.%d", basename, suffix);
      ret = copy_file(ctx, request->proxy_filename, filename);
      if (ret)
	 goto end;
      ret = set_renewal_times(ctx, filename, &record);
      if (ret)
	 goto end;
      record.suffix = suffix;
      ret = realloc_prd_list(ctx, &record.jobids);
      if (ret)
	 goto end;
      record.jobids.val[record.jobids.len - 1] = strdup(request->jobid);
      record.unique = request->unique;
      edg_wlpr_Log(ctx, LOG_DEBUG, "Created a new proxy file in repository (%s)",
	           filename);
   } else {
      ret = get_times(ctx, request->proxy_filename, &end_time_x509, &end_time_voms);
      if (ret)
	  goto end;

      snprintf(filename, sizeof(filename), "%s.%d", basename, record.suffix);
      if (record.end_time < end_time_x509 &&
	      (end_time_voms == 0 || record.next_renewal < end_time_voms)) {
	  ret = copy_file(ctx, request->proxy_filename, filename);
	  if (ret)
	      goto end;
	  edg_wlpr_Log(ctx, LOG_DEBUG, "Proxy %s of job %s has replaced the registered one.",
		       request->proxy_filename, request->jobid);
	  schedule_renewal(ctx, end_time_x509, end_time_voms, &record);
      }

      ret = realloc_prd_list(ctx, &record.jobids);
      if (ret)
	 goto end;
      record.jobids.val[record.jobids.len - 1] = strdup(request->jobid);
      edg_wlpr_Log(ctx, LOG_DEBUG, "Incremented counter on %s", filename);
   }

   ret = store_record(ctx, basename, &record);

end:
   if (meta_fd) {
      fclose(meta_fd);
   }

   if (basename)
      free(basename);

   if (ret == 0) {
      ret = filename_to_response(ctx, filename, response);
      edg_wlpr_Log(ctx, LOG_NOTICE,
                   "Proxy %s of job %s has been registered as %s",
		   request->proxy_filename, request->jobid, filename);
   } else
      edg_wlpr_Log(ctx, LOG_ERR, "Failed to register proxy %s: %s",
                   request->proxy_filename,
                   glite_renewal_core_get_err(ctx));

   record_to_response(ctx, ret, &record, response);
   free_record(ctx, &record);
}
Ejemplo n.º 3
-11
int main(int argc, char **argv)
{
    int c;

    char *input_file_name = NULL;
    char *output_file_name = NULL;
    char *output_basename = NULL;
    char *error_file_name = NULL;
    FILE *input_file = NULL;
    FILE *output_file = NULL;
    FILE *error_file = NULL;
    gregorio_file_format input_format = FORMAT_UNSET;
    gregorio_file_format output_format = FORMAT_UNSET;
    gregorio_verbosity verb_mode = 0;
    bool point_and_click = false;
    char *point_and_click_filename = NULL;
    bool debug = false;
    bool must_print_short_usage = false;
    int option_index = 0;
    static struct option long_options[] = {
        {"output-file", 1, 0, 'o'},
        {"stdout", 0, 0, 'S'},
        {"output-format", 1, 0, 'F'},
        {"messages-file", 1, 0, 'l'},
        {"input-format", 1, 0, 'f'},
        {"stdin", 0, 0, 's'},
        {"help", 0, 0, 'h'},
        {"version", 0, 0, 'V'},
        {"licence", 0, 0, 'L'},
        {"verbose", 0, 0, 'v'},
        {"all-warnings", 0, 0, 'W'},
        {"point-and-click", 0, 0, 'p'},
        {"debug", 0, 0, 'd'},
    };
    gregorio_score *score = NULL;

    gregorio_support_init("gregorio", argv[0]);

    if (argc == 1) {
        fprintf(stderr, "%s: missing file operand.\n", argv[0]);
        print_short_usage(argv[0]);
        gregorio_exit(0);
    }
    setlocale(LC_CTYPE, "C");

    while (1) {
        c = getopt_long(argc, argv, "o:SF:l:f:shOLVvWpd",
                        long_options, &option_index);
        if (c == -1)
            break;
        switch (c) {
        case 'o':
            if (output_file_name) {
                fprintf(stderr,
                        "warning: several output files declared, %s taken\n",
                        output_file_name);
                must_print_short_usage = true;
                break;
            }
            if (output_file) {  /* means that stdout is defined */
                fprintf(stderr,
                        "warning: can't write to file and stdout, writing on stdout\n");
                must_print_short_usage = true;
                break;
            }
            output_file_name = optarg;
            break;
        case 'S':
            if (output_file_name) {
                fprintf(stderr,
                        "warning: can't write to file and stdout, writing on %s\n",
                        output_file_name);
                must_print_short_usage = true;
                break;
            }
            if (output_file) {  /* means that stdout is defined */
                fprintf(stderr, "warning: option used several times: %c\n", c);
                must_print_short_usage = true;
                break;
            }
            output_file = stdout;
            break;
        case 'F':
            if (output_format) {
                fprintf(stderr,
                        "warning: several output formats declared, first taken\n");
                must_print_short_usage = true;
                break;
            }
            if (!strcmp(optarg, GABC_STR)) {
                output_format = GABC;
                break;
            }
            if (!strcmp(optarg, GTEX_STR)) {
                output_format = GTEX;
                break;
            }
            if (!strcmp(optarg, DUMP_STR)) {
                output_format = DUMP;
                break;
            } else {
                fprintf(stderr, "error: unknown output format: %s\n", optarg);
                print_short_usage(argv[0]);
                gregorio_exit(1);
            }
            break;
        case 'l':
            if (error_file_name) {
                fprintf(stderr,
                        "warning: several error files declared, %s taken\n",
                        error_file_name);
                must_print_short_usage = true;
                break;
            }
            error_file_name = optarg;
            break;
        case 'f':
            if (input_format) {
                gregorio_set_error_out(error_file);
                fprintf(stderr,
                        "warning: several output formats declared, first taken\n");
                must_print_short_usage = true;
                break;
            }
            if (!strcmp(optarg, GABC_STR)) {
                input_format = GABC;
                break;
            } else {
                fprintf(stderr, "error: unknown input format: %s\n", optarg);
                print_short_usage(argv[0]);
                gregorio_exit(1);
            }
            break;
        case 's':
            /* input_file_name will be null here because of the way
             * we use getopt_long */
            assert(!input_file_name);
            if (input_file) { /* means that stdin is defined */
                fprintf(stderr, "warning: option used several times: %c\n", c);
                must_print_short_usage = true;
                break;
            }
            input_file = stdin;
            break;
        case 'h':
            print_usage(argv[0]);
            gregorio_exit(0);
            break;
        case 'V':
            gregorio_print_version(copyright);
            gregorio_exit(0);
            break;
        case 'v':
            if (verb_mode && verb_mode != VERBOSITY_WARNING) {
                fprintf(stderr, "warning: verbose option passed several times\n");
                must_print_short_usage = true;
                break;
            }
            verb_mode = VERBOSITY_INFO;
            break;
        case 'W':
            if (verb_mode == VERBOSITY_WARNING) {
                fprintf(stderr,
                        "warning: all-warnings option passed several times\n");
                must_print_short_usage = true;
                break;
            }
            if (verb_mode != VERBOSITY_INFO) {
                verb_mode = VERBOSITY_WARNING;
            }
            break;
        case 'L':
            print_licence();
            gregorio_exit(0);
            break;
        case 'p':
            if (point_and_click) {
                fprintf(stderr,
                        "warning: point-and-click option passed several times\n");
                must_print_short_usage = true;
                break;
            }
            point_and_click = true;
            break;
        case 'd':
            if (debug) {
                fprintf(stderr,
                        "warning: debug option passed several times\n");
                must_print_short_usage = true;
                break;
            }
            debug = true;
            break;
        case '?':
            must_print_short_usage = true;
            break;
        default:
            /* not reachable unless there's a programming error */
            /* LCOV_EXCL_START */
            gregorio_fail2(main, "unknown option: %c", c);
            print_short_usage(argv[0]);
            gregorio_exit(1);
            break;
            /* LCOV_EXCL_STOP */
        }
    } /* end of while */
    if (optind == argc) {
        if (!input_file) { /* input not undefined (could be stdin) */
            fprintf(stderr, "%s: missing file operand.\n", argv[0]);
            print_short_usage(argv[0]);
            gregorio_exit(1);
        }
    } else {
        input_file_name = argv[optind++];
        output_basename = get_base_filename(input_file_name);
        if (input_file) {
            fprintf(stderr,
                    "warning: can't read from both stdin and a file, reading from %s\n",
                    input_file_name);
            input_file = NULL;
            must_print_short_usage = true;
        }
    }
    if (optind < argc) {
        must_print_short_usage = true;
        fprintf(stderr, "ignored arguments:");
        for (; optind < argc; ++optind) {
            fprintf(stderr, " %s", argv[optind]);
        }
        fprintf(stderr, "\n");
    }

    if (must_print_short_usage) {
        print_short_usage(argv[0]);
        fprintf(stderr, "Proceeding anyway...\n");
    }

    gregorio_set_debug_messages(debug);

    if (!input_format) {
        input_format = DEFAULT_INPUT_FORMAT;
    }

    if (!output_format) {
        output_format = DEFAULT_OUTPUT_FORMAT;
    }

    /* then we act... */

    if (!output_file_name && !output_file) {
        if (!output_basename) {
            output_file = stdout;
        } else {
            switch (output_format) {
            case GABC:
                output_file_name =
                    get_output_filename(output_basename, "gabc");
                break;
            case GTEX:
                output_file_name =
                    get_output_filename(output_basename, "gtex");
                break;
            case DUMP:
                output_file_name =
                    get_output_filename(output_basename, "dump");
                break;
            default:
                /* not reachable unless there's a programming error */
                /* LCOV_EXCL_START */
                fprintf(stderr, "error: unsupported format");
                print_short_usage(argv[0]);
                gregorio_exit(1);
                /* LCOV_EXCL_STOP */
            }
        }
    }

    if (output_basename) {
        free(output_basename);
    }

    if (!output_file) {
        if (!input_file) {
            check_input_clobber(input_file_name, output_file_name);
        }
        gregorio_check_file_access(write, output_file_name, ERROR,
                gregorio_exit(1));
        output_file = fopen(output_file_name, "wb");
        if (!output_file) {
            fprintf(stderr, "error: can't write in file %s", output_file_name);
            gregorio_exit(1);
        }
    }

    /* we always have input_file or input_file_name */
    if (input_file) {
        if (point_and_click) {
            fprintf(stderr,
                    "warning: disabling point-and-click since reading from stdin\n");
        }
    } else {
        gregorio_check_file_access(read, input_file_name, ERROR,
                gregorio_exit(1));
        input_file = fopen(input_file_name, "r");
        if (!input_file) {
            fprintf(stderr, "error: can't open file %s for reading\n",
                    input_file_name);
            gregorio_exit(1);
        }
        if (point_and_click) {
            point_and_click_filename = encode_point_and_click_filename(
                    input_file_name);
        }
    }

    if (!error_file_name) {
        error_file = stderr;
        gregorio_set_error_out(error_file);
    } else {
        gregorio_check_file_access(write, error_file_name, ERROR,
                gregorio_exit(1));
        error_file = fopen(error_file_name, "wb");
        if (!error_file) {
            fprintf(stderr, "error: can't open file %s for writing\n",
                    error_file_name);
            gregorio_exit(1);
        }
        gregorio_set_error_out(error_file);
    }

    if (!verb_mode) {
        verb_mode = VERBOSITY_DEPRECATION;
    }

    gregorio_set_verbosity_mode(verb_mode);

    switch (input_format) {
    case GABC:
        score = gabc_read_score(input_file);
        break;
    default:
        /* not reachable unless there's a programming error */
        /* LCOV_EXCL_START */
        fprintf(stderr, "error : invalid input format\n");
        fclose(input_file);
        fclose(output_file);
        gregorio_exit(1);
        break;
        /* LCOV_EXCL_STOP */
    }

    fclose(input_file);
    if (score == NULL) {
        /* score should never be NULL on return from gabc_read_score */
        /* LCOV_EXCL_START */
        fclose(output_file);
        fprintf(stderr, "error in file parsing\n");
        gregorio_exit(1);
        /* LCOV_EXCL_STOP */
    }

    switch (output_format) {
    case GABC:
        gabc_write_score(output_file, score);
        break;
    case GTEX:
        gregoriotex_write_score(output_file, score, point_and_click_filename);
        break;
    case DUMP:
        dump_write_score(output_file, score);
        break;
    default:
        /* not reachable unless there's a programming error */
        /* LCOV_EXCL_START */
        fprintf(stderr, "error : invalid output format\n");
        gregorio_free_score(score);
        fclose(output_file);
        gregorio_exit(1);
        break;
        /* LCOV_EXCL_STOP */
    }
    fclose(output_file);
    if (point_and_click_filename) {
        free(point_and_click_filename);
    }
    gregorio_free_score(score);
    gregorio_vowel_tables_free();
    gabc_score_determination_lex_destroy();
    gabc_notes_determination_lex_destroy();
    gregorio_vowel_rulefile_lex_destroy();
    if (error_file_name) {
        fclose(error_file);
    }

    gregorio_exit(gregorio_get_return_value());
} /* due to exit on prior line, this will never be reached; LCOV_EXCL_LINE */