]> andersk Git - moira.git/blob - gen/passwd.dc
New database and column names for Moira2.
[moira.git] / gen / passwd.dc
1 /* $Header$
2  *
3  * This generates a master /etc/passwd containing all active (status != 0)
4  * accounts.
5  *
6  *  (c) Copyright 1988, 1990 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 <stdio.h>
13 #include <moira.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17 EXEC SQL INCLUDE sqlca;
18
19 extern int errno;
20 char *whoami = "passwd.gen";
21
22
23 main(argc, argv)
24 int argc;
25 char **argv;
26 {
27     FILE *out = stdout;
28     char *outf = NULL, outft[64];
29     struct stat sb;
30     int flag;
31     EXEC SQL BEGIN DECLARE SECTION;
32     char login[9], shell[33], fullname[33], oa[17], op[13], hp[17];
33     char nickname[17];
34     int uid;
35     EXEC SQL END DECLARE SECTION;
36
37 #ifsql INGRES
38     EXEC SQL CONNECT moira;
39 #endsql
40 #ifsql INFORMIX
41     EXEC SQL DATABASE moira;
42 #endsql
43
44     if (argc == 2) {
45         if (stat(argv[1], &sb) == 0) {
46             if (ModDiff (&flag, "users", sb.st_mtime))
47               exit(MR_DATE);
48             if (flag < 0) {
49                 fprintf(stderr, "File %s does not need to be rebuilt.\n",
50                         argv[1]);
51                 exit(MR_NO_CHANGE);
52             }
53         }
54         outf = argv[1];
55         sprintf(outft, "%s~", outf);
56         if ((out = fopen(outft, "w")) == NULL) {
57             fprintf(stderr, "unable to open %s for output\n", outf);
58             exit(MR_OCONFIG);
59         }
60     } else if (argc != 1) {
61         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
62         exit(MR_ARGS);
63     } else {
64         outf = NULL;
65     }
66
67     /* The following is declarative, not executed,
68      * and so is dependent on where it is in the file,
69      * not in the order of execution of statements.
70      */
71     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
72
73     EXEC SQL DECLARE x CURSOR FOR SELECT 
74       login, uid, shell, fullname, office_addr, nickname,
75       office_phone, home_phone
76         FROM users WHERE status = 1;
77     EXEC SQL OPEN x;
78     while (1) {
79         EXEC SQL FETCH x INTO :login, :uid, :shell, :fullname, :oa, :nickname,
80         :op, :hp;
81         if (sqlca.sqlcode != 0) break;
82         strtrim(login);
83         strtrim(fullname);
84         strtrim(nickname);
85         strtrim(oa);
86         strtrim(op);
87         strtrim(hp);
88         strtrim(shell);
89         fprintf(out, "%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\n",
90                 login, uid, fullname, nickname, oa, op, hp, login, shell);
91     }
92
93     EXEC SQL CLOSE x;
94 #ifsql INGRES
95     EXEC SQL DISCONNECT;
96 #endsql
97 #ifsql INFORMIX
98     EXEC SQL CLOSE DATABASE;
99 #endsql
100
101     if (fclose(out)) {
102         perror("close failed");
103         exit(MR_CCONFIG);
104     }
105     if (outf)
106       fix_file(outf);
107     exit(MR_SUCCESS);
108
109  sqlerr:
110     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
111     critical_alert("DCM", "Passwd build encountered INGRES ERROR %d",
112                    sqlca.sqlcode);
113     exit(MR_INGRES_ERR);
114 }
This page took 0.049394 seconds and 5 git commands to generate.