github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/permission.go (about)

     1  /*
     2  	Copyright 2020 The pdfcpu Authors.
     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 api
    18  
    19  import (
    20  	"io"
    21  	"os"
    22  
    23  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
    24  	"github.com/pkg/errors"
    25  )
    26  
    27  // Permissions returns user access permissions for rs.
    28  func Permissions(rs io.ReadSeeker, conf *model.Configuration) (int, error) {
    29  	if rs == nil {
    30  		return 0, errors.New("pdfcpu: Permissions: missing rs")
    31  	}
    32  
    33  	if conf == nil {
    34  		conf = model.NewDefaultConfiguration()
    35  	}
    36  	conf.Cmd = model.LISTPERMISSIONS
    37  
    38  	ctx, err := ReadValidateAndOptimize(rs, conf)
    39  	if err != nil {
    40  		return 0, err
    41  	}
    42  
    43  	p := 0
    44  	if ctx.E != nil {
    45  		p = ctx.E.P
    46  	}
    47  
    48  	return p, nil
    49  }
    50  
    51  // SetPermissions sets user access permissions.
    52  // inFile has to be encrypted.
    53  // A configuration containing the current passwords is required.
    54  func SetPermissions(rs io.ReadSeeker, w io.Writer, conf *model.Configuration) error {
    55  	if rs == nil {
    56  		return errors.New("pdfcpu: SetPermissions: missing rs")
    57  	}
    58  
    59  	if conf == nil {
    60  		return errors.New("pdfcpu: missing configuration for setting permissions")
    61  	}
    62  	conf.Cmd = model.SETPERMISSIONS
    63  
    64  	ctx, err := ReadValidateAndOptimize(rs, conf)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	return WriteContext(ctx, w)
    70  }
    71  
    72  // SetPermissionsFile sets inFile's user access permissions.
    73  // inFile has to be encrypted.
    74  // A configuration containing the current passwords is required.
    75  func SetPermissionsFile(inFile, outFile string, conf *model.Configuration) (err error) {
    76  	if conf == nil {
    77  		return errors.New("pdfcpu: missing configuration for setting permissions")
    78  	}
    79  
    80  	var f1, f2 *os.File
    81  
    82  	if f1, err = os.Open(inFile); err != nil {
    83  		return err
    84  	}
    85  
    86  	tmpFile := inFile + ".tmp"
    87  	if outFile != "" && inFile != outFile {
    88  		tmpFile = outFile
    89  		logWritingTo(outFile)
    90  	} else {
    91  		logWritingTo(inFile)
    92  	}
    93  	if f2, err = os.Create(tmpFile); err != nil {
    94  		return err
    95  	}
    96  
    97  	defer func() {
    98  		if err != nil {
    99  			f2.Close()
   100  			f1.Close()
   101  			os.Remove(tmpFile)
   102  			return
   103  		}
   104  		if err = f2.Close(); err != nil {
   105  			return
   106  		}
   107  		if err = f1.Close(); err != nil {
   108  			return
   109  		}
   110  		if outFile == "" || inFile == outFile {
   111  			err = os.Rename(tmpFile, inFile)
   112  		}
   113  	}()
   114  
   115  	return SetPermissions(f1, f2, conf)
   116  }
   117  
   118  // GetPermissions returns the permissions for rs.
   119  func GetPermissions(rs io.ReadSeeker, conf *model.Configuration) (*int16, error) {
   120  	if rs == nil {
   121  		return nil, errors.New("pdfcpu: GetPermissions: missing rs")
   122  	}
   123  
   124  	if conf == nil {
   125  		conf = model.NewDefaultConfiguration()
   126  	}
   127  	// No cmd available.
   128  
   129  	ctx, err := ReadAndValidate(rs, conf)
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	if ctx.E == nil {
   135  		// Full access - permissions don't apply.
   136  		return nil, nil
   137  	}
   138  	p := int16(ctx.E.P)
   139  
   140  	return &p, nil
   141  }
   142  
   143  // GetPermissionsFile returns the permissions for inFile.
   144  func GetPermissionsFile(inFile string, conf *model.Configuration) (*int16, error) {
   145  	f, err := os.Open(inFile)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	defer f.Close()
   150  
   151  	return GetPermissions(f, conf)
   152  }