mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
userdb: Add support for receive-only users
Some use cases, like receive-only MTAs, need domain users for receiving emails, but have no real need for passwords since they will never use submission. Today, that is not supported, and those use-cases require the administrator to come up with a password unnecessarily, adding complexity and possibly risk. This patch implements "receive-only users", which don't have a valid password, thus exist for the purposes of delivering mail, but always fail authentication. See https://github.com/albertito/chasquid/issues/44 for more details and rationale. Thanks to xavierg who suggested this feature on IRC.
This commit is contained in:
@@ -29,7 +29,7 @@ import (
|
|||||||
// Usage to show users on --help or invocation errors.
|
// Usage to show users on --help or invocation errors.
|
||||||
const usage = `
|
const usage = `
|
||||||
Usage:
|
Usage:
|
||||||
chasquid-util [options] user-add <user@domain> [--password=<password>]
|
chasquid-util [options] user-add <user@domain> [--password=<password>] [--receive_only]
|
||||||
chasquid-util [options] user-remove <user@domain>
|
chasquid-util [options] user-remove <user@domain>
|
||||||
chasquid-util [options] authenticate <user@domain> [--password=<password>]
|
chasquid-util [options] authenticate <user@domain> [--password=<password>]
|
||||||
chasquid-util [options] check-userdb <domain>
|
chasquid-util [options] check-userdb <domain>
|
||||||
@@ -140,12 +140,25 @@ func checkUserDB() {
|
|||||||
fmt.Println("Database loaded")
|
fmt.Println("Database loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
// chasquid-util user-add <user@domain> [--password=<password>]
|
// chasquid-util user-add <user@domain> [--password=<password>] [--receive_only]
|
||||||
func userAdd() {
|
func userAdd() {
|
||||||
user, _, db := userDBFromArgs(true)
|
user, _, db := userDBFromArgs(true)
|
||||||
password := getPassword()
|
|
||||||
|
|
||||||
err := db.AddUser(user, password)
|
_, recvOnly := args["--receive_only"]
|
||||||
|
_, hasPassword := args["--password"]
|
||||||
|
|
||||||
|
if recvOnly && hasPassword {
|
||||||
|
Fatalf("Cannot specify both --receive_only and --password")
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if recvOnly {
|
||||||
|
err = db.AddDeniedUser(user)
|
||||||
|
} else {
|
||||||
|
password := getPassword()
|
||||||
|
err = db.AddUser(user, password)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatalf("Error adding user: %v", err)
|
Fatalf("Error adding user: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,17 @@ if ! r user-add user@domain --password=passwd > /dev/null; then
|
|||||||
fi
|
fi
|
||||||
check_userdb
|
check_userdb
|
||||||
|
|
||||||
|
if ! r user-add denied@domain --receive_only > /dev/null; then
|
||||||
|
echo user-add --receive_only failed
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
check_userdb
|
||||||
|
|
||||||
|
if r user-add xxx@domain --password=passwd --receive_only > /dev/null; then
|
||||||
|
echo user-add --password --receive_only worked
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
if ! r authenticate user@domain --password=passwd > /dev/null; then
|
if ! r authenticate user@domain --password=passwd > /dev/null; then
|
||||||
echo authenticate failed
|
echo authenticate failed
|
||||||
exit 1
|
exit 1
|
||||||
@@ -44,6 +55,11 @@ if r authenticate user@domain --password=abcd > /dev/null; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if r authenticate denied@domain --password=abcd > /dev/null; then
|
||||||
|
echo authenticate on a no-submission user worked
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Interactive authentication.
|
# Interactive authentication.
|
||||||
# Need to wrap the execution under "script" since the interaction requires an
|
# Need to wrap the execution under "script" since the interaction requires an
|
||||||
# actual TTY, and that's a fairly portable way to do that.
|
# actual TTY, and that's a fairly portable way to do that.
|
||||||
|
|||||||
@@ -133,7 +133,7 @@
|
|||||||
.\" ========================================================================
|
.\" ========================================================================
|
||||||
.\"
|
.\"
|
||||||
.IX Title "chasquid-util 1"
|
.IX Title "chasquid-util 1"
|
||||||
.TH chasquid-util 1 "2023-07-29" "" ""
|
.TH chasquid-util 1 "2023-12-03" "" ""
|
||||||
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
||||||
.\" way too many mistakes in technical documents.
|
.\" way too many mistakes in technical documents.
|
||||||
.if n .ad l
|
.if n .ad l
|
||||||
@@ -142,7 +142,7 @@
|
|||||||
chasquid\-util \- chasquid management tool
|
chasquid\-util \- chasquid management tool
|
||||||
.SH "SYNOPSIS"
|
.SH "SYNOPSIS"
|
||||||
.IX Header "SYNOPSIS"
|
.IX Header "SYNOPSIS"
|
||||||
\&\fBchasquid-util\fR [\fIoptions\fR] user-add \fIuser@domain\fR [\-\-password=\fIpassword\fR]
|
\&\fBchasquid-util\fR [\fIoptions\fR] user-add \fIuser@domain\fR [\-\-password=\fIpassword\fR] [\-\-receive_only]
|
||||||
.PP
|
.PP
|
||||||
\&\fBchasquid-util\fR [\fIoptions\fR] user-remove \fIuser@domain\fR
|
\&\fBchasquid-util\fR [\fIoptions\fR] user-remove \fIuser@domain\fR
|
||||||
.PP
|
.PP
|
||||||
@@ -160,9 +160,12 @@ chasquid\-util \- chasquid management tool
|
|||||||
chasquid-util is a command-line utility for \fBchasquid\fR\|(1) operations.
|
chasquid-util is a command-line utility for \fBchasquid\fR\|(1) operations.
|
||||||
.SH "OPTIONS"
|
.SH "OPTIONS"
|
||||||
.IX Header "OPTIONS"
|
.IX Header "OPTIONS"
|
||||||
.IP "\fBuser-add\fR \fIuser@domain\fR [\-\-password=\fIpassword\fR]" 8
|
.IP "\fBuser-add\fR \fIuser@domain\fR [\-\-password=\fIpassword\fR] [\-\-receive_only]" 8
|
||||||
.IX Item "user-add user@domain [--password=password]"
|
.IX Item "user-add user@domain [--password=password] [--receive_only]"
|
||||||
Add a new user to the domain.
|
Add a new user to the domain.
|
||||||
|
.Sp
|
||||||
|
If \fI\-\-receive_only\fR is given, then the user will never successfully
|
||||||
|
authenticate. This is useful when creating receive-only users.
|
||||||
.IP "\fBuser-remove\fR \fIuser@domain\fR" 8
|
.IP "\fBuser-remove\fR \fIuser@domain\fR" 8
|
||||||
.IX Item "user-remove user@domain"
|
.IX Item "user-remove user@domain"
|
||||||
Remove the user from the domain.
|
Remove the user from the domain.
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ chasquid-util - chasquid management tool
|
|||||||
|
|
||||||
# SYNOPSIS
|
# SYNOPSIS
|
||||||
|
|
||||||
**chasquid-util** \[_options_\] user-add _user@domain_ \[--password=_password_\]
|
**chasquid-util** \[_options_\] user-add _user@domain_ \[--password=_password_\] \[--receive\_only\]
|
||||||
|
|
||||||
**chasquid-util** \[_options_\] user-remove _user@domain_
|
**chasquid-util** \[_options_\] user-remove _user@domain_
|
||||||
|
|
||||||
@@ -24,10 +24,13 @@ chasquid-util is a command-line utility for [chasquid(1)](chasquid.1.md) operati
|
|||||||
|
|
||||||
# OPTIONS
|
# OPTIONS
|
||||||
|
|
||||||
- **user-add** _user@domain_ \[--password=_password_\]
|
- **user-add** _user@domain_ \[--password=_password_\] \[--receive\_only\]
|
||||||
|
|
||||||
Add a new user to the domain.
|
Add a new user to the domain.
|
||||||
|
|
||||||
|
If _--receive\_only_ is given, then the user will never successfully
|
||||||
|
authenticate. This is useful when creating receive-only users.
|
||||||
|
|
||||||
- **user-remove** _user@domain_
|
- **user-remove** _user@domain_
|
||||||
|
|
||||||
Remove the user from the domain.
|
Remove the user from the domain.
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ chasquid-util - chasquid management tool
|
|||||||
|
|
||||||
=head1 SYNOPSIS
|
=head1 SYNOPSIS
|
||||||
|
|
||||||
B<chasquid-util> [I<options>] user-add I<user@domain> [--password=I<password>]
|
B<chasquid-util> [I<options>] user-add I<user@domain> [--password=I<password>] [--receive_only]
|
||||||
|
|
||||||
B<chasquid-util> [I<options>] user-remove I<user@domain>
|
B<chasquid-util> [I<options>] user-remove I<user@domain>
|
||||||
|
|
||||||
@@ -28,10 +28,13 @@ chasquid-util is a command-line utility for chasquid(1) operations.
|
|||||||
|
|
||||||
=over 8
|
=over 8
|
||||||
|
|
||||||
=item B<user-add> I<user@domain> [--password=I<password>]
|
=item B<user-add> I<user@domain> [--password=I<password>] [--receive_only]
|
||||||
|
|
||||||
Add a new user to the domain.
|
Add a new user to the domain.
|
||||||
|
|
||||||
|
If I<--receive_only> is given, then the user will never successfully
|
||||||
|
authenticate. This is useful when creating receive-only users.
|
||||||
|
|
||||||
=item B<user-remove> I<user@domain>
|
=item B<user-remove> I<user@domain>
|
||||||
|
|
||||||
Remove the user from the domain.
|
Remove the user from the domain.
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ func (p *Password) PasswordMatches(plain string) bool {
|
|||||||
return s.Scrypt.PasswordMatches(plain)
|
return s.Scrypt.PasswordMatches(plain)
|
||||||
case *Password_Plain:
|
case *Password_Plain:
|
||||||
return s.Plain.PasswordMatches(plain)
|
return s.Plain.PasswordMatches(plain)
|
||||||
|
case *Password_Denied:
|
||||||
|
return false
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -164,6 +166,22 @@ func (db *DB) AddUser(name, plainPassword string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddDenied to the database. If the user is already present, override it.
|
||||||
|
// Note we enforce that the name has been normalized previously.
|
||||||
|
func (db *DB) AddDeniedUser(name string) error {
|
||||||
|
if norm, err := normalize.User(name); err != nil || name != norm {
|
||||||
|
return errors.New("invalid username")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.mu.Lock()
|
||||||
|
db.db.Users[name] = &Password{
|
||||||
|
Scheme: &Password_Denied{&Denied{}},
|
||||||
|
}
|
||||||
|
db.mu.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveUser from the database. Returns True if the user was there, False
|
// RemoveUser from the database. Returns True if the user was there, False
|
||||||
// otherwise.
|
// otherwise.
|
||||||
func (db *DB) RemoveUser(name string) bool {
|
func (db *DB) RemoveUser(name string) bool {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// protoc-gen-go v1.28.1
|
// protoc-gen-go v1.30.0
|
||||||
// protoc v3.21.12
|
// protoc v3.21.12
|
||||||
// source: userdb.proto
|
// source: userdb.proto
|
||||||
|
|
||||||
@@ -73,8 +73,10 @@ type Password struct {
|
|||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
// Types that are assignable to Scheme:
|
// Types that are assignable to Scheme:
|
||||||
|
//
|
||||||
// *Password_Scrypt
|
// *Password_Scrypt
|
||||||
// *Password_Plain
|
// *Password_Plain
|
||||||
|
// *Password_Denied
|
||||||
Scheme isPassword_Scheme `protobuf_oneof:"scheme"`
|
Scheme isPassword_Scheme `protobuf_oneof:"scheme"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,6 +133,13 @@ func (x *Password) GetPlain() *Plain {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *Password) GetDenied() *Denied {
|
||||||
|
if x, ok := x.GetScheme().(*Password_Denied); ok {
|
||||||
|
return x.Denied
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type isPassword_Scheme interface {
|
type isPassword_Scheme interface {
|
||||||
isPassword_Scheme()
|
isPassword_Scheme()
|
||||||
}
|
}
|
||||||
@@ -143,10 +152,16 @@ type Password_Plain struct {
|
|||||||
Plain *Plain `protobuf:"bytes,3,opt,name=plain,proto3,oneof"`
|
Plain *Plain `protobuf:"bytes,3,opt,name=plain,proto3,oneof"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Password_Denied struct {
|
||||||
|
Denied *Denied `protobuf:"bytes,4,opt,name=denied,proto3,oneof"`
|
||||||
|
}
|
||||||
|
|
||||||
func (*Password_Scrypt) isPassword_Scheme() {}
|
func (*Password_Scrypt) isPassword_Scheme() {}
|
||||||
|
|
||||||
func (*Password_Plain) isPassword_Scheme() {}
|
func (*Password_Plain) isPassword_Scheme() {}
|
||||||
|
|
||||||
|
func (*Password_Denied) isPassword_Scheme() {}
|
||||||
|
|
||||||
type Scrypt struct {
|
type Scrypt struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
@@ -281,6 +296,44 @@ func (x *Plain) GetPassword() []byte {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Denied struct {
|
||||||
|
state protoimpl.MessageState
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Denied) Reset() {
|
||||||
|
*x = Denied{}
|
||||||
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_userdb_proto_msgTypes[4]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Denied) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Denied) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *Denied) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_userdb_proto_msgTypes[4]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use Denied.ProtoReflect.Descriptor instead.
|
||||||
|
func (*Denied) Descriptor() ([]byte, []int) {
|
||||||
|
return file_userdb_proto_rawDescGZIP(), []int{4}
|
||||||
|
}
|
||||||
|
|
||||||
var File_userdb_proto protoreflect.FileDescriptor
|
var File_userdb_proto protoreflect.FileDescriptor
|
||||||
|
|
||||||
var file_userdb_proto_rawDesc = []byte{
|
var file_userdb_proto_rawDesc = []byte{
|
||||||
@@ -294,27 +347,30 @@ var file_userdb_proto_rawDesc = []byte{
|
|||||||
0x03, 0x6b, 0x65, 0x79, 0x12, 0x26, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20,
|
0x03, 0x6b, 0x65, 0x79, 0x12, 0x26, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20,
|
||||||
0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x64, 0x62, 0x2e, 0x50, 0x61, 0x73,
|
0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x64, 0x62, 0x2e, 0x50, 0x61, 0x73,
|
||||||
0x73, 0x77, 0x6f, 0x72, 0x64, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01,
|
0x73, 0x77, 0x6f, 0x72, 0x64, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01,
|
||||||
0x22, 0x65, 0x0a, 0x08, 0x50, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x12, 0x28, 0x0a, 0x06,
|
0x22, 0x8f, 0x01, 0x0a, 0x08, 0x50, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x12, 0x28, 0x0a,
|
||||||
0x73, 0x63, 0x72, 0x79, 0x70, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x75,
|
0x06, 0x73, 0x63, 0x72, 0x79, 0x70, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e,
|
||||||
0x73, 0x65, 0x72, 0x64, 0x62, 0x2e, 0x53, 0x63, 0x72, 0x79, 0x70, 0x74, 0x48, 0x00, 0x52, 0x06,
|
0x75, 0x73, 0x65, 0x72, 0x64, 0x62, 0x2e, 0x53, 0x63, 0x72, 0x79, 0x70, 0x74, 0x48, 0x00, 0x52,
|
||||||
0x73, 0x63, 0x72, 0x79, 0x70, 0x74, 0x12, 0x25, 0x0a, 0x05, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x18,
|
0x06, 0x73, 0x63, 0x72, 0x79, 0x70, 0x74, 0x12, 0x25, 0x0a, 0x05, 0x70, 0x6c, 0x61, 0x69, 0x6e,
|
||||||
0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x64, 0x62, 0x2e, 0x50,
|
0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x64, 0x62, 0x2e,
|
||||||
0x6c, 0x61, 0x69, 0x6e, 0x48, 0x00, 0x52, 0x05, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x42, 0x08, 0x0a,
|
0x50, 0x6c, 0x61, 0x69, 0x6e, 0x48, 0x00, 0x52, 0x05, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x12, 0x28,
|
||||||
0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x22, 0x82, 0x01, 0x0a, 0x06, 0x53, 0x63, 0x72, 0x79,
|
0x0a, 0x06, 0x64, 0x65, 0x6e, 0x69, 0x65, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e,
|
||||||
0x70, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6c, 0x6f, 0x67, 0x4e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04,
|
0x2e, 0x75, 0x73, 0x65, 0x72, 0x64, 0x62, 0x2e, 0x44, 0x65, 0x6e, 0x69, 0x65, 0x64, 0x48, 0x00,
|
||||||
0x52, 0x04, 0x6c, 0x6f, 0x67, 0x4e, 0x12, 0x0c, 0x0a, 0x01, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28,
|
0x52, 0x06, 0x64, 0x65, 0x6e, 0x69, 0x65, 0x64, 0x42, 0x08, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65,
|
||||||
0x05, 0x52, 0x01, 0x72, 0x12, 0x0c, 0x0a, 0x01, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52,
|
0x6d, 0x65, 0x22, 0x82, 0x01, 0x0a, 0x06, 0x53, 0x63, 0x72, 0x79, 0x70, 0x74, 0x12, 0x12, 0x0a,
|
||||||
0x01, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x6b, 0x65, 0x79, 0x4c, 0x65, 0x6e, 0x18, 0x04, 0x20, 0x01,
|
0x04, 0x6c, 0x6f, 0x67, 0x4e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x04, 0x6c, 0x6f, 0x67,
|
||||||
0x28, 0x05, 0x52, 0x06, 0x6b, 0x65, 0x79, 0x4c, 0x65, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x61,
|
0x4e, 0x12, 0x0c, 0x0a, 0x01, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x72, 0x12,
|
||||||
0x6c, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x73, 0x61, 0x6c, 0x74, 0x12, 0x1c,
|
0x0c, 0x0a, 0x01, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x70, 0x12, 0x16, 0x0a,
|
||||||
0x0a, 0x09, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28,
|
0x06, 0x6b, 0x65, 0x79, 0x4c, 0x65, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6b,
|
||||||
0x0c, 0x52, 0x09, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x22, 0x23, 0x0a, 0x05,
|
0x65, 0x79, 0x4c, 0x65, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x61, 0x6c, 0x74, 0x18, 0x05, 0x20,
|
||||||
0x50, 0x6c, 0x61, 0x69, 0x6e, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72,
|
0x01, 0x28, 0x0c, 0x52, 0x04, 0x73, 0x61, 0x6c, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x65, 0x6e, 0x63,
|
||||||
0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72,
|
0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x09, 0x65, 0x6e,
|
||||||
0x64, 0x42, 0x2c, 0x5a, 0x2a, 0x62, 0x6c, 0x69, 0x74, 0x69, 0x72, 0x69, 0x2e, 0x63, 0x6f, 0x6d,
|
0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x22, 0x23, 0x0a, 0x05, 0x50, 0x6c, 0x61, 0x69, 0x6e,
|
||||||
0x2e, 0x61, 0x72, 0x2f, 0x67, 0x6f, 0x2f, 0x63, 0x68, 0x61, 0x73, 0x71, 0x75, 0x69, 0x64, 0x2f,
|
0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x01, 0x20, 0x01,
|
||||||
0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x64, 0x62, 0x62,
|
0x28, 0x0c, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x22, 0x08, 0x0a, 0x06,
|
||||||
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
0x44, 0x65, 0x6e, 0x69, 0x65, 0x64, 0x42, 0x2c, 0x5a, 0x2a, 0x62, 0x6c, 0x69, 0x74, 0x69, 0x72,
|
||||||
|
0x69, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x61, 0x72, 0x2f, 0x67, 0x6f, 0x2f, 0x63, 0x68, 0x61, 0x73,
|
||||||
|
0x71, 0x75, 0x69, 0x64, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x75, 0x73,
|
||||||
|
0x65, 0x72, 0x64, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -329,24 +385,26 @@ func file_userdb_proto_rawDescGZIP() []byte {
|
|||||||
return file_userdb_proto_rawDescData
|
return file_userdb_proto_rawDescData
|
||||||
}
|
}
|
||||||
|
|
||||||
var file_userdb_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
|
var file_userdb_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
|
||||||
var file_userdb_proto_goTypes = []interface{}{
|
var file_userdb_proto_goTypes = []interface{}{
|
||||||
(*ProtoDB)(nil), // 0: userdb.ProtoDB
|
(*ProtoDB)(nil), // 0: userdb.ProtoDB
|
||||||
(*Password)(nil), // 1: userdb.Password
|
(*Password)(nil), // 1: userdb.Password
|
||||||
(*Scrypt)(nil), // 2: userdb.Scrypt
|
(*Scrypt)(nil), // 2: userdb.Scrypt
|
||||||
(*Plain)(nil), // 3: userdb.Plain
|
(*Plain)(nil), // 3: userdb.Plain
|
||||||
nil, // 4: userdb.ProtoDB.UsersEntry
|
(*Denied)(nil), // 4: userdb.Denied
|
||||||
|
nil, // 5: userdb.ProtoDB.UsersEntry
|
||||||
}
|
}
|
||||||
var file_userdb_proto_depIdxs = []int32{
|
var file_userdb_proto_depIdxs = []int32{
|
||||||
4, // 0: userdb.ProtoDB.users:type_name -> userdb.ProtoDB.UsersEntry
|
5, // 0: userdb.ProtoDB.users:type_name -> userdb.ProtoDB.UsersEntry
|
||||||
2, // 1: userdb.Password.scrypt:type_name -> userdb.Scrypt
|
2, // 1: userdb.Password.scrypt:type_name -> userdb.Scrypt
|
||||||
3, // 2: userdb.Password.plain:type_name -> userdb.Plain
|
3, // 2: userdb.Password.plain:type_name -> userdb.Plain
|
||||||
1, // 3: userdb.ProtoDB.UsersEntry.value:type_name -> userdb.Password
|
4, // 3: userdb.Password.denied:type_name -> userdb.Denied
|
||||||
4, // [4:4] is the sub-list for method output_type
|
1, // 4: userdb.ProtoDB.UsersEntry.value:type_name -> userdb.Password
|
||||||
4, // [4:4] is the sub-list for method input_type
|
5, // [5:5] is the sub-list for method output_type
|
||||||
4, // [4:4] is the sub-list for extension type_name
|
5, // [5:5] is the sub-list for method input_type
|
||||||
4, // [4:4] is the sub-list for extension extendee
|
5, // [5:5] is the sub-list for extension type_name
|
||||||
0, // [0:4] is the sub-list for field type_name
|
5, // [5:5] is the sub-list for extension extendee
|
||||||
|
0, // [0:5] is the sub-list for field type_name
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() { file_userdb_proto_init() }
|
func init() { file_userdb_proto_init() }
|
||||||
@@ -403,10 +461,23 @@ func file_userdb_proto_init() {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
file_userdb_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*Denied); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
file_userdb_proto_msgTypes[1].OneofWrappers = []interface{}{
|
file_userdb_proto_msgTypes[1].OneofWrappers = []interface{}{
|
||||||
(*Password_Scrypt)(nil),
|
(*Password_Scrypt)(nil),
|
||||||
(*Password_Plain)(nil),
|
(*Password_Plain)(nil),
|
||||||
|
(*Password_Denied)(nil),
|
||||||
}
|
}
|
||||||
type x struct{}
|
type x struct{}
|
||||||
out := protoimpl.TypeBuilder{
|
out := protoimpl.TypeBuilder{
|
||||||
@@ -414,7 +485,7 @@ func file_userdb_proto_init() {
|
|||||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||||
RawDescriptor: file_userdb_proto_rawDesc,
|
RawDescriptor: file_userdb_proto_rawDesc,
|
||||||
NumEnums: 0,
|
NumEnums: 0,
|
||||||
NumMessages: 5,
|
NumMessages: 6,
|
||||||
NumExtensions: 0,
|
NumExtensions: 0,
|
||||||
NumServices: 0,
|
NumServices: 0,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ message Password {
|
|||||||
oneof scheme {
|
oneof scheme {
|
||||||
Scrypt scrypt = 2;
|
Scrypt scrypt = 2;
|
||||||
Plain plain = 3;
|
Plain plain = 3;
|
||||||
|
Denied denied = 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,3 +28,5 @@ message Scrypt {
|
|||||||
message Plain {
|
message Plain {
|
||||||
bytes password = 1;
|
bytes password = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message Denied { }
|
||||||
|
|||||||
@@ -122,24 +122,27 @@ func TestWrite(t *testing.T) {
|
|||||||
t.Fatalf("expected %v, got %v", emptyDB, db)
|
t.Fatalf("expected %v, got %v", emptyDB, db)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add two users, write, and load again.
|
// Add users, write, and load again.
|
||||||
if err := db.AddUser("user1", "passwd1"); err != nil {
|
if err := db.AddUser("user1", "passwd1"); err != nil {
|
||||||
t.Fatalf("failed to add user1: %v", err)
|
t.Fatalf("failed to add user1: %v", err)
|
||||||
}
|
}
|
||||||
if err := db.AddUser("ñoño", "añicos"); err != nil {
|
if err := db.AddUser("ñoño", "añicos"); err != nil {
|
||||||
t.Fatalf("failed to add ñoño: %v", err)
|
t.Fatalf("failed to add ñoño: %v", err)
|
||||||
}
|
}
|
||||||
|
if err := db.AddDeniedUser("ñaca"); err != nil {
|
||||||
|
t.Fatalf("failed to add ñaca: %v", err)
|
||||||
|
}
|
||||||
if err := db.Write(); err != nil {
|
if err := db.Write(); err != nil {
|
||||||
t.Fatalf("error writing database: %v", err)
|
t.Fatalf("error writing database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
db = mustLoad(t, fname)
|
db = mustLoad(t, fname)
|
||||||
for _, name := range []string{"user1", "ñoño"} {
|
for _, name := range []string{"user1", "ñoño", "ñaca"} {
|
||||||
if !db.Exists(name) {
|
if !db.Exists(name) {
|
||||||
t.Errorf("user %q not in database", name)
|
t.Errorf("user %q not in database", name)
|
||||||
}
|
}
|
||||||
if db.db.Users[name].GetScheme() == nil {
|
if db.db.Users[name].GetScheme() == nil {
|
||||||
t.Errorf("user %q not using scrypt: %#v", name, db.db.Users[name])
|
t.Errorf("user %q missing scheme: %#v", name, db.db.Users[name])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,6 +156,8 @@ func TestWrite(t *testing.T) {
|
|||||||
{"user1", "passwd12", false},
|
{"user1", "passwd12", false},
|
||||||
{"ñoño", "añicos", true},
|
{"ñoño", "añicos", true},
|
||||||
{"ñoño", "anicos", false},
|
{"ñoño", "anicos", false},
|
||||||
|
{"ñaca", "", false},
|
||||||
|
{"ñaca", "lalala", false},
|
||||||
{"notindb", "something", false},
|
{"notindb", "something", false},
|
||||||
{"", "", false},
|
{"", "", false},
|
||||||
{" ", " ", false},
|
{" ", " ", false},
|
||||||
@@ -202,6 +207,11 @@ func TestInvalidUsername(t *testing.T) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("AddUser(%q) worked, expected it to fail", name)
|
t.Errorf("AddUser(%q) worked, expected it to fail", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = db.AddDeniedUser(name)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("AddDeniedUser(%q) worked, expected it to fail", name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,6 +245,24 @@ func TestPlainScheme(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test the denied scheme.
|
||||||
|
func TestDeniedScheme(t *testing.T) {
|
||||||
|
fname := mustCreateDB(t, "")
|
||||||
|
defer removeIfSuccessful(t, fname)
|
||||||
|
db := mustLoad(t, fname)
|
||||||
|
|
||||||
|
db.db.Users["user"] = &Password{Scheme: &Password_Denied{}}
|
||||||
|
err := db.Write()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Write failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db = mustLoad(t, fname)
|
||||||
|
if db.Authenticate("user", "anything") {
|
||||||
|
t.Errorf("denied authentication worked but it shouldn't")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestReload(t *testing.T) {
|
func TestReload(t *testing.T) {
|
||||||
content := "users:< key: 'u1' value:< plain:< password: 'pass' >>>"
|
content := "users:< key: 'u1' value:< plain:< password: 'pass' >>>"
|
||||||
fname := mustCreateDB(t, content)
|
fname := mustCreateDB(t, content)
|
||||||
@@ -326,4 +354,12 @@ func TestExists(t *testing.T) {
|
|||||||
if !db.Exists("user") {
|
if !db.Exists("user") {
|
||||||
t.Errorf("known user does not exist")
|
t.Errorf("known user does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := db.AddDeniedUser("denieduser"); err != nil {
|
||||||
|
t.Fatalf("error adding user: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !db.Exists("denieduser") {
|
||||||
|
t.Errorf("known (denied) user does not exist")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user