1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/* ROUTINES COMMON BETWEEN WRITE, AMIN, MESG, ETC */
#include "orville.h"
#include <sys/stat.h>
/* CONFIGURATION OPTION TABLE - various commands that can be set in the
* orville.conf file. The options are actually stored in the global variables
* declared here (with their default values). The conftab[] structure is used
* by the readconf() program to load them up easily.
*/
#define CO_OPT 0 /* 'options' command */
#define CO_STR 1 /* String-valued command */
#define CO_FLG 2 /* Flag-valued command */
#define CO_INT 3 /* Int-valued command */
char *f_wrttmp= D_WRTTMP; /* The who's-talking-to-whom file */
char *f_wrthist= D_WRTHIST; /* The who-talked-to-whom file */
char *f_novicehelp= NULL; /* Type this if "NOVICE" env var is defined */
char *f_log= NULL; /* Log for all write connections */
char *f_helperlist= NULL; /* File of users who may be helpers */
char *f_helpername= "help"; /* Name you write to get help */
char *f_nohelp= NULL; /* File to print if no helpers available */
int f_disconnect= TRUE; /* Does 'mesg d' disconnect users? */
int f_exceptions= TRUE; /* Do 'mesg ne' and 'mesg ye' work? */
int f_fromhost= FALSE; /* Include hostname in 'Message from' banner? */
int f_helpers= FALSE; /* Does 'write help' work? */
int f_pipes= TRUE; /* Can I cat files through write? */
int f_loglevel= 0; /* How much logging to do? */
int f_answertel= 240; /* How many seconds to answer telegrams? */
struct { char *cmd; int len; int type; void *var; } conftab[]= {
{"answertel", 9, CO_INT, (void *)&f_answertel},
{"disconnect", 10, CO_FLG, (void *)&f_disconnect},
{"exceptions", 10, CO_FLG, (void *)&f_exceptions},
{"fromhost", 8, CO_FLG, (void *)&f_fromhost},
{"helperlist", 10, CO_STR, (void *)&f_helperlist},
{"helpername", 10, CO_STR, (void *)&f_helpername},
{"helpers", 7, CO_FLG, (void *)&f_helpers},
{"log", 3, CO_STR, (void *)&f_log},
{"loglevel", 8, CO_INT, (void *)&f_loglevel},
{"nohelp", 6, CO_STR, (void *)&f_nohelp},
{"novicehelp", 10, CO_STR, (void *)&f_novicehelp},
{"options", 7, CO_OPT, NULL},
{"pipes", 5, CO_FLG, (void *)&f_pipes},
{"wrthist", 7, CO_STR, (void *)&f_wrthist},
{"wrttmp", 6, CO_STR, (void *)&f_wrttmp},
{NULL, 0, 0, NULL}};
char *progname; /* The name of this program */
int wstream; /* File descriptor for open wrttmp file */
struct wrthdr wt_head; /* Header read out of wrttmp file */
char mydevname[UT_LINESIZE+10]; /* my tty name in /dev/tty?? format */
/* LEAFNAME: return pointer to last component of a pathname */
char *leafname(char *fullpath)
{
char *leaf;
if ((leaf= strrchr(fullpath,'/')) == NULL)
leaf= fullpath;
else
leaf++;
return(leaf);
}
/* GETDEVTTY: Store the ttyname in the global mydevname variable.
* Print and error message and return non-zero if stderr not a tty open for
* read. This is to prevent people from pretending they are writing from a
* terminal other than their own by redirecting stderr to fool ttyname().
* They are unlikely to have read access to anyone else's tty.
*/
int getdevtty()
{
char *tty;
if (!(fcntl(2,F_GETFL,0) & O_RDWR) || !isatty(2))
{
printf("%s: stderr improperly redirected\n",progname);
return 1;
}
if ((tty= ttyname(2)) == NULL || strlen(tty) < 5)
{
printf("%s: Not on a valid tty\n");
return 2;
}
strncpy(mydevname,tty,UT_LINESIZE+10);
return 0;
}
/* INIT_WSTREAM - open the wrttmp file, loading and checking the header.
* Mode can be either O_RDONLY or O_RDWR. Prints error message and returns
* true if it fails.
*/
int init_wstream(int mode)
{
if ((wstream= open(f_wrttmp,mode)) < 0)
{
printf("%s: Unable to open %s to read/write\n", progname, f_wrttmp);
return(1);
}
fcntl(wstream,F_SETFD,1); /* Close over execs */
/* Read in wrttmp file header */
if (read(wstream,&wt_head,sizeof(struct wrthdr)) == sizeof(struct wrthdr))
{
if ( wt_head.hdr_size < sizeof(struct wrthdr) ||
wt_head.tmp_size < sizeof(struct wrttmp) )
{
printf("%s: %s file has undersized entries. Old version?\n",
progname, f_wrttmp);
return(1);
}
}
else
{
/* Initialize header for new wrttmp file */
wt_head.hdr_size= sizeof(struct wrthdr);
wt_head.tmp_size= sizeof(struct wrttmp);
if (mode != O_RDONLY)
{
lseek(wstream, 0L, 0);
write(wstream, &wt_head, sizeof(struct wrthdr));
lseek(wstream, 0L, 1); /* must seek between write and read */
}
}
return(0);
}
/* FIND_UTMP -- Find the named tty in the utmp file, returning NULL if we
* fail. The tty name need not be null terminated.
*/
struct utmp *find_utmp(char *tty)
{
struct utmp tmputmp;
strncpy(tmputmp.ut_line, tty, UT_LINESIZE);
setutent(); /* open and/or rewind */
return getutline(&tmputmp);
}
/* FIND_WRTTMP: Find the named tty in the wrttmp file and return the wrttmp
* field and the offset of that line's entry. If the entry's login time
* doesn't match the given one, or if there is no entry in the file, then
* return a default wrttmp entry instead. In the latter case, the position
* returned will be the offset of the first blank slot at the end of the file.
*/
void find_wrttmp(char *tty, time_t time,struct wrttmp *wbuf, long *pos)
{
register int slot;
slot= 0;
for(;;)
{
lseek(wstream, *pos= wrttmp_offset(slot++),0);
if (read(wstream, wbuf, sizeof(struct wrttmp)) != sizeof(struct wrttmp))
break;
if (!strncmp(tty, wbuf->wrt_line, UT_LINESIZE))
{
if (wbuf->wrt_time == time)
return;
else
break;
}
}
/* Set wbuf to default value */
dflt_wrttmp(wbuf,tty,time);
}
/* DFLT_WRTTMP -- set a wrttmp entry to default status */
void dflt_wrttmp(struct wrttmp *wbuf, char *tty, time_t time)
{
/* Set wbuf to default value */
strncpy(wbuf->wrt_line,tty,UT_LINESIZE);
wbuf->wrt_time= time;
wbuf->wrt_what[0]= '\0';
wbuf->wrt_last[0]= '\0';
wbuf->wrt_pid= -1;
wbuf->wrt_record= DFLT_RECO;
wbuf->wrt_telpref= DFLT_PREF;
wbuf->wrt_modepref= DFLT_MODE;
wbuf->wrt_bells= DFLT_BELL;
#ifndef TTYPERMS
wbuf->wrt_mesg= DFLT_MESG;
#endif /*TTYPERMS*/
wbuf->wrt_help= DFLT_HELP;
wbuf->wrt_except= DFLT_EXCP;
}
#ifdef TTYPERMS
int myperms; /* my original tty perms */
extern char mydevname[];
/* SAVEPERMS: Remember the current tty permissions of my terminal. Returns
* 1 on failure.
*/
int saveperms()
{
struct stat stb;
if (stat(mydevname,&stb))
{
printf("%s: Panic - can't stat %s\n",progname,mydevname);
return(1);
}
myperms= stb.st_mode;
return(0);
}
/* SETPERMS: Set the tty permissions to yes (perm='y'), no (perm='n'), or
* leave them alone (perm='s').
*/
void setperms(char perm)
{
if (perm == 'n')
chmod(mydevname,PERMS_OFF);
if (perm == 'y')
chmod(mydevname,PERMS_ON);
}
/* RESETPERMS: Set the tty permissions back to saved state.
*/
void resetperms()
{
chmod(mydevname,myperms);
}
#endif /*TTYPERMS*/
/* READCONFIG: This reads through the orville.conf file loading configuration
* settings. It calls set_dflt() on any 'options' lines it sees, and saves
* all configuration filenames. If set_dflt is NULL, it isn't called.
*/
void readconfig(void (*set_dflt)(char *))
{
FILE *fp;
#define BFSZ 1024
char buf[BFSZ+1];
int i;
char *p, *q;
if ((fp= fopen(ORVILLE_CONF,"r")) == NULL)
{
fprintf(stderr,"Unable to open "ORVILLE_CONF" to read\n");
exit(1);
}
while (fgets(buf,BFSZ,fp))
{
if (buf[0] == '#' || buf[0] == '\n') continue;
for (i= 0; conftab[i].cmd != NULL; i++)
{
if (strncmp(buf, conftab[i].cmd, conftab[i].len) ||
strchr(" \n\t\r", buf[conftab[i].len]) == NULL)
continue;
/* skip white space */
p= buf + conftab[i].len + 1;
while (*p != '\0' && strchr(" \t\n\r",*p) != NULL)
p++;
if (*p == '\0' || *p == '\n' || *p == '\r')
{
fprintf(stderr,"%s: No value given for %s in "
ORVILLE_CONF"\n", progname, conftab[i].cmd);
exit(1);
}
switch (conftab[i].type)
{
case CO_OPT: /* "options" command */
if (set_dflt != NULL)
(*set_dflt)(p);
break;
case CO_STR: /* string-valued commands */
/* Find end of word */
for (q= p; *q != '\0' && strchr(" \t\n\r",*q) == NULL; q++)
;
*q= '\0';
/* Save the new value, without freeing any previous value,
* which might be static.
*/
*(char **)conftab[i].var= malloc(q-p+1);
strcpy(*(char **)conftab[i].var,p);
break;
case CO_FLG: /* flag-valued commands */
*(int *)conftab[i].var= (*p == 'y' || *p == 'Y');
break;
case CO_INT: /* integer-valued commands */
*(int *)conftab[i].var= atoi(p);
break;
}
break;
}
}
fclose(fp);
}
|