/* $Header$ * * This generates the msql database for the network tables. * * (c) Copyright 1988, 1990 by the Massachusetts Institute of Technology. * For copying and distribution information, please see the file * . */ #include #include #include #include #include #include EXEC SQL INCLUDE sqlca; extern int errno; char *whoami = "ndb.gen"; char *db = "moira/moira"; void users(FILE *out); void hosts(FILE *out); int main(int argc, char **argv) { FILE *out = stdout; char *outf = NULL, outft[64]; struct stat sb; int flag; EXEC SQL CONNECT :db; if (argc == 2) { outf = argv[1]; sprintf(outft, "%s~", outf); if (!(out = fopen(outft, "w"))) { fprintf(stderr, "unable to open %s for output\n", outf); exit(MR_OCONFIG); } } else if (argc != 1) { fprintf(stderr, "usage: %s [outfile]\n", argv[0]); exit(MR_ARGS); } else outf = NULL; EXEC SQL COMMIT; fprintf(stderr, "users...\n"); users(out); fprintf(stderr, "hosts...\n"); hosts(out); if(fclose(out) < 0) { perror("close failed"); exit(MR_CCONFIG); } if (outf) fix_file(outf); exit(MR_SUCCESS); } void users(FILE *out) { char *c; EXEC SQL BEGIN DECLARE SECTION; char login[9], id[17]; EXEC SQL END DECLARE SECTION; EXEC SQL WHENEVER SQLERROR GOTO sqlerr; EXEC SQL DECLARE users1 CURSOR FOR SELECT login, clearid FROM users WHERE clearid != '0' AND clearid != '999999999' AND login NOT LIKE '#%'; EXEC SQL OPEN users1; while (1) { EXEC SQL FETCH users1 INTO :login, id; if (sqlca.sqlcode) break; strtrim(login); strtrim(id); c = login; while (*c) { if (!isdigit(*c++)) break; } fprintf(out, "user,%s,%s\n", id, login); } EXEC SQL CLOSE users1; EXEC SQL COMMIT; return; sqlerr: db_error(sqlca.sqlcode); exit(MR_DBMS_ERR); } void hosts(FILE *out) { struct hash *users; char *p; int i; EXEC SQL BEGIN DECLARE SECTION; char name[128], mitid[17], owner_type[9], addr[17], inuse[64]; int id, use, status, owner; EXEC SQL END DECLARE SECTION; EXEC SQL WHENEVER SQLERROR GOTO sqlerr; fprintf(stderr, "aliases...\n"); EXEC SQL DECLARE hosts1 CURSOR FOR SELECT mach_id, name FROM hostalias; EXEC SQL OPEN hosts1; while (1) { EXEC SQL FETCH hosts1 INTO :id, :name; if (sqlca.sqlcode) break; if (id == 0) continue; if (!*strtrim(name)) continue; if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) { fprintf(stderr, "Name %s not in MIT domain\n", name); continue; } fprintf(out, "host_alias,%d,%s\n", id, name); } EXEC SQL CLOSE hosts1; EXEC SQL COMMIT; fprintf(stderr, "users (again)...\n"); EXEC SQL DECLARE hosts2 CURSOR FOR SELECT users_id, clearid FROM users WHERE clearid != '0' and clearid !='999999999'; EXEC SQL OPEN hosts2; users = create_hash(20001); while (1) { EXEC SQL FETCH hosts2 INTO :id, :mitid; if (sqlca.sqlcode) break; if (id == 0) continue; if (!*strtrim(mitid)) continue; hash_store(users, id, strsave(mitid)); } EXEC SQL CLOSE hosts2; EXEC SQL COMMIT; fprintf(stderr, "hosts (for real)...\n"); EXEC SQL DECLARE hosts3 CURSOR FOR SELECT name, mach_id, address, use, inuse, status, owner_type, owner_id FROM machine; EXEC SQL OPEN hosts3; while (1) { EXEC SQL FETCH hosts3 INTO :name, :id, :addr, :use, :inuse, :status, :owner_type, :owner; if (sqlca.sqlcode) break; if (id == 0) continue; if (!*strtrim(name)) continue; if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) continue; strtrim(addr); strtrim(owner_type); strtrim(inuse); fprintf(out, "host,%d,%s,%s,%d,0,%d", id,name,addr,use,status); if (!strcmp(owner_type, "USER")) { if (p = hash_lookup(users, owner)) fprintf(out, ",USER,%s\n", p); } else fprintf(out, ",NONE,0\n"); } EXEC SQL CLOSE hosts3; EXEC SQL COMMIT; return; sqlerr: db_error(sqlca.sqlcode); exit(MR_DBMS_ERR); }