]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - term-utils/wall.c
Make the ways of using output stream consistent in usage()
[thirdparty/util-linux.git] / term-utils / wall.c
index 7d0dfe772d33fadefce2a631861e29fc1dc4eb87..a3fe7d29a6a79be5b1e8e1d0a7016d128630f51e 100644 (file)
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
-#include <utmp.h>
+#include <utmpx.h>
 #include <getopt.h>
+#include <sys/types.h>
+#include <grp.h>
+
+#if defined(USE_SYSTEMD) && HAVE_DECL_SD_SESSION_GET_USERNAME == 1
+# include <systemd/sd-login.h>
+# include <systemd/sd-daemon.h>
+#endif
 
 #include "nls.h"
 #include "xalloc.h"
 #include "pathnames.h"
 #include "carefulputc.h"
 #include "c.h"
+#include "cctype.h"
 #include "fileutils.h"
 #include "closestream.h"
+#include "timeutils.h"
+#include "pwdutils.h"
 
 #define        TERM_WIDTH      79
 #define        WRITE_TIME_OUT  300             /* in seconds */
@@ -76,8 +86,9 @@
 static char *makemsg(char *fname, char **mvec, int mvecsz,
                    size_t *mbufsize, int print_banner);
 
-static void __attribute__((__noreturn__)) usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
        fprintf(out,
              _(" %s [options] [<file> | <message>]\n"), program_invocation_short_name);
@@ -86,24 +97,101 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
        fputs(_("Write a message to all users.\n"), out);
 
        fputs(USAGE_OPTIONS, out);
+       fputs(_(" -g, --group <group>     only send message to group\n"), out);
        fputs(_(" -n, --nobanner          do not print banner, works only for root\n"), out);
        fputs(_(" -t, --timeout <timeout> write timeout in seconds\n"), out);
        fputs(USAGE_SEPARATOR, out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
+       fprintf(out, USAGE_HELP_OPTIONS(25));
        fprintf(out, USAGE_MAN_TAIL("wall(1)"));
 
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       exit(EXIT_SUCCESS);
+}
+
+struct group_workspace {
+       gid_t   requested_group;
+       int     ngroups;
+
+/* getgrouplist() on OSX takes int* not gid_t* */
+#ifdef __APPLE__
+       int     *groups;
+#else
+       gid_t   *groups;
+#endif
+};
+
+static gid_t get_group_gid(const char *group)
+{
+       struct group *gr;
+       gid_t gid;
+
+       if ((gr = getgrnam(group)))
+               return gr->gr_gid;
+
+       gid = strtou32_or_err(group, _("invalid group argument"));
+       if (!getgrgid(gid))
+               errx(EXIT_FAILURE, _("%s: unknown gid"), group);
+
+       return gid;
+}
+
+static struct group_workspace *init_group_workspace(const char *group)
+{
+       struct group_workspace *buf = xmalloc(sizeof(struct group_workspace));
+
+       buf->requested_group = get_group_gid(group);
+       buf->ngroups = sysconf(_SC_NGROUPS_MAX) + 1;  /* room for the primary gid */
+       buf->groups = xcalloc(sizeof(*buf->groups), buf->ngroups);
+
+       return buf;
+}
+
+static void free_group_workspace(struct group_workspace *buf)
+{
+       if (!buf)
+               return;
+
+       free(buf->groups);
+       free(buf);
+}
+
+static int is_gr_member(const char *login, const struct group_workspace *buf)
+{
+       struct passwd *pw;
+       int ngroups = buf->ngroups;
+       int rc;
+
+       pw = getpwnam(login);
+       if (!pw)
+               return 0;
+
+       if (buf->requested_group == pw->pw_gid)
+               return 1;
+
+       rc = getgrouplist(login, pw->pw_gid, buf->groups, &ngroups);
+       if (rc < 0) {
+               /* buffer too small, not sure how this can happen, since
+                  we used sysconf to get the size... */
+               errx(EXIT_FAILURE,
+                       _("getgrouplist found more groups than sysconf allows"));
+       }
+
+       for (; ngroups >= 0; --ngroups) {
+               if (buf->requested_group == (gid_t) buf->groups[ngroups])
+                       return 1;
+       }
+
+       return 0;
 }
 
 int main(int argc, char **argv)
 {
        int ch;
        struct iovec iov;
-       struct utmp *utmpptr;
+       struct utmpx *utmpptr;
        char *p;
        char line[sizeof(utmpptr->ut_line) + 1];
        int print_banner = TRUE;
+       struct group_workspace *group_buf = NULL;
        char *mbuf, *fname = NULL;
        size_t mbufsize;
        unsigned timeout = WRITE_TIME_OUT;
@@ -111,19 +199,20 @@ int main(int argc, char **argv)
        int mvecsz = 0;
 
        static const struct option longopts[] = {
-               { "nobanner",   no_argument,            0, 'n' },
-               { "timeout",    required_argument,      0, 't' },
-               { "version",    no_argument,            0, 'V' },
-               { "help",       no_argument,            0, 'h' },
-               { NULL, 0, 0, 0 }
+               { "nobanner",   no_argument,            NULL, 'n' },
+               { "timeout",    required_argument,      NULL, 't' },
+               { "group",      required_argument,      NULL, 'g' },
+               { "version",    no_argument,            NULL, 'V' },
+               { "help",       no_argument,            NULL, 'h' },
+               { NULL, 0, NULL, 0 }
        };
 
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
-       while ((ch = getopt_long(argc, argv, "nt:Vh", longopts, NULL)) != -1) {
+       while ((ch = getopt_long(argc, argv, "nt:g:Vh", longopts, NULL)) != -1) {
                switch (ch) {
                case 'n':
                        if (geteuid() == 0)
@@ -136,13 +225,16 @@ int main(int argc, char **argv)
                        if (timeout < 1)
                                errx(EXIT_FAILURE, _("invalid timeout argument: %s"), optarg);
                        break;
+               case 'g':
+                       group_buf = init_group_workspace(optarg);
+                       break;
+
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       exit(EXIT_SUCCESS);
+                       print_version(EXIT_SUCCESS);
                case 'h':
-                       usage(stdout);
+                       usage();
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
        }
        argc -= optind;
@@ -159,124 +251,95 @@ int main(int argc, char **argv)
 
        iov.iov_base = mbuf;
        iov.iov_len = mbufsize;
-       while((utmpptr = getutent())) {
-               if (!utmpptr->ut_user[0])
-                       continue;
-#ifdef USER_PROCESS
-               if (utmpptr->ut_type != USER_PROCESS)
-                       continue;
-#endif
-               /* Joey Hess reports that use-sessreg in /etc/X11/wdm/
-                  produces ut_line entries like :0, and a write
-                  to /dev/:0 fails. */
-               if (utmpptr->ut_line[0] == ':')
-                       continue;
-
-               xstrncpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line));
-               if ((p = ttymsg(&iov, 1, line, timeout)) != NULL)
-                       warnx("%s", p);
-       }
-       endutent();
-       free(mbuf);
-       exit(EXIT_SUCCESS);
-}
 
-struct buffer {
-       size_t  sz;
-       size_t  used;
-       char    *data;
-};
+#if defined(USE_SYSTEMD) && HAVE_DECL_SD_SESSION_GET_USERNAME == 1
+       if (sd_booted() > 0) {
+               char **sessions_list;
+               int sessions;
 
-static void buf_enlarge(struct buffer *bs, size_t len)
-{
-       if (bs->sz == 0 || len > bs->sz - bs->used) {
-               bs->sz += len < 128 ? 128 : len;
-               bs->data = xrealloc(bs->data, bs->sz);
-       }
-}
+               sessions = sd_get_sessions(&sessions_list);
+               if (sessions < 0)
+                       errx(EXIT_FAILURE, _("error getting sessions: %s"),
+                               strerror(-sessions));
 
-static void buf_puts(struct buffer *bs, const char *s)
-{
-       size_t len = strlen(s);
+               for (int i = 0; i < sessions; i++) {
+                       char *name, *tty;
+                       int r;
 
-       buf_enlarge(bs, len + 1);
-       memcpy(bs->data + bs->used, s, len + 1);
-       bs->used += len;
-}
+                       if ((r = sd_session_get_username(sessions_list[i], &name)) < 0)
+                               errx(EXIT_FAILURE, _("get user name failed: %s"), strerror (-r));
 
-static void buf_printf(struct buffer *bs, const char *fmt, ...)
-{
-       int rc;
-       va_list ap;
-       size_t limit;
-
-       buf_enlarge(bs, 0);     /* default size */
-       limit = bs->sz - bs->used;
-
-       va_start(ap, fmt);
-       rc = vsnprintf(bs->data + bs->used, limit, fmt, ap);
-       va_end(ap);
-
-       if (rc > 0 && (size_t) rc + 1 > limit) {        /* not enoght, enlarge */
-               buf_enlarge(bs, rc + 1);
-               limit = bs->sz - bs->used;
-               va_start(ap, fmt);
-               rc = vsnprintf(bs->data  + bs->used, limit, fmt, ap);;
-               va_end(ap);
-       }
+                       if (!(group_buf && !is_gr_member(name, group_buf))) {
+                               if (sd_session_get_tty(sessions_list[i], &tty) >= 0) {
+                                       if ((p = ttymsg(&iov, 1, tty, timeout)) != NULL)
+                                               warnx("%s", p);
 
-       if (rc > 0)
-               bs->used += rc;
-}
+                                       free(tty);
+                               }
+                       }
+                       free(name);
+                       free(sessions_list[i]);
+               }
+               free(sessions_list);
+       } else
+#endif
+       {
+               while ((utmpptr = getutxent())) {
+                       if (!utmpptr->ut_user[0])
+                               continue;
+#ifdef USER_PROCESS
+                       if (utmpptr->ut_type != USER_PROCESS)
+                               continue;
+#endif
+                       /* Joey Hess reports that use-sessreg in /etc/X11/wdm/ produces
+                        * ut_line entries like :0, and a write to /dev/:0 fails.
+                        *
+                        * It also seems that some login manager may produce empty ut_line.
+                        */
+                       if (!*utmpptr->ut_line || *utmpptr->ut_line == ':')
+                               continue;
 
-static void buf_putc_careful(struct buffer *bs, int c)
-{
-       if (isprint(c) || c == '\a' || c == '\t' || c == '\r' || c == '\n') {
-               buf_enlarge(bs, 1);
-               bs->data[bs->used++] = c;
-       } else if (!isascii(c))
-               buf_printf(bs, "\\%3o", (unsigned char)c);
-       else {
-               char tmp[] = { '^', c ^ 0x40, '\0' };
-               buf_puts(bs, tmp);
+                       if (group_buf && !is_gr_member(utmpptr->ut_user, group_buf))
+                               continue;
+
+                       mem2strcpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line), sizeof(line));
+                       if ((p = ttymsg(&iov, 1, line, timeout)) != NULL)
+                               warnx("%s", p);
+               }
+               endutxent();
        }
+
+       free(mbuf);
+       free_group_workspace(group_buf);
+       exit(EXIT_SUCCESS);
 }
 
 static char *makemsg(char *fname, char **mvec, int mvecsz,
                     size_t *mbufsize, int print_banner)
 {
-       struct buffer _bs = {.used = 0}, *bs = &_bs;
-       register int ch, cnt;
-       char *p, *lbuf;
-       long line_max;
-
-       line_max = sysconf(_SC_LINE_MAX);
-       if (line_max <= 0)
-               line_max = 512;
-
-       lbuf = xmalloc(line_max);
+       char *lbuf, *retbuf;
+       FILE * fs = open_memstream(&retbuf, mbufsize);
+       size_t lbuflen = 512;
+       lbuf = xmalloc(lbuflen);
 
        if (print_banner == TRUE) {
                char *hostname = xgethostname();
-               char *whom, *where, *date;
-               struct passwd *pw;
+               char *whom, *where, date[CTIME_BUFSIZ];
                time_t now;
 
-               if (!(whom = getlogin()) || !*whom)
-                       whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
+               whom = xgetlogin();
                if (!whom) {
-                       whom = "someone";
+                       whom = "<someone>";
                        warn(_("cannot get passwd uid"));
                }
                where = ttyname(STDOUT_FILENO);
                if (!where) {
                        where = "somewhere";
-                       warn(_("cannot get tty name"));
                } else if (strncmp(where, "/dev/", 5) == 0)
                        where += 5;
 
                time(&now);
-               date = xstrdup(ctime(&now));
+               ctime_r(&now, date);
                date[strlen(date) - 1] = '\0';
 
                /*
@@ -288,16 +351,15 @@ static char *makemsg(char *fname, char **mvec, int mvecsz,
                 */
                /* snprintf is not always available, but the sprintf's here
                   will not overflow as long as %d takes at most 100 chars */
-               buf_printf(bs, "\r%*s\r\n", TERM_WIDTH, " ");
+               fprintf(fs, "\r%*s\r\n", TERM_WIDTH, " ");
 
-               snprintf(lbuf, line_max,
+               snprintf(lbuf, lbuflen,
                                _("Broadcast message from %s@%s (%s) (%s):"),
                                whom, hostname, where, date);
-               buf_printf(bs, "%-*.*s\007\007\r\n", TERM_WIDTH, TERM_WIDTH, lbuf);
+               fprintf(fs, "%-*.*s\007\007\r\n", TERM_WIDTH, TERM_WIDTH, lbuf);
                free(hostname);
-               free(date);
        }
-       buf_printf(bs, "%*s\r\n", TERM_WIDTH, " ");
+       fprintf(fs, "%*s\r\n", TERM_WIDTH, " ");
 
         if (mvec) {
                /*
@@ -306,11 +368,11 @@ static char *makemsg(char *fname, char **mvec, int mvecsz,
                int i;
 
                for (i = 0; i < mvecsz; i++) {
-                       buf_puts(bs, mvec[i]);
+                       fputs(mvec[i], fs);
                        if (i < mvecsz - 1)
-                               buf_puts(bs, " ");
+                               fputc(' ', fs);
                }
-               buf_puts(bs, "\r\n");
+               fputs("\r\n", fs);
        } else {
                /*
                 * read message from <file>
@@ -335,26 +397,13 @@ static char *makemsg(char *fname, char **mvec, int mvecsz,
                /*
                 * Read message from stdin.
                 */
-               while (fgets(lbuf, line_max, stdin)) {
-                       for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
-                               if (cnt == TERM_WIDTH || ch == '\n') {
-                                       for (; cnt < TERM_WIDTH; ++cnt)
-                                               buf_puts(bs, " ");
-                                       buf_puts(bs, "\r\n");
-                                       cnt = 0;
-                               }
-                               if (ch == '\t')
-                                       cnt += (7 - (cnt % 8));
-                               if (ch != '\n')
-                                       buf_putc_careful(bs, ch);
-                       }
-               }
+               while (getline(&lbuf, &lbuflen, stdin) >= 0)
+                       fputs_careful(lbuf, fs, '^', true, TERM_WIDTH);
        }
-       buf_printf(bs, "%*s\r\n", TERM_WIDTH, " ");
+       fprintf(fs, "%*s\r\n", TERM_WIDTH, " ");
 
        free(lbuf);
 
-       bs->data[bs->used] = '\0';      /* be paranoid */
-       *mbufsize = bs->used;
-       return bs->data;
+       fclose(fs);
+       return retbuf;
 }