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

« back to all changes in this revision

Viewing changes to misc-utils/write.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) 1989, 1993
3
 
 *      The Regents of the University of California.  All rights reserved.
4
 
 *
5
 
 * This code is derived from software contributed to Berkeley by
6
 
 * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
7
 
 *
8
 
 * Redistribution and use in source and binary forms, with or without
9
 
 * modification, are permitted provided that the following conditions
10
 
 * are met:
11
 
 * 1. Redistributions of source code must retain the above copyright
12
 
 *    notice, this list of conditions and the following disclaimer.
13
 
 * 2. Redistributions in binary form must reproduce the above copyright
14
 
 *    notice, this list of conditions and the following disclaimer in the
15
 
 *    documentation and/or other materials provided with the distribution.
16
 
 * 3. All advertising materials mentioning features or use of this software
17
 
 *    must display the following acknowledgement:
18
 
 *      This product includes software developed by the University of
19
 
 *      California, Berkeley and its contributors.
20
 
 * 4. Neither the name of the University nor the names of its contributors
21
 
 *    may be used to endorse or promote products derived from this software
22
 
 *    without specific prior written permission.
23
 
 *
24
 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34
 
 * SUCH DAMAGE.
35
 
 *
36
 
 * Modified for Linux, Mon Mar  8 18:16:24 1993, faith@cs.unc.edu
37
 
 * Wed Jun 22 21:41:56 1994, faith@cs.unc.edu:
38
 
 *      Added fix from Mike Grupenhoff (kashmir@umiacs.umd.edu)
39
 
 * Mon Jul  1 17:01:39 MET DST 1996, janl@math.uio.no:
40
 
 *      - Added fix from David.Chapell@mail.trincoll.edu enabeling daemons
41
 
 *        to use write.
42
 
 *      - ANSIed it since I was working on it anyway.
43
 
 * 1999-02-22 Arkadiusz Miļæ½kiewicz <misiek@pld.ORG.PL>
44
 
 * - added Native Language Support
45
 
 *
46
 
 */
47
 
 
48
 
#include <stdio.h>
49
 
#include <unistd.h>
50
 
#include <utmp.h>
51
 
#include <errno.h>
52
 
#include <ctype.h>
53
 
#include <time.h>
54
 
#include <pwd.h>
55
 
#include <string.h>
56
 
#include <stdlib.h>
57
 
#include <signal.h>
58
 
#include <sys/param.h>
59
 
#include <sys/stat.h>
60
 
#include <sys/file.h>
61
 
#include <sys/time.h>
62
 
#include <paths.h>
63
 
#include "pathnames.h"
64
 
#include "carefulputc.h"
65
 
#include "nls.h"
66
 
 
67
 
void search_utmp(char *, char *, char *, uid_t);
68
 
void do_write(char *, char *, uid_t);
69
 
void wr_fputs(char *);
70
 
static void done(int);
71
 
int term_chk(char *, int *, time_t *, int);
72
 
int utmp_chk(char *, char *);
73
 
 
74
 
static gid_t myegid;
75
 
 
76
 
int
77
 
main(int argc, char **argv) {
78
 
        time_t atime;
79
 
        uid_t myuid;
80
 
        int msgsok, myttyfd;
81
 
        char tty[MAXPATHLEN], *mytty;
82
 
 
83
 
        setlocale(LC_ALL, "");
84
 
        bindtextdomain(PACKAGE, LOCALEDIR);
85
 
        textdomain(PACKAGE);
86
 
 
87
 
        myegid = getegid();
88
 
 
89
 
        /* check that sender has write enabled */
90
 
        if (isatty(fileno(stdin)))
91
 
                myttyfd = fileno(stdin);
92
 
        else if (isatty(fileno(stdout)))
93
 
                myttyfd = fileno(stdout);
94
 
        else if (isatty(fileno(stderr)))
95
 
                myttyfd = fileno(stderr);
96
 
        else {
97
 
                myttyfd = -1;
98
 
        }
99
 
        if (myttyfd != -1) {
100
 
          if (!(mytty = ttyname(myttyfd))) {
101
 
                (void)fprintf(stderr, _("write: can't find your tty's name\n"));
102
 
                exit(1);
103
 
          }
104
 
          /* We may have /dev/ttyN but also /dev/pts/xx.
105
 
             Below, term_chk() will put "/dev/" in front, so remove that part. */
106
 
          if (!strncmp(mytty, "/dev/", 5))
107
 
                mytty += 5;
108
 
          if (term_chk(mytty, &msgsok, &atime, 1))
109
 
                exit(1);
110
 
          if (!msgsok) {
111
 
                (void)fprintf(stderr,
112
 
                    _("write: you have write permission turned off.\n"));
113
 
                exit(1);
114
 
          }
115
 
        
116
 
        } else {
117
 
            mytty = "<no tty>";
118
 
        }
119
 
 
120
 
        myuid = getuid();
121
 
 
122
 
        /* check args */
123
 
        switch (argc) {
124
 
        case 2:
125
 
                search_utmp(argv[1], tty, mytty, myuid);
126
 
                do_write(tty, mytty, myuid);
127
 
                break;
128
 
        case 3:
129
 
                if (!strncmp(argv[2], "/dev/", 5))
130
 
                        argv[2] += 5;
131
 
                if (utmp_chk(argv[1], argv[2])) {
132
 
                        (void)fprintf(stderr,
133
 
                            _("write: %s is not logged in on %s.\n"),
134
 
                            argv[1], argv[2]);
135
 
                        exit(1);
136
 
                }
137
 
                if (term_chk(argv[2], &msgsok, &atime, 1))
138
 
                        exit(1);
139
 
                if (myuid && !msgsok) {
140
 
                        (void)fprintf(stderr,
141
 
                            _("write: %s has messages disabled on %s\n"),
142
 
                            argv[1], argv[2]);
143
 
                        exit(1);
144
 
                }
145
 
                do_write(argv[2], mytty, myuid);
146
 
                break;
147
 
        default:
148
 
                (void)fprintf(stderr, _("usage: write user [tty]\n"));
149
 
                exit(1);
150
 
        }
151
 
        done(0);
152
 
        /* NOTREACHED */
153
 
        return 0;
154
 
}
155
 
 
156
 
 
157
 
/*
158
 
 * utmp_chk - checks that the given user is actually logged in on
159
 
 *     the given tty
160
 
 */
161
 
int utmp_chk(char *user, char *tty)
162
 
 
163
 
{
164
 
        struct utmp u;
165
 
        struct utmp *uptr;
166
 
        int res = 1;
167
 
 
168
 
        utmpname(_PATH_UTMP);
169
 
        setutent();
170
 
 
171
 
        while ((uptr = getutent())) {
172
 
                memcpy(&u, uptr, sizeof(u));
173
 
                if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
174
 
                    strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
175
 
                        res = 0;
176
 
                        break;
177
 
                }
178
 
        }
179
 
 
180
 
        endutent();
181
 
        return(res);
182
 
}
183
 
 
184
 
/*
185
 
 * search_utmp - search utmp for the "best" terminal to write to
186
 
 *
187
 
 * Ignores terminals with messages disabled, and of the rest, returns
188
 
 * the one with the most recent access time.  Returns as value the number
189
 
 * of the user's terminals with messages enabled, or -1 if the user is
190
 
 * not logged in at all.
191
 
 *
192
 
 * Special case for writing to yourself - ignore the terminal you're
193
 
 * writing from, unless that's the only terminal with messages enabled.
194
 
 */
195
 
void search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
196
 
 
197
 
{
198
 
        struct utmp u;
199
 
        struct utmp *uptr;
200
 
        time_t bestatime, atime;
201
 
        int nloggedttys, nttys, msgsok, user_is_me;
202
 
        char atty[sizeof(u.ut_line) + 1];
203
 
 
204
 
        utmpname(_PATH_UTMP);
205
 
        setutent();
206
 
 
207
 
        nloggedttys = nttys = 0;
208
 
        bestatime = 0;
209
 
        user_is_me = 0;
210
 
        while ((uptr = getutent())) {
211
 
                memcpy(&u, uptr, sizeof(u));
212
 
                if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
213
 
                        ++nloggedttys;
214
 
                        (void)strncpy(atty, u.ut_line, sizeof(u.ut_line));
215
 
                        atty[sizeof(u.ut_line)] = '\0';
216
 
                        if (term_chk(atty, &msgsok, &atime, 0))
217
 
                                continue;       /* bad term? skip */
218
 
                        if (myuid && !msgsok)
219
 
                                continue;       /* skip ttys with msgs off */
220
 
                        if (strcmp(atty, mytty) == 0) {
221
 
                                user_is_me = 1;
222
 
                                continue;       /* don't write to yourself */
223
 
                        }
224
 
                        if (u.ut_type != USER_PROCESS)
225
 
                                continue;       /* it's not a valid entry */
226
 
                        ++nttys;
227
 
                        if (atime > bestatime) {
228
 
                                bestatime = atime;
229
 
                                (void)strcpy(tty, atty);
230
 
                        }
231
 
                }
232
 
        }
233
 
 
234
 
        endutent();
235
 
        if (nloggedttys == 0) {
236
 
                (void)fprintf(stderr, _("write: %s is not logged in\n"), user);
237
 
                exit(1);
238
 
        }
239
 
        if (nttys == 0) {
240
 
                if (user_is_me) {               /* ok, so write to yourself! */
241
 
                        (void)strcpy(tty, mytty);
242
 
                        return;
243
 
                }
244
 
                (void)fprintf(stderr,
245
 
                    _("write: %s has messages disabled\n"), user);
246
 
                exit(1);
247
 
        } else if (nttys > 1) {
248
 
                (void)fprintf(stderr,
249
 
                    _("write: %s is logged in more than once; writing to %s\n"),
250
 
                    user, tty);
251
 
        }
252
 
}
253
 
 
254
 
/*
255
 
 * term_chk - check that a terminal exists, and get the message bit
256
 
 *     and the access time
257
 
 */
258
 
int term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
259
 
 
260
 
{
261
 
        struct stat s;
262
 
        char path[MAXPATHLEN];
263
 
 
264
 
        if (strlen(tty) + 6 > sizeof(path))
265
 
                return(1);
266
 
        (void)sprintf(path, "/dev/%s", tty);
267
 
        if (stat(path, &s) < 0) {
268
 
                if (showerror)
269
 
                        (void)fprintf(stderr,
270
 
                            "write: %s: %s\n", path, strerror(errno));
271
 
                return(1);
272
 
        }
273
 
 
274
 
        /* group write bit and group ownership */
275
 
        *msgsokP = (s.st_mode & (S_IWRITE >> 3)) && myegid == s.st_gid;
276
 
        *atimeP = s.st_atime;
277
 
        return(0);
278
 
}
279
 
 
280
 
/*
281
 
 * do_write - actually make the connection
282
 
 */
283
 
void do_write(char *tty, char *mytty, uid_t myuid) {
284
 
        char *login, *pwuid, *nows;
285
 
        struct passwd *pwd;
286
 
        time_t now;
287
 
        char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
288
 
 
289
 
        /* Determine our login name(s) before the we reopen() stdout */
290
 
        if ((pwd = getpwuid(myuid)) != NULL)
291
 
                pwuid = pwd->pw_name;
292
 
        else
293
 
                pwuid = "???";
294
 
        if ((login = getlogin()) == NULL)
295
 
                login = pwuid;
296
 
 
297
 
        if (strlen(tty) + 6 > sizeof(path))
298
 
                exit(1);
299
 
        (void)sprintf(path, "/dev/%s", tty);
300
 
        if ((freopen(path, "w", stdout)) == NULL) {
301
 
                (void)fprintf(stderr, "write: %s: %s\n",
302
 
                              path, strerror(errno));
303
 
                exit(1);
304
 
        }
305
 
 
306
 
        (void)signal(SIGINT, done);
307
 
        (void)signal(SIGHUP, done);
308
 
 
309
 
        /* print greeting */
310
 
        if (gethostname(host, sizeof(host)) < 0)
311
 
                (void)strcpy(host, "???");
312
 
        now = time((time_t *)NULL);
313
 
        nows = ctime(&now);
314
 
        nows[16] = '\0';
315
 
        printf("\r\n\007\007\007");
316
 
        if (strcmp(login, pwuid))
317
 
                (void)printf(_("Message from %s@%s (as %s) on %s at %s ..."),
318
 
                             login, host, pwuid, mytty, nows + 11);
319
 
        else
320
 
                (void)printf(_("Message from %s@%s on %s at %s ..."),
321
 
                             login, host, mytty, nows + 11);
322
 
        printf("\r\n");
323
 
 
324
 
        while (fgets(line, sizeof(line), stdin) != NULL)
325
 
                wr_fputs(line);
326
 
}
327
 
 
328
 
/*
329
 
 * done - cleanup and exit
330
 
 */
331
 
static void
332
 
done(int dummy) {
333
 
        (void)printf("EOF\r\n");
334
 
        _exit(0);
335
 
}
336
 
 
337
 
/*
338
 
 * wr_fputs - like fputs(), but makes control characters visible and
339
 
 *     turns \n into \r\n.
340
 
 */
341
 
void
342
 
wr_fputs(char *s) {
343
 
        char c;
344
 
 
345
 
#define PUTC(c) if (carefulputc(c,stdout) == EOF) goto err;
346
 
 
347
 
        while(*s) {
348
 
                c = *s++;
349
 
                if (c == '\n')
350
 
                        PUTC('\r');
351
 
                PUTC(c);
352
 
        }
353
 
        return;
354
 
 
355
 
err:
356
 
        fprintf(stderr, "write: %s\n", strerror(errno));
357
 
        exit(1);
358
 
#undef PUTC
359
 
}