djbdnscurve6  38
djbdnscurve6
dnscache-conf.c
Go to the documentation of this file.
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include "hasdevtcp.h"
5 #ifdef HASDEVTCP
6 #include <sys/mkdev.h>
7 #endif
8 #include <pwd.h>
9 #include "buffer.h"
10 #include "uint_t.h"
11 #include "taia.h"
12 #include "str.h"
13 #include "open.h"
14 #include "logmsg.h"
15 #include "exit.h"
16 #include "auto_home.h"
17 #include "generic-conf.h"
18 
19 #define WHO "dnscache-conf"
20 
21 void usage(void)
22 {
23  logmsg(WHO,100,USAGE,"dnscache-conf acct logacct /dnscache [ myip ]");
24 }
25 
27 char rootserversbuf[64];
29 
30 char *dir;
31 char *user;
32 char *loguser;
33 struct passwd *pw;
34 const char *myip;
35 
36 uint32 seed[32];
37 int seedpos = 0;
38 
39 void seed_adduint32(uint32 u)
40 {
41  int i;
42 
43  seed[seedpos] += u;
44  if (++seedpos == 32) {
45  for (i = 0; i < 32; ++i) {
46  u = ((u ^ seed[i]) + 0x9e3779b9) ^ (u << 7) ^ (u >> 25);
47  seed[i] = u;
48  }
49  seedpos = 0;
50  }
51 }
52 
53 void seed_addtime(void)
54 {
55  struct taia t;
56  char tpack[TAIA_PACK];
57  int i;
58 
59  taia_now(&t);
60  taia_pack(tpack,&t);
61  for (i = 0; i < TAIA_PACK; ++i)
62  seed_adduint32(tpack[i]);
63 }
64 
65 int main(int argc,char **argv)
66 {
67  seed_addtime();
68  seed_adduint32(getpid());
69  seed_adduint32(getppid());
70  seed_adduint32(getuid());
71  seed_adduint32(getgid());
72 
73  user = argv[1];
74  if (!user) usage();
75  loguser = argv[2];
76  if (!loguser) usage();
77  dir = argv[3];
78  if (!dir) usage();
79  if (dir[0] != '/') usage();
80  myip = argv[4];
81  if (!myip) myip = "::1";
82 
83  pw = getpwnam(loguser);
84  seed_addtime();
85  if (!pw)
86  logmsg(WHO,111,FATAL,B("unknown account: ",loguser));
87 
88  if (chdir(auto_home) == -1)
89  logmsg(WHO,111,FATAL,B("unable to switch to: ",auto_home));
90 
91  fdrootservers = open_read("/etc/dnsroots.local");
92  if (fdrootservers == -1) {
93  if (errno != ENOENT)
94  logmsg(WHO,111,FATAL,"unable to open /etc/dnsroots.local");
95  fdrootservers = open_read("/etc/dnsroots.global");
96  if (fdrootservers == -1)
97  logmsg(WHO,111,FATAL,"unable to open /etc/dnsroots.global");
98  }
99 
100  init(dir,WHO);
101 
102  seed_addtime(); makedir("log");
103  seed_addtime(); perm(02755);
104  seed_addtime(); makedir("log/main");
105  seed_addtime(); owner(pw->pw_uid,pw->pw_gid);
106  seed_addtime(); perm(02755);
107  seed_addtime(); start("log/status"); finish();
108  seed_addtime(); owner(pw->pw_uid,pw->pw_gid);
109  seed_addtime(); perm(0644);
110  seed_addtime(); makedir("env");
111  seed_addtime(); perm(02755);
112  seed_addtime(); start("env/ROOT"); outs(dir); outs("/root\n"); finish();
113  seed_addtime(); perm(0644);
114  seed_addtime(); start("env/IP"); outs(myip); outs("\n"); finish();
115  seed_addtime(); perm(0644);
116  seed_addtime(); start("env/IPSEND"); outs("::\n"); finish();
117  seed_addtime(); perm(0644);
118  seed_addtime(); start("env/CACHESIZE"); outs("1000000\n"); finish();
119  seed_addtime(); perm(0644);
120  seed_addtime(); start("env/DATALIMIT"); outs("30000000\n"); finish();
121  seed_addtime(); perm(0644);
122  seed_addtime(); start("env/USETEXTFORMAT"); outs("1\n"); finish();
123  seed_addtime(); perm(0644);
124  seed_addtime(); start("env/UZ5FALLBACK"); outs("2\n"); finish();
125  seed_addtime(); perm(0644);
126  seed_addtime(); start("env/FLAGEDSERVER"); outs("1\n"); finish();
127  seed_addtime(); perm(0644);
128  seed_addtime(); start("run");
129  outs("#!/bin/sh\nexec 2>&1\nexec <seed\nexec envdir ./env sh -c '\n exec envuidgid "); outs(user);
130  outs(" softlimit -o250 -d \"$DATALIMIT\" ");
131  outs(auto_home); outs("/bin/dnscache\n'\n"); finish();
132  seed_addtime(); perm(0755);
133  seed_addtime(); start("log/run");
134  outs("#!/bin/sh\nexec setuidgid "); outs(loguser);
135  outs(" multilog t ./main\n"); finish();
136  seed_addtime(); perm(0755);
137  seed_addtime(); makedir("root");
138  seed_addtime(); perm(02755);
139  seed_addtime(); makedir("root/ip");
140  seed_addtime(); perm(02755);
141  seed_addtime(); start("root/ip/127.0.0.1"); start("root/ip/::1"); finish();
142  seed_addtime(); perm(0600);
143  seed_addtime(); makedir("root/servers");
144  seed_addtime(); perm(02755);
145  seed_addtime(); start("root/servers/@");
146  buffer_init(&ssrootservers,buffer_unixread,fdrootservers,rootserversbuf,sizeof(rootserversbuf));
148  finish();
149  seed_addtime(); perm(0644);
150  seed_addtime();
151 
152  start("seed"); out((char *) seed,128); finish();
153  perm(0600);
154 
155 #ifdef HASDEVTCP
156  makedir("root/etc");
157  perm(02755);
158  makedir("root/dev");
159  perm(02755);
160  start("root/etc/netconfig");
161  outs("tcp tpi_cots_ord v inet tcp /dev/tcp -\n");
162  outs("udp tpi_clts v inet udp /dev/udp -\n");
163  finish();
164  perm(0645);
165  umask(000);
166  if (mknod("root/dev/tcp",S_IFCHR | 0667,makedev(11,42)) == -1)
167  logmsg(WHO,111,FATAL,B("unable to create device ",dir,"/root/dev/tcp"));
168  if (mknod("root/dev/udp",S_IFCHR | 0667,makedev(11,41)) == -1)
169  logmsg(WHO,111,FATAL,B("unable to create device ",dir,"/root/dev/udp"));
170  umask(022);
171 #endif
172 
173  _exit(0);
174 }
const char auto_home[]
char * user
Definition: dnscache-conf.c:31
int seedpos
Definition: dnscache-conf.c:37
int main(int argc, char **argv)
Definition: dnscache-conf.c:65
uint32 seed[32]
Definition: dnscache-conf.c:36
void seed_addtime(void)
Definition: dnscache-conf.c:53
const char * myip
Definition: dnscache-conf.c:34
buffer ssrootservers
Definition: dnscache-conf.c:28
char * dir
Definition: dnscache-conf.c:30
struct passwd * pw
Definition: dnscache-conf.c:33
char rootserversbuf[64]
Definition: dnscache-conf.c:27
void seed_adduint32(uint32 u)
Definition: dnscache-conf.c:39
#define WHO
Definition: dnscache-conf.c:19
void usage(void)
Definition: dnscache-conf.c:21
int fdrootservers
Definition: dnscache-conf.c:26
char * loguser
Definition: dnscache-conf.c:32
struct tcpclient t[QUERY_MAXTCP]
void owner(int uid, int gid)
Definition: generic-conf.c:76
void outs(const char *s)
Definition: generic-conf.c:49
void init(const char *d, const char *f)
Definition: generic-conf.c:17
void start(const char *s)
Definition: generic-conf.c:41
void copyfrom(buffer *b)
Definition: generic-conf.c:59
void makedir(const char *s)
Definition: generic-conf.c:35
void perm(int mode)
Definition: generic-conf.c:71
void finish(void)
Definition: generic-conf.c:64
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54
unsigned long u
Definition: utime.c:10