/* $Header$
*
* This generates the zone files necessary to load a hesiod server.
- * The following zones are generated:
+ * The following zones are generated: passwd, uid, pobox, group,
+ * grplist, gid, filsys, cluster, pcap, sloc, service.
*/
#include <stdio.h>
if (argc > 2) {
fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
- exit(-1);
+ exit(SMS_ARGS);
}
## ingres sms
changed = do_passwd();
changed += do_pobox();
- changed += do_groups();
changed += do_filsys();
changed += do_cluster();
changed += do_printers();
- changed += do_services();
+ changed += do_sloc();
+ changed += do_service();
+ changed += do_groups();
## exit
if (!changed) {
fprintf(stderr, "No files updated.\n");
if (argc == 2 && stat(argv[1], &sb) == 0)
- exit(0);
+ exit(SMS_NO_CHANGE);
}
if (argc == 2) {
sprintf(cmd, "cd %s; tar cf %s .", HESIOD_DIR, argv[1]);
- exit(system(cmd));
+ if (system(cmd))
+ exit(SMS_TAR_FAIL);
}
- exit(0);
+ exit(SMS_SUCCESS);
}
do_passwd()
##{
FILE *pout, *uout;
- char poutf[64], uoutf[64];
+ char poutf[64], uoutf[64], poutft[64], uoutft[64];
struct stat psb, usb;
time_t ftime;
## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
}
}
- pout = fopen(poutf, "w");
+ sprintf(poutft, "%s~", poutf);
+ pout = fopen(poutft, "w");
if (!pout) {
- perror("cannot open passwd.db for write");
- exit(-1);
+ perror("cannot open passwd.db~ for write");
+ exit(SMS_OCONFIG);
}
- uout = fopen(uoutf, "w");
+ sprintf(uoutft, "%s~", uoutf);
+ uout = fopen(uoutft, "w");
if (!uout) {
- perror("cannot open uid.db for write");
- exit(-1);
+ perror("cannot open uid.db~ for write");
+ exit(SMS_OCONFIG);
}
fprintf(stderr, "Building passwd.db and uid.db\n");
## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
## fullname = u.#fullname, oa = u.office_addr,
## op = u.office_phone, hp = u.home_phone)
-## where u.status != 0 sort by #login {
+## where u.status = 1 sort by #login {
trim(login);
trim(fullname);
trim(oa);
if (fclose(pout) || fclose(uout)) {
fprintf(stderr, "Unsuccessful file close of passwd.db or uid.db\n");
- exit(-1);
+ exit(SMS_CCONFIG);
}
+ fix_file(poutf);
+ fix_file(uoutf);
return(1);
##}
do_pobox()
##{
FILE *out;
- char outf[64];
+ char outf[64], outft[64];
struct stat sb;
time_t ftime;
## char login[9], mach[33], *filetime;
}
}
- out = fopen(outf, "w");
+ sprintf(outft, "%s~", outf);
+ out = fopen(outft, "w");
if (!out) {
perror("cannot open pobox.db for write");
- exit(-1);
+ exit(SMS_OCONFIG);
}
fprintf(stderr, "Building pobox.db\n");
## range of u is users
## range of m is machine
## retrieve (login = u.#login, mach = m.#name)
-## where u.potype = "POP" and m.mach_id = u.pop_id {
+## where u.status = 1 and u.potype = "POP" and m.mach_id = u.pop_id {
trim(login);
trim(mach);
fprintf(out, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
if (fclose(out)) {
fprintf(stderr, "Unsuccessful close of pobox.db\n");
- exit(-1);
+ exit(SMS_CCONFIG);
}
+ fix_file(outf);
return(1);
##}
+/************************************************************************
+ * WARNING: this routine mallocs a large amount of memory which it never frees.
+ * This is considered OK only because main has been arranged to call this
+ * last, so the process will exit shortly after we return.
+ */
+
do_groups()
##{
FILE *iout, *gout, *lout;
char **groups;
struct stat isb, gsb, lsb;
time_t ftime;
- struct save_queue *sq, *sq_create();
- register int first;
+ register struct save_queue *sq;
+ struct save_queue *sq_create();
+ register int first, i;
+ register char **p;
## char name[33], *filetime;
## int gid, id, lid, flag1, flag2, flag3, maxid;
}
}
- iout = fopen(ioutf, "w");
+ sprintf(buf, "%s~", ioutf);
+ iout = fopen(buf, "w");
if (!iout) {
perror("cannot open gid.db for write");
- exit(-1);
+ exit(SMS_OCONFIG);
}
- gout = fopen(goutf, "w");
+ sprintf(buf, "%s~", goutf);
+ gout = fopen(buf, "w");
if (!gout) {
perror("cannot open group.db for write");
- exit(-1);
+ exit(SMS_OCONFIG);
}
- lout = fopen(loutf, "w");
+ sprintf(buf, "%s~", loutf);
+ lout = fopen(buf, "w");
if (!lout) {
perror("cannot open grplist.db for write");
- exit(-1);
+ exit(SMS_OCONFIG);
}
fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
/* retrieve simple groups */
## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
-## where l.group != 0 {
+## where l.group != 0 and l.active != 0 {
trim(name);
sprintf(buf, "%s:%d", name, gid);
groups[lid] = strsave(buf);
name, name, gid);
## }
+ fflush(iout);
+ fflush(gout);
+
/* get special cases: lists that aren't groups themselves but are
* members of groups. */
sq = sq_create();
## range of m is members
-## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
+## retrieve (name = list.#name, gid = list.#gid, lid = l.list_id)
## where l.group = 0 and m.member_type = "LIST" and
## m.member_id = l.list_id and m.list_id = list.list_id and
## list.group != 0 {
trim(name);
- sprintf(buf, "%s:%d", name, gid);
+ if (groups[lid]) {
+ sprintf(buf, "%s:%s:%d", groups[lid], name, gid);
+ free(groups[lid]);
+ } else {
+ sprintf(buf, "%s:%d", name, gid);
+ }
groups[lid] = strsave(buf);
sq_save_data(sq, lid);
## }
+
while (sq_get_data(sq, &id)) {
## repeat retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
## where l.group = 0 and m.member_type = "LIST" and
/* now do grplists */
sq = sq_create();
## range of u is users
-## retrieve (id = u.users_id) where u.status != 0 {
+## retrieve (id = u.users_id) where u.status = 1 {
sq_save_data(sq, id);
## }
while (sq_get_data(sq, &id)) {
-## repeat retrieve (name = u.login) where u.users_id = @id
- trim(name);
first = 1;
-## repeat retrieve (lid = m.list_id)
-## where m.member_type = "USER" and m.member_id = @id {
+## repeat retrieve (lid = m.list_id, name = u.login)
+## where m.member_type = "USER" and m.member_id = @id and
+## u.users_id = @id {
if (groups[lid]) {
- if (first)
- fprintf(lout, "%s.grplist\tHS UNSPECA \"%s",name,groups[lid]);
- else
+ if (first) {
+ trim(name);
+ fprintf(lout, "%s.grplist\tHS UNSPECA \"%s",
+ name, groups[lid]);
+ } else
fprintf(lout, ":%s", groups[lid]);
first = 0;
}
if (!first)
fprintf(lout, "\"\n");
}
+#ifdef notdef
+/* This is commented out because it takes on the order of 30 minutes to
+ * run. Instead, we never free the memory, but the program will exit
+ * shortly anyway.
+ */
sq_destroy(sq);
- for (id = 0; id < maxid; id++)
- if (groups[id])
- free(groups[id]);
+ for (p = &groups[maxid-1]; p >= groups; p--)
+ if (*p)
+ free(*p);
free(groups);
+#endif
if (fclose(iout) || fclose(gout) || fclose(lout)) {
fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
- exit(-1);
+ exit(SMS_CCONFIG);
}
+ fix_file(ioutf);
+ fix_file(goutf);
+ fix_file(loutf);
return(1);
##}
do_filsys()
##{
FILE *out;
- char outf[64];
+ char outf[64], outft[64];
struct stat sb;
time_t ftime;
## char name[33], type[9], loc[33], mach[33], access[2], mount[33], trans[257];
-## char *filetime;
+## char *filetime, comments[65];
## int flag1, flag2, flag3;
sprintf(outf, "%s/filsys.db", HESIOD_DIR);
}
}
-
- out = fopen(outf, "w");
+ sprintf(outft, "%s~", outf);
+ out = fopen(outft, "w");
if (!out) {
perror("cannot open filsys.db for write");
- exit(-1);
+ exit(SMS_OCONFIG);
}
fprintf(stderr, "Building filsys.db\n");
## range of f is filesys
## range of m is machine
## retrieve (name = f.label, type = f.#type, loc = f.#name, mach = m.#name,
-## access = f.#access, mount = f.#mount)
+## access = f.#access, mount = f.#mount, comments = f.#comments)
## where m.mach_id = f.mach_id {
trim(name);
trim(type);
- trim(loc);
- trim(mach);
- trim(access);
- trim(mount);
- fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
- name, type, loc, mach, access, mount);
+ if (strcmp(type, "ERR")) {
+ trim(loc);
+ trim(mach);
+ trim(access);
+ trim(mount);
+ fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
+ name, type, loc, mach, access, mount);
+ } else {
+ trim(comments);
+ fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s\"\n",
+ name, type, comments);
+ }
## }
## range of a is alias
if (fclose(out)) {
fprintf(stderr, "Unsuccessful close of filsys.db\n");
- exit(-1);
+ exit(SMS_CCONFIG);
}
+ fix_file(outf);
return(1);
##}
do_cluster()
##{
FILE *out;
- char outf[64];
+ char outf[64], outft[64];
struct stat sb;
time_t ftime;
## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
}
}
- out = fopen(outf, "w");
+ sprintf(outft, "%s~", outf);
+ out = fopen(outft, "w");
if (!out) {
perror("cannot open cluster.db for write");
- exit(-1);
+ exit(SMS_OCONFIG);
}
fprintf(stderr, "Building cluster.db\n");
if (!machs[mid])
continue;
ms = machs[mid];
- fprintf(out, "smsinternal-%d.cluster", mid);
for (cid = 1; cid < maxclu; cid++) {
if (SET_ISSET(cid, ms)) {
## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
## where d.clu_id = @cid {
trim(label);
trim(data);
- fprintf(out, "\tHS UNSPECA \"%s %s\"\n", label, data);
+ fprintf(out,
+ "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
+ mid, label, data);
## }
}
}
## repeat retrieve (mach = m.#name) where m.mach_id = @mid
trim(mach);
- fprintf(out, "%s.cluster\tHS CNAME smmsinternal-%d.cluster\n",
+ fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
mach, mid);
for (id = mid + 1; id < maxmach; id++) {
if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
machs[id] = NULL;
## repeat retrieve (mach = m.#name) where m.mach_id = @id
trim(mach);
- fprintf(out, "%s.cluster\tHS CNAME smmsinternal-%d.cluster\n",
+ fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
mach, mid);
}
}
if (fclose(out)) {
fprintf(stderr, "Unsuccessful close of cluster.db\n");
- exit(-1);
+ exit(SMS_CCONFIG);
}
+ fix_file(outf);
return(1);
##}
do_printers()
##{
FILE *out;
- char outf[64];
+ char outf[64], outft[64];
struct stat sb;
time_t ftime;
## char name[33], pcap[513], *filetime;
}
}
- out = fopen(outf, "w");
+ sprintf(outft, "%s~", outf);
+ out = fopen(outft, "w");
if (!out) {
perror("cannot open printcap.db for write");
- exit(-1);
+ exit(SMS_OCONFIG);
}
fprintf(stderr, "Building printcap.db\n");
if (fclose(out)) {
fprintf(stderr, "Unsuccessful close of pcap.db\n");
- exit(-1);
+ exit(SMS_CCONFIG);
}
+ fix_file(outf);
return(1);
##}
-do_services()
+do_sloc()
##{
- FILE *out;
- char outf[64];
+ FILE *out, *old, *new;
+ char outf[64], outft[64];
struct stat sb;
time_t ftime;
-## char mach[33], service[17], protocol[9], *filetime;
+ register int c;
+## char mach[33], service[17], *filetime;
## int port, flag1, flag2;
sprintf(outf, "%s/sloc.db", HESIOD_DIR);
## where tblstats.table = "machine"
if (flag1 < 0 && flag2 < 0) {
fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
- goto next;
+ return(0);
}
}
- out = fopen(outf, "w");
+ sprintf(outft, "%s~", outf);
+ out = fopen(outft, "w");
if (!out) {
perror("cannot open sloc.db for write");
- exit(-1);
+ exit(SMS_OCONFIG);
}
fprintf(stderr, "Building sloc.db\n");
## range of s is serverhosts
+## range of m is machine
## retrieve (service = s.#service, mach = m.name)
-## where m.mach_id = s.mach_id {
+## where m.mach_id = s.mach_id sort by #service {
trim(service);
trim(mach);
fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
if (fclose(out)) {
fprintf(stderr, "Unsuccessful close of sloc.db\n");
- exit(-1);
+ exit(SMS_CCONFIG);
+ }
+
+ /* diff outf & outft. If they are the same, report "nothing changed" */
+ old = fopen(outf, "r");
+ new = fopen(outft, "r");
+ while ((c = getc(old)) != EOF)
+ if (c != getc(new))
+ break;
+ if (c != EOF || !feof(new)) {
+ fix_file(outf);
+ fclose(old);
+ fclose(new);
+ return(1);
+ } else {
+ fprintf(stderr, "Nothing in sloc.db has changed.\n");
+ fclose(old);
+ fclose(new);
+ unlink(outft);
+ return(0);
}
+##}
+
+do_service()
+##{
+ FILE *out;
+ char outf[64], outft[64];
+ struct stat sb;
+ time_t ftime;
+## char mach[33], service[33], protocol[9], altserv[129], *filetime;
+## int port, flag1, flag2;
- next:
sprintf(outf, "%s/service.db", HESIOD_DIR);
if (stat(outf, &sb) == 0) {
}
}
- out = fopen(outf, "w");
+ sprintf(outft, "%s~", outf);
+ out = fopen(outft, "w");
if (!out) {
perror("cannot open service.db for write");
- exit(-1);
+ exit(SMS_OCONFIG);
}
fprintf(stderr, "Building service.db\n");
service, service, protocol, port);
## }
+## range of a is alias
+## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
+ trim(service);
+ trim(altserv);
+ fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
+## }
+
if (fclose(out)) {
fprintf(stderr, "Unsuccessful close of service.db\n");
- exit(-1);
+ exit(SMS_CCONFIG);
}
+ fix_file(outf);
return(1);
##}