- struct passwd *p;
- /* Locking can't work when running as root, because we have no way of
- knowing what the user id of the logged in user is (so we don't know
- whose password to prompt for.)
- */
- si->locking_disabled_p = True;
- si->nolock_reason = "running as root";
- p = getpwnam ("nobody");
- if (! p) p = getpwnam ("noaccess");
- if (! p) p = getpwnam ("daemon");
- if (! p) p = getpwnam ("bin");
- if (! p) p = getpwnam ("sys");
- if (! p)
- {
- hack_uid_error = "couldn't find safe uid; running as root.";
- hack_uid_errno = -1;
- }
- else
- {
- struct group *g = getgrgid (p->pw_gid);
- hack_uid_error = hack_uid_buf;
- sprintf (hack_uid_error, "changing uid/gid to %s/%s (%ld/%ld).",
- p->pw_name, (g ? g->gr_name : "???"),
- (long) p->pw_uid, (long) p->pw_gid);
-
- /* Change the gid to be a safe one. If we can't do that, then
- print a warning. We change the gid before the uid so that we
- change the gid while still root. */
- if (setgid (p->pw_gid) != 0)
- {
- hack_uid_errno = errno;
- sprintf (hack_uid_error, "couldn't set gid to %s (%ld)",
- (g ? g->gr_name : "???"), (long) p->pw_gid);
- }
-
- /* Now change the uid to be a safe one. */
- if (setuid (p->pw_uid) != 0)
- {
- hack_uid_errno = errno;
- sprintf (hack_uid_error, "couldn't set uid to %s (%ld)",
- p->pw_name, (long) p->pw_uid);
- }
- }