Commit c11f1eac c11f1eac143a6685d43c0d0029e2b96bf0546b78 by Sergey Poznyakoff

(options): New option --permissions allows to override default permissions on the database file.

1 parent 54b4b266
...@@ -28,6 +28,8 @@ int db_make (char *input_name, char *output_name); ...@@ -28,6 +28,8 @@ int db_make (char *input_name, char *output_name);
28 #define ACT_LIST 3 28 #define ACT_LIST 3
29 #define ACT_CHPASS 4 29 #define ACT_CHPASS 4
30 30
31 static int permissions = 0600;
32
31 struct action_data { 33 struct action_data {
32 int action; 34 int action;
33 char *input_name; 35 char *input_name;
...@@ -80,6 +82,7 @@ static struct argp_option options[] = ...@@ -80,6 +82,7 @@ static struct argp_option options[] =
80 { "output", 'o', N_("FILE"), 0, N_("Direct output to file"), 3 }, 82 { "output", 'o', N_("FILE"), 0, N_("Direct output to file"), 3 },
81 { "password", 'p', N_("STRING"), 0, N_("Specify user's password"), 3 }, 83 { "password", 'p', N_("STRING"), 0, N_("Specify user's password"), 3 },
82 { "user", 'u', N_("USERNAME"), 0, N_("Specify user name"), 3 }, 84 { "user", 'u', N_("USERNAME"), 0, N_("Specify user name"), 3 },
85 { "permissions", 'P', N_("PERM"), 0, N_("Force given permissions on the database"), 3 },
83 { NULL, } 86 { NULL, }
84 }; 87 };
85 88
...@@ -98,6 +101,24 @@ static const char *popauth_argp_capa[] = { ...@@ -98,6 +101,24 @@ static const char *popauth_argp_capa[] = {
98 NULL 101 NULL
99 }; 102 };
100 103
104 static void
105 set_db_perms (struct argp_state *astate, char *opt, int *pperm)
106 {
107 int perm = 0;
108
109 if (isdigit(opt[0]))
110 {
111 char *p;
112 perm = strtoul (opt, &p, 8);
113 if (*p)
114 {
115 argp_error (astate, _("invalid octal number: %s"), opt);
116 exit (1);
117 }
118 }
119 *pperm = perm;
120 }
121
101 static error_t 122 static error_t
102 popauth_parse_opt (int key, char *arg, struct argp_state *astate) 123 popauth_parse_opt (int key, char *arg, struct argp_state *astate)
103 { 124 {
...@@ -150,6 +171,10 @@ popauth_parse_opt (int key, char *arg, struct argp_state *astate) ...@@ -150,6 +171,10 @@ popauth_parse_opt (int key, char *arg, struct argp_state *astate)
150 ap->username = optarg; 171 ap->username = optarg;
151 break; 172 break;
152 173
174 case 'P':
175 set_db_perms (astate, optarg, &permissions);
176 break;
177
153 case ARGP_KEY_FINI: 178 case ARGP_KEY_FINI:
154 if (ap->action == -1) 179 if (ap->action == -1)
155 { 180 {
...@@ -159,6 +184,7 @@ popauth_parse_opt (int key, char *arg, struct argp_state *astate) ...@@ -159,6 +184,7 @@ popauth_parse_opt (int key, char *arg, struct argp_state *astate)
159 else 184 else
160 ap->action = ACT_CHPASS; 185 ap->action = ACT_CHPASS;
161 } 186 }
187 break;
162 188
163 default: 189 default:
164 return ARGP_ERR_UNKNOWN; 190 return ARGP_ERR_UNKNOWN;
...@@ -206,7 +232,7 @@ check_user_perm (int action, struct action_data *ap) ...@@ -206,7 +232,7 @@ check_user_perm (int action, struct action_data *ap)
206 if (ap->action == ACT_ADD) 232 if (ap->action == ACT_ADD)
207 { 233 {
208 DBM_FILE db; 234 DBM_FILE db;
209 if (mu_dbm_open (ap->input_name, &db, MU_STREAM_CREAT, 0600)) 235 if (mu_dbm_open (ap->input_name, &db, MU_STREAM_CREAT, permissions))
210 { 236 {
211 mu_error (_("can't create %s: %s"), 237 mu_error (_("can't create %s: %s"),
212 ap->input_name, mu_strerror (errno)); 238 ap->input_name, mu_strerror (errno));
...@@ -253,7 +279,7 @@ action_list (struct action_data *ap) ...@@ -253,7 +279,7 @@ action_list (struct action_data *ap)
253 DBM_DATUM contents; 279 DBM_DATUM contents;
254 280
255 check_user_perm (ACT_LIST, ap); 281 check_user_perm (ACT_LIST, ap);
256 if (mu_dbm_open (ap->input_name, &db, MU_STREAM_READ, 0600)) 282 if (mu_dbm_open (ap->input_name, &db, MU_STREAM_READ, permissions))
257 { 283 {
258 mu_error (_("can't open %s: %s"), ap->input_name, mu_strerror (errno)); 284 mu_error (_("can't open %s: %s"), ap->input_name, mu_strerror (errno));
259 return 1; 285 return 1;
...@@ -338,7 +364,7 @@ action_create (struct action_data *ap) ...@@ -338,7 +364,7 @@ action_create (struct action_data *ap)
338 364
339 if (!ap->output_name) 365 if (!ap->output_name)
340 ap->output_name = APOP_PASSFILE; 366 ap->output_name = APOP_PASSFILE;
341 if (mu_dbm_open (ap->output_name, &db, MU_STREAM_CREAT, 0600)) 367 if (mu_dbm_open (ap->output_name, &db, MU_STREAM_CREAT, permissions))
342 { 368 {
343 mu_error (_("can't create %s: %s"), ap->output_name, mu_strerror (errno)); 369 mu_error (_("can't create %s: %s"), ap->output_name, mu_strerror (errno));
344 return 1; 370 return 1;
...@@ -398,7 +424,7 @@ open_io (int action, struct action_data *ap, DBM_FILE *db, int *not_owner) ...@@ -398,7 +424,7 @@ open_io (int action, struct action_data *ap, DBM_FILE *db, int *not_owner)
398 int rc = check_user_perm (action, ap); 424 int rc = check_user_perm (action, ap);
399 if (not_owner) 425 if (not_owner)
400 *not_owner = rc; 426 *not_owner = rc;
401 if (mu_dbm_open (ap->input_name, db, MU_STREAM_RDWR, 0600)) 427 if (mu_dbm_open (ap->input_name, db, MU_STREAM_RDWR, permissions))
402 { 428 {
403 mu_error (_("can't open %s: %s"), ap->input_name, mu_strerror (errno)); 429 mu_error (_("can't open %s: %s"), ap->input_name, mu_strerror (errno));
404 return 1; 430 return 1;
......