dav/main.c

changeset 146
e48048334602
parent 144
c2c02c9b3be4
child 147
458a8dc68048
equal deleted inserted replaced
145:82475dc12dd4 146:e48048334602
34 #include <time.h> 34 #include <time.h>
35 #include <sys/types.h> 35 #include <sys/types.h>
36 #include <ucx/string.h> 36 #include <ucx/string.h>
37 #include <ucx/utils.h> 37 #include <ucx/utils.h>
38 #include <dirent.h> 38 #include <dirent.h>
39 #include <termios.h>
39 40
40 41
41 #include <libidav/utils.h> 42 #include <libidav/utils.h>
42 #include <libidav/crypto.h> 43 #include <libidav/crypto.h>
43 #include <libidav/session.h> 44 #include <libidav/session.h>
172 "with an optional path:\n"); 173 "with an optional path:\n");
173 fprintf(stderr, " <repository>/path/\n"); 174 fprintf(stderr, " <repository>/path/\n");
174 fprintf(stderr, "\n"); 175 fprintf(stderr, "\n");
175 } 176 }
176 177
178 char* password_input(char *prompt) {
179 fprintf(stderr, "%s", prompt);
180 fflush(stderr);
181
182 // hide terminal input
183 #ifdef _WIN32
184 // TODO
185 #else
186 struct termios oflags, nflags;
187 tcgetattr(fileno(stdin), &oflags);
188 nflags = oflags;
189 nflags.c_lflag &= ~ECHO;
190 nflags.c_lflag |= ECHONL;
191 if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
192 perror("tcsetattr");
193 }
194 #endif
195
196 // read password input
197 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
198 int c = 0;
199 while((c = getchar()) != EOF) {
200 if(c == '\n') {
201 break;
202 }
203 ucx_buffer_putc(buf, c);
204 }
205 ucx_buffer_putc(buf, 0);
206
207 // restore terminal settings
208 #ifdef _WIN32
209 // TODO
210 #else
211 if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
212 perror("tcsetattr");
213 }
214 #endif
215
216 char *str = buf->space;
217 free(buf); // only free the UcxBuffer struct
218 return str;
219 }
220
177 int request_auth(Repository *repo, DavSession *sn) { 221 int request_auth(Repository *repo, DavSession *sn) {
178 static int login = 0; 222 static int login = 0;
179 if(login) { 223 if(login) {
180 return 0; 224 return 0;
181 } 225 }
182 226
183 char *user = NULL; 227 char *user = NULL;
184 char *password = NULL;
185 char ubuf[256]; 228 char ubuf[256];
186 char pbuf[256];
187 if(repo->user) { 229 if(repo->user) {
188 user = repo->user; 230 user = repo->user;
189 } else { 231 } else {
190 printf("user: "); 232 fprintf(stderr, "user: ");
191 fflush(stdout); 233 fflush(stderr);
192 user = fgets(ubuf, 256, stdin); 234 user = fgets(ubuf, 256, stdin);
193 } 235 }
194 236
195 printf("password: "); 237 char *password = password_input("password: ");
196 fflush(stdout);
197 password = fgets(pbuf, 256, stdin);
198 238
199 size_t ulen = strlen(user); 239 size_t ulen = strlen(user);
200 if(user[ulen-1] == '\n') { 240 if(user[ulen-1] == '\n') {
201 user[ulen-1] = '\0'; 241 user[ulen-1] = '\0';
202 } 242 }
203 size_t plen = strlen(password);
204 if(password[plen-1] == '\n') {
205 password[plen-1] = '\0';
206 }
207 243
208 dav_session_set_auth(sn, user, password); 244 dav_session_set_auth(sn, user, password);
245 free(password);
209 login = 1; 246 login = 1;
210 return 1; 247 return 1;
211 } 248 }
212 249
213 Repository* url2repo(char *url, char **path) { 250 Repository* url2repo(char *url, char **path) {

mercurial