logo

skeud

Simple and portable utilities to deal with user accounts (su, login)git clone https://anongit.hacktivis.me/git/skeud.git/
commit: 978850878fc24edb9c696e6abff001f20ec7a8ad
parent 5fba57eeb4777ef01479e00c2d23a1d52d0812df
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 23 May 2025 20:18:36 +0200

Use `error:/info:` in stderr messages

Diffstat:

Mcommon.c16++++++++--------
Mlogin.c42++++++++++++++++++++++++------------------
Msu.c35+++++++++++++++++++----------------
3 files changed, 51 insertions(+), 42 deletions(-)

diff --git a/common.c b/common.c @@ -49,13 +49,13 @@ skeud_crypt_check(const char *hash, const char *password) char *chk_hash = crypt(password, hash); if(chk_hash == NULL) { - perror("skeud_crypt_check: crypt"); + perror("skeud_crypt_check: error: crypt"); return false; } bool match = hash_match(hash, chk_hash); - explicit_bzero(chk_hash, sizeof(chk_hash)); + explicit_bzero(chk_hash, strlen(chk_hash)); return match; } @@ -71,19 +71,19 @@ skeud_getpass(char **password) FILE *tty = fopen("/dev/tty", "rb+"); if(tty == NULL) { - perror("skeud_getpass: open(\"/dev/tty\")"); + perror("skeud_getpass: error: open(\"/dev/tty\")"); return got; } int tty_fd = fileno(tty); if(tty_fd < 0) { - perror("skeud_getpass: fileno(tty)"); + perror("skeud_getpass: error: fileno(tty)"); goto getpass_end; } if(tcgetattr(tty_fd, &t) < 0) { - perror("skeud_getpass: tcgetattr"); + perror("skeud_getpass: error: tcgetattr"); goto getpass_end; } @@ -91,7 +91,7 @@ skeud_getpass(char **password) t.c_lflag &= ~ECHO; if(tcsetattr(tty_fd, TCSANOW, &t) < 0) { - perror("skeud_getpass: tcsetattr(~ECHO)"); + perror("skeud_getpass: error: tcsetattr(~ECHO)"); goto getpass_end; } @@ -99,7 +99,7 @@ skeud_getpass(char **password) got = getline(password, &len, tty); if(got < 0) { - if(errno != 0) perror("skeud_getpass: getline"); + if(errno != 0) perror("skeud_getpass: error: getline"); goto getpass_clean; } fprintf(tty, "\n"); @@ -111,7 +111,7 @@ getpass_clean: t.c_lflag ^= ECHO; if(tcsetattr(tty_fd, TCSANOW, &t) < 0) { - perror("skeud_getpass: tcsetattr(ECHO)"); + perror("skeud_getpass: error: tcsetattr(ECHO)"); explicit_bzero(password, got); got = -1; goto getpass_end; diff --git a/login.c b/login.c @@ -41,7 +41,7 @@ main(int argc, char *argv[]) if(getuid() != 0) { - fprintf(stderr, "login: Not super-user\n"); + fprintf(stderr, "login: error: Not super-user\n"); return 1; } @@ -58,13 +58,13 @@ main(int argc, char *argv[]) opt_p = true; break; case ':': - fprintf(stderr, "login: Option -%c requires an operand\n", optopt); + fprintf(stderr, "login: error: Option -%c requires an operand\n", optopt); return 1; case '?': - fprintf(stderr, "login: Unrecognized option: '-%c'\n", optopt); + fprintf(stderr, "login: error: Unrecognized option: '-%c'\n", optopt); return 1; default: - fprintf(stderr, "login: Unknown getopt state, aborting\n"); + fprintf(stderr, "login: error: Unknown getopt state, aborting\n"); abort(); } } @@ -80,7 +80,7 @@ main(int argc, char *argv[]) } else if(argc > 1) { - fprintf(stderr, "login: Too many arguments given.\n"); + fprintf(stderr, "login: error: Got %d arguments, expected <= 1\n", argc); return 1; } } @@ -88,7 +88,7 @@ main(int argc, char *argv[]) { if(argc > 0) { - fprintf(stderr, "login: Too many arguments given.\n"); + fprintf(stderr, "login: error: Got %d arguments, expected <= 0\n", argc); return 1; } } @@ -101,7 +101,7 @@ main(int argc, char *argv[]) ssize_t got = getline(&username, &len, stdin); if(got < 0) { - if(errno != 0) perror("login: getline"); + if(errno != 0) perror("login: error: getline"); free(username); return 1; } @@ -116,7 +116,7 @@ main(int argc, char *argv[]) pwent = getpwnam(username); if(errno != 0) { - perror("login: getpwnam"); + perror("login: warning: getpwnam"); } if(!opt_f) @@ -166,7 +166,7 @@ main(int argc, char *argv[]) { free(username); sleep(2); - fprintf(stderr, "login: Invalid username or password\n"); + fprintf(stderr, "login: error: Invalid username or password\n"); return 1; } } @@ -187,7 +187,7 @@ main(int argc, char *argv[]) struct group *tty_group = getgrnam(TTY_GROUP); if(tty_group == NULL) { - perror("login: getgrnam"); + perror("login: warning: getgrnam"); } else { @@ -197,23 +197,28 @@ main(int argc, char *argv[]) /* considers that STDIN_FILENO is close enough to the current tty */ if(fchown(STDIN_FILENO, pwent->pw_uid, tty_gid) < 0) { - perror("login: fchown"); + perror("login: error: fchown"); + return 1; } if(fchmod(STDIN_FILENO, TTY_PERMS)) { - perror("login: fchmod"); + perror("login: error: fchmod"); + return 1; } if(setgid(pwent->pw_gid) < 0) { - perror("login: setgid"); + perror("login: error: setgid"); + return 1; } if(initgroups(pwent->pw_name, pwent->pw_gid) < 0) { - perror("login: initgroups"); + perror("login: error: initgroups"); + return 1; } if(setuid(pwent->pw_uid) < 0) { - perror("login: setuid"); + perror("login: error: setuid"); + return 1; } if(pwent->pw_shell != NULL) @@ -227,7 +232,8 @@ main(int argc, char *argv[]) if(chdir(pwent->pw_dir) != 0) { - perror("login: chdir"); + fprintf( + stderr, "login: warning: Failed to change current directory to: %s\n", pwent->pw_dir); } } @@ -248,12 +254,12 @@ main(int argc, char *argv[]) { if(errno == ENOENT) { - perror("login: execve"); + perror("login: error: execve"); return 127; } else { - perror("login: execve"); + perror("login: error: execve"); return 126; } } diff --git a/su.c b/su.c @@ -38,7 +38,7 @@ main(int argc, char *argv[]) if(geteuid() != 0) { - fprintf(stderr, "su: Not effectively super-user. Missing setuid?\n"); + fprintf(stderr, "su: error: Not effectively super-user. Missing setuid?\n"); return 1; } @@ -56,7 +56,7 @@ main(int argc, char *argv[]) case 's': // shell if(getuid() != 0) { - fprintf(stderr, "su: Only the super-user can override the target shell\n"); + fprintf(stderr, "su: error: Only the super-user can override the target shell\n"); return 1; } @@ -65,20 +65,20 @@ main(int argc, char *argv[]) case 'p': // preserve environment if(getuid() != 0) { - fprintf(stderr, "su: Only the super-user can preserve the environment\n"); + fprintf(stderr, "su: error: Only the super-user can preserve the environment\n"); return 1; } opt_p = true; break; case ':': - fprintf(stderr, "su: Option -%c requires an operand\n", optopt); + fprintf(stderr, "su: error: Option -%c requires an operand\n", optopt); return 1; case '?': - fprintf(stderr, "su: Unrecognized option: '-%c'\n", optopt); + fprintf(stderr, "su: error: Unrecognized option: '-%c'\n", optopt); return 1; default: - fprintf(stderr, "su: Unknown getopt state, aborting\n"); + fprintf(stderr, "su: error: Unknown getopt state, aborting\n"); abort(); } } @@ -96,7 +96,7 @@ main(int argc, char *argv[]) if(argc > 1) { - fprintf(stderr, "su: Too many arguments given.\n"); + fprintf(stderr, "su: error: Got %d arguments, expected <= 1\n", argc); return 1; } @@ -112,11 +112,11 @@ main(int argc, char *argv[]) { if(errno != 0) { - perror("su: getpwnam"); + perror("su: error: getpwnam"); } else { - fprintf(stderr, "su: getpwnam: No entry found for user %s\n", username); + fprintf(stderr, "su: error: getpwnam: No entry found for user %s\n", username); } return 1; @@ -130,13 +130,13 @@ main(int argc, char *argv[]) } else { - fprintf(stderr, "su: No shell entry for user %s\n", username); + fprintf(stderr, "su: error: No shell entry for user %s\n", username); return 1; } } - fprintf(stderr, "su: Authenticating as %s\n", username); + fprintf(stderr, "su: info: Authenticating as %s\n", username); if(getuid() != 0) { @@ -152,7 +152,7 @@ main(int argc, char *argv[]) struct spwd *swent = getspnam(username); if(errno != 0) { - perror("su: getspnam"); + perror("su: warning: getspnam"); } else { @@ -182,7 +182,7 @@ main(int argc, char *argv[]) if(!valid_p) { sleep(2); - fprintf(stderr, "su: Invalid username or password\n"); + fprintf(stderr, "su: error: Invalid username or password\n"); return 1; } } @@ -199,15 +199,18 @@ main(int argc, char *argv[]) if(setgid(pwent->pw_gid) < 0) { - perror("su: setgid"); + perror("su: error: setgid"); + return 1; } if(initgroups(username, pwent->pw_gid) < 0) { - perror("su: initgroups"); + perror("su: error: initgroups"); + return 1; } if(setuid(pwent->pw_uid) < 0) { - perror("su: setuid"); + perror("su: error: setuid"); + return 1; } const char *home = pwent->pw_dir ? pwent->pw_dir : "/";