]> andersk Git - moira.git/blobdiff - clients/addusr/addusr.c
When we encounter a duplicate user and fail to add them, report their
[moira.git] / clients / addusr / addusr.c
index e70ee023108effcea0574978b366051199bbef6f..820adfc261eff7c43dea98c7ce58d16cb3002505 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id $
+/* $Id$
  *
  * Program to add users en masse to the moira database
  *
@@ -12,6 +12,7 @@
 #include <mit-copyright.h>
 #include <moira.h>
 #include <moira_site.h>
+#include <mrclient.h>
 
 #include <ctype.h>
 #include <errno.h>
@@ -26,9 +27,11 @@ RCSID("$Header$");
 #define DEFAULT_SHELL "/bin/csh"
 #endif
 
+#define DEFAULT_WINCONSOLESHELL "cmd"
+
 /* flags from command line */
-char *class, *comment, *status_str, *shell, *filename;
-int reg_only, reg, verbose, nodupcheck;
+char *class, *comment, *status_str, *shell, *winconsoleshell, *filename;
+int reg_only, reg, verbose, nodupcheck, securereg;
 
 /* argument parsing macro */
 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
@@ -36,29 +39,24 @@ int reg_only, reg, verbose, nodupcheck;
 char *whoami;
 int duplicate, errors;
 
-extern int ureg_init(void);
-extern int grab_login(char *first, char *last, char *idnumber,
-                     char *hashidnumber, char *login);
-extern int set_password(char *first, char *last, char *idnumber,
-                       char *hashidnumber, char *password);
-
-
 void usage(char **argv);
 int usercheck(int argc, char **argv, void *qargv);
+int get_uid(int argc, char **argv, void *qargv);
 
 int main(int argc, char **argv)
 {
   int status, lineno;
   char **arg = argv, *qargv[U_END];
-  char *motd, *p, *first, *middle, *last, *id, *login, *passwd, *server;
-  char buf[BUFSIZ], idbuf[32];
+  char *p, *first, *middle, *last, *id, *login, *server;
+  char buf[BUFSIZ];
   FILE *input;
 
   /* clear all flags & lists */
-  reg_only = reg = verbose = lineno = nodupcheck = errors = 0;
+  reg_only = reg = verbose = lineno = nodupcheck = errors = securereg = 0;
   server = NULL;
   filename = "-";
   shell = DEFAULT_SHELL;
+  winconsoleshell = DEFAULT_WINCONSOLESHELL;
   class = "TEMP";
   comment = "";
   status_str = "0";
@@ -110,6 +108,18 @@ int main(int argc, char **argv)
              else
                usage(argv);
            }
+         else if (argis("w", "winshell"))
+           {
+             if (arg - argv < argc - 1)
+               {
+                 ++arg;
+                 winconsoleshell = *arg;
+               }
+             else 
+               usage(argv);
+           }
+         else if (argis("6", "secure"))
+           securereg++;
          else if (argis("r", "reg_only"))
            reg_only++;
          else if (argis("R", "register"))
@@ -157,50 +167,19 @@ int main(int argc, char **argv)
        }
     }
 
-  if (!reg_only)
-    {
-      /* fire up Moira */
-      if ((status = mr_connect(server)))
-       {
-         com_err(whoami, status, "unable to connect to the Moira server");
-         exit(2);
-       }
-      if ((status = mr_motd(&motd)))
-       {
-         com_err(whoami, status, "unable to check server status");
-         exit(2);
-       }
-      if (motd)
-       {
-         fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
-                 motd);
-         mr_disconnect();
-         exit(2);
-       }
-
-      if ((status = mr_auth("addusr")))
-       {
-         if (status == MR_USER_AUTH)
-           com_err(whoami, status, "");
-         else
-           {
-             com_err(whoami, status, "unable to authenticate to Moira");
-             exit(2);
-           }
-       }
-    }
-
-  if (reg || reg_only)
-    ureg_init();
+  /* fire up Moira */
+  if (mrcl_connect(server, "addusr", 3, 1) != MRCL_SUCCESS)
+    exit(2);
 
   qargv[U_NAME] = UNIQUE_LOGIN;
   qargv[U_UID] = UNIQUE_UID;
   qargv[U_SHELL] = shell;
+  qargv[U_WINCONSOLESHELL] = winconsoleshell;
   qargv[U_STATE] = status_str;
   qargv[U_CLASS] = class;
   qargv[U_COMMENT] = comment;
   qargv[U_SIGNATURE] = "";
-  qargv[U_SECURE] = "0";
+  qargv[U_SECURE] = securereg ? "1" : "0";
   while (fgets(buf, BUFSIZ, input))
     {
       /* throw away terminating newline */
@@ -226,19 +205,6 @@ int main(int argc, char **argv)
        p++;
       if (reg_only || reg)
        {
-         while (!isspace(*p))
-           p--;
-         if (p <= first)
-           {
-             com_err(whoami, 0, "Missing password on line %d", lineno);
-             errors++;
-             continue;
-           }
-         passwd = strtrim(&p[1]);
-         *p-- = '\0';
-         /* previous field is login name */
-         while (isspace(*p))
-           p--;
          while (!isspace(*p))
            p--;
          if (p <= first)
@@ -302,7 +268,7 @@ int main(int argc, char **argv)
                {
                  com_err(whoami, status,
                          "checking to see if user %s %s already exists",
-                         qargv[0], qargv[1]);
+                         qargv[U_FIRST], qargv[U_LAST]);
                  com_err(whoami, 0, "NOT ADDING USER");
                  errors++;
                  continue;
@@ -310,7 +276,7 @@ int main(int argc, char **argv)
              if (duplicate > 0)
                {
                  com_err(whoami, MR_EXISTS, "user %s %s already exists",
-                         qargv[0], qargv[1]);
+                         qargv[U_FIRST], qargv[U_LAST]);
                  com_err(whoami, 0, "NOT ADDING USER");
                  errors++;
                  continue;
@@ -338,9 +304,26 @@ int main(int argc, char **argv)
        }
       if (reg || reg_only)
        {
-         EncryptID(idbuf, qargv[U_MITID], qargv[U_FIRST], qargv[U_LAST]);
-         status = grab_login(qargv[U_FIRST], qargv[U_LAST],
-                             qargv[U_MITID], idbuf, login);
+         char *gargv[2], *rargv[3], uid[10];
+
+         uid[0] = '\0';
+         gargv[0] = qargv[U_FIRST];
+         gargv[1] = qargv[U_LAST];
+         status = mr_query("get_user_account_by_name", 2, gargv,
+                           get_uid, &uid);
+         if (status)
+           {
+             com_err(whoami, status, "while looking up uid for %s %s",
+                     qargv[U_FIRST], qargv[U_LAST]);
+             errors++;
+             continue;
+           }
+
+         rargv[0] = uid;
+         rargv[1] = login;
+         rargv[2] = "IMAP";
+
+         status = mr_query("register_user", 3, rargv, NULL, NULL);
          if (status)
            {
              com_err(whoami, status, "while registering (login) %s %s",
@@ -353,16 +336,6 @@ int main(int argc, char **argv)
              printf("Registered user %s %s as %s\n", qargv[U_FIRST],
                     qargv[U_LAST], login);
            }
-         status = set_password(qargv[U_FIRST], qargv[U_LAST],
-                               qargv[U_MITID], idbuf, passwd);
-         if (status)
-           {
-             com_err(whoami, status, "While registering (passwd) %s %s",
-                     qargv[U_FIRST], qargv[U_LAST]);
-             errors++;
-             continue;
-           } else if (verbose)
-             printf("Set password for user %s\n", login);
        }
     }
 
@@ -379,6 +352,8 @@ void usage(char **argv)
   fprintf(stderr, "   -C | -comment \"comment\" (default \"\")\n");
   fprintf(stderr, "   -s | -status status (default 0)\n");
   fprintf(stderr, "   -h | -shell shell (default %s)\n", DEFAULT_SHELL);
+  fprintf(stderr, "   -w | -winshell windows console shell (default %s)\n",
+         DEFAULT_WINCONSOLESHELL);
   fprintf(stderr, "   -r | -reg_only\n");
   fprintf(stderr, "   -R | -register (and add to database)\n");
   fprintf(stderr, "   -v | -verbose\n");
@@ -399,3 +374,19 @@ int usercheck(int argc, char **argv, void *qargv)
 
   return MR_CONT;
 }
+
+/* query callback to get uid of a just-added account */
+int get_uid(int argc, char **argv, void *uidv)
+{
+  char *uid = uidv;
+
+  if (uid[0] == '\0')
+    strcpy(uid, argv[U_UID]);
+  else
+    {
+      if (!strcmp(argv[U_MODWITH], "addusr"))
+       strcpy(uid, argv[U_UID]);
+    }
+
+  return MR_CONT;
+}
This page took 0.199682 seconds and 4 git commands to generate.