I'm writing a tiny unix executable file that changes my macbook's system time manually. Of course, this requires a password, and so I used the system() function to interface with the terminal and change the date using echo and sudo.

See below:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

    // Retrieve password:
    printf("Enter Sudo Password:\n");

    char *pswrd;
    pswrd = getpass("Password: ");
    char strA[512];

    // Instruction to manually change the date:
    char *strA1 = "echo \"";
    char *strA2 = "\" | sudo -S date 020821002014";

    // Stitch fields into one string:
    strcat(strA, strA1);
    strcat(strA, pswrd);
    strcat(strA, strA2);

    // Run string in Terminal:

You can create a Unix executable to test.

In terminal line, it is equivalent to: Sudo date 020821002014

> Anyway, my question:

I don't like that every time I run the executable file, I have to enter a password. Does anyone have suggestions for a way to input the admin password once and have my MacBook recognize the executable in future so I just have to click on the executable and change the date effortlessly.

Oh, and I only picked the date because it typically requires a password, but this would of-course generalize to other administrator instructions.

Things I've considered:

  1. Using root superuser, but it seems overkill - there must be another way
  2. Writing password into .c code directly, but that sounds sketchy, and doesn't scale easily for multiple users
  • A C program is very rarely called "a script". "Script" is usually used for programs written in interpreted languages. – Kusalananda Aug 24 '18 at 19:15
  • Thank you for that point @Kusalananda, what is a more appropriate synonym for .c file? I'll edit my post accordingly. – McMath Aug 24 '18 at 19:26
  • Just make a script that runs sudo date 020821002014. Sudo caches credentials per terminal based on user policy. The stuff you're doing is unecessary an error prone (getpass shouldn't be used and your quoting of the password is brittle). There shouldn't be a need for any of it. – PSkocik Aug 24 '18 at 20:23
  • @McMath Well, I would call it the source code and I would call the compiled executable "the executable" or "the binary". – Kusalananda Aug 24 '18 at 21:06

Another method (that does not leave the password lying around nor expose it to various process listing and diagnostic tools) would be to set a NOPASSWD entry in the sudo configuration file that allows only the date command to be run, possibly an exact date command:

fixmeyourusernamehere ALL=(root) NOPASSWD: /bin/date 020821002014

with this set at the bottom of the /etc/sudoers file (better edited with sudo visudo, though) then the user fixmeyourusernamehere should be able to run 'sudo date 020821002014without a password, but not any other commands (unless thesudo` configuration allows them other commands).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.