How to get Mac OS to remember previous auth. from my executable file












0















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:
system(strA);
}


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











share|improve this question

























  • 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
















0















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:
system(strA);
}


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











share|improve this question

























  • 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














0












0








0








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:
system(strA);
}


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











share|improve this question
















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:
system(strA);
}


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








shell-script command-line sudo password c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









Rui F Ribeiro

39.6k1479132




39.6k1479132










asked Aug 24 '18 at 19:13









McMathMcMath

13




13













  • 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



















  • 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

















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





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







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







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





@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










1 Answer
1






active

oldest

votes


















0














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).






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f464704%2fhow-to-get-mac-os-to-remember-previous-auth-from-my-executable-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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).






    share|improve this answer




























      0














      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).






      share|improve this answer


























        0












        0








        0







        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).






        share|improve this answer













        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).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 24 '18 at 20:12









        thrigthrig

        24.6k23056




        24.6k23056






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f464704%2fhow-to-get-mac-os-to-remember-previous-auth-from-my-executable-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Entries order in /etc/network/interfaces

            新発田市

            Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)