cybersecurity · intermediate · ~15 min · safe pentest lab

Is this a UID-0 passwd line?

Parse an /etc/passwd line and test whether its UID field is 0 (root-equivalent).

Challenge

An account with UID 0 has full root power. Parse one /etc/passwd line:

int is_uid0_line(const char *line);

The format is name:passwd:UID:GID:gecos:home:shell. Return 1 if the UID (third field) is exactly 0, else 0. Malformed lines return 0.

Input format

A single passwd line (no trailing newline required).

Output format

1 if UID==0, else 0.

Constraints

Fewer than two colons before the UID, or a non-numeric/empty UID, returns 0.

Starter code

#include <stddef.h>
/* /etc/passwd line "name:pw:UID:GID:gecos:home:shell". Return 1 if the UID field == 0. */
int is_uid0_line(const char *line){ (void)line; return 0; }

Common mistakes

Using atoi on the whole line; not validating that the UID field is purely numeric.

Edge cases to handle

00 equals 0; a line with only a name returns 0.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.