~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to login-utils/wall.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
Import upstream version 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 1988, 1990, 1993
3
 
 *      The Regents of the University of California.  All rights reserved.
4
 
 *
5
 
 * Redistribution and use in source and binary forms, with or without
6
 
 * modification, are permitted provided that the following conditions
7
 
 * are met:
8
 
 * 1. Redistributions of source code must retain the above copyright
9
 
 *    notice, this list of conditions and the following disclaimer.
10
 
 * 2. Redistributions in binary form must reproduce the above copyright
11
 
 *    notice, this list of conditions and the following disclaimer in the
12
 
 *    documentation and/or other materials provided with the distribution.
13
 
 * 3. All advertising materials mentioning features or use of this software
14
 
 *    must display the following acknowledgement:
15
 
 *      This product includes software developed by the University of
16
 
 *      California, Berkeley and its contributors.
17
 
 * 4. Neither the name of the University nor the names of its contributors
18
 
 *    may be used to endorse or promote products derived from this software
19
 
 *    without specific prior written permission.
20
 
 *
21
 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 
 * SUCH DAMAGE.
32
 
 *
33
 
 * Modified Sun Mar 12 10:34:34 1995, faith@cs.unc.edu, for Linux
34
 
 */
35
 
 
36
 
/*
37
 
 * This program is not related to David Wall, whose Stanford Ph.D. thesis
38
 
 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
39
 
 *
40
 
 * 1999-02-22 Arkadiusz Mi�kiewicz <misiek@pld.ORG.PL>
41
 
 * - added Native Language Support
42
 
 *
43
 
 */
44
 
 
45
 
#include <sys/param.h>
46
 
#include <sys/stat.h>
47
 
#include <sys/time.h>
48
 
#include <sys/uio.h>
49
 
 
50
 
#include <err.h>
51
 
#include <errno.h>
52
 
#include <paths.h>
53
 
#include <ctype.h>
54
 
#include <pwd.h>
55
 
#include <stdio.h>
56
 
#include <stdlib.h>
57
 
#include <string.h>
58
 
#include <time.h>
59
 
#include <unistd.h>
60
 
#include <utmp.h>
61
 
 
62
 
#include "nls.h"
63
 
#include "xalloc.h"
64
 
#include "strutils.h"
65
 
#include "ttymsg.h"
66
 
#include "pathnames.h"
67
 
#include "carefulputc.h"
68
 
 
69
 
void    makemsg __P((char *));
70
 
 
71
 
#define IGNOREUSER      "sleeper"
72
 
 
73
 
#ifndef MAXHOSTNAMELEN
74
 
# ifdef HOST_NAME_MAX
75
 
#  define MAXHOSTNAMELEN HOST_NAME_MAX
76
 
# else
77
 
#  define MAXHOSTNAMELEN 64
78
 
# endif
79
 
#endif
80
 
 
81
 
int nobanner;
82
 
int mbufsize;
83
 
char *mbuf;
84
 
 
85
 
static void __attribute__((__noreturn__)) usage(void)
86
 
{
87
 
        errx(EXIT_FAILURE, _("usage: %s [-n] [file]\n"),
88
 
             program_invocation_short_name);
89
 
}
90
 
 
91
 
int
92
 
main(int argc, char **argv) {
93
 
        extern int optind;
94
 
        int ch;
95
 
        struct iovec iov;
96
 
        struct utmp *utmpptr;
97
 
        char *p;
98
 
        char line[sizeof(utmpptr->ut_line) + 1];
99
 
 
100
 
        setlocale(LC_ALL, "");
101
 
        bindtextdomain(PACKAGE, LOCALEDIR);
102
 
        textdomain(PACKAGE);
103
 
 
104
 
        while ((ch = getopt(argc, argv, "n")) != -1) {
105
 
                switch (ch) {
106
 
                case 'n':
107
 
                        if (geteuid() == 0)
108
 
                                nobanner = 1;
109
 
                        break;
110
 
                case '?':
111
 
                default:
112
 
                        usage();
113
 
                }
114
 
        }
115
 
        argc -= optind;
116
 
        argv += optind;
117
 
        if (argc > 1)
118
 
                usage();
119
 
 
120
 
        makemsg(*argv);
121
 
 
122
 
        setutent();
123
 
 
124
 
        iov.iov_base = mbuf;
125
 
        iov.iov_len = mbufsize;
126
 
        while((utmpptr = getutent())) {
127
 
                if (!utmpptr->ut_name[0] ||
128
 
                    !strncmp(utmpptr->ut_name, IGNOREUSER,
129
 
                             sizeof(utmpptr->ut_name)))
130
 
                        continue;
131
 
#ifdef USER_PROCESS
132
 
                if (utmpptr->ut_type != USER_PROCESS)
133
 
                        continue;
134
 
#endif
135
 
 
136
 
                /* Joey Hess reports that use-sessreg in /etc/X11/wdm/
137
 
                   produces ut_line entries like :0, and a write
138
 
                   to /dev/:0 fails. */
139
 
                if (utmpptr->ut_line[0] == ':')
140
 
                        continue;
141
 
 
142
 
                xstrncpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line));
143
 
                if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
144
 
                        warnx("%s", p);
145
 
        }
146
 
        endutent();
147
 
        exit(EXIT_SUCCESS);
148
 
}
149
 
 
150
 
void
151
 
makemsg(fname)
152
 
        char *fname;
153
 
{
154
 
        register int ch, cnt;
155
 
        struct tm *lt;
156
 
        struct passwd *pw;
157
 
        struct stat sbuf;
158
 
        time_t now;
159
 
        FILE *fp;
160
 
        int fd;
161
 
        char *p, *whom, *where, hostname[MAXHOSTNAMELEN],
162
 
                lbuf[MAXHOSTNAMELEN + 320],
163
 
                tmpname[sizeof(_PATH_TMP) + 20];
164
 
 
165
 
        sprintf(tmpname, "%s/wall.XXXXXX", _PATH_TMP);
166
 
        if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+")))
167
 
                errx(EXIT_FAILURE, _("can't open temporary file"));
168
 
 
169
 
        unlink(tmpname);
170
 
 
171
 
        if (!nobanner) {
172
 
                if (!(whom = getlogin()) || !*whom)
173
 
                        whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
174
 
                if (!whom || strlen(whom) > 100)
175
 
                        whom = "someone";
176
 
                where = ttyname(2);
177
 
                if (!where || strlen(where) > 100)
178
 
                        where = "somewhere";
179
 
                gethostname(hostname, sizeof(hostname));
180
 
                time(&now);
181
 
                lt = localtime(&now);
182
 
 
183
 
                /*
184
 
                 * all this stuff is to blank out a square for the message;
185
 
                 * we wrap message lines at column 79, not 80, because some
186
 
                 * terminals wrap after 79, some do not, and we can't tell.
187
 
                 * Which means that we may leave a non-blank character
188
 
                 * in column 80, but that can't be helped.
189
 
                 */
190
 
                /* snprintf is not always available, but the sprintf's here
191
 
                   will not overflow as long as %d takes at most 100 chars */
192
 
                fprintf(fp, "\r%79s\r\n", " ");
193
 
                sprintf(lbuf, _("Broadcast Message from %s@%s"),
194
 
                              whom, hostname);
195
 
                fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
196
 
                sprintf(lbuf, "        (%s) at %d:%02d ...",
197
 
                              where, lt->tm_hour, lt->tm_min);
198
 
                fprintf(fp, "%-79.79s\r\n", lbuf);
199
 
        }
200
 
        fprintf(fp, "%79s\r\n", " ");
201
 
 
202
 
        if (fname) {
203
 
                /*
204
 
                 * When we are not root, but suid or sgid, refuse to read files
205
 
                 * (e.g. device files) that the user may not have access to.
206
 
                 * After all, our invoker can easily do "wall < file"
207
 
                 * instead of "wall file".
208
 
                 */
209
 
                int uid = getuid();
210
 
                if (uid && (uid != geteuid() || getgid() != getegid()))
211
 
                        errx(EXIT_FAILURE, _("will not read %s - use stdin."),
212
 
                             fname);
213
 
 
214
 
                if (!freopen(fname, "r", stdin))
215
 
                        errx(EXIT_FAILURE, _("can't read %s."), fname);
216
 
        }
217
 
 
218
 
        while (fgets(lbuf, sizeof(lbuf), stdin)) {
219
 
                for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
220
 
                        if (cnt == 79 || ch == '\n') {
221
 
                                for (; cnt < 79; ++cnt)
222
 
                                        putc(' ', fp);
223
 
                                putc('\r', fp);
224
 
                                putc('\n', fp);
225
 
                                cnt = 0;
226
 
                        }
227
 
                        if (ch != '\n')
228
 
                                carefulputc(ch, fp);
229
 
                }
230
 
        }
231
 
        fprintf(fp, "%79s\r\n", " ");
232
 
        rewind(fp);
233
 
 
234
 
        if (fstat(fd, &sbuf))
235
 
                err(EXIT_FAILURE, _("fstat failed"));
236
 
 
237
 
        mbufsize = sbuf.st_size;
238
 
        mbuf = xmalloc(mbufsize);
239
 
 
240
 
        if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
241
 
                err(EXIT_FAILURE, _("fread failed"));
242
 
 
243
 
        close(fd);
244
 
}