From: Marek Polacek Date: Wed, 1 Dec 2010 16:18:17 +0000 (+0100) Subject: login: use EXIT_* and err.h, clean up return codes X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=66020e56d2888f59aef577a90761014e202383f0;p=util-linux login: use EXIT_* and err.h, clean up return codes [kzak@redhat.com: - cleanup return codes - use warn()] Signed-off-by: Marek Polacek Signed-off-by: Karel Zak --- diff --git a/login-utils/login.c b/login-utils/login.c index 1802d3a2..5ece59b2 100644 --- a/login-utils/login.c +++ b/login-utils/login.c @@ -97,6 +97,7 @@ #include #include #include +#include #include #include #include @@ -124,7 +125,7 @@ # define PAM_FAIL_CHECK if (retcode != PAM_SUCCESS) { \ fprintf(stderr,"\n%s\n",pam_strerror(pamh, retcode)); \ syslog(LOG_ERR,"%s",pam_strerror(pamh, retcode)); \ - pam_end(pamh, retcode); exit(1); \ + pam_end(pamh, retcode); exit(EXIT_FAILURE); \ } # define PAM_END { \ pam_setcred(pamh, PAM_DELETE_CRED); \ @@ -196,15 +197,13 @@ opentty(const char * tty) { if (fd == -1) { syslog(LOG_ERR, _("FATAL: can't reopen tty: %s"), strerror(errno)); - sleep(1); - exit(1); + sleepexit(EXIT_FAILURE); } if (!isatty(fd)) { close(fd); syslog(LOG_ERR, _("FATAL: %s is not a terminal"), tty); - sleep(1); - exit(1); + sleepexit(EXIT_FAILURE); } flags = fcntl(fd, F_GETFL); @@ -235,9 +234,9 @@ check_ttyname(char *ttyn) { || !S_ISCHR(statbuf.st_mode) || (statbuf.st_nlink > 1 && strncmp(ttyn, "/dev/", 5)) || (access(ttyn, R_OK | W_OK) != 0)) { + syslog(LOG_ERR, _("FATAL: bad tty")); - sleep(1); - exit(1); + sleepexit(EXIT_FAILURE); } } @@ -443,7 +442,7 @@ main(int argc, char **argv) if (getuid()) { fprintf(stderr, _("login: -h for super-user only.\n")); - exit(1); + exit(EXIT_FAILURE); } hflag = 1; if (domain && (p = strchr(optarg, '.')) && @@ -486,7 +485,7 @@ main(int argc, char **argv) default: fprintf(stderr, _("usage: login [-fp] [username]\n")); - exit(1); + exit(EXIT_FAILURE); } argc -= optind; argv += optind; @@ -573,11 +572,10 @@ main(int argc, char **argv) retcode = pam_start(hflag?"remote":"login",username, &conv, &pamh); if(retcode != PAM_SUCCESS) { - fprintf(stderr, _("%s: PAM failure, aborting: %s\n"), - "login", pam_strerror(pamh, retcode)); + warnx(_("PAM failure, aborting: %s"), pam_strerror(pamh, retcode)); syslog(LOG_ERR, _("Couldn't initialize PAM: %s"), pam_strerror(pamh, retcode)); - exit(99); + exit(EXIT_FAILURE); } /* hostname & tty are either set to NULL or their correct values, depending on how much we know */ @@ -661,7 +659,7 @@ main(int argc, char **argv) fprintf(stderr,_("\nLogin incorrect\n")); pam_end(pamh, retcode); - exit(0); + exit(EXIT_SUCCESS); } } @@ -687,18 +685,18 @@ main(int argc, char **argv) PAM_FAIL_CHECK; if (!username || !*username) { - fprintf(stderr, _("\nSession setup problem, abort.\n")); + warnx(_("\nSession setup problem, abort.")); syslog(LOG_ERR, _("NULL user name in %s:%d. Abort."), __FUNCTION__, __LINE__); pam_end(pamh, PAM_SYSTEM_ERR); - exit(1); + exit(EXIT_FAILURE); } if (!(pwd = getpwnam(username))) { - fprintf(stderr, _("\nSession setup problem, abort.\n")); + warnx(_("\nSession setup problem, abort.")); syslog(LOG_ERR, _("Invalid user name \"%s\" in %s:%d. Abort."), username, __FUNCTION__, __LINE__); pam_end(pamh, PAM_SYSTEM_ERR); - exit(1); + exit(EXIT_FAILURE); } /* @@ -714,10 +712,10 @@ main(int argc, char **argv) pwd->pw_shell = strdup(pwd->pw_shell); if (!pwd->pw_name || !pwd->pw_passwd || !pwd->pw_gecos || !pwd->pw_dir || !pwd->pw_shell) { - fprintf(stderr, _("login: Out of memory\n")); + warnx(_("out of memory")); syslog(LOG_ERR, "Out of memory"); pam_end(pamh, PAM_SYSTEM_ERR); - exit(1); + exit(EXIT_FAILURE); } username = pwd->pw_name; @@ -728,9 +726,9 @@ main(int argc, char **argv) */ if (initgroups(username, pwd->pw_gid) < 0) { syslog(LOG_ERR, "initgroups: %m"); - fprintf(stderr, _("\nSession setup problem, abort.\n")); + warnx(_("\nSession setup problem, abort.")); pam_end(pamh, PAM_SYSTEM_ERR); - exit(1); + exit(EXIT_FAILURE); } retcode = pam_open_session(pamh, 0); @@ -760,7 +758,7 @@ main(int argc, char **argv) if (username[0] == '+') { puts(_("Illegal username")); badlogin(username); - sleepexit(1); + sleepexit(EXIT_FAILURE); } /* (void)strcpy(tbuf, username); why was this here? */ @@ -800,8 +798,7 @@ main(int argc, char **argv) * refuse the login attempt. */ if (pwd && pwd->pw_uid == 0 && !rootterm(tty_name)) { - fprintf(stderr, - _("%s login refused on this terminal.\n"), + warnx(_("%s login refused on this terminal."), pwd->pw_name); if (hostname) @@ -871,7 +868,7 @@ main(int argc, char **argv) /* we allow 10 tries, but after 3 we start backing off */ if (++cnt > 3) { if (cnt >= 10) { - sleepexit(1); + sleepexit(EXIT_FAILURE); } sleep((unsigned int)((cnt - 3) * 5)); } @@ -1170,11 +1167,10 @@ Michael Riepe child_pid = fork(); if (child_pid < 0) { - int errsv = errno; /* error in fork() */ - fprintf(stderr, _("login: failure forking: %s"), strerror(errsv)); + warn(_("failure forking")); PAM_END; - exit(0); + exit(EXIT_FAILURE); } if (child_pid) { @@ -1191,7 +1187,7 @@ Michael Riepe ; openlog("login", LOG_ODELAY, LOG_AUTHPRIV); PAM_END; - exit(0); + exit(EXIT_SUCCESS); } /* child */ @@ -1200,7 +1196,7 @@ Michael Riepe sigaction(SIGHUP, &oldsa_hup, NULL); sigaction(SIGTERM, &oldsa_term, NULL); if(got_sig) - exit(1); + exit(EXIT_FAILURE); /* * Problem: if the user's shell is a shell like ash that doesnt do @@ -1226,14 +1222,14 @@ Michael Riepe /* discard permissions last so can't get killed and drop core */ if(setuid(pwd->pw_uid) < 0 && pwd->pw_uid) { syslog(LOG_ALERT, _("setuid() failed")); - exit(1); + exit(EXIT_FAILURE); } /* wait until here to change directory! */ if (chdir(pwd->pw_dir) < 0) { - printf(_("No directory %s!\n"), pwd->pw_dir); + warn(_("%s: change directory failed"), pwd->pw_dir); if (chdir("/")) - exit(0); + exit(EXIT_FAILURE); pwd->pw_dir = "/"; printf(_("Logging in with home = \"/\".\n")); } @@ -1265,12 +1261,11 @@ Michael Riepe errsv = errno; if (!strcmp(childArgv[0], "/bin/sh")) - fprintf(stderr, _("login: couldn't exec shell script: %s.\n"), - strerror(errsv)); + warn(_("couldn't exec shell script")); else - fprintf(stderr, _("login: no shell: %s.\n"), strerror(errsv)); + warn(_("no shell")); - exit(0); + exit(EXIT_SUCCESS); } #ifndef HAVE_SECURITY_PAM_MISC_H @@ -1287,22 +1282,20 @@ getloginname(void) { for (p = nbuf; (ch = getchar()) != '\n'; ) { if (ch == EOF) { badlogin("EOF"); - exit(0); + exit(EXIT_FAILURE); } if (p < nbuf + UT_NAMESIZE) *p++ = ch; cnt++; if (cnt > UT_NAMESIZE + 20) { - fprintf(stderr, _("login name much too long.\n")); badlogin(_("NAME too long")); - exit(0); + errx(EXIT_FAILURE, _("login name much too long.")); } } if (p > nbuf) { if (nbuf[0] == '-') - fprintf(stderr, - _("login names may not start with '-'.\n")); + warnx(_("login names may not start with '-'.")); else { *p = '\0'; username = nbuf; @@ -1312,9 +1305,8 @@ getloginname(void) { cnt2++; if (cnt2 > 50) { - fprintf(stderr, _("too many bare linefeeds.\n")); badlogin(_("EXCESSIVE linefeeds")); - exit(0); + errx(EXIT_FAILURE, _("too many bare linefeeds.")); } } } @@ -1338,14 +1330,14 @@ timedout2(int sig) { tcgetattr(0, &ti); ti.c_lflag |= ECHO; tcsetattr(0, TCSANOW, &ti); - exit(0); /* %% */ + exit(EXIT_SUCCESS); /* %% */ } static void timedout(int sig) { signal(SIGALRM, timedout2); alarm(10); - fprintf(stderr, _("Login timed out after %d seconds\n"), timeout); + warnx(_("timed out after %d seconds"), timeout); signal(SIGALRM, SIG_IGN); alarm(0); timedout2(0); @@ -1375,10 +1367,10 @@ rootterm(char * ttyn) return 1; } else continue; - } else { + } else { close(fd); return 0; - } + } } } #endif /* !HAVE_SECURITY_PAM_MISC_H */ @@ -1416,7 +1408,7 @@ checknologin(void) { while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) write(fileno(stdout), tbuf, nchars); close(fd); - sleepexit(0); + sleepexit(EXIT_SUCCESS); } } #endif