]> git.saurik.com Git - apple/system_cmds.git/blobdiff - chkpasswd.tproj/passwd.c
system_cmds-880.40.5.tar.gz
[apple/system_cmds.git] / chkpasswd.tproj / passwd.c
index f422f05a2791de11550bc5d29352e3be8a7ebb26..bff82809172ed282d614a8eee491efeeb2a42fd3 100644 (file)
@@ -1,30 +1,30 @@
 /*
 /*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  *
  * @APPLE_LICENSE_HEADER_START@
- * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
- * 
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
- * 
+ *
+ * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
+ * Reserved.  This file contains Original Code and/or Modifications of
+ * Original Code as defined in and that are subject to the Apple Public
+ * Source License Version 1.0 (the 'License').  You may not use this file
+ * except in compliance with the License.  Please obtain a copy of the
+ * License at http://www.apple.com/publicsource and read it before using
+ * this file.
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
- * limitations under the License.
- * 
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
+ * License for the specific language governing rights and limitations
+ * under the License."
+ *
  * @APPLE_LICENSE_HEADER_END@
  */
  * @APPLE_LICENSE_HEADER_END@
  */
-#define INFO_NETINFO 0
 #define INFO_FILE 1
 #define INFO_NIS 2
 #define INFO_FILE 1
 #define INFO_NIS 2
+#define INFO_OPEN_DIRECTORY 3
+#define INFO_PAM 4
 
 #ifndef __SLICK__
 #define _PASSWD_FILE "/etc/master.passwd"
 
 #ifndef __SLICK__
 #define _PASSWD_FILE "/etc/master.passwd"
 #include <ctype.h>
 #include <string.h>
 #include <pwd.h>
 #include <ctype.h>
 #include <string.h>
 #include <pwd.h>
-#include <netinfo/ni.h>
 #include "stringops.h"
 
 #ifdef __SLICK__
 #define _PASSWORD_LEN 8
 #endif
 
 #include "stringops.h"
 
 #ifdef __SLICK__
 #define _PASSWORD_LEN 8
 #endif
 
-static int literal = 0;
+#include "passwd.h"
 
 
-extern int file_check_passwd(char *, char *);
-extern int netinfo_check_passwd(char *, char *);
-extern int nis_check_passwd(char *, char *);
+const char* progname = "chkpasswd";
+
+static int literal = 0;
 
 void
 checkpasswd(char *name, char *old_pw)
 
 void
 checkpasswd(char *name, char *old_pw)
@@ -78,96 +77,98 @@ checkpasswd(char *name, char *old_pw)
        return;
 }
 
        return;
 }
 
-void
-usage()
+static void
+usage(void)
 {
        fprintf(stderr, "usage: chkpasswd [-i infosystem] [-l location] [-c] [name]\n");
 {
        fprintf(stderr, "usage: chkpasswd [-i infosystem] [-l location] [-c] [name]\n");
-       fprintf(stderr, "supported infosystems are:\n");
-       fprintf(stderr, "    netinfo\n");
+       fprintf(stderr, "  infosystem:\n");
        fprintf(stderr, "    file\n");
        fprintf(stderr, "    file\n");
-       fprintf(stderr, "    nis\n");
-       fprintf(stderr, "for netinfo, location may be a domain name or server/tag\n");
-       fprintf(stderr, "for file, location may be a file name (%s is the default)\n",
-               _PASSWD_FILE);
-       fprintf(stderr, "for nis, location may be a NIS domainname\n");
-       fprintf(stderr, "if -c is specified, the password you supply is compared\n");
-       fprintf(stderr, "verbatim without first being crypted\n");
+       fprintf(stderr, "    NIS\n");
+       fprintf(stderr, "    OpenDirectory\n");
+       fprintf(stderr, "  location (for infosystem):\n");
+       fprintf(stderr, "    file           location is path to file (default is %s)\n", _PASSWD_FILE);
+       fprintf(stderr, "    NIS            location is NIS domain name\n");
+       fprintf(stderr, "    OpenDirectory  location is directory node name\n");
+       fprintf(stderr, "  -c: supplied password is compared verbatim without first\n");
+       fprintf(stderr, "      being crypted\n");
        exit(1);
 }
 
 int
 main(int argc, char *argv[])
 {
        exit(1);
 }
 
 int
 main(int argc, char *argv[])
 {
-       char *user, *locn;
-       int i, infosystem;
-       struct passwd *pw;
-
-       infosystem = INFO_NETINFO;
-       user = NULL;
-       locn = NULL;
-
-       for (i = 1; i < argc; i++)
-       {
-               if (!strcmp(argv[i], "-i"))
-               {
-                       if (++i >= argc)
-                       {
-                               fprintf(stderr, "no argument for -i option\n");
-                               usage();
-                       }
-
-                       if (!strcmp(argv[i], "NetInfo")) infosystem = INFO_NETINFO;
-                       else if (!strcmp(argv[i], "netinfo")) infosystem = INFO_NETINFO;
-                       else if (!strcmp(argv[i], "File")) infosystem = INFO_FILE;
-                       else if (!strcmp(argv[i], "file")) infosystem = INFO_FILE;
-                       else if (!strcmp(argv[i], "NIS")) infosystem = INFO_NIS;
-                       else if (!strcmp(argv[i], "nis")) infosystem = INFO_NIS;
-                       else if (!strcmp(argv[i], "YP")) infosystem = INFO_NIS;
-                       else if (!strcmp(argv[i], "yp")) infosystem = INFO_NIS;
-                       else
-                       {
-                               fprintf(stderr, "unknown info system \"%s\"\n", argv[i]);
-                               usage();
-                       }
-               }
-
-               else if (!strcmp(argv[i], "-l"))
-               {
-                       if (++i >= argc)
-                       {
-                               fprintf(stderr, "no argument for -l option\n");
+       char* user = NULL;
+       char* locn = NULL;
+       int infosystem, ch;
+
+       infosystem = INFO_PAM;
+
+       while ((ch = getopt(argc, argv, "ci:l:")) != -1) {
+               switch(ch) {
+               case 'i':
+                       if (!strcasecmp(optarg, "file")) {
+                               infosystem = INFO_FILE;
+                       } else if (!strcasecmp(optarg, "NIS")) {
+                               infosystem = INFO_NIS;
+                       } else if (!strcasecmp(optarg, "YP")) {
+                               infosystem = INFO_NIS;
+                       } else if (!strcasecmp(optarg, "opendirectory")) {
+                               infosystem = INFO_OPEN_DIRECTORY;
+                       } else if (!strcasecmp(optarg, "PAM")) {
+                               infosystem = INFO_PAM;
+                       } else {
+                               fprintf(stderr, "%s: Unknown info system \'%s\'.\n",
+                                       progname, optarg);
                                usage();
                        }
                                usage();
                        }
-                       locn = argv[i];
+                       break;
+               case 'l':
+                       locn = optarg;
+                       break;
+               case 'c':
+                       literal++;
+                       break;
+               case '?':
+               default:
+                       usage();
+                       break;
                }
                }
+       }
+       argc -= optind;
+       argv += optind;
 
 
-               else if (!strcmp(argv[i], "-c")) literal++;
-               else if (user == NULL) user = argv[i];
-               else usage();
+       if (argc > 1) {
+               usage();
+       } else if (argc == 1) {
+               user = argv[0];
        }
 
        }
 
-       if (user == NULL)
-       {
-               if ((pw = getpwuid(getuid())) == NULL || (user = pw->pw_name) == NULL)
-               {
+       if (user == NULL) {
+               struct passwd* pw = getpwuid(getuid());
+               if (pw != NULL && pw->pw_name != NULL) {
+                       user = strdup(pw->pw_name);
+               }
+               if (user == NULL) {
                        fprintf(stderr, "you don't have a login name\n");
                        exit(1);
                }
        }
                        fprintf(stderr, "you don't have a login name\n");
                        exit(1);
                }
        }
-       
+
        switch (infosystem)
        {
        switch (infosystem)
        {
-               case INFO_NETINFO:
-                       netinfo_check_passwd(user, locn);
-                       break;
                case INFO_FILE:
                        file_check_passwd(user, locn);
                        break;
                case INFO_NIS:
                        nis_check_passwd(user, locn);
                        break;
                case INFO_FILE:
                        file_check_passwd(user, locn);
                        break;
                case INFO_NIS:
                        nis_check_passwd(user, locn);
                        break;
+               case INFO_OPEN_DIRECTORY:
+                       od_check_passwd(user, locn);
+                       break;
+               case INFO_PAM:
+                       pam_check_passwd(user);
+                       break;
        }
 
        exit(0);
 }
        }
 
        exit(0);
 }
-