]> andersk Git - moira.git/commitdiff
1) separate service & sloc
authormar <mar>
Thu, 28 Jul 1988 21:33:00 +0000 (21:33 +0000)
committermar <mar>
Thu, 28 Jul 1988 21:33:00 +0000 (21:33 +0000)
2) better error codes
3) backup file names

gen/hesiod.qc

index b8856467ca7c96397156065181ef75d30eaf8300..3c14a60e819f80565bd9cd74d6dce12d2a8a5d76 100644 (file)
@@ -1,7 +1,8 @@
 /* $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>
@@ -28,7 +29,7 @@ char **argv;
 
     if (argc > 2) {
        fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
-       exit(-1);
+       exit(SMS_ARGS);
     }
 
 ##  ingres sms
@@ -36,33 +37,35 @@ char **argv;
 
     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;
@@ -82,15 +85,17 @@ do_passwd()
       }
     }
 
-    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");
@@ -99,7 +104,7 @@ do_passwd()
 ##  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);
@@ -113,8 +118,10 @@ do_passwd()
 
     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);
 ##}
 
@@ -122,7 +129,7 @@ do_passwd()
 do_pobox()
 ##{
     FILE *out;
-    char outf[64];
+    char outf[64], outft[64];
     struct stat sb;
     time_t ftime;
 ##  char login[9], mach[33], *filetime;
@@ -143,10 +150,11 @@ do_pobox()
       }
     }
 
-    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");
@@ -154,7 +162,7 @@ do_pobox()
 ##  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",
@@ -163,12 +171,19 @@ do_pobox()
 
     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;
@@ -176,8 +191,10 @@ do_groups()
     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;
 
@@ -201,20 +218,23 @@ do_groups()
       }
     }
 
-    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");
@@ -231,7 +251,7 @@ do_groups()
 
     /* 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);
@@ -240,19 +260,28 @@ do_groups()
                    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
@@ -268,19 +297,20 @@ do_groups()
     /* 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;
            }
@@ -288,16 +318,25 @@ do_groups()
        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);
 ##}
 
@@ -305,11 +344,11 @@ do_groups()
 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);
@@ -329,11 +368,11 @@ do_filsys()
       }
     }
 
-
-    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");
@@ -341,16 +380,22 @@ do_filsys()
 ##  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
@@ -362,8 +407,9 @@ do_filsys()
 
     if (fclose(out)) {
        fprintf(stderr, "Unsuccessful close of filsys.db\n");
-       exit(-1);
+       exit(SMS_CCONFIG);
     }
+    fix_file(outf);
     return(1);
 ##}
 
@@ -390,7 +436,7 @@ typedef long        set_mask;
 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;
@@ -416,10 +462,11 @@ do_cluster()
       }
     }
 
-    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");
@@ -446,21 +493,22 @@ do_cluster()
        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)) {
@@ -468,7 +516,7 @@ do_cluster()
                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);
            }
        }
@@ -488,8 +536,9 @@ do_cluster()
 
     if (fclose(out)) {
        fprintf(stderr, "Unsuccessful close of cluster.db\n");
-       exit(-1);
+       exit(SMS_CCONFIG);
     }
+    fix_file(outf);
     return(1);
 ##}
 
@@ -497,7 +546,7 @@ do_cluster()
 do_printers()
 ##{
     FILE *out;
-    char outf[64];
+    char outf[64], outft[64];
     struct stat sb;
     time_t ftime;
 ##  char name[33], pcap[513], *filetime;
@@ -516,10 +565,11 @@ do_printers()
       }
     }
 
-    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");
@@ -533,19 +583,21 @@ do_printers()
 
     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);
@@ -559,21 +611,23 @@ do_services()
 ##         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);
@@ -581,10 +635,38 @@ do_services()
 
     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) {
@@ -598,10 +680,11 @@ do_services()
       }
     }
 
-    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");
@@ -615,9 +698,17 @@ do_services()
                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);
 ##}
This page took 1.740031 seconds and 5 git commands to generate.