]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - login-utils/lslogins.c
man pages: Change a HYPHEN-MINUS (-) to a minus (\-) for options and numbers
[thirdparty/util-linux.git] / login-utils / lslogins.c
index ea40ab615c600c18c52b214bcd613063cb62542c..3d9c9b97a21b8f715c391726997a285df83208ec 100644 (file)
@@ -74,7 +74,7 @@ static int lslogins_flag;
 
 #define UL_UID_MIN 1000
 #define UL_UID_MAX 60000
-#define UL_SYS_UID_MIN 201
+#define UL_SYS_UID_MIN 101
 #define UL_SYS_UID_MAX 999
 
 /* we use the value of outmode to determine
@@ -269,6 +269,7 @@ struct lslogins_control {
        const char *journal_path;
 
        unsigned int selinux_enabled : 1,
+                    fail_on_unknown : 1,               /* fail if user does not exist */
                     ulist_on : 1,
                     noheadings : 1,
                     notrunc : 1;
@@ -610,19 +611,25 @@ static const char *get_pwd_method(const char *str, const char **next, unsigned i
 
 #define is_valid_pwd_char(x)   (isalnum((unsigned char) (x)) || (x) ==  '.' || (x) == '/')
 
+/*
+ * This function do not accept empty passwords or locked accouns.
+ */
 static int valid_pwd(const char *str)
 {
        const char *p = str;
        unsigned int sz = 0, n;
 
+       if (!str || !*str)
+               return 0;
+
        /* $id$ */
        if (get_pwd_method(str, &p, &sz) == NULL)
                return 0;
-       if (!*p)
+       if (!p || !*p)
                return 0;
 
        /* salt$ */
-       for (; p && *p; p++) {
+       for (; *p; p++) {
                if (*p == '$') {
                        p++;
                        break;
@@ -634,7 +641,7 @@ static int valid_pwd(const char *str)
                return 0;
 
        /* encrypted */
-       for (n = 0; p && *p; p++, n++) {
+       for (n = 0; *p; p++, n++) {
                if (!is_valid_pwd_char(*p))
                        return 0;
        }
@@ -656,6 +663,7 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
        uid_t uid;
        errno = 0;
 
+       errno = 0;
        pwd = username ? getpwnam(username) : getpwent();
        if (!pwd)
                return NULL;
@@ -678,6 +686,7 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
                return NULL;
        }
 
+       errno = 0;
        grp = getgrgid(pwd->pw_gid);
        if (!grp)
                return NULL;
@@ -977,10 +986,16 @@ static int create_usertree(struct lslogins_control *ctl)
 
        if (ctl->ulist_on) {
                for (n = 0; n < ctl->ulsiz; n++) {
-                       if (get_user(ctl, &user, ctl->ulist[n]))
+                       int rc = get_user(ctl, &user, ctl->ulist[n]);
+
+                       if (ctl->fail_on_unknown && !user) {
+                               warnx(_("cannot found '%s'"), ctl->ulist[n]);
+                               return -1;
+                       }
+                       if (rc || !user)
                                continue;
-                       if (user) /* otherwise an invalid user name has probably been given */
-                               tsearch(user, &ctl->usertree, cmp_uid);
+
+                       tsearch(user, &ctl->usertree, cmp_uid);
                }
        } else {
                while ((user = get_next_user(ctl)))
@@ -1410,7 +1425,7 @@ int main(int argc, char *argv[])
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        ctl->time_mode = TIME_SHORT;
 
@@ -1513,8 +1528,7 @@ int main(int argc, char *argv[])
                        ctl->time_mode = parse_time_mode(optarg);
                        break;
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'Z':
                {
 #ifdef HAVE_LIBSELINUX
@@ -1537,6 +1551,7 @@ int main(int argc, char *argv[])
                        errx(EXIT_FAILURE, _("Only one user may be specified. Use -l for multiple users."));
                logins = argv[optind];
                outmode = OUT_PRETTY;
+               ctl->fail_on_unknown = 1;
        } else if (argc != optind)
                errx(EXIT_FAILURE, _("Only one user may be specified. Use -l for multiple users."));
 
@@ -1552,7 +1567,7 @@ int main(int argc, char *argv[])
                         columns[ncolumns++] = i;
 
        } else if (ncolumns == 2) {
-               /* default colummns */
+               /* default columns */
                add_column(columns, ncolumns++, COL_NPROCS);
                add_column(columns, ncolumns++, COL_PWDLOCK);
                add_column(columns, ncolumns++, COL_PWDDENY);