APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
Copyright (c) 2012-2019 Roger Light <roger@atchoo.org>
 
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
 
The Eclipse Public License is available at
   http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
  http://www.eclipse.org/org/documents/edl-v10.php.
 
Contributors:
   Roger Light - initial implementation and documentation.
*/
 
#include "config.h"
 
#include <errno.h>
#include <openssl/opensslv.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/buffer.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
#ifdef WIN32
#  include <windows.h>
#  include <process.h>
#    ifndef __cplusplus
#        if defined(_MSC_VER) && _MSC_VER < 1900
#            define bool char
#            define true 1
#            define false 0
#        else
#            include <stdbool.h>
#        endif
#    endif
#   define snprintf sprintf_s
#    include <io.h>
#    include <windows.h>
#else
#  include <stdbool.h>
#  include <unistd.h>
#  include <termios.h>
#  include <sys/stat.h>
#endif
 
#define MAX_BUFFER_LEN 1024
#define SALT_LEN 12
 
#ifdef WIN32
static FILE *mpw_tmpfile(void)
{
    return tmpfile();
}
#else
 
static char alphanum[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 
static unsigned char tmpfile_path[36];
static FILE *mpw_tmpfile(void)
{
    int fd;
    size_t i;
 
    if(RAND_bytes(tmpfile_path, sizeof(tmpfile_path)) != 1){
        return NULL;
    }
 
    strcpy((char *)tmpfile_path, "/tmp/");
 
    for(i=strlen((char *)tmpfile_path); i<sizeof(tmpfile_path)-8; i++){
        tmpfile_path[i] = alphanum[tmpfile_path[i]%(sizeof(alphanum)-1)];
    }
    tmpfile_path[sizeof(tmpfile_path)-8] = '-';
    for(i=sizeof(tmpfile_path)-7; i<sizeof(tmpfile_path)-1; i++){
        tmpfile_path[i] = 'X';
    }
    tmpfile_path[sizeof(tmpfile_path)-1] = '\0';
 
    umask(077);
    fd = mkstemp((char *)tmpfile_path);
    if(fd < 0) return NULL;
    unlink((char *)tmpfile_path);
 
    return fdopen(fd, "w+");
}
#endif
 
 
int base64_encode(unsigned char *in, unsigned int in_len, char **encoded)
{
    BIO *bmem, *b64;
    BUF_MEM *bptr;
 
    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    bmem = BIO_new(BIO_s_mem());
    b64 = BIO_push(b64, bmem);
    BIO_write(b64, in, in_len);
    if(BIO_flush(b64) != 1){
        BIO_free_all(b64);
        return 1;
    }
    BIO_get_mem_ptr(b64, &bptr);
    *encoded = malloc(bptr->length+1);
    if(!(*encoded)){
        BIO_free_all(b64);
        return 1;
    }
    memcpy(*encoded, bptr->data, bptr->length);
    (*encoded)[bptr->length] = '\0';
    BIO_free_all(b64);
 
    return 0;
}
 
 
void print_usage(void)
{
    printf("mosquitto_passwd is a tool for managing password files for mosquitto.\n\n");
    printf("Usage: mosquitto_passwd [-c | -D] passwordfile username\n");
    printf("       mosquitto_passwd -b passwordfile username password\n");
    printf("       mosquitto_passwd -U passwordfile\n");
    printf(" -b : run in batch mode to allow passing passwords on the command line.\n");
    printf(" -c : create a new password file. This will overwrite existing files.\n");
    printf(" -D : delete the username rather than adding/updating its password.\n");
    printf(" -U : update a plain text password file to use hashed passwords.\n");
    printf("\nSee https://mosquitto.org/ for more information.\n\n");
}
 
int output_new_password(FILE *fptr, const char *username, const char *password)
{
    int rc;
    unsigned char salt[SALT_LEN];
    char *salt64 = NULL, *hash64 = NULL;
    unsigned char hash[EVP_MAX_MD_SIZE];
    unsigned int hash_len;
    const EVP_MD *digest;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
    EVP_MD_CTX context;
#else
    EVP_MD_CTX *context;
#endif
 
    rc = RAND_bytes(salt, SALT_LEN);
    if(!rc){
        fprintf(stderr, "Error: Insufficient entropy available to perform password generation.\n");
        return 1;
    }
 
    rc = base64_encode(salt, SALT_LEN, &salt64);
    if(rc){
        free(salt64);
        fprintf(stderr, "Error: Unable to encode salt.\n");
        return 1;
    }
 
 
    digest = EVP_get_digestbyname("sha512");
    if(!digest){
        free(salt64);
        fprintf(stderr, "Error: Unable to create openssl digest.\n");
        return 1;
    }
 
#if OPENSSL_VERSION_NUMBER < 0x10100000L
    EVP_MD_CTX_init(&context);
    EVP_DigestInit_ex(&context, digest, NULL);
    EVP_DigestUpdate(&context, password, strlen(password));
    EVP_DigestUpdate(&context, salt, SALT_LEN);
    EVP_DigestFinal_ex(&context, hash, &hash_len);
    EVP_MD_CTX_cleanup(&context);
#else
    context = EVP_MD_CTX_new();
    EVP_DigestInit_ex(context, digest, NULL);
    EVP_DigestUpdate(context, password, strlen(password));
    EVP_DigestUpdate(context, salt, SALT_LEN);
    EVP_DigestFinal_ex(context, hash, &hash_len);
    EVP_MD_CTX_free(context);
#endif
 
    rc = base64_encode(hash, hash_len, &hash64);
    if(rc){
        free(salt64);
        free(hash64);
        fprintf(stderr, "Error: Unable to encode hash.\n");
        return 1;
    }
 
    fprintf(fptr, "%s:$6$%s$%s\n", username, salt64, hash64);
    free(salt64);
    free(hash64);
 
    return 0;
}
 
int delete_pwuser(FILE *fptr, FILE *ftmp, const char *username)
{
    char buf[MAX_BUFFER_LEN];
    char lbuf[MAX_BUFFER_LEN], *token;
    bool found = false;
    int line = 0;
 
    while(!feof(fptr) && fgets(buf, MAX_BUFFER_LEN, fptr)){
        line++;
        memcpy(lbuf, buf, MAX_BUFFER_LEN);
        token = strtok(lbuf, ":");
        if(!token){
            fprintf(stderr, "Error: Corrupt password file at line %d.\n", line);
            return 1;
        }
 
        if(strcmp(username, token)){
            fprintf(ftmp, "%s", buf);
        }else{
            found = true;
        }
    }
    if(!found){
        fprintf(stderr, "Warning: User %s not found in password file.\n", username);
    }
    return 0;
}
 
int update_file(FILE *fptr, FILE *ftmp)
{
    char buf[MAX_BUFFER_LEN];
    char lbuf[MAX_BUFFER_LEN];
    char *username, *password;
    int rc;
    int len;
 
    while(!feof(fptr) && fgets(buf, MAX_BUFFER_LEN, fptr)){
        memcpy(lbuf, buf, MAX_BUFFER_LEN);
        username = strtok(lbuf, ":");
        password = strtok(NULL, ":");
        if(password){
            len = strlen(password);
            while(len && (password[len-1] == '\n' || password[len-1] == '\r')){
                password[len-1] = '\0';
                len = strlen(password);
            }
            rc = output_new_password(ftmp, username, password);
            if(rc) return rc;
        }else{
            fprintf(ftmp, "%s", username);
        }
    }
    return 0;
}
 
int update_pwuser(FILE *fptr, FILE *ftmp, const char *username, const char *password)
{
    char buf[MAX_BUFFER_LEN];
    char lbuf[MAX_BUFFER_LEN], *token;
    bool found = false;
    int rc = 1;
    int line = 0;
 
    while(!feof(fptr) && fgets(buf, MAX_BUFFER_LEN, fptr)){
        line++;
        memcpy(lbuf, buf, MAX_BUFFER_LEN);
        token = strtok(lbuf, ":");
        if(!token){
            fprintf(stderr, "Error: Corrupt password file at line %d.\n", line);
            return 1;
        }
 
        if(strcmp(username, token)){
            fprintf(ftmp, "%s", buf);
        }else{
            rc = output_new_password(ftmp, username, password);
            found = true;
        }
    }
    if(found){
        return rc;
    }else{
        return output_new_password(ftmp, username, password);
    }
}
 
int gets_quiet(char *s, int len)
{
#ifdef WIN32
    HANDLE h;
    DWORD con_orig, con_quiet = 0;
    DWORD read_len = 0;
 
    memset(s, 0, len);
    h  = GetStdHandle(STD_INPUT_HANDLE);
    GetConsoleMode(h, &con_orig);
    con_quiet = con_orig;
    con_quiet &= ~ENABLE_ECHO_INPUT;
    con_quiet |= ENABLE_LINE_INPUT;
    SetConsoleMode(h, con_quiet);
    if(!ReadConsole(h, s, len, &read_len, NULL)){
        SetConsoleMode(h, con_orig);
        return 1;
    }
    while(s[strlen(s)-1] == 10 || s[strlen(s)-1] == 13){
        s[strlen(s)-1] = 0;
    }
    if(strlen(s) == 0){
        return 1;
    }
    SetConsoleMode(h, con_orig);
 
    return 0;
#else
    struct termios ts_quiet, ts_orig;
    char *rs;
 
    memset(s, 0, len);
    tcgetattr(0, &ts_orig);
    ts_quiet = ts_orig;
    ts_quiet.c_lflag &= ~(ECHO | ICANON);
    tcsetattr(0, TCSANOW, &ts_quiet);
 
    rs = fgets(s, len, stdin);
    tcsetattr(0, TCSANOW, &ts_orig);
 
    if(!rs){
        return 1;
    }else{
        while(s[strlen(s)-1] == 10 || s[strlen(s)-1] == 13){
            s[strlen(s)-1] = 0;
        }
        if(strlen(s) == 0){
            return 1;
        }
    }
    return 0;
#endif
}
 
int get_password(char *password, int len)
{
    char pw1[MAX_BUFFER_LEN], pw2[MAX_BUFFER_LEN];
 
    printf("Password: ");
    fflush(stdout);
    if(gets_quiet(pw1, MAX_BUFFER_LEN)){
        fprintf(stderr, "Error: Empty password.\n");
        return 1;
    }
    printf("\n");
 
    printf("Reenter password: ");
    fflush(stdout);
    if(gets_quiet(pw2, MAX_BUFFER_LEN)){
        fprintf(stderr, "Error: Empty password.\n");
        return 1;
    }
    printf("\n");
 
    if(strcmp(pw1, pw2)){
        fprintf(stderr, "Error: Passwords do not match.\n");
        return 1;
    }
 
    strncpy(password, pw1, len);
    return 0;
}
 
int copy_contents(FILE *src, FILE *dest)
{
    char buf[MAX_BUFFER_LEN];
    size_t len;
 
    rewind(src);
    rewind(dest);
    
#ifdef WIN32
    _chsize(fileno(dest), 0);
#else
    if(ftruncate(fileno(dest), 0)) return 1;
#endif
 
    while(!feof(src)){
        len = fread(buf, 1, MAX_BUFFER_LEN, src);
        if(len > 0){
            if(fwrite(buf, 1, len, dest) != len){
                return 1;
            }
        }else{
            return !feof(src);
        }
    }
    return 0;
}
 
int create_backup(const char *backup_file, FILE *fptr)
{
    FILE *fbackup;
 
    fbackup = fopen(backup_file, "wt");
    if(!fbackup){
        fprintf(stderr, "Error creating backup password file \"%s\", not continuing.\n", backup_file);
        return 1;
    }
    if(copy_contents(fptr, fbackup)){
        fprintf(stderr, "Error copying data to backup password file \"%s\", not continuing.\n", backup_file);
        fclose(fbackup);
        return 1;
    }
    fclose(fbackup);
    rewind(fptr);
    return 0;
}
void handle_sigint(int signal)
{
#ifndef WIN32
    struct termios ts;
 
    tcgetattr(0, &ts);
    ts.c_lflag |= ECHO | ICANON;
    tcsetattr(0, TCSANOW, &ts);
#endif
 
    UNUSED(signal);
 
    exit(0);
}
 
int main(int argc, char *argv[])
{
    char *password_file_tmp = NULL;
    char *password_file = NULL;
    char *username = NULL;
    char *password_cmd = NULL;
    bool batch_mode = false;
    bool create_new = false;
    bool delete_user = false;
    FILE *fptr, *ftmp;
    char password[MAX_BUFFER_LEN];
    int rc;
    bool do_update_file = false;
    char *backup_file;
 
    signal(SIGINT, handle_sigint);
    signal(SIGTERM, handle_sigint);
 
#if OPENSSL_VERSION_NUMBER < 0x10100000L || OPENSSL_API_COMPAT < 0x10100000L
    OpenSSL_add_all_digests();
#else
    OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS \
            | OPENSSL_INIT_ADD_ALL_DIGESTS \
            | OPENSSL_INIT_LOAD_CONFIG, NULL);
#endif
 
    if(argc == 1){
        print_usage();
        return 1;
    }
 
    if(!strcmp(argv[1], "-c")){
        create_new = true;
        if(argc != 4){
            fprintf(stderr, "Error: -c argument given but password file or username missing.\n");
            return 1;
        }else{
            password_file_tmp = argv[2];
            username = argv[3];
        }
    }else if(!strcmp(argv[1], "-D")){
        delete_user = true;
        if(argc != 4){
            fprintf(stderr, "Error: -D argument given but password file or username missing.\n");
            return 1;
        }else{
            password_file_tmp = argv[2];
            username = argv[3];
        }
    }else if(!strcmp(argv[1], "-b")){
        batch_mode = true;
        if(argc != 5){
            fprintf(stderr, "Error: -b argument given but password file, username or password missing.\n");
            return 1;
        }else{
            password_file_tmp = argv[2];
            username = argv[3];
            password_cmd = argv[4];
        }
    }else if(!strcmp(argv[1], "-U")){
        if(argc != 3){
            fprintf(stderr, "Error: -U argument given but password file missing.\n");
            return 1;
        }else{
            do_update_file = true;
            password_file_tmp = argv[2];
        }
    }else if(argc == 3){
        password_file_tmp = argv[1];
        username = argv[2];
    }else{
        print_usage();
        return 1;
    }
 
#ifdef WIN32
    password_file = _fullpath(NULL, password_file_tmp, 0);
    if(!password_file){
        fprintf(stderr, "Error getting full path for password file.\n");
        return 1;
    }
#else
    password_file = realpath(password_file_tmp, NULL);
    if(!password_file){
        if(errno == ENOENT){
            password_file = strdup(password_file_tmp);
            if(!password_file){
                fprintf(stderr, "Error: Out of memory.\n");
                return 1;
            }
        }else{
            fprintf(stderr, "Error reading password file: %s\n", strerror(errno));
            return 1;
        }
    }
#endif
 
    if(create_new){
        rc = get_password(password, 1024);
        if(rc){
            free(password_file);
            return rc;
        }
        fptr = fopen(password_file, "wt");
        if(!fptr){
            fprintf(stderr, "Error: Unable to open file %s for writing. %s.\n", password_file, strerror(errno));
            free(password_file);
            return 1;
        }
        free(password_file);
        rc = output_new_password(fptr, username, password);
        fclose(fptr);
        return rc;
    }else{
        fptr = fopen(password_file, "r+t");
        if(!fptr){
            fprintf(stderr, "Error: Unable to open password file %s. %s.\n", password_file, strerror(errno));
            free(password_file);
            return 1;
        }
 
        backup_file = malloc(strlen(password_file)+5);
        if(!backup_file){
            fprintf(stderr, "Error: Out of memory.\n");
            free(password_file);
            return 1;
        }
        snprintf(backup_file, strlen(password_file)+5, "%s.tmp", password_file);
        free(password_file);
        password_file = NULL;
 
        if(create_backup(backup_file, fptr)){
            fclose(fptr);
            free(backup_file);
            return 1;
        }
 
        ftmp = mpw_tmpfile();
        if(!ftmp){
            fprintf(stderr, "Error: Unable to open temporary file. %s.\n", strerror(errno));
            fclose(fptr);
            free(backup_file);
            return 1;
        }
        if(delete_user){
            rc = delete_pwuser(fptr, ftmp, username);
        }else if(do_update_file){
            rc = update_file(fptr, ftmp);
        }else{
            if(batch_mode){
                /* Update password for individual user */
                rc = update_pwuser(fptr, ftmp, username, password_cmd);
            }else{
                rc = get_password(password, 1024);
                if(rc){
                    fclose(fptr);
                    fclose(ftmp);
                    unlink(backup_file);
                    free(backup_file);
                    return rc;
                }
                /* Update password for individual user */
                rc = update_pwuser(fptr, ftmp, username, password);
            }
        }
        if(rc){
            fclose(fptr);
            fclose(ftmp);
            unlink(backup_file);
            free(backup_file);
            return rc;
        }
 
        if(copy_contents(ftmp, fptr)){
            fclose(fptr);
            fclose(ftmp);
            fprintf(stderr, "Error occurred updating password file.\n");
            fprintf(stderr, "Password file may be corrupt, check the backup file: %s.\n", backup_file);
            free(backup_file);
            return 1;
        }
        fclose(fptr);
        fclose(ftmp);
 
        /* Everything was ok so backup no longer needed. May contain old
         * passwords so shouldn't be kept around. */
        unlink(backup_file);
        free(backup_file);
    }
 
    return 0;
}