1 | /* $Id: directory.pc,v 1.6 2009-09-23 14:33:54 zacheiss Exp $ |
---|
2 | * |
---|
3 | * This generates a master /etc/passwd containing all active (status != 0) |
---|
4 | * accounts. |
---|
5 | * |
---|
6 | * Copyright (C) 1998 by the Massachusetts Institute of Technology. |
---|
7 | * For copying and distribution information, please see the file |
---|
8 | * <mit-copyright.h>. |
---|
9 | */ |
---|
10 | |
---|
11 | #include <mit-copyright.h> |
---|
12 | #include <moira.h> |
---|
13 | |
---|
14 | #include <sys/stat.h> |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <time.h> |
---|
18 | |
---|
19 | #include "util.h" |
---|
20 | |
---|
21 | EXEC SQL INCLUDE sqlca; |
---|
22 | |
---|
23 | RCSID("$Header: /afs/.athena.mit.edu/astaff/project/moiradev/repository/moira/gen/directory.pc,v 1.6 2009-09-23 14:33:54 zacheiss Exp $"); |
---|
24 | |
---|
25 | char *whoami = "directory.gen"; |
---|
26 | char *db = "moira/moira"; |
---|
27 | |
---|
28 | int main(int argc, char **argv) |
---|
29 | { |
---|
30 | FILE *out = stdout; |
---|
31 | char *outf = NULL, outft[MAXPATHLEN]; |
---|
32 | EXEC SQL BEGIN DECLARE SECTION; |
---|
33 | char login[USERS_LOGIN_SIZE], last_name[USERS_LAST_SIZE]; |
---|
34 | char first_name[USERS_FIRST_SIZE], middle_name[USERS_MIDDLE_SIZE]; |
---|
35 | char office_address[USERS_OFFICE_ADDR_SIZE]; |
---|
36 | char office_phone[USERS_OFFICE_PHONE_SIZE]; |
---|
37 | char home_address[USERS_HOME_ADDR_SIZE], home_phone[USERS_HOME_PHONE_SIZE]; |
---|
38 | char id[USERS_CLEARID_SIZE], type[USERS_TYPE_SIZE]; |
---|
39 | EXEC SQL END DECLARE SECTION; |
---|
40 | |
---|
41 | EXEC SQL CONNECT :db; |
---|
42 | |
---|
43 | if (argc == 2) |
---|
44 | { |
---|
45 | outf = argv[1]; |
---|
46 | sprintf(outft, "%s~", outf); |
---|
47 | if (!(out = fopen(outft, "w"))) |
---|
48 | { |
---|
49 | fprintf(stderr, "unable to open %s for output\n", outf); |
---|
50 | exit(MR_OCONFIG); |
---|
51 | } |
---|
52 | } |
---|
53 | else if (argc != 1) |
---|
54 | { |
---|
55 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); |
---|
56 | exit(MR_ARGS); |
---|
57 | } |
---|
58 | else |
---|
59 | outf = NULL; |
---|
60 | |
---|
61 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; |
---|
62 | |
---|
63 | EXEC SQL DECLARE x CURSOR FOR SELECT |
---|
64 | login, last, first, middle, office_addr, office_phone, |
---|
65 | home_addr, home_phone, clearid, type |
---|
66 | FROM users WHERE status = 1 AND type != 'SYSTEM' AND type != 'STAFF' |
---|
67 | AND type != 'TEST' and type != 'REGTEST' AND type NOT LIKE 'GUES%'; |
---|
68 | EXEC SQL OPEN x; |
---|
69 | while (1) |
---|
70 | { |
---|
71 | EXEC SQL FETCH x INTO :login, :last_name, :first_name, :middle_name, |
---|
72 | :office_address, :office_phone, :home_address, :home_phone, |
---|
73 | :id, :type; |
---|
74 | if (sqlca.sqlcode) |
---|
75 | break; |
---|
76 | strtrim(login); |
---|
77 | strtrim(last_name); |
---|
78 | strtrim(first_name); |
---|
79 | strtrim(middle_name); |
---|
80 | strtrim(office_address); |
---|
81 | strtrim(office_phone); |
---|
82 | strtrim(home_address); |
---|
83 | strtrim(home_phone); |
---|
84 | strtrim(id); |
---|
85 | strtrim(type); |
---|
86 | #ifdef notdef |
---|
87 | if(isdigit(*id)) |
---|
88 | fprintf(out, "%s^4:p\t24:%s %s\t7:%s, %s %s\t3:%s, %s " |
---|
89 | "%s\t5:%s\t2:%s@mit.edu\t1:%s\t0:%s\t14:%s\t15:%s\t10:" |
---|
90 | "Unlisted Account\n", id, last_name, login, last_name, |
---|
91 | first_name, middle_name, last_name, first_name, |
---|
92 | middle_name, login, login, office_phone, office_address, |
---|
93 | home_phone, home_address); |
---|
94 | #else |
---|
95 | if(isdigit(*id)) |
---|
96 | fprintf(out, "%s^4:p\t24:%s %s\t7:%s, %s %s\t3:%s, %s " |
---|
97 | "%s\t5:%s\t2:%s@mit.edu\t1:%s\t0:%s\t14:%s\t15:%s\t10:%s\n", |
---|
98 | id, last_name, login, last_name, first_name, middle_name, |
---|
99 | last_name, first_name, middle_name, login, login, |
---|
100 | "", "", "", "", "MIT Affiliate"); |
---|
101 | #endif |
---|
102 | } |
---|
103 | EXEC SQL CLOSE x; |
---|
104 | |
---|
105 | EXEC SQL COMMIT; |
---|
106 | |
---|
107 | if (fclose(out)) |
---|
108 | { |
---|
109 | perror("close failed"); |
---|
110 | exit(MR_CCONFIG); |
---|
111 | } |
---|
112 | if (outf) |
---|
113 | fix_file(outf); |
---|
114 | exit(MR_SUCCESS); |
---|
115 | |
---|
116 | sqlerr: |
---|
117 | db_error(sqlca.sqlcode); |
---|
118 | exit(MR_DBMS_ERR); |
---|
119 | } |
---|