예제 #1
0
/**
 * @brief Add days or seconds to an ISO time string.
 * @naslfn{isotime_add}
 *
 * This function adds days or seconds to an ISO time string and
 * returns the resulting time string.  The number of days or seconds
 * are given using the named parameters; if none are given nothing is
 * added; if both are given both additions are performed.  This
 * function won't work for dates before the Gregorian calendar switch.
 *
 * @nasluparam
 *
 * - An ISO time string
 *
 * @naslnparam
 *
 * - @a years An integer with the number of years to add to the timestamp.
 *
 * - @a days An integer with the number of days to add to the timestamp.
 *
 * - @a seconds An integer with the number of seconds to add to the
 *              timestamp.
 *
 * @naslret The resulting ISO time string or NULL if the provided ISO
 *          time string is not valid or the result would overflow
 *          (i.e. year > 9999).
 *
 * @param[in] lexic  Lexical context of the NASL interpreter.
 *
 * @return A tree cell.
 */
tree_cell *
nasl_isotime_add (lex_ctxt *lexic)
{
  tree_cell *retc;
  my_isotime_t timebuf;
  const char *string;
  int nyears, ndays, nseconds;

  string = get_str_var_by_num (lexic, 0);
  if (!string
      || get_var_size_by_num (lexic, 0) < ISOTIME_SIZE -1
      || check_isotime (string))
    return NULL;
  memcpy (timebuf, string, ISOTIME_SIZE -1);
  timebuf[ISOTIME_SIZE - 1] = 0;

  nyears = get_int_local_var_by_name (lexic, "years", 0);
  ndays = get_int_local_var_by_name (lexic, "days", 0);
  nseconds = get_int_local_var_by_name (lexic, "seconds", 0);

  if (nyears && add_years_to_isotime (timebuf, nyears))
    return NULL;
  if (ndays && add_days_to_isotime (timebuf, ndays))
    return NULL;
  if (nseconds && add_seconds_to_isotime (timebuf, nseconds))
    return NULL;
  /* If nothing was added, explicitly add 0 years.  */
  if (!nyears && !ndays && !nseconds && add_years_to_isotime (timebuf, 0))
    return NULL;

  retc = alloc_typed_cell (CONST_STR);
  retc->x.str_val = estrdup (timebuf);
  retc->size = strlen (timebuf);
  return retc;
}
예제 #2
0
/* Callback function to try the unprotection from the passpharse query
   code. */
static int
try_unprotect_cb (struct pin_entry_info_s *pi)
{
  struct try_unprotect_arg_s *arg = pi->check_cb_arg;
  size_t dummy;
  gpg_error_t err;
  gnupg_isotime_t now, protected_at, tmptime;
  char *desc = NULL;

  assert (!arg->unprotected_key);

  arg->change_required = 0;
  err = agent_unprotect (arg->protected_key, pi->pin, protected_at,
                         &arg->unprotected_key, &dummy);
  if (err)
    return err;
  if (!opt.max_passphrase_days || arg->ctrl->in_passwd)
    return 0;  /* No regular passphrase change required.  */

  if (!*protected_at)
    {
      /* No protection date known - must force passphrase change.  */
      desc = xtrystrdup (_("Note: This passphrase has never been changed.%0A"
                           "Please change it now."));
      if (!desc)
        return gpg_error_from_syserror ();
    }
  else
    {
      gnupg_get_isotime (now);
      gnupg_copy_time (tmptime, protected_at);
      err = add_days_to_isotime (tmptime, opt.max_passphrase_days);
      if (err)
        return err;
      if (strcmp (now, tmptime) > 0 )
        {
          /* Passphrase "expired".  */
          desc = xtryasprintf
            (_("This passphrase has not been changed%%0A"
               "since %.4s-%.2s-%.2s.  Please change it now."),
             protected_at, protected_at+4, protected_at+6);
          if (!desc)
            return gpg_error_from_syserror ();
        }
    }

  if (desc)
    {
      /* Change required.  */
      if (opt.enforce_passphrase_constraints)
        {
          err = agent_get_confirmation (arg->ctrl, desc,
                                        _("Change passphrase"), NULL, 0);
          if (!err)
            arg->change_required = 1;
        }
      else
        {
          err = agent_get_confirmation (arg->ctrl, desc,
                                        _("Change passphrase"),
                                        _("I'll change it later"), 0);
          if (!err)
            arg->change_required = 1;
          else if (gpg_err_code (err) == GPG_ERR_CANCELED)
            err = 0;
        }
      xfree (desc);
    }

  return 0;
}