]> andersk Git - moira.git/blobdiff - gen/passwd.dc
updated to handle subnets as well
[moira.git] / gen / passwd.dc
index 926f9884da9c002548944287f5e59937f9e9cf0b..3c4bbad83f576db89da1cd2ce49a7cbabb0cc1b5 100644 (file)
  *
  * This generates a master /etc/passwd containing all active (status != 0)
  * accounts.
+ *
+ *  (c) Copyright 1988, 1990 by the Massachusetts Institute of Technology.
+ *  For copying and distribution information, please see the file
+ *  <mit-copyright.h>.
  */
 
+#include <mit-copyright.h>
 #include <stdio.h>
+#include <moira.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+EXEC SQL INCLUDE sqlca;
 
 extern int errno;
+char *whoami = "passwd.gen";
+
 
 main(argc, argv)
 int argc;
 char **argv;
 {
-##  char login[9], shell[33], fullname[33], oa[17], op[13], hp[17];
-##  int uid, error;
     FILE *out = stdout;
+    char *outf = NULL, outft[64];
+    struct stat sb;
+    int flag;
+    EXEC SQL BEGIN DECLARE SECTION;
+    char login[9], shell[33], fullname[33], oa[17], op[13], hp[17];
+    char nickname[17];
+    int uid;
+    EXEC SQL END DECLARE SECTION;
+
+#ifsql INGRES
+    EXEC SQL CONNECT moira;
+    EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
+#endsql
+#ifsql INFORMIX
+    EXEC SQL DATABASE moira;
+#endsql
 
     if (argc == 2) {
-       if ((out = fopen(argv[1], "w")) == NULL) {
-           fprintf(stderr, "unable to open %s for output\n", argv[1]);
-           exit(errno);
+       if (stat(argv[1], &sb) == 0) {
+           if (ModDiff (&flag, "users", sb.st_mtime))
+             exit(MR_DATE);
+           if (flag < 0) {
+               fprintf(stderr, "File %s does not need to be rebuilt.\n",
+                       argv[1]);
+               exit(MR_NO_CHANGE);
+           }
+       }
+       outf = argv[1];
+       sprintf(outft, "%s~", outf);
+       if ((out = fopen(outft, "w")) == NULL) {
+           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(-1);
+       exit(MR_ARGS);
+    } else {
+       outf = NULL;
     }
 
-##  ingres sms
-##  set lockmode on users where readlock = nolock
+    EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
 
-##  range of u is users
-##  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 {
-           trim(login);
-           trim(fullname);
-           trim(oa);
-           trim(op);
-           trim(hp);
-           trim(shell);
-           fprintf(out, "%s:*:%d:101:%s,%s,%s,%s:/mit/%s:%s\n",
-                   login, uid, fullname, oa, op, hp, login, shell);
-##  }
-##  inquire_equel(error = "errorno")
-    if (error)  {
-       fprintf(out, "Ingres error %d\n", error);
-       exit(error);
+    EXEC SQL DECLARE x CURSOR FOR SELECT 
+      login, uid, shell, fullname, office_addr, nickname,
+      office_phone, home_phone
+       FROM users WHERE status = 1;
+    EXEC SQL OPEN x;
+    while (1) {
+       EXEC SQL FETCH x INTO :login, :uid, :shell, :fullname, :oa, :nickname,
+       :op, :hp;
+       if (sqlca.sqlcode != 0) break;
+       strtrim(login);
+       strtrim(fullname);
+       strtrim(nickname);
+       strtrim(oa);
+       strtrim(op);
+       strtrim(hp);
+       strtrim(shell);
+       fprintf(out, "%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\n",
+               login, uid, fullname, nickname, oa, op, hp, login, shell);
     }
 
-##  exit
+    EXEC SQL CLOSE x;
+#ifsql INGRES
+    EXEC SQL DISCONNECT;
+#endsql
+#ifsql INFORMIX
+    EXEC SQL CLOSE DATABASE;
+#endsql
 
     if (fclose(out)) {
        perror("close failed");
-       exit(errno);
+       exit(MR_CCONFIG);
     }
-    exit(0);
-}
-
-trim(s)
-register char *s;
-{
-    register char *p;
+    if (outf)
+      fix_file(outf);
+    exit(MR_SUCCESS);
 
-    for (p = s; *s; s++)
-      if (*s != ' ')
-       p = s;
-    if (p != s) {
-       if (*p == ' ')
-         *p = 0;
-       else
-         p[1] = 0;
-    }
+ sqlerr:
+    com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
+    critical_alert("DCM", "Passwd build encountered INGRES ERROR %d",
+                  sqlca.sqlcode);
+    exit(MR_INGRES_ERR);
 }
This page took 0.057091 seconds and 4 git commands to generate.