source: trunk/third/moira/gen/directory.pc @ 26024

Revision 26024, 3.3 KB checked in by jdreed, 11 years ago (diff)
In moira: * Snapshot moira at r4113 to pick up new firewall-related changes
Line 
1/* $Id: directory.pc 4113 2013-05-28 14:29:10Z zacheiss $
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
21EXEC SQL INCLUDE sqlca;
22
23RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/gen/directory.pc $ $Id: directory.pc 4113 2013-05-28 14:29:10Z zacheiss $");
24
25char *whoami = "directory.gen";
26char *db = "moira/moira";
27
28int 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 OR status = 10 OR status = 11) AND type != 'SYSTEM' AND type != 'STAFF'
67    AND type != 'TEST' AND type != 'REGTEST' AND type != 'SHARED'
68    AND type != 'MGMT' AND type != 'DOOMED' AND type NOT LIKE 'GUES%';
69  EXEC SQL OPEN x;
70  while (1)
71    {
72      EXEC SQL FETCH x INTO :login, :last_name, :first_name, :middle_name,
73        :office_address, :office_phone, :home_address, :home_phone,
74        :id, :type;
75      if (sqlca.sqlcode)
76        break;
77      strtrim(login);
78      strtrim(last_name);
79      strtrim(first_name);
80      strtrim(middle_name);
81      strtrim(office_address);
82      strtrim(office_phone);
83      strtrim(home_address);
84      strtrim(home_phone);
85      strtrim(id);
86      strtrim(type);
87#ifdef notdef
88      if(isdigit(*id))
89        fprintf(out, "%s^4:p\t24:%s %s\t7:%s, %s %s\t3:%s, %s "
90                "%s\t5:%s\t2:%s@mit.edu\t1:%s\t0:%s\t14:%s\t15:%s\t10:"
91                "Unlisted Account\n", id, last_name, login, last_name,
92                first_name, middle_name, last_name, first_name,
93                middle_name, login, login, office_phone, office_address,
94                home_phone, home_address);
95#else
96      if(isdigit(*id))
97        fprintf(out, "%s^4:p\t24:%s %s\t7:%s, %s %s\t3:%s, %s "
98                "%s\t5:%s\t2:%s@mit.edu\t1:%s\t0:%s\t14:%s\t15:%s\t10:%s\n",
99                id, last_name, login, last_name, first_name, middle_name,
100                last_name, first_name, middle_name, login, login,
101                "", "", "", "", "MIT Affiliate");
102#endif
103    }
104  EXEC SQL CLOSE x;
105
106  EXEC SQL COMMIT;
107
108  if (fclose(out))
109    {
110      perror("close failed");
111      exit(MR_CCONFIG);
112    }
113  if (outf)
114    fix_file(outf);
115  exit(MR_SUCCESS);
116
117sqlerr:
118  db_error(sqlca.sqlcode);
119  exit(MR_DBMS_ERR);
120}
Note: See TracBrowser for help on using the repository browser.