MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
crypt.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/utils/crypt.c,v 1.18 2017/01/12 15:10:27 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  *
10  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
11  * via La Masa, 34 - 20156 Milano, Italy
12  * http://www.aero.polimi.it
13  *
14  * Changing this copyright notice is forbidden.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation (version 2 of the License).
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30 
31 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
32 
33 #include <stdio.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include "ac/getopt.h"
39 
40 #include <string.h>
41 
42 #include "crypt.h"
43 
44 static void
45 usage(int rc)
46 {
47  fprintf(stderr, "usage: crypt [-f salt] [-h] [-c asserted] cred\n");
48  exit(rc);
49 }
50 
51 int
52 main(int argc, char *argv[])
53 {
54 #ifndef HAVE_CRYPT
55  fprintf(stderr, "no valid crypt available\n");
56  exit(EXIT_FAILURE);
57 #else /* HAVE_CRYPT */
58 
59  char *salt_format = NULL, salt[35];
60  char *asserted_cred = NULL, asserted_cred_buf[35];
61  size_t asserted_cred_len = 0;
62 
63  while (1) {
64  int opt = getopt(argc, argv, "c:f:h");
65 
66  if (opt == EOF) {
67  break;
68  }
69 
70  switch (opt) {
71  case 'c':
72  asserted_cred = optarg;
73  break;
74 
75  case 'f':
76  salt_format = optarg;
77  break;
78 
79  case 'h':
80  usage(EXIT_SUCCESS);
81  break;
82 
83  default:
84  usage(EXIT_FAILURE);
85  }
86  }
87 
88  if (optind == argc) {
89  usage(EXIT_FAILURE);
90  }
91 
92  (void)mbdyn_make_salt(salt, sizeof(salt), salt_format);
93  salt[STRLENOF(salt)] = '\0';
94 
95  if (asserted_cred) {
96  if (strncmp(asserted_cred, "{CRYPT}", STRLENOF("{CRYPT}")) == 0) {
97  asserted_cred += STRLENOF("{CRYPT}");
98  } else {
99  memcpy(asserted_cred_buf, asserted_cred, sizeof(asserted_cred_buf));
100  asserted_cred_buf[STRLENOF(asserted_cred_buf)] = '\0';
101 
102  asserted_cred = crypt(asserted_cred_buf, salt);
103 
104  memcpy(asserted_cred_buf, asserted_cred, sizeof(asserted_cred_buf));
105  asserted_cred[STRLENOF(asserted_cred_buf)] = '\0';
106  asserted_cred = asserted_cred_buf;
107  }
108  asserted_cred_len = strlen(asserted_cred);
109  }
110 
111  argv = &argv[optind];
112  argc -= optind;
113 
114  for (; argc; argc--) {
115  char cred[35];
116  char *c;
117 
118  memcpy(cred, argv[0], sizeof(cred));
119  cred[STRLENOF(cred)] = '\0';
120 
121  if (asserted_cred) {
122  c = crypt(cred, asserted_cred);
123 
124  if (strcmp(c, asserted_cred) == 0) {
125  printf("%s OK\n", c);
126  } else {
127  printf("%s ERR\n", c);
128  }
129  } else {
130  c = crypt(cred, salt);
131  printf("%s\n", c);
132  }
133  }
134 
135  return EXIT_SUCCESS;
136 #endif /* HAVE_CRYPT */
137 }
int optind
Definition: getopt.c:72
static std::stack< cleanup * > c
Definition: cleanup.cc:59
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
#define STRLENOF(s)
Definition: mbdyn.h:166
char * mbdyn_make_salt(char *salt, size_t saltlen, const char *salt_format)
Definition: crypt.cc:40
int main(int argc, char *argv[])
Definition: crypt.c:52
char * optarg
Definition: getopt.c:74
static void usage(int rc)
Definition: crypt.c:45