Skip to content

Instantly share code, notes, and snippets.

@growler
Created February 20, 2024 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save growler/ad9f2cbf2ed3c56c0aaf2c5c24950c25 to your computer and use it in GitHub Desktop.
Save growler/ad9f2cbf2ed3c56c0aaf2c5c24950c25 to your computer and use it in GitHub Desktop.
A simple sudo for launchd scripts
{pkgs, ... }:
{
nixpkgs.overlays = [
(final: prev: {
exec-as = pkgs.writeCBin "exec-as" ''
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
void su(const char* username) {
struct passwd *pw = getpwnam(username);
if (pw == NULL) {
perror("failed to get user information");
exit(-1);
}
if (setgid(pw->pw_gid) != 0) {
perror("failed to set group");
exit(-1);
}
if (setuid(pw->pw_uid) != 0) {
perror("failed to set user");
exit(-1);
}
}
int main(int argc, char *argv[]) {
if (argc < 4) {
fprintf(stderr, "Usage: %s <username> <command-path> <command-name> [arguments...]\n", argv[0]);
exit(-1);
}
su(argv[1]);
execv(argv[2], &argv[3]);
perror("execv failed");
exit(-1);
}
'';
})
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment