2019-09-06 15:34:26 +02:00
|
|
|
#include <AK/String.h>
|
2020-01-12 12:13:54 +02:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
2019-02-27 12:32:53 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2019-02-27 12:32:53 +01:00
|
|
|
|
2019-06-07 11:49:31 +02:00
|
|
|
int main(int argc, char** argv)
|
2019-02-27 12:32:53 +01:00
|
|
|
{
|
2020-01-12 13:52:02 +02:00
|
|
|
if (pledge("stdio rpath chown", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-27 12:32:53 +01:00
|
|
|
if (argc < 2) {
|
|
|
|
printf("usage: chown <uid[:gid]> <path>\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_t new_uid = -1;
|
|
|
|
gid_t new_gid = -1;
|
|
|
|
|
|
|
|
auto parts = String(argv[1]).split(':');
|
|
|
|
if (parts.is_empty()) {
|
|
|
|
fprintf(stderr, "Empty uid/gid spec\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-01-12 12:13:54 +02:00
|
|
|
|
2019-02-27 12:32:53 +01:00
|
|
|
bool ok;
|
|
|
|
new_uid = parts[0].to_uint(ok);
|
|
|
|
if (!ok) {
|
2020-01-12 12:13:54 +02:00
|
|
|
new_uid = getpwnam(parts[0].characters())->pw_uid;
|
|
|
|
|
|
|
|
if (!new_uid) {
|
|
|
|
fprintf(stderr, "Invalid uid: '%s'\n", parts[0].characters());
|
|
|
|
return 1;
|
|
|
|
}
|
2019-02-27 12:32:53 +01:00
|
|
|
}
|
2020-01-12 12:13:54 +02:00
|
|
|
|
2019-02-27 12:32:53 +01:00
|
|
|
if (parts.size() == 2) {
|
|
|
|
new_gid = parts[1].to_uint(ok);
|
|
|
|
if (!ok) {
|
2020-01-12 12:13:54 +02:00
|
|
|
new_gid = getgrnam(parts[1].characters())->gr_gid;
|
|
|
|
|
|
|
|
if(!new_gid) {
|
|
|
|
fprintf(stderr, "Invalid gid: '%s'\n", parts[1].characters());
|
|
|
|
return 1;
|
|
|
|
}
|
2019-02-27 12:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc = chown(argv[2], new_uid, new_gid);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("chown");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|