MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
crypt.c File Reference
#include "mbconfig.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "ac/getopt.h"
#include <string.h>
#include "crypt.h"
Include dependency graph for crypt.c:

Go to the source code of this file.

Functions

static void usage (int rc)
 
int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 52 of file crypt.c.

References c, getopt(), mbdyn_make_salt(), optarg, optind, STRLENOF, and usage().

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
char * optarg
Definition: getopt.c:74
static void usage(int rc)
Definition: crypt.c:45

Here is the call graph for this function:

static void usage ( int  rc)
static

Definition at line 45 of file crypt.c.

Referenced by main().

46 {
47  fprintf(stderr, "usage: crypt [-f salt] [-h] [-c asserted] cred\n");
48  exit(rc);
49 }