Esempio n. 1
0
grub_err_t
grub_auth_authenticate (const char *user)
{
  struct grub_auth_user *cur;

  cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
  if (!cur)
    cur = grub_zalloc (sizeof (*cur));
  if (!cur)
    return grub_errno;

  cur->authenticated = 1;

  if (! cur->name)
    {
      cur->name = grub_strdup (user);
      if (!cur->name)
	{
	  grub_free (cur);
	  return grub_errno;
	}
      grub_list_push (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
    }

  return GRUB_ERR_NONE;
}
Esempio n. 2
0
grub_err_t
grub_auth_register_authentication (const char *user,
				   grub_auth_callback_t callback,
				   void *arg)
{
  struct grub_auth_user *cur;

  cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
  if (!cur)
    cur = grub_zalloc (sizeof (*cur));
  if (!cur)
    return grub_errno;
  cur->callback = callback;
  cur->arg = arg;
  if (! cur->name)
    {
      cur->name = grub_strdup (user);
      if (!cur->name)
	{
	  grub_free (cur);
	  return grub_errno;
	}
      grub_list_push (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
    }
  return GRUB_ERR_NONE;
}
Esempio n. 3
0
static void
grub_gettext_delete_list (void)
{
  struct grub_gettext_msg *item;

  while ((item =
	  grub_list_pop (GRUB_AS_LIST_P (&grub_gettext_msg_list))) != 0)
    {
      char *original = (char *) ((struct grub_gettext_msg *) item)->name;
      grub_free (original);

      /* Don't delete the translated message because could be in use.  */
    }
}
Esempio n. 4
0
grub_err_t
grub_auth_deauthenticate (const char *user)
{
  struct grub_auth_user *cur;
  cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
  if (!cur)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "user '%s' not found", user);
  if (!cur->callback)
    {
      grub_free (cur->name);
      grub_list_remove (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
      grub_free (cur);
    }
  else
    cur->authenticated = 0;
  return GRUB_ERR_NONE;
}
Esempio n. 5
0
static void
insert_item (grub_autolist_t *list, char *name, char *file)
{
  grub_autolist_t p;

  p = grub_malloc (sizeof (*p));
  if (! p)
    return;

  p->name = grub_malloc (grub_strlen (name) + grub_strlen (file) + 2);
  if (! p->name)
    {
      grub_free (p);
      return;
    }

  grub_strcpy (p->name, name);
  p->value = p->name + grub_strlen (name) + 1;
  grub_strcpy (p->value, file);
  grub_list_push (GRUB_AS_LIST_P (list), GRUB_AS_LIST (p));
}
Esempio n. 6
0
/* Check if DEVICE can be read.  Skip any DEVICE that we have already seen.
   If an error occurs, return zero, otherwise return non-zero.  */
static int
check_device_readable_unique (const char *device)
{
  char *real_device;
  char buf[512];
  FILE *fp;
  struct seen_device *seen_elt;

  /* If DEVICE is empty, just return error.  */
  if (*device == 0)
    return 0;

  /* Have we seen this device already?  */
  real_device = canonicalize_file_name (device);
  if (! real_device)
    return 0;
  if (grub_named_list_find (GRUB_AS_NAMED_LIST (seen), real_device))
    {
      grub_dprintf ("deviceiter", "Already seen %s (%s)\n",
		    device, real_device);
      goto fail;
    }

  fp = fopen (device, "r");
  if (! fp)
    {
      switch (errno)
	{
#ifdef ENOMEDIUM
	case ENOMEDIUM:
# if 0
	  /* At the moment, this finds only CDROMs, which can't be
	     read anyway, so leave it out. Code should be
	     reactivated if `removable disks' and CDROMs are
	     supported.  */
	  /* Accept it, it may be inserted.  */
	  return 1;
# endif
	  break;
#endif /* ENOMEDIUM */
	default:
	  /* Break case and leave.  */
	  break;
	}
      /* Error opening the device.  */
      goto fail;
    }

  /* Make sure CD-ROMs don't get assigned a BIOS disk number
     before SCSI disks!  */
#ifdef __linux__
# ifdef CDROM_GET_CAPABILITY
  if (ioctl (fileno (fp), CDROM_GET_CAPABILITY, 0) >= 0)
    goto fail;
# else /* ! CDROM_GET_CAPABILITY */
  /* Check if DEVICE is a CD-ROM drive by the HDIO_GETGEO ioctl.  */
  {
    struct hd_geometry hdg;
    struct stat st;

    if (fstat (fileno (fp), &st))
      goto fail;

    /* If it is a block device and isn't a floppy, check if HDIO_GETGEO
       succeeds.  */
    if (S_ISBLK (st.st_mode)
	&& MAJOR (st.st_rdev) != FLOPPY_MAJOR
	&& ioctl (fileno (fp), HDIO_GETGEO, &hdg))
      goto fail;
  }
# endif /* ! CDROM_GET_CAPABILITY */
#endif /* __linux__ */

#if defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
# ifdef CDIOCCLRDEBUG
  if (ioctl (fileno (fp), CDIOCCLRDEBUG, 0) >= 0)
    goto fail;
# endif /* CDIOCCLRDEBUG */
#endif /* __FreeBSD_kernel__ || __NetBSD__ || __OpenBSD__ */

  /* Attempt to read the first sector.  */
  if (fread (buf, 1, 512, fp) != 512)
    {
      fclose (fp);
      goto fail;
    }

  /* Remember that we've seen this device.  */
  seen_elt = xmalloc (sizeof (*seen_elt));
  seen_elt->name = real_device; /* steal memory */
  grub_list_push (GRUB_AS_LIST_P (&seen), GRUB_AS_LIST (seen_elt));

  fclose (fp);
  return 1;

fail:
  free (real_device);
  return 0;
}
Esempio n. 7
0
static grub_err_t
handle_command (int argc, char **args, struct abstract_terminal **enabled,
                struct abstract_terminal **disabled,
                struct grub_term_autoload *autoloads,
                const char *active_str,
                const char *available_str)
{
    int i;
    struct abstract_terminal *term;
    struct grub_term_autoload *aut;

    if (argc == 0)
    {
        grub_puts_ (active_str);
        for (term = *enabled; term; term = term->next)
            grub_printf ("%s ", term->name);
        grub_printf ("\n");
        grub_puts_ (available_str);
        for (term = *disabled; term; term = term->next)
            grub_printf ("%s ", term->name);
        /* This is quadratic but we don't expect mode than 30 terminal
          modules ever.  */
        for (aut = autoloads; aut; aut = aut->next)
        {
            for (term = *disabled; term; term = term->next)
                if (grub_strcmp (term->name, aut->name) == 0
                        || (aut->name[0] && aut->name[grub_strlen (aut->name) - 1] == '*'
                            && grub_memcmp (term->name, aut->name,
                                            grub_strlen (aut->name) - 1) == 0))
                    break;
            if (!term)
                for (term = *enabled; term; term = term->next)
                    if (grub_strcmp (term->name, aut->name) == 0
                            || (aut->name[0] && aut->name[grub_strlen (aut->name) - 1] == '*'
                                && grub_memcmp (term->name, aut->name,
                                                grub_strlen (aut->name) - 1) == 0))
                        break;
            if (!term)
                grub_printf ("%s ", aut->name);
        }
        grub_printf ("\n");
        return GRUB_ERR_NONE;
    }
    i = 0;

    if (grub_strcmp (args[0], "--append") == 0
            || grub_strcmp (args[0], "--remove") == 0)
        i++;

    if (i == argc)
        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_ ("no terminal specified"));

    for (; i < argc; i++)
    {
        int again = 0;
        while (1)
        {
            for (term = *disabled; term; term = term->next)
                if (grub_strcmp (args[i], term->name) == 0)
                    break;
            if (term == 0)
                for (term = *enabled; term; term = term->next)
                    if (grub_strcmp (args[i], term->name) == 0)
                        break;
            if (term)
                break;
            if (again)
                return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminal '%s'\n",
                                   args[i]);
            for (aut = autoloads; aut; aut = aut->next)
                if (grub_strcmp (args[i], aut->name) == 0
                        || (aut->name[0] && aut->name[grub_strlen (aut->name) - 1] == '*'
                            && grub_memcmp (args[i], aut->name,
                                            grub_strlen (aut->name) - 1) == 0))
                {
                    grub_dl_t mod;
                    mod = grub_dl_load (aut->modname);
                    if (mod)
                        grub_dl_ref (mod);
                    grub_errno = GRUB_ERR_NONE;
                    break;
                }
            if (!aut)
                return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminal '%s'\n",
                                   args[i]);
            again = 1;
        }
    }

    if (grub_strcmp (args[0], "--append") == 0)
    {
        for (i = 1; i < argc; i++)
        {
            for (term = *disabled; term; term = term->next)
                if (grub_strcmp (args[i], term->name) == 0)
                    break;
            if (term)
            {
                if (term->init && term->init (term) != GRUB_ERR_NONE)
                    return grub_errno;

                grub_list_remove (GRUB_AS_LIST_P (disabled), GRUB_AS_LIST (term));
                grub_list_push (GRUB_AS_LIST_P (enabled), GRUB_AS_LIST (term));
            }
        }
        return GRUB_ERR_NONE;
    }

    if (grub_strcmp (args[0], "--remove") == 0)
    {
        for (i = 1; i < argc; i++)
        {
            for (term = *enabled; term; term = term->next)
                if (grub_strcmp (args[i], term->name) == 0)
                    break;
            if (term)
            {
                if (!term->next && term == *enabled)
                    return grub_error (GRUB_ERR_BAD_ARGUMENT,
                                       "can't remove the last terminal");
                grub_list_remove (GRUB_AS_LIST_P (enabled), GRUB_AS_LIST (term));
                if (term->fini)
                    term->fini (term);
                grub_list_push (GRUB_AS_LIST_P (disabled), GRUB_AS_LIST (term));
            }
        }
        return GRUB_ERR_NONE;
    }
    for (i = 0; i < argc; i++)
    {
        for (term = *disabled; term; term = term->next)
            if (grub_strcmp (args[i], term->name) == 0)
                break;
        if (term)
        {
            if (term->init && term->init (term) != GRUB_ERR_NONE)
                return grub_errno;

            grub_list_remove (GRUB_AS_LIST_P (disabled), GRUB_AS_LIST (term));
            grub_list_push (GRUB_AS_LIST_P (enabled), GRUB_AS_LIST (term));
        }
    }

    {
        struct abstract_terminal *next;
        for (term = *enabled; term; term = next)
        {
            next = term->next;
            for (i = 0; i < argc; i++)
                if (grub_strcmp (args[i], term->name) == 0)
                    break;
            if (i == argc)
            {
                if (!term->next && term == *enabled)
                    return grub_error (GRUB_ERR_BAD_ARGUMENT,
                                       "can't remove the last terminal");
                grub_list_remove (GRUB_AS_LIST_P (enabled), GRUB_AS_LIST (term));
                if (term->fini)
                    term->fini (term);
                grub_list_push (GRUB_AS_LIST_P (disabled), GRUB_AS_LIST (term));
            }
        }
    }

    return GRUB_ERR_NONE;
}
Esempio n. 8
0
static const char *
grub_gettext_translate (const char *orig)
{
  char *current_string;
  const char *ret;

  int min, max, current;
  int found = 0;

  struct grub_gettext_msg *cur;

  /* Make sure we can use grub_gettext_translate for error messages.  Push
     active error message to error stack and reset error message.  */
  grub_error_push ();

  cur = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_gettext_msg_list),
			      orig);

  if (cur)
    {
      grub_error_pop ();
      return cur->translated;
    }

  if (fd_mo == 0)
    {
      grub_error_pop ();
      return orig;
    }

  min = 0;
  max = grub_gettext_max;

  current = (max + min) / 2;

  while (current != min && current != max && found == 0)
    {
      current_string = grub_gettext_getstring_from_position (current);

      /* Search by bisection.  */
      if (grub_strcmp (current_string, orig) < 0)
	{
	  grub_free (current_string);
	  min = current;
	}
      else if (grub_strcmp (current_string, orig) > 0)
	{
	  grub_free (current_string);
	  max = current;
	}
      else if (grub_strcmp (current_string, orig) == 0)
	{
	  grub_free (current_string);
	  found = 1;
	}
      current = (max + min) / 2;
    }

  ret = found ? grub_gettext_gettranslation_from_position (current) : orig;

  if (found)
    {
      cur = grub_zalloc (sizeof (*cur));

      if (cur)
	{
	  cur->name = grub_strdup (orig);
	  if (cur->name)
	    {
	      cur->translated = ret;
	      grub_list_push (GRUB_AS_LIST_P (&grub_gettext_msg_list),
			      GRUB_AS_LIST (cur));
	    }
	}
      else
	grub_errno = GRUB_ERR_NONE;
    }

  grub_error_pop ();
  return ret;
}
Esempio n. 9
0
static int
grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
{
  size_t len = strlen (dev);
  const char *format;
  char *p;
  int i;
  char real_dev[PATH_MAX];
  struct linux_partition_cache *cache;
  int missing = 0;

  strcpy(real_dev, dev);

  if (have_devfs () && strcmp (real_dev + len - 5, "/disc") == 0)
    {
      p = real_dev + len - 4;
      format = "part%d";
    }
  else if (strncmp (real_dev, "/dev/disk/by-id/",
		    sizeof ("/dev/disk/by-id/") - 1) == 0)
    {
      p = real_dev + len;
      format = "-part%d";
    }
  else if (real_dev[len - 1] >= '0' && real_dev[len - 1] <= '9')
    {
      p = real_dev + len;
      format = "p%d";
    }
  else
    {
      p = real_dev + len;
      format = "%d";
    }

  for (cache = linux_partition_cache_list; cache; cache = cache->next)
    {
      if (strcmp (cache->dev, dev) == 0 && cache->start == sector)
	{
	  sprintf (p, format, cache->partno);
	  strcpy (dev, real_dev);
	  return 1;
	}
    }

  for (i = 1; i < 10000; i++)
    {
      grub_util_fd_t fd;
      grub_disk_addr_t start;

      sprintf (p, format, i);

      fd = open (real_dev, O_RDONLY);
      if (fd == -1)
	{
	  if (missing++ < 10)
	    continue;
	  else
	    return 0;
	}
      missing = 0;
      close (fd);

      if (!grub_util_device_is_mapped (real_dev)
	  || !grub_util_get_dm_node_linear_info (real_dev, 0, 0, &start))
	start = grub_util_find_partition_start_os (real_dev);
      /* We don't care about errors here.  */
      grub_errno = GRUB_ERR_NONE;

      if (start == sector)
	{
	  struct linux_partition_cache *new_cache_item;

	  new_cache_item = xmalloc (sizeof *new_cache_item);
	  new_cache_item->dev = xstrdup (dev);
	  new_cache_item->start = start;
	  new_cache_item->partno = i;
	  grub_list_push (GRUB_AS_LIST_P (&linux_partition_cache_list),
			  GRUB_AS_LIST (new_cache_item));

	  strcpy (dev, real_dev);
	  return 1;
	}
    }

  return 0;
}