wall.c (56e7ae90cb45f7803a1821b1fd3178a04a99bdf3) | wall.c (6a20d55a591484ce93ad7002606c9ca4dbbd7ebb) |
---|---|
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 --- 42 unchanged lines hidden (view full) --- 51 */ 52 53#include <sys/param.h> 54#include <sys/stat.h> 55#include <sys/uio.h> 56 57#include <ctype.h> 58#include <err.h> | 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 --- 42 unchanged lines hidden (view full) --- 51 */ 52 53#include <sys/param.h> 54#include <sys/stat.h> 55#include <sys/uio.h> 56 57#include <ctype.h> 58#include <err.h> |
59#include <grp.h> |
|
59#include <locale.h> 60#include <paths.h> 61#include <pwd.h> 62#include <stdio.h> 63#include <stdlib.h> 64#include <string.h> 65#include <time.h> 66#include <unistd.h> 67#include <utmp.h> 68 | 60#include <locale.h> 61#include <paths.h> 62#include <pwd.h> 63#include <stdio.h> 64#include <stdlib.h> 65#include <string.h> 66#include <time.h> 67#include <unistd.h> 68#include <utmp.h> 69 |
69void makemsg __P((char *)); 70static void usage __P((void)); 71char *ttymsg __P((struct iovec *, int, char *, int)); | 70static void makemsg(char *); 71static void usage(void); 72char *ttymsg(struct iovec *, int, const char *, int); |
72 73#define IGNOREUSER "sleeper" 74 | 73 74#define IGNOREUSER "sleeper" 75 |
76struct wallgroup { 77 struct wallgroup *next; 78 char *name; 79 gid_t gid; 80} *grouplist; |
|
75int nobanner; 76int mbufsize; 77char *mbuf; 78 | 81int nobanner; 82int mbufsize; 83char *mbuf; 84 |
79/* ARGSUSED */ | |
80int | 85int |
81main(argc, argv) 82 int argc; 83 char **argv; | 86main(int argc, char *argv[]) |
84{ | 87{ |
85 int ch; | |
86 struct iovec iov; 87 struct utmp utmp; | 88 struct iovec iov; 89 struct utmp utmp; |
90 gid_t grps[NGROUPS_MAX]; 91 int ch; 92 int ingroup, ngrps, i; |
|
88 FILE *fp; | 93 FILE *fp; |
94 struct wallgroup *g; 95 struct group *grp; |
|
89 char *p; | 96 char *p; |
97 struct passwd *pw; |
|
90 char line[sizeof(utmp.ut_line) + 1]; | 98 char line[sizeof(utmp.ut_line) + 1]; |
99 char username[sizeof(utmp.ut_name) + 1]; |
|
91 | 100 |
101 ingroup = 0; |
|
92 (void)setlocale(LC_CTYPE, ""); 93 | 102 (void)setlocale(LC_CTYPE, ""); 103 |
94 while ((ch = getopt(argc, argv, "n")) != -1) | 104 while ((ch = getopt(argc, argv, "g:n")) != -1) |
95 switch (ch) { 96 case 'n': 97 /* undoc option for shutdown: suppress banner */ 98 if (geteuid() == 0) 99 nobanner = 1; 100 break; | 105 switch (ch) { 106 case 'n': 107 /* undoc option for shutdown: suppress banner */ 108 if (geteuid() == 0) 109 nobanner = 1; 110 break; |
111 case 'g': 112 g = (struct wallgroup *)malloc(sizeof *g); 113 g->next = grouplist; 114 g->name = optarg; 115 g->gid = -1; 116 grouplist = g; 117 break; |
|
101 case '?': 102 default: 103 usage(); 104 } 105 argc -= optind; 106 argv += optind; 107 if (argc > 1) 108 usage(); 109 | 118 case '?': 119 default: 120 usage(); 121 } 122 argc -= optind; 123 argv += optind; 124 if (argc > 1) 125 usage(); 126 |
127 for (g = grouplist; g; g = g->next) { 128 grp = getgrnam(g->name); 129 if (grp) 130 g->gid = grp->gr_gid; 131 } 132 |
|
110 makemsg(*argv); 111 112 if (!(fp = fopen(_PATH_UTMP, "r"))) 113 err(1, "cannot read %s", _PATH_UTMP); 114 iov.iov_base = mbuf; 115 iov.iov_len = mbufsize; 116 /* NOSTRICT */ 117 while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 118 if (!utmp.ut_name[0] || 119 !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 120 continue; | 133 makemsg(*argv); 134 135 if (!(fp = fopen(_PATH_UTMP, "r"))) 136 err(1, "cannot read %s", _PATH_UTMP); 137 iov.iov_base = mbuf; 138 iov.iov_len = mbufsize; 139 /* NOSTRICT */ 140 while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 141 if (!utmp.ut_name[0] || 142 !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 143 continue; |
144 if (grouplist) { 145 strlcpy(username, utmp.ut_name, sizeof(utmp.ut_name)); 146 pw = getpwnam(username); 147 if (!pw) 148 continue; 149 ngrps = getgroups(pw->pw_gid, grps); 150 for (g = grouplist; g && ingroup == 0; g = g->next) { 151 if (g->gid == -1) 152 continue; 153 if (g->gid == pw->pw_gid) 154 ingroup = 1; 155 for (i = 0; i < ngrps && ingroup == 0; i++) 156 if (g->gid == grps[i]) 157 ingroup = 1; 158 } 159 if (ingroup == 0) 160 continue; 161 } |
|
121 strncpy(line, utmp.ut_line, sizeof(utmp.ut_line)); 122 line[sizeof(utmp.ut_line)] = '\0'; 123 if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL) 124 warnx("%s", p); 125 } 126 exit(0); 127} 128 129static void 130usage() 131{ 132 (void)fprintf(stderr, "usage: wall [file]\n"); 133 exit(1); 134} 135 136void | 162 strncpy(line, utmp.ut_line, sizeof(utmp.ut_line)); 163 line[sizeof(utmp.ut_line)] = '\0'; 164 if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL) 165 warnx("%s", p); 166 } 167 exit(0); 168} 169 170static void 171usage() 172{ 173 (void)fprintf(stderr, "usage: wall [file]\n"); 174 exit(1); 175} 176 177void |
137makemsg(fname) 138 char *fname; | 178makemsg(char *fname) |
139{ | 179{ |
140 register int cnt; 141 register unsigned char ch; | 180 int cnt; 181 unsigned char ch; |
142 struct tm *lt; 143 struct passwd *pw; 144 struct stat sbuf; 145 time_t now; 146 FILE *fp; 147 int fd; 148 char *p, *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64]; 149 const char *whom; --- 94 unchanged lines hidden --- | 182 struct tm *lt; 183 struct passwd *pw; 184 struct stat sbuf; 185 time_t now; 186 FILE *fp; 187 int fd; 188 char *p, *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64]; 189 const char *whom; --- 94 unchanged lines hidden --- |