source: trunk/gsdl/src/setpasswd/setpasswd.cpp@ 1797

Last change on this file since 1797 was 1797, checked in by sjboddie, 23 years ago

Added setpasswd utility for encrypting passwords and adding them to
a database

  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1/**********************************************************************
2 *
3 * setpasswd.cpp --
4 * Copyright (C) 2000 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26// setpasswd is a windows application that can be used to encrypt a password
27// and write it (along with its corresponding username) to a gdbm database.
28
29// it handles writing to the gdbm database itself to avoid having to call
30// the txt2db console application (and therefore avoiding the console
31// window popping up when called from another windows application).
32
33// note that setpasswd does no checking to make sure that any of it's
34// input arguments are valid (or even reasonable) values.
35
36// usage:
37// setpasswd -u username -p password -o output_gdbm_file
38
39#include "text_t.h"
40#include "crypt.h"
41#include "autoconf.h"
42#include "systems.h"
43#include "gdbmconst.h"
44#include "gdbm.h"
45
46#include <windows.h>
47
48text_t username;
49text_t password;
50text_t output_gdbm_file;
51
52bool parse_cmdline (LPSTR cmdline) {
53
54 bool in_quote = false;
55 text_t arg;
56 text_tarray args;
57 unsigned char *c = (unsigned char *)cmdline;
58 while (*c != '\0') {
59 if (*c == '"') {
60 if (!in_quote) {
61 in_quote = true;
62 } else {
63 in_quote = false;
64 if (!arg.empty()) args.push_back (arg);
65 arg.clear();
66 }
67 } else if (*c == ' ' && !in_quote) {
68 if (!arg.empty()) args.push_back (arg);
69 arg.clear();
70 } else {
71 arg.push_back (*c);
72 }
73 c ++;
74 }
75 if (!arg.empty()) args.push_back (arg);
76
77 text_tarray::const_iterator here = args.begin();
78 text_tarray::const_iterator end = args.end();
79 while (here != end) {
80 if (*here == "-u" && (++here != end)) username = *here;
81 else if (*here == "-p" && (++here != end)) password = *here;
82 else if (*here == "-o" && (++here != end)) output_gdbm_file = *here;
83 if (here != end) here ++;
84 }
85 if (username.empty() || password.empty() || output_gdbm_file.empty()) {
86 MessageBox (NULL, "Usage:\n setpasswd -u username -p password -o output_gdbm_file",
87 "setpasswd failed", MB_OK);
88 return false;
89 }
90 return true;
91}
92
93text_t crypt_text (const text_t &text) {
94 static const char *salt = "Tp";
95 text_t crypt_password;
96
97 if (text.empty()) return "";
98
99 // encrypt the password
100 char *text_cstr = text.getcstr();
101 if (text_cstr == NULL) return "";
102 crypt_password = crypt(text_cstr, salt);
103 delete text_cstr;
104
105 return crypt_password;
106}
107
108bool add_to_db () {
109
110 int block_size = 0;
111 GDBM_FILE dbf;
112 char *dbname = output_gdbm_file.getcstr();
113
114 // open the database
115 int read_write = GDBM_WRCREAT;
116 dbf = gdbm_open (dbname, block_size, read_write, 00664, NULL, 1);
117 if (dbf == NULL) {
118 MessageBox (NULL, "gdbm_open failed\n", "setpasswd", MB_OK);
119 return false;
120 }
121
122 datum key_data;
123 key_data.dptr = username.getcstr();
124 if (key_data.dptr == NULL) {
125 MessageBox (NULL, "null key_data\n", "setpasswd", MB_OK);
126 return false;
127 }
128 key_data.dsize = strlen(key_data.dptr);
129
130 text_t value = "<comment>\n";
131 value += "<enabled>true\n";
132 value += "<groups>administrator,colbuilder\n";
133 value += "<password>" + password + "\n";
134 value += "<username>" + username + "\n";
135
136 datum value_data;
137 value_data.dptr = value.getcstr();
138 if (value_data.dptr == NULL) {
139 MessageBox (NULL, "null value_data\n", "setpasswd", MB_OK);
140 return false;
141 }
142 value_data.dsize = strlen(value_data.dptr);
143
144 // store the value
145 if (gdbm_store (dbf, key_data, value_data, GDBM_REPLACE) < 0) {
146 MessageBox (NULL, "gdbm_store failed\n", "setpasswd", MB_OK);
147 return false;
148 }
149 gdbm_close (dbf);
150
151 delete key_data.dptr;
152 delete value_data.dptr;
153 delete dbname;
154 return true;
155}
156
157int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
158 LPSTR lpCmdLine, int nCmdShow) {
159
160 // parse command line arguments
161 if (!parse_cmdline (lpCmdLine)) return 1;
162
163 // encrypt the password
164 password = crypt_text (password);
165
166 // append the password and username to database
167 add_to_db();
168
169 return 0;
170}
171
172
173
Note: See TracBrowser for help on using the repository browser.