Getting root access (with permission, obviously)

I'm writing an installer for a bootloader I'm writing and I want to be able to write the bootsector, mount devices, etc.

How can I get root access? Other programs can do it (such as su or sudo), so how can I do it?

At the moment I'm just printing an error message if the UID isn't 0 and exiting, but I'd like to be able to get root access from within the program itself.
Okay. If you're trying to unlock root, then here's what you do...

The password for root on most sane GNU/Linux systems is something that nobody knows about. However using sudo passwd root might be of use to you for changing root's password.
Good luck.

Else, I'm not sure what you meant.

-Albatross
Last edited on
I'm not interested in messing up the user's setup, I'm interested in making an installer for my bootloader. :S
Obviously unprivileged users are not able to mount disks or write to certain files (such as those in /dev). What I'm trying to do is get the ability to do those things. If the user runs the program as non-root, I'd like to prompt them for the root password and attempt to get root access.

I know it sounds "fishy", so I'll post my main function (which is all I've written so far) to show what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
int main(int argc, char** argv)
{
	char* device = NULL;

	/*
	 * Check for args
	 */
	if (argc < 2) {
		fprintf(stderr, "Usage: %s <device>\n", argv[0]);
		exit(1);
	}

	device = argv[1];

	/*
	 * Check the device we're given is a real one
	 */
	if (!(fexists(device))) {
		fprintf(stderr, "%s: \"%s\": file doesn't exist\n",
			argv[0], device);
		exit(1);
	}
	
	/*
	 * Check we have root access
	 */
	if (getuid()) {
		fprintf(stderr, "%s: you are not root. This program will only "
			"work if run as root.\n", argv[0]);
		exit(1);
	}
	
	/*
	 * Write the bootsector and the other files
	 */	
	if (write_mbr(device) < 0) {
		fprintf(stderr, "%s: couldn't write MBR to \"%s\"\n",
			argv[0], device);
		exit(1);
	}
	
	if (has_extfs(device)) {
		if (write_extfs(device) < 0) {
			fprintf(stderr, "%s: couldn't write files to \"%s\"\n",
				argv[0], device);
			exit(1);
		}
	} else {
		if (write_nofs(device) < 0) {
			fprintf(stderr, "%s: couldn't write files to \"%s\"\n",
				argv[0], device);
			exit(1);
		}
	}

	return 0;
}
Unfortunately, the only way I know of gaining root access from inside this program is to use system(). If you don't want to use system, then I recommend writing that you require the user to run this program as sudo bootloader. If they don't run the program as root, then demand in that section that you hand the program root privileges via sudo.

Pre-P.S.- There are a lot of programs that run requiring sudo before them. Not even apt-get can run without you putting sudo before it.

-Albatross
Last edited on
You might want to check out the SUID file permission.
Ok. I assumed programs could gain root access (they can, with setuid() but I don't know how that except from an already privileged program) but I guess my error message will do.

Edit:
@moorecm,
I don't know that that will help. I think asking the user to give root access before running the program will do, if I can't get root access from within the program.
Last edited on
Topic archived. No new replies allowed.