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
|
/*
* Steven Schultz - sms@moe.2bsd.com
*
* @(#)ctimed.c 1.0 (2.11BSD) 1996/6/25
*
* ctimed - the daemon that supports the ctime() and getpw*() stubs
* in 'libcstubs.a'.
*/
#include <signal.h>
#include <stdio.h>
#include <setjmp.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <pwd.h>
#include <utmp.h>
/*
* These should probably be placed in an include file. If you add anything
* here then you will also have to modify /usr/src/usr.lib/libstubs/stubs.c
* (if for no other reason than to add the stub code).
*/
#define CTIME 1
#define ASCTIME 2
#define TZSET 3
#define LOCALTIME 4
#define GMTIME 5
#define OFFTIME 6
#define GETPWENT 7
#define GETPWNAM 8
#define GETPWUID 9
#define SETPASSENT 10
#define ENDPWENT 11
extern struct tm *offtime();
jmp_buf env;
char *cp;
char junk[256 + sizeof (struct passwd) + 4];
long off;
time_t l;
void timeout(), checkppid();
struct tm tmtmp, *tp;
main()
{
register int i;
register struct passwd *pw;
struct itimerval it;
u_char c, xxx;
int len, tosslen;
uid_t uid;
signal(SIGPIPE, SIG_DFL);
for (i = getdtablesize(); --i > 2; )
close(i);
/*
* Need a timer running while we disassociate from the control terminal
* in case of a modem line which has lost carrier.
*/
timerclear(&it.it_interval);
it.it_value.tv_sec = 5;
it.it_value.tv_usec = 0;
signal(SIGALRM, timeout);
setitimer(ITIMER_REAL, &it, (struct itimerval *) NULL);
if (setjmp(env) == 0)
{
i = open("/dev/tty", 0);
if (i >= 0)
{
ioctl(i, TIOCNOTTY, NULL);
close(i);
}
}
/*
* Now start a timer with one minute refresh. In the signal service
* routine, check the parent process id to see if this process has
* been orphaned and if so exit. This is primarily aimed at removing
* the 'ctimed' process left behind by 'sendmail's multi-fork startup
* but may prove useful in preventing accumulation of 'ctimed' processes
* in other circumstances as well. Normally this process is short
* lived.
*/
it.it_interval.tv_sec = 60;
it.it_interval.tv_usec = 0;
it.it_value.tv_sec = 60;
it.it_value.tv_usec = 0;
signal(SIGALRM, checkppid);
setitimer(ITIMER_REAL, &it, (struct itimerval *) NULL);
while (read(fileno(stdin), &c, 1) == 1)
{
switch (c)
{
case CTIME:
l = 0L;
getb(fileno(stdin), &l, sizeof l);
cp = ctime(&l);
write(fileno(stdout), cp, 26);
break;
case ASCTIME:
getb(fileno(stdin), &tmtmp, sizeof tmtmp);
cp = asctime(&tmtmp);
write(fileno(stdout), cp, 26);
break;
case TZSET:
(void) tzset();
break;
case LOCALTIME:
l = 0L;
getb(fileno(stdin), &l, sizeof l);
tp = localtime(&l);
write(fileno(stdout), tp, sizeof (*tp));
strcpy(junk, tp->tm_zone);
junk[24] = '\0';
write(fileno(stdout), junk, 24);
break;
case GMTIME:
l = 0L;
getb(fileno(stdin), &l, sizeof l);
tp = gmtime(&l);
write(fileno(stdout), tp, sizeof (*tp));
strcpy(junk, tp->tm_zone);
junk[24] = '\0';
write(fileno(stdout), junk, 24);
break;
case OFFTIME:
getb(fileno(stdin), &l, sizeof l);
getb(fileno(stdin), &off, sizeof off);
#ifdef __bsdi__
l += off;
tp = localtime(&l);
#else
tp = offtime(&l, off);
#endif
write(fileno(stdout), tp, sizeof (*tp));
break;
case GETPWENT:
pw = getpwent();
do_pw(pw);
break;
case GETPWNAM:
getb(fileno(stdin), &len, sizeof (int));
if (len > UT_NAMESIZE)
{
tosslen = len - UT_NAMESIZE;
len = UT_NAMESIZE;
}
else
tosslen = 0;
getb(fileno(stdin), junk, len);
for (;tosslen; tosslen--)
getb(fileno(stdin), &xxx, 1);
junk[len] = '\0';
pw = getpwnam(junk);
do_pw(pw);
break;
case GETPWUID:
getb(fileno(stdin), &uid, sizeof (uid_t));
pw = getpwuid(uid);
do_pw(pw);
break;
case SETPASSENT:
getb(fileno(stdin), &len, sizeof (int));
if (setpassent(len))
len = 1;
else
len = 0;
write(fileno(stdout), &len, sizeof (int));
break;
case ENDPWENT:
endpwent();
break;
default:
abort("switch");
}
}
}
getb(f, p, n)
int f;
register char *p;
register int n;
{
register int i;
while (n)
{
i = read(f, p, n);
if (i <= 0)
return;
p += i;
n -= i;
}
}
void
timeout()
{
longjmp(env, 1);
}
void
checkppid()
{
if (getppid() == 1)
exit(0);
}
do_pw(pw)
struct passwd *pw;
{
int len;
if (!pw)
{
len = 0;
write(fileno(stdout), &len, sizeof (int));
return;
}
len = packpwtobuf(pw, junk);
write(fileno(stdout), &len, sizeof (int));
write(fileno(stdout), pw, sizeof (*pw));
write(fileno(stdout), junk, len);
return;
}
packpwtobuf(pw, buf)
register struct passwd *pw;
char *buf;
{
register char *cp = buf;
register char *dp;
dp = pw->pw_name;
pw->pw_name = (char*) 0;
while (*cp++ = *dp++)
;
dp = pw->pw_passwd;
pw->pw_passwd = (char*) (cp - buf);
while (*cp++ = *dp++)
;
dp = pw->pw_class;
pw->pw_class = (char*) (cp - buf);
while (*cp++ = *dp++)
;
dp = pw->pw_gecos;
pw->pw_gecos = (char*) (cp - buf);
while (*cp++ = *dp++)
;
dp = pw->pw_dir;
pw->pw_dir = (char*) (cp - buf);
while (*cp++ = *dp++)
;
dp = pw->pw_shell;
pw->pw_shell = (char*) (cp - buf);
while (*cp++ = *dp++)
;
return(cp - buf);
}
|