| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package pgp |
| 18 | |
| 19 | // Implementation for the "relic sign-pgp" and "relic remote sign-pgp" |
| 20 | // commands, that sort of looks like gpg arguments so it can be used where gpg |
| 21 | // is. This just transforms the "compatible" arguments into an ordinary sign |
| 22 | // command and calls it. |
| 23 | |
| 24 | import ( |
| 25 | "errors" |
| 26 | "strings" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/cmdline/shared" |
| 29 | |
| 30 | "github.com/spf13/cobra" |
| 31 | "github.com/spf13/pflag" |
| 32 | ) |
| 33 | |
| 34 | var ( |
| 35 | argDigest string |
| 36 | argOutput string |
| 37 | argPgpUser string |
| 38 | argPgpArmor bool |
| 39 | argPgpDetached bool |
| 40 | argPgpClearsign bool |
| 41 | argPgpTextMode bool |
| 42 | ) |
| 43 | |
| 44 | func AddCompatFlags(cmd *cobra.Command) { |
| 45 | flags := cmd.Flags() |
| 46 | flags.StringVarP(&argPgpUser, "local-user", "u", "", "Specify keyname or cfgfile:keyname") |
| 47 | flags.StringVarP(&argOutput, "output", "o", "", "Write output to file") |
| 48 | flags.BoolVarP(&argPgpArmor, "armor", "a", false, "Create ASCII armored output") |
| 49 | flags.BoolVarP(&argPgpTextMode, "textmode", "t", false, "Sign in CRLF canonical text form") |
| 50 | flags.BoolVarP(&argPgpDetached, "detach-sign", "b", false, "Create a detached signature") |
| 51 | flags.BoolVar(&argPgpClearsign, "clearsign", false, "Create a cleartext signature") |
| 52 | flags.StringVar(&argDigest, "digest-algo", "", "Digest algorithm") |
| 53 | |
| 54 | flags.BoolP("sign", "s", false, "(ignored)") |
| 55 | flags.BoolP("verbose", "v", false, "(ignored)") |
| 56 | flags.Bool("no-armor", false, "(ignored)") |
| 57 | flags.Bool("no-verbose", false, "(ignored)") |
| 58 | flags.BoolP("quiet", "q", false, "(ignored)") |
| 59 | flags.Bool("no-secmem-warning", false, "(ignored)") |
| 60 | flags.String("status-fd", "", "(ignored)") |
| 61 | flags.String("logger-fd", "", "(ignored)") |
| 62 | flags.String("attribute-fd", "", "(ignored)") |
| 63 | } |
| 64 | |
| 65 | func CallCmd(src, dest *cobra.Command, args []string) error { |
| 66 | if argPgpUser == "" { |
| 67 | return errors.New("-u must be set to a keyname or cfgpath:keyname") |
| 68 | } |
| 69 | setFlag(dest.Flags(), "sig-type", "pgp") |
| 70 | idx := strings.LastIndex(argPgpUser, ":") |
| 71 | if idx <= 0 { |
| 72 | setFlag(dest.Flags(), "key", argPgpUser) |
| 73 | } else { |
| 74 | setFlag(shared.RootCmd.PersistentFlags(), "config", argPgpUser[:idx]) |
| 75 | setFlag(dest.Flags(), "key", argPgpUser[idx+1:]) |
| 76 | } |
| 77 | if len(args) == 0 { |
| 78 | setFlag(dest.Flags(), "file", "-") |
| 79 | } else if len(args) == 1 { |
| 80 | setFlag(dest.Flags(), "file", args[0]) |
| 81 | } else { |
| 82 | return errors.New("expected 0 or 1 argument") |
| 83 | } |
| 84 | if argOutput == "" { |
| 85 | argOutput = "-" |
| 86 | } |
| 87 | setFlag(dest.Flags(), "output", argOutput) |
| 88 | if argPgpArmor { |
| 89 | setFlag(dest.Flags(), "armor", "true") |
| 90 | } |
| 91 | if argPgpTextMode { |
| 92 | setFlag(dest.Flags(), "textmode", "true") |
| 93 | } |
| 94 | if argPgpClearsign { |
| 95 | setFlag(dest.Flags(), "clearsign", "true") |
| 96 | } else if !argPgpDetached { |
| 97 | setFlag(dest.Flags(), "inline", "true") |
| 98 | } |
| 99 | if argDigest != "" { |
| 100 | setFlag(dest.Flags(), "digest", argDigest) |
| 101 | } |
| 102 | return dest.RunE(dest, []string{}) |
| 103 | } |
| 104 | |
| 105 | func setFlag(flags *pflag.FlagSet, name, value string) { |
| 106 | if err := flags.Set(name, value); err != nil { |
| 107 | panic(err) |
| 108 | } |
| 109 | } |