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

Last change on this file since 15161 was 15161, checked in by kjdon, 14 years ago

not sure what this file does, but I have changed the colbuilder group to all-collections-editor, because colbuilder is no longer used

  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 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// this program should be compiled into a binary called setpw.exe (to be
37// short enough not to mess with 16 bit Windows platforms).
38
39// usage:
40// setpw -u username -p password -o output_gdbm_file
41
42#include "text_t.h"
43#include "crypt.h"
44#include "autoconf.h"
45#include "systems.h"
46#include "gdbmconst.h"
47#include "gdbm.h"
48
49#include <windows.h>
50
51text_t username;
52text_t password;
53text_t output_gdbm_file;
54
55bool parse_cmdline (LPSTR cmdline) {
56
57 bool in_quote = false;
58 text_t arg;
59 text_tarray args;
60 unsigned char *c = (unsigned char *)cmdline;
61 while (*c != '\0') {
62 if (*c == '"') {
63 if (!in_quote) {
64 in_quote = true;
65 } else {
66 in_quote = false;
67 if (!arg.empty()) args.push_back (arg);
68 arg.clear();
69 }
70 } else if (*c == ' ' && !in_quote) {
71 if (!arg.empty()) args.push_back (arg);
72 arg.clear();
73 } else {
74 arg.push_back (*c);
75 }
76 ++c;
77 }
78 if (!arg.empty()) args.push_back (arg);
79
80 text_tarray::const_iterator here = args.begin();
81 text_tarray::const_iterator end = args.end();
82 while (here != end) {
83 if (*here == "-u" && (++here != end)) username = *here;
84 else if (*here == "-p" && (++here != end)) password = *here;
85 else if (*here == "-o" && (++here != end)) output_gdbm_file = *here;
86 if (here != end) ++here;
87 }
88 if (username.empty() || password.empty() || output_gdbm_file.empty()) {
89 MessageBox (NULL, "Usage:\n setpasswd -u username -p password -o output_gdbm_file",
90 "setpasswd failed", MB_OK);
91 return false;
92 }
93 return true;
94}
95
96text_t crypt_text (const text_t &text) {
97 static const char *salt = "Tp";
98 text_t crypt_password;
99
100 if (text.empty()) return "";
101
102 // encrypt the password
103 char *text_cstr = text.getcstr();
104 if (text_cstr == NULL) return "";
105 crypt_password = crypt(text_cstr, salt);
106 delete []text_cstr;
107
108 return crypt_password;
109}
110
111bool add_to_db () {
112
113 int block_size = 0;
114 GDBM_FILE dbf;
115 char *dbname = output_gdbm_file.getcstr();
116
117 // open the database
118 int read_write = GDBM_WRCREAT;
119 dbf = gdbm_open (dbname, block_size, read_write, 00664, NULL, 1);
120 if (dbf == NULL) {
121 MessageBox (NULL, "gdbm_open failed\n", "setpasswd", MB_OK);
122 return false;
123 }
124
125 datum key_data;
126 key_data.dptr = username.getcstr();
127 if (key_data.dptr == NULL) {
128 MessageBox (NULL, "null key_data\n", "setpasswd", MB_OK);
129 return false;
130 }
131 key_data.dsize = strlen(key_data.dptr);
132
133 text_t value = "<comment>\n";
134 value += "<enabled>true\n";
135 value += "<groups>administrator,all-collections-editor\n";
136 value += "<password>" + password + "\n";
137 value += "<username>" + username + "\n";
138
139 datum value_data;
140 value_data.dptr = value.getcstr();
141 if (value_data.dptr == NULL) {
142 MessageBox (NULL, "null value_data\n", "setpasswd", MB_OK);
143 return false;
144 }
145 value_data.dsize = strlen(value_data.dptr);
146
147 // store the value
148 if (gdbm_store (dbf, key_data, value_data, GDBM_REPLACE) < 0) {
149 MessageBox (NULL, "gdbm_store failed\n", "setpasswd", MB_OK);
150 return false;
151 }
152 gdbm_close (dbf);
153
154 delete []key_data.dptr;
155 delete []value_data.dptr;
156 delete []dbname;
157 return true;
158}
159
160int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
161 LPSTR lpCmdLine, int nCmdShow) {
162
163 // parse command line arguments
164 if (!parse_cmdline (lpCmdLine)) return 1;
165
166 // encrypt the password
167 password = crypt_text (password);
168
169 // append the password and username to database
170 add_to_db();
171
172 return 0;
173}
174
175
176
Note: See TracBrowser for help on using the repository browser.